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

Fix CairoPie serialization. #1444

Merged
merged 19 commits into from
Sep 18, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add prime field to the program.
  • Loading branch information
azteca1998 committed Sep 14, 2023
commit 622d73c4b0b23a3bc2ce0810252f35608d2df393
1 change: 1 addition & 0 deletions vm/src/tests/cairo_pie_test.rs
Original file line number Diff line number Diff line change
@@ -277,6 +277,7 @@ fn serialize_cairo_pie() {
}),
);

println!("{}", serde_json::to_string_pretty(&cairo_pie).unwrap());
assert_eq!(
serde_json::to_value(&cairo_pie).unwrap(),
serde_json::from_str::<serde_json::Value>(include_str!("cairo_pie_test_output.json"))
3 changes: 2 additions & 1 deletion vm/src/tests/cairo_pie_test_output.json
Original file line number Diff line number Diff line change
@@ -36,7 +36,8 @@
2345108766317314046
],
"builtins": [],
"main": 5
"main": 5,
"prime": 3618502788666131213697322783095070105623107215331596699973092056135872020481
},
"program_segment": {
"index": 0,
1 change: 1 addition & 0 deletions vm/src/types/program.rs
Original file line number Diff line number Diff line change
@@ -327,6 +327,7 @@ impl Program {
.shared_program_data
.main
.ok_or(ProgramError::StrippedProgramNoMain)?,
prime: (),
})
}
}
26 changes: 22 additions & 4 deletions vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
@@ -74,14 +74,18 @@ pub struct CairoPieMetadata {

#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
pub struct StrippedProgram {
#[serde(serialize_with = "program_data_serde::serialize")]
#[serde(serialize_with = "serde_impl::serialize_program_data")]
pub data: Vec<MaybeRelocatable>,
pub builtins: Vec<BuiltinName>,
pub main: usize,

// Dummy field for serialization only.
#[serde(serialize_with = "serde_impl::serialize_prime")]
pub prime: (),
}

mod program_data_serde {
use crate::types::relocatable::MaybeRelocatable;
mod serde_impl {
use crate::{types::relocatable::MaybeRelocatable, utils::CAIRO_PRIME};
use felt::Felt252;
use serde::{ser::SerializeSeq, Serialize, Serializer};

@@ -100,7 +104,10 @@ mod program_data_serde {
}
}

pub fn serialize<S>(values: &[MaybeRelocatable], serializer: S) -> Result<S::Ok, S::Error>
pub fn serialize_program_data<S>(
values: &[MaybeRelocatable],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
@@ -117,4 +124,15 @@ mod program_data_serde {

seq_serializer.end()
}

pub fn serialize_prime<S>(_value: &(), serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[cfg(target_arch = "wasm32")]
use crate::alloc::string::ToString;

// Note: This uses an API intended only for testing.
serde_json::Number::from_string_unchecked(CAIRO_PRIME.to_string()).serialize(serializer)
}
}