arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/src/merkle/store/mod.rs
src/merkle/store/mod.rsRaw
  1use super::{
  2    mmr::Mmr, BTreeMap, EmptySubtreeRoots, InnerNodeInfo, KvMap, MerkleError, MerklePath,
  3    MerkleTree, NodeIndex, PartialMerkleTree, RecordingMap, RootPath, Rpo256, RpoDigest, SimpleSmt,
  4    Smt, ValuePath, Vec,
  5};
  6use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
  7use core::borrow::Borrow;
  8
  9#[cfg(test)]
 10mod tests;
 11
 12// MERKLE STORE
 13// ================================================================================================
 14
 15/// A default [MerkleStore] which uses a simple [BTreeMap] as the backing storage.
 16pub type DefaultMerkleStore = MerkleStore<BTreeMap<RpoDigest, StoreNode>>;
 17
 18/// A [MerkleStore] with recording capabilities which uses [RecordingMap] as the backing storage.
 19pub type RecordingMerkleStore = MerkleStore<RecordingMap<RpoDigest, StoreNode>>;
 20
 21#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
 22#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
 23pub struct StoreNode {
 24    left: RpoDigest,
 25    right: RpoDigest,
 26}
 27
 28/// An in-memory data store for Merkelized data.
 29///
 30/// This is a in memory data store for Merkle trees, this store allows all the nodes of multiple
 31/// trees to live as long as necessary and without duplication, this allows the implementation of
 32/// space efficient persistent data structures.
 33///
 34/// Example usage:
 35///
 36/// ```rust
 37/// # use miden_crypto::{ZERO, Felt, Word};
 38/// # use miden_crypto::merkle::{NodeIndex, MerkleStore, MerkleTree};
 39/// # use miden_crypto::hash::rpo::Rpo256;
 40/// # const fn int_to_node(value: u64) -> Word {
 41/// #     [Felt::new(value), ZERO, ZERO, ZERO]
 42/// # }
 43/// # let A = int_to_node(1);
 44/// # let B = int_to_node(2);
 45/// # let C = int_to_node(3);
 46/// # let D = int_to_node(4);
 47/// # let E = int_to_node(5);
 48/// # let F = int_to_node(6);
 49/// # let G = int_to_node(7);
 50/// # let H0 = int_to_node(8);
 51/// # let H1 = int_to_node(9);
 52/// # let T0 = MerkleTree::new([A, B, C, D, E, F, G, H0].to_vec()).expect("even number of leaves provided");
 53/// # let T1 = MerkleTree::new([A, B, C, D, E, F, G, H1].to_vec()).expect("even number of leaves provided");
 54/// # let ROOT0 = T0.root();
 55/// # let ROOT1 = T1.root();
 56/// let mut store: MerkleStore = MerkleStore::new();
 57///
 58/// // the store is initialized with the SMT empty nodes
 59/// assert_eq!(store.num_internal_nodes(), 255);
 60///
 61/// let tree1 = MerkleTree::new(vec![A, B, C, D, E, F, G, H0]).unwrap();
 62/// let tree2 = MerkleTree::new(vec![A, B, C, D, E, F, G, H1]).unwrap();
 63///
 64/// // populates the store with two merkle trees, common nodes are shared
 65/// store.extend(tree1.inner_nodes());
 66/// store.extend(tree2.inner_nodes());
 67///
 68/// // every leaf except the last are the same
 69/// for i in 0..7 {
 70///     let idx0 = NodeIndex::new(3, i).unwrap();
 71///     let d0 = store.get_node(ROOT0, idx0).unwrap();
 72///     let idx1 = NodeIndex::new(3, i).unwrap();
 73///     let d1 = store.get_node(ROOT1, idx1).unwrap();
 74///     assert_eq!(d0, d1, "Both trees have the same leaf at pos {i}");
 75/// }
 76///
 77/// // The leafs A-B-C-D are the same for both trees, so are their 2 immediate parents
 78/// for i in 0..4 {
 79///     let idx0 = NodeIndex::new(3, i).unwrap();
 80///     let d0 = store.get_path(ROOT0, idx0).unwrap();
 81///     let idx1 = NodeIndex::new(3, i).unwrap();
 82///     let d1 = store.get_path(ROOT1, idx1).unwrap();
 83///     assert_eq!(d0.path[0..2], d1.path[0..2], "Both sub-trees are equal up to two levels");
 84/// }
 85///
 86/// // Common internal nodes are shared, the two added trees have a total of 30, but the store has
 87/// // only 10 new entries, corresponding to the 10 unique internal nodes of these trees.
 88/// assert_eq!(store.num_internal_nodes() - 255, 10);
 89/// ```
 90#[derive(Debug, Clone, Eq, PartialEq)]
 91#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
 92pub struct MerkleStore<T: KvMap<RpoDigest, StoreNode> = BTreeMap<RpoDigest, StoreNode>> {
 93    nodes: T,
 94}
 95
 96impl<T: KvMap<RpoDigest, StoreNode>> Default for MerkleStore<T> {
 97    fn default() -> Self {
 98        Self::new()
 99    }
100}
101
102impl<T: KvMap<RpoDigest, StoreNode>> MerkleStore<T> {
103    // CONSTRUCTORS
104    // --------------------------------------------------------------------------------------------
105
106    /// Creates an empty `MerkleStore` instance.
107    pub fn new() -> MerkleStore<T> {
108        // pre-populate the store with the empty hashes
109        let nodes = empty_hashes().into_iter().collect();
110        MerkleStore { nodes }
111    }
112
113    // PUBLIC ACCESSORS
114    // --------------------------------------------------------------------------------------------
115
116    /// Return a count of the non-leaf nodes in the store.
117    pub fn num_internal_nodes(&self) -> usize {
118        self.nodes.len()
119    }
120
121    /// Returns the node at `index` rooted on the tree `root`.
122    ///
123    /// # Errors
124    /// This method can return the following errors:
125    /// - `RootNotInStore` if the `root` is not present in the store.
126    /// - `NodeNotInStore` if a node needed to traverse from `root` to `index` is not present in
127    ///   the store.
128    pub fn get_node(&self, root: RpoDigest, index: NodeIndex) -> Result<RpoDigest, MerkleError> {
129        let mut hash = root;
130
131        // corner case: check the root is in the store when called with index `NodeIndex::root()`
132        self.nodes.get(&hash).ok_or(MerkleError::RootNotInStore(hash))?;
133
134        for i in (0..index.depth()).rev() {
135            let node = self.nodes.get(&hash).ok_or(MerkleError::NodeNotInStore(hash, index))?;
136
137            let bit = (index.value() >> i) & 1;
138            hash = if bit == 0 { node.left } else { node.right }
139        }
140
141        Ok(hash)
142    }
143
144    /// Returns the node at the specified `index` and its opening to the `root`.
145    ///
146    /// The path starts at the sibling of the target leaf.
147    ///
148    /// # Errors
149    /// This method can return the following errors:
150    /// - `RootNotInStore` if the `root` is not present in the store.
151    /// - `NodeNotInStore` if a node needed to traverse from `root` to `index` is not present in
152    ///   the store.
153    pub fn get_path(&self, root: RpoDigest, index: NodeIndex) -> Result<ValuePath, MerkleError> {
154        let mut hash = root;
155        let mut path = Vec::with_capacity(index.depth().into());
156
157        // corner case: check the root is in the store when called with index `NodeIndex::root()`
158        self.nodes.get(&hash).ok_or(MerkleError::RootNotInStore(hash))?;
159
160        for i in (0..index.depth()).rev() {
161            let node = self.nodes.get(&hash).ok_or(MerkleError::NodeNotInStore(hash, index))?;
162
163            let bit = (index.value() >> i) & 1;
164            hash = if bit == 0 {
165                path.push(node.right);
166                node.left
167            } else {
168                path.push(node.left);
169                node.right
170            }
171        }
172
173        // the path is computed from root to leaf, so it must be reversed
174        path.reverse();
175
176        Ok(ValuePath::new(hash, MerklePath::new(path)))
177    }
178
179    // LEAF TRAVERSAL
180    // --------------------------------------------------------------------------------------------
181
182    /// Returns the depth of the first leaf or an empty node encountered while traversing the tree
183    /// from the specified root down according to the provided index.
184    ///
185    /// The `tree_depth` parameter specifies the depth of the tree rooted at `root`. The
186    /// maximum value the argument accepts is [u64::BITS].
187    ///
188    /// # Errors
189    /// Will return an error if:
190    /// - The provided root is not found.
191    /// - The provided `tree_depth` is greater than 64.
192    /// - The provided `index` is not valid for a depth equivalent to `tree_depth`.
193    /// - No leaf or an empty node was found while traversing the tree down to `tree_depth`.
194    pub fn get_leaf_depth(
195        &self,
196        root: RpoDigest,
197        tree_depth: u8,
198        index: u64,
199    ) -> Result<u8, MerkleError> {
200        // validate depth and index
201        if tree_depth > 64 {
202            return Err(MerkleError::DepthTooBig(tree_depth as u64));
203        }
204        NodeIndex::new(tree_depth, index)?;
205
206        // check if the root exists, providing the proper error report if it doesn't
207        let empty = EmptySubtreeRoots::empty_hashes(tree_depth);
208        let mut hash = root;
209        if !self.nodes.contains_key(&hash) {
210            return Err(MerkleError::RootNotInStore(hash));
211        }
212
213        // we traverse from root to leaf, so the path is reversed
214        let mut path = (index << (64 - tree_depth)).reverse_bits();
215
216        // iterate every depth and reconstruct the path from root to leaf
217        for depth in 0..=tree_depth {
218            // we short-circuit if an empty node has been found
219            if hash == empty[depth as usize] {
220                return Ok(depth);
221            }
222
223            // fetch the children pair, mapped by its parent hash
224            let children = match self.nodes.get(&hash) {
225                Some(node) => node,
226                None => return Ok(depth),
227            };
228
229            // traverse down
230            hash = if path & 1 == 0 { children.left } else { children.right };
231            path >>= 1;
232        }
233
234        // return an error because we exhausted the index but didn't find either a leaf or an
235        // empty node
236        Err(MerkleError::DepthTooBig(tree_depth as u64 + 1))
237    }
238
239    /// Returns index and value of a leaf node which is the only leaf node in a subtree defined by
240    /// the provided root. If the subtree contains zero or more than one leaf nodes None is
241    /// returned.
242    ///
243    /// The `tree_depth` parameter specifies the depth of the parent tree such that `root` is
244    /// located in this tree at `root_index`. The maximum value the argument accepts is
245    /// [u64::BITS].
246    ///
247    /// # Errors
248    /// Will return an error if:
249    /// - The provided root is not found.
250    /// - The provided `tree_depth` is greater than 64.
251    /// - The provided `root_index` has depth greater than `tree_depth`.
252    /// - A lone node at depth `tree_depth` is not a leaf node.
253    pub fn find_lone_leaf(
254        &self,
255        root: RpoDigest,
256        root_index: NodeIndex,
257        tree_depth: u8,
258    ) -> Result<Option<(NodeIndex, RpoDigest)>, MerkleError> {
259        // we set max depth at u64::BITS as this is the largest meaningful value for a 64-bit index
260        const MAX_DEPTH: u8 = u64::BITS as u8;
261        if tree_depth > MAX_DEPTH {
262            return Err(MerkleError::DepthTooBig(tree_depth as u64));
263        }
264        let empty = EmptySubtreeRoots::empty_hashes(MAX_DEPTH);
265
266        let mut node = root;
267        if !self.nodes.contains_key(&node) {
268            return Err(MerkleError::RootNotInStore(node));
269        }
270
271        let mut index = root_index;
272        if index.depth() > tree_depth {
273            return Err(MerkleError::DepthTooBig(index.depth() as u64));
274        }
275
276        // traverse down following the path of single non-empty nodes; this works because if a
277        // node has two empty children it cannot contain a lone leaf. similarly if a node has
278        // two non-empty children it must contain at least two leaves.
279        for depth in index.depth()..tree_depth {
280            // if the node is a leaf, return; otherwise, examine the node's children
281            let children = match self.nodes.get(&node) {
282                Some(node) => node,
283                None => return Ok(Some((index, node))),
284            };
285
286            let empty_node = empty[depth as usize + 1];
287            node = if children.left != empty_node && children.right == empty_node {
288                index = index.left_child();
289                children.left
290            } else if children.left == empty_node && children.right != empty_node {
291                index = index.right_child();
292                children.right
293            } else {
294                return Ok(None);
295            };
296        }
297
298        // if we are here, we got to `tree_depth`; thus, either the current node is a leaf node,
299        // and so we return it, or it is an internal node, and then we return an error
300        if self.nodes.contains_key(&node) {
301            Err(MerkleError::DepthTooBig(tree_depth as u64 + 1))
302        } else {
303            Ok(Some((index, node)))
304        }
305    }
306
307    // DATA EXTRACTORS
308    // --------------------------------------------------------------------------------------------
309
310    /// Returns a subset of this Merkle store such that the returned Merkle store contains all
311    /// nodes which are descendants of the specified roots.
312    ///
313    /// The roots for which no descendants exist in this Merkle store are ignored.
314    pub fn subset<I, R>(&self, roots: I) -> MerkleStore<T>
315    where
316        I: Iterator<Item = R>,
317        R: Borrow<RpoDigest>,
318    {
319        let mut store = MerkleStore::new();
320        for root in roots {
321            let root = *root.borrow();
322            store.clone_tree_from(root, self);
323        }
324        store
325    }
326
327    /// Iterator over the inner nodes of the [MerkleStore].
328    pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
329        self.nodes
330            .iter()
331            .map(|(r, n)| InnerNodeInfo { value: *r, left: n.left, right: n.right })
332    }
333
334    /// Iterator over the non-empty leaves of the Merkle tree associated with the specified `root`
335    /// and `max_depth`.
336    pub fn non_empty_leaves(
337        &self,
338        root: RpoDigest,
339        max_depth: u8,
340    ) -> impl Iterator<Item = (NodeIndex, RpoDigest)> + '_ {
341        let empty_roots = EmptySubtreeRoots::empty_hashes(max_depth);
342        let mut stack = Vec::new();
343        stack.push((NodeIndex::new_unchecked(0, 0), root));
344
345        core::iter::from_fn(move || {
346            while let Some((index, node_hash)) = stack.pop() {
347                // if we are at the max depth then we have reached a leaf
348                if index.depth() == max_depth {
349                    return Some((index, node_hash));
350                }
351
352                // fetch the nodes children and push them onto the stack if they are not the roots
353                // of empty subtrees
354                if let Some(node) = self.nodes.get(&node_hash) {
355                    if !empty_roots.contains(&node.left) {
356                        stack.push((index.left_child(), node.left));
357                    }
358                    if !empty_roots.contains(&node.right) {
359                        stack.push((index.right_child(), node.right));
360                    }
361
362                // if the node is not in the store assume it is a leaf
363                } else {
364                    return Some((index, node_hash));
365                }
366            }
367
368            None
369        })
370    }
371
372    // STATE MUTATORS
373    // --------------------------------------------------------------------------------------------
374
375    /// Adds all the nodes of a Merkle path represented by `path`, opening to `node`. Returns the
376    /// new root.
377    ///
378    /// This will compute the sibling elements determined by the Merkle `path` and `node`, and
379    /// include all the nodes into the store.
380    pub fn add_merkle_path(
381        &mut self,
382        index: u64,
383        node: RpoDigest,
384        path: MerklePath,
385    ) -> Result<RpoDigest, MerkleError> {
386        let root = path.inner_nodes(index, node)?.fold(RpoDigest::default(), |_, node| {
387            let value: RpoDigest = node.value;
388            let left: RpoDigest = node.left;
389            let right: RpoDigest = node.right;
390
391            debug_assert_eq!(Rpo256::merge(&[left, right]), value);
392            self.nodes.insert(value, StoreNode { left, right });
393
394            node.value
395        });
396        Ok(root)
397    }
398
399    /// Adds all the nodes of multiple Merkle paths into the store.
400    ///
401    /// This will compute the sibling elements for each Merkle `path` and include all the nodes
402    /// into the store.
403    ///
404    /// For further reference, check [MerkleStore::add_merkle_path].
405    pub fn add_merkle_paths<I>(&mut self, paths: I) -> Result<(), MerkleError>
406    where
407        I: IntoIterator<Item = (u64, RpoDigest, MerklePath)>,
408    {
409        for (index_value, node, path) in paths.into_iter() {
410            self.add_merkle_path(index_value, node, path)?;
411        }
412        Ok(())
413    }
414
415    /// Sets a node to `value`.
416    ///
417    /// # Errors
418    /// This method can return the following errors:
419    /// - `RootNotInStore` if the `root` is not present in the store.
420    /// - `NodeNotInStore` if a node needed to traverse from `root` to `index` is not present in
421    ///   the store.
422    pub fn set_node(
423        &mut self,
424        mut root: RpoDigest,
425        index: NodeIndex,
426        value: RpoDigest,
427    ) -> Result<RootPath, MerkleError> {
428        let node = value;
429        let ValuePath { value, path } = self.get_path(root, index)?;
430
431        // performs the update only if the node value differs from the opening
432        if node != value {
433            root = self.add_merkle_path(index.value(), node, path.clone())?;
434        }
435
436        Ok(RootPath { root, path })
437    }
438
439    /// Merges two elements and adds the resulting node into the store.
440    ///
441    /// Merges arbitrary values. They may be leafs, nodes, or a mixture of both.
442    pub fn merge_roots(
443        &mut self,
444        left_root: RpoDigest,
445        right_root: RpoDigest,
446    ) -> Result<RpoDigest, MerkleError> {
447        let parent = Rpo256::merge(&[left_root, right_root]);
448        self.nodes.insert(parent, StoreNode { left: left_root, right: right_root });
449
450        Ok(parent)
451    }
452
453    // DESTRUCTURING
454    // --------------------------------------------------------------------------------------------
455
456    /// Returns the inner storage of this MerkleStore while consuming `self`.
457    pub fn into_inner(self) -> T {
458        self.nodes
459    }
460
461    // HELPER METHODS
462    // --------------------------------------------------------------------------------------------
463
464    /// Recursively clones a tree with the specified root from the specified source into self.
465    ///
466    /// If the source store does not contain a tree with the specified root, this is a noop.
467    fn clone_tree_from(&mut self, root: RpoDigest, source: &Self) {
468        // process the node only if it is in the source
469        if let Some(node) = source.nodes.get(&root) {
470            // if the node has already been inserted, no need to process it further as all of its
471            // descendants should be already cloned from the source store
472            if self.nodes.insert(root, *node).is_none() {
473                self.clone_tree_from(node.left, source);
474                self.clone_tree_from(node.right, source);
475            }
476        }
477    }
478}
479
480// CONVERSIONS
481// ================================================================================================
482
483impl<T: KvMap<RpoDigest, StoreNode>> From<&MerkleTree> for MerkleStore<T> {
484    fn from(value: &MerkleTree) -> Self {
485        let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
486        Self { nodes }
487    }
488}
489
490impl<T: KvMap<RpoDigest, StoreNode>, const DEPTH: u8> From<&SimpleSmt<DEPTH>> for MerkleStore<T> {
491    fn from(value: &SimpleSmt<DEPTH>) -> Self {
492        let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
493        Self { nodes }
494    }
495}
496
497impl<T: KvMap<RpoDigest, StoreNode>> From<&Smt> for MerkleStore<T> {
498    fn from(value: &Smt) -> Self {
499        let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
500        Self { nodes }
501    }
502}
503
504impl<T: KvMap<RpoDigest, StoreNode>> From<&Mmr> for MerkleStore<T> {
505    fn from(value: &Mmr) -> Self {
506        let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
507        Self { nodes }
508    }
509}
510
511impl<T: KvMap<RpoDigest, StoreNode>> From<&PartialMerkleTree> for MerkleStore<T> {
512    fn from(value: &PartialMerkleTree) -> Self {
513        let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
514        Self { nodes }
515    }
516}
517
518impl<T: KvMap<RpoDigest, StoreNode>> From<T> for MerkleStore<T> {
519    fn from(values: T) -> Self {
520        let nodes = values.into_iter().chain(empty_hashes()).collect();
521        Self { nodes }
522    }
523}
524
525impl<T: KvMap<RpoDigest, StoreNode>> FromIterator<InnerNodeInfo> for MerkleStore<T> {
526    fn from_iter<I: IntoIterator<Item = InnerNodeInfo>>(iter: I) -> Self {
527        let nodes = combine_nodes_with_empty_hashes(iter).collect();
528        Self { nodes }
529    }
530}
531
532impl<T: KvMap<RpoDigest, StoreNode>> FromIterator<(RpoDigest, StoreNode)> for MerkleStore<T> {
533    fn from_iter<I: IntoIterator<Item = (RpoDigest, StoreNode)>>(iter: I) -> Self {
534        let nodes = iter.into_iter().chain(empty_hashes()).collect();
535        Self { nodes }
536    }
537}
538
539// ITERATORS
540// ================================================================================================
541impl<T: KvMap<RpoDigest, StoreNode>> Extend<InnerNodeInfo> for MerkleStore<T> {
542    fn extend<I: IntoIterator<Item = InnerNodeInfo>>(&mut self, iter: I) {
543        self.nodes.extend(
544            iter.into_iter()
545                .map(|info| (info.value, StoreNode { left: info.left, right: info.right })),
546        );
547    }
548}
549
550// SERIALIZATION
551// ================================================================================================
552
553impl Serializable for StoreNode {
554    fn write_into<W: ByteWriter>(&self, target: &mut W) {
555        self.left.write_into(target);
556        self.right.write_into(target);
557    }
558}
559
560impl Deserializable for StoreNode {
561    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
562        let left = RpoDigest::read_from(source)?;
563        let right = RpoDigest::read_from(source)?;
564        Ok(StoreNode { left, right })
565    }
566}
567
568impl<T: KvMap<RpoDigest, StoreNode>> Serializable for MerkleStore<T> {
569    fn write_into<W: ByteWriter>(&self, target: &mut W) {
570        target.write_u64(self.nodes.len() as u64);
571
572        for (k, v) in self.nodes.iter() {
573            k.write_into(target);
574            v.write_into(target);
575        }
576    }
577}
578
579impl<T: KvMap<RpoDigest, StoreNode>> Deserializable for MerkleStore<T> {
580    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
581        let len = source.read_u64()?;
582        let mut nodes: Vec<(RpoDigest, StoreNode)> = Vec::with_capacity(len as usize);
583
584        for _ in 0..len {
585            let key = RpoDigest::read_from(source)?;
586            let value = StoreNode::read_from(source)?;
587            nodes.push((key, value));
588        }
589
590        Ok(nodes.into_iter().collect())
591    }
592}
593
594// HELPER FUNCTIONS
595// ================================================================================================
596
597/// Creates empty hashes for all the subtrees of a tree with a max depth of 255.
598fn empty_hashes() -> impl IntoIterator<Item = (RpoDigest, StoreNode)> {
599    let subtrees = EmptySubtreeRoots::empty_hashes(255);
600    subtrees
601        .iter()
602        .rev()
603        .copied()
604        .zip(subtrees.iter().rev().skip(1).copied())
605        .map(|(child, parent)| (parent, StoreNode { left: child, right: child }))
606}
607
608/// Consumes an iterator of [InnerNodeInfo] and returns an iterator of `(value, node)` tuples
609/// which includes the nodes associate with roots of empty subtrees up to a depth of 255.
610fn combine_nodes_with_empty_hashes(
611    nodes: impl IntoIterator<Item = InnerNodeInfo>,
612) -> impl Iterator<Item = (RpoDigest, StoreNode)> {
613    nodes
614        .into_iter()
615        .map(|info| (info.value, StoreNode { left: info.left, right: info.right }))
616        .chain(empty_hashes())
617}