Skip to main content

miden_crypto/merkle/smt/
mod.rs

1//! Sparse Merkle Tree (SMT) data structures.
2
3use alloc::vec::Vec;
4use core::{
5    fmt::{self, Display},
6    hash::Hash,
7};
8
9use super::{EmptySubtreeRoots, InnerNodeInfo, MerkleError, NodeIndex, SparseMerklePath};
10use crate::{
11    EMPTY_WORD, Map, Word,
12    hash::poseidon2::Poseidon2,
13    utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
14};
15
16mod full;
17pub use full::{MAX_LEAF_ENTRIES, SMT_DEPTH, Smt, SmtLeaf, SmtLeafError, SmtProof, SmtProofError};
18
19#[cfg(feature = "concurrent")]
20mod large;
21#[cfg(feature = "internal")]
22pub use full::concurrent::{SubtreeLeaf, build_subtree_for_bench};
23#[cfg(feature = "concurrent")]
24pub use large::{
25    LargeSmt, LargeSmtError, MemoryStorage, SmtStorage, StorageError, StorageUpdateParts,
26    StorageUpdates, Subtree, SubtreeError, SubtreeUpdate,
27};
28#[cfg(feature = "rocksdb")]
29pub use large::{RocksDbConfig, RocksDbStorage};
30
31mod large_forest;
32pub use large_forest::{
33    Backend, BackendError, Config as ForestConfig,
34    DEFAULT_MAX_HISTORY_VERSIONS as FOREST_DEFAULT_MAX_HISTORY_VERSIONS, ForestOperation,
35    InMemoryBackend as ForestInMemoryBackend, LargeSmtForest, LargeSmtForestError, LineageId,
36    MIN_HISTORY_VERSIONS as FOREST_MIN_HISTORY_VERSIONS, RootInfo, SmtForestUpdateBatch,
37    SmtUpdateBatch, TreeEntry, TreeId, TreeWithRoot, VersionId,
38};
39#[cfg(feature = "persistent-forest")]
40pub use large_forest::{PersistentBackend as ForestPersistentBackend, PersistentBackendConfig};
41
42mod simple;
43pub use simple::{SimpleSmt, SimpleSmtProof};
44
45mod partial;
46pub use partial::PartialSmt;
47
48mod forest;
49pub use forest::SmtForest;
50
51// CONSTANTS
52// ================================================================================================
53
54/// Minimum supported depth.
55pub const SMT_MIN_DEPTH: u8 = 1;
56
57/// Maximum supported depth.
58pub const SMT_MAX_DEPTH: u8 = 64;
59
60// SPARSE MERKLE TREE
61// ================================================================================================
62
63type InnerNodes = Map<NodeIndex, InnerNode>;
64type Leaves<T> = Map<u64, T>;
65type NodeMutations = Map<NodeIndex, NodeMutation>;
66
67/// An abstract description of a sparse Merkle tree.
68///
69/// A sparse Merkle tree is a key-value map which also supports proving that a given value is indeed
70/// stored at a given key in the tree. It is viewed as always being fully populated. If a leaf's
71/// value was not explicitly set, then its value is the default value. Typically, the vast majority
72/// of leaves will store the default value (hence it is "sparse"), and therefore the internal
73/// representation of the tree will only keep track of the leaves that have a different value from
74/// the default.
75///
76/// All leaves sit at the same depth. The deeper the tree, the more leaves it has; but also the
77/// longer its proofs are - of exactly `log(depth)` size. A tree cannot have depth 0, since such a
78/// tree is just a single value, and is probably a programming mistake.
79///
80/// Every key maps to one leaf. If there are as many keys as there are leaves, then
81/// [Self::Leaf] should be the same type as [Self::Value], as is the case with
82/// [`SimpleSmt`]. However, if there are more keys than leaves, then [`Self::Leaf`]
83/// must accommodate all keys that map to the same leaf.
84///
85/// [SparseMerkleTree] currently doesn't support optimizations that compress Merkle proofs.
86pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
87    /// The type for a key
88    type Key: Clone + Ord + Eq + Hash;
89    /// The type for a value
90    type Value: Clone + PartialEq;
91    /// The type for a leaf
92    type Leaf: Clone;
93    /// The type for an opening (i.e. a "proof") of a leaf
94    type Opening;
95
96    /// The default value used to compute the hash of empty leaves
97    const EMPTY_VALUE: Self::Value;
98
99    /// The root of the empty tree with provided DEPTH
100    const EMPTY_ROOT: Word;
101
102    // PROVIDED METHODS
103    // ---------------------------------------------------------------------------------------------
104
105    /// Returns a [SparseMerklePath] to the specified key.
106    ///
107    /// Mostly this is an implementation detail of [`Self::open()`].
108    fn get_path(&self, key: &Self::Key) -> SparseMerklePath {
109        let index = NodeIndex::from(Self::key_to_leaf_index(key));
110
111        // SAFETY: this is guaranteed to have depth <= SMT_MAX_DEPTH
112        SparseMerklePath::from_sized_iter(
113            index.proof_indices().map(|index| self.get_node_hash(index)),
114        )
115        .expect("failed to convert to SparseMerklePath")
116    }
117
118    /// Get the hash of a node at an arbitrary index, including the root or leaf hashes.
119    ///
120    /// The root index simply returns [`Self::root()`]. Other hashes are retrieved by calling
121    /// [`Self::get_inner_node()`] on the parent, and returning the respective child hash.
122    fn get_node_hash(&self, index: NodeIndex) -> Word {
123        if index.is_root() {
124            return self.root();
125        }
126
127        let InnerNode { left, right } = self.get_inner_node(index.parent());
128
129        let index_is_right = index.is_position_odd();
130        if index_is_right { right } else { left }
131    }
132
133    /// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a sparse
134    /// Merkle path to the leaf, as well as the leaf itself.
135    fn open(&self, key: &Self::Key) -> Self::Opening {
136        let leaf = self.get_leaf(key);
137        let merkle_path = self.get_path(key);
138
139        Self::path_and_leaf_to_opening(merkle_path, leaf)
140    }
141
142    /// Inserts a value at the specified key, returning the previous value associated with that key.
143    /// Recall that by definition, any key that hasn't been updated is associated with
144    /// [`Self::EMPTY_VALUE`].
145    ///
146    /// This also recomputes all hashes between the leaf (associated with the key) and the root,
147    /// updating the root itself.
148    fn insert(&mut self, key: Self::Key, value: Self::Value) -> Result<Self::Value, MerkleError> {
149        let old_value = self.insert_value(key.clone(), value.clone())?.unwrap_or(Self::EMPTY_VALUE);
150
151        // if the old value and new value are the same, there is nothing to update
152        if value == old_value {
153            return Ok(value);
154        }
155
156        let leaf = self.get_leaf(&key);
157        let node_index = {
158            let leaf_index: LeafIndex<DEPTH> = Self::key_to_leaf_index(&key);
159            leaf_index.into()
160        };
161
162        self.recompute_nodes_from_index_to_root(node_index, Self::hash_leaf(&leaf));
163
164        Ok(old_value)
165    }
166
167    /// Recomputes the branch nodes (including the root) from `index` all the way to the root.
168    /// `node_hash_at_index` is the hash of the node stored at index.
169    fn recompute_nodes_from_index_to_root(
170        &mut self,
171        mut index: NodeIndex,
172        node_hash_at_index: Word,
173    ) {
174        let mut node_hash = node_hash_at_index;
175        for node_depth in (0..index.depth()).rev() {
176            let is_right = index.is_position_odd();
177            index.move_up();
178            let InnerNode { left, right } = self.get_inner_node(index);
179            let (left, right) = if is_right {
180                (left, node_hash)
181            } else {
182                (node_hash, right)
183            };
184            node_hash = Poseidon2::merge(&[left, right]);
185
186            if node_hash == *EmptySubtreeRoots::entry(DEPTH, node_depth) {
187                // If a subtree is empty, then can remove the inner node, since it's equal to the
188                // default value
189                self.remove_inner_node(index);
190            } else {
191                self.insert_inner_node(index, InnerNode { left, right });
192            }
193        }
194        self.set_root(node_hash);
195    }
196
197    /// Computes what changes are necessary to insert the specified key-value pairs into this Merkle
198    /// tree, allowing for validation before applying those changes.
199    ///
200    /// This method returns a [`MutationSet`], which contains all the information for inserting
201    /// `kv_pairs` into this Merkle tree already calculated, including the new root hash, which can
202    /// be queried with [`MutationSet::root()`]. Once a mutation set is returned,
203    /// [`SparseMerkleTree::apply_mutations()`] can be called in order to commit these changes to
204    /// the Merkle tree, or [`drop()`] to discard them.
205    ///
206    /// # Errors
207    /// If mutations would exceed [`crate::merkle::smt::MAX_LEAF_ENTRIES`] (1024 entries) in a leaf,
208    /// returns
209    /// [`MerkleError::TooManyLeafEntries`].
210    fn compute_mutations(
211        &self,
212        kv_pairs: impl IntoIterator<Item = (Self::Key, Self::Value)>,
213    ) -> Result<MutationSet<DEPTH, Self::Key, Self::Value>, MerkleError> {
214        self.compute_mutations_sequential(kv_pairs)
215    }
216
217    /// Sequential version of [`SparseMerkleTree::compute_mutations()`].
218    /// This is the default implementation.
219    fn compute_mutations_sequential(
220        &self,
221        kv_pairs: impl IntoIterator<Item = (Self::Key, Self::Value)>,
222    ) -> Result<MutationSet<DEPTH, Self::Key, Self::Value>, MerkleError> {
223        use NodeMutation::*;
224
225        let mut new_root = self.root();
226        let mut new_pairs: Map<Self::Key, Self::Value> = Default::default();
227        let mut node_mutations: NodeMutations = Default::default();
228
229        for (key, value) in kv_pairs {
230            // If the old value and the new value are the same, there is nothing to update.
231            // For the unusual case that kv_pairs has multiple values at the same key, we'll have
232            // to check the key-value pairs we've already seen to get the "effective" old value.
233            let old_value = new_pairs.get(&key).cloned().unwrap_or_else(|| self.get_value(&key));
234            if value == old_value {
235                continue;
236            }
237
238            let leaf_index = Self::key_to_leaf_index(&key);
239            let mut node_index = NodeIndex::from(leaf_index);
240
241            // We need the current leaf's hash to calculate the new leaf, but in the rare case that
242            // `kv_pairs` has multiple pairs that go into the same leaf, then those pairs are also
243            // part of the "current leaf".
244            let old_leaf = {
245                let pairs_at_index = new_pairs
246                    .iter()
247                    .filter(|&(new_key, _)| Self::key_to_leaf_index(new_key) == leaf_index);
248
249                pairs_at_index.fold(self.get_leaf(&key), |acc, (k, v)| {
250                    // Most of the time `pairs_at_index` should only contain a single entry (or
251                    // none at all), as multi-leaves should be really rare.
252                    let existing_leaf = acc.clone();
253                    self.construct_prospective_leaf(existing_leaf, k, v)
254                        .expect("current leaf should be valid")
255                })
256            };
257
258            let new_leaf =
259                self.construct_prospective_leaf(old_leaf, &key, &value).map_err(|e| match e {
260                    SmtLeafError::TooManyLeafEntries { actual } => {
261                        MerkleError::TooManyLeafEntries { actual }
262                    },
263                    other => panic!("unexpected SmtLeaf::insert error: {:?}", other),
264                })?;
265
266            let mut new_child_hash = Self::hash_leaf(&new_leaf);
267
268            for node_depth in (0..node_index.depth()).rev() {
269                // Whether the node we're replacing is the right child or the left child.
270                let is_right = node_index.is_position_odd();
271                node_index.move_up();
272
273                let old_node = node_mutations
274                    .get(&node_index)
275                    .map(|mutation| match mutation {
276                        Addition(node) => node.clone(),
277                        Removal => EmptySubtreeRoots::get_inner_node(DEPTH, node_depth),
278                    })
279                    .unwrap_or_else(|| self.get_inner_node(node_index));
280
281                let new_node = if is_right {
282                    InnerNode {
283                        left: old_node.left,
284                        right: new_child_hash,
285                    }
286                } else {
287                    InnerNode {
288                        left: new_child_hash,
289                        right: old_node.right,
290                    }
291                };
292
293                // The next iteration will operate on this new node's hash.
294                new_child_hash = new_node.hash();
295
296                let &equivalent_empty_hash = EmptySubtreeRoots::entry(DEPTH, node_depth);
297                let is_removal = new_child_hash == equivalent_empty_hash;
298                let new_entry = if is_removal { Removal } else { Addition(new_node) };
299                node_mutations.insert(node_index, new_entry);
300            }
301
302            // Once we're at depth 0, the last node we made is the new root.
303            new_root = new_child_hash;
304            // And then we're done with this pair; on to the next one.
305            new_pairs.insert(key, value);
306        }
307
308        Ok(MutationSet {
309            old_root: self.root(),
310            new_root,
311            node_mutations,
312            new_pairs,
313        })
314    }
315
316    /// Applies the prospective mutations computed with [`SparseMerkleTree::compute_mutations()`] to
317    /// this tree.
318    ///
319    /// # Errors
320    /// If `mutations` was computed on a tree with a different root than this one, returns
321    /// [`MerkleError::ConflictingRoots`] with a two-item [`Vec`]. The first item is the root hash
322    /// the `mutations` were computed against, and the second item is the actual current root of
323    /// this tree.
324    /// If mutations would exceed [`crate::merkle::smt::MAX_LEAF_ENTRIES`] (1024 entries) in a leaf,
325    /// returns
326    /// [`MerkleError::TooManyLeafEntries`].
327    fn apply_mutations(
328        &mut self,
329        mutations: MutationSet<DEPTH, Self::Key, Self::Value>,
330    ) -> Result<(), MerkleError>
331    where
332        Self: Sized,
333    {
334        use NodeMutation::*;
335        let MutationSet {
336            old_root,
337            node_mutations,
338            new_pairs,
339            new_root,
340        } = mutations;
341
342        // Guard against accidentally trying to apply mutations that were computed against a
343        // different tree, including a stale version of this tree.
344        if old_root != self.root() {
345            return Err(MerkleError::ConflictingRoots {
346                expected_root: self.root(),
347                actual_root: old_root,
348            });
349        }
350
351        for (index, mutation) in node_mutations {
352            match mutation {
353                Removal => {
354                    self.remove_inner_node(index);
355                },
356                Addition(node) => {
357                    self.insert_inner_node(index, node);
358                },
359            }
360        }
361
362        for (key, value) in new_pairs {
363            self.insert_value(key, value)?;
364        }
365
366        self.set_root(new_root);
367
368        Ok(())
369    }
370
371    /// Applies the prospective mutations computed with [`SparseMerkleTree::compute_mutations()`] to
372    /// this tree and returns the reverse mutation set. Applying the reverse mutation sets to the
373    /// updated tree will revert the changes.
374    ///
375    /// # Errors
376    /// If `mutations` was computed on a tree with a different root than this one, returns
377    /// [`MerkleError::ConflictingRoots`] with a two-item [`Vec`]. The first item is the root hash
378    /// the `mutations` were computed against, and the second item is the actual current root of
379    /// this tree.
380    fn apply_mutations_with_reversion(
381        &mut self,
382        mutations: MutationSet<DEPTH, Self::Key, Self::Value>,
383    ) -> Result<MutationSet<DEPTH, Self::Key, Self::Value>, MerkleError>
384    where
385        Self: Sized,
386    {
387        use NodeMutation::*;
388        let MutationSet {
389            old_root,
390            node_mutations,
391            new_pairs,
392            new_root,
393        } = mutations;
394
395        // Guard against accidentally trying to apply mutations that were computed against a
396        // different tree, including a stale version of this tree.
397        if old_root != self.root() {
398            return Err(MerkleError::ConflictingRoots {
399                expected_root: self.root(),
400                actual_root: old_root,
401            });
402        }
403
404        let mut reverse_mutations = NodeMutations::new();
405        for (index, mutation) in node_mutations {
406            match mutation {
407                Removal => {
408                    if let Some(node) = self.remove_inner_node(index) {
409                        reverse_mutations.insert(index, Addition(node));
410                    }
411                },
412                Addition(node) => {
413                    if let Some(old_node) = self.insert_inner_node(index, node) {
414                        reverse_mutations.insert(index, Addition(old_node));
415                    } else {
416                        reverse_mutations.insert(index, Removal);
417                    }
418                },
419            }
420        }
421
422        let mut reverse_pairs = Map::new();
423        for (key, value) in new_pairs {
424            match self.insert_value(key.clone(), value)? {
425                Some(old_value) => {
426                    reverse_pairs.insert(key, old_value);
427                },
428                None => {
429                    reverse_pairs.insert(key, Self::EMPTY_VALUE);
430                },
431            }
432        }
433
434        self.set_root(new_root);
435
436        Ok(MutationSet {
437            old_root: new_root,
438            node_mutations: reverse_mutations,
439            new_pairs: reverse_pairs,
440            new_root: old_root,
441        })
442    }
443
444    // REQUIRED METHODS
445    // ---------------------------------------------------------------------------------------------
446
447    /// Construct this type from already computed leaves and nodes. The caller ensures passed
448    /// arguments are correct and consistent with each other.
449    fn from_raw_parts(
450        inner_nodes: InnerNodes,
451        leaves: Leaves<Self::Leaf>,
452        root: Word,
453    ) -> Result<Self, MerkleError>
454    where
455        Self: Sized;
456
457    /// The root of the tree
458    fn root(&self) -> Word;
459
460    /// Sets the root of the tree
461    fn set_root(&mut self, root: Word);
462
463    /// Retrieves an inner node at the given index
464    fn get_inner_node(&self, index: NodeIndex) -> InnerNode;
465
466    /// Inserts an inner node at the given index
467    fn insert_inner_node(&mut self, index: NodeIndex, inner_node: InnerNode) -> Option<InnerNode>;
468
469    /// Removes an inner node at the given index
470    fn remove_inner_node(&mut self, index: NodeIndex) -> Option<InnerNode>;
471
472    /// Inserts a leaf node, and returns the value at the key if already exists
473    fn insert_value(
474        &mut self,
475        key: Self::Key,
476        value: Self::Value,
477    ) -> Result<Option<Self::Value>, MerkleError>;
478
479    /// Returns the value at the specified key. Recall that by definition, any key that hasn't been
480    /// updated is associated with [`Self::EMPTY_VALUE`].
481    fn get_value(&self, key: &Self::Key) -> Self::Value;
482
483    /// Returns the leaf at the specified index.
484    fn get_leaf(&self, key: &Self::Key) -> Self::Leaf;
485
486    /// Returns the hash of a leaf
487    fn hash_leaf(leaf: &Self::Leaf) -> Word;
488
489    /// Returns what a leaf would look like if a key-value pair were inserted into the tree, without
490    /// mutating the tree itself. The existing leaf can be empty.
491    ///
492    /// To get a prospective leaf based on the current state of the tree, use `self.get_leaf(key)`
493    /// as the argument for `existing_leaf`. The return value from this function can be chained back
494    /// into this function as the first argument to continue making prospective changes.
495    ///
496    /// # Invariants
497    /// Because this method is for a prospective key-value insertion into a specific leaf,
498    /// `existing_leaf` must have the same leaf index as `key` (as determined by
499    /// [`SparseMerkleTree::key_to_leaf_index()`]), or the result will be meaningless.
500    ///
501    /// # Errors
502    /// If inserting the key-value pair would exceed
503    /// [`crate::merkle::smt::MAX_LEAF_ENTRIES`] (1024 entries) in a leaf,
504    /// returns [`SmtLeafError::TooManyLeafEntries`].
505    fn construct_prospective_leaf(
506        &self,
507        existing_leaf: Self::Leaf,
508        key: &Self::Key,
509        value: &Self::Value,
510    ) -> Result<Self::Leaf, SmtLeafError>;
511
512    /// Maps a key to a leaf index
513    fn key_to_leaf_index(key: &Self::Key) -> LeafIndex<DEPTH>;
514
515    /// Maps a (SparseMerklePath, Self::Leaf) to an opening.
516    ///
517    /// The length `path` is guaranteed to be equal to `DEPTH`
518    fn path_and_leaf_to_opening(path: SparseMerklePath, leaf: Self::Leaf) -> Self::Opening;
519}
520
521// INNER NODE
522// ================================================================================================
523
524/// This struct is public so functions returning it can be used in `benches/`, but is otherwise not
525/// part of the public API.
526#[doc(hidden)]
527#[derive(Debug, Default, Clone, PartialEq, Eq)]
528#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
529pub struct InnerNode {
530    pub left: Word,
531    pub right: Word,
532}
533
534impl InnerNode {
535    pub fn hash(&self) -> Word {
536        Poseidon2::merge(&[self.left, self.right])
537    }
538}
539
540// LEAF INDEX
541// ================================================================================================
542
543/// The index of a leaf, at a depth known at compile-time.
544#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
545#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
546pub struct LeafIndex<const DEPTH: u8> {
547    index: NodeIndex,
548}
549
550impl<const DEPTH: u8> LeafIndex<DEPTH> {
551    /// Creates a new `LeafIndex` with the specified value.
552    ///
553    /// # Errors
554    ///
555    /// Returns an error if the provided depth is less than the minimum supported depth.
556    pub fn new(value: u64) -> Result<Self, MerkleError> {
557        if DEPTH < SMT_MIN_DEPTH {
558            return Err(MerkleError::DepthTooSmall(DEPTH));
559        }
560
561        Ok(LeafIndex { index: NodeIndex::new(DEPTH, value)? })
562    }
563
564    /// Returns the position of this leaf index within its depth layer.
565    pub fn position(&self) -> u64 {
566        self.index.position()
567    }
568}
569
570impl LeafIndex<SMT_MAX_DEPTH> {
571    /// Creates a new `LeafIndex` at the maximum supported depth without validation.
572    pub const fn new_max_depth(value: u64) -> Self {
573        LeafIndex {
574            index: NodeIndex::new_unchecked(SMT_MAX_DEPTH, value),
575        }
576    }
577}
578
579impl<const DEPTH: u8> From<LeafIndex<DEPTH>> for NodeIndex {
580    fn from(value: LeafIndex<DEPTH>) -> Self {
581        value.index
582    }
583}
584
585impl<const DEPTH: u8> TryFrom<NodeIndex> for LeafIndex<DEPTH> {
586    type Error = MerkleError;
587
588    fn try_from(node_index: NodeIndex) -> Result<Self, Self::Error> {
589        if node_index.depth() != DEPTH {
590            return Err(MerkleError::InvalidNodeIndexDepth {
591                expected: DEPTH,
592                provided: node_index.depth(),
593            });
594        }
595
596        Self::new(node_index.position())
597    }
598}
599
600impl<const DEPTH: u8> Serializable for LeafIndex<DEPTH> {
601    fn write_into<W: ByteWriter>(&self, target: &mut W) {
602        self.index.write_into(target);
603    }
604}
605
606impl<const DEPTH: u8> Deserializable for LeafIndex<DEPTH> {
607    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
608        Ok(Self { index: source.read()? })
609    }
610}
611
612impl<const DEPTH: u8> Display for LeafIndex<DEPTH> {
613    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
614        write!(f, "DEPTH={}, position={}", DEPTH, self.position())
615    }
616}
617
618// MUTATIONS
619// ================================================================================================
620
621/// A change to an inner node of a sparse Merkle tree that hasn't yet been applied.
622/// [`MutationSet`] stores this type in relation to a [`NodeIndex`] to keep track of what changes
623/// need to occur at which node indices.
624#[derive(Debug, Clone, PartialEq, Eq)]
625pub enum NodeMutation {
626    /// Node needs to be removed.
627    Removal,
628    /// Node needs to be inserted.
629    Addition(InnerNode),
630}
631
632/// Represents a group of prospective mutations to a `SparseMerkleTree`, created by
633/// `SparseMerkleTree::compute_mutations()`, and that can be applied with
634/// `SparseMerkleTree::apply_mutations()`.
635#[derive(Debug, Clone, Default, PartialEq, Eq)]
636pub struct MutationSet<const DEPTH: u8, K: Eq + Hash, V> {
637    /// The root of the Merkle tree this MutationSet is for, recorded at the time
638    /// [`SparseMerkleTree::compute_mutations()`] was called. Exists to guard against applying
639    /// mutations to the wrong tree or applying stale mutations to a tree that has since changed.
640    old_root: Word,
641    /// The set of nodes that need to be removed or added. The "effective" node at an index is the
642    /// Merkle tree's existing node at that index, with the [`NodeMutation`] in this map at that
643    /// index overlaid, if any. Each [`NodeMutation::Addition`] corresponds to a
644    /// [`SparseMerkleTree::insert_inner_node()`] call, and each [`NodeMutation::Removal`]
645    /// corresponds to a [`SparseMerkleTree::remove_inner_node()`] call.
646    node_mutations: NodeMutations,
647    /// The set of top-level key-value pairs we're prospectively adding to the tree, including
648    /// adding empty values. The "effective" value for a key is the value in this Map, falling
649    /// back to the existing value in the Merkle tree. Each entry corresponds to a
650    /// [`SparseMerkleTree::insert_value()`] call.
651    new_pairs: Map<K, V>,
652    /// The calculated root for the Merkle tree, given these mutations. Publicly retrievable with
653    /// [`MutationSet::root()`]. Corresponds to a [`SparseMerkleTree::set_root()`]. call.
654    new_root: Word,
655}
656
657impl<const DEPTH: u8, K: Eq + Hash, V> MutationSet<DEPTH, K, V> {
658    /// Returns the SMT root that was calculated during `SparseMerkleTree::compute_mutations()`. See
659    /// that method for more information.
660    pub fn root(&self) -> Word {
661        self.new_root
662    }
663
664    /// Returns the SMT root before the mutations were applied.
665    pub fn old_root(&self) -> Word {
666        self.old_root
667    }
668
669    /// Returns the set of inner nodes that need to be removed or added.
670    pub fn node_mutations(&self) -> &NodeMutations {
671        &self.node_mutations
672    }
673
674    /// Returns the set of top-level key-value pairs that need to be added, updated or deleted
675    /// (i.e. set to `EMPTY_WORD`).
676    pub fn new_pairs(&self) -> &Map<K, V> {
677        &self.new_pairs
678    }
679
680    /// Returns `true` if the mutation set represents no changes to the tree, and `false` otherwise.
681    pub fn is_empty(&self) -> bool {
682        self.node_mutations.is_empty()
683            && self.new_pairs.is_empty()
684            && self.old_root == self.new_root
685    }
686}
687
688// SERIALIZATION
689// ================================================================================================
690
691impl Serializable for InnerNode {
692    fn write_into<W: ByteWriter>(&self, target: &mut W) {
693        target.write(self.left);
694        target.write(self.right);
695    }
696}
697
698impl Deserializable for InnerNode {
699    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
700        let left = source.read()?;
701        let right = source.read()?;
702
703        Ok(Self { left, right })
704    }
705}
706
707impl Serializable for NodeMutation {
708    fn write_into<W: ByteWriter>(&self, target: &mut W) {
709        match self {
710            NodeMutation::Removal => target.write_bool(false),
711            NodeMutation::Addition(inner_node) => {
712                target.write_bool(true);
713                inner_node.write_into(target);
714            },
715        }
716    }
717}
718
719impl Deserializable for NodeMutation {
720    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
721        if source.read_bool()? {
722            let inner_node = source.read()?;
723            return Ok(NodeMutation::Addition(inner_node));
724        }
725
726        Ok(NodeMutation::Removal)
727    }
728}
729
730impl<const DEPTH: u8, K: Serializable + Eq + Hash, V: Serializable> Serializable
731    for MutationSet<DEPTH, K, V>
732{
733    fn write_into<W: ByteWriter>(&self, target: &mut W) {
734        target.write(self.old_root);
735        target.write(self.new_root);
736
737        let inner_removals: Vec<_> = self
738            .node_mutations
739            .iter()
740            .filter(|(_, value)| matches!(value, NodeMutation::Removal))
741            .map(|(key, _)| key)
742            .collect();
743        let inner_additions: Vec<_> = self
744            .node_mutations
745            .iter()
746            .filter_map(|(key, value)| match value {
747                NodeMutation::Addition(node) => Some((key, node)),
748                _ => None,
749            })
750            .collect();
751
752        target.write(inner_removals);
753        target.write(inner_additions);
754
755        target.write_usize(self.new_pairs.len());
756        target.write_many(&self.new_pairs);
757    }
758}
759
760impl<const DEPTH: u8, K: Deserializable + Ord + Eq + Hash, V: Deserializable> Deserializable
761    for MutationSet<DEPTH, K, V>
762{
763    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
764        let old_root = source.read()?;
765        let new_root = source.read()?;
766
767        let inner_removals: Vec<NodeIndex> = source.read()?;
768        let inner_additions: Vec<(NodeIndex, InnerNode)> = source.read()?;
769
770        let node_mutations = NodeMutations::from_iter(
771            inner_removals.into_iter().map(|index| (index, NodeMutation::Removal)).chain(
772                inner_additions
773                    .into_iter()
774                    .map(|(index, node)| (index, NodeMutation::Addition(node))),
775            ),
776        );
777
778        let num_new_pairs = source.read_usize()?;
779        let new_pairs: Map<_, _> =
780            source.read_many_iter(num_new_pairs)?.collect::<Result<_, _>>()?;
781
782        Ok(Self {
783            old_root,
784            node_mutations,
785            new_pairs,
786            new_root,
787        })
788    }
789}