This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
cachedtree_test.go
360 lines (339 loc) · 10.6 KB
/
cachedtree_test.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
package merkletree
import (
"bytes"
"crypto/sha256"
"testing"
)
// addSubTree will create a subtree of the desired height using the dataSeed to
// seed the data. addSubTree will add the data created in the subtree to the
// Tree as well. The tree must have the proveIndex set separately.
func addSubTree(height uint64, dataSeed []byte, subtreeProveIndex uint64, fullTree *Tree) (subTree *Tree) {
data := sum(sha256.New(), dataSeed)
leaves := 1 << height
subTree = New(sha256.New())
err := subTree.SetIndex(subtreeProveIndex)
if err != nil {
panic(err)
}
for i := 0; i < leaves; i++ {
subTree.Push(data)
fullTree.Push(data)
data = sum(sha256.New(), data)
}
return subTree
}
// TestCachedTreeConstruction checks that a CachedTree will correctly build to
// the same merkle root as the Tree when using caches at various heights and
// lengths.
func TestCachedTreeConstruction(t *testing.T) {
arbData := [][]byte{
[]byte{1},
[]byte{2},
[]byte{3},
[]byte{4},
[]byte{5},
[]byte{6},
[]byte{7},
[]byte{8},
}
// Test that a CachedTree with no elements will return the same value as a
// tree with no elements.
tree := New(sha256.New())
cachedTree := NewCachedTree(sha256.New(), 0)
if !bytes.Equal(tree.Root(), cachedTree.Root()) {
t.Error("empty Tree and empty CachedTree do not match")
}
// Try comparing the root of a cached tree with one element, where the
// cache height is 0.
tree = New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 0)
tree.Push(arbData[0])
subRoot := tree.Root()
cachedTree.Push(subRoot)
if !bytes.Equal(tree.Root(), cachedTree.Root()) {
t.Error("naive 1-height Tree and CachedTree do not match")
}
// Try comparing the root of a cached tree where the cache height is 0, and
// there are 3 cached elements.
tree = New(sha256.New())
subTree1 := New(sha256.New())
subTree2 := New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 0)
// Create 3 subtrees, one for caching each element.
subTree3 := New(sha256.New())
subTree1.Push(arbData[0])
subTree2.Push(arbData[1])
subTree3.Push(arbData[2])
// Pushed the cached roots into the cachedTree.
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree2.Root())
cachedTree.Push(subTree3.Root())
// Create a tree from the original elements.
tree.Push(arbData[0])
tree.Push(arbData[1])
tree.Push(arbData[2])
if !bytes.Equal(tree.Root(), cachedTree.Root()) {
t.Error("adding 3 len cacheing is causing problems")
}
// Try comparing the root of a cached tree where the cache height is 1, and
// there is 1 cached element.
tree = New(sha256.New())
subTree1 = New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 1)
// Build the subtrees to get the cached roots.
subTree1.Push(arbData[0])
subTree1.Push(arbData[1])
// Supply the cached roots to the cached tree.
cachedTree.Push(subTree1.Root())
// Compare against a formally built tree.
tree.Push(arbData[0])
tree.Push(arbData[1])
if !bytes.Equal(cachedTree.Root(), tree.Root()) {
t.Error("comparison has failed")
}
// Mirror the above test, but attempt a mutation, which should cause a
// failure.
tree = New(sha256.New())
subTree1 = New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 1)
// Build the subtrees to get the cached roots.
subTree1.Push(arbData[0])
subTree1.Push(arbData[1])
// Supply the cached roots to the cached tree.
cachedTree.Push(subTree1.Root())
// Compare against a formally built tree.
tree.Push(arbData[1]) // Intentional mistake.
tree.Push(arbData[1])
if bytes.Equal(cachedTree.Root(), tree.Root()) {
t.Error("comparison has succeeded despite mutation")
}
// Try comparing the root of a cached tree where the cache height is 2, and
// there are 5 cached elements.
tree = New(sha256.New())
subTree1 = New(sha256.New())
subTree2 = New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 2)
// Build the subtrees to get the cached roots.
subTree1.Push(arbData[0])
subTree1.Push(arbData[1])
subTree1.Push(arbData[2])
subTree1.Push(arbData[3])
subTree2.Push(arbData[4])
subTree2.Push(arbData[5])
subTree2.Push(arbData[6])
subTree2.Push(arbData[7])
// Supply the cached roots to the cached tree.
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree2.Root())
// Compare against a formally built tree.
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
tree.Push(arbData[j])
}
}
for i := 4; i < 8; i++ {
tree.Push(arbData[i])
}
if !bytes.Equal(cachedTree.Root(), tree.Root()) {
t.Error("comparison has failed")
}
// Try proving on an uninitialized cached tree.
cachedTree = NewCachedTree(sha256.New(), 0)
if err := cachedTree.SetIndex(0); err != nil {
t.Fatal(err)
}
_, proofSet, _, _ := cachedTree.Prove(nil)
if proofSet != nil {
t.Error("proving an empty set resulted in a valid proof?")
}
cachedTree = NewCachedTree(sha256.New(), 1)
if err := cachedTree.SetIndex(0); err != nil {
t.Fatal(err)
}
_, proofSet, _, _ = cachedTree.Prove(nil)
if proofSet != nil {
t.Error("proving an empty set resulted in a valid proof?")
}
cachedTree = NewCachedTree(sha256.New(), 2)
if err := cachedTree.SetIndex(0); err != nil {
t.Fatal(err)
}
_, proofSet, _, _ = cachedTree.Prove(nil)
if proofSet != nil {
t.Error("proving an empty set resulted in a valid proof?")
}
// Try creating a cached proof with cache height 1, 2 cached nodes, index
// 1.
tree = New(sha256.New())
subTree1 = New(sha256.New())
err := subTree1.SetIndex(1) // subtree index 0-1, corresponding to index 1.
if err != nil {
t.Fatal(err)
}
subTree2 = New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 1)
err = cachedTree.SetIndex(1)
if err != nil {
t.Fatal(err)
}
// Build the subtrees.
subTree1.Push(arbData[0])
subTree1.Push(arbData[1])
subTree2.Push(arbData[2])
subTree2.Push(arbData[3])
// Supply the cached root to the cached tree.
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree2.Root())
// Get the root from the tree, to have certainty about integrity.
tree.Push(arbData[0])
tree.Push(arbData[1])
tree.Push(arbData[2])
tree.Push(arbData[3])
root := tree.Root()
// Construct the proofs.
_, subTreeProofSet, _, _ := subTree1.Prove()
_, proofSet, proofIndex, numLeaves := cachedTree.Prove(subTreeProofSet)
if !VerifyProof(sha256.New(), root, proofSet, proofIndex, numLeaves) {
t.Error("proof was unsuccessful")
}
// Try creating a cached proof with cache height 0, 3 cached nodes, index
// 2.
tree = New(sha256.New())
subTree1 = New(sha256.New())
subTree2 = New(sha256.New())
subTree3 = New(sha256.New())
err = subTree3.SetIndex(0) // subtree index 2-0, corresponding to index 2.
if err != nil {
t.Fatal(err)
}
cachedTree = NewCachedTree(sha256.New(), 0)
err = cachedTree.SetIndex(2)
if err != nil {
t.Fatal(err)
}
// Build the subtrees.
subTree1.Push(arbData[0])
subTree2.Push(arbData[1])
subTree3.Push(arbData[2])
// Supply the cached root to the cached tree.
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree2.Root())
cachedTree.Push(subTree3.Root())
// Get the root from the tree, to have certainty about integrity.
tree.Push(arbData[0])
tree.Push(arbData[1])
tree.Push(arbData[2])
root = tree.Root()
// Construct the proofs.
_, subTreeProofSet, _, _ = subTree3.Prove()
_, proofSet, proofIndex, numLeaves = cachedTree.Prove(subTreeProofSet)
if !VerifyProof(sha256.New(), root, proofSet, proofIndex, numLeaves) {
t.Error("proof was unsuccessful")
}
// Try creating a cached proof with cache height 2, 3 cached nodes, index
// 6.
tree = New(sha256.New())
subTree1 = New(sha256.New())
subTree2 = New(sha256.New())
err = subTree2.SetIndex(2) // subtree index 1-2, corresponding to index 6.
if err != nil {
t.Fatal(err)
}
subTree3 = New(sha256.New())
cachedTree = NewCachedTree(sha256.New(), 2)
err = cachedTree.SetIndex(6)
if err != nil {
t.Fatal(err)
}
// Build the subtrees.
subTree1.Push(arbData[0])
subTree1.Push(arbData[1])
subTree1.Push(arbData[2])
subTree1.Push(arbData[3])
subTree2.Push(arbData[4])
subTree2.Push(arbData[5])
subTree2.Push(arbData[6])
subTree2.Push(arbData[7])
subTree3.Push(arbData[1])
subTree3.Push(arbData[3])
subTree3.Push(arbData[5])
subTree3.Push(arbData[7])
// Supply the cached root to the cached tree.
cachedTree.Push(subTree1.Root())
cachedTree.Push(subTree2.Root())
cachedTree.Push(subTree3.Root())
// Get the root from the tree, to have certainty about integrity.
tree.Push(arbData[0])
tree.Push(arbData[1])
tree.Push(arbData[2])
tree.Push(arbData[3])
tree.Push(arbData[4])
tree.Push(arbData[5])
tree.Push(arbData[6])
tree.Push(arbData[7])
tree.Push(arbData[1])
tree.Push(arbData[3])
tree.Push(arbData[5])
tree.Push(arbData[7])
root = tree.Root()
// Construct the proofs.
_, subTreeProofSet, _, _ = subTree2.Prove()
_, proofSet, proofIndex, numLeaves = cachedTree.Prove(subTreeProofSet)
if !VerifyProof(sha256.New(), root, proofSet, proofIndex, numLeaves) {
t.Error("proof was unsuccessful")
}
}
// TestCachedTreeConstructionAuto uses automation to build out a wide set of
// trees of different types to make sure the Cached Tree maintains consistency
// with the actual tree.
func TestCachedTreeConstructionAuto(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// Build out cached trees with up to 33 cached elements, each height 'h'.
for h := uint64(0); h < 5; h++ {
n := uint64(1) << h
for i := uint64(0); i < 35; i++ {
// Try creating a proof at each index.
for j := uint64(0); j < i*n; j++ {
tree := New(sha256.New())
err := tree.SetIndex(j)
if err != nil {
t.Fatal(err)
}
cachedTree := NewCachedTree(sha256.New(), h)
err = cachedTree.SetIndex(j)
if err != nil {
t.Fatal(err)
}
var subProof [][]byte
// Build out 'i' subtrees that form the components of the cached
// tree.
for k := uint64(0); k < i; k++ {
subtree := addSubTree(uint64(h), []byte{byte(k)}, j%n, tree)
cachedTree.Push(subtree.Root())
if !bytes.Equal(tree.Root(), cachedTree.Root()) {
t.Error("naive 1-height Tree and Cached tree roots do not match")
}
// Get the proof of the subtree
if k == j/n {
_, subProof, _, _ = subtree.Prove()
}
}
// Verify that the tree was built correctly.
treeRoot, treeProof, treeProofIndex, treeLeaves := tree.Prove()
if !VerifyProof(sha256.New(), treeRoot, treeProof, treeProofIndex, treeLeaves) {
t.Error("tree problems", i, j)
}
// Verify that the cached tree was built correctly.
cachedRoot, cachedProof, cachedProofIndex, cachedLeaves := cachedTree.Prove(subProof)
if !VerifyProof(sha256.New(), cachedRoot, cachedProof, cachedProofIndex, cachedLeaves) {
t.Error("cached tree problems", i, j)
}
}
}
}
}