From 72662c7e09c229c54b29175e480816ab57dc5fe4 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Thu, 29 Aug 2024 16:30:37 -0600 Subject: [PATCH] feat(smt): make leaf-emptiness knowable at the trait level --- src/merkle/smt/full/mod.rs | 7 +++++++ src/merkle/smt/mod.rs | 7 +++++++ src/merkle/smt/simple/mod.rs | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/src/merkle/smt/full/mod.rs b/src/merkle/smt/full/mod.rs index 2c38256..adf0a0a 100644 --- a/src/merkle/smt/full/mod.rs +++ b/src/merkle/smt/full/mod.rs @@ -285,6 +285,13 @@ impl SparseMerkleTree for Smt { } } + fn is_leaf_empty(leaf: &SmtLeaf) -> bool { + match leaf { + SmtLeaf::Empty(_) => true, + _ => false, + } + } + fn key_to_leaf_index(key: &RpoDigest) -> LeafIndex { let most_significant_felt = key[3]; LeafIndex::new_max_depth(most_significant_felt.as_int()) diff --git a/src/merkle/smt/mod.rs b/src/merkle/smt/mod.rs index a2f3735..c23c855 100644 --- a/src/merkle/smt/mod.rs +++ b/src/merkle/smt/mod.rs @@ -167,6 +167,13 @@ pub(crate) trait SparseMerkleTree { /// Returns the hash of a leaf 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 /// 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 diff --git a/src/merkle/smt/simple/mod.rs b/src/merkle/smt/simple/mod.rs index 66c2a62..6ab2f00 100644 --- a/src/merkle/smt/simple/mod.rs +++ b/src/merkle/smt/simple/mod.rs @@ -311,6 +311,10 @@ impl SparseMerkleTree for SimpleSmt { *value } + fn is_leaf_empty(leaf: &Word) -> bool { + *leaf == Self::EMPTY_VALUE + } + fn key_to_leaf_index(key: &LeafIndex) -> LeafIndex { *key }