Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tree hash padding #35

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ssz/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def merkle_hash(input_items: Sequence[Any]) -> Hash32:

# Store length of list (to compensate for non-bijectiveness of padding)
data_length = len(input_items).to_bytes(32, "little")

if len(input_items) == 0:
# Handle empty list case
chunks = (b'\x00' * SSZ_CHUNK_SIZE,)
Expand All @@ -40,10 +39,14 @@ def merkle_hash(input_items: Sequence[Any]) -> Hash32:
items_per_chunk = SSZ_CHUNK_SIZE // len(input_items[0])

# Build a list of chunks based on the number of items in the chunk
chunks = tuple(
chunks_unpadded = (
b''.join(input_items[i:i + items_per_chunk])
for i in range(0, len(input_items), items_per_chunk)
)
chunks = tuple(
chunk.ljust(SSZ_CHUNK_SIZE, b"\x00")
for chunk in chunks_unpadded
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My instinct tells me it's faster in one for loop:

@to_tuple
def get_chunkz_new(input_items, items_per_chunk):
    for i in range(0, len(input_items), items_per_chunk):
        yield b''.join(input_items[i:i + items_per_chunk]).ljust(SSZ_CHUNK_SIZE, b"\x00")

(Although maybe not much performance improvement difference.)

else:
# Leave large items alone
chunks = input_items
Expand Down
10 changes: 5 additions & 5 deletions tests/tree_hash/test_merkle_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@
),
(
(b'\x01',),
b'\x01' + int(1).to_bytes(32, "little"),
b'\x01' + b'\x00' * 127 + int(1).to_bytes(32, "little"),
),
(
(b'\x01', b'\x01', b'\x01',),
b'\x01\x01\x01' + int(3).to_bytes(32, "little"),
b'\x01\x01\x01' + b'\x00' * 125 + int(3).to_bytes(32, "little"),
),
# two items in one chunk
(
(b'\x55' * 64, b'\x66' * 64, b'\x77' * 64,),
(
hash_eth2(b'\x55' * 64 + b'\x66' * 64 + b'\x77' * 64) +
hash_eth2(b'\x55' * 64 + b'\x66' * 64 + b'\x77' * 64 + b'\x00' * 64) +
int(3).to_bytes(32, "little")
),
),
(
(b'\x55' * 96, b'\x66' * 96, b'\x77' * 96, b'\x88' * 96),
(
hash_eth2((
hash_eth2(b'\x55' * 96 + b'\x66' * 96) +
hash_eth2(b'\x77' * 96 + b'\x88' * 96)
hash_eth2(b'\x55' * 96 + b'\x00' * 32 + b'\x66' * 96 + b'\x00' * 32) +
hash_eth2(b'\x77' * 96 + b'\x00' * 32 + b'\x88' * 96 + b'\x00' * 32)
)) +
int(4).to_bytes(32, "little")
),
Expand Down