miden-crypto/src/merkle/tiered_smt/mod.rs
src/merkle/tiered_smt/mod.rsRaw
1use super::{
2 BTreeMap, BTreeSet, EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex,
3 Rpo256, RpoDigest, StarkField, Vec, Word,
4};
5use core::cmp;
6
7mod nodes;
8use nodes::NodeStore;
9
10mod values;
11use values::ValueStore;
12
13#[cfg(test)]
14mod tests;
15
16// TIERED SPARSE MERKLE TREE
17// ================================================================================================
18
19/// Tiered (compacted) Sparse Merkle tree mapping 256-bit keys to 256-bit values. Both keys and
20/// values are represented by 4 field elements.
21///
22/// Leaves in the tree can exist only on specific depths called "tiers". These depths are: 16, 32,
23/// 48, and 64. Initially, when a tree is empty, it is equivalent to an empty Sparse Merkle tree
24/// of depth 64 (i.e., leaves at depth 64 are set to [ZERO; 4]). As non-empty values are inserted
25/// into the tree they are added to the first available tier.
26///
27/// For example, when the first key-value pair is inserted, it will be stored in a node at depth
28/// 16 such that the 16 most significant bits of the key determine the position of the node at
29/// depth 16. If another value with a key sharing the same 16-bit prefix is inserted, both values
30/// move into the next tier (depth 32). This process is repeated until values end up at the bottom
31/// tier (depth 64). If multiple values have keys with a common 64-bit prefix, such key-value pairs
32/// are stored in a sorted list at the bottom tier.
33///
34/// To differentiate between internal and leaf nodes, node values are computed as follows:
35/// - Internal nodes: hash(left_child, right_child).
36/// - Leaf node at depths 16, 32, or 64: hash(key, value, domain=depth).
37/// - Leaf node at depth 64: hash([key_0, value_0, ..., key_n, value_n, domain=64]).
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct TieredSmt {
40 root: RpoDigest,
41 nodes: NodeStore,
42 values: ValueStore,
43}
44
45impl TieredSmt {
46 // CONSTANTS
47 // --------------------------------------------------------------------------------------------
48
49 /// The number of levels between tiers.
50 const TIER_SIZE: u8 = 16;
51
52 /// Depths at which leaves can exist in a tiered SMT.
53 const TIER_DEPTHS: [u8; 4] = [16, 32, 48, 64];
54
55 /// Maximum node depth. This is also the bottom tier of the tree.
56 const MAX_DEPTH: u8 = 64;
57
58 /// Value of an empty leaf.
59 pub const EMPTY_VALUE: Word = super::empty_roots::EMPTY_WORD;
60
61 // CONSTRUCTORS
62 // --------------------------------------------------------------------------------------------
63
64 /// Returns a new [TieredSmt] instantiated with the specified key-value pairs.
65 ///
66 /// # Errors
67 /// Returns an error if the provided entries contain multiple values for the same key.
68 pub fn with_leaves<R, I>(entries: R) -> Result<Self, MerkleError>
69 where
70 R: IntoIterator<IntoIter = I>,
71 I: Iterator<Item = (RpoDigest, Word)> + ExactSizeIterator,
72 {
73 // create an empty tree
74 let mut tree = Self::default();
75
76 // append leaves to the tree returning an error if a duplicate entry for the same key
77 // is found
78 let mut empty_entries = BTreeSet::new();
79 for (key, value) in entries {
80 let old_value = tree.insert(key, value);
81 if old_value != Self::EMPTY_VALUE || empty_entries.contains(&key) {
82 return Err(MerkleError::DuplicateValuesForKey(key));
83 }
84 // if we've processed an empty entry, add the key to the set of empty entry keys, and
85 // if this key was already in the set, return an error
86 if value == Self::EMPTY_VALUE && !empty_entries.insert(key) {
87 return Err(MerkleError::DuplicateValuesForKey(key));
88 }
89 }
90 Ok(tree)
91 }
92
93 // PUBLIC ACCESSORS
94 // --------------------------------------------------------------------------------------------
95
96 /// Returns the root of this Merkle tree.
97 pub const fn root(&self) -> RpoDigest {
98 self.root
99 }
100
101 /// Returns a node at the specified index.
102 ///
103 /// # Errors
104 /// Returns an error if:
105 /// - The specified index depth is 0 or greater than 64.
106 /// - The node with the specified index does not exists in the Merkle tree. This is possible
107 /// when a leaf node with the same index prefix exists at a tier higher than the requested
108 /// node.
109 pub fn get_node(&self, index: NodeIndex) -> Result<RpoDigest, MerkleError> {
110 self.nodes.get_node(index)
111 }
112
113 /// Returns a Merkle path from the node at the specified index to the root.
114 ///
115 /// The node itself is not included in the path.
116 ///
117 /// # Errors
118 /// Returns an error if:
119 /// - The specified index depth is 0 or greater than 64.
120 /// - The node with the specified index does not exists in the Merkle tree. This is possible
121 /// when a leaf node with the same index prefix exists at a tier higher than the node to
122 /// which the path is requested.
123 pub fn get_path(&self, index: NodeIndex) -> Result<MerklePath, MerkleError> {
124 self.nodes.get_path(index)
125 }
126
127 /// Returns the value associated with the specified key.
128 ///
129 /// If nothing was inserted into this tree for the specified key, [ZERO; 4] is returned.
130 pub fn get_value(&self, key: RpoDigest) -> Word {
131 match self.values.get(&key) {
132 Some(value) => *value,
133 None => Self::EMPTY_VALUE,
134 }
135 }
136
137 // STATE MUTATORS
138 // --------------------------------------------------------------------------------------------
139
140 /// Inserts the provided value into the tree under the specified key and returns the value
141 /// previously stored under this key.
142 ///
143 /// If the value for the specified key was not previously set, [ZERO; 4] is returned.
144 pub fn insert(&mut self, key: RpoDigest, value: Word) -> Word {
145 // if an empty value is being inserted, remove the leaf node to make it look as if the
146 // value was never inserted
147 if value == Self::EMPTY_VALUE {
148 return self.remove_leaf_node(key);
149 }
150
151 // insert the value into the value store, and if nothing has changed, return
152 let (old_value, is_update) = match self.values.insert(key, value) {
153 Some(old_value) => {
154 if old_value == value {
155 return old_value;
156 }
157 (old_value, true)
158 }
159 None => (Self::EMPTY_VALUE, false),
160 };
161
162 // determine the index for the value node; this index could have 3 different meanings:
163 // - it points to a root of an empty subtree (excluding depth = 64); in this case, we can
164 // replace the node with the value node immediately.
165 // - it points to a node at the bottom tier (i.e., depth = 64); in this case, we need to
166 // process bottom-tier insertion which will be handled by insert_leaf_node().
167 // - it points to an existing leaf node; this node could be a node with the same key or a
168 // different key with a common prefix; in the latter case, we'll need to move the leaf
169 // to a lower tier
170 let (index, leaf_exists) = self.nodes.get_insert_location(&key);
171 debug_assert!(!is_update || leaf_exists);
172
173 // if the returned index points to a leaf, and this leaf is for a different key (i.e., we
174 // are not updating a value for an existing key), we need to replace this leaf with a tree
175 // containing leaves for both the old and the new key-value pairs
176 if leaf_exists && !is_update {
177 // get the key-value pair for the key with the same prefix; since the key-value
178 // pair has already been inserted into the value store, we need to filter it out
179 // when looking for the other key-value pair
180 let (other_key, other_value) = self
181 .values
182 .get_first_filtered(index_to_prefix(&index), &key)
183 .expect("other key-value pair not found");
184
185 // determine how far down the tree should we move the leaves
186 let common_prefix_len = get_common_prefix_tier(&key, other_key);
187 let depth = cmp::min(common_prefix_len + Self::TIER_SIZE, Self::MAX_DEPTH);
188
189 // compute node locations for new and existing key-value paris
190 let new_index = key_to_index(&key, depth);
191 let other_index = key_to_index(other_key, depth);
192
193 // compute node values for the new and existing key-value pairs
194 let new_node = self.build_leaf_node(new_index, key, value);
195 let other_node = self.build_leaf_node(other_index, *other_key, *other_value);
196
197 // replace the leaf located at index with a subtree containing nodes for new and
198 // existing key-value paris
199 self.root = self.nodes.replace_leaf_with_subtree(
200 index,
201 [(new_index, new_node), (other_index, other_node)],
202 );
203 } else {
204 // if the returned index points to an empty subtree, or a leaf with the same key (i.e.,
205 // we are performing an update), or a leaf is at the bottom tier, compute its node
206 // value and do a simple insert
207 let node = self.build_leaf_node(index, key, value);
208 self.root = self.nodes.insert_leaf_node(index, node);
209 }
210
211 old_value
212 }
213
214 // ITERATORS
215 // --------------------------------------------------------------------------------------------
216
217 /// Returns an iterator over all inner nodes of this [TieredSmt] (i.e., nodes not at depths 16
218 /// 32, 48, or 64).
219 ///
220 /// The iterator order is unspecified.
221 pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
222 self.nodes.inner_nodes()
223 }
224
225 /// Returns an iterator over upper leaves (i.e., depth = 16, 32, or 48) for this [TieredSmt]
226 /// where each yielded item is a (node, key, value) tuple.
227 ///
228 /// The iterator order is unspecified.
229 pub fn upper_leaves(&self) -> impl Iterator<Item = (RpoDigest, RpoDigest, Word)> + '_ {
230 self.nodes.upper_leaves().map(|(index, node)| {
231 let key_prefix = index_to_prefix(index);
232 let (key, value) = self.values.get_first(key_prefix).expect("upper leaf not found");
233 (*node, *key, *value)
234 })
235 }
236
237 /// Returns an iterator over bottom leaves (i.e., depth = 64) of this [TieredSmt].
238 ///
239 /// Each yielded item consists of the hash of the leaf and its contents, where contents is
240 /// a vector containing key-value pairs of entries storied in this leaf.
241 ///
242 /// The iterator order is unspecified.
243 pub fn bottom_leaves(&self) -> impl Iterator<Item = (RpoDigest, Vec<(RpoDigest, Word)>)> + '_ {
244 self.nodes.bottom_leaves().map(|(&prefix, node)| {
245 let values = self.values.get_all(prefix).expect("bottom leaf not found");
246 (*node, values)
247 })
248 }
249
250 // HELPER METHODS
251 // --------------------------------------------------------------------------------------------
252
253 /// Removes the node holding the key-value pair for the specified key from this tree, and
254 /// returns the value associated with the specified key.
255 ///
256 /// If no value was associated with the specified key, [ZERO; 4] is returned.
257 fn remove_leaf_node(&mut self, key: RpoDigest) -> Word {
258 // remove the key-value pair from the value store; if no value was associated with the
259 // specified key, return.
260 let old_value = match self.values.remove(&key) {
261 Some(old_value) => old_value,
262 None => return Self::EMPTY_VALUE,
263 };
264
265 // determine the location of the leaf holding the key-value pair to be removed
266 let (index, leaf_exists) = self.nodes.get_insert_location(&key);
267 debug_assert!(index.depth() == Self::MAX_DEPTH || leaf_exists);
268
269 // if the leaf is at the bottom tier and after removing the key-value pair from it, the
270 // leaf is still not empty, just recompute its hash and update the leaf node.
271 if index.depth() == Self::MAX_DEPTH {
272 if let Some(values) = self.values.get_all(index.value()) {
273 let node = hash_bottom_leaf(&values);
274 self.root = self.nodes.update_leaf_node(index, node);
275 return old_value;
276 };
277 }
278
279 // if the removed key-value pair has a lone sibling at the current tier with a root at
280 // higher tier, we need to move the sibling to a higher tier
281 if let Some((sib_key, sib_val, new_sib_index)) = self.values.get_lone_sibling(index) {
282 // determine the current index of the sibling node
283 let sib_index = key_to_index(sib_key, index.depth());
284 debug_assert!(sib_index.depth() > new_sib_index.depth());
285
286 // compute node value for the new location of the sibling leaf and replace the subtree
287 // with this leaf node
288 let node = self.build_leaf_node(new_sib_index, *sib_key, *sib_val);
289 let new_sib_depth = new_sib_index.depth();
290 self.root = self.nodes.replace_subtree_with_leaf(index, sib_index, new_sib_depth, node);
291 } else {
292 // if the removed key-value pair did not have a sibling at the current tier with a
293 // root at higher tiers, just clear the leaf node
294 self.root = self.nodes.clear_leaf_node(index);
295 }
296
297 old_value
298 }
299
300 /// Builds and returns a leaf node value for the node located as the specified index.
301 ///
302 /// This method assumes that the key-value pair for the node has already been inserted into
303 /// the value store, however, for depths 16, 32, and 48, the node is computed directly from
304 /// the passed-in values (for depth 64, the value store is queried to get all the key-value
305 /// pairs located at the specified index).
306 fn build_leaf_node(&self, index: NodeIndex, key: RpoDigest, value: Word) -> RpoDigest {
307 let depth = index.depth();
308 debug_assert!(Self::TIER_DEPTHS.contains(&depth));
309
310 // insert the key into index-key map and compute the new value of the node
311 if index.depth() == Self::MAX_DEPTH {
312 // for the bottom tier, we add the key-value pair to the existing leaf, or create a
313 // new leaf with this key-value pair
314 let values = self.values.get_all(index.value()).unwrap();
315 hash_bottom_leaf(&values)
316 } else {
317 debug_assert_eq!(self.values.get_first(index_to_prefix(&index)), Some(&(key, value)));
318 hash_upper_leaf(key, value, depth)
319 }
320 }
321}
322
323impl Default for TieredSmt {
324 fn default() -> Self {
325 let root = EmptySubtreeRoots::empty_hashes(Self::MAX_DEPTH)[0];
326 Self {
327 root,
328 nodes: NodeStore::new(root),
329 values: ValueStore::default(),
330 }
331 }
332}
333
334// HELPER FUNCTIONS
335// ================================================================================================
336
337/// Returns the value representing the 64 most significant bits of the specified key.
338fn get_key_prefix(key: &RpoDigest) -> u64 {
339 Word::from(key)[3].as_int()
340}
341
342/// Returns the index value shifted to be in the most significant bit positions of the returned
343/// u64 value.
344fn index_to_prefix(index: &NodeIndex) -> u64 {
345 index.value() << (TieredSmt::MAX_DEPTH - index.depth())
346}
347
348/// Returns index for the specified key inserted at the specified depth.
349///
350/// The value for the key is computed by taking n most significant bits from the most significant
351/// element of the key, where n is the specified depth.
352fn key_to_index(key: &RpoDigest, depth: u8) -> NodeIndex {
353 let mse = get_key_prefix(key);
354 let value = match depth {
355 16 | 32 | 48 | 64 => mse >> ((TieredSmt::MAX_DEPTH - depth) as u32),
356 _ => unreachable!("invalid depth: {depth}"),
357 };
358 NodeIndex::new_unchecked(depth, value)
359}
360
361/// Returns tiered common prefix length between the most significant elements of the provided keys.
362///
363/// Specifically:
364/// - returns 64 if the most significant elements are equal.
365/// - returns 48 if the common prefix is between 48 and 63 bits.
366/// - returns 32 if the common prefix is between 32 and 47 bits.
367/// - returns 16 if the common prefix is between 16 and 31 bits.
368/// - returns 0 if the common prefix is fewer than 16 bits.
369fn get_common_prefix_tier(key1: &RpoDigest, key2: &RpoDigest) -> u8 {
370 let e1 = get_key_prefix(key1);
371 let e2 = get_key_prefix(key2);
372 let ex = (e1 ^ e2).leading_zeros() as u8;
373 (ex / 16) * 16
374}
375
376/// Returns a tier for the specified index.
377///
378/// The tiers are defined as follows:
379/// - Tier 0: depth 0 through 16 (inclusive).
380/// - Tier 1: depth 17 through 32 (inclusive).
381/// - Tier 2: depth 33 through 48 (inclusive).
382/// - Tier 3: depth 49 through 64 (inclusive).
383const fn get_index_tier(index: &NodeIndex) -> usize {
384 debug_assert!(index.depth() <= TieredSmt::MAX_DEPTH, "invalid depth");
385 match index.depth() {
386 0..=16 => 0,
387 17..=32 => 1,
388 33..=48 => 2,
389 _ => 3,
390 }
391}
392
393/// Returns true if the specified index is an index for an leaf node (i.e., the depth is 16, 32,
394/// 48, or 64).
395const fn is_leaf_node(index: &NodeIndex) -> bool {
396 matches!(index.depth(), 16 | 32 | 48 | 64)
397}
398
399/// Computes node value for leaves at tiers 16, 32, or 48.
400///
401/// Node value is computed as: hash(key || value, domain = depth).
402pub fn hash_upper_leaf(key: RpoDigest, value: Word, depth: u8) -> RpoDigest {
403 const NUM_UPPER_TIERS: usize = TieredSmt::TIER_DEPTHS.len() - 1;
404 debug_assert!(TieredSmt::TIER_DEPTHS[..NUM_UPPER_TIERS].contains(&depth));
405 Rpo256::merge_in_domain(&[key, value.into()], depth.into())
406}
407
408/// Computes node value for leaves at the bottom tier (depth 64).
409///
410/// Node value is computed as: hash([key_0, value_0, ..., key_n, value_n, domain=64]).
411pub fn hash_bottom_leaf(values: &[(RpoDigest, Word)]) -> RpoDigest {
412 let mut elements = Vec::with_capacity(values.len() * 8);
413 for (key, val) in values.iter() {
414 elements.extend_from_slice(key.as_elements());
415 elements.extend_from_slice(val.as_slice());
416 }
417 // TODO: hash in domain
418 Rpo256::hash_elements(&elements)
419}