Skip to content

Commit 94f54b1

Browse files
tcoratgerclaude
andauthored
refactor(ssz): collapse identical hash_tree_root leaf handlers (leanEthereum#1037)
* refactor(ssz): collapse identical hash_tree_root leaf handlers Four singledispatch handlers for hash_tree_root had byte-identical bodies: the ones for unsigned integers, booleans, field elements, and fixed-size byte vectors all merkleized the packed encoded bytes. Collapse them into one handler registered against all four base types with stacked explicit registrations. Dispatch precedence is unchanged: the fixed byte-vector base is a subclass of the bytes builtin, so its instances still resolve to the new handler while plain bytes, bytearray, and memoryview keep their distinct handlers. The byte-list, bitvector, bitlist, vector, list, and container handlers are untouched because their bodies differ. Verified with just check (lint, format, types, spell, mdformat) and uv run fill --fork=lstar --clean -n auto: 539 vectors pass and the cross-seed determinism check is green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore(vulture): sync whitelist with collapsed hash_tree_root handler Collapsing the four identical leaf handlers into one left the whitelist naming the four removed functions and not the new one, so vulture flagged the new singledispatch handler as unused. Replace the four stale entries with the single new handler name. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e85dae7 commit 94f54b1

2 files changed

Lines changed: 8 additions & 21 deletions

File tree

src/lean_spec/spec/crypto/merkleization.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,13 @@ def hash_tree_root(value: object) -> Bytes32:
208208
raise TypeError(f"hash_tree_root: unsupported value type {type(value).__name__}")
209209

210210

211-
@hash_tree_root.register
212-
def _htr_uint(value: BaseUint) -> Bytes32:
213-
return merkleize(_pack_bytes(value.encode_bytes()))
214-
215-
216-
@hash_tree_root.register
217-
def _htr_boolean(value: Boolean) -> Bytes32:
218-
return merkleize(_pack_bytes(value.encode_bytes()))
219-
220-
221-
@hash_tree_root.register
222-
def _htr_fp(value: Fp) -> Bytes32:
211+
@hash_tree_root.register(BaseUint)
212+
@hash_tree_root.register(Boolean)
213+
@hash_tree_root.register(Fp)
214+
@hash_tree_root.register(BaseBytes)
215+
def _htr_packed_leaf(value: BaseUint | Boolean | Fp | BaseBytes) -> Bytes32:
216+
# Each of these encodes to a fixed-width byte string with no length prefix.
217+
# The root is the Merkle root of those bytes packed into 32-byte chunks.
223218
return merkleize(_pack_bytes(value.encode_bytes()))
224219

225220

@@ -228,11 +223,6 @@ def _htr_bytes(value: bytes) -> Bytes32:
228223
return merkleize(_pack_bytes(value))
229224

230225

231-
@hash_tree_root.register
232-
def _htr_bytevector(value: BaseBytes) -> Bytes32:
233-
return merkleize(_pack_bytes(value.encode_bytes()))
234-
235-
236226
@hash_tree_root.register
237227
def _htr_bytelist(value: BaseByteList) -> Bytes32:
238228
serialized_bytes = value.encode_bytes()

vulture_whitelist.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515

1616
# Single-dispatch handlers for the hash_tree_root generic function.
1717
# Dispatched by argument type, so they have no direct call site.
18-
_htr_uint
19-
_htr_boolean
20-
_htr_fp
18+
_htr_packed_leaf
2119
_htr_bytes
22-
_htr_bytevector
2320
_htr_bytelist
2421
_htr_bitvector_base
2522
_htr_bitlist_base

0 commit comments

Comments
 (0)