From 07f58c97dc1b439e0c9995a17f78366da3f2b73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Awirski?= Date: Wed, 16 Apr 2025 11:58:18 +0200 Subject: [PATCH] loop over depths in tests --- miden-crypto/src/merkle/sparse_path.rs | 153 ++++--------------------- 1 file changed, 21 insertions(+), 132 deletions(-) diff --git a/miden-crypto/src/merkle/sparse_path.rs b/miden-crypto/src/merkle/sparse_path.rs index a86095b..a918510 100644 --- a/miden-crypto/src/merkle/sparse_path.rs +++ b/miden-crypto/src/merkle/sparse_path.rs @@ -561,145 +561,34 @@ mod tests { assert_eq!(sparse_path.empty_nodes_mask, EMPTY_BITS); - // Depth 8. - { - let depth: u8 = 8; + // Keep track of how many non-empty nodes we have seen + let mut nonempty_idx = 0; - // Check that the way we calculate these indices is correct. + // Test starting from the deepest nodes (depth 8) + for depth in (1..=8).rev() { let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 0); + let bit = 1 << (depth - 1); - // Check that the way we calculate these bitmasks is correct. - let bit = 0b1000_0000; - assert_eq!(bit, 1 << (depth - 1)); - - // Check that the depth-8 bit is not set... - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert!(!is_set); - // ...which should match the status of the `sparse_nodes` element being `None`. - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - - // And finally, check that we can calculate non-empty indices correctly. - let control_node = raw_nodes.get(idx).unwrap(); - let nonempty_idx: usize = 0; - assert_eq!(sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()), nonempty_idx); - let test_node = sparse_path.nodes.get(nonempty_idx).unwrap(); - assert_eq!(test_node, control_node); - } - - // Rinse and repeat for each remaining depth. - - // Depth 7. - { - let depth: u8 = 7; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 1); - let bit = 0b0100_0000; - assert_eq!(bit, 1 << (depth - 1)); - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert!(is_set); - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - - let &test_node = sparse_nodes.get(idx).unwrap(); - assert_eq!(test_node, None); - } - - // Depth 6. - { - let depth: u8 = 6; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 2); - let bit = 0b0010_0000; - assert_eq!(bit, 1 << (depth - 1)); + // Check that the depth bit is set correctly... let is_set = (sparse_path.empty_nodes_mask & bit) != 0; assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - assert!(is_set); - let &test_node = sparse_nodes.get(idx).unwrap(); - assert_eq!(test_node, None); - } + if is_set { + // Check that we don't return digests for empty nodes + let &test_node = sparse_nodes.get(idx).unwrap(); + assert_eq!(test_node, None); + } else { + // Check that we can calculate non-empty indices correctly. + let control_node = raw_nodes.get(idx).unwrap(); + assert_eq!( + sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()), + nonempty_idx + ); + let test_node = sparse_path.nodes.get(nonempty_idx).unwrap(); + assert_eq!(test_node, control_node); - // Depth 5. - { - let depth: u8 = 5; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 3); - let bit = 0b0001_0000; - assert_eq!(bit, 1 << (depth - 1)); - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - assert!(!is_set); - - let control_node = raw_nodes.get(idx).unwrap(); - let nonempty_idx: usize = 1; - assert_eq!(sparse_path.nodes.get(nonempty_idx).unwrap(), control_node); - assert_eq!(sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()), nonempty_idx,); - let test_node = sparse_path.nodes.get(nonempty_idx).unwrap(); - assert_eq!(test_node, control_node); - } - - // Depth 4. - { - let depth: u8 = 4; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 4); - let bit = 0b0000_1000; - assert_eq!(bit, 1 << (depth - 1)); - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - assert!(!is_set); - - let control_node = raw_nodes.get(idx).unwrap(); - let nonempty_idx: usize = 2; - assert_eq!(sparse_path.nodes.get(nonempty_idx).unwrap(), control_node); - assert_eq!(sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()), nonempty_idx,); - let test_node = sparse_path.nodes.get(nonempty_idx).unwrap(); - assert_eq!(test_node, control_node); - } - - // Depth 3. - { - let depth: u8 = 3; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 5); - let bit = 0b0000_0100; - assert_eq!(bit, 1 << (depth - 1)); - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert!(is_set); - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - - let &test_node = sparse_nodes.get(idx).unwrap(); - assert_eq!(test_node, None); - } - - // Depth 2. - { - let depth: u8 = 2; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 6); - let bit = 0b0000_0010; - assert_eq!(bit, 1 << (depth - 1)); - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert!(is_set); - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - - let &test_node = sparse_nodes.get(idx).unwrap(); - assert_eq!(test_node, None); - } - - // Depth 1. - { - let depth: u8 = 1; - let idx = (sparse_path.depth() - depth) as usize; - assert_eq!(idx, 7); - let bit = 0b0000_0001; - assert_eq!(bit, 1 << (depth - 1)); - let is_set = (sparse_path.empty_nodes_mask & bit) != 0; - assert!(is_set); - assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none()); - - let &test_node = sparse_nodes.get(idx).unwrap(); - assert_eq!(test_node, None); + nonempty_idx += 1; + } } }