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