smt: change SimpleSmt::open() to return a sparse path

This commit is contained in:
Qyriad 2025-04-04 15:14:27 +02:00
parent 39397c707d
commit 21b2c08cf9
2 changed files with 24 additions and 2 deletions

View file

@ -1,5 +1,7 @@
use alloc::collections::BTreeSet; use alloc::collections::BTreeSet;
use crate::merkle::{SparseMerklePath, SparseValuePath};
use super::{ use super::{
super::ValuePath, EMPTY_WORD, EmptySubtreeRoots, InnerNode, InnerNodeInfo, InnerNodes, super::ValuePath, EMPTY_WORD, EmptySubtreeRoots, InnerNode, InnerNodeInfo, InnerNodes,
LeafIndex, MerkleError, MerklePath, MutationSet, NodeIndex, RpoDigest, SMT_MAX_DEPTH, LeafIndex, MerkleError, MerklePath, MutationSet, NodeIndex, RpoDigest, SMT_MAX_DEPTH,
@ -169,8 +171,15 @@ impl<const DEPTH: u8> SimpleSmt<DEPTH> {
/// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a Merkle /// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a Merkle
/// path to the leaf, as well as the leaf itself. /// path to the leaf, as well as the leaf itself.
pub fn open(&self, key: &LeafIndex<DEPTH>) -> ValuePath { pub fn open(&self, key: &LeafIndex<DEPTH>) -> SparseValuePath {
<Self as SparseMerkleTree<DEPTH>>::open(self, key) let value = RpoDigest::new(self.get_value(key));
let nodes = key.index.proof_indices().map(|index| self.get_hash(index));
// `from_sized_iter()` returns an error if there are more nodes than `SMT_MAX_DEPTH`, but
// this could only happen if we have more levels than `SMT_MAX_DEPTH` ourselves, which is
// guarded against in `SimpleSmt::new()`.
let path = SparseMerklePath::from_sized_iter(nodes).unwrap();
SparseValuePath { value, path }
} }
/// Returns a boolean value indicating whether the SMT is empty. /// Returns a boolean value indicating whether the SMT is empty.

View file

@ -495,6 +495,9 @@ mod tests {
/// Manually test the exact bit patterns for a sample path of 8 nodes, including both empty and /// Manually test the exact bit patterns for a sample path of 8 nodes, including both empty and
/// non-empty nodes. /// non-empty nodes.
///
/// This also offers an overview of what each part of the bit-math involved means and
/// represents.
#[test] #[test]
fn test_sparse_bits() { fn test_sparse_bits() {
const DEPTH: u8 = 8; const DEPTH: u8 = 8;
@ -547,14 +550,22 @@ mod tests {
// Depth 8. // Depth 8.
{ {
let depth: u8 = 8; let depth: u8 = 8;
// Check that the way we calculate these indices is correct.
let idx = (sparse_path.depth() - depth) as usize; let idx = (sparse_path.depth() - depth) as usize;
assert_eq!(idx, 0); assert_eq!(idx, 0);
// Check that the way we calculate these bitmasks is correct.
let bit = 0b1000_0000; let bit = 0b1000_0000;
assert_eq!(bit, 1 << (depth - 1)); assert_eq!(bit, 1 << (depth - 1));
// Check that the depth-8 bit is not set...
let is_set = (sparse_path.empty_nodes & bit) != 0; let is_set = (sparse_path.empty_nodes & bit) != 0;
assert!(!is_set); 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()); 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 control_node = raw_nodes.get(idx).unwrap();
let nonempty_idx: usize = 0; let nonempty_idx: usize = 0;
assert_eq!(sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()), nonempty_idx); assert_eq!(sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()), nonempty_idx);
@ -562,6 +573,8 @@ mod tests {
assert_eq!(test_node, control_node); assert_eq!(test_node, control_node);
} }
// Rinse and repeat for each remaining depth.
// Depth 7. // Depth 7.
{ {
let depth: u8 = 7; let depth: u8 = 7;