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

Serialize and Deserialize CairoPie struct #1438

Merged
merged 4 commits into from
Sep 6, 2023
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: Implement the Serialize and Deserialize traits for the CairoPie struct [#1438](https://github.com/lambdaclass/cairo-vm/pull/1438)

* fix: Using UINT256_HINT no longer panics when b is greater than 2^256 [#1430](https://github.com/lambdaclass/cairo-vm/pull/1430)

* feat: Added a differential fuzzer for programs with whitelisted hints [#1358](https://github.com/lambdaclass/cairo-vm/pull/1358)
Expand Down
30 changes: 29 additions & 1 deletion vm/src/tests/get_cairo_pie_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::stdlib::{collections::HashMap, prelude::*};
use crate::{
stdlib::{collections::HashMap, prelude::*},
vm::runners::cairo_pie::CairoPie,
};

use felt::felt_str;

Expand Down Expand Up @@ -245,3 +248,28 @@ fn relocate_segments() {
Into::<CairoPieMemory>::into(&vm.segments.memory)
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn serialize_and_deserialize_cairo_pie() {
// Run the program
let program_content = include_bytes!("../../../cairo_programs/relocate_segments.json");
let mut hint_processor = BuiltinHintProcessor::new_empty();
let result = cairo_run(
program_content,
&CairoRunConfig {
layout: "all_cairo",
..Default::default()
},
&mut hint_processor,
);
assert!(result.is_ok());
let (runner, vm) = result.unwrap();
// Obtain the pie
let result = runner.get_cairo_pie(&vm);
assert!(result.is_ok());
let cairo_pie = result.unwrap();
let cairo_pie_serialized = serde_json::to_string(&cairo_pie).unwrap();
let cairo_pie_deserialized: CairoPie = serde_json::from_str(&cairo_pie_serialized).unwrap();
assert_eq!(cairo_pie_deserialized, cairo_pie);
}
3 changes: 2 additions & 1 deletion vm/src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use core::num::NonZeroUsize;
use felt::{Felt252, PRIME_STR};
use serde::{Deserialize, Serialize};

#[cfg(feature = "std")]
use std::path::Path;
Expand Down Expand Up @@ -163,7 +164,7 @@ pub struct Program {
pub(crate) builtins: Vec<BuiltinName>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct StrippedProgram {
pub data: Vec<MaybeRelocatable>,
pub builtins: Vec<BuiltinName>,
Expand Down
14 changes: 8 additions & 6 deletions vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};

use super::cairo_runner::ExecutionResources;
use crate::felt::Felt252;
use crate::stdlib::{collections::HashMap, prelude::*};
use crate::types::program::StrippedProgram;
use crate::types::relocatable::{MaybeRelocatable, Relocatable};

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct SegmentInfo {
pub index: isize,
pub size: usize,
Expand All @@ -24,7 +26,7 @@ impl From<(isize, usize)> for SegmentInfo {
// Allows practical serialization + conversion between CairoPieMemory & Memory
pub type CairoPieMemory = Vec<((usize, usize), MaybeRelocatable)>;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PublicMemoryPage {
pub start: usize,
pub size: usize,
Expand All @@ -34,13 +36,13 @@ pub struct PublicMemoryPage {
pub type Attributes = HashMap<String, Vec<usize>>;
pub type Pages = HashMap<usize, PublicMemoryPage>;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct OutputBuiltinAdditionalData {
pub pages: Pages,
pub attributes: Attributes,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum BuiltinAdditionalData {
// Contains verified addresses as contiguous index, value pairs
Hash(Vec<Relocatable>),
Expand All @@ -50,15 +52,15 @@ pub enum BuiltinAdditionalData {
None,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CairoPie {
pub metadata: CairoPieMetadata,
pub memory: CairoPieMemory,
pub execution_resources: ExecutionResources,
pub additional_data: HashMap<String, BuiltinAdditionalData>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CairoPieMetadata {
pub program: StrippedProgram,
pub program_segment: SegmentInfo,
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::{
use felt::Felt252;
use num_integer::div_rem;
use num_traits::{ToPrimitive, Zero};
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use super::{
builtin_runner::{KeccakBuiltinRunner, PoseidonBuiltinRunner, OUTPUT_BUILTIN_NAME},
Expand Down Expand Up @@ -1292,7 +1292,7 @@ pub struct SegmentInfo {
//* ExecutionResources
//* ----------------------

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct ExecutionResources {
pub n_steps: usize,
pub n_memory_holes: usize,
Expand Down