Skip to content

Instantly share code, notes, and snippets.

@Warchant
Created April 10, 2017 00:39
Show Gist options
  • Select an option

  • Save Warchant/774ad06f7c3e0e17bad0359f4bae3411 to your computer and use it in GitHub Desktop.

Select an option

Save Warchant/774ad06f7c3e0e17bad0359f4bae3411 to your computer and use it in GitHub Desktop.
merkle tree test case
import sha3
import hashlib
def h(s):
return hashlib.sha3_256(s).digest()
# a tree with 4 leafs
d = [0] * (4*2-1)
# store 8 roots for 8 pushes
r = [0] * 8
# first push
d[3] = h(b'1')
r[0] = d[3]
# second push
d[4] = h(b'1')
d[1] = h(d[3] + d[4])
r[1] = d[1]
# third push
d[5] = h(b'1')
d[2] = d[5]
d[0] = h(d[1] + d[2])
r[2] = d[0]
# fourth push
d[6] = h(b'1')
d[2] = h(d[5] + d[6])
d[0] = h(d[1] + d[2])
r[3] = d[0]
# fifth push
d[3] = d[0]
d[4] = h(b'1')
d[1] = h(d[3] + d[4])
r[4] = d[1]
# sixth push
d[5] = h(b'1')
d[2] = d[5]
d[0] = h(d[1] + d[2])
r[5] = d[0]
# seventh push
d[6] = h(b'1')
d[2] = h(d[5] + d[6])
d[0] = h(d[1] + d[2])
r[6] = d[0]
# the last push
d[3] = d[0]
d[4] = h(b'1')
d[1] = h(d[3] + d[4])
r[7] = d[1]
for i in r:
print i.encode('hex')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment