-
Notifications
You must be signed in to change notification settings - Fork 18
/
ReferenceMerkleSet.py
328 lines (258 loc) · 9.2 KB
/
ReferenceMerkleSet.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from hashlib import blake2b
"""
A simple, confidence-inspiring Merkle Set standard
Advantages of this standard:
Low CPU requirements
Small proofs of inclusion/exclusion
Reasonably simple implementation
The main tricks in this standard are:
Uses blake2b because that has the best performance on 512 bit inputs
Skips repeated hashing of exactly two things even when they share prefix bits
Proofs support proving including/exclusion for a large number of values in
a single string. They're a serialization of a subset of the tree.
Proof format:
multiproof: subtree
subtree: middle or terminal or truncated or empty
middle: MIDDLE 1 subtree subtree
terminal: TERMINAL 1 hash 32
# If the sibling is empty truncated implies more than two children.
truncated: TRUNCATED 1 hash 32
empty: EMPTY 1
EMPTY: \x00
TERMINAL: \x01
MIDDLE: \x02
TRUNCATED: \x03
"""
EMPTY = bytes([0])
TERMINAL = bytes([1])
MIDDLE = bytes([2])
TRUNCATED = bytes([3])
BLANK = bytes([0] * 32)
prehashed = {}
def init_prehashed():
for x in [EMPTY, TERMINAL, MIDDLE]:
for y in [EMPTY, TERMINAL, MIDDLE]:
prehashed[x + y] = blake2b(bytes([0] * 30) + x + y)
init_prehashed()
def hashdown(mystr):
assert len(mystr) == 66
h = prehashed[bytes(mystr[0:1] + mystr[33:34])].copy()
h.update(mystr[1:33] + mystr[34:])
return h.digest()[:32]
def compress_root(mystr):
assert len(mystr) == 33
if mystr[0:1] == MIDDLE:
return mystr[1:]
if mystr[0:1] == EMPTY:
assert mystr[1:] == BLANK
return BLANK
return blake2b(mystr).digest()[:32]
def get_bit(mybytes, pos):
assert len(mybytes) == 32
return (mybytes[pos // 8] >> (7 - (pos % 8))) & 1
class ReferenceMerkleSet:
def __init__(self, root = None):
self.root = root
if root is None:
self.root = _empty
def get_root(self):
return compress_root(self.root.get_hash())
def add_already_hashed(self, toadd):
self.root = self.root.add(toadd, 0)
def remove_already_hashed(self, toremove):
self.root = self.root.remove(toremove, 0)
def is_included_already_hashed(self, tocheck):
proof = []
r = self.root.is_included(tocheck, 0, proof)
return r, b''.join(proof)
def _audit(self, hashes):
newhashes = []
self.root._audit(newhashes, [])
assert newhashes == sorted(newhashes)
class EmptyNode:
def __init__(self):
self.hash = BLANK
def get_hash(self):
return EMPTY + BLANK
def is_empty(self):
return True
def is_terminal(self):
return False
def is_double(self):
raise SetError()
def add(self, toadd, depth):
return TerminalNode(toadd)
def remove(self, toremove, depth):
return self
def is_included(self, tocheck, depth, p):
p.append(EMPTY)
return False
def other_included(self, tocheck, depth, p, collapse):
p.append(EMPTY)
def _audit(self, hashes, bits):
pass
_empty = EmptyNode()
class TerminalNode:
def __init__(self, hash, bits = None):
assert len(hash) == 32
self.hash = hash
if bits is not None:
self._audit([], bits)
def get_hash(self):
return TERMINAL + self.hash
def is_empty(self):
return False
def is_terminal(self):
return True
def is_double(self):
raise SetError()
def add(self, toadd, depth):
if toadd == self.hash:
return self
if toadd > self.hash:
return self._make_middle([self, TerminalNode(toadd)], depth)
else:
return self._make_middle([TerminalNode(toadd), self], depth)
def _make_middle(self, children, depth):
cbits = [get_bit(child.hash, depth) for child in children]
if cbits[0] != cbits[1]:
return MiddleNode(children)
nextvals = [None, None]
nextvals[cbits[0] ^ 1] = _empty
nextvals[cbits[0]] = self._make_middle(children, depth + 1)
return MiddleNode(nextvals)
def remove(self, toremove, depth):
if toremove == self.hash:
return _empty
return self
def is_included(self, tocheck, depth, proof):
proof.append(TERMINAL + self.hash)
return tocheck == self.hash
def other_included(self, tocheck, depth, p, collapse):
p.append(TERMINAL + self.hash)
def _audit(self, hashes, bits):
hashes.append(self.hash)
for pos, v in enumerate(bits):
assert get_bit(self.hash, pos) == v
class MiddleNode:
def __init__(self, children):
self.children = children
if children[0].is_empty() and children[1].is_double():
self.hash = children[1].hash
elif children[1].is_empty() and children[0].is_double():
self.hash = children[0].hash
else:
if children[0].is_empty() and (children[1].is_empty() or children[1].is_terminal()):
raise SetError()
if children[1].is_empty() and children[0].is_terminal():
raise SetError
if children[0].is_terminal() and children[1].is_terminal() and children[0].hash >= children[1].hash:
raise SetError
self.hash = hashdown(children[0].get_hash() + children[1].get_hash())
def get_hash(self):
return MIDDLE + self.hash
def is_empty(self):
return False
def is_terminal(self):
return False
def is_double(self):
if self.children[0].is_empty():
return self.children[1].is_double()
if self.children[1].is_empty():
return self.children[0].is_double()
return self.children[0].is_terminal() and self.children[1].is_terminal()
def add(self, toadd, depth):
bit = get_bit(toadd, depth)
child = self.children[bit]
newchild = child.add(toadd, depth + 1)
if newchild is child:
return self
newvals = [x for x in self.children]
newvals[bit] = newchild
return MiddleNode(newvals)
def remove(self, toremove, depth):
bit = get_bit(toremove, depth)
child = self.children[bit]
newchild = child.remove(toremove, depth + 1)
if newchild is child:
return self
otherchild = self.children[bit ^ 1]
if newchild.is_empty() and otherchild.is_terminal():
return otherchild
if newchild.is_terminal() and otherchild.is_empty():
return newchild
newvals = [x for x in self.children]
newvals[bit] = newchild
return MiddleNode(newvals)
def is_included(self, tocheck, depth, p):
p.append(MIDDLE)
if get_bit(tocheck, depth) == 0:
r = self.children[0].is_included(tocheck, depth + 1, p)
self.children[1].other_included(tocheck, depth + 1, p, not self.children[0].is_empty())
return r
else:
self.children[0].other_included(tocheck, depth + 1, p, not self.children[1].is_empty())
return self.children[1].is_included(tocheck, depth + 1, p)
def other_included(self, tocheck, depth, p, collapse):
if collapse or not self.is_double():
p.append(TRUNCATED + self.hash)
else:
self.is_included(tocheck, depth, p)
def _audit(self, hashes, bits):
self.children[0]._audit(hashes, bits + [0])
self.children[1]._audit(hashes, bits + [1])
class TruncatedNode:
def __init__(self, hash):
self.hash = hash
def get_hash(self):
return MIDDLE + self.hash
def is_empty(self):
return False
def is_terminal(self):
return False
def is_double(self):
return False
def is_included(self, tocheck, depth, p):
raise SetError()
def other_included(self, tocheck, depth, p, collapse):
p.append(TRUNCATED + self.hash)
class SetError(BaseException):
pass
def confirm_included(root, val, proof):
return confirm_not_included_already_hashed(root, sha256(val).digest(), proof)
def confirm_included_already_hashed(root, val, proof):
return _confirm(root, val, proof, True)
def confirm_not_included(root, val, proof):
return confirm_not_included_already_hashed(root, sha256(val).digest(), proof)
def confirm_not_included_already_hashed(root, val, proof):
return _confirm(root, val, proof, False)
def _confirm(root, val, proof, expected):
try:
p = deserialize_proof(proof)
if p.get_root() != root:
return False
r, junk = p.is_included_already_hashed(val)
return r == expected
except SetError:
return False
def deserialize_proof(proof):
try:
r, pos = _deserialize(proof, 0, [])
if pos != len(proof):
raise SetError()
return ReferenceMerkleSet(r)
except IndexError:
raise SetError()
def _deserialize(proof, pos, bits):
t = proof[pos:pos + 1]
if t == EMPTY:
return _empty, pos + 1
if t == TERMINAL:
return TerminalNode(proof[pos + 1:pos + 33], bits), pos + 33
if t == TRUNCATED:
return TruncatedNode(proof[pos + 1:pos + 33]), pos + 33
if t != MIDDLE:
raise SetError()
v0, pos = _deserialize(proof, pos + 1, bits + [0])
v1, pos = _deserialize(proof, pos, bits + [1])
return MiddleNode([v0, v1]), pos