-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproof.py
75 lines (61 loc) · 1.35 KB
/
proof.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# caculate the tree node
def treeNodeCount (leafCount):
count = 1
i = leafCount
while i > 1:
count += i
i = (i + 1) >> 1
return count
def treeWidth (n, h):
return (n + (1 << h) - 1) >> h
def merkleProof (tree, leaf):
try:
index = tree.index(leaf)
except Exception as e:
# raise RuntimeError(e)
return None
n = len(tree)
nodes = []
z = treeWidth(n, 1)
while z > 0:
if treeNodeCount(z) == n:
break
z -= 1
if z == 0:
raise RuntimeError('Unknown solution')
height = 0
i = 0
while i < n -1:
layerWidth = treeWidth(z, height)
height += 1
odd = index % 2
if odd != 0:
index -= 1
offset = i + index
left = tree[offset]
right = left if index == layerWidth - 1 else tree[offset + 1]
if i > 0:
if odd != 0:
nodes.append(left)
nodes.append(None)
else:
nodes.append(None)
nodes.append(right)
else:
nodes.append(left)
nodes.append(right)
index = int(index/2) | 0
i += layerWidth
nodes.append(tree[n - 1])
return nodes
def verify(proof, digestFn):
if proof == None:
return False
root = proof[len(proof) -1]
hash = root
for i in range(0, len(proof) -1, 2):
left = proof[i] or hash
right = proof[i + 1] or hash
data = left + right
hash = digestFn(data)
return hash == root