forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
448 lines (374 loc) · 10.5 KB
/
tree.go
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package ssz
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
)
// Proof represents a merkle proof against a general index.
type Proof struct {
Index int
Leaf []byte
Hashes [][]byte
}
// Multiproof represents a merkle proof of several leaves.
type Multiproof struct {
Indices []int
Leaves [][]byte
Hashes [][]byte
}
// Compress returns a new proof with zero hashes omitted.
// See `CompressedMultiproof` for more info.
func (p *Multiproof) Compress() *CompressedMultiproof {
compressed := &CompressedMultiproof{
Indices: p.Indices,
Leaves: p.Leaves,
Hashes: make([][]byte, 0, len(p.Hashes)),
ZeroLevels: make([]int, 0, len(p.Hashes)),
}
for _, h := range p.Hashes {
if l, ok := zeroHashLevels[string(h)]; ok {
compressed.ZeroLevels = append(compressed.ZeroLevels, l)
compressed.Hashes = append(compressed.Hashes, nil)
} else {
compressed.Hashes = append(compressed.Hashes, h)
}
}
return compressed
}
// CompressedMultiproof represents a compressed merkle proof of several leaves.
// Compression is achieved by omitting zero hashes (and their hashes). `ZeroLevels`
// contains information which helps the verifier fill in those hashes.
type CompressedMultiproof struct {
Indices []int
Leaves [][]byte
Hashes [][]byte
ZeroLevels []int // Stores the level for every omitted zero hash in the proof
}
// Decompress returns a new multiproof, filling in the omitted
// zero hashes. See `CompressedMultiProof` for more info.
func (c *CompressedMultiproof) Decompress() *Multiproof {
p := &Multiproof{
Indices: c.Indices,
Leaves: c.Leaves,
Hashes: make([][]byte, len(c.Hashes)),
}
zc := 0
for i, h := range c.Hashes {
if h == nil {
p.Hashes[i] = zeroHashes[c.ZeroLevels[zc]][:]
zc++
} else {
p.Hashes[i] = c.Hashes[i]
}
}
return p
}
// Node represents a node in the tree
// backing of a SSZ object.
type Node struct {
left *Node
right *Node
isEmpty bool
value []byte
}
func (n *Node) Show(maxDepth int) {
fmt.Printf("--- Show node ---\n")
n.show(0, maxDepth)
}
func (n *Node) show(depth int, maxDepth int) {
space := ""
for i := 0; i < depth; i++ {
space += "\t"
}
print := func(msgs ...string) {
for _, msg := range msgs {
fmt.Printf("%s%s", space, msg)
}
}
if n.left != nil || n.right != nil {
// leaf hash is the same as value
print("HASH: " + hex.EncodeToString(n.Hash()) + "\n")
}
if n.value != nil {
print("VALUE: " + hex.EncodeToString(n.value) + "\n")
}
if maxDepth > 0 {
if depth == maxDepth {
// only print hash if we are too deep
return
}
}
if n.left != nil {
print("LEFT: \n")
n.left.show(depth+1, maxDepth)
}
if n.right != nil {
print("RIGHT: \n")
n.right.show(depth+1, maxDepth)
}
}
// NewNodeWithValue initializes a leaf node.
func NewNodeWithValue(value []byte) *Node {
return &Node{left: nil, right: nil, value: value}
}
func NewEmptyNode(zeroOrderHash []byte) *Node {
return &Node{left: nil, right: nil, value: zeroOrderHash, isEmpty: true}
}
// NewNodeWithLR initializes a branch node.
func NewNodeWithLR(left, right *Node) *Node {
return &Node{left: left, right: right, value: nil}
}
// TreeFromChunks constructs a tree from leaf values.
// The number of leaves should be a power of 2.
func TreeFromChunks(chunks [][]byte) (*Node, error) {
numLeaves := len(chunks)
if !isPowerOfTwo(numLeaves) {
return nil, errors.New("Number of leaves should be a power of 2")
}
leaves := make([]*Node, numLeaves)
for i, c := range chunks {
leaves[i] = NewNodeWithValue(c)
}
return TreeFromNodes(leaves, numLeaves)
}
// TreeFromNodes constructs a tree from leaf nodes.
// This is useful for merging subtrees.
// The limit should be a power of 2.
// Adjacent sibling nodes will be filled with zero order hashes that have been precomputed based on the tree depth.
func TreeFromNodes(leaves []*Node, limit int) (*Node, error) {
numLeaves := len(leaves)
depth := floorLog2(limit)
zeroOrderHashes := getZeroOrderHashes(depth)
// there are no leaves, return a zero order hash node
if numLeaves == 0 {
return NewEmptyNode(zeroOrderHashes[0]), nil
}
// now we know numLeaves are at least 1.
// if the max leaf limit is 1, return the one leaf we have
if limit == 1 {
return leaves[0], nil
}
// if the max leaf limit is 2
if limit == 2 {
// but we only have 1 leaf, add a zero order hash as the right node
if numLeaves == 1 {
return NewNodeWithLR(leaves[0], NewEmptyNode(zeroOrderHashes[1])), nil
}
// otherwise return the two nodes we have
return NewNodeWithLR(leaves[0], leaves[1]), nil
}
if !isPowerOfTwo(limit) {
return nil, errors.New("number of leaves should be a power of 2")
}
leavesStart := powerTwo(depth)
leafIndex := numLeaves - 1
nodes := make(map[int]*Node)
nodesStartIndex := leavesStart
nodesEndIndex := nodesStartIndex + numLeaves - 1
// for each tree level
for k := depth; k >= 0; k-- {
for i := nodesEndIndex; i >= nodesStartIndex; i-- {
// leaf node, add to map
if k == depth {
nodes[i] = leaves[leafIndex]
leafIndex--
} else { // branch node, compute
leftIndex := i * 2
rightIndex := i*2 + 1
// both nodes are empty, unexpected condition
if nodes[leftIndex] == nil && nodes[rightIndex] == nil {
return nil, errors.New("unexpected empty right and left nodes")
}
// node with empty right node, add zero order hash as right node and mark right node as empty
if nodes[leftIndex] != nil && nodes[rightIndex] == nil {
nodes[i] = NewNodeWithLR(nodes[leftIndex], NewEmptyNode(zeroOrderHashes[k+1]))
}
// node with left and right child
if nodes[leftIndex] != nil && nodes[rightIndex] != nil {
nodes[i] = NewNodeWithLR(nodes[leftIndex], nodes[rightIndex])
}
}
}
nodesStartIndex = nodesStartIndex / 2
nodesEndIndex = int(math.Floor(float64(nodesEndIndex)) / 2)
}
rootNode := nodes[1]
if rootNode == nil {
return nil, errors.New("tree root node could not be computed")
}
return nodes[1], nil
}
func TreeFromNodesWithMixin(leaves []*Node, num, limit int) (*Node, error) {
if !isPowerOfTwo(limit) {
return nil, errors.New("size of tree should be a power of 2")
}
mainTree, err := TreeFromNodes(leaves, limit)
if err != nil {
return nil, err
}
// Mixin len
countLeaf := LeafFromUint64(uint64(num))
node := NewNodeWithLR(mainTree, countLeaf)
return node, nil
}
// Get fetches a node with the given general index.
func (n *Node) Get(index int) (*Node, error) {
pathLen := getPathLength(index)
cur := n
for i := pathLen - 1; i >= 0; i-- {
if isRight := getPosAtLevel(index, i); isRight {
cur = cur.right
} else {
cur = cur.left
}
if cur == nil {
return nil, errors.New("Node not found in tree")
}
}
return cur, nil
}
// Hash returns the hash of the subtree with the given Node as its root.
// If root has no children, it returns root's value (not its hash).
func (n *Node) Hash() []byte {
// TODO: handle special cases: empty root, one non-empty node
return hashNode(n)
}
func hashNode(n *Node) []byte {
if n.left == nil && n.right == nil {
return n.value
}
if n.left == nil {
panic("Tree incomplete")
}
if n.right.isEmpty {
result := hashFn(append(hashNode(n.left), n.right.value...))
n.value = result // Set the hash result on each node so that proofs can be generated for any level
return result
}
result := hashFn(append(hashNode(n.left), hashNode(n.right)...))
n.value = result
return result
}
// getZeroOrderHashes precomputes zero order hashes to create an easy map lookup
// for zero leafs and their parent nodes.
func getZeroOrderHashes(depth int) map[int][]byte {
zeroOrderHashes := make(map[int][]byte)
emptyValue := make([]byte, 32)
zeroOrderHashes[depth] = emptyValue
for i := depth - 1; i >= 0; i-- {
zeroOrderHashes[i] = hashFn(append(zeroOrderHashes[i+1], zeroOrderHashes[i+1]...))
}
return zeroOrderHashes
}
// Prove returns a list of sibling values and hashes needed
// to compute the root hash for a given general index.
func (n *Node) Prove(index int) (*Proof, error) {
pathLen := getPathLength(index)
proof := &Proof{Index: index}
hashes := make([][]byte, 0, pathLen)
cur := n
for i := pathLen - 1; i >= 0; i-- {
var siblingHash []byte
if isRight := getPosAtLevel(index, i); isRight {
siblingHash = hashNode(cur.left)
cur = cur.right
} else {
siblingHash = hashNode(cur.right)
cur = cur.left
}
hashes = append([][]byte{siblingHash}, hashes...)
if cur == nil {
return nil, errors.New("Node not found in tree")
}
}
proof.Hashes = hashes
proof.Leaf = cur.value
return proof, nil
}
func (n *Node) ProveMulti(indices []int) (*Multiproof, error) {
reqIndices := getRequiredIndices(indices)
proof := &Multiproof{Indices: indices, Leaves: make([][]byte, len(indices)), Hashes: make([][]byte, len(reqIndices))}
for i, gi := range indices {
node, err := n.Get(gi)
if err != nil {
return nil, err
}
proof.Leaves[i] = node.value
}
for i, gi := range reqIndices {
cur, err := n.Get(gi)
if err != nil {
return nil, err
}
proof.Hashes[i] = hashNode(cur)
}
return proof, nil
}
func LeafFromUint64(i uint64) *Node {
buf := make([]byte, 32)
binary.LittleEndian.PutUint64(buf[:8], i)
return NewNodeWithValue(buf)
}
func LeafFromUint32(i uint32) *Node {
buf := make([]byte, 32)
binary.LittleEndian.PutUint32(buf[:4], i)
return NewNodeWithValue(buf)
}
func LeafFromUint16(i uint16) *Node {
buf := make([]byte, 32)
binary.LittleEndian.PutUint16(buf[:2], i)
return NewNodeWithValue(buf)
}
func LeafFromUint8(i uint8) *Node {
buf := make([]byte, 32)
buf[0] = byte(i)
return NewNodeWithValue(buf)
}
func LeafFromBool(b bool) *Node {
buf := make([]byte, 32)
if b {
buf[0] = 1
}
return NewNodeWithValue(buf)
}
func LeafFromBytes(b []byte) *Node {
l := len(b)
if l > 32 {
panic("Unimplemented")
}
if l == 32 {
return NewNodeWithValue(b[:])
}
// < 32
return NewNodeWithValue(append(b, zeroBytes[:32-l]...))
}
func EmptyLeaf() *Node {
return NewNodeWithValue(zeroBytes[:32])
}
func LeavesFromUint64(items []uint64) []*Node {
if len(items) == 0 {
return []*Node{}
}
numLeaves := (len(items)*8 + 31) / 32
buf := make([]byte, numLeaves*32)
for i, v := range items {
binary.LittleEndian.PutUint64(buf[i*8:(i+1)*8], v)
}
leaves := make([]*Node, numLeaves)
for i := 0; i < numLeaves; i++ {
v := buf[i*32 : (i+1)*32]
leaves[i] = NewNodeWithValue(v)
}
return leaves
}
func isPowerOfTwo(n int) bool {
return (n & (n - 1)) == 0
}
func floorLog2(n int) int {
return int(math.Floor(math.Log2(float64(n))))
}
func powerTwo(n int) int {
return int(math.Pow(2, float64(n)))
}