miden-crypto/src/merkle/store/tests.rs
src/merkle/store/tests.rsRaw
1use super::*;
2use crate::{
3 hash::rpo::Rpo256,
4 merkle::{int_to_node, MerklePathSet, MerkleTree, SimpleSmt},
5 Felt, Word, WORD_SIZE, ZERO,
6};
7
8#[cfg(feature = "std")]
9use std::error::Error;
10
11const KEYS4: [u64; 4] = [0, 1, 2, 3];
12const LEAVES4: [Word; 4] = [int_to_node(1), int_to_node(2), int_to_node(3), int_to_node(4)];
13const EMPTY: Word = [ZERO; WORD_SIZE];
14
15#[test]
16fn test_root_not_in_store() -> Result<(), MerkleError> {
17 let mtree = MerkleTree::new(LEAVES4.to_vec())?;
18 let store = MerkleStore::from(&mtree);
19 assert_eq!(
20 store.get_node(LEAVES4[0], NodeIndex::make(mtree.depth(), 0)),
21 Err(MerkleError::RootNotInStore(LEAVES4[0])),
22 "Leaf 0 is not a root"
23 );
24 assert_eq!(
25 store.get_path(LEAVES4[0], NodeIndex::make(mtree.depth(), 0)),
26 Err(MerkleError::RootNotInStore(LEAVES4[0])),
27 "Leaf 0 is not a root"
28 );
29
30 Ok(())
31}
32
33#[test]
34fn test_merkle_tree() -> Result<(), MerkleError> {
35 let mtree = MerkleTree::new(LEAVES4.to_vec())?;
36 let store = MerkleStore::from(&mtree);
37
38 // STORE LEAVES ARE CORRECT ==============================================================
39 // checks the leaves in the store corresponds to the expected values
40 assert_eq!(
41 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 0)),
42 Ok(LEAVES4[0]),
43 "node 0 must be in the tree"
44 );
45 assert_eq!(
46 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 1)),
47 Ok(LEAVES4[1]),
48 "node 1 must be in the tree"
49 );
50 assert_eq!(
51 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 2)),
52 Ok(LEAVES4[2]),
53 "node 2 must be in the tree"
54 );
55 assert_eq!(
56 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 3)),
57 Ok(LEAVES4[3]),
58 "node 3 must be in the tree"
59 );
60
61 // STORE LEAVES MATCH TREE ===============================================================
62 // sanity check the values returned by the store and the tree
63 assert_eq!(
64 mtree.get_node(NodeIndex::make(mtree.depth(), 0)),
65 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 0)),
66 "node 0 must be the same for both MerkleTree and MerkleStore"
67 );
68 assert_eq!(
69 mtree.get_node(NodeIndex::make(mtree.depth(), 1)),
70 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 1)),
71 "node 1 must be the same for both MerkleTree and MerkleStore"
72 );
73 assert_eq!(
74 mtree.get_node(NodeIndex::make(mtree.depth(), 2)),
75 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 2)),
76 "node 2 must be the same for both MerkleTree and MerkleStore"
77 );
78 assert_eq!(
79 mtree.get_node(NodeIndex::make(mtree.depth(), 3)),
80 store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 3)),
81 "node 3 must be the same for both MerkleTree and MerkleStore"
82 );
83
84 // STORE MERKLE PATH MATCHS ==============================================================
85 // assert the merkle path returned by the store is the same as the one in the tree
86 let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 0)).unwrap();
87 assert_eq!(
88 LEAVES4[0], result.value,
89 "Value for merkle path at index 0 must match leaf value"
90 );
91 assert_eq!(
92 mtree.get_path(NodeIndex::make(mtree.depth(), 0)),
93 Ok(result.path),
94 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
95 );
96
97 let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 1)).unwrap();
98 assert_eq!(
99 LEAVES4[1], result.value,
100 "Value for merkle path at index 0 must match leaf value"
101 );
102 assert_eq!(
103 mtree.get_path(NodeIndex::make(mtree.depth(), 1)),
104 Ok(result.path),
105 "merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
106 );
107
108 let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 2)).unwrap();
109 assert_eq!(
110 LEAVES4[2], result.value,
111 "Value for merkle path at index 0 must match leaf value"
112 );
113 assert_eq!(
114 mtree.get_path(NodeIndex::make(mtree.depth(), 2)),
115 Ok(result.path),
116 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
117 );
118
119 let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 3)).unwrap();
120 assert_eq!(
121 LEAVES4[3], result.value,
122 "Value for merkle path at index 0 must match leaf value"
123 );
124 assert_eq!(
125 mtree.get_path(NodeIndex::make(mtree.depth(), 3)),
126 Ok(result.path),
127 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
128 );
129
130 Ok(())
131}
132
133#[test]
134fn test_empty_roots() {
135 let store = MerkleStore::default();
136 let mut root = RpoDigest::new(EMPTY);
137
138 for depth in 0..255 {
139 root = Rpo256::merge(&[root; 2]);
140 assert!(
141 store.get_node(root.into(), NodeIndex::make(0, 0)).is_ok(),
142 "The root of the empty tree of depth {depth} must be registered"
143 );
144 }
145}
146
147#[test]
148fn test_leaf_paths_for_empty_trees() -> Result<(), MerkleError> {
149 let store = MerkleStore::default();
150
151 // Starts at 1 because leafs are not included in the store.
152 // Ends at 64 because it is not possible to represent an index of a depth greater than 64,
153 // because a u64 is used to index the leaf.
154 for depth in 1..64 {
155 let smt = SimpleSmt::new(depth)?;
156
157 let index = NodeIndex::make(depth, 0);
158 let store_path = store.get_path(smt.root(), index)?;
159 let smt_path = smt.get_path(index)?;
160 assert_eq!(store_path.value, EMPTY, "the leaf of an empty tree is always ZERO");
161 assert_eq!(
162 store_path.path, smt_path,
163 "the returned merkle path does not match the computed values"
164 );
165 assert_eq!(
166 store_path.path.compute_root(depth.into(), EMPTY).unwrap(),
167 smt.root(),
168 "computed root from the path must match the empty tree root"
169 );
170 }
171
172 Ok(())
173}
174
175#[test]
176fn test_get_invalid_node() {
177 let mtree = MerkleTree::new(LEAVES4.to_vec()).expect("creating a merkle tree must work");
178 let store = MerkleStore::from(&mtree);
179 let _ = store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 3));
180}
181
182#[test]
183fn test_add_sparse_merkle_tree_one_level() -> Result<(), MerkleError> {
184 let keys2: [u64; 2] = [0, 1];
185 let leaves2: [Word; 2] = [int_to_node(1), int_to_node(2)];
186 let smt = SimpleSmt::with_leaves(1, keys2.into_iter().zip(leaves2.into_iter())).unwrap();
187 let store = MerkleStore::from(&smt);
188
189 let idx = NodeIndex::make(1, 0);
190 assert_eq!(smt.get_node(idx).unwrap(), leaves2[0]);
191 assert_eq!(store.get_node(smt.root(), idx).unwrap(), smt.get_node(idx).unwrap());
192
193 let idx = NodeIndex::make(1, 1);
194 assert_eq!(smt.get_node(idx).unwrap(), leaves2[1]);
195 assert_eq!(store.get_node(smt.root(), idx).unwrap(), smt.get_node(idx).unwrap());
196
197 Ok(())
198}
199
200#[test]
201fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
202 let smt =
203 SimpleSmt::with_leaves(SimpleSmt::MAX_DEPTH, KEYS4.into_iter().zip(LEAVES4.into_iter()))
204 .unwrap();
205
206 let store = MerkleStore::from(&smt);
207
208 // STORE LEAVES ARE CORRECT ==============================================================
209 // checks the leaves in the store corresponds to the expected values
210 assert_eq!(
211 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 0)),
212 Ok(LEAVES4[0]),
213 "node 0 must be in the tree"
214 );
215 assert_eq!(
216 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 1)),
217 Ok(LEAVES4[1]),
218 "node 1 must be in the tree"
219 );
220 assert_eq!(
221 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 2)),
222 Ok(LEAVES4[2]),
223 "node 2 must be in the tree"
224 );
225 assert_eq!(
226 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 3)),
227 Ok(LEAVES4[3]),
228 "node 3 must be in the tree"
229 );
230 assert_eq!(
231 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 4)),
232 Ok(EMPTY),
233 "unmodified node 4 must be ZERO"
234 );
235
236 // STORE LEAVES MATCH TREE ===============================================================
237 // sanity check the values returned by the store and the tree
238 assert_eq!(
239 smt.get_node(NodeIndex::make(smt.depth(), 0)),
240 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 0)),
241 "node 0 must be the same for both SparseMerkleTree and MerkleStore"
242 );
243 assert_eq!(
244 smt.get_node(NodeIndex::make(smt.depth(), 1)),
245 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 1)),
246 "node 1 must be the same for both SparseMerkleTree and MerkleStore"
247 );
248 assert_eq!(
249 smt.get_node(NodeIndex::make(smt.depth(), 2)),
250 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 2)),
251 "node 2 must be the same for both SparseMerkleTree and MerkleStore"
252 );
253 assert_eq!(
254 smt.get_node(NodeIndex::make(smt.depth(), 3)),
255 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 3)),
256 "node 3 must be the same for both SparseMerkleTree and MerkleStore"
257 );
258 assert_eq!(
259 smt.get_node(NodeIndex::make(smt.depth(), 4)),
260 store.get_node(smt.root(), NodeIndex::make(smt.depth(), 4)),
261 "node 4 must be the same for both SparseMerkleTree and MerkleStore"
262 );
263
264 // STORE MERKLE PATH MATCHS ==============================================================
265 // assert the merkle path returned by the store is the same as the one in the tree
266 let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 0)).unwrap();
267 assert_eq!(
268 LEAVES4[0], result.value,
269 "Value for merkle path at index 0 must match leaf value"
270 );
271 assert_eq!(
272 smt.get_path(NodeIndex::make(smt.depth(), 0)),
273 Ok(result.path),
274 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
275 );
276
277 let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 1)).unwrap();
278 assert_eq!(
279 LEAVES4[1], result.value,
280 "Value for merkle path at index 1 must match leaf value"
281 );
282 assert_eq!(
283 smt.get_path(NodeIndex::make(smt.depth(), 1)),
284 Ok(result.path),
285 "merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
286 );
287
288 let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 2)).unwrap();
289 assert_eq!(
290 LEAVES4[2], result.value,
291 "Value for merkle path at index 2 must match leaf value"
292 );
293 assert_eq!(
294 smt.get_path(NodeIndex::make(smt.depth(), 2)),
295 Ok(result.path),
296 "merkle path for index 2 must be the same for the MerkleTree and MerkleStore"
297 );
298
299 let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 3)).unwrap();
300 assert_eq!(
301 LEAVES4[3], result.value,
302 "Value for merkle path at index 3 must match leaf value"
303 );
304 assert_eq!(
305 smt.get_path(NodeIndex::make(smt.depth(), 3)),
306 Ok(result.path),
307 "merkle path for index 3 must be the same for the MerkleTree and MerkleStore"
308 );
309
310 let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 4)).unwrap();
311 assert_eq!(EMPTY, result.value, "Value for merkle path at index 4 must match leaf value");
312 assert_eq!(
313 smt.get_path(NodeIndex::make(smt.depth(), 4)),
314 Ok(result.path),
315 "merkle path for index 4 must be the same for the MerkleTree and MerkleStore"
316 );
317
318 Ok(())
319}
320
321#[test]
322fn test_add_merkle_paths() -> Result<(), MerkleError> {
323 let mtree = MerkleTree::new(LEAVES4.to_vec())?;
324
325 let i0 = 0;
326 let p0 = mtree.get_path(NodeIndex::make(2, i0)).unwrap();
327
328 let i1 = 1;
329 let p1 = mtree.get_path(NodeIndex::make(2, i1)).unwrap();
330
331 let i2 = 2;
332 let p2 = mtree.get_path(NodeIndex::make(2, i2)).unwrap();
333
334 let i3 = 3;
335 let p3 = mtree.get_path(NodeIndex::make(2, i3)).unwrap();
336
337 let paths = [
338 (i0, LEAVES4[i0 as usize], p0),
339 (i1, LEAVES4[i1 as usize], p1),
340 (i2, LEAVES4[i2 as usize], p2),
341 (i3, LEAVES4[i3 as usize], p3),
342 ];
343
344 let mut store = MerkleStore::default();
345 store.add_merkle_paths(paths.clone()).expect("the valid paths must work");
346
347 let depth = 2;
348 let set = MerklePathSet::new(depth).with_paths(paths).unwrap();
349
350 // STORE LEAVES ARE CORRECT ==============================================================
351 // checks the leaves in the store corresponds to the expected values
352 assert_eq!(
353 store.get_node(set.root(), NodeIndex::make(set.depth(), 0)),
354 Ok(LEAVES4[0]),
355 "node 0 must be in the set"
356 );
357 assert_eq!(
358 store.get_node(set.root(), NodeIndex::make(set.depth(), 1)),
359 Ok(LEAVES4[1]),
360 "node 1 must be in the set"
361 );
362 assert_eq!(
363 store.get_node(set.root(), NodeIndex::make(set.depth(), 2)),
364 Ok(LEAVES4[2]),
365 "node 2 must be in the set"
366 );
367 assert_eq!(
368 store.get_node(set.root(), NodeIndex::make(set.depth(), 3)),
369 Ok(LEAVES4[3]),
370 "node 3 must be in the set"
371 );
372
373 // STORE LEAVES MATCH SET ================================================================
374 // sanity check the values returned by the store and the set
375 assert_eq!(
376 set.get_node(NodeIndex::make(set.depth(), 0)),
377 store.get_node(set.root(), NodeIndex::make(set.depth(), 0)),
378 "node 0 must be the same for both SparseMerkleTree and MerkleStore"
379 );
380 assert_eq!(
381 set.get_node(NodeIndex::make(set.depth(), 1)),
382 store.get_node(set.root(), NodeIndex::make(set.depth(), 1)),
383 "node 1 must be the same for both SparseMerkleTree and MerkleStore"
384 );
385 assert_eq!(
386 set.get_node(NodeIndex::make(set.depth(), 2)),
387 store.get_node(set.root(), NodeIndex::make(set.depth(), 2)),
388 "node 2 must be the same for both SparseMerkleTree and MerkleStore"
389 );
390 assert_eq!(
391 set.get_node(NodeIndex::make(set.depth(), 3)),
392 store.get_node(set.root(), NodeIndex::make(set.depth(), 3)),
393 "node 3 must be the same for both SparseMerkleTree and MerkleStore"
394 );
395
396 // STORE MERKLE PATH MATCHS ==============================================================
397 // assert the merkle path returned by the store is the same as the one in the set
398 let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 0)).unwrap();
399 assert_eq!(
400 LEAVES4[0], result.value,
401 "Value for merkle path at index 0 must match leaf value"
402 );
403 assert_eq!(
404 set.get_path(NodeIndex::make(set.depth(), 0)),
405 Ok(result.path),
406 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
407 );
408
409 let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 1)).unwrap();
410 assert_eq!(
411 LEAVES4[1], result.value,
412 "Value for merkle path at index 0 must match leaf value"
413 );
414 assert_eq!(
415 set.get_path(NodeIndex::make(set.depth(), 1)),
416 Ok(result.path),
417 "merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
418 );
419
420 let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 2)).unwrap();
421 assert_eq!(
422 LEAVES4[2], result.value,
423 "Value for merkle path at index 0 must match leaf value"
424 );
425 assert_eq!(
426 set.get_path(NodeIndex::make(set.depth(), 2)),
427 Ok(result.path),
428 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
429 );
430
431 let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 3)).unwrap();
432 assert_eq!(
433 LEAVES4[3], result.value,
434 "Value for merkle path at index 0 must match leaf value"
435 );
436 assert_eq!(
437 set.get_path(NodeIndex::make(set.depth(), 3)),
438 Ok(result.path),
439 "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
440 );
441
442 Ok(())
443}
444
445#[test]
446fn wont_open_to_different_depth_root() {
447 let empty = EmptySubtreeRoots::empty_hashes(64);
448 let a = [Felt::new(1); 4];
449 let b = [Felt::new(2); 4];
450
451 // Compute the root for a different depth. We cherry-pick this specific depth to prevent a
452 // regression to a bug in the past that allowed the user to fetch a node at a depth lower than
453 // the inserted path of a Merkle tree.
454 let mut root = Rpo256::merge(&[a.into(), b.into()]);
455 for depth in (1..=63).rev() {
456 root = Rpo256::merge(&[root, empty[depth]]);
457 }
458 let root = Word::from(root);
459
460 // For this example, the depth of the Merkle tree is 1, as we have only two leaves. Here we
461 // attempt to fetch a node on the maximum depth, and it should fail because the root shouldn't
462 // exist for the set.
463 let mtree = MerkleTree::new(vec![a, b]).unwrap();
464 let store = MerkleStore::from(&mtree);
465 let index = NodeIndex::root();
466 let err = store.get_node(root, index).err().unwrap();
467 assert_eq!(err, MerkleError::RootNotInStore(root));
468}
469
470#[test]
471fn store_path_opens_from_leaf() {
472 let a = [Felt::new(1); 4];
473 let b = [Felt::new(2); 4];
474 let c = [Felt::new(3); 4];
475 let d = [Felt::new(4); 4];
476 let e = [Felt::new(5); 4];
477 let f = [Felt::new(6); 4];
478 let g = [Felt::new(7); 4];
479 let h = [Felt::new(8); 4];
480
481 let i = Rpo256::merge(&[a.into(), b.into()]);
482 let j = Rpo256::merge(&[c.into(), d.into()]);
483 let k = Rpo256::merge(&[e.into(), f.into()]);
484 let l = Rpo256::merge(&[g.into(), h.into()]);
485
486 let m = Rpo256::merge(&[i.into(), j.into()]);
487 let n = Rpo256::merge(&[k.into(), l.into()]);
488
489 let root = Rpo256::merge(&[m.into(), n.into()]);
490
491 let mtree = MerkleTree::new(vec![a, b, c, d, e, f, g, h]).unwrap();
492 let store = MerkleStore::from(&mtree);
493 let path = store.get_path(root.into(), NodeIndex::make(3, 1)).unwrap().path;
494
495 let expected = MerklePath::new([a.into(), j.into(), n.into()].to_vec());
496 assert_eq!(path, expected);
497}
498
499#[test]
500fn test_set_node() -> Result<(), MerkleError> {
501 let mtree = MerkleTree::new(LEAVES4.to_vec())?;
502 let mut store = MerkleStore::from(&mtree);
503 let value = int_to_node(42);
504 let index = NodeIndex::make(mtree.depth(), 0);
505 let new_root = store.set_node(mtree.root(), index, value)?.root;
506 assert_eq!(store.get_node(new_root, index), Ok(value), "Value must have changed");
507
508 Ok(())
509}
510
511#[test]
512fn test_constructors() -> Result<(), MerkleError> {
513 let mtree = MerkleTree::new(LEAVES4.to_vec())?;
514 let store = MerkleStore::from(&mtree);
515
516 let depth = mtree.depth();
517 let leaves = 2u64.pow(depth.into());
518 for index in 0..leaves {
519 let index = NodeIndex::make(depth, index);
520 let value_path = store.get_path(mtree.root(), index)?;
521 assert_eq!(mtree.get_path(index)?, value_path.path);
522 }
523
524 let depth = 32;
525 let smt = SimpleSmt::with_leaves(depth, KEYS4.into_iter().zip(LEAVES4.into_iter())).unwrap();
526 let store = MerkleStore::from(&smt);
527 let depth = smt.depth();
528
529 for key in KEYS4 {
530 let index = NodeIndex::make(depth, key);
531 let value_path = store.get_path(smt.root(), index)?;
532 assert_eq!(smt.get_path(index)?, value_path.path);
533 }
534
535 let d = 2;
536 let paths = [
537 (0, LEAVES4[0], mtree.get_path(NodeIndex::make(d, 0)).unwrap()),
538 (1, LEAVES4[1], mtree.get_path(NodeIndex::make(d, 1)).unwrap()),
539 (2, LEAVES4[2], mtree.get_path(NodeIndex::make(d, 2)).unwrap()),
540 (3, LEAVES4[3], mtree.get_path(NodeIndex::make(d, 3)).unwrap()),
541 ];
542
543 let mut store1 = MerkleStore::default();
544 store1.add_merkle_paths(paths.clone())?;
545
546 let mut store2 = MerkleStore::default();
547 store2.add_merkle_path(0, LEAVES4[0], mtree.get_path(NodeIndex::make(d, 0))?)?;
548 store2.add_merkle_path(1, LEAVES4[1], mtree.get_path(NodeIndex::make(d, 1))?)?;
549 store2.add_merkle_path(2, LEAVES4[2], mtree.get_path(NodeIndex::make(d, 2))?)?;
550 store2.add_merkle_path(3, LEAVES4[3], mtree.get_path(NodeIndex::make(d, 3))?)?;
551 let set = MerklePathSet::new(d).with_paths(paths).unwrap();
552
553 for key in [0, 1, 2, 3] {
554 let index = NodeIndex::make(d, key);
555 let value_path1 = store1.get_path(set.root(), index)?;
556 let value_path2 = store2.get_path(set.root(), index)?;
557 assert_eq!(value_path1, value_path2);
558
559 let index = NodeIndex::make(d, key);
560 assert_eq!(set.get_path(index)?, value_path1.path);
561 }
562
563 Ok(())
564}
565
566#[test]
567fn node_path_should_be_truncated_by_midtier_insert() {
568 let key = 0b11010010_11001100_11001100_11001100_11001100_11001100_11001100_11001100_u64;
569
570 let mut store = MerkleStore::new();
571 let root: Word = EmptySubtreeRoots::empty_hashes(64)[0].into();
572
573 // insert first node - works as expected
574 let depth = 64;
575 let node = [Felt::new(key); WORD_SIZE];
576 let index = NodeIndex::new(depth, key).unwrap();
577 let root = store.set_node(root, index, node).unwrap().root;
578 let result = store.get_node(root, index).unwrap();
579 let path = store.get_path(root, index).unwrap().path;
580 assert_eq!(node, result);
581 assert_eq!(path.depth(), depth);
582 assert!(path.verify(index.value(), result, &root));
583
584 // flip the first bit of the key and insert the second node on a different depth
585 let key = key ^ (1 << 63);
586 let key = key >> 8;
587 let depth = 56;
588 let node = [Felt::new(key); WORD_SIZE];
589 let index = NodeIndex::new(depth, key).unwrap();
590 let root = store.set_node(root, index, node).unwrap().root;
591 let result = store.get_node(root, index).unwrap();
592 let path = store.get_path(root, index).unwrap().path;
593 assert_eq!(node, result);
594 assert_eq!(path.depth(), depth);
595 assert!(path.verify(index.value(), result, &root));
596
597 // attempt to fetch a path of the second node to depth 64
598 // should fail because the previously inserted node will remove its sub-tree from the set
599 let key = key << 8;
600 let index = NodeIndex::new(64, key).unwrap();
601 assert!(store.get_node(root, index).is_err());
602}
603
604#[test]
605fn get_leaf_depth_works_depth_64() {
606 let mut store = MerkleStore::new();
607 let mut root: Word = EmptySubtreeRoots::empty_hashes(64)[0].into();
608 let key = u64::MAX;
609
610 // this will create a rainbow tree and test all opening to depth 64
611 for d in 0..64 {
612 let k = key & (u64::MAX >> d);
613 let node = [Felt::new(k); WORD_SIZE];
614 let index = NodeIndex::new(64, k).unwrap();
615
616 // assert the leaf doesn't exist before the insert. the returned depth should always
617 // increment with the paths count of the set, as they are insersecting one another up to
618 // the first bits of the used key.
619 assert_eq!(d, store.get_leaf_depth(root, 64, k).unwrap());
620
621 // insert and assert the correct depth
622 root = store.set_node(root, index, node).unwrap().root;
623 assert_eq!(64, store.get_leaf_depth(root, 64, k).unwrap());
624 }
625}
626
627#[test]
628fn get_leaf_depth_works_with_incremental_depth() {
629 let mut store = MerkleStore::new();
630 let mut root: Word = EmptySubtreeRoots::empty_hashes(64)[0].into();
631
632 // insert some path to the left of the root and assert it
633 let key = 0b01001011_10110110_00001101_01110100_00111011_10101101_00000100_01000001_u64;
634 assert_eq!(0, store.get_leaf_depth(root, 64, key).unwrap());
635 let depth = 64;
636 let index = NodeIndex::new(depth, key).unwrap();
637 let node = [Felt::new(key); WORD_SIZE];
638 root = store.set_node(root, index, node).unwrap().root;
639 assert_eq!(depth, store.get_leaf_depth(root, 64, key).unwrap());
640
641 // flip the key to the right of the root and insert some content on depth 16
642 let key = 0b11001011_10110110_00000000_00000000_00000000_00000000_00000000_00000000_u64;
643 assert_eq!(1, store.get_leaf_depth(root, 64, key).unwrap());
644 let depth = 16;
645 let index = NodeIndex::new(depth, key >> (64 - depth)).unwrap();
646 let node = [Felt::new(key); WORD_SIZE];
647 root = store.set_node(root, index, node).unwrap().root;
648 assert_eq!(depth, store.get_leaf_depth(root, 64, key).unwrap());
649
650 // attempt the sibling of the previous leaf
651 let key = 0b11001011_10110111_00000000_00000000_00000000_00000000_00000000_00000000_u64;
652 assert_eq!(16, store.get_leaf_depth(root, 64, key).unwrap());
653 let index = NodeIndex::new(depth, key >> (64 - depth)).unwrap();
654 let node = [Felt::new(key); WORD_SIZE];
655 root = store.set_node(root, index, node).unwrap().root;
656 assert_eq!(depth, store.get_leaf_depth(root, 64, key).unwrap());
657
658 // move down to the next depth and assert correct behavior
659 let key = 0b11001011_10110100_00000000_00000000_00000000_00000000_00000000_00000000_u64;
660 assert_eq!(15, store.get_leaf_depth(root, 64, key).unwrap());
661 let depth = 17;
662 let index = NodeIndex::new(depth, key >> (64 - depth)).unwrap();
663 let node = [Felt::new(key); WORD_SIZE];
664 root = store.set_node(root, index, node).unwrap().root;
665 assert_eq!(depth, store.get_leaf_depth(root, 64, key).unwrap());
666}
667
668#[test]
669fn get_leaf_depth_works_with_depth_8() {
670 let mut store = MerkleStore::new();
671 let mut root: Word = EmptySubtreeRoots::empty_hashes(8)[0].into();
672
673 // insert some random, 8 depth keys. `a` diverges from the first bit
674 let a = 0b01101001_u64;
675 let b = 0b10011001_u64;
676 let c = 0b10010110_u64;
677 let d = 0b11110110_u64;
678
679 for k in [a, b, c, d] {
680 let index = NodeIndex::new(8, k).unwrap();
681 let node = [Felt::new(k); WORD_SIZE];
682 root = store.set_node(root, index, node).unwrap().root;
683 }
684
685 // assert all leaves returns the inserted depth
686 for k in [a, b, c, d] {
687 assert_eq!(8, store.get_leaf_depth(root, 8, k).unwrap());
688 }
689
690 // flip last bit of a and expect it to return the the same depth, but for an empty node
691 assert_eq!(8, store.get_leaf_depth(root, 8, 0b01101000_u64).unwrap());
692
693 // flip fourth bit of a and expect an empty node on depth 4
694 assert_eq!(4, store.get_leaf_depth(root, 8, 0b01111001_u64).unwrap());
695
696 // flip third bit of a and expect an empty node on depth 3
697 assert_eq!(3, store.get_leaf_depth(root, 8, 0b01001001_u64).unwrap());
698
699 // flip second bit of a and expect an empty node on depth 2
700 assert_eq!(2, store.get_leaf_depth(root, 8, 0b00101001_u64).unwrap());
701
702 // flip fourth bit of c and expect an empty node on depth 4
703 assert_eq!(4, store.get_leaf_depth(root, 8, 0b10000110_u64).unwrap());
704
705 // flip second bit of d and expect an empty node on depth 3 as depth 2 conflicts with b and c
706 assert_eq!(3, store.get_leaf_depth(root, 8, 0b10110110_u64).unwrap());
707
708 // duplicate the tree on `a` and assert the depth is short-circuited by such sub-tree
709 let index = NodeIndex::new(8, a).unwrap();
710 root = store.set_node(root, index, root).unwrap().root;
711 assert_eq!(Err(MerkleError::DepthTooBig(9)), store.get_leaf_depth(root, 8, a));
712}
713
714#[cfg(feature = "std")]
715#[test]
716fn test_serialization() -> Result<(), Box<dyn Error>> {
717 let mtree = MerkleTree::new(LEAVES4.to_vec())?;
718 let store = MerkleStore::from(&mtree);
719 let decoded = MerkleStore::read_from_bytes(&store.to_bytes()).expect("deserialization failed");
720 assert_eq!(store, decoded);
721 Ok(())
722}