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