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 program and shared data #3

Merged
merged 6 commits into from
Nov 27, 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
Next Next commit
serialize program and shared data
  • Loading branch information
Harsh Bajpai authored and greged93 committed Nov 27, 2023
commit eb2c808089c7610ec1b997f30ed41f135e22711c
3 changes: 2 additions & 1 deletion vm/src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ use crate::vm::vm_core::VirtualMachine;

use super::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use felt::Felt252;
use serde::{Deserialize, Serialize};

#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
@@ -79,7 +80,7 @@ fn get_ids_data(
}

#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct HintReference {
pub offset1: OffsetValue,
pub offset2: OffsetValue,
70 changes: 48 additions & 22 deletions vm/src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ impl Default for ApTracking {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Identifier {
pub pc: Option<usize>,
#[serde(rename(deserialize = "type"))]
#[serde(rename = "type")]
pub type_: Option<String>,
#[serde(default)]
#[serde(deserialize_with = "felt_from_number")]
@@ -225,23 +225,49 @@ fn felt_from_number<'de, D>(deserializer: D) -> Result<Option<Felt252>, D::Error
where
D: Deserializer<'de>,
{
let n = Number::deserialize(deserializer)?;
match Felt252::parse_bytes(n.to_string().as_bytes(), 10) {
Some(x) => Ok(Some(x)),
None => {
// Handle de Number with scientific notation cases
// e.g.: n = Number(1e27)
let felt = deserialize_scientific_notation(n);
if felt.is_some() {
return Ok(felt);
}

Err(de::Error::custom(String::from(
"felt_from_number parse error",
)))

// This value can be of 3 possible types
// Felt252, Number, None
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum Tmp{
Felt252(Option<Number>),
SerializedFelt252(Felt252)
}

let n: Tmp = Tmp::deserialize(deserializer)?;

match n {
Tmp::Felt252(n) => {
match n {
Some(n) => {
match Felt252::parse_bytes(n.to_string().as_bytes(), 10) {
Some(x) => Ok(Some(x)),
None => {
// Handle de Number with scientific notation cases
// e.g.: n = Number(1e27)
let felt = deserialize_scientific_notation(n);
if felt.is_some() {
return Ok(felt);
}

Err(de::Error::custom(String::from(
"felt_from_number parse error",
)))
}
}
},
None => {
Ok(None)
}
}
},
Tmp::SerializedFelt252 (value ) => {
Ok(Some(value))
}
}
}

fn deserialize_scientific_notation(n: Number) -> Option<Felt252> {
match n.as_f64() {
@@ -580,7 +606,7 @@ mod tests {
"attributes": [],
"debug_info": {
"instruction_locations": {}
},
},
"builtins": [],
"data": [
"0x480680017fff8000",
@@ -1107,7 +1133,7 @@ mod tests {
"attributes": [],
"debug_info": {
"instruction_locations": {}
},
},
"builtins": [],
"data": [
],
@@ -1188,10 +1214,10 @@ mod tests {
"start_pc": 402,
"value": "SafeUint256: subtraction overflow"
}
],
],
"debug_info": {
"instruction_locations": {}
},
},
"builtins": [],
"data": [
],
@@ -1245,7 +1271,7 @@ mod tests {
let valid_json = r#"
{
"prime": "0x800000000000011000000000000000000000000000000000000000000000001",
"attributes": [],
"attributes": [],
"debug_info": {
"file_contents": {},
"instruction_locations": {
@@ -1296,7 +1322,7 @@ mod tests {
}
}
}
},
},
"builtins": [],
"data": [
],
@@ -1354,7 +1380,7 @@ mod tests {
let valid_json = r#"
{
"prime": "0x800000000000011000000000000000000000000000000000000000000000001",
"attributes": [],
"attributes": [],
"debug_info": {
"file_contents": {},
"instruction_locations": {
@@ -1401,7 +1427,7 @@ mod tests {
}
}
}
},
},
"builtins": [],
"data": [
],
59 changes: 42 additions & 17 deletions vm/src/types/program.rs
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ use crate::{
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use core::num::NonZeroUsize;
use felt::{Felt252, PRIME_STR};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "std")]
use std::path::Path;
@@ -55,18 +56,18 @@ use arbitrary::{Arbitrary, Unstructured};
// exceptional circumstances, such as when reconstructing a backtrace on execution
// failures.
// Fields in `Program` (other than `SharedProgramData` itself) are used by the main logic.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub(crate) struct SharedProgramData {
pub(crate) data: Vec<MaybeRelocatable>,
pub(crate) hints_collection: HintsCollection,
pub(crate) main: Option<usize>,
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SharedProgramData {
pub data: Vec<MaybeRelocatable>,
pub hints_collection: HintsCollection,
pub main: Option<usize>,
//start and end labels will only be used in proof-mode
pub(crate) start: Option<usize>,
pub(crate) end: Option<usize>,
pub(crate) error_message_attributes: Vec<Attribute>,
pub(crate) instruction_locations: Option<HashMap<usize, InstructionLocation>>,
pub(crate) identifiers: HashMap<String, Identifier>,
pub(crate) reference_manager: Vec<HintReference>,
pub start: Option<usize>,
pub end: Option<usize>,
pub error_message_attributes: Vec<Attribute>,
pub instruction_locations: Option<HashMap<usize, InstructionLocation>>,
pub identifiers: HashMap<String, Identifier>,
pub reference_manager: Vec<HintReference>,
}

#[cfg(all(feature = "arbitrary", feature = "std"))]
@@ -102,8 +103,8 @@ impl<'a> Arbitrary<'a> for SharedProgramData {
}
}

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub(crate) struct HintsCollection {
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HintsCollection {
hints: Vec<HintParams>,
/// This maps a PC to the range of hints in `hints` that correspond to it.
hints_ranges: Vec<HintRange>,
@@ -176,11 +177,35 @@ impl From<&HintsCollection> for BTreeMap<usize, Vec<HintParams>> {
type HintRange = Option<(usize, NonZeroUsize)>;

#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))]
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Program {
pub(crate) shared_program_data: Arc<SharedProgramData>,
pub(crate) constants: HashMap<String, Felt252>,
pub(crate) builtins: Vec<BuiltinName>,
#[serde(
serialize_with = "serialize_shared_program_data",
deserialize_with = "deserialize_shared_program_data"
)]
pub shared_program_data: Arc<SharedProgramData>,
pub constants: HashMap<String, Felt252>,
pub builtins: Vec<BuiltinName>,
}

pub fn serialize_shared_program_data<S>(
shared_program_data: &Arc<SharedProgramData>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
shared_program_data.serialize(serializer)
}

pub fn deserialize_shared_program_data<'de, D>(
deserializer: D,
) -> Result<Arc<SharedProgramData>, D::Error>
where
D: Deserializer<'de>,
{
let shared_program_data = SharedProgramData::deserialize(deserializer)?;
Ok(Arc::new(shared_program_data))
}

impl Program {