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(48, 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(
235        SimpleSmt::MAX_DEPTH,
236        KEYS4.into_iter().zip(LEAVES4.into_iter()),
237    )?;
238
239    let smt = SimpleSmt::new(SimpleSmt::MAX_DEPTH)
240        .unwrap()
241        .with_leaves(KEYS4.into_iter().zip(LEAVES4.into_iter()))
242        .unwrap();
243
244    // STORE LEAVES ARE CORRECT ==============================================================
245    // checks the leaves in the store corresponds to the expected values
246    assert_eq!(
247        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 0)),
248        Ok(LEAVES4[0]),
249        "node 0 must be in the tree"
250    );
251    assert_eq!(
252        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 1)),
253        Ok(LEAVES4[1]),
254        "node 1 must be in the tree"
255    );
256    assert_eq!(
257        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 2)),
258        Ok(LEAVES4[2]),
259        "node 2 must be in the tree"
260    );
261    assert_eq!(
262        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 3)),
263        Ok(LEAVES4[3]),
264        "node 3 must be in the tree"
265    );
266    assert_eq!(
267        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 4)),
268        Ok(EMPTY),
269        "unmodified node 4 must be ZERO"
270    );
271
272    // STORE LEAVES MATCH TREE ===============================================================
273    // sanity check the values returned by the store and the tree
274    assert_eq!(
275        smt.get_node(&NodeIndex::new(smt.depth(), 0)),
276        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 0)),
277        "node 0 must be the same for both SparseMerkleTree and MerkleStore"
278    );
279    assert_eq!(
280        smt.get_node(&NodeIndex::new(smt.depth(), 1)),
281        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 1)),
282        "node 1 must be the same for both SparseMerkleTree and MerkleStore"
283    );
284    assert_eq!(
285        smt.get_node(&NodeIndex::new(smt.depth(), 2)),
286        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 2)),
287        "node 2 must be the same for both SparseMerkleTree and MerkleStore"
288    );
289    assert_eq!(
290        smt.get_node(&NodeIndex::new(smt.depth(), 3)),
291        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 3)),
292        "node 3 must be the same for both SparseMerkleTree and MerkleStore"
293    );
294    assert_eq!(
295        smt.get_node(&NodeIndex::new(smt.depth(), 4)),
296        store.get_node(smt.root(), NodeIndex::new(smt.depth(), 4)),
297        "node 4 must be the same for both SparseMerkleTree and MerkleStore"
298    );
299
300    // STORE MERKLE PATH MATCHS ==============================================================
301    // assert the merkle path returned by the store is the same as the one in the tree
302    let result = store
303        .get_path(smt.root(), NodeIndex::new(smt.depth(), 0))
304        .unwrap();
305    assert_eq!(
306        LEAVES4[0], result.value,
307        "Value for merkle path at index 0 must match leaf value"
308    );
309    assert_eq!(
310        smt.get_path(NodeIndex::new(smt.depth(), 0)),
311        Ok(result.path),
312        "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
313    );
314
315    let result = store
316        .get_path(smt.root(), NodeIndex::new(smt.depth(), 1))
317        .unwrap();
318    assert_eq!(
319        LEAVES4[1], result.value,
320        "Value for merkle path at index 1 must match leaf value"
321    );
322    assert_eq!(
323        smt.get_path(NodeIndex::new(smt.depth(), 1)),
324        Ok(result.path),
325        "merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
326    );
327
328    let result = store
329        .get_path(smt.root(), NodeIndex::new(smt.depth(), 2))
330        .unwrap();
331    assert_eq!(
332        LEAVES4[2], result.value,
333        "Value for merkle path at index 2 must match leaf value"
334    );
335    assert_eq!(
336        smt.get_path(NodeIndex::new(smt.depth(), 2)),
337        Ok(result.path),
338        "merkle path for index 2 must be the same for the MerkleTree and MerkleStore"
339    );
340
341    let result = store
342        .get_path(smt.root(), NodeIndex::new(smt.depth(), 3))
343        .unwrap();
344    assert_eq!(
345        LEAVES4[3], result.value,
346        "Value for merkle path at index 3 must match leaf value"
347    );
348    assert_eq!(
349        smt.get_path(NodeIndex::new(smt.depth(), 3)),
350        Ok(result.path),
351        "merkle path for index 3 must be the same for the MerkleTree and MerkleStore"
352    );
353
354    let result = store
355        .get_path(smt.root(), NodeIndex::new(smt.depth(), 4))
356        .unwrap();
357    assert_eq!(
358        EMPTY, result.value,
359        "Value for merkle path at index 4 must match leaf value"
360    );
361    assert_eq!(
362        smt.get_path(NodeIndex::new(smt.depth(), 4)),
363        Ok(result.path),
364        "merkle path for index 4 must be the same for the MerkleTree and MerkleStore"
365    );
366
367    Ok(())
368}
369
370#[test]
371fn test_add_merkle_paths() -> Result<(), MerkleError> {
372    let mtree = MerkleTree::new(LEAVES4.to_vec())?;
373
374    let i0 = 0;
375    let p0 = mtree.get_path(NodeIndex::new(2, i0)).unwrap();
376
377    let i1 = 1;
378    let p1 = mtree.get_path(NodeIndex::new(2, i1)).unwrap();
379
380    let i2 = 2;
381    let p2 = mtree.get_path(NodeIndex::new(2, i2)).unwrap();
382
383    let i3 = 3;
384    let p3 = mtree.get_path(NodeIndex::new(2, i3)).unwrap();
385
386    let paths = [
387        (i0, LEAVES4[i0 as usize], p0),
388        (i1, LEAVES4[i1 as usize], p1),
389        (i2, LEAVES4[i2 as usize], p2),
390        (i3, LEAVES4[i3 as usize], p3),
391    ];
392
393    let mut store = MerkleStore::default();
394    store
395        .add_merkle_paths(paths.clone())
396        .expect("the valid paths must work");
397
398    let depth = 2;
399    let set = MerklePathSet::new(depth).with_paths(paths).unwrap();
400
401    // STORE LEAVES ARE CORRECT ==============================================================
402    // checks the leaves in the store corresponds to the expected values
403    assert_eq!(
404        store.get_node(set.root(), NodeIndex::new(set.depth(), 0)),
405        Ok(LEAVES4[0]),
406        "node 0 must be in the set"
407    );
408    assert_eq!(
409        store.get_node(set.root(), NodeIndex::new(set.depth(), 1)),
410        Ok(LEAVES4[1]),
411        "node 1 must be in the set"
412    );
413    assert_eq!(
414        store.get_node(set.root(), NodeIndex::new(set.depth(), 2)),
415        Ok(LEAVES4[2]),
416        "node 2 must be in the set"
417    );
418    assert_eq!(
419        store.get_node(set.root(), NodeIndex::new(set.depth(), 3)),
420        Ok(LEAVES4[3]),
421        "node 3 must be in the set"
422    );
423
424    // STORE LEAVES MATCH SET ================================================================
425    // sanity check the values returned by the store and the set
426    assert_eq!(
427        set.get_node(NodeIndex::new(set.depth(), 0)),
428        store.get_node(set.root(), NodeIndex::new(set.depth(), 0)),
429        "node 0 must be the same for both SparseMerkleTree and MerkleStore"
430    );
431    assert_eq!(
432        set.get_node(NodeIndex::new(set.depth(), 1)),
433        store.get_node(set.root(), NodeIndex::new(set.depth(), 1)),
434        "node 1 must be the same for both SparseMerkleTree and MerkleStore"
435    );
436    assert_eq!(
437        set.get_node(NodeIndex::new(set.depth(), 2)),
438        store.get_node(set.root(), NodeIndex::new(set.depth(), 2)),
439        "node 2 must be the same for both SparseMerkleTree and MerkleStore"
440    );
441    assert_eq!(
442        set.get_node(NodeIndex::new(set.depth(), 3)),
443        store.get_node(set.root(), NodeIndex::new(set.depth(), 3)),
444        "node 3 must be the same for both SparseMerkleTree and MerkleStore"
445    );
446
447    // STORE MERKLE PATH MATCHS ==============================================================
448    // assert the merkle path returned by the store is the same as the one in the set
449    let result = store
450        .get_path(set.root(), NodeIndex::new(set.depth(), 0))
451        .unwrap();
452    assert_eq!(
453        LEAVES4[0], result.value,
454        "Value for merkle path at index 0 must match leaf value"
455    );
456    assert_eq!(
457        set.get_path(NodeIndex::new(set.depth(), 0)),
458        Ok(result.path),
459        "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
460    );
461
462    let result = store
463        .get_path(set.root(), NodeIndex::new(set.depth(), 1))
464        .unwrap();
465    assert_eq!(
466        LEAVES4[1], result.value,
467        "Value for merkle path at index 0 must match leaf value"
468    );
469    assert_eq!(
470        set.get_path(NodeIndex::new(set.depth(), 1)),
471        Ok(result.path),
472        "merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
473    );
474
475    let result = store
476        .get_path(set.root(), NodeIndex::new(set.depth(), 2))
477        .unwrap();
478    assert_eq!(
479        LEAVES4[2], result.value,
480        "Value for merkle path at index 0 must match leaf value"
481    );
482    assert_eq!(
483        set.get_path(NodeIndex::new(set.depth(), 2)),
484        Ok(result.path),
485        "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
486    );
487
488    let result = store
489        .get_path(set.root(), NodeIndex::new(set.depth(), 3))
490        .unwrap();
491    assert_eq!(
492        LEAVES4[3], result.value,
493        "Value for merkle path at index 0 must match leaf value"
494    );
495    assert_eq!(
496        set.get_path(NodeIndex::new(set.depth(), 3)),
497        Ok(result.path),
498        "merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
499    );
500
501    Ok(())
502}
503
504#[test]
505fn wont_open_to_different_depth_root() {
506    let empty = EmptySubtreeRoots::empty_hashes(64);
507    let a = [Felt::new(1); 4];
508    let b = [Felt::new(2); 4];
509
510    // Compute the root for a different depth. We cherry-pick this specific depth to prevent a
511    // regression to a bug in the past that allowed the user to fetch a node at a depth lower than
512    // the inserted path of a Merkle tree.
513    let mut root = Rpo256::merge(&[a.into(), b.into()]);
514    for depth in (1..=63).rev() {
515        root = Rpo256::merge(&[root, empty[depth]]);
516    }
517    let root = Word::from(root);
518
519    // For this example, the depth of the Merkle tree is 1, as we have only two leaves. Here we
520    // attempt to fetch a node on the maximum depth, and it should fail because the root shouldn't
521    // exist for the set.
522    let store = MerkleStore::default().with_merkle_tree([a, b]).unwrap();
523    let index = NodeIndex::root();
524    let err = store.get_node(root, index).err().unwrap();
525    assert_eq!(err, MerkleError::RootNotInStore(root));
526}
527
528#[test]
529fn store_path_opens_from_leaf() {
530    let a = [Felt::new(1); 4];
531    let b = [Felt::new(2); 4];
532    let c = [Felt::new(3); 4];
533    let d = [Felt::new(4); 4];
534    let e = [Felt::new(5); 4];
535    let f = [Felt::new(6); 4];
536    let g = [Felt::new(7); 4];
537    let h = [Felt::new(8); 4];
538
539    let i = Rpo256::merge(&[a.into(), b.into()]);
540    let j = Rpo256::merge(&[c.into(), d.into()]);
541    let k = Rpo256::merge(&[e.into(), f.into()]);
542    let l = Rpo256::merge(&[g.into(), h.into()]);
543
544    let m = Rpo256::merge(&[i.into(), j.into()]);
545    let n = Rpo256::merge(&[k.into(), l.into()]);
546
547    let root = Rpo256::merge(&[m.into(), n.into()]);
548
549    let store = MerkleStore::default()
550        .with_merkle_tree([a, b, c, d, e, f, g, h])
551        .unwrap();
552    let path = store
553        .get_path(root.into(), NodeIndex::new(3, 1))
554        .unwrap()
555        .path;
556
557    let expected = MerklePath::new([a.into(), j.into(), n.into()].to_vec());
558    assert_eq!(path, expected);
559}
560
561#[test]
562fn test_set_node() -> Result<(), MerkleError> {
563    let mtree = MerkleTree::new(LEAVES4.to_vec())?;
564    let mut store = MerkleStore::default().with_merkle_tree(LEAVES4)?;
565    let value = int_to_node(42);
566    let index = NodeIndex::new(mtree.depth(), 0);
567    let new_root = store.set_node(mtree.root(), index, value)?.root;
568    assert_eq!(
569        store.get_node(new_root, index),
570        Ok(value),
571        "Value must have changed"
572    );
573
574    Ok(())
575}
576
577#[test]
578fn test_constructors() -> Result<(), MerkleError> {
579    let store = MerkleStore::new().with_merkle_tree(LEAVES4)?;
580    let mtree = MerkleTree::new(LEAVES4.to_vec())?;
581
582    let depth = mtree.depth();
583    let leaves = 2u64.pow(depth.into());
584    for index in 0..leaves {
585        let index = NodeIndex::new(depth, index);
586        let value_path = store.get_path(mtree.root(), index)?;
587        assert_eq!(mtree.get_path(index)?, value_path.path);
588    }
589
590    let depth = 32;
591    let store = MerkleStore::default()
592        .with_sparse_merkle_tree(depth, KEYS4.into_iter().zip(LEAVES4.into_iter()))?;
593    let smt = SimpleSmt::new(depth)
594        .unwrap()
595        .with_leaves(KEYS4.into_iter().zip(LEAVES4.into_iter()))
596        .unwrap();
597    let depth = smt.depth();
598
599    for key in KEYS4 {
600        let index = NodeIndex::new(depth, key);
601        let value_path = store.get_path(smt.root(), index)?;
602        assert_eq!(smt.get_path(index)?, value_path.path);
603    }
604
605    let d = 2;
606    let paths = [
607        (0, LEAVES4[0], mtree.get_path(NodeIndex::new(d, 0)).unwrap()),
608        (1, LEAVES4[1], mtree.get_path(NodeIndex::new(d, 1)).unwrap()),
609        (2, LEAVES4[2], mtree.get_path(NodeIndex::new(d, 2)).unwrap()),
610        (3, LEAVES4[3], mtree.get_path(NodeIndex::new(d, 3)).unwrap()),
611    ];
612
613    let store1 = MerkleStore::default().with_merkle_paths(paths.clone())?;
614    let store2 = MerkleStore::default()
615        .with_merkle_path(0, LEAVES4[0], mtree.get_path(NodeIndex::new(d, 0))?)?
616        .with_merkle_path(1, LEAVES4[1], mtree.get_path(NodeIndex::new(d, 1))?)?
617        .with_merkle_path(2, LEAVES4[2], mtree.get_path(NodeIndex::new(d, 2))?)?
618        .with_merkle_path(3, LEAVES4[3], mtree.get_path(NodeIndex::new(d, 3))?)?;
619    let set = MerklePathSet::new(d).with_paths(paths).unwrap();
620
621    for key in [0, 1, 2, 3] {
622        let index = NodeIndex::new(d, key);
623        let value_path1 = store1.get_path(set.root(), index)?;
624        let value_path2 = store2.get_path(set.root(), index)?;
625        assert_eq!(value_path1, value_path2);
626
627        let index = NodeIndex::new(d, key);
628        assert_eq!(set.get_path(index)?, value_path1.path);
629    }
630
631    Ok(())
632}
633
634#[cfg(std)]
635#[test]
636fn test_serialization() -> Result<(), Box<dyn Error>> {
637    let original = MerkleStore::new().with_merkle_tree(LEAVES4)?;
638    let decoded = MerkleStore::read_from_bytes(&original.to_bytes())?;
639    assert_eq!(original, decoded);
640    Ok(())
641}