Skip to content

Commit

Permalink
Sort builtin segment info upon serialization for Cairo PIE (#1654)
Browse files Browse the repository at this point in the history
* Custom serialization

* Add changelog entry

* Clippy
  • Loading branch information
fmoletta authored Mar 7, 2024
1 parent 136296a commit 7566aa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 7566aa5

Please sign in to comment.