Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort builtin segment info upon serialization for Cairo PIE #1654

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

* feat: Sort builtin segment info upon serialization for Cairo PIE [#1654](https://github.com/lambdaclass/cairo-vm/pull/1654)

* feat: Fix output serialization for cairo 1 [#1645](https://github.com/lambdaclass/cairo-vm/pull/1645)
* Reverts changes added by #1630
* Extends the serialization of Arrays added by the `print_output` flag to Spans and Dictionaries
Expand Down
31 changes: 30 additions & 1 deletion vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct CairoPieMetadata {
pub execution_segment: SegmentInfo,
pub ret_fp_segment: SegmentInfo,
pub ret_pc_segment: SegmentInfo,
#[serde(serialize_with = "serde_impl::serialize_builtin_segments")]
pub builtin_segments: HashMap<String, SegmentInfo>,
pub extra_segments: Vec<SegmentInfo>,
}
Expand Down Expand Up @@ -132,8 +133,9 @@ impl CairoPie {
mod serde_impl {
use crate::stdlib::collections::HashMap;
use num_traits::Num;
use serde::ser::SerializeMap;

use super::{CairoPieMemory, CAIRO_PIE_VERSION};
use super::{CairoPieMemory, SegmentInfo, CAIRO_PIE_VERSION};
use crate::stdlib::prelude::{String, Vec};
use crate::{
types::relocatable::{MaybeRelocatable, Relocatable},
Expand Down Expand Up @@ -321,6 +323,33 @@ mod serde_impl {

seq_serializer.end()
}

pub fn serialize_builtin_segments<S>(
values: &HashMap<String, SegmentInfo>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map_serializer = serializer.serialize_map(Some(values.len()))?;
const BUILTIN_ORDERED_LIST: &[&str] = &[
"output",
"pedersen",
"range_check",
"ecdsa",
"bitwise",
"ec_op",
"keccak",
"poseidon",
];

for name in BUILTIN_ORDERED_LIST {
if let Some(info) = values.get(*name) {
map_serializer.serialize_entry(name, info)?
}
}
map_serializer.end()
}
}

#[cfg(test)]
Expand Down
Loading