Debian Codegraph @

cachedtree_test.go

raw symbols SCIP
1 package merkletree
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "testing"
7 )
8
9 // addSubTree will create a subtree of the desired height using the dataSeed to
10 // seed the data. addSubTree will add the data created in the subtree to the
11 // Tree as well. The tree must have the proveIndex set separately.
12 func addSubTree(height uint64, dataSeed []byte, subtreeProveIndex uint64, fullTree *Tree) (subTree *Tree) {
13 data := sum(sha256.New(), dataSeed)
14 leaves := 1 << height
15
16 subTree = New(sha256.New())
17 err := subTree.SetIndex(subtreeProveIndex)
18 if err != nil {
19 panic(err)
20 }
21
22 for i := 0; i < leaves; i++ {
23 subTree.Push(data)
24 fullTree.Push(data)
25 data = sum(sha256.New(), data)
26 }
27 return subTree
28 }
29
30 // TestCachedTreeConstruction checks that a CachedTree will correctly build to
31 // the same merkle root as the Tree when using caches at various heights and
32 // lengths.
33 func TestCachedTreeConstruction(t *testing.T) {
34 arbData := [][]byte{
35 []byte{1},
36 []byte{2},
37 []byte{3},
38 []byte{4},
39 []byte{5},
40 []byte{6},
41 []byte{7},
42 []byte{8},
43 }
44
45 // Test that a CachedTree with no elements will return the same value as a
46 // tree with no elements.
47 tree := New(sha256.New())
48 cachedTree := NewCachedTree(sha256.New(), 0)
49 if !bytes.Equal(tree.Root(), cachedTree.Root()) {
50 t.Error("empty Tree and empty CachedTree do not match")
51 }
52
53 // Try comparing the root of a cached tree with one element, where the
54 // cache height is 0.
55 tree = New(sha256.New())
56 cachedTree = NewCachedTree(sha256.New(), 0)
57 tree.Push(arbData[0])
58 subRoot := tree.Root()
59 cachedTree.Push(subRoot)
60 if !bytes.Equal(tree.Root(), cachedTree.Root()) {
61 t.Error("naive 1-height Tree and CachedTree do not match")
62 }
63
64 // Try comparing the root of a cached tree where the cache height is 0, and
65 // there are 3 cached elements.
66 tree = New(sha256.New())
67 subTree1 := New(sha256.New())
68 subTree2 := New(sha256.New())
69 cachedTree = NewCachedTree(sha256.New(), 0)
70 // Create 3 subtrees, one for caching each element.
71 subTree3 := New(sha256.New())
72 subTree1.Push(arbData[0])
73 subTree2.Push(arbData[1])
74 subTree3.Push(arbData[2])
75 // Pushed the cached roots into the cachedTree.
76 cachedTree.Push(subTree1.Root())
77 cachedTree.Push(subTree2.Root())
78 cachedTree.Push(subTree3.Root())
79 // Create a tree from the original elements.
80 tree.Push(arbData[0])
81 tree.Push(arbData[1])
82 tree.Push(arbData[2])
83 if !bytes.Equal(tree.Root(), cachedTree.Root()) {
84 t.Error("adding 3 len cacheing is causing problems")
85 }
86
87 // Try comparing the root of a cached tree where the cache height is 1, and
88 // there is 1 cached element.
89 tree = New(sha256.New())
90 subTree1 = New(sha256.New())
91 cachedTree = NewCachedTree(sha256.New(), 1)
92 // Build the subtrees to get the cached roots.
93 subTree1.Push(arbData[0])
94 subTree1.Push(arbData[1])
95 // Supply the cached roots to the cached tree.
96 cachedTree.Push(subTree1.Root())
97 // Compare against a formally built tree.
98 tree.Push(arbData[0])
99 tree.Push(arbData[1])
100 if !bytes.Equal(cachedTree.Root(), tree.Root()) {
101 t.Error("comparison has failed")
102 }
103
104 // Mirror the above test, but attempt a mutation, which should cause a
105 // failure.
106 tree = New(sha256.New())
107 subTree1 = New(sha256.New())
108 cachedTree = NewCachedTree(sha256.New(), 1)
109 // Build the subtrees to get the cached roots.
110 subTree1.Push(arbData[0])
111 subTree1.Push(arbData[1])
112 // Supply the cached roots to the cached tree.
113 cachedTree.Push(subTree1.Root())
114 // Compare against a formally built tree.
115 tree.Push(arbData[1]) // Intentional mistake.
116 tree.Push(arbData[1])
117 if bytes.Equal(cachedTree.Root(), tree.Root()) {
118 t.Error("comparison has succeeded despite mutation")
119 }
120
121 // Try comparing the root of a cached tree where the cache height is 2, and
122 // there are 5 cached elements.
123 tree = New(sha256.New())
124 subTree1 = New(sha256.New())
125 subTree2 = New(sha256.New())
126 cachedTree = NewCachedTree(sha256.New(), 2)
127 // Build the subtrees to get the cached roots.
128 subTree1.Push(arbData[0])
129 subTree1.Push(arbData[1])
130 subTree1.Push(arbData[2])
131 subTree1.Push(arbData[3])
132 subTree2.Push(arbData[4])
133 subTree2.Push(arbData[5])
134 subTree2.Push(arbData[6])
135 subTree2.Push(arbData[7])
136 // Supply the cached roots to the cached tree.
137 cachedTree.Push(subTree1.Root())
138 cachedTree.Push(subTree1.Root())
139 cachedTree.Push(subTree1.Root())
140 cachedTree.Push(subTree1.Root())
141 cachedTree.Push(subTree2.Root())
142 // Compare against a formally built tree.
143 for i := 0; i < 4; i++ {
144 for j := 0; j < 4; j++ {
145 tree.Push(arbData[j])
146 }
147 }
148 for i := 4; i < 8; i++ {
149 tree.Push(arbData[i])
150 }
151 if !bytes.Equal(cachedTree.Root(), tree.Root()) {
152 t.Error("comparison has failed")
153 }
154
155 // Try proving on an uninitialized cached tree.
156 cachedTree = NewCachedTree(sha256.New(), 0)
157 if err := cachedTree.SetIndex(0); err != nil {
158 t.Fatal(err)
159 }
160 _, proofSet, _, _ := cachedTree.Prove(nil)
161 if proofSet != nil {
162 t.Error("proving an empty set resulted in a valid proof?")
163 }
164 cachedTree = NewCachedTree(sha256.New(), 1)
165 if err := cachedTree.SetIndex(0); err != nil {
166 t.Fatal(err)
167 }
168 _, proofSet, _, _ = cachedTree.Prove(nil)
169 if proofSet != nil {
170 t.Error("proving an empty set resulted in a valid proof?")
171 }
172 cachedTree = NewCachedTree(sha256.New(), 2)
173 if err := cachedTree.SetIndex(0); err != nil {
174 t.Fatal(err)
175 }
176 _, proofSet, _, _ = cachedTree.Prove(nil)
177 if proofSet != nil {
178 t.Error("proving an empty set resulted in a valid proof?")
179 }
180
181 // Try creating a cached proof with cache height 1, 2 cached nodes, index
182 // 1.
183 tree = New(sha256.New())
184 subTree1 = New(sha256.New())
185 err := subTree1.SetIndex(1) // subtree index 0-1, corresponding to index 1.
186 if err != nil {
187 t.Fatal(err)
188 }
189 subTree2 = New(sha256.New())
190 cachedTree = NewCachedTree(sha256.New(), 1)
191 err = cachedTree.SetIndex(1)
192 if err != nil {
193 t.Fatal(err)
194 }
195 // Build the subtrees.
196 subTree1.Push(arbData[0])
197 subTree1.Push(arbData[1])
198 subTree2.Push(arbData[2])
199 subTree2.Push(arbData[3])
200 // Supply the cached root to the cached tree.
201 cachedTree.Push(subTree1.Root())
202 cachedTree.Push(subTree2.Root())
203 // Get the root from the tree, to have certainty about integrity.
204 tree.Push(arbData[0])
205 tree.Push(arbData[1])
206 tree.Push(arbData[2])
207 tree.Push(arbData[3])
208 root := tree.Root()
209 // Construct the proofs.
210 _, subTreeProofSet, _, _ := subTree1.Prove()
211 _, proofSet, proofIndex, numLeaves := cachedTree.Prove(subTreeProofSet)
212 if !VerifyProof(sha256.New(), root, proofSet, proofIndex, numLeaves) {
213 t.Error("proof was unsuccessful")
214 }
215
216 // Try creating a cached proof with cache height 0, 3 cached nodes, index
217 // 2.
218 tree = New(sha256.New())
219 subTree1 = New(sha256.New())
220 subTree2 = New(sha256.New())
221 subTree3 = New(sha256.New())
222 err = subTree3.SetIndex(0) // subtree index 2-0, corresponding to index 2.
223 if err != nil {
224 t.Fatal(err)
225 }
226 cachedTree = NewCachedTree(sha256.New(), 0)
227 err = cachedTree.SetIndex(2)
228 if err != nil {
229 t.Fatal(err)
230 }
231 // Build the subtrees.
232 subTree1.Push(arbData[0])
233 subTree2.Push(arbData[1])
234 subTree3.Push(arbData[2])
235 // Supply the cached root to the cached tree.
236 cachedTree.Push(subTree1.Root())
237 cachedTree.Push(subTree2.Root())
238 cachedTree.Push(subTree3.Root())
239 // Get the root from the tree, to have certainty about integrity.
240 tree.Push(arbData[0])
241 tree.Push(arbData[1])
242 tree.Push(arbData[2])
243 root = tree.Root()
244 // Construct the proofs.
245 _, subTreeProofSet, _, _ = subTree3.Prove()
246 _, proofSet, proofIndex, numLeaves = cachedTree.Prove(subTreeProofSet)
247 if !VerifyProof(sha256.New(), root, proofSet, proofIndex, numLeaves) {
248 t.Error("proof was unsuccessful")
249 }
250
251 // Try creating a cached proof with cache height 2, 3 cached nodes, index
252 // 6.
253 tree = New(sha256.New())
254 subTree1 = New(sha256.New())
255 subTree2 = New(sha256.New())
256 err = subTree2.SetIndex(2) // subtree index 1-2, corresponding to index 6.
257 if err != nil {
258 t.Fatal(err)
259 }
260 subTree3 = New(sha256.New())
261 cachedTree = NewCachedTree(sha256.New(), 2)
262 err = cachedTree.SetIndex(6)
263 if err != nil {
264 t.Fatal(err)
265 }
266 // Build the subtrees.
267 subTree1.Push(arbData[0])
268 subTree1.Push(arbData[1])
269 subTree1.Push(arbData[2])
270 subTree1.Push(arbData[3])
271 subTree2.Push(arbData[4])
272 subTree2.Push(arbData[5])
273 subTree2.Push(arbData[6])
274 subTree2.Push(arbData[7])
275 subTree3.Push(arbData[1])
276 subTree3.Push(arbData[3])
277 subTree3.Push(arbData[5])
278 subTree3.Push(arbData[7])
279 // Supply the cached root to the cached tree.
280 cachedTree.Push(subTree1.Root())
281 cachedTree.Push(subTree2.Root())
282 cachedTree.Push(subTree3.Root())
283 // Get the root from the tree, to have certainty about integrity.
284 tree.Push(arbData[0])
285 tree.Push(arbData[1])
286 tree.Push(arbData[2])
287 tree.Push(arbData[3])
288 tree.Push(arbData[4])
289 tree.Push(arbData[5])
290 tree.Push(arbData[6])
291 tree.Push(arbData[7])
292 tree.Push(arbData[1])
293 tree.Push(arbData[3])
294 tree.Push(arbData[5])
295 tree.Push(arbData[7])
296 root = tree.Root()
297 // Construct the proofs.
298 _, subTreeProofSet, _, _ = subTree2.Prove()
299 _, proofSet, proofIndex, numLeaves = cachedTree.Prove(subTreeProofSet)
300 if !VerifyProof(sha256.New(), root, proofSet, proofIndex, numLeaves) {
301 t.Error("proof was unsuccessful")
302 }
303 }
304
305 // TestCachedTreeConstructionAuto uses automation to build out a wide set of
306 // trees of different types to make sure the Cached Tree maintains consistency
307 // with the actual tree.
308 func TestCachedTreeConstructionAuto(t *testing.T) {
309 if testing.Short() {
310 t.SkipNow()
311 }
312
313 // Build out cached trees with up to 33 cached elements, each height 'h'.
314 for h := uint64(0); h < 5; h++ {
315 n := uint64(1) << h
316 for i := uint64(0); i < 35; i++ {
317 // Try creating a proof at each index.
318 for j := uint64(0); j < i*n; j++ {
319 tree := New(sha256.New())
320 err := tree.SetIndex(j)
321 if err != nil {
322 t.Fatal(err)
323 }
324 cachedTree := NewCachedTree(sha256.New(), h)
325 err = cachedTree.SetIndex(j)
326 if err != nil {
327 t.Fatal(err)
328 }
329 var subProof [][]byte
330
331 // Build out 'i' subtrees that form the components of the cached
332 // tree.
333 for k := uint64(0); k < i; k++ {
334 subtree := addSubTree(uint64(h), []byte{byte(k)}, j%n, tree)
335 cachedTree.Push(subtree.Root())
336 if !bytes.Equal(tree.Root(), cachedTree.Root()) {
337 t.Error("naive 1-height Tree and Cached tree roots do not match")
338 }
339
340 // Get the proof of the subtree
341 if k == j/n {
342 _, subProof, _, _ = subtree.Prove()
343 }
344 }
345
346 // Verify that the tree was built correctly.
347 treeRoot, treeProof, treeProofIndex, treeLeaves := tree.Prove()
348 if !VerifyProof(sha256.New(), treeRoot, treeProof, treeProofIndex, treeLeaves) {
349 t.Error("tree problems", i, j)
350 }
351
352 // Verify that the cached tree was built correctly.
353 cachedRoot, cachedProof, cachedProofIndex, cachedLeaves := cachedTree.Prove(subProof)
354 if !VerifyProof(sha256.New(), cachedRoot, cachedProof, cachedProofIndex, cachedLeaves) {
355 t.Error("cached tree problems", i, j)
356 }
357 }
358 }
359 }
360 }