Skip to content

Commit

Permalink
Fixed CairoPie output builtin data serde. (#1781)
Browse files Browse the repository at this point in the history
Co-authored-by: Pedro Fontana <[email protected]>
  • Loading branch information
Alon-Ti and pefontana committed Jun 7, 2024
1 parent e941395 commit 3f9428e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* fix: Change (de)serialization of CairoPie's `OutputBuiltinAdditionalData`'s `PublicMemoryPage` to vectors of length 2. [#1781](https://github.com/lambdaclass/cairo-vm/pull/1781)

* fix: Fixed deserialization issue when signature additional data is empty, and the name of the builtin range_check96 [#1785](https://github.com/lambdaclass/cairo-vm/pull/1785)

* refactor + bugfix: Improve arg handling for cairo1-run [#1782](https://github.com/lambdaclass/cairo-vm/pull/1782)
Expand Down
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 52 additions & 1 deletion vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,22 @@ pub struct PublicMemoryPage {
pub size: usize,
}

impl From<&Vec<usize>> for PublicMemoryPage {
fn from(vec: &Vec<usize>) -> Self {
Self {
start: vec[0],
size: vec[1],
}
}
}

// HashMap value based on starknet/core/os/output.cairo usage
pub type Attributes = HashMap<String, Vec<usize>>;
pub type Pages = HashMap<usize, PublicMemoryPage>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct OutputBuiltinAdditionalData {
#[serde(with = "serde_impl::pages")]
pub pages: Pages,
pub attributes: Attributes,
}
Expand Down Expand Up @@ -365,7 +375,7 @@ pub(super) mod serde_impl {
use num_traits::Num;

use super::CAIRO_PIE_VERSION;
use super::{CairoPieMemory, SegmentInfo};
use super::{CairoPieMemory, Pages, PublicMemoryPage, SegmentInfo};
#[cfg(any(target_arch = "wasm32", no_std, not(feature = "std")))]
use crate::alloc::string::ToString;
use crate::stdlib::prelude::{String, Vec};
Expand Down Expand Up @@ -537,6 +547,47 @@ pub(super) mod serde_impl {
serializer.serialize_str(&string)
}

pub mod pages {
use super::*;

pub fn serialize<S>(pages: &Pages, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(pages.len()))?;
for (k, v) in pages {
map.serialize_entry(&k.to_string(), &vec![v.start, v.size])?;
}
map.end()
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Pages, D::Error>
where
D: Deserializer<'de>,
{
Ok(HashMap::<String, Vec<usize>>::deserialize(deserializer)?
.iter()
.map(|(k, v)| {
if v.len() == 2 {
Ok((
k.parse::<usize>().map_err(|_| {
D::Error::custom("Failed to deserialize page index.")
})?,
PublicMemoryPage::from(v),
))
} else {
Err(D::Error::custom(
"Memory page description must be of length 2.",
))
}
})
.collect::<Result<Vec<_>, _>>()
.map_err(|_| D::Error::custom("PublicMemoryPage deserialization failed."))?
.into_iter()
.collect::<Pages>())
}
}

impl CairoPieMemory {
pub fn to_bytes(&self) -> Vec<u8> {
// Missing segment and memory holes can be ignored
Expand Down

0 comments on commit 3f9428e

Please sign in to comment.