mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-12 00:51:29 +01:00
store: remove SimpleSMT/MerkleTree/Mmr add/with methods
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use super::mmr::{Mmr, MmrPeaks};
|
||||
use super::mmr::Mmr;
|
||||
use super::{
|
||||
BTreeMap, EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, MerklePathSet, MerkleTree,
|
||||
NodeIndex, RootPath, Rpo256, RpoDigest, SimpleSmt, ValuePath, Vec, Word,
|
||||
BTreeMap, EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, MerklePathSet, NodeIndex,
|
||||
RootPath, Rpo256, RpoDigest, ValuePath, Vec, Word,
|
||||
};
|
||||
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
|
||||
|
||||
@@ -47,9 +47,13 @@ pub struct Node {
|
||||
/// // the store is initialized with the SMT empty nodes
|
||||
/// assert_eq!(store.num_internal_nodes(), 255);
|
||||
///
|
||||
/// let tree1 = MerkleTree::new(vec![A, B, C, D, E, F, G, H0]).unwrap();
|
||||
/// let tree2 = MerkleTree::new(vec![A, B, C, D, E, F, G, H1]).unwrap();
|
||||
///
|
||||
/// // populates the store with two merkle trees, common nodes are shared
|
||||
/// store.add_merkle_tree([A, B, C, D, E, F, G, H0]);
|
||||
/// store.add_merkle_tree([A, B, C, D, E, F, G, H1]);
|
||||
/// store
|
||||
/// .extend(tree1.inner_nodes())
|
||||
/// .extend(tree2.inner_nodes());
|
||||
///
|
||||
/// // every leaf except the last are the same
|
||||
/// for i in 0..7 {
|
||||
@@ -111,31 +115,6 @@ impl MerkleStore {
|
||||
MerkleStore { nodes }
|
||||
}
|
||||
|
||||
/// Appends the provided merkle tree represented by its `leaves` to the set.
|
||||
pub fn with_merkle_tree<I>(mut self, leaves: I) -> Result<Self, MerkleError>
|
||||
where
|
||||
I: IntoIterator<Item = Word>,
|
||||
{
|
||||
self.add_merkle_tree(leaves)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// 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(depth, entries)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Appends the provided merkle path set.
|
||||
pub fn with_merkle_path(
|
||||
mut self,
|
||||
@@ -156,15 +135,6 @@ impl MerkleStore {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Appends the provided [Mmr] represented by its `leaves` to the set.
|
||||
pub fn with_mmr<I>(mut self, leaves: I) -> Result<Self, MerkleError>
|
||||
where
|
||||
I: IntoIterator<Item = Word>,
|
||||
{
|
||||
self.add_mmr(leaves)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
// PUBLIC ACCESSORS
|
||||
// --------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -324,54 +294,6 @@ impl MerkleStore {
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds all the nodes of a Merkle tree represented by `leaves`.
|
||||
///
|
||||
/// This will instantiate a Merkle tree using `leaves` and include all the nodes into the
|
||||
/// store.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This method may return the following errors:
|
||||
/// - `DepthTooSmall` if leaves is empty or contains only 1 element
|
||||
/// - `NumLeavesNotPowerOfTwo` if the number of leaves is not a power-of-two
|
||||
pub fn add_merkle_tree<I>(&mut self, leaves: I) -> Result<Word, MerkleError>
|
||||
where
|
||||
I: IntoIterator<Item = Word>,
|
||||
{
|
||||
let leaves: Vec<_> = leaves.into_iter().collect();
|
||||
if leaves.len() < 2 {
|
||||
return Err(MerkleError::DepthTooSmall(leaves.len() as u8));
|
||||
}
|
||||
|
||||
let tree = MerkleTree::new(leaves)?;
|
||||
self.extend(tree.inner_nodes());
|
||||
|
||||
Ok(tree.root())
|
||||
}
|
||||
|
||||
/// Adds a Sparse Merkle tree defined by the specified `entries` to the store, and returns the
|
||||
/// root of the added tree.
|
||||
///
|
||||
/// The entries are expected to contain tuples of `(index, node)` describing nodes in the tree
|
||||
/// at `depth`.
|
||||
///
|
||||
/// # Errors
|
||||
/// 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(depth)?.with_leaves(entries)?;
|
||||
self.extend(smt.inner_nodes());
|
||||
|
||||
Ok(smt.root())
|
||||
}
|
||||
|
||||
/// Adds all the nodes of a Merkle path represented by `path`, opening to `node`. Returns the
|
||||
/// new root.
|
||||
///
|
||||
@@ -433,17 +355,6 @@ impl MerkleStore {
|
||||
Ok(root)
|
||||
}
|
||||
|
||||
/// Appends the provided [Mmr] into the store.
|
||||
pub fn add_mmr<I>(&mut self, leaves: I) -> Result<MmrPeaks, MerkleError>
|
||||
where
|
||||
I: IntoIterator<Item = Word>,
|
||||
{
|
||||
let mmr = Mmr::from(leaves);
|
||||
self.extend(mmr.inner_nodes());
|
||||
|
||||
Ok(mmr.accumulator())
|
||||
}
|
||||
|
||||
/// Sets a node to `value`.
|
||||
///
|
||||
/// # Errors
|
||||
|
||||
Reference in New Issue
Block a user