Skip to main content

miden_protocol/account/storage/map/
partial.rs

1use alloc::collections::BTreeMap;
2
3use miden_core::utils::{Deserializable, Serializable};
4use miden_crypto::Word;
5use miden_crypto::merkle::smt::{LeafIndex, PartialSmt, SMT_DEPTH, SmtLeaf, SmtProof};
6use miden_crypto::merkle::{InnerNodeInfo, MerkleError};
7
8use crate::account::{StorageMap, StorageMapWitness};
9use crate::utils::serde::{ByteReader, DeserializationError};
10
11/// A partial representation of a [`StorageMap`], containing only proofs for a subset of the
12/// key-value pairs.
13///
14/// A partial storage map carries only the Merkle authentication data a transaction will need.
15/// Every included entry pairs a value with its proof, letting the transaction kernel verify reads
16/// (and prepare writes) without needing the complete tree.
17///
18/// ## Guarantees
19///
20/// This type guarantees that the raw key-value pairs it contains are all present in the
21/// contained partial SMT. Note that the inverse is not necessarily true. The SMT may contain more
22/// entries than the map because to prove inclusion of a given raw key A an
23/// [`SmtLeaf::Multiple`] may be present that contains both keys hash(A) and hash(B). However, B may
24/// not be present in the key-value pairs and this is a valid state.
25#[derive(Clone, Debug, PartialEq, Eq, Default)]
26pub struct PartialStorageMap {
27    partial_smt: PartialSmt,
28    /// The entries of the map where the key is the raw user-chosen one.
29    ///
30    /// It is an invariant of this type that the map's entries are always consistent with the
31    /// partial SMT's entries and vice-versa.
32    entries: BTreeMap<Word, Word>,
33}
34
35impl PartialStorageMap {
36    // CONSTRUCTORS
37    // --------------------------------------------------------------------------------------------
38
39    /// Constructs a [`PartialStorageMap`] from a [`StorageMap`] root.
40    ///
41    /// For conversion from a [`StorageMap`], prefer [`Self::new_minimal`] to be more explicit.
42    pub fn new(root: Word) -> Self {
43        PartialStorageMap {
44            partial_smt: PartialSmt::new(root),
45            entries: BTreeMap::new(),
46        }
47    }
48
49    /// Returns a new instance of a [`PartialStorageMap`] with all provided witnesses added to it.
50    pub fn with_witnesses(
51        witnesses: impl IntoIterator<Item = StorageMapWitness>,
52    ) -> Result<Self, MerkleError> {
53        let mut map = BTreeMap::new();
54
55        let partial_smt = PartialSmt::from_proofs(witnesses.into_iter().map(|witness| {
56            map.extend(witness.entries());
57            SmtProof::from(witness)
58        }))?;
59
60        Ok(PartialStorageMap { partial_smt, entries: map })
61    }
62
63    /// Converts a [`StorageMap`] into a partial storage representation.
64    ///
65    /// The resulting [`PartialStorageMap`] will contain the _full_ entries and merkle paths of the
66    /// original storage map.
67    pub fn new_full(storage_map: StorageMap) -> Self {
68        let partial_smt = PartialSmt::from(storage_map.smt);
69        let entries = storage_map.entries;
70
71        PartialStorageMap { partial_smt, entries }
72    }
73
74    /// Converts a [`StorageMap`] into a partial storage representation.
75    ///
76    /// The resulting [`PartialStorageMap`] will represent the root of the storage map, but not
77    /// track any key-value pairs, which means it is the most _minimal_ representation of the
78    /// storage map.
79    pub fn new_minimal(storage_map: &StorageMap) -> Self {
80        Self::new(storage_map.root())
81    }
82
83    // ACCESSORS
84    // --------------------------------------------------------------------------------------------
85
86    /// Returns a reference to the underlying [`PartialSmt`].
87    pub fn partial_smt(&self) -> &PartialSmt {
88        &self.partial_smt
89    }
90
91    /// Returns the root of the underlying [`PartialSmt`].
92    pub fn root(&self) -> Word {
93        self.partial_smt.root()
94    }
95
96    /// Looks up the provided key in this map and returns:
97    /// - a non-empty [`Word`] if the key is tracked by this map and exists in it,
98    /// - [`Word::empty`] if the key is tracked by this map and does not exist,
99    /// - `None` if the key is not tracked by this map.
100    pub fn get(&self, raw_key: &Word) -> Option<Word> {
101        let hashed_key = StorageMap::hash_key(*raw_key);
102        // This returns an error if the key is not tracked which we map to a `None`.
103        self.partial_smt.get_value(&hashed_key).ok()
104    }
105
106    /// Returns an opening of the leaf associated with the raw key.
107    ///
108    /// Conceptually, an opening is a Merkle path to the leaf, as well as the leaf itself.
109    ///
110    /// # Errors
111    ///
112    /// Returns an error if:
113    /// - the key is not tracked by this partial storage map.
114    pub fn open(&self, raw_key: &Word) -> Result<StorageMapWitness, MerkleError> {
115        let hashed_key = StorageMap::hash_key(*raw_key);
116        let smt_proof = self.partial_smt.open(&hashed_key)?;
117        let value = self.entries.get(raw_key).copied().unwrap_or_default();
118
119        // SAFETY: The key value pair is guaranteed to be present in the provided proof since we
120        // open its hashed version and because of the guarantees of the partial storage map.
121        Ok(StorageMapWitness::new_unchecked(smt_proof, [(*raw_key, value)]))
122    }
123
124    // ITERATORS
125    // --------------------------------------------------------------------------------------------
126
127    /// Returns an iterator over the leaves of the underlying [`PartialSmt`].
128    pub fn leaves(&self) -> impl Iterator<Item = (LeafIndex<SMT_DEPTH>, &SmtLeaf)> {
129        self.partial_smt.leaves()
130    }
131
132    /// Returns an iterator over the key-value pairs in this storage map.
133    ///
134    /// Note that the returned key is the raw map key.
135    pub fn entries(&self) -> impl Iterator<Item = (&Word, &Word)> {
136        self.entries.iter()
137    }
138
139    /// Returns an iterator over the inner nodes of the underlying [`PartialSmt`].
140    pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ {
141        self.partial_smt.inner_nodes()
142    }
143
144    // MUTATORS
145    // --------------------------------------------------------------------------------------------
146
147    /// Adds a [`StorageMapWitness`] for the specific key-value pair to this [`PartialStorageMap`].
148    pub fn add(&mut self, witness: StorageMapWitness) -> Result<(), MerkleError> {
149        self.entries.extend(witness.entries().map(|(key, value)| (*key, *value)));
150        self.partial_smt.add_proof(SmtProof::from(witness))
151    }
152}
153
154impl Serializable for PartialStorageMap {
155    fn write_into<W: miden_core::utils::ByteWriter>(&self, target: &mut W) {
156        target.write(&self.partial_smt);
157        target.write_usize(self.entries.len());
158        target.write_many(self.entries.keys());
159    }
160}
161
162impl Deserializable for PartialStorageMap {
163    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
164        let mut map = BTreeMap::new();
165
166        let partial_smt: PartialSmt = source.read()?;
167        let num_entries: usize = source.read()?;
168
169        for _ in 0..num_entries {
170            let key: Word = source.read()?;
171            let hashed_map_key = StorageMap::hash_key(key);
172            let value = partial_smt.get_value(&hashed_map_key).map_err(|err| {
173                DeserializationError::InvalidValue(format!(
174                    "failed to find map key {key} in partial SMT: {err}"
175                ))
176            })?;
177            map.insert(key, value);
178        }
179
180        Ok(PartialStorageMap { partial_smt, entries: map })
181    }
182}