Skip to content

Commit

Permalink
SSZ impl. rework started, see issue 1064
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed May 12, 2019
1 parent 9bd771f commit 761c9e5
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 332 deletions.
32 changes: 31 additions & 1 deletion test_libs/pyspec/eth2spec/utils/merkle_minimal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .hash_function import hash


zerohashes = [b'\x00' * 32]
ZERO_BYTES32 = b'\x00' * 32

zerohashes = [ZERO_BYTES32]
for layer in range(1, 32):
zerohashes.append(hash(zerohashes[layer - 1] + zerohashes[layer - 1]))

Expand All @@ -28,3 +30,31 @@ def get_merkle_proof(tree, item_index):
subindex = (item_index // 2**i) ^ 1
proof.append(tree[i][subindex] if subindex < len(tree[i]) else zerohashes[i])
return proof


def next_power_of_two(v: int) -> int:
"""
Get the next power of 2. (for 64 bit range ints)
Examples:
0 -> 0, 1 -> 1, 2 -> 2, 3 -> 4, 32 -> 32, 33 -> 64
"""
# effectively fill the bitstring (1 less, do not want to with ones, then increment for next power of 2.
v -= 1
v |= v >> (1 << 0)
v |= v >> (1 << 1)
v |= v >> (1 << 2)
v |= v >> (1 << 3)
v |= v >> (1 << 4)
v |= v >> (1 << 5)
v += 1
return v


def merkleize_chunks(chunks):
tree = chunks[::]
margin = next_power_of_two(len(chunks)) - len(chunks)
tree.extend([ZERO_BYTES32] * margin)
tree = [ZERO_BYTES32] * len(tree) + tree
for i in range(len(tree) // 2 - 1, 0, -1):
tree[i] = hash(tree[i * 2] + tree[i * 2 + 1])
return tree[1]
331 changes: 0 additions & 331 deletions test_libs/pyspec/eth2spec/utils/minimal_ssz.py

This file was deleted.

Loading

0 comments on commit 761c9e5

Please sign in to comment.