mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-11 08:31:30 +01:00
feat: Add depth as store SMT argument
Prior to this commit, MerkleStore allowed the creation of Sparse Merkle tree only with the maximum depth of 63. However, this doesn't fit the Tiered Sparse Merkle tree requirements, as it will contain trees of depth 16. This commit adds the `depth` argument to the MerkleStore methods that will create Sparse Merkle trees.
This commit is contained in:
@@ -115,13 +115,19 @@ impl MerkleStore {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Appends the provided sparse merkle tree represented by its `entries` to the set.
|
||||
pub fn with_sparse_merkle_tree<R, I>(mut self, entries: R) -> Result<Self, MerkleError>
|
||||
/// Appends the provided Sparse Merkle tree represented by its `entries` to the set.
|
||||
///
|
||||
/// For more information, check [MerkleStore::add_sparse_merkle_tree].
|
||||
pub fn with_sparse_merkle_tree<R, I>(
|
||||
mut self,
|
||||
depth: u8,
|
||||
entries: R,
|
||||
) -> Result<Self, MerkleError>
|
||||
where
|
||||
R: IntoIterator<IntoIter = I>,
|
||||
I: Iterator<Item = (u64, Word)> + ExactSizeIterator,
|
||||
{
|
||||
self.add_sparse_merkle_tree(entries)?;
|
||||
self.add_sparse_merkle_tree(depth, entries)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
@@ -272,20 +278,24 @@ impl MerkleStore {
|
||||
Ok(tree.nodes[1])
|
||||
}
|
||||
|
||||
/// Adds all the nodes of a Sparse Merkle tree represented by `entries`.
|
||||
/// Adds a Sparse Merkle tree defined by the specified `entries` to the store, and returns the
|
||||
/// root of the added tree.
|
||||
///
|
||||
/// This will instantiate a Sparse Merkle tree using `entries` and include all the nodes into
|
||||
/// the store.
|
||||
/// The entries are expected to contain tuples of `(index, node)` describing nodes in the tree
|
||||
/// at `depth`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This will return `InvalidEntriesCount` if the length of `entries` is not `63`.
|
||||
pub fn add_sparse_merkle_tree<R, I>(&mut self, entries: R) -> Result<Word, MerkleError>
|
||||
/// Returns an error if the provided `depth` is greater than [SimpleSmt::MAX_DEPTH].
|
||||
pub fn add_sparse_merkle_tree<R, I>(
|
||||
&mut self,
|
||||
depth: u8,
|
||||
entries: R,
|
||||
) -> Result<Word, MerkleError>
|
||||
where
|
||||
R: IntoIterator<IntoIter = I>,
|
||||
I: Iterator<Item = (u64, Word)> + ExactSizeIterator,
|
||||
{
|
||||
let smt = SimpleSmt::new(SimpleSmt::MAX_DEPTH)?.with_leaves(entries)?;
|
||||
let smt = SimpleSmt::new(depth)?.with_leaves(entries)?;
|
||||
for branch in smt.store.branches.values() {
|
||||
let parent = Rpo256::merge(&[branch.left, branch.right]);
|
||||
self.nodes.insert(
|
||||
|
||||
@@ -205,7 +205,7 @@ fn test_add_sparse_merkle_tree_one_level() -> Result<(), MerkleError> {
|
||||
let mut store = MerkleStore::default();
|
||||
let keys2: [u64; 2] = [0, 1];
|
||||
let leaves2: [Word; 2] = [int_to_node(1), int_to_node(2)];
|
||||
store.add_sparse_merkle_tree(keys2.into_iter().zip(leaves2.into_iter()))?;
|
||||
store.add_sparse_merkle_tree(48, keys2.into_iter().zip(leaves2.into_iter()))?;
|
||||
let smt = SimpleSmt::new(1)
|
||||
.unwrap()
|
||||
.with_leaves(keys2.into_iter().zip(leaves2.into_iter()))
|
||||
@@ -231,7 +231,10 @@ fn test_add_sparse_merkle_tree_one_level() -> Result<(), MerkleError> {
|
||||
#[test]
|
||||
fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
|
||||
let mut store = MerkleStore::default();
|
||||
store.add_sparse_merkle_tree(KEYS4.into_iter().zip(LEAVES4.into_iter()))?;
|
||||
store.add_sparse_merkle_tree(
|
||||
SimpleSmt::MAX_DEPTH,
|
||||
KEYS4.into_iter().zip(LEAVES4.into_iter()),
|
||||
)?;
|
||||
|
||||
let smt = SimpleSmt::new(SimpleSmt::MAX_DEPTH)
|
||||
.unwrap()
|
||||
@@ -584,9 +587,10 @@ fn test_constructors() -> Result<(), MerkleError> {
|
||||
assert_eq!(mtree.get_path(index)?, value_path.path);
|
||||
}
|
||||
|
||||
let depth = 32;
|
||||
let store = MerkleStore::default()
|
||||
.with_sparse_merkle_tree(KEYS4.into_iter().zip(LEAVES4.into_iter()))?;
|
||||
let smt = SimpleSmt::new(SimpleSmt::MAX_DEPTH)
|
||||
.with_sparse_merkle_tree(depth, KEYS4.into_iter().zip(LEAVES4.into_iter()))?;
|
||||
let smt = SimpleSmt::new(depth)
|
||||
.unwrap()
|
||||
.with_leaves(KEYS4.into_iter().zip(LEAVES4.into_iter()))
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user