feat(smt): make leaf-emptiness knowable at the trait level

This commit is contained in:
Qyriad 2024-08-29 16:30:37 -06:00
parent 731afe30ec
commit 4f17c1deb1
3 changed files with 15 additions and 0 deletions

View file

@ -263,6 +263,13 @@ impl SparseMerkleTree<SMT_DEPTH> for Smt {
leaf.hash() leaf.hash()
} }
fn is_leaf_empty(leaf: &SmtLeaf) -> bool {
match leaf {
SmtLeaf::Empty(_) => true,
_ => false,
}
}
fn hash_prospective_leaf(&self, key: &RpoDigest, value: &Word) -> RpoDigest { fn hash_prospective_leaf(&self, key: &RpoDigest, value: &Word) -> RpoDigest {
// Future work could avoid cloning the leaf by mirroring some of the insertion logic and // Future work could avoid cloning the leaf by mirroring some of the insertion logic and
// hashing without an intermediate leaf, but cloning is only expensive for multi-leaves, // hashing without an intermediate leaf, but cloning is only expensive for multi-leaves,

View file

@ -167,6 +167,10 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
/// Returns the hash of a leaf /// Returns the hash of a leaf
fn hash_leaf(leaf: &Self::Leaf) -> RpoDigest; fn hash_leaf(leaf: &Self::Leaf) -> RpoDigest;
/// Returns true if a leaf is completely sparse and does not hold any key-value pairs.
#[allow(dead_code)]
fn is_leaf_empty(leaf: &Self::Leaf) -> bool;
/// Returns the hash of a leaf if the leaf WERE inserted into the tree, /// Returns the hash of a leaf if the leaf WERE inserted into the tree,
/// without performing any insertion or other mutation. /// without performing any insertion or other mutation.
/// ///

View file

@ -302,6 +302,10 @@ impl<const DEPTH: u8> SparseMerkleTree<DEPTH> for SimpleSmt<DEPTH> {
leaf.into() leaf.into()
} }
fn is_leaf_empty(leaf: &Word) -> bool {
*leaf == Self::EMPTY_VALUE
}
fn hash_prospective_leaf(&self, _key: &LeafIndex<DEPTH>, value: &Word) -> RpoDigest { fn hash_prospective_leaf(&self, _key: &LeafIndex<DEPTH>, value: &Word) -> RpoDigest {
Self::hash_leaf(value) Self::hash_leaf(value)
} }