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