Compare commits

..

3 commits

Author SHA1 Message Date
1795ef4ba3 WIP: smt: implement root-checked insertion 2024-08-22 15:54:24 -06:00
00e2be79a4 feat: export Merkle get_value() in the trait 2024-08-22 15:54:24 -06:00
52ab7bca97 feat: impl hashing Merkle leaves that don't yet exist
This commit implements 'prospective leaf hashing' -- computing what the
hash of a sparse Merkle tree leaf *would* be for a key-value insertion
without actually performing that insertion.

For SimpleSmt, this is trivial, since the leaf hash and its payload are
the same.

For the full Smt, the new leaf payload (and thus, its hash) depend on
the existing payload in that leaf, making the prospective hash logic a
combination of the normal insertion logic and the normal hash logic. But
because we're only interested in the hash and not the intermediate
value, we can skip allocations and sorts for the payload itself.
2024-08-22 15:54:24 -06:00
3 changed files with 4 additions and 1 deletions

View file

@ -336,7 +336,7 @@ impl SparseMerkleTree<SMT_DEPTH> for Smt {
let (before_pos, rest) = pairs.split_at(pos);
let with_pos_removed = rest.iter().copied().skip(1);
let middle = iter::once(new_pair).filter(|_| !is_removal);
let middle = if !is_removal { Some(new_pair) } else { None };
let elements: Vec<Felt> = before_pos
.iter()
.copied()

View file

@ -287,6 +287,7 @@ fn test_checked_insertion() {
assert_eq!(old_value_1, EMPTY_WORD);
assert_eq!(prospective, smt.get_leaf(&key_1).hash());
assert_eq!(smt.get_leaf(&key_1), SmtLeaf::Single((key_1, value_1)));
smt.root()

View file

@ -259,6 +259,8 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
/// Inserts a leaf node, and returns the value at the key if already exists
fn insert_value(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>;
/// Returns the value at the specified key. Recall that by definition, any key that hasn't been
/// updated is associated with [`Self::EMPTY_VALUE`].
fn get_value(&self, key: &Self::Key) -> Self::Value;
/// Returns the leaf at the specified index.