arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
feat: add `Smt::is_empty` (#337)

Andrey Khmuro andrey@polygon.technology
Fri, 18 Oct 2024 00:27:50 +0300
commit

940cc04670924511777d9d3ef627a5ce3f528792

parent

e82baa35bb9e0eb0ca160e431aa5e9af33afaf8f

M CHANGELOG.md → CHANGELOG.md

@@ -6,6 +6,7 @@ - Standardised CI and Makefile across Miden repos (#323).

- Added `Smt::compute_mutations()` and `Smt::apply_mutations()` for validation-checked insertions (#327). - [BREAKING] Changed return value of the `Mmr::verify()` and `MerklePath::verify()` from `bool` to `Result<>` (#). +- Added `is_empty()` functions to the `SimpleSmt` and `Smt` structures. Added `EMPTY_ROOT` constant to the `SparseMerkleTree` trait (#337). ## 0.10.1 (2024-09-13)
M src/merkle/smt/full/mod.rs → src/merkle/smt/full/mod.rs

@@ -130,6 +130,12 @@ pub fn open(&self, key: &RpoDigest) -> SmtProof {

<Self as SparseMerkleTree<SMT_DEPTH>>::open(self, key) } + /// Returns a boolean value indicating whether the SMT is empty. + pub fn is_empty(&self) -> bool { + debug_assert_eq!(self.leaves.is_empty(), self.root == Self::EMPTY_ROOT); + self.root == Self::EMPTY_ROOT + } + // ITERATORS // --------------------------------------------------------------------------------------------

@@ -252,6 +258,7 @@ type Leaf = SmtLeaf;

type Opening = SmtProof; const EMPTY_VALUE: Self::Value = EMPTY_WORD; + const EMPTY_ROOT: RpoDigest = *EmptySubtreeRoots::entry(SMT_DEPTH, 0); fn root(&self) -> RpoDigest { self.root
M src/merkle/smt/full/tests.rs → src/merkle/smt/full/tests.rs

@@ -516,6 +516,16 @@ assert_eq!(&(key_2, value_2), entries.next().unwrap());

assert!(entries.next().is_none()); } +/// Tests that `EMPTY_ROOT` constant generated in the `Smt` equals to the root of the empty tree of +/// depth 64 +#[test] +fn test_smt_check_empty_root_constant() { + // get the root of the empty tree of depth 64 + let empty_root_64_depth = EmptySubtreeRoots::empty_hashes(64)[0]; + + assert_eq!(empty_root_64_depth, Smt::EMPTY_ROOT); +} + // SMT LEAF // --------------------------------------------------------------------------------------------
M src/merkle/smt/mod.rs → src/merkle/smt/mod.rs

@@ -56,6 +56,9 @@

/// The default value used to compute the hash of empty leaves const EMPTY_VALUE: Self::Value; + /// The root of the empty tree with provided DEPTH + const EMPTY_ROOT: RpoDigest; + // PROVIDED METHODS // ---------------------------------------------------------------------------------------------
M src/merkle/smt/simple/mod.rs → src/merkle/smt/simple/mod.rs

@@ -158,6 +158,12 @@ pub fn open(&self, key: &LeafIndex<DEPTH>) -> ValuePath {

<Self as SparseMerkleTree<DEPTH>>::open(self, key) } + /// Returns a boolean value indicating whether the SMT is empty. + pub fn is_empty(&self) -> bool { + debug_assert_eq!(self.leaves.is_empty(), self.root == Self::EMPTY_ROOT); + self.root == Self::EMPTY_ROOT + } + // ITERATORS // --------------------------------------------------------------------------------------------

@@ -298,6 +304,7 @@ type Leaf = Word;

type Opening = ValuePath; const EMPTY_VALUE: Self::Value = EMPTY_WORD; + const EMPTY_ROOT: RpoDigest = *EmptySubtreeRoots::entry(DEPTH, 0); fn root(&self) -> RpoDigest { self.root
M src/merkle/smt/simple/tests.rs → src/merkle/smt/simple/tests.rs

@@ -444,6 +444,23 @@

assert_eq!(tree.root(), *EmptySubtreeRoots::entry(DEPTH, 0)); } +/// Tests that `EMPTY_ROOT` constant generated in the `SimpleSmt` equals to the root of the empty +/// tree of depth 64 +#[test] +fn test_simplesmt_check_empty_root_constant() { + // get the root of the empty tree of depth 64 + let empty_root_64_depth = EmptySubtreeRoots::empty_hashes(64)[0]; + assert_eq!(empty_root_64_depth, SimpleSmt::<64>::EMPTY_ROOT); + + // get the root of the empty tree of depth 32 + let empty_root_32_depth = EmptySubtreeRoots::empty_hashes(32)[0]; + assert_eq!(empty_root_32_depth, SimpleSmt::<32>::EMPTY_ROOT); + + // get the root of the empty tree of depth 0 + let empty_root_1_depth = EmptySubtreeRoots::empty_hashes(1)[0]; + assert_eq!(empty_root_1_depth, SimpleSmt::<1>::EMPTY_ROOT); +} + // HELPER FUNCTIONS // --------------------------------------------------------------------------------------------