miden-crypto/src/merkle/store/mod.rs
src/merkle/store/mod.rsRaw
1use super::{
2 empty_roots::EMPTY_WORD, mmr::Mmr, BTreeMap, EmptySubtreeRoots, InnerNodeInfo, KvMap,
3 MerkleError, MerklePath, MerkleStoreDelta, MerkleTree, NodeIndex, PartialMerkleTree,
4 RecordingMap, RootPath, Rpo256, RpoDigest, SimpleSmt, TieredSmt, TryApplyDiff, 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 {
177 value: hash,
178 path: MerklePath::new(path),
179 })
180 }
181
182 /// Reconstructs a path from the root until a leaf or empty node and returns its depth.
183 ///
184 /// The `tree_depth` parameter defines up to which depth the tree will be traversed, starting
185 /// from `root`. The maximum value the argument accepts is [u64::BITS].
186 ///
187 /// The traversed path from leaf to root will start at the least significant bit of `index`,
188 /// and will be executed for `tree_depth` bits.
189 ///
190 /// # Errors
191 /// Will return an error if:
192 /// - The provided root is not found.
193 /// - The path from the root continues to a depth greater than `tree_depth`.
194 /// - The provided `tree_depth` is greater than `64.
195 /// - The provided `index` is not valid for a depth equivalent to `tree_depth`. For more
196 /// information, check [NodeIndex::new].
197 pub fn get_leaf_depth(
198 &self,
199 root: RpoDigest,
200 tree_depth: u8,
201 index: u64,
202 ) -> Result<u8, MerkleError> {
203 // validate depth and index
204 if tree_depth > 64 {
205 return Err(MerkleError::DepthTooBig(tree_depth as u64));
206 }
207 NodeIndex::new(tree_depth, index)?;
208
209 // it's not illegal to have a maximum depth of `0`; we should just return the root in that
210 // case. this check will simplify the implementation as we could overflow bits for depth
211 // `0`.
212 if tree_depth == 0 {
213 return Ok(0);
214 }
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 // at max depth assert it doesn't have sub-trees
245 if self.nodes.contains_key(&hash) {
246 return Err(MerkleError::DepthTooBig(tree_depth as u64 + 1));
247 }
248
249 // depleted bits; return max depth
250 Ok(tree_depth)
251 }
252
253 // DATA EXTRACTORS
254 // --------------------------------------------------------------------------------------------
255
256 /// Returns a subset of this Merkle store such that the returned Merkle store contains all
257 /// nodes which are descendants of the specified roots.
258 ///
259 /// The roots for which no descendants exist in this Merkle store are ignored.
260 pub fn subset<I, R>(&self, roots: I) -> MerkleStore<T>
261 where
262 I: Iterator<Item = R>,
263 R: Borrow<RpoDigest>,
264 {
265 let mut store = MerkleStore::new();
266 for root in roots {
267 let root = *root.borrow();
268 store.clone_tree_from(root, self);
269 }
270 store
271 }
272
273 /// Iterator over the inner nodes of the [MerkleStore].
274 pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
275 self.nodes.iter().map(|(r, n)| InnerNodeInfo {
276 value: *r,
277 left: n.left,
278 right: n.right,
279 })
280 }
281
282 /// Iterator over the non-empty leaves of the Merkle tree associated with the specified `root`
283 /// and `max_depth`.
284 pub fn non_empty_leaves(
285 &self,
286 root: RpoDigest,
287 max_depth: u8,
288 ) -> impl Iterator<Item = (NodeIndex, RpoDigest)> + '_ {
289 let empty_roots = EmptySubtreeRoots::empty_hashes(max_depth);
290 let mut stack = Vec::new();
291 stack.push((NodeIndex::new_unchecked(0, 0), root));
292
293 core::iter::from_fn(move || {
294 while let Some((index, node_hash)) = stack.pop() {
295 if index.depth() == max_depth {
296 return Some((index, node_hash));
297 }
298
299 if let Some(node) = self.nodes.get(&node_hash) {
300 if !empty_roots.contains(&node.left) {
301 stack.push((index.left_child(), node.left));
302 }
303 if !empty_roots.contains(&node.right) {
304 stack.push((index.right_child(), node.right));
305 }
306 }
307 }
308
309 None
310 })
311 }
312
313 // STATE MUTATORS
314 // --------------------------------------------------------------------------------------------
315
316 /// Adds all the nodes of a Merkle path represented by `path`, opening to `node`. Returns the
317 /// new root.
318 ///
319 /// This will compute the sibling elements determined by the Merkle `path` and `node`, and
320 /// include all the nodes into the store.
321 pub fn add_merkle_path(
322 &mut self,
323 index: u64,
324 node: RpoDigest,
325 path: MerklePath,
326 ) -> Result<RpoDigest, MerkleError> {
327 let root = path.inner_nodes(index, node)?.fold(RpoDigest::default(), |_, node| {
328 let value: RpoDigest = node.value;
329 let left: RpoDigest = node.left;
330 let right: RpoDigest = node.right;
331
332 debug_assert_eq!(Rpo256::merge(&[left, right]), value);
333 self.nodes.insert(value, StoreNode { left, right });
334
335 node.value
336 });
337 Ok(root)
338 }
339
340 /// Adds all the nodes of multiple Merkle paths into the store.
341 ///
342 /// This will compute the sibling elements for each Merkle `path` and include all the nodes
343 /// into the store.
344 ///
345 /// For further reference, check [MerkleStore::add_merkle_path].
346 pub fn add_merkle_paths<I>(&mut self, paths: I) -> Result<(), MerkleError>
347 where
348 I: IntoIterator<Item = (u64, RpoDigest, MerklePath)>,
349 {
350 for (index_value, node, path) in paths.into_iter() {
351 self.add_merkle_path(index_value, node, path)?;
352 }
353 Ok(())
354 }
355
356 /// Sets a node to `value`.
357 ///
358 /// # Errors
359 /// This method can return the following errors:
360 /// - `RootNotInStore` if the `root` is not present in the store.
361 /// - `NodeNotInStore` if a node needed to traverse from `root` to `index` is not present in
362 /// the store.
363 pub fn set_node(
364 &mut self,
365 mut root: RpoDigest,
366 index: NodeIndex,
367 value: RpoDigest,
368 ) -> Result<RootPath, MerkleError> {
369 let node = value;
370 let ValuePath { value, path } = self.get_path(root, index)?;
371
372 // performs the update only if the node value differs from the opening
373 if node != value {
374 root = self.add_merkle_path(index.value(), node, path.clone())?;
375 }
376
377 Ok(RootPath { root, path })
378 }
379
380 /// Merges two elements and adds the resulting node into the store.
381 ///
382 /// Merges arbitrary values. They may be leafs, nodes, or a mixture of both.
383 pub fn merge_roots(
384 &mut self,
385 left_root: RpoDigest,
386 right_root: RpoDigest,
387 ) -> Result<RpoDigest, MerkleError> {
388 let parent = Rpo256::merge(&[left_root, right_root]);
389 self.nodes.insert(
390 parent,
391 StoreNode {
392 left: left_root,
393 right: right_root,
394 },
395 );
396
397 Ok(parent)
398 }
399
400 // DESTRUCTURING
401 // --------------------------------------------------------------------------------------------
402
403 /// Returns the inner storage of this MerkleStore while consuming `self`.
404 pub fn into_inner(self) -> T {
405 self.nodes
406 }
407
408 // HELPER METHODS
409 // --------------------------------------------------------------------------------------------
410
411 /// Recursively clones a tree with the specified root from the specified source into self.
412 ///
413 /// If the source store does not contain a tree with the specified root, this is a noop.
414 fn clone_tree_from(&mut self, root: RpoDigest, source: &Self) {
415 // process the node only if it is in the source
416 if let Some(node) = source.nodes.get(&root) {
417 // if the node has already been inserted, no need to process it further as all of its
418 // descendants should be already cloned from the source store
419 if self.nodes.insert(root, *node).is_none() {
420 self.clone_tree_from(node.left, source);
421 self.clone_tree_from(node.right, source);
422 }
423 }
424 }
425}
426
427// CONVERSIONS
428// ================================================================================================
429
430impl<T: KvMap<RpoDigest, StoreNode>> From<&MerkleTree> for MerkleStore<T> {
431 fn from(value: &MerkleTree) -> Self {
432 let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
433 Self { nodes }
434 }
435}
436
437impl<T: KvMap<RpoDigest, StoreNode>> From<&SimpleSmt> for MerkleStore<T> {
438 fn from(value: &SimpleSmt) -> Self {
439 let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
440 Self { nodes }
441 }
442}
443
444impl<T: KvMap<RpoDigest, StoreNode>> From<&Mmr> for MerkleStore<T> {
445 fn from(value: &Mmr) -> Self {
446 let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
447 Self { nodes }
448 }
449}
450
451impl<T: KvMap<RpoDigest, StoreNode>> From<&TieredSmt> for MerkleStore<T> {
452 fn from(value: &TieredSmt) -> Self {
453 let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
454 Self { nodes }
455 }
456}
457
458impl<T: KvMap<RpoDigest, StoreNode>> From<&PartialMerkleTree> for MerkleStore<T> {
459 fn from(value: &PartialMerkleTree) -> Self {
460 let nodes = combine_nodes_with_empty_hashes(value.inner_nodes()).collect();
461 Self { nodes }
462 }
463}
464
465impl<T: KvMap<RpoDigest, StoreNode>> From<T> for MerkleStore<T> {
466 fn from(values: T) -> Self {
467 let nodes = values.into_iter().chain(empty_hashes()).collect();
468 Self { nodes }
469 }
470}
471
472impl<T: KvMap<RpoDigest, StoreNode>> FromIterator<InnerNodeInfo> for MerkleStore<T> {
473 fn from_iter<I: IntoIterator<Item = InnerNodeInfo>>(iter: I) -> Self {
474 let nodes = combine_nodes_with_empty_hashes(iter).collect();
475 Self { nodes }
476 }
477}
478
479impl<T: KvMap<RpoDigest, StoreNode>> FromIterator<(RpoDigest, StoreNode)> for MerkleStore<T> {
480 fn from_iter<I: IntoIterator<Item = (RpoDigest, StoreNode)>>(iter: I) -> Self {
481 let nodes = iter.into_iter().chain(empty_hashes()).collect();
482 Self { nodes }
483 }
484}
485
486// ITERATORS
487// ================================================================================================
488impl<T: KvMap<RpoDigest, StoreNode>> Extend<InnerNodeInfo> for MerkleStore<T> {
489 fn extend<I: IntoIterator<Item = InnerNodeInfo>>(&mut self, iter: I) {
490 self.nodes.extend(iter.into_iter().map(|info| {
491 (
492 info.value,
493 StoreNode {
494 left: info.left,
495 right: info.right,
496 },
497 )
498 }));
499 }
500}
501
502// DiffT & ApplyDiffT TRAIT IMPLEMENTATION
503// ================================================================================================
504impl<T: KvMap<RpoDigest, StoreNode>> TryApplyDiff<RpoDigest, StoreNode> for MerkleStore<T> {
505 type Error = MerkleError;
506 type DiffType = MerkleStoreDelta;
507
508 fn try_apply(&mut self, diff: Self::DiffType) -> Result<(), MerkleError> {
509 for (root, delta) in diff.0 {
510 let mut root = root;
511 for cleared_slot in delta.cleared_slots() {
512 root = self
513 .set_node(
514 root,
515 NodeIndex::new(delta.depth(), *cleared_slot)?,
516 EMPTY_WORD.into(),
517 )?
518 .root;
519 }
520 for (updated_slot, updated_value) in delta.updated_slots() {
521 root = self
522 .set_node(
523 root,
524 NodeIndex::new(delta.depth(), *updated_slot)?,
525 (*updated_value).into(),
526 )?
527 .root;
528 }
529 }
530
531 Ok(())
532 }
533}
534
535// SERIALIZATION
536// ================================================================================================
537
538impl Serializable for StoreNode {
539 fn write_into<W: ByteWriter>(&self, target: &mut W) {
540 self.left.write_into(target);
541 self.right.write_into(target);
542 }
543}
544
545impl Deserializable for StoreNode {
546 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
547 let left = RpoDigest::read_from(source)?;
548 let right = RpoDigest::read_from(source)?;
549 Ok(StoreNode { left, right })
550 }
551}
552
553impl<T: KvMap<RpoDigest, StoreNode>> Serializable for MerkleStore<T> {
554 fn write_into<W: ByteWriter>(&self, target: &mut W) {
555 target.write_u64(self.nodes.len() as u64);
556
557 for (k, v) in self.nodes.iter() {
558 k.write_into(target);
559 v.write_into(target);
560 }
561 }
562}
563
564impl<T: KvMap<RpoDigest, StoreNode>> Deserializable for MerkleStore<T> {
565 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
566 let len = source.read_u64()?;
567 let mut nodes: Vec<(RpoDigest, StoreNode)> = Vec::with_capacity(len as usize);
568
569 for _ in 0..len {
570 let key = RpoDigest::read_from(source)?;
571 let value = StoreNode::read_from(source)?;
572 nodes.push((key, value));
573 }
574
575 Ok(nodes.into_iter().collect())
576 }
577}
578
579// HELPER FUNCTIONS
580// ================================================================================================
581
582/// Creates empty hashes for all the subtrees of a tree with a max depth of 255.
583fn empty_hashes() -> impl IntoIterator<Item = (RpoDigest, StoreNode)> {
584 let subtrees = EmptySubtreeRoots::empty_hashes(255);
585 subtrees.iter().rev().copied().zip(subtrees.iter().rev().skip(1).copied()).map(
586 |(child, parent)| {
587 (
588 parent,
589 StoreNode {
590 left: child,
591 right: child,
592 },
593 )
594 },
595 )
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 = (RpoDigest, StoreNode)> {
603 nodes
604 .into_iter()
605 .map(|info| {
606 (
607 info.value,
608 StoreNode {
609 left: info.left,
610 right: info.right,
611 },
612 )
613 })
614 .chain(empty_hashes())
615}