From 248af038d6b011a66df8aaeca56b2d84caf8c64f Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Apr 2024 10:28:34 -0300 Subject: [PATCH 01/18] Use enum for LayoutName --- cairo-vm-cli/src/main.rs | 23 +++---------- vm/src/serde/deserialize_program.rs | 53 +++++++++++++++++++++++++++++ vm/src/types/layout.rs | 44 ++++++++++++------------ vm/src/vm/runners/cairo_runner.rs | 10 +++--- 4 files changed, 84 insertions(+), 46 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index ddb97c068a..363b272c1b 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -6,6 +6,7 @@ use cairo_vm::cairo_run::{self, EncodeTraceError}; use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; #[cfg(feature = "with_tracer")] use cairo_vm::serde::deserialize_program::DebugInfo; +use cairo_vm::serde::deserialize_program::LayoutName; use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; use cairo_vm::vm::errors::trace_errors::TraceError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; @@ -17,7 +18,7 @@ use cairo_vm::vm::vm_core::VirtualMachine; use cairo_vm_tracer::error::trace_data_errors::TraceDataError; #[cfg(feature = "with_tracer")] use cairo_vm_tracer::tracer::run_tracer; -use clap::{Parser, ValueHint}; +use clap::{ArgEnum, Parser, ValueHint}; use std::io::{self, Write}; use std::path::{Path, PathBuf}; use thiserror::Error; @@ -42,8 +43,8 @@ struct Args { entrypoint: String, #[structopt(long = "memory_file")] memory_file: Option, - #[clap(long = "layout", default_value = "plain", value_parser=validate_layout)] - layout: String, + #[clap(long = "layout", default_value = "plain", arg_enum)] + layout: LayoutName, #[structopt(long = "proof_mode")] proof_mode: bool, #[structopt(long = "secure_run")] @@ -69,22 +70,6 @@ struct Args { tracer: bool, } -fn validate_layout(value: &str) -> Result { - match value { - "plain" - | "small" - | "dex" - | "recursive" - | "starknet" - | "starknet_with_keccak" - | "recursive_large_output" - | "all_cairo" - | "all_solidity" - | "dynamic" => Ok(value.to_string()), - _ => Err(format!("{value} is not a valid layout")), - } -} - #[derive(Debug, Error)] enum Error { #[error("Invalid arguments")] diff --git a/vm/src/serde/deserialize_program.rs b/vm/src/serde/deserialize_program.rs index b897223f87..3954fb7234 100644 --- a/vm/src/serde/deserialize_program.rs +++ b/vm/src/serde/deserialize_program.rs @@ -6,6 +6,8 @@ //! To generate a [`Program`] from a JSON string, see [`Program::from_bytes()`]. //! To do the same from a JSON file, see [`Program::from_file()`]. +use core::fmt::Display; + use crate::{ stdlib::{ collections::{BTreeMap, HashMap}, @@ -43,6 +45,57 @@ use serde_json::Number; #[cfg(all(feature = "arbitrary", feature = "std"))] use arbitrary::{self, Arbitrary, Unstructured}; +// This enum is used to deserialize program layouts into &str and catch non-valid names +#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] +#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] +#[allow(non_camel_case_types)] +pub enum LayoutName { + plain, + small, + dex, + recursive, + starknet, + starknet_with_keccak, + recursive_large_output, + all_solidity, + all_cairo, + dynamic, +} + +impl LayoutName { + pub fn to_str(&self) -> &'static str { + match self { + LayoutName::plain => "plain", + LayoutName::small => "small", + LayoutName::dex => "dex", + LayoutName::recursive => "recursive", + LayoutName::starknet => "starknet", + LayoutName::starknet_with_keccak => "starknet_with_keccak", + LayoutName::recursive_large_output => "recursive_large_output", + LayoutName::all_solidity => "all_solidity", + LayoutName::all_cairo => "all_cairo", + LayoutName::dynamic => "all_cairo", + } + } +} + +impl Display for LayoutName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + LayoutName::plain => "plain".fmt(f), + LayoutName::small => "small".fmt(f), + LayoutName::dex => "dex".fmt(f), + LayoutName::recursive => "recursive".fmt(f), + LayoutName::starknet => "starknet".fmt(f), + LayoutName::starknet_with_keccak => "starknet_with_keccak".fmt(f), + LayoutName::recursive_large_output => "recursive_large_output".fmt(f), + LayoutName::all_solidity => "all_solidity".fmt(f), + LayoutName::all_cairo => "all_cairo".fmt(f), + LayoutName::dynamic => "all_cairo".fmt(f), + } + } +} + // This enum is used to deserialize program builtins into &str and catch non-valid names #[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index f9a2f8376f..855fdeca06 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,4 +1,4 @@ -use crate::stdlib::prelude::*; +use crate::serde::deserialize_program::LayoutName; use super::instance_definitions::{ builtins_instance_def::BuiltinsInstanceDef, cpu_instance_def::CpuInstanceDef, @@ -9,7 +9,7 @@ use serde::Serialize; #[derive(Serialize, Debug)] pub struct CairoLayout { - pub(crate) _name: String, + pub(crate) name: LayoutName, pub(crate) _cpu_component_step: u32, pub(crate) rc_units: u32, pub(crate) builtins: BuiltinsInstanceDef, @@ -23,7 +23,7 @@ pub struct CairoLayout { impl CairoLayout { pub(crate) fn plain_instance() -> CairoLayout { CairoLayout { - _name: String::from("plain"), + name: LayoutName::plain, _cpu_component_step: 1, rc_units: 16, builtins: BuiltinsInstanceDef::plain(), @@ -37,7 +37,7 @@ impl CairoLayout { pub(crate) fn small_instance() -> CairoLayout { CairoLayout { - _name: String::from("small"), + name: LayoutName::small, _cpu_component_step: 1, rc_units: 16, builtins: BuiltinsInstanceDef::small(), @@ -51,7 +51,7 @@ impl CairoLayout { pub(crate) fn dex_instance() -> CairoLayout { CairoLayout { - _name: String::from("dex"), + name: LayoutName::dex, _cpu_component_step: 1, rc_units: 4, builtins: BuiltinsInstanceDef::dex(), @@ -65,7 +65,7 @@ impl CairoLayout { pub(crate) fn recursive_instance() -> CairoLayout { CairoLayout { - _name: String::from("recursive"), + name: LayoutName::recursive, _cpu_component_step: 1, rc_units: 4, builtins: BuiltinsInstanceDef::recursive(), @@ -79,7 +79,7 @@ impl CairoLayout { pub(crate) fn starknet_instance() -> CairoLayout { CairoLayout { - _name: String::from("starknet"), + name: LayoutName::starknet, _cpu_component_step: 1, rc_units: 4, builtins: BuiltinsInstanceDef::starknet(), @@ -93,7 +93,7 @@ impl CairoLayout { pub(crate) fn starknet_with_keccak_instance() -> CairoLayout { CairoLayout { - _name: String::from("starknet_with_keccak"), + name: LayoutName::starknet_with_keccak, _cpu_component_step: 1, rc_units: 4, builtins: BuiltinsInstanceDef::starknet_with_keccak(), @@ -107,7 +107,7 @@ impl CairoLayout { pub(crate) fn recursive_large_output_instance() -> CairoLayout { CairoLayout { - _name: String::from("recursive_large_output"), + name: LayoutName::recursive_large_output, _cpu_component_step: 1, rc_units: 4, builtins: BuiltinsInstanceDef::recursive_large_output(), @@ -121,7 +121,7 @@ impl CairoLayout { pub(crate) fn all_cairo_instance() -> CairoLayout { CairoLayout { - _name: String::from("all_cairo"), + name: LayoutName::all_cairo, _cpu_component_step: 1, rc_units: 4, builtins: BuiltinsInstanceDef::all_cairo(), @@ -135,7 +135,7 @@ impl CairoLayout { pub(crate) fn all_solidity_instance() -> CairoLayout { CairoLayout { - _name: String::from("all_solidity"), + name: LayoutName::all_solidity, _cpu_component_step: 1, rc_units: 8, builtins: BuiltinsInstanceDef::all_solidity(), @@ -149,7 +149,7 @@ impl CairoLayout { pub(crate) fn dynamic_instance() -> CairoLayout { CairoLayout { - _name: String::from("dynamic"), + name: LayoutName::dynamic, _cpu_component_step: 1, rc_units: 16, builtins: BuiltinsInstanceDef::dynamic(), @@ -174,7 +174,7 @@ mod tests { fn get_plain_instance() { let layout = CairoLayout::plain_instance(); let builtins = BuiltinsInstanceDef::plain(); - assert_eq!(&layout._name, "plain"); + assert_eq!(layout.name, LayoutName::plain); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 16); assert_eq!(layout.builtins, builtins); @@ -190,7 +190,7 @@ mod tests { fn get_small_instance() { let layout = CairoLayout::small_instance(); let builtins = BuiltinsInstanceDef::small(); - assert_eq!(&layout._name, "small"); + assert_eq!(layout.name, LayoutName::small); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 16); assert_eq!(layout.builtins, builtins); @@ -206,7 +206,7 @@ mod tests { fn get_dex_instance() { let layout = CairoLayout::dex_instance(); let builtins = BuiltinsInstanceDef::dex(); - assert_eq!(&layout._name, "dex"); + assert_eq!(layout.name, LayoutName::dex); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); @@ -221,7 +221,7 @@ mod tests { fn get_recursive_instance() { let layout = CairoLayout::recursive_instance(); let builtins = BuiltinsInstanceDef::recursive(); - assert_eq!(&layout._name, "recursive"); + assert_eq!(layout.name, LayoutName::recursive); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); @@ -239,7 +239,7 @@ mod tests { fn get_starknet_instance() { let layout = CairoLayout::starknet_instance(); let builtins = BuiltinsInstanceDef::starknet(); - assert_eq!(&layout._name, "starknet"); + assert_eq!(layout.name, LayoutName::starknet); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); @@ -257,7 +257,7 @@ mod tests { fn get_starknet_with_keccak_instance() { let layout = CairoLayout::starknet_with_keccak_instance(); let builtins = BuiltinsInstanceDef::starknet_with_keccak(); - assert_eq!(&layout._name, "starknet_with_keccak"); + assert_eq!(layout.name, LayoutName::starknet_with_keccak); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); @@ -275,7 +275,7 @@ mod tests { fn get_recursive_large_output_instance() { let layout = CairoLayout::recursive_large_output_instance(); let builtins = BuiltinsInstanceDef::recursive_large_output(); - assert_eq!(&layout._name, "recursive_large_output"); + assert_eq!(layout.name, LayoutName::recursive_large_output); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); @@ -293,7 +293,7 @@ mod tests { fn get_all_cairo_instance() { let layout = CairoLayout::all_cairo_instance(); let builtins = BuiltinsInstanceDef::all_cairo(); - assert_eq!(&layout._name, "all_cairo"); + assert_eq!(layout.name, LayoutName::all_cairo); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); @@ -311,7 +311,7 @@ mod tests { fn get_all_solidity_instance() { let layout = CairoLayout::all_solidity_instance(); let builtins = BuiltinsInstanceDef::all_solidity(); - assert_eq!(&layout._name, "all_solidity"); + assert_eq!(layout.name, LayoutName::all_solidity); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 8); assert_eq!(layout.builtins, builtins); @@ -329,7 +329,7 @@ mod tests { fn get_dynamic_instance() { let layout = CairoLayout::dynamic_instance(); let builtins = BuiltinsInstanceDef::dynamic(); - assert_eq!(&layout._name, "dynamic"); + assert_eq!(layout.name, LayoutName::dynamic); assert_eq!(layout._cpu_component_step, 1); assert_eq!(layout.rc_units, 16); assert_eq!(layout.builtins, builtins); diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index f405e672af..736093136d 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,6 +1,7 @@ use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, + serde::deserialize_program::LayoutName, stdlib::{ any::Any, collections::{HashMap, HashSet}, @@ -360,7 +361,7 @@ impl CairoRunner { if !program_builtins.is_empty() && !allow_missing_builtins { return Err(RunnerError::NoBuiltinForInstance(Box::new(( program_builtins.iter().map(|n| n.name()).collect(), - self.layout._name.clone(), + self.layout.name.to_string(), )))); } @@ -1459,15 +1460,14 @@ impl CairoRunner { &self, vm: &VirtualMachine, ) -> Result { - let layout_name = self.get_layout()._name.as_str(); - let dyn_layout = match layout_name { - "dynamic" => Some(self.get_layout()), + let dyn_layout = match self.layout.name { + LayoutName::dynamic => Some(self.get_layout()), _ => None, }; PublicInput::new( &self.relocated_memory, - layout_name, + self.layout.name.to_str(), dyn_layout, &vm.get_public_memory_addresses()?, self.get_memory_segment_addresses(vm)?, From 9f16091f3dd81f8e8390739cddf5b62e5cc2e628 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Apr 2024 17:39:06 -0300 Subject: [PATCH 02/18] Move LayoutName to its own module + fix tests --- cairo-vm-cli/src/main.rs | 4 +- cairo1-run/src/cairo_run.rs | 8 +- cairo1-run/src/main.rs | 24 ++---- examples/hyper_threading/src/main.rs | 4 +- examples/wasm-demo/src/lib.rs | 3 +- vm/src/air_private_input.rs | 3 +- vm/src/air_public_input.rs | 4 +- vm/src/cairo_run.rs | 27 +++--- vm/src/serde/deserialize_program.rs | 53 ------------ vm/src/tests/cairo_pie_test.rs | 8 +- vm/src/types/layout.rs | 2 +- vm/src/types/layout_name.rs | 55 ++++++++++++ vm/src/types/mod.rs | 1 + vm/src/utils.rs | 7 +- vm/src/vm/errors/vm_exception.rs | 19 +++-- vm/src/vm/runners/builtin_runner/modulo.rs | 3 +- vm/src/vm/runners/cairo_runner.rs | 97 +++++++++++----------- 17 files changed, 163 insertions(+), 159 deletions(-) create mode 100644 vm/src/types/layout_name.rs diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 363b272c1b..021095134f 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -6,7 +6,7 @@ use cairo_vm::cairo_run::{self, EncodeTraceError}; use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; #[cfg(feature = "with_tracer")] use cairo_vm::serde::deserialize_program::DebugInfo; -use cairo_vm::serde::deserialize_program::LayoutName; +use cairo_vm::types::layout_name::LayoutName; use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; use cairo_vm::vm::errors::trace_errors::TraceError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; @@ -158,7 +158,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { entrypoint: &args.entrypoint, trace_enabled, relocate_mem: args.memory_file.is_some() || args.air_public_input.is_some(), - layout: &args.layout, + layout: args.layout, proof_mode: args.proof_mode, secure_run: args.secure_run, allow_missing_builtins: args.allow_missing_builtins, diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 8477eb7d88..09a9034eda 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -32,7 +32,7 @@ use cairo_vm::{ serde::deserialize_program::{ ApTracking, BuiltinName, FlowTrackingData, HintParams, ReferenceManager, }, - types::{program::Program, relocatable::MaybeRelocatable}, + types::{layout_name::LayoutName, program::Program, relocatable::MaybeRelocatable}, vm::{ errors::{runner_errors::RunnerError, vm_errors::VirtualMachineError}, runners::{ @@ -59,7 +59,7 @@ pub struct Cairo1RunConfig<'a> { pub serialize_output: bool, pub trace_enabled: bool, pub relocate_mem: bool, - pub layout: &'a str, + pub layout: LayoutName, pub proof_mode: bool, // Should be true if either air_public_input or cairo_pie_output are needed // Sets builtins stop_ptr by calling `final_stack` on each builtin @@ -75,7 +75,7 @@ impl Default for Cairo1RunConfig<'_> { serialize_output: false, trace_enabled: false, relocate_mem: false, - layout: "plain", + layout: LayoutName::plain, proof_mode: false, finalize_builtins: false, append_return_values: false, @@ -1170,7 +1170,7 @@ mod tests { // Set proof_mode let cairo_run_config = Cairo1RunConfig { proof_mode, - layout: "all_cairo", + layout: LayoutName::all_cairo, append_return_values: !proof_mode, // This is so we can test appending return values when not running in proof_mode finalize_builtins: true, ..Default::default() diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index e8aeabadb6..4a549a6686 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -6,6 +6,7 @@ use cairo_run::Cairo1RunConfig; use cairo_vm::{ air_public_input::PublicInputError, cairo_run::EncodeTraceError, + serde::deserialize_program::LayoutName, types::errors::program_errors::ProgramError, vm::errors::{ memory_errors::MemoryError, runner_errors::RunnerError, trace_errors::TraceError, @@ -13,7 +14,7 @@ use cairo_vm::{ }, Felt252, }; -use clap::{Parser, ValueHint}; +use clap::{Parser, ValueEnum, ValueHint}; use itertools::Itertools; use std::{ io::{self, Write}, @@ -32,8 +33,8 @@ struct Args { trace_file: Option, #[structopt(long = "memory_file")] memory_file: Option, - #[clap(long = "layout", default_value = "plain", value_parser=validate_layout)] - layout: String, + #[clap(long = "layout", default_value = "plain", value_enum)] + layout: LayoutName, #[clap(long = "proof_mode", value_parser)] proof_mode: bool, #[clap(long = "air_public_input", requires = "proof_mode")] @@ -109,21 +110,6 @@ fn process_args(value: &str) -> Result { Ok(FuncArgs(args)) } -fn validate_layout(value: &str) -> Result { - match value { - "plain" - | "small" - | "dex" - | "starknet" - | "starknet_with_keccak" - | "recursive_large_output" - | "all_cairo" - | "all_solidity" - | "dynamic" => Ok(value.to_string()), - _ => Err(format!("{value} is not a valid layout")), - } -} - #[derive(Debug, Error)] pub enum Error { #[error("Invalid arguments")] @@ -213,7 +199,7 @@ fn run(args: impl Iterator) -> Result, Error> { proof_mode: args.proof_mode, serialize_output: args.print_output, relocate_mem: args.memory_file.is_some() || args.air_public_input.is_some(), - layout: &args.layout, + layout: args.layout, trace_enabled: args.trace_file.is_some() || args.air_public_input.is_some(), args: &args.args.0, finalize_builtins: args.air_private_input.is_some() || args.cairo_pie_output.is_some(), diff --git a/examples/hyper_threading/src/main.rs b/examples/hyper_threading/src/main.rs index eb6fbf47c9..12144fab9d 100644 --- a/examples/hyper_threading/src/main.rs +++ b/examples/hyper_threading/src/main.rs @@ -1,7 +1,7 @@ use cairo_vm::{ cairo_run::{cairo_run_program, CairoRunConfig}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, - types::program::Program, + types::{layout_name::LayoutName, program::Program}, }; use rayon::iter::{IntoParallelIterator, ParallelIterator}; use std::path::Path; @@ -51,7 +51,7 @@ fn main() { entrypoint: "main", trace_enabled: false, relocate_mem: false, - layout: "all_cairo", + layout: LayoutName::all_cairo, proof_mode: true, secure_run: Some(false), ..Default::default() diff --git a/examples/wasm-demo/src/lib.rs b/examples/wasm-demo/src/lib.rs index 60ffcbe59b..dc5b197430 100644 --- a/examples/wasm-demo/src/lib.rs +++ b/examples/wasm-demo/src/lib.rs @@ -3,6 +3,7 @@ mod utils; use cairo_vm::{ cairo_run::{cairo_run, CairoRunConfig}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, + types::layout_name::LayoutName, }; use wasm_bindgen::prelude::*; @@ -32,7 +33,7 @@ pub fn run_cairo_program() -> Result { let mut hint_executor = BuiltinHintProcessor::new_empty(); let cairo_run_config = CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, relocate_mem: true, trace_enabled: true, ..Default::default() diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index d1c5f1a8ca..1dbb96bb8f 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -208,6 +208,7 @@ impl AirPrivateInputSerializable { #[cfg(test)] mod tests { + use crate::types::layout_name::LayoutName; #[cfg(feature = "std")] use { super::*, @@ -299,7 +300,7 @@ mod tests { proof_mode: true, relocate_mem: true, trace_enabled: true, - layout: "small", + layout: LayoutName::small, ..Default::default() }; let (runner, vm) = crate::cairo_run::cairo_run(include_bytes!("../../cairo_programs/proof_programs/fibonacci.json"), &config, &mut crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor::new_empty()).unwrap(); diff --git a/vm/src/air_public_input.rs b/vm/src/air_public_input.rs index 3a2bf8bc37..c8f48e3f6f 100644 --- a/vm/src/air_public_input.rs +++ b/vm/src/air_public_input.rs @@ -192,11 +192,13 @@ mod tests { #[case(include_bytes!("../../cairo_programs/proof_programs/pedersen_test.json"))] #[case(include_bytes!("../../cairo_programs/proof_programs/ec_op.json"))] fn serialize_and_deserialize_air_public_input(#[case] program_content: &[u8]) { + use crate::types::layout_name::LayoutName; + let config = crate::cairo_run::CairoRunConfig { proof_mode: true, relocate_mem: true, trace_enabled: true, - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }; let (runner, vm) = crate::cairo_run::cairo_run(program_content, &config, &mut crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor::new_empty()).unwrap(); diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 07c3a29c77..931dc8896a 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -1,5 +1,6 @@ use crate::{ hint_processor::hint_processor_definition::HintProcessor, + types::layout_name::LayoutName, types::program::Program, vm::{ errors::{cairo_run_errors::CairoRunError, vm_exception::VmException}, @@ -24,7 +25,7 @@ pub struct CairoRunConfig<'a> { pub trace_enabled: bool, pub relocate_mem: bool, #[cfg_attr(feature = "arbitrary", arbitrary(with = arbitrary_layout))] - pub layout: &'a str, + pub layout: LayoutName, pub proof_mode: bool, pub secure_run: Option, pub disable_trace_padding: bool, @@ -32,19 +33,19 @@ pub struct CairoRunConfig<'a> { } #[cfg(feature = "arbitrary")] -fn arbitrary_layout<'a>(u: &mut Unstructured) -> arbitrary::Result<&'a str> { +fn arbitrary_layout<'a>(u: &mut Unstructured) -> arbitrary::Result { let layouts = [ - "plain", - "small", - "dex", - "starknet", - "starknet_with_keccak", - "recursive_large_output", - "all_cairo", - "all_solidity", - "dynamic", + LayoutName::plain, + LayoutName::small, + LayoutName::dex, + LayoutName::starknet, + LayoutName::starknet_with_keccak, + LayoutName::recursive_large_output, + LayoutName::all_cairo, + LayoutName::all_solidity, + LayoutName::dynamic, ]; - Ok(u.choose(&layouts)?) + Ok(*u.choose(&layouts)?) } impl<'a> Default for CairoRunConfig<'a> { @@ -53,7 +54,7 @@ impl<'a> Default for CairoRunConfig<'a> { entrypoint: "main", trace_enabled: false, relocate_mem: false, - layout: "plain", + layout: LayoutName::plain, proof_mode: false, secure_run: None, disable_trace_padding: false, diff --git a/vm/src/serde/deserialize_program.rs b/vm/src/serde/deserialize_program.rs index 3954fb7234..b897223f87 100644 --- a/vm/src/serde/deserialize_program.rs +++ b/vm/src/serde/deserialize_program.rs @@ -6,8 +6,6 @@ //! To generate a [`Program`] from a JSON string, see [`Program::from_bytes()`]. //! To do the same from a JSON file, see [`Program::from_file()`]. -use core::fmt::Display; - use crate::{ stdlib::{ collections::{BTreeMap, HashMap}, @@ -45,57 +43,6 @@ use serde_json::Number; #[cfg(all(feature = "arbitrary", feature = "std"))] use arbitrary::{self, Arbitrary, Unstructured}; -// This enum is used to deserialize program layouts into &str and catch non-valid names -#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] -#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] -#[allow(non_camel_case_types)] -pub enum LayoutName { - plain, - small, - dex, - recursive, - starknet, - starknet_with_keccak, - recursive_large_output, - all_solidity, - all_cairo, - dynamic, -} - -impl LayoutName { - pub fn to_str(&self) -> &'static str { - match self { - LayoutName::plain => "plain", - LayoutName::small => "small", - LayoutName::dex => "dex", - LayoutName::recursive => "recursive", - LayoutName::starknet => "starknet", - LayoutName::starknet_with_keccak => "starknet_with_keccak", - LayoutName::recursive_large_output => "recursive_large_output", - LayoutName::all_solidity => "all_solidity", - LayoutName::all_cairo => "all_cairo", - LayoutName::dynamic => "all_cairo", - } - } -} - -impl Display for LayoutName { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - LayoutName::plain => "plain".fmt(f), - LayoutName::small => "small".fmt(f), - LayoutName::dex => "dex".fmt(f), - LayoutName::recursive => "recursive".fmt(f), - LayoutName::starknet => "starknet".fmt(f), - LayoutName::starknet_with_keccak => "starknet_with_keccak".fmt(f), - LayoutName::recursive_large_output => "recursive_large_output".fmt(f), - LayoutName::all_solidity => "all_solidity".fmt(f), - LayoutName::all_cairo => "all_cairo".fmt(f), - LayoutName::dynamic => "all_cairo".fmt(f), - } - } -} - // This enum is used to deserialize program builtins into &str and catch non-valid names #[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] diff --git a/vm/src/tests/cairo_pie_test.rs b/vm/src/tests/cairo_pie_test.rs index 933485fb73..6e3c5de4e4 100644 --- a/vm/src/tests/cairo_pie_test.rs +++ b/vm/src/tests/cairo_pie_test.rs @@ -1,4 +1,4 @@ -use crate::felt_str; +use crate::{felt_str, types::layout_name::LayoutName}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -41,7 +41,7 @@ fn pedersen_test() { let result = cairo_run( program_content, &CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }, &mut hint_processor, @@ -126,7 +126,7 @@ fn common_signature() { let result = cairo_run( program_content, &CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }, &mut hint_processor, @@ -201,7 +201,7 @@ fn relocate_segments() { let result = cairo_run( program_content, &CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }, &mut hint_processor, diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 855fdeca06..7b4ac8bf86 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,4 +1,4 @@ -use crate::serde::deserialize_program::LayoutName; +use crate::types::layout_name::LayoutName; use super::instance_definitions::{ builtins_instance_def::BuiltinsInstanceDef, cpu_instance_def::CpuInstanceDef, diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs new file mode 100644 index 0000000000..96f066cadd --- /dev/null +++ b/vm/src/types/layout_name.rs @@ -0,0 +1,55 @@ +#[cfg(all(feature = "arbitrary", feature = "std"))] +use arbitrary::{self, Arbitrary}; +use core::fmt::{self, Display}; +use serde::{Deserialize, Serialize}; + +// This enum is used to deserialize program layouts into &str and catch non-valid names +#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] +#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] +#[allow(non_camel_case_types)] +pub enum LayoutName { + plain, + small, + dex, + recursive, + starknet, + starknet_with_keccak, + recursive_large_output, + all_solidity, + all_cairo, + dynamic, +} + +impl LayoutName { + pub fn to_str(&self) -> &'static str { + match self { + LayoutName::plain => "plain", + LayoutName::small => "small", + LayoutName::dex => "dex", + LayoutName::recursive => "recursive", + LayoutName::starknet => "starknet", + LayoutName::starknet_with_keccak => "starknet_with_keccak", + LayoutName::recursive_large_output => "recursive_large_output", + LayoutName::all_solidity => "all_solidity", + LayoutName::all_cairo => "all_cairo", + LayoutName::dynamic => "all_cairo", + } + } +} + +impl Display for LayoutName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + LayoutName::plain => "plain".fmt(f), + LayoutName::small => "small".fmt(f), + LayoutName::dex => "dex".fmt(f), + LayoutName::recursive => "recursive".fmt(f), + LayoutName::starknet => "starknet".fmt(f), + LayoutName::starknet_with_keccak => "starknet_with_keccak".fmt(f), + LayoutName::recursive_large_output => "recursive_large_output".fmt(f), + LayoutName::all_solidity => "all_solidity".fmt(f), + LayoutName::all_cairo => "all_cairo".fmt(f), + LayoutName::dynamic => "all_cairo".fmt(f), + } + } +} diff --git a/vm/src/types/mod.rs b/vm/src/types/mod.rs index 4e7ce2f77e..c40e417c22 100644 --- a/vm/src/types/mod.rs +++ b/vm/src/types/mod.rs @@ -3,5 +3,6 @@ pub mod exec_scope; pub mod instance_definitions; pub mod instruction; pub mod layout; +pub mod layout_name; pub mod program; pub mod relocatable; diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 4ec902dcfb..c2ed917d04 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -249,7 +249,12 @@ pub mod test_utils { macro_rules! cairo_runner { ($program:expr) => { - CairoRunner::new(&$program, "all_cairo", false).unwrap() + CairoRunner::new( + &$program, + crate::types::layout_name::LayoutName::all_cairo, + false, + ) + .unwrap() }; ($program:expr, $layout:expr) => { CairoRunner::new(&$program, $layout, false).unwrap() diff --git a/vm/src/vm/errors/vm_exception.rs b/vm/src/vm/errors/vm_exception.rs index f8ab67088c..05077525d5 100644 --- a/vm/src/vm/errors/vm_exception.rs +++ b/vm/src/vm/errors/vm_exception.rs @@ -309,6 +309,7 @@ impl Location { #[cfg(test)] mod test { use crate::stdlib::{boxed::Box, collections::HashMap}; + use crate::types::layout_name::LayoutName; use assert_matches::assert_matches; #[cfg(feature = "std")] use std::path::Path; @@ -656,7 +657,7 @@ mod test { .expect("Call to `Program::from_file()` failed."); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -670,7 +671,7 @@ mod test { let expected_traceback = String::from("Cairo traceback (most recent call last):\ncairo_programs/bad_programs/bad_dict_update.cairo:10:5: (pc=0:34)\n"); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -708,7 +709,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60) "; let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -867,7 +868,7 @@ cairo_programs/bad_programs/bad_range_check.cairo:11:5: (pc=0:6) .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -912,7 +913,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60) .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -951,7 +952,7 @@ cairo_programs/bad_programs/ec_recover_product_mod_m_zero.cairo:11:5: (pc=0:18) .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -990,7 +991,7 @@ cairo_programs/bad_programs/ec_recover_div_mod_n_packed_n_zero.cairo:11:5: (pc=0 .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -1029,7 +1030,7 @@ cairo_programs/bad_programs/uint512_unsigned_div_rem_div_is_zero.cairo:15:2: (pc .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -1066,7 +1067,7 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 924aa65a28..6d0878e878 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -699,6 +699,7 @@ mod tests { use crate::{ air_private_input::{ModInput, ModInputInstance, ModInputMemoryVars, PrivateInput}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, + types::layout_name::LayoutName, utils::test_utils::Program, vm::runners::{ builtin_runner::{BuiltinRunner, ADD_MOD_BUILTIN_NAME, MUL_MOD_BUILTIN_NAME}, @@ -713,7 +714,7 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let program = Program::from_bytes(program_data, Some("main")).unwrap(); - let mut runner = CairoRunner::new(&program, "all_cairo", true).unwrap(); + let mut runner = CairoRunner::new(&program, LayoutName::all_cairo, true).unwrap(); let mut vm = VirtualMachine::new(false); let end = runner.initialize(&mut vm, false).unwrap(); diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 736093136d..a76bb7457d 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,7 +1,6 @@ use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, - serde::deserialize_program::LayoutName, stdlib::{ any::Any, collections::{HashMap, HashSet}, @@ -9,6 +8,7 @@ use crate::{ prelude::*, }, types::instance_definitions::keccak_instance_def::KeccakInstanceDef, + types::layout_name::LayoutName, vm::{ runners::builtin_runner::SegmentArenaBuiltinRunner, trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry}, @@ -177,25 +177,20 @@ pub enum RunnerMode { impl CairoRunner { pub fn new_v2( program: &Program, - layout: &str, + layout: LayoutName, mode: RunnerMode, ) -> Result { let cairo_layout = match layout { - "plain" => CairoLayout::plain_instance(), - "small" => CairoLayout::small_instance(), - "dex" => CairoLayout::dex_instance(), - "recursive" => CairoLayout::recursive_instance(), - "starknet" => CairoLayout::starknet_instance(), - "starknet_with_keccak" => CairoLayout::starknet_with_keccak_instance(), - "recursive_large_output" => CairoLayout::recursive_large_output_instance(), - "all_cairo" => CairoLayout::all_cairo_instance(), - "all_solidity" => CairoLayout::all_solidity_instance(), - "dynamic" => CairoLayout::dynamic_instance(), - name => { - return Err(RunnerError::InvalidLayoutName( - name.to_string().into_boxed_str(), - )) - } + LayoutName::plain => CairoLayout::plain_instance(), + LayoutName::small => CairoLayout::small_instance(), + LayoutName::dex => CairoLayout::dex_instance(), + LayoutName::recursive => CairoLayout::recursive_instance(), + LayoutName::starknet => CairoLayout::starknet_instance(), + LayoutName::starknet_with_keccak => CairoLayout::starknet_with_keccak_instance(), + LayoutName::recursive_large_output => CairoLayout::recursive_large_output_instance(), + LayoutName::all_cairo => CairoLayout::all_cairo_instance(), + LayoutName::all_solidity => CairoLayout::all_solidity_instance(), + LayoutName::dynamic => CairoLayout::dynamic_instance(), }; Ok(CairoRunner { program: program.clone(), @@ -224,7 +219,7 @@ impl CairoRunner { pub fn new( program: &Program, - layout: &str, + layout: LayoutName, proof_mode: bool, ) -> Result { if proof_mode { @@ -1686,7 +1681,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initialize_builtins_with_disordered_builtins() { let program = program![BuiltinName::range_check, BuiltinName::output]; - let cairo_runner = cairo_runner!(program, "plain"); + let cairo_runner = cairo_runner!(program, LayoutName::plain); let mut vm = vm!(); assert!(cairo_runner.initialize_builtins(&mut vm, false).is_err()); } @@ -1695,7 +1690,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initialize_builtins_missing_builtins_no_allow_missing() { let program = program![BuiltinName::output, BuiltinName::ecdsa]; - let cairo_runner = cairo_runner!(program, "plain"); + let cairo_runner = cairo_runner!(program, LayoutName::plain); let mut vm = vm!(); assert_matches!( cairo_runner.initialize_builtins(&mut vm, false), @@ -3845,7 +3840,7 @@ mod tests { .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, true); let mut vm = vm!(true); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -3914,7 +3909,7 @@ mod tests { entrypoint: "main", trace_enabled: true, relocate_mem: false, - layout: "all_cairo", + layout: LayoutName::all_cairo, proof_mode: false, secure_run: Some(false), ..Default::default() @@ -3932,7 +3927,7 @@ mod tests { entrypoint: "main", trace_enabled: false, relocate_mem: false, - layout: "all_cairo", + layout: LayoutName::all_cairo, proof_mode: false, secure_run: Some(false), ..Default::default() @@ -4051,7 +4046,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn finalize_segments_run_ended_emptyproof_mode() { let program = program!(); - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -4068,7 +4063,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -4102,7 +4097,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4)]; //Program data len = 4 - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 1))); cairo_runner.execution_public_memory = Some(vec![1_usize, 3_usize, 5_usize, 4_usize]); @@ -4207,7 +4202,7 @@ mod tests { fn check_range_check_usage_without_builtins() { let program = program!(); - let cairo_runner = cairo_runner!(program, "plain"); + let cairo_runner = cairo_runner!(program, LayoutName::plain); let mut vm = vm!(); vm.builtin_runners = vec![]; vm.current_step = 10000; @@ -4529,12 +4524,12 @@ mod tests { fn initialize_segments_incorrect_layout_plain_one_builtin() { let program = program![BuiltinName::output]; let mut vm = vm!(); - let cairo_runner = cairo_runner!(program, "plain"); + let cairo_runner = cairo_runner!(program, LayoutName::plain); assert_eq!( cairo_runner.initialize_builtins(&mut vm, false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::output.name()]), - String::from("plain") + String::from(LayoutName::plain) )))) ); } @@ -4544,12 +4539,12 @@ mod tests { fn initialize_segments_incorrect_layout_plain_two_builtins() { let program = program![BuiltinName::output, BuiltinName::pedersen]; let mut vm = vm!(); - let cairo_runner = cairo_runner!(program, "plain"); + let cairo_runner = cairo_runner!(program, LayoutName::plain); assert_eq!( cairo_runner.initialize_builtins(&mut vm, false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::output.name(), HASH_BUILTIN_NAME]), - String::from("plain") + String::from(LayoutName::plain) )))) ); } @@ -4728,7 +4723,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -4752,7 +4747,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = false; @@ -4770,7 +4765,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -4789,7 +4784,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "all_cairo", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -4820,7 +4815,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "all_cairo", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -4851,7 +4846,7 @@ mod tests { Arc::get_mut(&mut program.shared_program_data).unwrap().data = vec_data![(1), (2), (3), (4), (5), (6), (7), (8)]; //Program data len = 8 - let mut cairo_runner = cairo_runner!(program, "all_cairo", true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, true); cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; @@ -5363,14 +5358,15 @@ mod tests { #[test] fn get_cairo_pie_no_program_base() { - let runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let runner = CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let vm = vm!(); assert_eq!(runner.get_cairo_pie(&vm), Err(RunnerError::NoProgBase)) } #[test] fn get_cairo_pie_no_execution_base() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); assert_eq!(runner.get_cairo_pie(&vm), Err(RunnerError::NoExecBase)) @@ -5378,7 +5374,8 @@ mod tests { #[test] fn get_cairo_pie_no_segment_sizes() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); @@ -5398,7 +5395,8 @@ mod tests { #[test] fn get_cairo_pie_ret_pc_segment_size_not_zero() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); @@ -5420,7 +5418,8 @@ mod tests { #[test] fn get_cairo_pie_program_base_offset_not_zero() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 1))); runner.execution_base = Some(Relocatable::from((1, 0))); @@ -5442,7 +5441,8 @@ mod tests { #[test] fn get_cairo_pie_execution_base_offset_not_zero() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 1))); @@ -5464,7 +5464,8 @@ mod tests { #[test] fn get_cairo_pie_ret_fp_offset_not_zero() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); @@ -5486,7 +5487,8 @@ mod tests { #[test] fn get_cairo_pie_ret_pc_offset_not_zero() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); @@ -5508,7 +5510,8 @@ mod tests { #[test] fn get_cairo_pie_ok() { - let mut runner = CairoRunner::new(&Program::default(), "all_cairo", false).unwrap(); + let mut runner = + CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); let mut vm = vm!(); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); @@ -5532,7 +5535,7 @@ mod tests { program_content, &CairoRunConfig { proof_mode: true, - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }, &mut BuiltinHintProcessor::new_empty(), From a1b7ab32cb9be242399594180cd72f0c4d182d63 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Apr 2024 18:05:18 -0300 Subject: [PATCH 03/18] Fix clap enum parsing --- Cargo.lock | 1 + cairo-vm-cli/Cargo.toml | 2 +- cairo-vm-cli/src/main.rs | 28 ++-------------------------- cairo1-run/Cargo.toml | 2 +- cairo1-run/src/main.rs | 8 +++----- vm/Cargo.toml | 3 +++ vm/src/types/layout_name.rs | 3 +++ 7 files changed, 14 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d51294e05e..56bf228d87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -894,6 +894,7 @@ dependencies = [ "bitvec", "cairo-lang-casm", "cairo-lang-starknet", + "clap", "criterion", "generic-array", "hashbrown 0.14.3", diff --git a/cairo-vm-cli/Cargo.toml b/cairo-vm-cli/Cargo.toml index 0b63d2745e..15869fe90f 100644 --- a/cairo-vm-cli/Cargo.toml +++ b/cairo-vm-cli/Cargo.toml @@ -8,7 +8,7 @@ readme.workspace = true keywords.workspace = true [dependencies] -cairo-vm = { workspace = true, features = ["std"] } +cairo-vm = { workspace = true, features = ["std", "clap"] } cairo-vm-tracer = { workspace = true, optional = true } clap = { version = "4.3.10", features = ["derive"] } mimalloc = { version = "0.1.37", default-features = false, optional = true } diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 021095134f..9ca039dbaa 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -18,7 +18,7 @@ use cairo_vm::vm::vm_core::VirtualMachine; use cairo_vm_tracer::error::trace_data_errors::TraceDataError; #[cfg(feature = "with_tracer")] use cairo_vm_tracer::tracer::run_tracer; -use clap::{ArgEnum, Parser, ValueHint}; +use clap::{Parser, ValueHint}; use std::io::{self, Write}; use std::path::{Path, PathBuf}; use thiserror::Error; @@ -43,7 +43,7 @@ struct Args { entrypoint: String, #[structopt(long = "memory_file")] memory_file: Option, - #[clap(long = "layout", default_value = "plain", arg_enum)] + #[clap(long = "layout", default_value = "plain", value_enum)] layout: LayoutName, #[structopt(long = "proof_mode")] proof_mode: bool, @@ -395,28 +395,4 @@ mod tests { fn test_main() { main().unwrap(); } - - #[test] - fn test_valid_layouts() { - let valid_layouts = vec![ - "plain", - "small", - "dex", - "starknet", - "starknet_with_keccak", - "recursive_large_output", - "all_cairo", - "all_solidity", - ]; - - for layout in valid_layouts { - assert_eq!(validate_layout(layout), Ok(layout.to_string())); - } - } - - #[test] - fn test_invalid_layout() { - let invalid_layout = "invalid layout name"; - assert!(validate_layout(invalid_layout).is_err()); - } } diff --git a/cairo1-run/Cargo.toml b/cairo1-run/Cargo.toml index a4423fabf8..40b6361b07 100644 --- a/cairo1-run/Cargo.toml +++ b/cairo1-run/Cargo.toml @@ -9,7 +9,7 @@ keywords.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cairo-vm = {workspace = true, features = ["std", "cairo-1-hints"]} +cairo-vm = {workspace = true, features = ["std", "cairo-1-hints", "clap"]} cairo-lang-sierra-type-size = { version = "2.5.4", default-features = false } cairo-lang-sierra-ap-change = { version = "2.5.4", default-features = false } diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 4a549a6686..e925d6e313 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -6,15 +6,13 @@ use cairo_run::Cairo1RunConfig; use cairo_vm::{ air_public_input::PublicInputError, cairo_run::EncodeTraceError, - serde::deserialize_program::LayoutName, - types::errors::program_errors::ProgramError, + types::{errors::program_errors::ProgramError, layout_name::LayoutName}, vm::errors::{ memory_errors::MemoryError, runner_errors::RunnerError, trace_errors::TraceError, vm_errors::VirtualMachineError, - }, - Felt252, + }, Felt252, }; -use clap::{Parser, ValueEnum, ValueHint}; +use clap::{Parser, ValueHint}; use itertools::Itertools; use std::{ io::{self, Write}, diff --git a/vm/Cargo.toml b/vm/Cargo.toml index d1b25dace9..bcd2cfb94c 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -80,6 +80,9 @@ ark-std = { workspace = true, optional = true } # Enable arbitrary when fuzzing arbitrary = { workspace = true, features = ["derive"], optional = true } +# Used to derive clap traits for CLIs + +clap = { version = "4.3.10", features = ["derive"], optional = true} [dev-dependencies] assert_matches = "1.5.0" rstest = { version = "0.17.0", default-features = false } diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index 96f066cadd..251c49f649 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -1,9 +1,12 @@ #[cfg(all(feature = "arbitrary", feature = "std"))] use arbitrary::{self, Arbitrary}; +#[cfg(all(feature = "clap", feature = "std"))] +use clap::ValueEnum; use core::fmt::{self, Display}; use serde::{Deserialize, Serialize}; // This enum is used to deserialize program layouts into &str and catch non-valid names +#[cfg_attr(all(feature = "clap", feature = "std"), derive(ValueEnum))] #[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] #[allow(non_camel_case_types)] From 29ed4d5a94af716c211228ae40b74e05eedd8945 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Apr 2024 18:13:09 -0300 Subject: [PATCH 04/18] Fix tests --- vm/src/tests/cairo_pie_test.rs | 2 +- vm/src/tests/cairo_run_test.rs | 8 ++++---- vm/src/tests/mod.rs | 17 +++++++++-------- vm/src/types/layout_name.rs | 2 +- vm/src/vm/errors/runner_errors.rs | 5 ++--- vm/src/vm/runners/builtin_runner/ec_op.rs | 5 +++-- vm/src/vm/runners/cairo_runner.rs | 10 +++++----- vm/src/vm/vm_core.rs | 5 +++-- 8 files changed, 28 insertions(+), 26 deletions(-) diff --git a/vm/src/tests/cairo_pie_test.rs b/vm/src/tests/cairo_pie_test.rs index 6e3c5de4e4..ad791af277 100644 --- a/vm/src/tests/cairo_pie_test.rs +++ b/vm/src/tests/cairo_pie_test.rs @@ -260,7 +260,7 @@ fn serialize_cairo_pie() { let result = cairo_run( program_content, &CairoRunConfig { - layout: "small", + layout: LayoutName::small, ..Default::default() }, &mut hint_processor, diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index bf44200dd3..6387b20c1c 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -1,4 +1,4 @@ -use crate::tests::*; +use crate::{tests::*, types::layout_name::LayoutName}; #[cfg(feature = "mod_builtin")] use crate::{ utils::test_utils::Program, @@ -1076,7 +1076,7 @@ fn run_program_allow_missing_builtins() { let program_data = include_bytes!("../../../cairo_programs/pedersen_extra_builtins.json"); let config = CairoRunConfig { allow_missing_builtins: Some(true), - layout: "small", // The program logic only uses builtins in the small layout but contains builtins outside of it + layout: LayoutName::small, // The program logic only uses builtins in the small layout but contains builtins outside of it ..Default::default() }; assert!(crate::cairo_run::cairo_run( @@ -1094,7 +1094,7 @@ fn run_program_allow_missing_builtins_proof() { let config = CairoRunConfig { proof_mode: true, allow_missing_builtins: Some(true), - layout: "small", // The program logic only uses builtins in the small layout but contains builtins outside of it + layout: LayoutName::small, // The program logic only uses builtins in the small layout but contains builtins outside of it ..Default::default() }; assert!(crate::cairo_run::cairo_run( @@ -1167,7 +1167,7 @@ fn run_program_with_custom_mod_builtin_params( security_error: Option<&str>, ) { let cairo_run_config = CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, proof_mode, ..Default::default() }; diff --git a/vm/src/tests/mod.rs b/vm/src/tests/mod.rs index d09918726f..3a3d5b2e05 100644 --- a/vm/src/tests/mod.rs +++ b/vm/src/tests/mod.rs @@ -1,3 +1,4 @@ +use crate::types::layout_name::LayoutName; #[cfg(feature = "cairo-1-hints")] use crate::vm::errors::cairo_run_errors::CairoRunError; #[cfg(feature = "cairo-1-hints")] @@ -48,32 +49,32 @@ mod skip_instruction_test; //For simple programs that should just succeed and have no special needs. //Checks memory holes == 0 fn run_program_simple(data: &[u8]) { - run_program(data, false, Some("all_cairo"), None, None) + run_program(data, false, None, None, None) } //For simple programs that should just succeed but using small layout. fn run_program_small(data: &[u8]) { - run_program(data, false, Some("small"), None, None) + run_program(data, false, Some(LayoutName::small), None, None) } fn run_program_with_trace(data: &[u8], trace: &[(usize, usize, usize)]) { - run_program(data, false, Some("all_cairo"), Some(trace), None) + run_program(data, false, None, Some(trace), None) } fn run_program_with_error(data: &[u8], error: &str) { - run_program(data, false, Some("all_cairo"), None, Some(error)) + run_program(data, false, None, None, Some(error)) } fn run_program( data: &[u8], proof_mode: bool, - layout: Option<&str>, + layout: Option, trace: Option<&[(usize, usize, usize)]>, error: Option<&str>, ) { let mut hint_executor = BuiltinHintProcessor::new_empty(); let cairo_run_config = CairoRunConfig { - layout: layout.unwrap_or("all_cairo"), + layout: layout.unwrap_or(LayoutName::all_cairo), relocate_mem: true, trace_enabled: true, proof_mode, @@ -115,7 +116,7 @@ fn run_cairo_1_entrypoint( let mut runner = CairoRunner::new( &(contract_class.clone().try_into().unwrap()), - "all_cairo", + LayoutName::all_cairo, false, ) .unwrap(); @@ -217,7 +218,7 @@ fn run_cairo_1_entrypoint_with_run_resources( ) -> Result, CairoRunError> { let mut runner = CairoRunner::new( &(contract_class.clone().try_into().unwrap()), - "all_cairo", + LayoutName::all_cairo, false, ) .unwrap(); diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index 251c49f649..3683e43ba7 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -5,7 +5,7 @@ use clap::ValueEnum; use core::fmt::{self, Display}; use serde::{Deserialize, Serialize}; -// This enum is used to deserialize program layouts into &str and catch non-valid names +/// Enum representing the name of a Cairo Layout #[cfg_attr(all(feature = "clap", feature = "std"), derive(ValueEnum))] #[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index b0961d69df..37380903bd 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -2,6 +2,7 @@ #![allow(clippy::explicit_auto_deref)] use crate::stdlib::{collections::HashSet, prelude::*}; +use crate::types::layout_name::LayoutName; use thiserror_no_std::Error; use super::{memory_errors::MemoryError, trace_errors::TraceError}; @@ -39,9 +40,7 @@ pub enum RunnerError { #[error("EcOpBuiltin: point {0:?} is not on the curve")] PointNotOnCurve(Box<(Felt252, Felt252)>), #[error("Builtin(s) {:?} not present in layout {}", (*.0).0, (*.0).1)] - NoBuiltinForInstance(Box<(HashSet<&'static str>, String)>), - #[error("Invalid layout {0}")] - InvalidLayoutName(Box), + NoBuiltinForInstance(Box<(HashSet<&'static str>, LayoutName)>), #[error("end_run called twice.")] EndRunCalledTwice, #[error("end_run must be called before finalize_segments.")] diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 2c9be94793..de28357f7e 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -253,6 +253,7 @@ mod tests { use super::*; use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; use crate::serde::deserialize_program::BuiltinName; + use crate::types::layout_name::LayoutName; use crate::types::program::Program; use crate::utils::test_utils::*; use crate::vm::errors::cairo_run_errors::CairoRunError; @@ -854,7 +855,7 @@ mod tests { let program = include_bytes!("../../../../../cairo_programs/bad_programs/ec_op_same_x.json"); let cairo_run_config = crate::cairo_run::CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, ..crate::cairo_run::CairoRunConfig::default() }; let result = crate::cairo_run::cairo_run( @@ -879,7 +880,7 @@ mod tests { let program = include_bytes!("../../../../../cairo_programs/bad_programs/ec_op_not_in_curve.json"); let cairo_run_config = crate::cairo_run::CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, ..crate::cairo_run::CairoRunConfig::default() }; let result = crate::cairo_run::cairo_run( diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index a76bb7457d..4a51359142 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -356,7 +356,7 @@ impl CairoRunner { if !program_builtins.is_empty() && !allow_missing_builtins { return Err(RunnerError::NoBuiltinForInstance(Box::new(( program_builtins.iter().map(|n| n.name()).collect(), - self.layout.name.to_string(), + self.layout.name, )))); } @@ -4529,7 +4529,7 @@ mod tests { cairo_runner.initialize_builtins(&mut vm, false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::output.name()]), - String::from(LayoutName::plain) + LayoutName::plain )))) ); } @@ -4544,7 +4544,7 @@ mod tests { cairo_runner.initialize_builtins(&mut vm, false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::output.name(), HASH_BUILTIN_NAME]), - String::from(LayoutName::plain) + LayoutName::plain )))) ); } @@ -4554,12 +4554,12 @@ mod tests { fn initialize_segments_incorrect_layout_small_two_builtins() { let program = program![BuiltinName::output, BuiltinName::bitwise]; let mut vm = vm!(); - let cairo_runner = cairo_runner!(program, "small"); + let cairo_runner = cairo_runner!(program, LayoutName::small); assert_eq!( cairo_runner.initialize_builtins(&mut vm, false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::bitwise.name()]), - String::from("small") + LayoutName::small, )))) ); } diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index eac5544536..0ce10cce82 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -1245,6 +1245,7 @@ mod tests { use super::*; use crate::felt_hex; use crate::stdlib::collections::HashMap; + use crate::types::layout_name::LayoutName; use crate::types::program::Program; use crate::vm::runners::builtin_runner::{ BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, @@ -4313,7 +4314,7 @@ mod tests { .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); @@ -4338,7 +4339,7 @@ mod tests { .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all_cairo", false); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); let mut vm = vm!(); let end = cairo_runner.initialize(&mut vm, false).unwrap(); From 2d555ca0b27d6fffc59dc63bc25d331a374a1cc1 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 10:41:05 -0300 Subject: [PATCH 05/18] Fix test & doc --- README.md | 2 +- cairo1-run/src/main.rs | 3 ++- examples/custom_hint/src/main.rs | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a213ad393e..de1d3b9ede 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ When running a Cairo program directly using the Cairo-vm repository you would fi ```rust let mut vm = VirtualMachine::new(false); - let mut cairo_runner = CairoRunner::new(&program, "all_cairo", false); + let mut cairo_runner = CairoRunner::new(&program, LayoutName::all_cairo, false); let mut hint_processor = BuiltinHintProcessor::new_empty(); diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index e925d6e313..1020705a9f 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -10,7 +10,8 @@ use cairo_vm::{ vm::errors::{ memory_errors::MemoryError, runner_errors::RunnerError, trace_errors::TraceError, vm_errors::VirtualMachineError, - }, Felt252, + }, + Felt252, }; use clap::{Parser, ValueHint}; use itertools::Itertools; diff --git a/examples/custom_hint/src/main.rs b/examples/custom_hint/src/main.rs index 1917a3268a..c0a4dba450 100644 --- a/examples/custom_hint/src/main.rs +++ b/examples/custom_hint/src/main.rs @@ -6,6 +6,7 @@ use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_integer_fr use cairo_vm::hint_processor::hint_processor_definition::HintReference; use cairo_vm::serde::deserialize_program::ApTracking; use cairo_vm::types::exec_scope::ExecutionScopes; +use cairo_vm::types::layout_name::LayoutName; use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; use cairo_vm::Felt252; use std::collections::HashMap; @@ -46,7 +47,7 @@ fn main() { cairo_run( &buffer, &CairoRunConfig { - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }, &mut hint_processor, From 6455b6b13d6570900546417691c8a412ef75c991 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 10:41:24 -0300 Subject: [PATCH 06/18] Manually implement ValueEnum --- vm/src/types/layout_name.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index 3683e43ba7..ee86b303bd 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -1,12 +1,11 @@ #[cfg(all(feature = "arbitrary", feature = "std"))] use arbitrary::{self, Arbitrary}; #[cfg(all(feature = "clap", feature = "std"))] -use clap::ValueEnum; +use clap::{builder::PossibleValue, ValueEnum}; use core::fmt::{self, Display}; use serde::{Deserialize, Serialize}; /// Enum representing the name of a Cairo Layout -#[cfg_attr(all(feature = "clap", feature = "std"), derive(ValueEnum))] #[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq, Hash)] #[allow(non_camel_case_types)] @@ -56,3 +55,36 @@ impl Display for LayoutName { } } } + +#[cfg(all(feature = "clap", feature = "std"))] +impl ValueEnum for LayoutName { + fn value_variants<'a>() -> &'a [Self] { + &[ + Self::plain, + Self::small, + Self::dex, + Self::recursive, + Self::starknet, + Self::starknet_with_keccak, + Self::recursive_large_output, + Self::all_solidity, + Self::all_cairo, + Self::dynamic, + ] + } + + fn to_possible_value(&self) -> Option { + Some(match self { + LayoutName::plain => PossibleValue::new("plain"), + LayoutName::small => PossibleValue::new("small"), + LayoutName::dex => PossibleValue::new("dex"), + LayoutName::recursive => PossibleValue::new("recursive"), + LayoutName::starknet => PossibleValue::new("starknet"), + LayoutName::starknet_with_keccak => PossibleValue::new("starknet_with_keccak"), + LayoutName::recursive_large_output => PossibleValue::new("recursive_large_output"), + LayoutName::all_solidity => PossibleValue::new("all_solidity"), + LayoutName::all_cairo => PossibleValue::new("all_cairo"), + LayoutName::dynamic => PossibleValue::new("all_cairo"), + }) + } +} From b29473310ac558e3e04eea5843f6614af7d20d8e Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 10:44:30 -0300 Subject: [PATCH 07/18] fmt --- vm/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index bcd2cfb94c..115ae89bf0 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -81,8 +81,8 @@ ark-std = { workspace = true, optional = true } arbitrary = { workspace = true, features = ["derive"], optional = true } # Used to derive clap traits for CLIs - clap = { version = "4.3.10", features = ["derive"], optional = true} + [dev-dependencies] assert_matches = "1.5.0" rstest = { version = "0.17.0", default-features = false } From c068b795f61b7ad8976d606453f91b7a0022255f Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 10:46:49 -0300 Subject: [PATCH 08/18] Simplify code --- vm/src/types/layout_name.rs | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index ee86b303bd..bfb1695de9 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -41,18 +41,7 @@ impl LayoutName { impl Display for LayoutName { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - LayoutName::plain => "plain".fmt(f), - LayoutName::small => "small".fmt(f), - LayoutName::dex => "dex".fmt(f), - LayoutName::recursive => "recursive".fmt(f), - LayoutName::starknet => "starknet".fmt(f), - LayoutName::starknet_with_keccak => "starknet_with_keccak".fmt(f), - LayoutName::recursive_large_output => "recursive_large_output".fmt(f), - LayoutName::all_solidity => "all_solidity".fmt(f), - LayoutName::all_cairo => "all_cairo".fmt(f), - LayoutName::dynamic => "all_cairo".fmt(f), - } + self.to_str().fmt(f) } } @@ -74,17 +63,6 @@ impl ValueEnum for LayoutName { } fn to_possible_value(&self) -> Option { - Some(match self { - LayoutName::plain => PossibleValue::new("plain"), - LayoutName::small => PossibleValue::new("small"), - LayoutName::dex => PossibleValue::new("dex"), - LayoutName::recursive => PossibleValue::new("recursive"), - LayoutName::starknet => PossibleValue::new("starknet"), - LayoutName::starknet_with_keccak => PossibleValue::new("starknet_with_keccak"), - LayoutName::recursive_large_output => PossibleValue::new("recursive_large_output"), - LayoutName::all_solidity => PossibleValue::new("all_solidity"), - LayoutName::all_cairo => PossibleValue::new("all_cairo"), - LayoutName::dynamic => PossibleValue::new("all_cairo"), - }) + Some(PossibleValue::new(self.to_str())) } } From 961e66191facfc05bf113da968afb4506101e886 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 10:49:07 -0300 Subject: [PATCH 09/18] Move arbitrary impl to module --- vm/src/cairo_run.rs | 20 ++------------------ vm/src/types/layout_name.rs | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 931dc8896a..750bb03bb8 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -16,7 +16,7 @@ use bincode::enc::write::Writer; use thiserror_no_std::Error; #[cfg(feature = "arbitrary")] -use arbitrary::{self, Arbitrary, Unstructured}; +use arbitrary::{self, Arbitrary}; #[cfg_attr(feature = "arbitrary", derive(Arbitrary))] pub struct CairoRunConfig<'a> { @@ -24,7 +24,7 @@ pub struct CairoRunConfig<'a> { pub entrypoint: &'a str, pub trace_enabled: bool, pub relocate_mem: bool, - #[cfg_attr(feature = "arbitrary", arbitrary(with = arbitrary_layout))] + #[cfg_attr(feature = "arbitrary", arbitrary(with = crate::types::layout_name::arbitrary_layout))] pub layout: LayoutName, pub proof_mode: bool, pub secure_run: Option, @@ -32,22 +32,6 @@ pub struct CairoRunConfig<'a> { pub allow_missing_builtins: Option, } -#[cfg(feature = "arbitrary")] -fn arbitrary_layout<'a>(u: &mut Unstructured) -> arbitrary::Result { - let layouts = [ - LayoutName::plain, - LayoutName::small, - LayoutName::dex, - LayoutName::starknet, - LayoutName::starknet_with_keccak, - LayoutName::recursive_large_output, - LayoutName::all_cairo, - LayoutName::all_solidity, - LayoutName::dynamic, - ]; - Ok(*u.choose(&layouts)?) -} - impl<'a> Default for CairoRunConfig<'a> { fn default() -> Self { CairoRunConfig { diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index bfb1695de9..91d0fb0d4f 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -1,5 +1,5 @@ #[cfg(all(feature = "arbitrary", feature = "std"))] -use arbitrary::{self, Arbitrary}; +use arbitrary::{self, Arbitrary, Unstructured}; #[cfg(all(feature = "clap", feature = "std"))] use clap::{builder::PossibleValue, ValueEnum}; use core::fmt::{self, Display}; @@ -66,3 +66,19 @@ impl ValueEnum for LayoutName { Some(PossibleValue::new(self.to_str())) } } + +#[cfg(all(feature = "arbitrary", feature = "std"))] +pub fn arbitrary_layout<'a>(u: &mut Unstructured) -> arbitrary::Result { + let layouts = [ + LayoutName::plain, + LayoutName::small, + LayoutName::dex, + LayoutName::starknet, + LayoutName::starknet_with_keccak, + LayoutName::recursive_large_output, + LayoutName::all_cairo, + LayoutName::all_solidity, + LayoutName::dynamic, + ]; + Ok(*u.choose(&layouts)?) +} From 785cab61701876f3b8f9c0bfe95a0f44ef2553ec Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 11:06:18 -0300 Subject: [PATCH 10/18] Fix benchmark code --- bench/criterion_benchmark.rs | 6 +++--- bench/iai_benchmark.rs | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bench/criterion_benchmark.rs b/bench/criterion_benchmark.rs index 48b095cc1f..64074ff121 100644 --- a/bench/criterion_benchmark.rs +++ b/bench/criterion_benchmark.rs @@ -1,5 +1,5 @@ use cairo_vm::{ - types::program::Program, + types::{layout_name::LayoutName, program::Program}, vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, }; use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; @@ -29,7 +29,7 @@ fn build_many_runners(c: &mut Criterion) { _ = black_box( CairoRunner::new( black_box(&program), - black_box("starknet_with_keccak"), + black_box(LayoutName::starknet_with_keccak), black_box(false), ) .unwrap(), @@ -46,7 +46,7 @@ fn load_program_data(c: &mut Criterion) { b.iter_batched( || { ( - CairoRunner::new(&program, "starknet_with_keccak", false).unwrap(), + CairoRunner::new(&program, LayoutName::starknet_with_keccak, false).unwrap(), VirtualMachine::new(false), ) }, diff --git a/bench/iai_benchmark.rs b/bench/iai_benchmark.rs index 02666c93c4..650775f11e 100644 --- a/bench/iai_benchmark.rs +++ b/bench/iai_benchmark.rs @@ -2,7 +2,7 @@ use core::hint::black_box; use iai_callgrind::main; use cairo_vm::{ - types::program::Program, + types::{layout_name::LayoutName, program::Program}, vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, }; @@ -31,7 +31,8 @@ fn parse_program_helper() -> Program { #[inline(never)] fn build_runner() { let program = parse_program_helper(); - let runner = CairoRunner::new(black_box(&program), "starknet_with_keccak", false).unwrap(); + let runner = + CairoRunner::new(black_box(&program), LayoutName::starknet_with_keccak, false).unwrap(); core::mem::drop(black_box(runner)); } @@ -41,7 +42,7 @@ fn build_runner_helper() -> (CairoRunner, VirtualMachine) { //Picked the biggest one at the time of writing let program = include_bytes!("../cairo_programs/benchmarks/keccak_integration_benchmark.json"); let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap(); - let runner = CairoRunner::new(&program, "starknet_with_keccak", false).unwrap(); + let runner = CairoRunner::new(&program, LayoutName::starknet_with_keccak, false).unwrap(); let vm = VirtualMachine::new(false); (runner, vm) } From 1fa267c9fbfd3ff70369dd1622b22ea9fbecc37e Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 11:13:15 -0300 Subject: [PATCH 11/18] Add changelog entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fbaa1b980..aecc357a46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ #### Upcoming Changes +* refactor(BREAKING): Use an enum to represent layout name[#1715](https://github.com/lambdaclass/cairo-vm/pull/1715) + * Add enum `LayoutName` to represent cairo layout names. + * `CairoRunConfig`, `Cairo1RunConfig` & `CairoRunner` field `layout` type changed from `String` to `LayoutName`. + * `CairoLayout` field `name` type changed from `String` to `LayoutName`. + * refactor: Remove unused code & use constants whenever possible for builtin instance definitions[#1707](https://github.com/lambdaclass/cairo-vm/pull/1707) * feat: missing EC hints for Starknet OS 0.13.1 [#1706](https://github.com/lambdaclass/cairo-vm/pull/1706) From 69bdf189762eb43a0b71f66e2358e346d18fa27d Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Apr 2024 11:20:13 -0300 Subject: [PATCH 12/18] Remove unnecessary impl --- vm/src/cairo_run.rs | 1 - vm/src/types/layout_name.rs | 18 +----------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 750bb03bb8..633b339e7d 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -24,7 +24,6 @@ pub struct CairoRunConfig<'a> { pub entrypoint: &'a str, pub trace_enabled: bool, pub relocate_mem: bool, - #[cfg_attr(feature = "arbitrary", arbitrary(with = crate::types::layout_name::arbitrary_layout))] pub layout: LayoutName, pub proof_mode: bool, pub secure_run: Option, diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index ac088079a7..bfb1695de9 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -1,5 +1,5 @@ #[cfg(all(feature = "arbitrary", feature = "std"))] -use arbitrary::{self, Arbitrary, Unstructured}; +use arbitrary::{self, Arbitrary}; #[cfg(all(feature = "clap", feature = "std"))] use clap::{builder::PossibleValue, ValueEnum}; use core::fmt::{self, Display}; @@ -66,19 +66,3 @@ impl ValueEnum for LayoutName { Some(PossibleValue::new(self.to_str())) } } - -#[cfg(all(feature = "arbitrary", feature = "std"))] -pub fn arbitrary_layout(u: &mut Unstructured) -> arbitrary::Result { - let layouts = [ - LayoutName::plain, - LayoutName::small, - LayoutName::dex, - LayoutName::starknet, - LayoutName::starknet_with_keccak, - LayoutName::recursive_large_output, - LayoutName::all_cairo, - LayoutName::all_solidity, - LayoutName::dynamic, - ]; - Ok(*u.choose(&layouts)?) -} From 5a2a513056f5ed8348994ccd9ce38010f8bc8544 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:56:31 -0300 Subject: [PATCH 13/18] Use value instead of reference Co-authored-by: Mario Rugiero --- vm/src/types/layout_name.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index bfb1695de9..d851292a51 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -23,7 +23,7 @@ pub enum LayoutName { } impl LayoutName { - pub fn to_str(&self) -> &'static str { + pub fn to_str(self) -> &'static str { match self { LayoutName::plain => "plain", LayoutName::small => "small", From a6ed611fd87645c2e4b0d5a4801ea2ff090ce94e Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 18 Apr 2024 15:41:44 -0300 Subject: [PATCH 14/18] Fix test added by merge --- vm/src/tests/cairo_run_test.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index 98bcafdd46..dec10e63eb 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -1228,7 +1228,7 @@ fn run_program_with_custom_mod_builtin_params( fn cairo_run_apply_poly() { let program_data = include_bytes!("../../../cairo_programs/mod_builtin_feature/apply_poly.json"); - run_program(program_data, false, Some("all_cairo"), None, None); + run_program(program_data, false, None, None, None); } #[test] @@ -1236,5 +1236,5 @@ fn cairo_run_apply_poly() { fn cairo_run_apply_poly_proof() { let program_data = include_bytes!("../../../cairo_programs/mod_builtin_feature/proof/apply_poly.json"); - run_program(program_data, true, Some("all_cairo"), None, None); + run_program(program_data, true, None, None, None); } From 44464c4c169095a047a1d870240193661d42c2f0 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 18 Apr 2024 16:04:24 -0300 Subject: [PATCH 15/18] Add `recursive_with_poseidon` layout --- .../compare_factorial_outputs_all_layouts.sh | 2 +- .../builtins_instance_def.rs | 16 ++++++++++++++++ vm/src/types/layout.rs | 9 +++++++++ vm/src/types/layout_name.rs | 3 +++ vm/src/vm/runners/cairo_runner.rs | 1 + 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/vm/src/tests/compare_factorial_outputs_all_layouts.sh b/vm/src/tests/compare_factorial_outputs_all_layouts.sh index 1a2067e4c8..e634fbb9d4 100755 --- a/vm/src/tests/compare_factorial_outputs_all_layouts.sh +++ b/vm/src/tests/compare_factorial_outputs_all_layouts.sh @@ -5,7 +5,7 @@ passed_tests=0 failed_tests=0 exit_code=0 -for layout in "plain" "small" "dex" "recursive" "starknet" "starknet_with_keccak" "recursive_large_output" "all_solidity" "starknet_with_keccak"; do +for layout in "plain" "small" "dex" "recursive" "starknet" "starknet_with_keccak" "recursive_large_output" "recursive_with_poseidon" "all_solidity" "starknet_with_keccak"; do # Run cairo_vm echo "Running cairo-vm with layout $layout" cargo run -p cairo-vm-cli --release -- --layout $layout --proof_mode $factorial_compiled --trace_file factorial_rs.trace --memory_file factorial_rs.memory --air_public_input factorial_rs.air_public_input --air_private_input factorial_rs.air_private_input diff --git a/vm/src/types/instance_definitions/builtins_instance_def.rs b/vm/src/types/instance_definitions/builtins_instance_def.rs index a6d0bc44cc..ab13cf14fb 100644 --- a/vm/src/types/instance_definitions/builtins_instance_def.rs +++ b/vm/src/types/instance_definitions/builtins_instance_def.rs @@ -138,6 +138,22 @@ impl BuiltinsInstanceDef { } } + pub(crate) fn recursive_with_poseidon() -> BuiltinsInstanceDef { + BuiltinsInstanceDef { + output: true, + pedersen: Some(PedersenInstanceDef::new(Some(256))), + range_check: Some(RangeCheckInstanceDef::new(Some(16))), + ecdsa: None, + bitwise: Some(BitwiseInstanceDef::new(Some(16))), + ec_op: None, + keccak: None, + poseidon: Some(PoseidonInstanceDef::new(Some(64))), + range_check96: None, + add_mod: None, + mul_mod: None, + } + } + pub(crate) fn all_cairo() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index f7c523e5d7..93d1077b4d 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -87,6 +87,15 @@ impl CairoLayout { diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), } } + pub(crate) fn recursive_with_poseidon() -> CairoLayout { + CairoLayout { + name: LayoutName::recursive_with_poseidon, + rc_units: 4, + builtins: BuiltinsInstanceDef::recursive_with_poseidon(), + public_memory_fraction: 8, + diluted_pool_instance_def: Some(DilutedPoolInstanceDef::new(3, 4, 16)), + } + } pub(crate) fn all_cairo_instance() -> CairoLayout { CairoLayout { diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index d851292a51..1c957fbf90 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -17,6 +17,7 @@ pub enum LayoutName { starknet, starknet_with_keccak, recursive_large_output, + recursive_with_poseidon, all_solidity, all_cairo, dynamic, @@ -32,6 +33,7 @@ impl LayoutName { LayoutName::starknet => "starknet", LayoutName::starknet_with_keccak => "starknet_with_keccak", LayoutName::recursive_large_output => "recursive_large_output", + LayoutName::recursive_with_poseidon => "recursive_with_poseidon", LayoutName::all_solidity => "all_solidity", LayoutName::all_cairo => "all_cairo", LayoutName::dynamic => "all_cairo", @@ -56,6 +58,7 @@ impl ValueEnum for LayoutName { Self::starknet, Self::starknet_with_keccak, Self::recursive_large_output, + Self::recursive_with_poseidon, Self::all_solidity, Self::all_cairo, Self::dynamic, diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 02a0811d52..1040e1f52a 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -183,6 +183,7 @@ impl CairoRunner { LayoutName::starknet => CairoLayout::starknet_instance(), LayoutName::starknet_with_keccak => CairoLayout::starknet_with_keccak_instance(), LayoutName::recursive_large_output => CairoLayout::recursive_large_output_instance(), + LayoutName::recursive_with_poseidon => CairoLayout::recursive_with_poseidon(), LayoutName::all_cairo => CairoLayout::all_cairo_instance(), LayoutName::all_solidity => CairoLayout::all_solidity_instance(), LayoutName::dynamic => CairoLayout::dynamic_instance(), From f5e5cf9bb6c758b0eaa96133b1eafeaa49bf9019 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 18 Apr 2024 16:08:46 -0300 Subject: [PATCH 16/18] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a16017d4a..d21298bab8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Add `recursive_with_poseidon` layout[#1724](https://github.com/lambdaclass/cairo-vm/pull/1724) + * refactor(BREAKING): Use an enum to represent layout name[#1715](https://github.com/lambdaclass/cairo-vm/pull/1715) * Add enum `LayoutName` to represent cairo layout names. * `CairoRunConfig`, `Cairo1RunConfig` & `CairoRunner` field `layout` type changed from `String` to `LayoutName`. From 75ae7a3d0e6135811395bb24dd33c30d7c8fba2f Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 18 Apr 2024 16:38:29 -0300 Subject: [PATCH 17/18] Add comment + fix log_units in diluted pool --- vm/src/types/instance_definitions/diluted_pool_instance_def.rs | 2 +- vm/src/types/layout.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/types/instance_definitions/diluted_pool_instance_def.rs b/vm/src/types/instance_definitions/diluted_pool_instance_def.rs index 24e1a51ba6..777abab488 100644 --- a/vm/src/types/instance_definitions/diluted_pool_instance_def.rs +++ b/vm/src/types/instance_definitions/diluted_pool_instance_def.rs @@ -2,7 +2,7 @@ use serde::Serialize; #[derive(Serialize, Debug, PartialEq)] pub(crate) struct DilutedPoolInstanceDef { - pub(crate) units_per_step: u32, + pub(crate) units_per_step: u32, // log_units_per_step ^ 2 (for cairo_lang comparison) pub(crate) spacing: u32, pub(crate) n_bits: u32, } diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 93d1077b4d..bad20d8166 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -93,7 +93,7 @@ impl CairoLayout { rc_units: 4, builtins: BuiltinsInstanceDef::recursive_with_poseidon(), public_memory_fraction: 8, - diluted_pool_instance_def: Some(DilutedPoolInstanceDef::new(3, 4, 16)), + diluted_pool_instance_def: Some(DilutedPoolInstanceDef::new(9, 4, 16)), } } From 56ad219420920af644a6199827349c72812fb257 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 18 Apr 2024 17:06:03 -0300 Subject: [PATCH 18/18] Fix log_units & comment --- vm/src/types/instance_definitions/diluted_pool_instance_def.rs | 2 +- vm/src/types/layout.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/types/instance_definitions/diluted_pool_instance_def.rs b/vm/src/types/instance_definitions/diluted_pool_instance_def.rs index 777abab488..9be73a835d 100644 --- a/vm/src/types/instance_definitions/diluted_pool_instance_def.rs +++ b/vm/src/types/instance_definitions/diluted_pool_instance_def.rs @@ -2,7 +2,7 @@ use serde::Serialize; #[derive(Serialize, Debug, PartialEq)] pub(crate) struct DilutedPoolInstanceDef { - pub(crate) units_per_step: u32, // log_units_per_step ^ 2 (for cairo_lang comparison) + pub(crate) units_per_step: u32, // 2 ^ log_units_per_step (for cairo_lang comparison) pub(crate) spacing: u32, pub(crate) n_bits: u32, } diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index bad20d8166..ee0fae3196 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -93,7 +93,7 @@ impl CairoLayout { rc_units: 4, builtins: BuiltinsInstanceDef::recursive_with_poseidon(), public_memory_fraction: 8, - diluted_pool_instance_def: Some(DilutedPoolInstanceDef::new(9, 4, 16)), + diluted_pool_instance_def: Some(DilutedPoolInstanceDef::new(8, 4, 16)), } }