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,17 +79,15 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
// PROVIDED METHODS
// ---------------------------------------------------------------------------------------------
/// 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);
/// Returns a [`MerklePath`] to the specified key.
///
/// Mostly this is an implementation detail of [`Self::open()`].
fn path(&self, key: &Self::Key) -> MerklePath {
let mut index: NodeIndex = {
let leaf_index: LeafIndex<DEPTH> = Self::key_to_leaf_index(key);
leaf_index.into()
};
let merkle_path = {
let mut path = Vec::with_capacity(index.depth() as usize);
for _ in 0..index.depth() {
let is_right = index.is_value_odd();
@ -100,7 +98,13 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
}
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)
}