Skip to main content

miden_crypto/merkle/smt/forest/
mod.rs

1use alloc::{collections::BTreeSet, vec::Vec};
2
3use super::{EmptySubtreeRoots, MerkleError, NodeIndex, SmtLeaf, SmtProof, Word};
4use crate::{
5    Map,
6    merkle::smt::{LeafIndex, SMT_DEPTH, SmtLeafError, SmtProofError, forest::store::SmtStore},
7};
8
9mod store;
10
11#[cfg(test)]
12mod tests;
13
14// SPARSE MERKLE TREE FOREST
15// ================================================================================================
16
17/// An in-memory data collection of sparse Merkle trees (SMTs).
18///
19/// Each SMT in the forest is identified by its root hash. The forest stores all leaves of all SMTs
20/// in the forest, as well as all Merkle paths required to prove membership of any leaf in any SMT.
21///
22/// An empty tree root is always present in the forest.
23///
24/// Example usage:
25///
26/// ```rust
27/// use miden_crypto::{
28///     Felt, ONE, WORD_SIZE, Word, ZERO,
29///     merkle::{
30///         EmptySubtreeRoots,
31///         smt::{MAX_LEAF_ENTRIES, SMT_DEPTH, SmtForest},
32///     },
33/// };
34///
35/// // Create a new SMT forest
36/// let mut forest = SmtForest::new();
37///
38/// // Insert a key-value pair into an SMT with an empty root
39/// let empty_tree_root = *EmptySubtreeRoots::entry(SMT_DEPTH, 0);
40/// let key = Word::new([ZERO; WORD_SIZE]);
41/// let value = Word::new([ONE; WORD_SIZE]);
42/// let new_root = forest.insert(empty_tree_root, key, value).unwrap();
43///
44/// // Insert multiple key-value pairs
45/// let mut entries = Vec::new();
46/// for i in 0..MAX_LEAF_ENTRIES {
47///     let key = Word::new([Felt::new(i as u64); WORD_SIZE]);
48///     let value = Word::new([Felt::new((i + 1) as u64); WORD_SIZE]);
49///     entries.push((key, value));
50/// }
51/// let new_root = forest.batch_insert(new_root, entries.into_iter()).unwrap();
52///
53/// // Open a proof for the inserted key
54/// let proof = forest.open(new_root, key).unwrap();
55///
56/// // Prune SMTs to release memory used by their nodes and leaves
57/// forest.pop_smts(vec![new_root]);
58/// ```
59#[derive(Debug, Clone, Eq, PartialEq)]
60pub struct SmtForest {
61    /// Roots of all SMTs in this forest. Any time an SMT in this forest is updated, we add a new
62    /// root to this set.
63    roots: BTreeSet<Word>,
64
65    /// Stores Merkle paths for all SMTs in this forest.
66    store: SmtStore,
67
68    /// Leaves of all SMTs stored in this forest
69    leaves: Map<Word, SmtLeaf>,
70}
71
72impl Default for SmtForest {
73    fn default() -> Self {
74        Self::new()
75    }
76}
77
78impl SmtForest {
79    // CONSTRUCTORS
80    // --------------------------------------------------------------------------------------------
81
82    /// Creates an empty `SmtForest` instance.
83    pub fn new() -> SmtForest {
84        let roots = BTreeSet::new();
85        let store = SmtStore::new();
86        let leaves = Map::new();
87
88        SmtForest { roots, store, leaves }
89    }
90
91    // DATA EXTRACTORS
92    // --------------------------------------------------------------------------------------------
93
94    /// Returns an opening for the specified key in the SMT with the specified root.
95    ///
96    /// Returns an error if an SMT with this root is not in the forest, or if the forest does
97    /// not have sufficient data to provide an opening for the specified key.
98    pub fn open(&self, root: Word, key: Word) -> Result<SmtProof, MerkleError> {
99        if !self.contains_root(root) {
100            return Err(MerkleError::RootNotInStore(root));
101        }
102
103        let leaf_index = NodeIndex::from(LeafIndex::from(key));
104
105        let proof = self.store.get_path(root, leaf_index)?;
106        let path = proof.path.try_into()?;
107        let leaf_hash = proof.value;
108        let leaf = if leaf_hash == crate::EMPTY_WORD {
109            SmtLeaf::new_empty(LeafIndex::from(key))
110        } else {
111            let Some(leaf) = self.leaves.get(&leaf_hash).cloned() else {
112                return Err(MerkleError::UntrackedKey(key));
113            };
114            leaf
115        };
116
117        SmtProof::new(path, leaf).map_err(|error| match error {
118            SmtProofError::InvalidMerklePathLength(depth) => MerkleError::InvalidPathLength(depth),
119        })
120    }
121
122    // STATE MUTATORS
123    // --------------------------------------------------------------------------------------------
124
125    /// Inserts the specified key-value pair into an SMT with the specified root. This will also
126    /// add a new root to the forest. Returns the new root.
127    ///
128    /// Returns an error if an SMT with the specified root is not in the forest, these is not
129    /// enough data in the forest to perform the insert, or if the insert would create a leaf
130    /// with too many entries.
131    pub fn insert(&mut self, root: Word, key: Word, value: Word) -> Result<Word, MerkleError> {
132        self.batch_insert(root, vec![(key, value)])
133    }
134
135    /// Inserts the specified key-value pairs into an SMT with the specified root. This will also
136    /// add a single new root to the forest for the entire batch of inserts. Returns the new root.
137    ///
138    /// Returns an error if an SMT with the specified root is not in the forest, these is not
139    /// enough data in the forest to perform the insert, or if the insert would create a leaf
140    /// with too many entries.
141    pub fn batch_insert(
142        &mut self,
143        root: Word,
144        entries: impl IntoIterator<Item = (Word, Word)> + Clone,
145    ) -> Result<Word, MerkleError> {
146        if !self.contains_root(root) {
147            return Err(MerkleError::RootNotInStore(root));
148        }
149
150        // Find all affected leaf indices
151        let indices = entries
152            .clone()
153            .into_iter()
154            .map(|(key, _)| LeafIndex::from(key))
155            .collect::<BTreeSet<_>>();
156
157        // Create new SmtLeaf objects for updated key-value pairs
158        let mut new_leaves = Map::new();
159        for index in indices {
160            let node_index = NodeIndex::from(index);
161            let current_hash = self.store.get_node(root, node_index)?;
162
163            let current_leaf = self
164                .leaves
165                .get(&current_hash)
166                .cloned()
167                .unwrap_or_else(|| SmtLeaf::new_empty(index));
168
169            new_leaves.insert(index, (current_hash, current_leaf));
170        }
171        for (key, value) in entries {
172            let index = LeafIndex::from(key);
173            let (_old_hash, leaf) = new_leaves.get_mut(&index).unwrap();
174            if value == crate::EMPTY_WORD {
175                let _ = leaf.remove(key);
176            } else {
177                leaf.insert(key, value).map_err(to_merkle_error)?;
178            }
179        }
180
181        // Calculate new leaf hashes, skip processing unchanged leaves
182        new_leaves = new_leaves
183            .into_iter()
184            .filter_map(|(key, (old_hash, leaf))| {
185                let new_hash = leaf.hash();
186                if new_hash == old_hash {
187                    None
188                } else {
189                    Some((key, (new_hash, leaf)))
190                }
191            })
192            .collect();
193
194        // Update SmtStore with new leaf hashes
195        let new_leaf_entries =
196            new_leaves.iter().map(|(index, leaf)| (NodeIndex::from(*index), leaf.0));
197        let new_root = self.store.set_leaves(root, new_leaf_entries)?;
198
199        // Update successful, insert new leaves into the forest
200        for (leaf_hash, leaf) in new_leaves.into_values() {
201            if leaf_hash != crate::EMPTY_WORD {
202                self.leaves.insert(leaf_hash, leaf);
203            }
204        }
205        // Never register the empty tree root in self.roots, as it is always implicitly valid
206        // and the empty hash nodes are pre-populated infrastructure in the SmtStore.
207        // Adding it here would let `pop_smts` walk and destroy those nodes,
208        // corrupting the store for all future operations.
209        if new_root != *EmptySubtreeRoots::entry(SMT_DEPTH, 0) {
210            self.roots.insert(new_root);
211        }
212
213        Ok(new_root)
214    }
215
216    /// Removes the specified SMTs (identified by their roots) from the forest.
217    /// Releases memory used by nodes and leaves that are no longer reachable.
218    /// Roots not in the forest and empty trees are ignored.
219    pub fn pop_smts(&mut self, roots: impl IntoIterator<Item = Word>) {
220        let roots = roots
221            .into_iter()
222            .filter(|root| {
223                // don't use self.contains_root here because we don't remove empty trees
224                self.roots.contains(root)
225            })
226            .collect::<Vec<_>>();
227
228        for root in &roots {
229            self.roots.remove(root);
230        }
231
232        for leaf in self.store.remove_roots(roots) {
233            self.leaves.remove(&leaf);
234        }
235    }
236
237    // HELPER METHODS
238    // --------------------------------------------------------------------------------------------
239
240    /// Checks if the forest contains the specified root or if it is the empty tree root
241    /// (always present in the forest).
242    fn contains_root(&self, root: Word) -> bool {
243        self.roots.contains(&root) || *EmptySubtreeRoots::entry(SMT_DEPTH, 0) == root
244    }
245}
246
247fn to_merkle_error(err: SmtLeafError) -> MerkleError {
248    match err {
249        SmtLeafError::TooManyLeafEntries { actual } => MerkleError::TooManyLeafEntries { actual },
250        _ => unreachable!("other SmtLeafError variants should not be possible here"),
251    }
252}