Skip to content

Commit

Permalink
Make wasm compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Aug 26, 2024
1 parent 11edca3 commit adbba88
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 44 deletions.
8 changes: 7 additions & 1 deletion cairo-vm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +168,11 @@ fn run(args: impl Iterator<Item = String>) -> 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)?),

Check warning on line 172 in cairo-vm-cli/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo-vm-cli/src/main.rs#L172

Added line #L172 was not covered by tests
None => None,
};

let cairo_run_config = cairo_run::CairoRunConfig {
entrypoint: &args.entrypoint,
trace_enabled,
Expand All @@ -175,7 +181,7 @@ fn run(args: impl Iterator<Item = String>) -> 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()
};

Expand Down
12 changes: 6 additions & 6 deletions cairo1-run/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand Down Expand Up @@ -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<PathBuf>,
pub cairo_layout_params: Option<CairoLayoutParams>,
/// Run in proof_mode
pub proof_mode: bool,
/// Should be true if either air_public_input or cairo_pie_output are needed
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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,
)?;
Expand Down
8 changes: 7 additions & 1 deletion cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -158,6 +159,11 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, 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)?),

Check warning on line 163 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L163

Added line #L163 was not covered by tests
None => None,
};

let cairo_run_config = Cairo1RunConfig {
proof_mode: args.proof_mode,
serialize_output: args.print_output,
Expand All @@ -167,7 +173,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, 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
Expand Down
2 changes: 1 addition & 1 deletion fuzzer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions vm/src/cairo_run.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<PathBuf>,
pub cairo_layout_params: Option<CairoLayoutParams>,
pub proof_mode: bool,
pub secure_run: Option<bool>,
pub disable_trace_padding: bool,
Expand All @@ -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,
}
}
}
Expand All @@ -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,
)?;
Expand Down Expand Up @@ -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,
)?;
Expand Down Expand Up @@ -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(),

Check warning on line 232 in vm/src/cairo_run.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/cairo_run.rs#L232

Added line #L232 was not covered by tests
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
24 changes: 14 additions & 10 deletions vm/src/types/layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{fs::File, io, path::Path};

use crate::types::layout_name::LayoutName;

use super::instance_definitions::{
Expand Down Expand Up @@ -131,16 +129,13 @@ impl CairoLayout {
builtins: BuiltinsInstanceDef::dynamic(params),
}
}

pub(crate) fn dynamic_instance_from_file(params_file: &Path) -> io::Result<CairoLayout> {
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))]

Check warning on line 137 in vm/src/types/layout.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/types/layout.rs#L137

Added line #L137 was not covered by tests
#[derive(Deserialize, Debug, Default, Clone)]
pub struct CairoLayoutParams {
pub rc_units: u32,
pub log_diluted_units_per_step: u32,
Expand Down Expand Up @@ -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<Self> {
let params_file = std::fs::File::open(params_path)?;
let params = serde_json::from_reader(params_file)?;
Ok(params)
}

Check warning on line 176 in vm/src/types/layout.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/types/layout.rs#L172-L176

Added lines #L172 - L176 were not covered by tests
}

#[cfg(test)]
mod tests {
use crate::types::instance_definitions::{
Expand Down
32 changes: 16 additions & 16 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

use crate::{
air_private_input::AirPrivateInput,
air_public_input::{PublicInput, PublicInputError},
Expand All @@ -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},
Expand Down Expand Up @@ -170,12 +172,12 @@ pub enum RunnerMode {
}

impl CairoRunner {
/// The `cairo_layout_params_file` argument should only be used with dynamic layout.
/// The `cairo_layout_params` argument should only be used with dynamic layout.
/// It is ignored otherwise.
pub fn new_v2(
program: &Program,
layout: LayoutName,
cairo_layout_params_file: Option<PathBuf>,
cairo_layout_params: Option<CairoLayoutParams>,
mode: RunnerMode,
trace_enabled: bool,
) -> Result<CairoRunner, RunnerError> {
Expand All @@ -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"

Check warning on line 198 in vm/src/vm/runners/cairo_runner.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/runners/cairo_runner.rs#L197-L198

Added lines #L197 - L198 were not covered by tests
);

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(),
))?;

Check warning on line 203 in vm/src/vm/runners/cairo_runner.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/runners/cairo_runner.rs#L201-L203

Added lines #L201 - L203 were not covered by tests

CairoLayout::dynamic_instance_from_file(&params_file)
.map_err(|err| RunnerError::BadDynamicLayoutParams(err.to_string()))?
CairoLayout::dynamic_instance(params)

Check warning on line 205 in vm/src/vm/runners/cairo_runner.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/runners/cairo_runner.rs#L205

Added line #L205 was not covered by tests
}
};
Ok(CairoRunner {
Expand Down Expand Up @@ -233,23 +233,23 @@ impl CairoRunner {
pub fn new(
program: &Program,
layout: LayoutName,
cairo_layout_params_file: Option<PathBuf>,
cairo_layout_params: Option<CairoLayoutParams>,
proof_mode: bool,
trace_enabled: bool,
) -> Result<CairoRunner, RunnerError> {
if proof_mode {
Self::new_v2(
program,
layout,
cairo_layout_params_file,
cairo_layout_params,
RunnerMode::ProofModeCanonical,
trace_enabled,
)
} else {
Self::new_v2(
program,
layout,
cairo_layout_params_file,
cairo_layout_params,
RunnerMode::ExecutionMode,
trace_enabled,
)
Expand Down

0 comments on commit adbba88

Please sign in to comment.