Skip to content

Commit

Permalink
feat: add PartialEq,Eq and De/Serialize for AdviceInputs and …
Browse files Browse the repository at this point in the history
…`AdviceMap` (#1494)
  • Loading branch information
SantiagoPittella authored Sep 13, 2024
1 parent 4923e3d commit 57beef8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.10.6 (TBD)

#### Enhancements

- Added `PartialEq`, `Eq`, `Serialize` and `Deserialize` to `AdviceMap` and `AdviceInputs` structs (#1494).

## 0.10.5 (2024-08-21)

#### Enhancements
Expand Down
59 changes: 56 additions & 3 deletions processor/src/host/advice/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use alloc::vec::Vec;

use vm_core::crypto::hash::RpoDigest;
use vm_core::{
crypto::hash::RpoDigest,
utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
};

use super::{AdviceMap, Felt, InnerNodeInfo, InputError, MerkleStore};

Expand All @@ -19,7 +22,7 @@ use super::{AdviceMap, Felt, InnerNodeInfo, InputError, MerkleStore};
/// 3. Merkle store, which is used to provide nondeterministic inputs for instructions that operates
/// with Merkle trees.
#[cfg(not(feature = "testing"))]
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AdviceInputs {
stack: Vec<Felt>,
map: AdviceMap,
Expand Down Expand Up @@ -132,13 +135,63 @@ impl AdviceInputs {
}
}

impl Serializable for AdviceInputs {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let Self { stack, map, store } = self;
stack.write_into(target);
map.write_into(target);
store.write_into(target);
}
}

impl Deserializable for AdviceInputs {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let stack = Vec::<Felt>::read_from(source)?;
let map = AdviceMap::read_from(source)?;
let store = MerkleStore::read_from(source)?;
Ok(Self { stack, map, store })
}
}

// TESTING
// ================================================================================================

#[cfg(feature = "testing")]
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AdviceInputs {
pub stack: Vec<Felt>,
pub map: AdviceMap,
pub store: MerkleStore,
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
use winter_utils::{Deserializable, Serializable};

use crate::AdviceInputs;

#[test]
fn test_advice_inputs_eq() {
let advice1 = AdviceInputs::default();
let advice2 = AdviceInputs::default();

assert_eq!(advice1, advice2);

let advice1 = AdviceInputs::default().with_stack_values([1, 2, 3].iter().copied()).unwrap();
let advice2 = AdviceInputs::default().with_stack_values([1, 2, 3].iter().copied()).unwrap();

assert_eq!(advice1, advice2);
}

#[test]
fn test_advice_inputs_serialization() {
let advice1 = AdviceInputs::default().with_stack_values([1, 2, 3].iter().copied()).unwrap();
let bytes = advice1.to_bytes();
let advice2 = AdviceInputs::read_from_bytes(&bytes).unwrap();

assert_eq!(advice1, advice2);
}
}
45 changes: 43 additions & 2 deletions processor/src/host/advice/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use alloc::{
vec::Vec,
};

use vm_core::crypto::hash::RpoDigest;
use vm_core::{
crypto::hash::RpoDigest,
utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
};

use super::Felt;

Expand All @@ -15,7 +18,7 @@ use super::Felt;
/// 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)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AdviceMap(BTreeMap<RpoDigest, Vec<Felt>>);

impl AdviceMap {
Expand Down Expand Up @@ -60,3 +63,41 @@ impl Extend<(RpoDigest, Vec<Felt>)> for AdviceMap {
self.0.extend(iter)
}
}

impl Serializable for AdviceMap {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_usize(self.0.len());
for (key, values) in self.0.iter() {
target.write((key, values));
}
}
}

impl Deserializable for AdviceMap {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let mut map = BTreeMap::new();
let count = source.read_usize()?;
for _ in 0..count {
let (key, values) = source.read()?;
map.insert(key, values);
}
Ok(Self(map))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_advice_map_serialization() {
let mut map1 = AdviceMap::new();
map1.insert(RpoDigest::default(), vec![Felt::from(1u32), Felt::from(2u32)]);

let bytes = map1.to_bytes();

let map2 = AdviceMap::read_from_bytes(&bytes).unwrap();

assert_eq!(map1, map2);
}
}

0 comments on commit 57beef8

Please sign in to comment.