diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6bf4581f..4a16017d4a 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`. + * fix(BREAKING): Remove unsafe impl of `Add for &'a Relocatable`[#1718](https://github.com/lambdaclass/cairo-vm/pull/1718) * fix(BREAKING): Handle triple dereference references[#1708](https://github.com/lambdaclass/cairo-vm/pull/1708) diff --git a/Cargo.lock b/Cargo.lock index 499b5fe6c0..254a00c096 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/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/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) } 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 ddb97c068a..9ca039dbaa 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::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; @@ -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", value_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")] @@ -173,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, @@ -410,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/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 8e2a99e021..1020705a9f 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -6,7 +6,7 @@ use cairo_run::Cairo1RunConfig; use cairo_vm::{ air_public_input::PublicInputError, cairo_run::EncodeTraceError, - 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, @@ -32,8 +32,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,22 +109,6 @@ fn process_args(value: &str) -> Result { Ok(FuncArgs(args)) } -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)] pub enum Error { #[error("Invalid arguments")] @@ -214,7 +198,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/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, 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/Cargo.toml b/vm/Cargo.toml index e04782221e..579f5c0298 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/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..633b339e7d 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}, @@ -15,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> { @@ -23,37 +24,20 @@ pub struct CairoRunConfig<'a> { pub entrypoint: &'a str, 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, pub allow_missing_builtins: Option, } -#[cfg(feature = "arbitrary")] -fn arbitrary_layout<'a>(u: &mut Unstructured) -> arbitrary::Result<&'a str> { - let layouts = [ - "plain", - "small", - "dex", - "starknet", - "starknet_with_keccak", - "recursive_large_output", - "all_cairo", - "all_solidity", - "dynamic", - ]; - Ok(u.choose(&layouts)?) -} - impl<'a> Default for CairoRunConfig<'a> { fn default() -> Self { CairoRunConfig { 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/tests/cairo_pie_test.rs b/vm/src/tests/cairo_pie_test.rs index 933485fb73..ad791af277 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, @@ -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 fc1023d23e..dec10e63eb 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() }; @@ -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); } 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.rs b/vm/src/types/layout.rs index 3533f817fe..f7c523e5d7 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,4 +1,4 @@ -use crate::stdlib::prelude::*; +use crate::types::layout_name::LayoutName; use super::instance_definitions::{ builtins_instance_def::BuiltinsInstanceDef, diluted_pool_instance_def::DilutedPoolInstanceDef, @@ -10,7 +10,7 @@ use serde::Serialize; #[derive(Serialize, Debug)] pub struct CairoLayout { - pub(crate) name: String, + pub(crate) name: LayoutName, pub(crate) rc_units: u32, pub(crate) builtins: BuiltinsInstanceDef, pub(crate) public_memory_fraction: u32, @@ -20,7 +20,7 @@ pub struct CairoLayout { impl CairoLayout { pub(crate) fn plain_instance() -> CairoLayout { CairoLayout { - name: String::from("plain"), + name: LayoutName::plain, rc_units: 16, builtins: BuiltinsInstanceDef::plain(), public_memory_fraction: 4, @@ -30,7 +30,7 @@ impl CairoLayout { pub(crate) fn small_instance() -> CairoLayout { CairoLayout { - name: String::from("small"), + name: LayoutName::small, rc_units: 16, builtins: BuiltinsInstanceDef::small(), public_memory_fraction: 4, @@ -40,7 +40,7 @@ impl CairoLayout { pub(crate) fn dex_instance() -> CairoLayout { CairoLayout { - name: String::from("dex"), + name: LayoutName::dex, rc_units: 4, builtins: BuiltinsInstanceDef::dex(), public_memory_fraction: 4, @@ -50,7 +50,7 @@ impl CairoLayout { pub(crate) fn recursive_instance() -> CairoLayout { CairoLayout { - name: String::from("recursive"), + name: LayoutName::recursive, rc_units: 4, builtins: BuiltinsInstanceDef::recursive(), public_memory_fraction: 8, @@ -60,7 +60,7 @@ impl CairoLayout { pub(crate) fn starknet_instance() -> CairoLayout { CairoLayout { - name: String::from("starknet"), + name: LayoutName::starknet, rc_units: 4, builtins: BuiltinsInstanceDef::starknet(), public_memory_fraction: 8, @@ -70,7 +70,7 @@ impl CairoLayout { pub(crate) fn starknet_with_keccak_instance() -> CairoLayout { CairoLayout { - name: String::from("starknet_with_keccak"), + name: LayoutName::starknet_with_keccak, rc_units: 4, builtins: BuiltinsInstanceDef::starknet_with_keccak(), public_memory_fraction: 8, @@ -80,7 +80,7 @@ impl CairoLayout { pub(crate) fn recursive_large_output_instance() -> CairoLayout { CairoLayout { - name: String::from("recursive_large_output"), + name: LayoutName::recursive_large_output, rc_units: 4, builtins: BuiltinsInstanceDef::recursive_large_output(), public_memory_fraction: 8, @@ -90,7 +90,7 @@ impl CairoLayout { pub(crate) fn all_cairo_instance() -> CairoLayout { CairoLayout { - name: String::from("all_cairo"), + name: LayoutName::all_cairo, rc_units: 4, builtins: BuiltinsInstanceDef::all_cairo(), public_memory_fraction: 8, @@ -100,7 +100,7 @@ impl CairoLayout { pub(crate) fn all_solidity_instance() -> CairoLayout { CairoLayout { - name: String::from("all_solidity"), + name: LayoutName::all_solidity, rc_units: 8, builtins: BuiltinsInstanceDef::all_solidity(), public_memory_fraction: 8, @@ -110,7 +110,7 @@ impl CairoLayout { pub(crate) fn dynamic_instance() -> CairoLayout { CairoLayout { - name: String::from("dynamic"), + name: LayoutName::dynamic, rc_units: 16, builtins: BuiltinsInstanceDef::dynamic(), public_memory_fraction: 8, @@ -131,7 +131,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.rc_units, 16); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 4); @@ -143,7 +143,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.rc_units, 16); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 4); @@ -155,7 +155,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.rc_units, 4); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 4); @@ -166,7 +166,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.rc_units, 4); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); @@ -180,7 +180,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.rc_units, 4); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); @@ -194,7 +194,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.rc_units, 4); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); @@ -208,7 +208,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.rc_units, 4); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); @@ -222,7 +222,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.rc_units, 4); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); @@ -236,7 +236,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.rc_units, 8); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); @@ -250,7 +250,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.rc_units, 16); assert_eq!(layout.builtins, builtins); assert_eq!(layout.public_memory_fraction, 8); diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs new file mode 100644 index 0000000000..d851292a51 --- /dev/null +++ b/vm/src/types/layout_name.rs @@ -0,0 +1,68 @@ +#[cfg(all(feature = "arbitrary", feature = "std"))] +use arbitrary::{self, Arbitrary}; +#[cfg(all(feature = "clap", feature = "std"))] +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 = "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 { + self.to_str().fmt(f) + } +} + +#[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(PossibleValue::new(self.to_str())) + } +} 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 adb68cbc07..2aae236af3 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/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/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/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 8dd50f0ffa..a72166e7bb 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -243,6 +243,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; @@ -834,7 +835,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( @@ -859,7 +860,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/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 91254d4789..4cbfd56328 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -692,6 +692,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}, @@ -706,7 +707,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 8ea8600d3e..02a0811d52 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -7,14 +7,14 @@ use crate::{ ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}, prelude::*, }, - types::layout::MEMORY_UNITS_PER_STEP, + types::{layout::MEMORY_UNITS_PER_STEP, layout_name::LayoutName}, vm::{ runners::builtin_runner::SegmentArenaBuiltinRunner, trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry}, }, + Felt252, }; -use crate::Felt252; use crate::{ hint_processor::hint_processor_definition::{HintProcessor, HintReference}, math_utils::safe_div_usize, @@ -172,25 +172,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(), @@ -219,7 +214,7 @@ impl CairoRunner { pub fn new( program: &Program, - layout: &str, + layout: LayoutName, proof_mode: bool, ) -> Result { if proof_mode { @@ -358,7 +353,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, )))); } @@ -1449,15 +1444,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)?, @@ -1675,7 +1669,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()); } @@ -1684,7 +1678,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), @@ -3833,7 +3827,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(); @@ -3902,7 +3896,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() @@ -3920,7 +3914,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() @@ -4039,7 +4033,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; @@ -4056,7 +4050,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; @@ -4090,7 +4084,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]); @@ -4195,7 +4189,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; @@ -4517,12 +4511,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") + LayoutName::plain )))) ); } @@ -4532,12 +4526,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") + LayoutName::plain )))) ); } @@ -4547,12 +4541,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, )))) ); } @@ -4716,7 +4710,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; @@ -4740,7 +4734,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; @@ -4758,7 +4752,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; @@ -4777,7 +4771,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; @@ -4808,7 +4802,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; @@ -4839,7 +4833,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; @@ -5351,14 +5345,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)) @@ -5366,7 +5361,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))); @@ -5386,7 +5382,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))); @@ -5408,7 +5405,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))); @@ -5430,7 +5428,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))); @@ -5452,7 +5451,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))); @@ -5474,7 +5474,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))); @@ -5496,7 +5497,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))); @@ -5520,7 +5522,7 @@ mod tests { program_content, &CairoRunConfig { proof_mode: true, - layout: "all_cairo", + layout: LayoutName::all_cairo, ..Default::default() }, &mut BuiltinHintProcessor::new_empty(), diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index 42a02b80fb..da21035a06 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, @@ -4310,7 +4311,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(); @@ -4335,7 +4336,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();