miden_core/advice/
map.rs

1use alloc::{
2    boxed::Box,
3    collections::{BTreeMap, btree_map::IntoIter},
4    vec::Vec,
5};
6
7use miden_crypto::{Felt, Word, utils::collections::KvMap};
8
9use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
10
11// ADVICE MAP
12// ================================================================================================
13
14/// Defines a set of non-deterministic (advice) inputs which the VM can access by their keys.
15///
16/// Each key maps to one or more field element. To access the elements, the VM can move the values
17/// associated with a given key onto the advice stack using `adv.push_mapval` instruction. The VM
18/// can also insert new values into the advice map during execution.
19#[derive(Debug, Clone, Default, PartialEq, Eq)]
20pub struct AdviceMap(BTreeMap<Word, Vec<Felt>>);
21
22/// Pair representing a key-value entry in an [`AdviceMap`]
23type MapEntry = (Word, Vec<Felt>);
24
25impl AdviceMap {
26    /// Creates a new advice map.
27    pub fn new() -> Self {
28        Self(BTreeMap::<Word, Vec<Felt>>::new())
29    }
30
31    /// Returns the values associated with given key.
32    pub fn get(&self, key: &Word) -> Option<&[Felt]> {
33        self.0.get(key).map(|v| v.as_slice())
34    }
35
36    /// Inserts a key value pair in the advice map and returns the inserted value.
37    pub fn insert(&mut self, key: Word, value: Vec<Felt>) -> Option<Vec<Felt>> {
38        self.0.insert(key, value)
39    }
40
41    /// Removes the value associated with the key and returns the removed element.
42    pub fn remove(&mut self, key: &Word) -> Option<Vec<Felt>> {
43        self.0.remove(key)
44    }
45
46    /// Returns the number of key value pairs in the advice map.
47    pub fn len(&self) -> usize {
48        self.0.len()
49    }
50
51    /// Returns true if the advice map is empty.
52    pub fn is_empty(&self) -> bool {
53        self.0.is_empty()
54    }
55
56    /// Merges all entries from the given [`AdviceMap`] into the current advice map.
57    ///
58    /// If an entry from the new map already exists with the same key but different value,
59    /// an error is returned containing the existing entry along with the value that would replace
60    /// it. The current map remains unchanged.
61    pub fn merge_advice_map(&mut self, other: &AdviceMap) -> Result<(), (MapEntry, Vec<Felt>)> {
62        // Check if any values are already present and different from those we are merging.
63        for (key, value) in other.iter() {
64            if let Some(existing) = self.get(key) {
65                if existing != value {
66                    return Err(((*key, existing.to_vec()), value.to_vec()));
67                }
68            }
69        }
70
71        // Insert all other values, overwriting existing ones which we have checked are the same.
72        for (key, value) in other.iter() {
73            self.insert(*key, value.clone());
74        }
75        Ok(())
76    }
77}
78
79impl From<BTreeMap<Word, Vec<Felt>>> for AdviceMap {
80    fn from(value: BTreeMap<Word, Vec<Felt>>) -> Self {
81        Self(value)
82    }
83}
84
85impl IntoIterator for AdviceMap {
86    type Item = (Word, Vec<Felt>);
87    type IntoIter = IntoIter<Word, Vec<Felt>>;
88
89    fn into_iter(self) -> Self::IntoIter {
90        self.0.into_iter()
91    }
92}
93
94impl FromIterator<(Word, Vec<Felt>)> for AdviceMap {
95    fn from_iter<T: IntoIterator<Item = (Word, Vec<Felt>)>>(iter: T) -> Self {
96        iter.into_iter().collect::<BTreeMap<Word, Vec<Felt>>>().into()
97    }
98}
99
100impl KvMap<Word, Vec<Felt>> for AdviceMap {
101    fn get(&self, key: &Word) -> Option<&Vec<Felt>> {
102        self.0.get(key)
103    }
104
105    fn contains_key(&self, key: &Word) -> bool {
106        self.0.contains_key(key)
107    }
108
109    fn len(&self) -> usize {
110        self.len()
111    }
112
113    fn insert(&mut self, key: Word, value: Vec<Felt>) -> Option<Vec<Felt>> {
114        self.insert(key, value)
115    }
116
117    fn remove(&mut self, key: &Word) -> Option<Vec<Felt>> {
118        self.remove(key)
119    }
120
121    fn iter(&self) -> Box<dyn Iterator<Item = (&Word, &Vec<Felt>)> + '_> {
122        Box::new(self.0.iter())
123    }
124}
125
126impl Extend<(Word, Vec<Felt>)> for AdviceMap {
127    fn extend<T: IntoIterator<Item = (Word, Vec<Felt>)>>(&mut self, iter: T) {
128        self.0.extend(iter)
129    }
130}
131
132impl Serializable for AdviceMap {
133    fn write_into<W: ByteWriter>(&self, target: &mut W) {
134        target.write_usize(self.0.len());
135        for (key, values) in self.0.iter() {
136            target.write((key, values));
137        }
138    }
139}
140
141impl Deserializable for AdviceMap {
142    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
143        let mut map = BTreeMap::new();
144        let count = source.read_usize()?;
145        for _ in 0..count {
146            let (key, values) = source.read()?;
147            map.insert(key, values);
148        }
149        Ok(Self(map))
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn test_advice_map_serialization() {
159        let mut map1 = AdviceMap::new();
160        map1.insert(Word::default(), vec![Felt::from(1u32), Felt::from(2u32)]);
161
162        let bytes = map1.to_bytes();
163
164        let map2 = AdviceMap::read_from_bytes(&bytes).unwrap();
165
166        assert_eq!(map1, map2);
167    }
168}