forked from 0xPolygonMiden/miden-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add AdviceMap wrapper (0xPolygonMiden#1207)
- Loading branch information
1 parent
9db662f
commit 6e7bbda
Showing
15 changed files
with
136 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::{BTreeMap, Felt, Vec}; | ||
use vm_core::{crypto::hash::RpoDigest, utils::collections::btree_map::IntoIter}; | ||
|
||
// ADVICE MAP | ||
// ================================================================================================ | ||
|
||
/// Defines a set of non-deterministic (advice) inputs which the VM can access by their keys. | ||
/// | ||
/// Each key maps to one or more field element. To access the elements, the VM can move the values | ||
/// associated with a given key onto the advice stack using `adv.push_mapval` instruction. The VM | ||
/// can also insert new values into the advice map during execution. | ||
#[derive(Debug, Clone, Default)] | ||
pub struct AdviceMap(BTreeMap<RpoDigest, Vec<Felt>>); | ||
|
||
impl AdviceMap { | ||
/// Creates a new advice map. | ||
pub fn new() -> Self { | ||
Self(BTreeMap::<RpoDigest, Vec<Felt>>::new()) | ||
} | ||
|
||
/// Returns the values associated with given key. | ||
pub fn get(&self, key: &RpoDigest) -> Option<&[Felt]> { | ||
self.0.get(key).map(|v| v.as_slice()) | ||
} | ||
|
||
/// Inserts a key value pair in the advice map and returns the inserted value. | ||
pub fn insert(&mut self, key: RpoDigest, value: Vec<Felt>) -> Option<Vec<Felt>> { | ||
self.0.insert(key, value) | ||
} | ||
|
||
/// Removes the value associated with the key and returns the removed element. | ||
pub fn remove(&mut self, key: RpoDigest) -> Option<Vec<Felt>> { | ||
self.0.remove(&key) | ||
} | ||
} | ||
|
||
impl IntoIterator for AdviceMap { | ||
type Item = (RpoDigest, Vec<Felt>); | ||
type IntoIter = IntoIter<RpoDigest, Vec<Felt>>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.0.into_iter() | ||
} | ||
} | ||
|
||
impl Extend<(RpoDigest, Vec<Felt>)> for AdviceMap { | ||
fn extend<T: IntoIterator<Item = (RpoDigest, Vec<Felt>)>>(&mut self, iter: T) { | ||
self.0.extend(iter) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.