WIP: smt: factor out MerklePath logic

This commit is contained in:
Qyriad 2025-02-25 21:02:08 +01:00
parent 1e87cd60ff
commit 901816f139

View file

@ -79,28 +79,32 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
// PROVIDED METHODS // PROVIDED METHODS
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
/// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a Merkle /// Returns a [`MerklePath`] to the specified key.
/// path to the leaf, as well as the leaf itself. ///
fn open(&self, key: &Self::Key) -> Self::Opening { /// Mostly this is an implementation detail of [`Self::open()`].
let leaf = self.get_leaf(key); fn path(&self, key: &Self::Key) -> MerklePath {
let mut index: NodeIndex = { let mut index: NodeIndex = {
let leaf_index: LeafIndex<DEPTH> = Self::key_to_leaf_index(key); let leaf_index: LeafIndex<DEPTH> = Self::key_to_leaf_index(key);
leaf_index.into() leaf_index.into()
}; };
let merkle_path = { let mut path = Vec::with_capacity(index.depth() as usize);
let mut path = Vec::with_capacity(index.depth() as usize); for _ in 0..index.depth() {
for _ in 0..index.depth() { let is_right = index.is_value_odd();
let is_right = index.is_value_odd(); index.move_up();
index.move_up(); let InnerNode { left, right } = self.get_inner_node(index);
let InnerNode { left, right } = self.get_inner_node(index); let value = if is_right { left } else { right };
let value = if is_right { left } else { right }; path.push(value);
path.push(value); }
}
MerklePath::new(path) MerklePath::new(path)
}; }
/// 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.
fn open(&self, key: &Self::Key) -> Self::Opening {
let leaf = self.get_leaf(key);
let merkle_path = self.path(key);
Self::path_and_leaf_to_opening(merkle_path, leaf) Self::path_and_leaf_to_opening(merkle_path, leaf)
} }