-
Notifications
You must be signed in to change notification settings - Fork 44
/
tree.go
328 lines (277 loc) · 7.37 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
package ssz
import (
"encoding/binary"
"errors"
)
// 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
value []byte
}
// NewNodeWithValue initializes a leaf node.
func NewNodeWithValue(value []byte) *Node {
return &Node{left: nil, right: nil, value: value}
}
// 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] = &Node{left: nil, right: nil, value: c}
}
return TreeFromNodes(leaves)
}
// TreeFromNodes constructs a tree from leaf nodes.
// This is useful for merging subtrees.
// The number of leaves should be a power of 2.
func TreeFromNodes(leaves []*Node) (*Node, error) {
numLeaves := len(leaves)
if numLeaves == 1 {
return leaves[0], nil
}
if numLeaves == 2 {
return NewNodeWithLR(leaves[0], leaves[1]), nil
}
if !isPowerOfTwo(numLeaves) {
return nil, errors.New("Number of leaves should be a power of 2")
}
numNodes := numLeaves*2 - 1
nodes := make([]*Node, numNodes)
for i := numNodes; i > 0; i-- {
// Is a leaf
if i > numNodes-numLeaves {
nodes[i-1] = leaves[i-numLeaves]
} else {
// Is a branch node
nodes[i-1] = &Node{left: nodes[(i*2)-1], right: nodes[(i*2+1)-1], value: nil}
}
}
return nodes[0], nil
}
func TreeFromNodesWithMixin(leaves []*Node, num, limit int) (*Node, error) {
numLeaves := len(leaves)
if !isPowerOfTwo(limit) {
return nil, errors.New("Size of tree should be a power of 2")
}
allLeaves := make([]*Node, limit)
emptyLeaf := NewNodeWithValue(make([]byte, 32))
for i := 0; i < limit; i++ {
if i < numLeaves {
allLeaves[i] = leaves[i]
} else {
allLeaves[i] = emptyLeaf
}
}
mainTree, err := TreeFromNodes(allLeaves)
if err != nil {
return nil, err
}
// Mixin len
countLeaf := LeafFromUint64(uint64(num))
return NewNodeWithLR(mainTree, countLeaf), 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 {
// Leaf
if n.left == nil && n.right == nil {
return n.value
}
// Only one child
if n.left == nil || n.right == nil {
panic("Tree incomplete")
}
return hashFn(append(hashNode(n.left), hashNode(n.right)...))
}
// 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[:])
}
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
}