From 0e9d12b27ba14d1815888bc7b18a07f2c3c4d813 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 11 Feb 2019 14:21:51 +0000 Subject: [PATCH 1/2] Fix SSZ merkleisation bug --- specs/simple-serialize.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/specs/simple-serialize.md b/specs/simple-serialize.md index 690974dde4..55b0988238 100644 --- a/specs/simple-serialize.md +++ b/specs/simple-serialize.md @@ -414,13 +414,15 @@ def merkle_hash(lst): # Leave large items alone chunkz = lst - # Tree-hash - while len(chunkz) > 1: - if len(chunkz) % 2 == 1: - chunkz.append(b'\x00' * SSZ_CHUNK_SIZE) + # Merkleise + def next_power_of_2(x): + return 1 if x == 0 else 2**(x - 1).bit_length() + for i in range(len(chunkz), next_power_of_2(len(chunkz))): + chunkz.append(b'\x00' * SSZ_CHUNK_SIZE) + while len(chunkz) > 1: chunkz = [hash(chunkz[i] + chunkz[i+1]) for i in range(0, len(chunkz), 2)] - # Return hash of root and length data + # Return hash of root and data length return hash(chunkz[0] + datalen) ``` From 14392ba3c505a3a5ac4712b88d0af30f9343901b Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 11 Feb 2019 08:34:40 -0800 Subject: [PATCH 2/2] Update simple-serialize.md --- specs/simple-serialize.md | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/simple-serialize.md b/specs/simple-serialize.md index 55b0988238..d084e810a2 100644 --- a/specs/simple-serialize.md +++ b/specs/simple-serialize.md @@ -417,6 +417,7 @@ def merkle_hash(lst): # Merkleise def next_power_of_2(x): return 1 if x == 0 else 2**(x - 1).bit_length() + for i in range(len(chunkz), next_power_of_2(len(chunkz))): chunkz.append(b'\x00' * SSZ_CHUNK_SIZE) while len(chunkz) > 1: