-
Notifications
You must be signed in to change notification settings - Fork 2
/
merkle_tree_tool.py
125 lines (112 loc) · 5.14 KB
/
merkle_tree_tool.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import hashlib
import base64
EMPTY_HASH = hashlib.sha256(b'').digest()
RIGHT_EMPTY_HASH_B64 = base64.b64encode(b'\xaa' + EMPTY_HASH)
LEFT_EMPTY_HASH_B64 = base64.b64encode(b'\xbb' + EMPTY_HASH)
class Node:
def __init__(self, height: int):
self.height = height
if height == 0:
self.left_sibling = None
self.right_sibling = None
self.value = hashlib.sha256(b'')
else:
self.left_sibling = Node(height-1)
self.right_sibling = Node(height-1)
self.value = hashlib.sha256(self.left_sibling.value.digest() + self.right_sibling.value.digest())
def get_value(self):
to_return = b''
if self.value != b'':
to_return = base64.b64encode(self.value.digest())
return f'b64:{to_return.decode()}'
def append(self, value):
if self.height == 0:
if self.value.digest() == EMPTY_HASH:
self.value = hashlib.sha256(value.encode('utf-8'))
return 0, [f'str:{value}']
else:
return -1, []
left = self.left_sibling.append(value)
if left[0] == 0:
sib = RIGHT_EMPTY_HASH_B64
if self.right_sibling.value.digest() != EMPTY_HASH:
self.value = hashlib.sha256(self.left_sibling.value.digest() + self.right_sibling.value.digest())
sib = base64.b64encode(b'\xaa' + self.right_sibling.value.digest())
else:
self.value = hashlib.sha256(self.left_sibling.value.digest() + EMPTY_HASH)
return 0, left[1] + [f'b64:{sib.decode()}']
right = self.right_sibling.append(value)
if right[0] == 0:
sib = base64.b64encode(b'\xbb' + self.left_sibling.value.digest())
self.value = hashlib.sha256(self.left_sibling.value.digest() + self.right_sibling.value.digest())
return 0, right[1] + [f'b64:{sib.decode()}']
return -1, []
def verify(self, value):
if self.height == 0:
if self.value.digest() == hashlib.sha256(value.encode('utf-8')).digest():
return 0, [f'str:{value}']
else:
return -1, []
left = self.left_sibling.verify(value)
if left[0] == 0:
sib = RIGHT_EMPTY_HASH_B64
if self.right_sibling.value.digest() != EMPTY_HASH:
sib = base64.b64encode(b'\xaa' + self.right_sibling.value.digest())
return 0, left[1] + [f'b64:{sib.decode()}']
right = self.right_sibling.verify(value)
if right[0] == 0:
sib = base64.b64encode(b'\xbb' + self.left_sibling.value.digest())
return 0, right[1] + [f'b64:{sib.decode()}']
return -1, []
def update(self, old_value, new_value):
if self.height == 0:
if self.value.digest() == hashlib.sha256(old_value.encode('utf-8')).digest():
self.value = hashlib.sha256(new_value.encode('utf-8'))
return 0, [f'str:{old_value}', f'str:{new_value}']
else:
return -1, []
left = self.left_sibling.update(old_value, new_value)
if left[0] == 0:
sib = RIGHT_EMPTY_HASH_B64
if self.right_sibling.value.digest() != EMPTY_HASH:
self.value = hashlib.sha256(self.left_sibling.value.digest() + self.right_sibling.value.digest())
sib = base64.b64encode(b'\xaa' + self.right_sibling.value.digest())
else:
self.value = hashlib.sha256(self.left_sibling.value.digest() + EMPTY_HASH)
return 0, left[1][:-1] + [f'b64:{sib.decode()}'] + left[1][-1:]
right = self.right_sibling.update(old_value, new_value)
if right[0] == 0:
sib = LEFT_EMPTY_HASH_B64
if self.left_sibling.value.digest() != EMPTY_HASH:
self.value = hashlib.sha256(self.left_sibling.value.digest() + self.right_sibling.value.digest())
sib = base64.b64encode(b'\xbb' + self.left_sibling.value.digest())
else:
self.value = hashlib.sha256(EMPTY_HASH + self.right_sibling.value.digest())
return 0, right[1][:-1] + [f'b64:{sib.decode()}'] + right[1][-1:]
return -1, []
class MerkleTree:
def __init__(self, height: int):
self.root = Node(height)
self.max_records = 2**height
self.size = 0
def append(self, value: str):
if self.size == self.max_records:
return []
ok, path = self.root.append(value)
if ok != 0:
return []
new_root = self.root.get_value()
args = ['str:append'] + path
return '--app-arg ' + ' --app-arg '.join([f'"{arg}"' for arg in args])
def verify(self, value: str):
ok, path = self.root.verify(value)
if ok != 0:
return []
args = ['str:verify'] + path
return '--app-arg ' + ' --app-arg '.join([f'"{arg}"' for arg in args])
def update(self, old_value, new_value):
ok, path = self.root.update(old_value, new_value)
if ok != 0:
return []
args = ['str:update'] + path
return '--app-arg ' + ' --app-arg '.join([f'"{arg}"' for arg in args])