From 4913d865a4ed630fde9c4530ae2c9f544e52e9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 18:55:27 -0300 Subject: [PATCH] Make wasm compatible --- cairo-vm-cli/src/main.rs | 8 +++++++- cairo1-run/src/cairo_run.rs | 12 ++++++------ cairo1-run/src/main.rs | 8 +++++++- fuzzer/Cargo.lock | 2 +- vm/src/cairo_run.rs | 17 +++++++++-------- vm/src/tests/cairo_run_test.rs | 2 +- vm/src/types/layout.rs | 24 ++++++++++++++---------- vm/src/vm/runners/cairo_runner.rs | 30 +++++++++++++++--------------- 8 files changed, 60 insertions(+), 43 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index e43e06c748..3a164f2c4b 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::CairoLayoutParams; use cairo_vm::types::layout_name::LayoutName; use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; use cairo_vm::vm::errors::trace_errors::TraceError; @@ -167,6 +168,11 @@ fn run(args: impl Iterator) -> Result<(), Error> { let trace_enabled = args.trace_file.is_some() || args.air_public_input.is_some(); + let cairo_layout_params = match args.cairo_layout_params_file { + Some(file) => Some(CairoLayoutParams::from_file(&file)?), + None => None, + }; + let cairo_run_config = cairo_run::CairoRunConfig { entrypoint: &args.entrypoint, trace_enabled, @@ -175,7 +181,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { proof_mode: args.proof_mode, secure_run: args.secure_run, allow_missing_builtins: args.allow_missing_builtins, - cairo_layout_params_file: args.cairo_layout_params_file, + cairo_layout_params, ..Default::default() }; diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 60bd02614f..7aca8f5710 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -38,8 +38,8 @@ use cairo_vm::{ math_utils::signed_felt, serde::deserialize_program::{ApTracking, FlowTrackingData, HintParams, ReferenceManager}, types::{ - builtin_name::BuiltinName, layout_name::LayoutName, program::Program, - relocatable::MaybeRelocatable, + builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName, + program::Program, relocatable::MaybeRelocatable, }, vm::{ errors::{runner_errors::RunnerError, vm_errors::VirtualMachineError}, @@ -51,7 +51,7 @@ use cairo_vm::{ use itertools::{chain, Itertools}; use num_bigint::{BigInt, Sign}; use num_traits::{cast::ToPrimitive, Zero}; -use std::{collections::HashMap, iter::Peekable, path::PathBuf}; +use std::{collections::HashMap, iter::Peekable}; /// Representation of a cairo argument /// Can consist of a single Felt or an array of Felts @@ -86,7 +86,7 @@ pub struct Cairo1RunConfig<'a> { pub relocate_mem: bool, /// Cairo layout chosen for the run pub layout: LayoutName, - pub cairo_layout_params_file: Option, + pub cairo_layout_params: Option, /// Run in proof_mode pub proof_mode: bool, /// Should be true if either air_public_input or cairo_pie_output are needed @@ -107,7 +107,7 @@ impl Default for Cairo1RunConfig<'_> { proof_mode: false, finalize_builtins: false, append_return_values: false, - cairo_layout_params_file: None, + cairo_layout_params: None, } } } @@ -250,7 +250,7 @@ pub fn cairo_run_program( let mut runner = CairoRunner::new_v2( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), runner_mode, cairo_run_config.trace_enabled, )?; diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 41587e8b92..8969b747d8 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -4,6 +4,7 @@ use cairo1_run::{cairo_run_program, Cairo1RunConfig, FuncArg}; use cairo_lang_compiler::{ compile_prepared_db, db::RootDatabase, project::setup_project, CompilerConfig, }; +use cairo_vm::types::layout::CairoLayoutParams; use cairo_vm::{ air_public_input::PublicInputError, types::layout_name::LayoutName, vm::errors::trace_errors::TraceError, Felt252, @@ -158,6 +159,11 @@ fn run(args: impl Iterator) -> Result, Error> { args.args = process_args(&std::fs::read_to_string(filename)?).unwrap(); } + let cairo_layout_params = match args.cairo_layout_params_file { + Some(file) => Some(CairoLayoutParams::from_file(&file)?), + None => None, + }; + let cairo_run_config = Cairo1RunConfig { proof_mode: args.proof_mode, serialize_output: args.print_output, @@ -167,7 +173,7 @@ fn run(args: impl Iterator) -> Result, Error> { args: &args.args.0, finalize_builtins: args.air_public_input.is_some() || args.cairo_pie_output.is_some(), append_return_values: args.append_return_values, - cairo_layout_params_file: args.cairo_layout_params_file, + cairo_layout_params, }; // Try to parse the file as a sierra program diff --git a/fuzzer/Cargo.lock b/fuzzer/Cargo.lock index e1ccedbfb8..e1ef312c3a 100644 --- a/fuzzer/Cargo.lock +++ b/fuzzer/Cargo.lock @@ -216,7 +216,7 @@ dependencies = [ [[package]] name = "cairo-vm" -version = "1.0.0-rc5" +version = "1.0.1" dependencies = [ "anyhow", "arbitrary", diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 806e0b08bd..a96e6860a1 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -1,8 +1,9 @@ -use std::path::PathBuf; - use crate::{ hint_processor::hint_processor_definition::HintProcessor, - types::{builtin_name::BuiltinName, layout_name::LayoutName, program::Program}, + types::{ + builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName, + program::Program, + }, vm::{ errors::{ cairo_run_errors::CairoRunError, runner_errors::RunnerError, vm_exception::VmException, @@ -28,7 +29,7 @@ pub struct CairoRunConfig<'a> { pub trace_enabled: bool, pub relocate_mem: bool, pub layout: LayoutName, - pub cairo_layout_params_file: Option, + pub cairo_layout_params: Option, pub proof_mode: bool, pub secure_run: Option, pub disable_trace_padding: bool, @@ -46,7 +47,7 @@ impl<'a> Default for CairoRunConfig<'a> { secure_run: None, disable_trace_padding: false, allow_missing_builtins: None, - cairo_layout_params_file: None, + cairo_layout_params: None, } } } @@ -69,7 +70,7 @@ pub fn cairo_run_program_with_initial_scope( let mut cairo_runner = CairoRunner::new( program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; @@ -156,7 +157,7 @@ pub fn cairo_run_pie( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), false, cairo_run_config.trace_enabled, )?; @@ -228,7 +229,7 @@ pub fn cairo_run_fuzzed_program( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index 741955a16a..7f17ea62cf 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -1182,7 +1182,7 @@ fn run_program_with_custom_mod_builtin_params( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file, + cairo_run_config.cairo_layout_params, cairo_run_config.proof_mode, cairo_run_config.trace_enabled, ) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 0d6209e2e7..debbb2f244 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,5 +1,3 @@ -use std::{fs::File, io, path::Path}; - use crate::types::layout_name::LayoutName; use super::instance_definitions::{ @@ -131,16 +129,13 @@ impl CairoLayout { builtins: BuiltinsInstanceDef::dynamic(params), } } - - pub(crate) fn dynamic_instance_from_file(params_file: &Path) -> io::Result { - let params_file = File::open(params_file)?; - let params: CairoLayoutParams = serde_json::from_reader(params_file)?; - - Ok(Self::dynamic_instance(params)) - } } -#[derive(Deserialize, Debug, Default)] +#[cfg(feature = "test_utils")] +use arbitrary::{self, Arbitrary}; + +#[cfg_attr(feature = "test_utils", derive(Arbitrary))] +#[derive(Deserialize, Debug, Default, Clone)] pub struct CairoLayoutParams { pub rc_units: u32, pub log_diluted_units_per_step: u32, @@ -172,6 +167,15 @@ pub struct CairoLayoutParams { pub add_mod_ratio_den: u32, } +impl CairoLayoutParams { + #[cfg(feature = "std")] + pub fn from_file(params_path: &std::path::Path) -> std::io::Result { + let params_file = std::fs::File::open(params_path)?; + let params = serde_json::from_reader(params_file)?; + Ok(params) + } +} + #[cfg(test)] mod tests { use crate::types::instance_definitions::{ diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 77683e5523..4fab319bdc 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, @@ -9,7 +7,11 @@ use crate::{ ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}, prelude::*, }, - types::{builtin_name::BuiltinName, layout::MEMORY_UNITS_PER_STEP, layout_name::LayoutName}, + types::{ + builtin_name::BuiltinName, + layout::{CairoLayoutParams, MEMORY_UNITS_PER_STEP}, + layout_name::LayoutName, + }, vm::{ runners::builtin_runner::SegmentArenaBuiltinRunner, trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry}, @@ -175,7 +177,7 @@ impl CairoRunner { pub fn new_v2( program: &Program, layout: LayoutName, - cairo_layout_params_file: Option, + cairo_layout_params: Option, mode: RunnerMode, trace_enabled: bool, ) -> Result { @@ -192,17 +194,15 @@ impl CairoRunner { LayoutName::all_solidity => CairoLayout::all_solidity_instance(), LayoutName::dynamic => { debug_assert!( - cairo_layout_params_file.is_some(), - "cairo layout params is missing with dynamic layout" + cairo_layout_params.is_some(), + "cairo layout params is missing while using dynamic layout" ); - let params_file = - cairo_layout_params_file.ok_or(RunnerError::BadDynamicLayoutParams( - "cairo layout param file is missing".to_string(), - ))?; + let params = cairo_layout_params.ok_or(RunnerError::BadDynamicLayoutParams( + "cairo layout param is missing".to_string(), + ))?; - CairoLayout::dynamic_instance_from_file(¶ms_file) - .map_err(|err| RunnerError::BadDynamicLayoutParams(err.to_string()))? + CairoLayout::dynamic_instance(params) } }; Ok(CairoRunner { @@ -233,7 +233,7 @@ impl CairoRunner { pub fn new( program: &Program, layout: LayoutName, - cairo_layout_params_file: Option, + cairo_layout_params: Option, proof_mode: bool, trace_enabled: bool, ) -> Result { @@ -241,7 +241,7 @@ impl CairoRunner { Self::new_v2( program, layout, - cairo_layout_params_file, + cairo_layout_params, RunnerMode::ProofModeCanonical, trace_enabled, ) @@ -249,7 +249,7 @@ impl CairoRunner { Self::new_v2( program, layout, - cairo_layout_params_file, + cairo_layout_params, RunnerMode::ExecutionMode, trace_enabled, )