From f7aae3d36815c830456eca2586a5e2e8d36bf575 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Wed, 6 Sep 2023 11:29:49 -0300 Subject: [PATCH] Serialize and Deserialize CairoPie struct (#1438) * Add Serialize and Deserialize trait to CairoPie struct * Add test * Update Changelog.md --- CHANGELOG.md | 2 ++ vm/src/tests/get_cairo_pie_tests.rs | 30 ++++++++++++++++++++++++++++- vm/src/types/program.rs | 3 ++- vm/src/vm/runners/cairo_pie.rs | 14 ++++++++------ vm/src/vm/runners/cairo_runner.rs | 4 ++-- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77facb9564..73364f3374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/vm/src/tests/get_cairo_pie_tests.rs b/vm/src/tests/get_cairo_pie_tests.rs index 9661ca4d4f..257dbb8e08 100644 --- a/vm/src/tests/get_cairo_pie_tests.rs +++ b/vm/src/tests/get_cairo_pie_tests.rs @@ -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; @@ -245,3 +248,28 @@ fn relocate_segments() { Into::::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); +} diff --git a/vm/src/types/program.rs b/vm/src/types/program.rs index 3133c9a973..a88e04aae4 100644 --- a/vm/src/types/program.rs +++ b/vm/src/types/program.rs @@ -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; @@ -163,7 +164,7 @@ pub struct Program { pub(crate) builtins: Vec, } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct StrippedProgram { pub data: Vec, pub builtins: Vec, diff --git a/vm/src/vm/runners/cairo_pie.rs b/vm/src/vm/runners/cairo_pie.rs index 5359785ab5..9dacb46b50 100644 --- a/vm/src/vm/runners/cairo_pie.rs +++ b/vm/src/vm/runners/cairo_pie.rs @@ -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, @@ -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, @@ -34,13 +36,13 @@ pub struct PublicMemoryPage { pub type Attributes = HashMap>; pub type Pages = HashMap; -#[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), @@ -50,7 +52,7 @@ 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, @@ -58,7 +60,7 @@ pub struct CairoPie { pub additional_data: HashMap, } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct CairoPieMetadata { pub program: StrippedProgram, pub program_segment: SegmentInfo, diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index a80d7896f5..6fac3dd69a 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -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}, @@ -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,