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 e69f7b6973
commit 72662c7e09
3 changed files with 18 additions and 0 deletions

View file

@ -285,6 +285,13 @@ impl SparseMerkleTree<SMT_DEPTH> for Smt {
} }
} }
fn is_leaf_empty(leaf: &SmtLeaf) -> bool {
match leaf {
SmtLeaf::Empty(_) => true,
_ => false,
}
}
fn key_to_leaf_index(key: &RpoDigest) -> LeafIndex<SMT_DEPTH> { fn key_to_leaf_index(key: &RpoDigest) -> LeafIndex<SMT_DEPTH> {
let most_significant_felt = key[3]; let most_significant_felt = key[3];
LeafIndex::new_max_depth(most_significant_felt.as_int()) LeafIndex::new_max_depth(most_significant_felt.as_int())

View file

@ -167,6 +167,13 @@ 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 what `existing_leaf` would look like if `key` and `value` WERE inserted into the
/// tree, without mutating the tree itself.
///
/// `existing_leaf` must have the same index as the key, or the result will be meaningless. To /// `existing_leaf` must have the same index as the key, or the result will be meaningless. To
/// get a prospective leaf based on the current state of the tree, use `self.get_leaf(key)` as /// get a prospective leaf based on the current state of the tree, use `self.get_leaf(key)` as
/// the argument for `existing_leaf`. The return value from this function can be chained back /// the argument for `existing_leaf`. The return value from this function can be chained back

View file

@ -311,6 +311,10 @@ impl<const DEPTH: u8> SparseMerkleTree<DEPTH> for SimpleSmt<DEPTH> {
*value *value
} }
fn is_leaf_empty(leaf: &Word) -> bool {
*leaf == Self::EMPTY_VALUE
}
fn key_to_leaf_index(key: &LeafIndex<DEPTH>) -> LeafIndex<DEPTH> { fn key_to_leaf_index(key: &LeafIndex<DEPTH>) -> LeafIndex<DEPTH> {
*key *key
} }