From 9ef2ab88d89a9b879553b55e045db1f4e148ac26 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 10 May 2024 11:43:26 -0300 Subject: [PATCH] refactor: Move the VM back to the CairoRunner (#1743) * Initial commit * Remove pycache * Makt it compile * Fix tests * Fix feature-gated code * Fix benhcmark code * Update docs * Add changelog entry * shorten call paths * Fix tests --- CHANGELOG.md | 50 + README.md | 9 +- bench/criterion_benchmark.rs | 14 +- bench/iai_benchmark.rs | 21 +- cairo-vm-cli/src/main.rs | 19 +- cairo1-run/src/cairo_run.rs | 55 +- cairo1-run/src/main.rs | 8 +- examples/wasm-demo/src/lib.rs | 4 +- fuzzer/Cargo.lock | 2 +- fuzzer/src/py_export.rs | 2 +- vm/src/air_private_input.rs | 4 +- vm/src/air_public_input.rs | 4 +- vm/src/cairo_run.rs | 162 +- .../builtin_hint_processor/bigint.rs | 1 - .../builtin_hint_processor/dict_hint_utils.rs | 2 +- .../builtin_hint_processor/dict_manager.rs | 2 +- .../builtin_hint_processor/ec_recover.rs | 1 - .../builtin_hint_processor/hint_utils.rs | 8 +- .../builtin_hint_processor/keccak_utils.rs | 1 - .../builtin_hint_processor/poseidon_utils.rs | 1 - .../builtin_hint_processor/segments.rs | 1 - .../builtin_hint_processor/vrf/pack.rs | 1 - vm/src/hint_processor/hint_processor_utils.rs | 6 +- vm/src/tests/cairo_pie_test.rs | 26 +- vm/src/tests/cairo_run_test.rs | 28 +- vm/src/tests/mod.rs | 67 +- ...un_deprecated_contract_class_simplified.rs | 2 +- vm/src/utils.rs | 30 +- vm/src/vm/errors/vm_exception.rs | 133 +- vm/src/vm/hooks.rs | 66 +- vm/src/vm/runners/builtin_runner/bitwise.rs | 30 +- vm/src/vm/runners/builtin_runner/ec_op.rs | 25 +- vm/src/vm/runners/builtin_runner/hash.rs | 29 +- vm/src/vm/runners/builtin_runner/keccak.rs | 12 +- vm/src/vm/runners/builtin_runner/mod.rs | 35 +- vm/src/vm/runners/builtin_runner/modulo.rs | 27 +- vm/src/vm/runners/builtin_runner/output.rs | 5 +- vm/src/vm/runners/builtin_runner/poseidon.rs | 25 +- .../vm/runners/builtin_runner/range_check.rs | 30 +- .../runners/builtin_runner/segment_arena.rs | 1 - vm/src/vm/runners/builtin_runner/signature.rs | 1 - vm/src/vm/runners/cairo_pie.rs | 4 +- vm/src/vm/runners/cairo_runner.rs | 1837 ++++++++--------- vm/src/vm/security.rs | 109 +- vm/src/vm/vm_core.rs | 33 +- 45 files changed, 1377 insertions(+), 1556 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e2f10302..d85c85eea9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,56 @@ #### Upcoming Changes +* refactor(BREAKING): Move the VM back to the CairoRunner [#1743](https://github.com/lambdaclass/cairo-vm/pull/1743) + * `CairoRunner` has a new public field `vm: VirtualMachine` + * `CairoRunner` no longer derives `Debug` + * `CairoRunner` methods `new_v2` & `new` take an extra boolean argument `trace_enabled`. + * Functions `cairo_run` , `cairo_run_program` & `cairo_run_fuzzed_program` from `vm` crate and `cairo_run_program` from `cairo1-run` crate now retun only `CairoRunner` instead of `(CairoRunner, VirtualMachine)` + * `CairoRunner` methods no longer take a reference to `VirtualMachine`. Methods that took an immutable reference to self and a mutable reference to the VM now take a mutable reference to self. Affected methods: + * `initialize` + * `initialize_builtins` + * `initialize_all_builtins` + * `initialize_segments` + * `initialize_state` + * `initialize_function_entrypoint` + * `initialize_state` + * `initialize_main_entrypoint` + * `initialize_vm` + * `run_until_pc` + * `run_for_steps` + * `run_until_steps` + * `run_until_power_of_2` + * `get_perm_range_check_limits` + * `check_range_check_usage` + * `get_memory_holes` + * `check_diluted_check_usage` + * `end_run` + * `relocate_trace` + * `relocate_memory` + * `relocate` + * `get_builtin_segments_info` + * `get_builtin_segments_info_for_pie` + * `get_execution_resources` + * `finalize_segments` + * `run_from_entrypoint` + * `check_used_cells` + * `check_memory_usage` + * `initialize_function_runner_cairo_1` + * `initialize_function_runner` + * `read_return_values` + * `get_builtins_final_stack` + * `get_cairo_pie` + * `get_air_public_input` + * `get_air_private_input` + * `get_memory_segment_addresses` + * Functions & methods taking a reference to `CairoRunner` & `VirtualMachine` now only take a reference to `CairoRunner`: + * `start_tracer` + * `VmException::from_vm_error` + * `get_error_attr_value` + * `get_traceback` + * `verify_secure_runner` + * [hooks feature] `BeforeFirstStepHookFunc` dyn Fn no longer takes a mutable reference to `CairoRunner`, along with `VirtualMachine::execute_before_first_step`. + * fix: add support for arrays shorter than 2 as arguments for cairo1-run [#1737](https://github.com/lambdaclass/cairo-vm/pull/1737) * bugfix: Fix BuiltinRunner::final_stack for SegmentArena[#1747](https://github.com/lambdaclass/cairo-vm/pull/1747) diff --git a/README.md b/README.md index 8cf284b38b..1115281012 100644 --- a/README.md +++ b/README.md @@ -211,9 +211,7 @@ When running a Cairo program directly using the Cairo-vm repository you would fi 2. Instantiate the VM, the cairo_runner, the hint processor, and the entrypoint ```rust - let mut vm = VirtualMachine::new(false); - - let mut cairo_runner = CairoRunner::new(&program, LayoutName::all_cairo, false); + let mut cairo_runner = CairoRunner::new(&program, LayoutName::all_cairo, false, false); let mut hint_processor = BuiltinHintProcessor::new_empty(); @@ -226,8 +224,8 @@ When running a Cairo program directly using the Cairo-vm repository you would fi 3. Lastly, initialize the builtins and segments. ```rust - cairo_runner.initialize_builtins(&mut vm)?; - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_builtins(false)?; + cairo_runner.initialize_segments(None); ``` When using cairo-vm with the Starknet devnet there are additional parameters that are part of the OS context passed on to the `run_from_entrypoint` method that we do not have here when using it directly. These parameters are, for example, initial stacks of the builtins, which are the base of each of them and are needed as they are the implicit arguments of the function. @@ -240,7 +238,6 @@ When using cairo-vm with the Starknet devnet there are additional parameters tha &MaybeRelocatable::from((2,0)).into() //this would be the output_ptr for example if our cairo function uses it ], false, - &mut vm, &mut hint_processor, ); ``` diff --git a/bench/criterion_benchmark.rs b/bench/criterion_benchmark.rs index 64074ff121..7078ad21a8 100644 --- a/bench/criterion_benchmark.rs +++ b/bench/criterion_benchmark.rs @@ -1,6 +1,6 @@ use cairo_vm::{ types::{layout_name::LayoutName, program::Program}, - vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, + vm::runners::cairo_runner::CairoRunner, }; use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; @@ -31,6 +31,7 @@ fn build_many_runners(c: &mut Criterion) { black_box(&program), black_box(LayoutName::starknet_with_keccak), black_box(false), + black_box(false), ) .unwrap(), ); @@ -44,15 +45,8 @@ fn load_program_data(c: &mut Criterion) { let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap(); c.bench_function("initialize", |b| { b.iter_batched( - || { - ( - CairoRunner::new(&program, LayoutName::starknet_with_keccak, false).unwrap(), - VirtualMachine::new(false), - ) - }, - |(mut runner, mut vm)| { - _ = black_box(runner.initialize(black_box(&mut vm), false).unwrap()) - }, + || CairoRunner::new(&program, LayoutName::starknet_with_keccak, false, false).unwrap(), + |mut runner| _ = black_box(runner.initialize(false).unwrap()), BatchSize::SmallInput, ) }); diff --git a/bench/iai_benchmark.rs b/bench/iai_benchmark.rs index 650775f11e..b5bff69dce 100644 --- a/bench/iai_benchmark.rs +++ b/bench/iai_benchmark.rs @@ -3,7 +3,7 @@ use iai_callgrind::main; use cairo_vm::{ types::{layout_name::LayoutName, program::Program}, - vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, + vm::runners::cairo_runner::CairoRunner, }; use mimalloc::MiMalloc; @@ -31,26 +31,29 @@ fn parse_program_helper() -> Program { #[inline(never)] fn build_runner() { let program = parse_program_helper(); - let runner = - CairoRunner::new(black_box(&program), LayoutName::starknet_with_keccak, false).unwrap(); + let runner = CairoRunner::new( + black_box(&program), + LayoutName::starknet_with_keccak, + false, + false, + ) + .unwrap(); core::mem::drop(black_box(runner)); } #[export_name = "helper::build_runner"] #[inline(never)] -fn build_runner_helper() -> (CairoRunner, VirtualMachine) { +fn build_runner_helper() -> CairoRunner { //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, LayoutName::starknet_with_keccak, false).unwrap(); - let vm = VirtualMachine::new(false); - (runner, vm) + CairoRunner::new(&program, LayoutName::starknet_with_keccak, false, false).unwrap() } #[inline(never)] fn load_program_data() { - let (mut runner, mut vm) = build_runner_helper(); - _ = black_box(runner.initialize(black_box(&mut vm), false).unwrap()); + let mut runner = build_runner_helper(); + _ = black_box(runner.initialize(false).unwrap()); } main!( diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 376fd2e371..f213c2527e 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -15,8 +15,6 @@ use cairo_vm::vm::runners::cairo_pie::CairoPie; use cairo_vm::vm::runners::cairo_runner::CairoRunner; use cairo_vm::vm::runners::cairo_runner::RunResources; #[cfg(feature = "with_tracer")] -use cairo_vm::vm::vm_core::VirtualMachine; -#[cfg(feature = "with_tracer")] use cairo_vm_tracer::error::trace_data_errors::TraceDataError; #[cfg(feature = "with_tracer")] use cairo_vm_tracer::tracer::run_tracer; @@ -134,8 +132,9 @@ impl FileWriter { } #[cfg(feature = "with_tracer")] -fn start_tracer(cairo_runner: &CairoRunner, vm: &VirtualMachine) -> Result<(), TraceDataError> { - let relocation_table = vm +fn start_tracer(cairo_runner: &CairoRunner) -> Result<(), TraceDataError> { + let relocation_table = cairo_runner + .vm .relocate_segments() .map_err(TraceDataError::FailedToGetRelocationTable)?; let instruction_locations = cairo_runner @@ -174,7 +173,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { ..Default::default() }; - let (cairo_runner, mut vm) = match { + let mut cairo_runner = match { if args.run_from_cairo_pie { let pie = CairoPie::read_zip_file(&args.filename)?; let mut hint_processor = BuiltinHintProcessor::new( @@ -197,7 +196,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { if args.print_output { let mut output_buffer = "Program Output:\n".to_string(); - vm.write_output(&mut output_buffer)?; + cairo_runner.vm.write_output(&mut output_buffer)?; print!("{output_buffer}"); } @@ -225,13 +224,13 @@ fn run(args: impl Iterator) -> Result<(), Error> { } if let Some(file_path) = args.air_public_input { - let json = cairo_runner.get_air_public_input(&vm)?.serialize_json()?; + let json = cairo_runner.get_air_public_input()?.serialize_json()?; std::fs::write(file_path, json)?; } #[cfg(feature = "with_tracer")] if args.tracer { - start_tracer(&cairo_runner, &vm)?; + start_tracer(&cairo_runner)?; } if let (Some(file_path), Some(ref trace_file), Some(ref memory_file)) = @@ -252,7 +251,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { .to_string(); let json = cairo_runner - .get_air_private_input(&vm) + .get_air_private_input() .to_serializable(trace_path, memory_path) .serialize_json() .map_err(PublicInputError::Serde)?; @@ -262,7 +261,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { if let Some(ref file_name) = args.cairo_pie_output { let file_path = Path::new(file_name); cairo_runner - .get_cairo_pie(&vm) + .get_cairo_pie() .map_err(CairoRunError::Runner)? .write_zip_file(file_path)? } diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 5d4edfb799..691781bc3d 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -109,19 +109,11 @@ impl Default for Cairo1RunConfig<'_> { } // Runs a Cairo 1 program -// Returns the runner & VM after execution + the return values + the serialized return values (if serialize_output is enabled) +// Returns the runner after execution + the return values + the serialized return values (if serialize_output is enabled) pub fn cairo_run_program( sierra_program: &SierraProgram, cairo_run_config: Cairo1RunConfig, -) -> Result< - ( - CairoRunner, - VirtualMachine, - Vec, - Option, - ), - Error, -> { +) -> Result<(CairoRunner, Vec, Option), Error> { let metadata = calc_metadata_ap_change_only(sierra_program) .map_err(|_| VirtualMachineError::Unexpected)?; let sierra_program_registry = ProgramRegistry::::new(sierra_program)?; @@ -215,24 +207,28 @@ pub fn cairo_run_program( RunnerMode::ExecutionMode }; - let mut runner = CairoRunner::new_v2(&program, cairo_run_config.layout, runner_mode)?; - let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled); - let end = runner.initialize(&mut vm, cairo_run_config.proof_mode)?; + let mut runner = CairoRunner::new_v2( + &program, + cairo_run_config.layout, + runner_mode, + cairo_run_config.trace_enabled, + )?; + let end = runner.initialize(cairo_run_config.proof_mode)?; // Run it until the end / infinite loop in proof_mode - runner.run_until_pc(end, &mut vm, &mut hint_processor)?; + runner.run_until_pc(end, &mut hint_processor)?; if cairo_run_config.proof_mode { - runner.run_for_steps(1, &mut vm, &mut hint_processor)?; + runner.run_for_steps(1, &mut hint_processor)?; } - runner.end_run(false, false, &mut vm, &mut hint_processor)?; + runner.end_run(false, false, &mut hint_processor)?; let skip_output = cairo_run_config.proof_mode || cairo_run_config.append_return_values; // Fetch return values let return_values = fetch_return_values( return_type_size, return_type_id, - &vm, + &runner.vm, builtin_count, skip_output, )?; @@ -240,7 +236,7 @@ pub fn cairo_run_program( let serialized_output = if cairo_run_config.serialize_output { Some(serialize_output( &return_values, - &mut vm, + &mut runner.vm, return_type_id, &sierra_program_registry, &type_sizes, @@ -253,12 +249,15 @@ pub fn cairo_run_program( if cairo_run_config.finalize_builtins { if skip_output { // Set stop pointer for each builtin - vm.builtins_final_stack_from_stack_pointer_dict( + runner.vm.builtins_final_stack_from_stack_pointer_dict( &builtins .iter() .enumerate() .map(|(i, builtin)| { - (*builtin, (vm.get_ap() - (builtins.len() - 1 - i)).unwrap()) + ( + *builtin, + (runner.vm.get_ap() - (builtins.len() - 1 - i)).unwrap(), + ) }) .collect(), false, @@ -267,20 +266,20 @@ pub fn cairo_run_program( finalize_builtins( &main_func.signature.ret_types, &type_sizes, - &mut vm, + &mut runner.vm, builtin_count, )?; } // Build execution public memory if cairo_run_config.proof_mode { - runner.finalize_segments(&mut vm)?; + runner.finalize_segments()?; } } - runner.relocate(&mut vm, true)?; + runner.relocate(true)?; - Ok((runner, vm, return_values, serialized_output)) + Ok((runner, return_values, serialized_output)) } #[allow(clippy::type_complexity)] @@ -1217,7 +1216,7 @@ mod tests { ..Default::default() }; // Run program - let (_, vm, return_values, _) = + let (runner, return_values, _) = cairo_run_program(&sierra_program, cairo_run_config).unwrap(); // When the return type is a PanicResult, we remove the panic wrapper when returning the ret values // And handle the panics returning an error, so we need to add it here @@ -1230,14 +1229,16 @@ mod tests { }; // Check that the output segment contains the return values // The output builtin will always be the first builtin, so we know it's segment is 2 - let output_builtin_segment = vm + let output_builtin_segment = runner + .vm .get_continuous_range((2, 0).into(), return_values.len()) .unwrap(); // While this test can make sure that the return values are the same as the output segment values, as the code that fetches return values // takes them from the output segment we can't be sure that these return values are correct, for this we use the integration tests in the main.rs file assert_eq!(output_builtin_segment, return_values, "{}", filename); // Just for consistency, we will check that there are no values in the output segment after the return values - assert!(vm + assert!(runner + .vm .get_maybe(&Relocatable::from((2_isize, return_values.len()))) .is_none()); } diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index cd9d01b5ff..0657eaa38e 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -180,10 +180,10 @@ fn run(args: impl Iterator) -> Result, Error> { } }; - let (runner, vm, _, serialized_output) = cairo_run_program(&sierra_program, cairo_run_config)?; + let (runner, _, serialized_output) = cairo_run_program(&sierra_program, cairo_run_config)?; if let Some(file_path) = args.air_public_input { - let json = runner.get_air_public_input(&vm)?.serialize_json()?; + let json = runner.get_air_public_input()?.serialize_json()?; std::fs::write(file_path, json)?; } @@ -207,7 +207,7 @@ fn run(args: impl Iterator) -> Result, Error> { .to_string(); let json = runner - .get_air_private_input(&vm) + .get_air_private_input() .to_serializable(trace_path, memory_path) .serialize_json() .map_err(PublicInputError::Serde)?; @@ -215,7 +215,7 @@ fn run(args: impl Iterator) -> Result, Error> { } if let Some(ref file_path) = args.cairo_pie_output { - runner.get_cairo_pie(&vm)?.write_zip_file(file_path)? + runner.get_cairo_pie()?.write_zip_file(file_path)? } if let Some(trace_path) = args.trace_file { diff --git a/examples/wasm-demo/src/lib.rs b/examples/wasm-demo/src/lib.rs index dc5b197430..fc126f9499 100644 --- a/examples/wasm-demo/src/lib.rs +++ b/examples/wasm-demo/src/lib.rs @@ -39,7 +39,7 @@ pub fn run_cairo_program() -> Result { ..Default::default() }; - let (_runner, mut vm) = wrap_error!(cairo_run( + let mut runner = wrap_error!(cairo_run( PROGRAM_JSON, &cairo_run_config, &mut hint_executor @@ -47,7 +47,7 @@ pub fn run_cairo_program() -> Result { let mut buffer = String::new(); - wrap_error!(vm.write_output(&mut buffer))?; + wrap_error!(runner.vm.write_output(&mut buffer))?; log(buffer.as_str()); diff --git a/fuzzer/Cargo.lock b/fuzzer/Cargo.lock index 33f13e1caf..31e011a071 100644 --- a/fuzzer/Cargo.lock +++ b/fuzzer/Cargo.lock @@ -210,7 +210,7 @@ dependencies = [ [[package]] name = "cairo-vm" -version = "1.0.0-rc1" +version = "1.0.0-rc2" dependencies = [ "anyhow", "arbitrary", diff --git a/fuzzer/src/py_export.rs b/fuzzer/src/py_export.rs index 332035c009..8af6e9d084 100644 --- a/fuzzer/src/py_export.rs +++ b/fuzzer/src/py_export.rs @@ -40,7 +40,7 @@ fn cairo_run_dump_mem(json: String) -> PyResult> { PanicTriggered::new_err(format! {"Rust VM panicked! {:?}", e.downcast::()}) })?; - let (cairo_runner, _) = + let cairo_runner = result_no_panic.map_err(|e| VMError::new_err(format! {"VM error: {:?}", e.to_string()}))?; let mut memory_dump = Vec::new(); diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index be3bceedab..fa05cc86d5 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -295,8 +295,8 @@ mod tests { 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(); - let public_input = runner.get_air_private_input(&vm); + let runner = 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(); + let public_input = runner.get_air_private_input(); let serialized_public_input = public_input.to_serializable("/dev/null".to_string(), "/dev/null".to_string()); assert!(serialized_public_input.pedersen.is_some()); diff --git a/vm/src/air_public_input.rs b/vm/src/air_public_input.rs index d40bfc6df7..a2e19f56b7 100644 --- a/vm/src/air_public_input.rs +++ b/vm/src/air_public_input.rs @@ -198,8 +198,8 @@ mod tests { 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(); - let public_input = runner.get_air_public_input(&vm).unwrap(); + let runner = crate::cairo_run::cairo_run(program_content, &config, &mut crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor::new_empty()).unwrap(); + let public_input = runner.get_air_public_input().unwrap(); // We already know serialization works as expected due to the comparison against python VM let serialized_public_input = public_input.serialize_json().unwrap(); let deserialized_public_input: PublicInput = diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index b43b6f4187..d48f7c5552 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -7,7 +7,6 @@ use crate::{ }, runners::{cairo_pie::CairoPie, cairo_runner::CairoRunner}, security::verify_secure_runner, - vm_core::VirtualMachine, }, }; @@ -50,8 +49,8 @@ impl<'a> Default for CairoRunConfig<'a> { pub fn cairo_run_program( program: &Program, cairo_run_config: &CairoRunConfig, - hint_executor: &mut dyn HintProcessor, -) -> Result<(CairoRunner, VirtualMachine), CairoRunError> { + hint_processor: &mut dyn HintProcessor, +) -> Result { let secure_run = cairo_run_config .secure_run .unwrap_or(!cairo_run_config.proof_mode); @@ -64,47 +63,46 @@ pub fn cairo_run_program( program, cairo_run_config.layout, cairo_run_config.proof_mode, + cairo_run_config.trace_enabled, )?; - let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled); - let end = cairo_runner.initialize(&mut vm, allow_missing_builtins)?; + let end = cairo_runner.initialize(allow_missing_builtins)?; // check step calculation cairo_runner - .run_until_pc(end, &mut vm, hint_executor) - .map_err(|err| VmException::from_vm_error(&cairo_runner, &vm, err))?; + .run_until_pc(end, hint_processor) + .map_err(|err| VmException::from_vm_error(&cairo_runner, err))?; if cairo_run_config.proof_mode { - cairo_runner.run_for_steps(1, &mut vm, hint_executor)?; + cairo_runner.run_for_steps(1, hint_processor)?; } cairo_runner.end_run( cairo_run_config.disable_trace_padding, false, - &mut vm, - hint_executor, + hint_processor, )?; - vm.verify_auto_deductions()?; - cairo_runner.read_return_values(&mut vm, allow_missing_builtins)?; + cairo_runner.vm.verify_auto_deductions()?; + cairo_runner.read_return_values(allow_missing_builtins)?; if cairo_run_config.proof_mode { - cairo_runner.finalize_segments(&mut vm)?; + cairo_runner.finalize_segments()?; } if secure_run { - verify_secure_runner(&cairo_runner, true, None, &mut vm)?; + verify_secure_runner(&cairo_runner, true, None)?; } - cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?; + cairo_runner.relocate(cairo_run_config.relocate_mem)?; - Ok((cairo_runner, vm)) + Ok(cairo_runner) } pub fn cairo_run( program_content: &[u8], cairo_run_config: &CairoRunConfig, - hint_executor: &mut dyn HintProcessor, -) -> Result<(CairoRunner, VirtualMachine), CairoRunError> { + hint_processor: &mut dyn HintProcessor, +) -> Result { let program = Program::from_bytes(program_content, Some(cairo_run_config.entrypoint))?; - cairo_run_program(&program, cairo_run_config, hint_executor) + cairo_run_program(&program, cairo_run_config, hint_processor) } /// Runs a Cairo PIE generated by a previous cairo execution /// To generate a cairo pie use the runner's method `get_cairo_pie` @@ -116,7 +114,7 @@ pub fn cairo_run_pie( pie: &CairoPie, cairo_run_config: &CairoRunConfig, hint_processor: &mut dyn HintProcessor, -) -> Result<(CairoRunner, VirtualMachine), CairoRunError> { +) -> Result { if cairo_run_config.proof_mode { return Err(RunnerError::CairoPieProofMode.into()); } @@ -132,58 +130,67 @@ pub fn cairo_run_pie( let allow_missing_builtins = cairo_run_config.allow_missing_builtins.unwrap_or_default(); let program = Program::from_stripped_program(&pie.metadata.program); - let mut cairo_runner = CairoRunner::new(&program, cairo_run_config.layout, false)?; + let mut cairo_runner = CairoRunner::new( + &program, + cairo_run_config.layout, + false, + cairo_run_config.trace_enabled, + )?; - let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled); - let end = cairo_runner.initialize(&mut vm, allow_missing_builtins)?; - vm.finalize_segments_by_cairo_pie(pie); + let end = cairo_runner.initialize(allow_missing_builtins)?; + cairo_runner.vm.finalize_segments_by_cairo_pie(pie); // Load builtin additional data for (name, data) in pie.additional_data.0.iter() { // Data is not trusted in secure_run, therefore we skip extending the hash builtin's data if matches!(name, BuiltinName::pedersen) && secure_run { continue; } - if let Some(builtin) = vm.builtin_runners.iter_mut().find(|b| b.name() == *name) { + if let Some(builtin) = cairo_runner + .vm + .builtin_runners + .iter_mut() + .find(|b| b.name() == *name) + { builtin.extend_additional_data(data)?; } } // Load previous execution memory let n_extra_segments = pie.metadata.extra_segments.len(); - vm.segments.load_pie_memory(&pie.memory, n_extra_segments)?; + cairo_runner + .vm + .segments + .load_pie_memory(&pie.memory, n_extra_segments)?; cairo_runner - .run_until_pc(end, &mut vm, hint_processor) - .map_err(|err| VmException::from_vm_error(&cairo_runner, &vm, err))?; + .run_until_pc(end, hint_processor) + .map_err(|err| VmException::from_vm_error(&cairo_runner, err))?; cairo_runner.end_run( cairo_run_config.disable_trace_padding, false, - &mut vm, hint_processor, )?; - vm.verify_auto_deductions()?; - cairo_runner.read_return_values(&mut vm, allow_missing_builtins)?; + cairo_runner.vm.verify_auto_deductions()?; + cairo_runner.read_return_values(allow_missing_builtins)?; if secure_run { - verify_secure_runner(&cairo_runner, true, None, &mut vm)?; + verify_secure_runner(&cairo_runner, true, None)?; // Check that the Cairo PIE produced by this run is compatible with the Cairo PIE received - cairo_runner - .get_cairo_pie(&vm)? - .check_pie_compatibility(pie)?; + cairo_runner.get_cairo_pie()?.check_pie_compatibility(pie)?; } - cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?; + cairo_runner.relocate(cairo_run_config.relocate_mem)?; - Ok((cairo_runner, vm)) + Ok(cairo_runner) } #[cfg(feature = "test_utils")] pub fn cairo_run_fuzzed_program( program: Program, cairo_run_config: &CairoRunConfig, - hint_executor: &mut dyn HintProcessor, + hint_processor: &mut dyn HintProcessor, steps_limit: usize, -) -> Result<(CairoRunner, VirtualMachine), CairoRunError> { +) -> Result { use crate::vm::errors::vm_errors::VirtualMachineError; let secure_run = cairo_run_config @@ -198,32 +205,31 @@ pub fn cairo_run_fuzzed_program( &program, cairo_run_config.layout, cairo_run_config.proof_mode, + cairo_run_config.trace_enabled, )?; - let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled); + let _end = cairo_runner.initialize(allow_missing_builtins)?; - let _end = cairo_runner.initialize(&mut vm, allow_missing_builtins)?; - - let res = match cairo_runner.run_until_steps(steps_limit, &mut vm, hint_executor) { + let res = match cairo_runner.run_until_steps(steps_limit, hint_processor) { Err(VirtualMachineError::EndOfProgram(_remaining)) => Ok(()), // program ran OK but ended before steps limit res => res, }; - res.map_err(|err| VmException::from_vm_error(&cairo_runner, &vm, err))?; + res.map_err(|err| VmException::from_vm_error(&cairo_runner, err))?; - cairo_runner.end_run(false, false, &mut vm, hint_executor)?; + cairo_runner.end_run(false, false, hint_processor)?; - vm.verify_auto_deductions()?; - cairo_runner.read_return_values(&mut vm, allow_missing_builtins)?; + cairo_runner.vm.verify_auto_deductions()?; + cairo_runner.read_return_values(allow_missing_builtins)?; if cairo_run_config.proof_mode { - cairo_runner.finalize_segments(&mut vm)?; + cairo_runner.finalize_segments()?; } if secure_run { - verify_secure_runner(&cairo_runner, true, None, &mut vm)?; + verify_secure_runner(&cairo_runner, true, None)?; } - cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?; + cairo_runner.relocate(cairo_run_config.relocate_mem)?; - Ok((cairo_runner, vm)) + Ok(cairo_runner) } #[derive(Debug, Error)] @@ -296,19 +302,16 @@ mod tests { fn run_test_program( program_content: &[u8], hint_processor: &mut dyn HintProcessor, - ) -> Result<(CairoRunner, VirtualMachine), CairoRunError> { + ) -> Result { let program = Program::from_bytes(program_content, Some("main")).unwrap(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); let end = cairo_runner - .initialize(&mut vm, false) + .initialize(false) .map_err(CairoRunError::Runner)?; - assert!(cairo_runner - .run_until_pc(end, &mut vm, hint_processor) - .is_ok()); + assert!(cairo_runner.run_until_pc(end, hint_processor).is_ok()); - Ok((cairo_runner, vm)) + Ok(cairo_runner) } #[test] @@ -319,15 +322,12 @@ mod tests { Some("not_main"), ) .unwrap(); - let mut vm = vm!(); let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_ok()); - assert!(cairo_runner.relocate(&mut vm, true).is_ok()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok()); + assert!(cairo_runner.relocate(true).is_ok()); // `main` returns without doing nothing, but `not_main` sets `[ap]` to `1` // Memory location was found empirically and simply hardcoded assert_eq!(cairo_runner.relocated_memory[2], Some(Felt252::from(123))); @@ -374,11 +374,11 @@ mod tests { fn write_output_program() { let program_content = include_bytes!("../../cairo_programs/bitwise_output.json"); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let (_, mut vm) = run_test_program(program_content, &mut hint_processor) + let mut runner = run_test_program(program_content, &mut hint_processor) .expect("Couldn't initialize cairo runner"); let mut output_buffer = String::new(); - vm.write_output(&mut output_buffer).unwrap(); + runner.vm.write_output(&mut output_buffer).unwrap(); assert_eq!(&output_buffer, "0\n"); } @@ -391,11 +391,9 @@ mod tests { // run test program until the end let mut hint_processor = BuiltinHintProcessor::new_empty(); - let cairo_runner_result = run_test_program(program_content, &mut hint_processor); - let (mut cairo_runner, mut vm) = cairo_runner_result.unwrap(); + let mut cairo_runner = run_test_program(program_content, &mut hint_processor).unwrap(); - // relocate memory so we can dump it to file - assert!(cairo_runner.relocate(&mut vm, false).is_ok()); + assert!(cairo_runner.relocate(false).is_ok()); let trace_entries = cairo_runner.relocated_trace.unwrap(); let mut buffer = [0; 24]; @@ -416,11 +414,10 @@ mod tests { // run test program until the end let mut hint_processor = BuiltinHintProcessor::new_empty(); - let cairo_runner_result = run_test_program(program_content, &mut hint_processor); - let (mut cairo_runner, mut vm) = cairo_runner_result.unwrap(); + let mut cairo_runner = run_test_program(program_content, &mut hint_processor).unwrap(); // relocate memory so we can dump it to file - assert!(cairo_runner.relocate(&mut vm, true).is_ok()); + assert!(cairo_runner.relocate(true).is_ok()); let mut buffer = [0; 120]; let mut buff_writer = SliceWriter::new(&mut buffer); @@ -442,12 +439,9 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_ok()); - assert!(cairo_runner.relocate(&mut vm, false).is_ok()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok()); + assert!(cairo_runner.relocate(false).is_ok()); assert!(cairo_runner.relocated_trace.is_none()); } @@ -465,13 +459,13 @@ mod tests { }; // First run program to get Cairo PIE let cairo_pie = { - let (runner, vm) = cairo_run( + let runner = cairo_run( program_content, &cairo_run_config, &mut BuiltinHintProcessor::new_empty(), ) .unwrap(); - runner.get_cairo_pie(&vm).unwrap() + runner.get_cairo_pie().unwrap() }; let mut hint_processor = BuiltinHintProcessor::new( Default::default(), @@ -485,13 +479,13 @@ mod tests { fn cairo_run_pie_n_steps_not_set() { // First run program to get Cairo PIE let cairo_pie = { - let (runner, vm) = cairo_run( + let runner = cairo_run( include_bytes!("../../cairo_programs/fibonacci.json"), &CairoRunConfig::default(), &mut BuiltinHintProcessor::new_empty(), ) .unwrap(); - runner.get_cairo_pie(&vm).unwrap() + runner.get_cairo_pie().unwrap() }; // Run Cairo PIE let res = cairo_run_pie( diff --git a/vm/src/hint_processor/builtin_hint_processor/bigint.rs b/vm/src/hint_processor/builtin_hint_processor/bigint.rs index ff5326bd58..0c14aab3cd 100644 --- a/vm/src/hint_processor/builtin_hint_processor/bigint.rs +++ b/vm/src/hint_processor/builtin_hint_processor/bigint.rs @@ -105,7 +105,6 @@ mod test { use crate::hint_processor::hint_processor_definition::{HintProcessorLogic, HintReference}; use crate::types::exec_scope::ExecutionScopes; use crate::utils::test_utils::*; - use crate::vm::vm_core::VirtualMachine; use assert_matches::assert_matches; use num_bigint::BigInt; diff --git a/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs index 42c7bd37c8..8e8ae9271b 100644 --- a/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs @@ -268,7 +268,7 @@ mod tests { relocatable, types::relocatable::{MaybeRelocatable, Relocatable}, utils::test_utils::*, - vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, + vm::errors::memory_errors::MemoryError, }; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/dict_manager.rs b/vm/src/hint_processor/builtin_hint_processor/dict_manager.rs index 29d73a4a32..30429278db 100644 --- a/vm/src/hint_processor/builtin_hint_processor/dict_manager.rs +++ b/vm/src/hint_processor/builtin_hint_processor/dict_manager.rs @@ -205,7 +205,7 @@ impl DictTracker { #[cfg(test)] mod tests { use super::*; - use crate::{relocatable, utils::test_utils::*, vm::vm_core::VirtualMachine}; + use crate::{relocatable, utils::test_utils::*}; use assert_matches::assert_matches; #[cfg(target_arch = "wasm32")] diff --git a/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs b/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs index dec7784461..e92bc29c8b 100644 --- a/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/vm/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -131,7 +131,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::hint_processor_definition::HintReference; use crate::utils::test_utils::*; - use crate::vm::vm_core::VirtualMachine; use crate::{ any_box, hint_processor::{ diff --git a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs index 6e0a0ee12f..74caa13473 100644 --- a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -135,11 +135,9 @@ mod tests { use super::*; use crate::{ - hint_processor::hint_processor_definition::HintReference, - relocatable, - serde::deserialize_program::OffsetValue, - utils::test_utils::*, - vm::{vm_core::VirtualMachine, vm_memory::memory::Memory}, + hint_processor::hint_processor_definition::HintReference, relocatable, + serde::deserialize_program::OffsetValue, utils::test_utils::*, + vm::vm_memory::memory::Memory, }; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs index 69b593e36f..b348df8a04 100644 --- a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs @@ -298,7 +298,6 @@ mod tests { hint_processor_definition::{HintProcessorLogic, HintReference}, }, utils::test_utils::*, - vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs b/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs index 1cc19daa4d..4a8f58f769 100644 --- a/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs @@ -57,7 +57,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; use crate::hint_processor::hint_processor_definition::HintReference; - use crate::vm::vm_core::VirtualMachine; use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*}; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/segments.rs b/vm/src/hint_processor/builtin_hint_processor/segments.rs index 36576973d9..c996c2f9b1 100644 --- a/vm/src/hint_processor/builtin_hint_processor/segments.rs +++ b/vm/src/hint_processor/builtin_hint_processor/segments.rs @@ -58,7 +58,6 @@ mod tests { hint_processor_definition::HintProcessorLogic, }, utils::test_utils::*, - vm::vm_core::VirtualMachine, }; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs b/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs index b5d0dc1f8d..e092fb5c1f 100644 --- a/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs +++ b/vm/src/hint_processor/builtin_hint_processor/vrf/pack.rs @@ -84,7 +84,6 @@ mod test { use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; use crate::utils::test_utils::*; - use crate::vm::vm_core::VirtualMachine; use num_bigint::BigInt; use num_traits::One; use num_traits::Zero; diff --git a/vm/src/hint_processor/hint_processor_utils.rs b/vm/src/hint_processor/hint_processor_utils.rs index af83fcd57a..8a0a3e9e58 100644 --- a/vm/src/hint_processor/hint_processor_utils.rs +++ b/vm/src/hint_processor/hint_processor_utils.rs @@ -164,11 +164,7 @@ fn get_offset_value( mod tests { use super::*; - use crate::{ - relocatable, - utils::test_utils::*, - vm::{vm_core::VirtualMachine, vm_memory::memory::Memory}, - }; + use crate::{relocatable, utils::test_utils::*, vm::vm_memory::memory::Memory}; use assert_matches::assert_matches; #[cfg(target_arch = "wasm32")] diff --git a/vm/src/tests/cairo_pie_test.rs b/vm/src/tests/cairo_pie_test.rs index 6b5b6f8e70..7701d66057 100644 --- a/vm/src/tests/cairo_pie_test.rs +++ b/vm/src/tests/cairo_pie_test.rs @@ -46,9 +46,9 @@ fn pedersen_test() { &mut hint_processor, ); assert!(result.is_ok()); - let (runner, vm) = result.unwrap(); + let runner = result.unwrap(); // Obtain the pie - let result = runner.get_cairo_pie(&vm); + let result = runner.get_cairo_pie(); assert!(result.is_ok()); let cairo_pie = result.unwrap(); // Check pie values @@ -109,7 +109,7 @@ fn pedersen_test() { // memory assert_eq!( cairo_pie.memory, - Into::::into(&vm.segments.memory) + Into::::into(&runner.vm.segments.memory) ); } @@ -128,9 +128,9 @@ fn common_signature() { &mut hint_processor, ); assert!(result.is_ok()); - let (runner, vm) = result.unwrap(); + let runner = result.unwrap(); // Obtain the pie - let result = runner.get_cairo_pie(&vm); + let result = runner.get_cairo_pie(); assert!(result.is_ok()); let cairo_pie = result.unwrap(); // Check pie values @@ -184,7 +184,7 @@ fn common_signature() { // memory assert_eq!( cairo_pie.memory, - Into::::into(&vm.segments.memory) + Into::::into(&runner.vm.segments.memory) ); } @@ -203,9 +203,9 @@ fn relocate_segments() { &mut hint_processor, ); assert!(result.is_ok()); - let (runner, vm) = result.unwrap(); + let runner = result.unwrap(); // Obtain the pie - let result = runner.get_cairo_pie(&vm); + let result = runner.get_cairo_pie(); assert!(result.is_ok()); let cairo_pie = result.unwrap(); // Check pie values @@ -243,7 +243,7 @@ fn relocate_segments() { // memory assert_eq!( cairo_pie.memory, - Into::::into(&vm.segments.memory) + Into::::into(&runner.vm.segments.memory) ); } @@ -262,9 +262,9 @@ fn serialize_cairo_pie() { &mut hint_processor, ); assert!(result.is_ok()); - let (runner, vm) = result.unwrap(); + let runner = result.unwrap(); // Obtain the pie - let result = runner.get_cairo_pie(&vm); + let result = runner.get_cairo_pie(); assert!(result.is_ok()); let cairo_pie = result.unwrap(); @@ -281,7 +281,7 @@ fn run_pie_validity_checks_integration() { // Run the program let program_content = include_bytes!("../../../cairo_programs/integration.json"); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let (runner, vm) = cairo_run( + let runner = cairo_run( program_content, &CairoRunConfig { layout: LayoutName::all_cairo, @@ -291,6 +291,6 @@ fn run_pie_validity_checks_integration() { ) .expect("cairo_run failure"); // Obtain the pie - let cairo_pie = runner.get_cairo_pie(&vm).expect("Failed to get pie"); + let cairo_pie = runner.get_cairo_pie().expect("Failed to get pie"); assert!(cairo_pie.run_validity_checks().is_ok()) } diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index 4fb9b4d691..ed6de4384e 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -5,7 +5,6 @@ use crate::{ vm::{ runners::{builtin_runner::BuiltinRunner, cairo_runner::CairoRunner}, security::verify_secure_runner, - vm_core::VirtualMachine, }, }; @@ -1012,8 +1011,8 @@ fn fibonacci_proof_mode_disable_trace_padding() { ..Default::default() }; let mut hint_processor = BuiltinHintProcessor::new_empty(); - let (r, v) = cairo_run(program_data, &config, &mut hint_processor).unwrap(); - assert!(r.get_memory_holes(&v).unwrap().is_zero()); + let runner = cairo_run(program_data, &config, &mut hint_processor).unwrap(); + assert!(runner.get_memory_holes().unwrap().is_zero()); } #[test] @@ -1177,43 +1176,38 @@ fn run_program_with_custom_mod_builtin_params( &program, cairo_run_config.layout, cairo_run_config.proof_mode, + cairo_run_config.trace_enabled, ) .unwrap(); - let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); // Modify add_mod & mul_mod params - for runner in vm.get_builtin_runners_as_mut() { + for runner in cairo_runner.vm.get_builtin_runners_as_mut() { if let BuiltinRunner::Mod(runner) = runner { runner.override_layout_params(batch_size, word_bit_len) } } - cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .unwrap(); + cairo_runner.run_until_pc(end, &mut hint_processor).unwrap(); if cairo_run_config.proof_mode { - cairo_runner - .run_for_steps(1, &mut vm, &mut hint_processor) - .unwrap(); + cairo_runner.run_for_steps(1, &mut hint_processor).unwrap(); } cairo_runner .end_run( cairo_run_config.disable_trace_padding, false, - &mut vm, &mut hint_processor, ) .unwrap(); - vm.verify_auto_deductions().unwrap(); - cairo_runner.read_return_values(&mut vm, false).unwrap(); + cairo_runner.vm.verify_auto_deductions().unwrap(); + cairo_runner.read_return_values(false).unwrap(); if cairo_run_config.proof_mode { - cairo_runner.finalize_segments(&mut vm).unwrap(); + cairo_runner.finalize_segments().unwrap(); } if !cairo_run_config.proof_mode { - let security_res = verify_secure_runner(&cairo_runner, true, None, &mut vm); + let security_res = verify_secure_runner(&cairo_runner, true, None); if let Some(error) = security_error { assert!(security_res.is_err()); assert!(security_res.err().unwrap().to_string().contains(error)); diff --git a/vm/src/tests/mod.rs b/vm/src/tests/mod.rs index 7f3fa1cbd9..d1558730c1 100644 --- a/vm/src/tests/mod.rs +++ b/vm/src/tests/mod.rs @@ -10,10 +10,7 @@ use crate::Felt252; use crate::{ hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor, types::{builtin_name::BuiltinName, relocatable::MaybeRelocatable}, - vm::{ - runners::cairo_runner::{CairoArg, CairoRunner}, - vm_core::VirtualMachine, - }, + vm::runners::cairo_runner::{CairoArg, CairoRunner}, }; #[cfg(feature = "cairo-1-hints")] use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; @@ -85,7 +82,7 @@ fn run_program( assert!(res.err().unwrap().to_string().contains(error)); return; } - let (runner, _) = res.expect("Execution failed"); + let runner = res.expect("Execution failed"); if let Some(trace) = trace { let expected_trace: Vec<_> = trace .iter() @@ -117,21 +114,22 @@ fn run_cairo_1_entrypoint( &(contract_class.clone().try_into().unwrap()), LayoutName::all_cairo, false, + false, ) .unwrap(); - let mut vm = VirtualMachine::new(false); let program_builtins = get_casm_contract_builtins(&contract_class, entrypoint_offset); runner - .initialize_function_runner_cairo_1(&mut vm, &program_builtins) + .initialize_function_runner_cairo_1(&program_builtins) .unwrap(); // Implicit Args - let syscall_segment = MaybeRelocatable::from(vm.add_memory_segment()); + let syscall_segment = MaybeRelocatable::from(runner.vm.add_memory_segment()); let builtins = runner.get_program_builtins(); - let builtin_segment: Vec = vm + let builtin_segment: Vec = runner + .vm .get_builtin_runners() .iter() .filter(|b| builtins.contains(&b.name())) @@ -149,20 +147,25 @@ fn run_cairo_1_entrypoint( // Load builtin costs let builtin_costs: Vec = vec![0.into(), 0.into(), 0.into(), 0.into(), 0.into()]; - let builtin_costs_ptr = vm.add_memory_segment(); - vm.load_data(builtin_costs_ptr, &builtin_costs).unwrap(); + let builtin_costs_ptr = runner.vm.add_memory_segment(); + runner + .vm + .load_data(builtin_costs_ptr, &builtin_costs) + .unwrap(); // Load extra data let core_program_end_ptr = (runner.program_base.unwrap() + runner.program.shared_program_data.data.len()).unwrap(); let program_extra_data: Vec = vec![0x208B7FFF7FFF7FFE.into(), builtin_costs_ptr.into()]; - vm.load_data(core_program_end_ptr, &program_extra_data) + runner + .vm + .load_data(core_program_end_ptr, &program_extra_data) .unwrap(); // Load calldata - let calldata_start = vm.add_memory_segment(); - let calldata_end = vm.load_data(calldata_start, &args.to_vec()).unwrap(); + let calldata_start = runner.vm.add_memory_segment(); + let calldata_end = runner.vm.load_data(calldata_start, &args.to_vec()).unwrap(); // Create entrypoint_args @@ -184,16 +187,16 @@ fn run_cairo_1_entrypoint( &entrypoint_args, true, Some(runner.program.shared_program_data.data.len() + program_extra_data.len()), - &mut vm, &mut hint_processor, ) .unwrap(); // Check return values - let return_values = vm.get_return_values(5).unwrap(); + let return_values = runner.vm.get_return_values(5).unwrap(); let retdata_start = return_values[3].get_relocatable().unwrap(); let retdata_end = return_values[4].get_relocatable().unwrap(); - let retdata: Vec = vm + let retdata: Vec = runner + .vm .get_integer_range(retdata_start, (retdata_end - retdata_start).unwrap()) .unwrap() .iter() @@ -215,21 +218,22 @@ fn run_cairo_1_entrypoint_with_run_resources( &(contract_class.clone().try_into().unwrap()), LayoutName::all_cairo, false, + false, ) .unwrap(); - let mut vm = VirtualMachine::new(false); let program_builtins = get_casm_contract_builtins(&contract_class, entrypoint_offset); runner - .initialize_function_runner_cairo_1(&mut vm, &program_builtins) + .initialize_function_runner_cairo_1(&program_builtins) .unwrap(); // Implicit Args - let syscall_segment = MaybeRelocatable::from(vm.add_memory_segment()); + let syscall_segment = MaybeRelocatable::from(runner.vm.add_memory_segment()); let builtins = runner.get_program_builtins(); - let builtin_segment: Vec = vm + let builtin_segment: Vec = runner + .vm .get_builtin_runners() .iter() .filter(|b| builtins.contains(&b.name())) @@ -247,20 +251,25 @@ fn run_cairo_1_entrypoint_with_run_resources( // Load builtin costs let builtin_costs: Vec = vec![0.into(), 0.into(), 0.into(), 0.into(), 0.into()]; - let builtin_costs_ptr = vm.add_memory_segment(); - vm.load_data(builtin_costs_ptr, &builtin_costs).unwrap(); + let builtin_costs_ptr = runner.vm.add_memory_segment(); + runner + .vm + .load_data(builtin_costs_ptr, &builtin_costs) + .unwrap(); // Load extra data let core_program_end_ptr = (runner.program_base.unwrap() + runner.program.shared_program_data.data.len()).unwrap(); let program_extra_data: Vec = vec![0x208B7FFF7FFF7FFE.into(), builtin_costs_ptr.into()]; - vm.load_data(core_program_end_ptr, &program_extra_data) + runner + .vm + .load_data(core_program_end_ptr, &program_extra_data) .unwrap(); // Load calldata - let calldata_start = vm.add_memory_segment(); - let calldata_end = vm.load_data(calldata_start, &args.to_vec()).unwrap(); + let calldata_start = runner.vm.add_memory_segment(); + let calldata_end = runner.vm.load_data(calldata_start, &args.to_vec()).unwrap(); // Create entrypoint_args @@ -281,15 +290,15 @@ fn run_cairo_1_entrypoint_with_run_resources( &entrypoint_args, true, Some(runner.program.shared_program_data.data.len() + program_extra_data.len()), - &mut vm, hint_processor, )?; // Check return values - let return_values = vm.get_return_values(5).unwrap(); + let return_values = runner.vm.get_return_values(5).unwrap(); let retdata_start = return_values[3].get_relocatable().unwrap(); let retdata_end = return_values[4].get_relocatable().unwrap(); - let retdata: Vec = vm + let retdata: Vec = runner + .vm .get_integer_range(retdata_start, (retdata_end - retdata_start).unwrap()) .unwrap() .iter() diff --git a/vm/src/tests/run_deprecated_contract_class_simplified.rs b/vm/src/tests/run_deprecated_contract_class_simplified.rs index 6a46e46cb4..c097cf7428 100644 --- a/vm/src/tests/run_deprecated_contract_class_simplified.rs +++ b/vm/src/tests/run_deprecated_contract_class_simplified.rs @@ -326,7 +326,7 @@ fn run_deprecated_cc() { let mut hint_processor = SimplifiedOsHintProcessor::default(); let program_content = include_bytes!("../../../cairo_programs/noretrocompat/starknet_os_deprecated_cc.json"); - let (runner, _) = cairo_run( + let runner = cairo_run( program_content, &CairoRunConfig { trace_enabled: true, diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 78b7148955..070efcaf6d 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -249,21 +249,35 @@ pub mod test_utils { macro_rules! cairo_runner { ($program:expr) => { - CairoRunner::new( + crate::vm::runners::cairo_runner::CairoRunner::new( &$program, crate::types::layout_name::LayoutName::all_cairo, false, + false, ) .unwrap() }; ($program:expr, $layout:expr) => { - CairoRunner::new(&$program, $layout, false).unwrap() + crate::vm::runners::cairo_runner::CairoRunner::new(&$program, $layout, false, false) + .unwrap() }; ($program:expr, $layout:expr, $proof_mode:expr) => { - CairoRunner::new(&$program, $layout, $proof_mode).unwrap() + crate::vm::runners::cairo_runner::CairoRunner::new( + &$program, + $layout, + $proof_mode, + false, + ) + .unwrap() }; - ($program:expr, $layout:expr, $proof_mode:expr) => { - CairoRunner::new(&program, $layout.to_string(), proof_mode).unwrap() + ($program:expr, $layout:expr, $proof_mode:expr, $trace_enabled:expr) => { + crate::vm::runners::cairo_runner::CairoRunner::new( + &$program, + $layout, + $proof_mode, + $trace_enabled, + ) + .unwrap() }; } pub(crate) use cairo_runner; @@ -386,11 +400,11 @@ pub mod test_utils { macro_rules! vm { () => {{ - VirtualMachine::new(false) + crate::vm::vm_core::VirtualMachine::new(false) }}; ($use_trace:expr) => {{ - VirtualMachine::new($use_trace) + crate::vm::vm_core::VirtualMachine::new($use_trace) }}; } pub(crate) use vm; @@ -639,7 +653,7 @@ mod test { serde::deserialize_program::ReferenceManager, types::{exec_scope::ExecutionScopes, program::Program, relocatable::MaybeRelocatable}, utils::test_utils::*, - vm::{trace::trace_entry::TraceEntry, vm_core::VirtualMachine, vm_memory::memory::Memory}, + vm::{trace::trace_entry::TraceEntry, vm_memory::memory::Memory}, }; #[cfg(target_arch = "wasm32")] diff --git a/vm/src/vm/errors/vm_exception.rs b/vm/src/vm/errors/vm_exception.rs index 05077525d5..5a31d6669c 100644 --- a/vm/src/vm/errors/vm_exception.rs +++ b/vm/src/vm/errors/vm_exception.rs @@ -13,7 +13,7 @@ use crate::{ hint_processor::hint_processor_utils::get_maybe_relocatable_from_reference, serde::deserialize_program::{ApTracking, Attribute, Location, OffsetValue}, types::{instruction::Register, relocatable::MaybeRelocatable}, - vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, + vm::runners::cairo_runner::CairoRunner, }; use super::vm_errors::VirtualMachineError; @@ -27,14 +27,10 @@ pub struct VmException { } impl VmException { - pub fn from_vm_error( - runner: &CairoRunner, - vm: &VirtualMachine, - error: VirtualMachineError, - ) -> Self { - let pc = vm.run_context.pc; + pub fn from_vm_error(runner: &CairoRunner, error: VirtualMachineError) -> Self { + let pc = runner.vm.run_context.pc; let error_attr_value = if pc.segment_index == 0 { - get_error_attr_value(pc.offset, runner, vm) + get_error_attr_value(pc.offset, runner) } else { None }; @@ -52,22 +48,18 @@ impl VmException { }, inner_exc: error, error_attr_value, - traceback: get_traceback(vm, runner), + traceback: get_traceback(runner), } } } -pub fn get_error_attr_value( - pc: usize, - runner: &CairoRunner, - vm: &VirtualMachine, -) -> Option { +pub fn get_error_attr_value(pc: usize, runner: &CairoRunner) -> Option { let mut errors = String::new(); for attribute in &runner.program.shared_program_data.error_message_attributes { if attribute.start_pc <= pc && attribute.end_pc > pc { errors.push_str(&format!( "Error message: {}\n", - substitute_error_message_references(attribute, runner, vm) + substitute_error_message_references(attribute, runner) )); } } @@ -100,12 +92,12 @@ pub fn get_location( } // Returns the traceback at the current pc. -pub fn get_traceback(vm: &VirtualMachine, runner: &CairoRunner) -> Option { +pub fn get_traceback(runner: &CairoRunner) -> Option { let mut traceback = String::new(); - for (_fp, traceback_pc) in vm.get_traceback_entries() { + for (_fp, traceback_pc) in runner.vm.get_traceback_entries() { if let (0, Some(ref attr)) = ( traceback_pc.segment_index, - get_error_attr_value(traceback_pc.offset, runner, vm), + get_error_attr_value(traceback_pc.offset, runner), ) { traceback.push_str(attr) } @@ -129,7 +121,6 @@ pub fn get_traceback(vm: &VirtualMachine, runner: &CairoRunner) -> Option String { let mut error_msg = error_message_attr.value.clone(); if let Some(tracking_data) = &error_message_attr.flow_tracking_data { @@ -147,12 +138,7 @@ fn substitute_error_message_references( // Look for the formated name inside the error message if error_msg.contains(&formated_variable_name) { // Get the value of the cairo variable from its reference id - match get_value_from_simple_reference( - *ref_id, - &tracking_data.ap_tracking, - runner, - vm, - ) { + match get_value_from_simple_reference(*ref_id, &tracking_data.ap_tracking, runner) { Some(cairo_variable) => { // Replace the value in the error message error_msg = @@ -183,7 +169,6 @@ fn get_value_from_simple_reference( ref_id: usize, ap_tracking: &ApTracking, runner: &CairoRunner, - vm: &VirtualMachine, ) -> Option { let reference = runner .program @@ -197,7 +182,7 @@ fn get_value_from_simple_reference( // Filer complex types (only felt/felt pointers) match reference.cairo_type { Some(ref cairo_type) if cairo_type.contains("felt") => Some( - get_maybe_relocatable_from_reference(vm, reference, ap_tracking)?, + get_maybe_relocatable_from_reference(&runner.vm, reference, ap_tracking)?, ), _ => None, } @@ -349,7 +334,7 @@ mod test { ); let runner = cairo_runner!(program); assert_matches!( - VmException::from_vm_error(&runner, &vm!(), VirtualMachineError::NoImm,), + VmException::from_vm_error(&runner, VirtualMachineError::NoImm,), VmException { pc: x, inst_location: Some(y), @@ -541,9 +526,8 @@ mod test { }]; let program = program!(error_message_attributes = attributes,); let runner = cairo_runner!(program); - let vm = vm!(); assert_eq!( - get_error_attr_value(2, &runner, &vm), + get_error_attr_value(2, &runner), Some(String::from("Error message: Invalid hash\n")) ); } @@ -560,8 +544,7 @@ mod test { }]; let program = program!(error_message_attributes = attributes,); let runner = cairo_runner!(program); - let vm = vm!(); - assert_eq!(get_error_attr_value(5, &runner, &vm), None); + assert_eq!(get_error_attr_value(5, &runner), None); } #[test] @@ -658,12 +641,9 @@ mod test { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); #[cfg(feature = "std")] 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 dict_update{dict_ptr=my_dict}(key=2, prev_value=3, new_value=4);\n ^*************************************************************^\n"); @@ -672,13 +652,10 @@ mod test { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); - assert_eq!(get_traceback(&vm, &cairo_runner), Some(expected_traceback)); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); + assert_eq!(get_traceback(&cairo_runner), Some(expected_traceback)); } #[test] @@ -710,14 +687,11 @@ 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, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); assert_eq!( - get_traceback(&vm, &cairo_runner), + get_traceback(&cairo_runner), Some(expected_traceback.to_string()) ); } @@ -869,13 +843,12 @@ cairo_programs/bad_programs/bad_range_check.cairo:11:5: (pc=0:6) let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); let error = cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .unwrap_err(); - let vm_excepction = VmException::from_vm_error(&cairo_runner, &vm, error); + let vm_excepction = VmException::from_vm_error(&cairo_runner, error); assert_eq!(vm_excepction.to_string(), expected_error_string); } @@ -914,13 +887,12 @@ 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, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); let error = cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .unwrap_err(); - let vm_excepction = VmException::from_vm_error(&cairo_runner, &vm, error); + let vm_excepction = VmException::from_vm_error(&cairo_runner, error); assert_eq!(vm_excepction.to_string(), expected_error_string); } @@ -953,13 +925,12 @@ cairo_programs/bad_programs/ec_recover_product_mod_m_zero.cairo:11:5: (pc=0:18) let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); let error = cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .unwrap_err(); - let vm_excepction = VmException::from_vm_error(&cairo_runner, &vm, error); + let vm_excepction = VmException::from_vm_error(&cairo_runner, error); assert_eq!(vm_excepction.to_string(), expected_error_string); } @@ -992,13 +963,12 @@ cairo_programs/bad_programs/ec_recover_div_mod_n_packed_n_zero.cairo:11:5: (pc=0 let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); let error = cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .unwrap_err(); - let vm_excepction = VmException::from_vm_error(&cairo_runner, &vm, error); + let vm_excepction = VmException::from_vm_error(&cairo_runner, error); assert_eq!(vm_excepction.to_string(), expected_error_string); } @@ -1031,13 +1001,12 @@ cairo_programs/bad_programs/uint512_unsigned_div_rem_div_is_zero.cairo:15:2: (pc let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); let error = cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .unwrap_err(); - let vm_excepction = VmException::from_vm_error(&cairo_runner, &vm, error); + let vm_excepction = VmException::from_vm_error(&cairo_runner, error); assert_eq!(vm_excepction.to_string(), expected_error_string); } @@ -1068,13 +1037,12 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); let error = cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .unwrap_err(); - let vm_excepction = VmException::from_vm_error(&cairo_runner, &vm, error); + let vm_excepction = VmException::from_vm_error(&cairo_runner, error); assert_eq!(vm_excepction.to_string(), expected_error_string); } @@ -1089,10 +1057,9 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) // This program uses a tempvar inside an error attribute // This reference should be rejected when substituting the error attribute references let runner = cairo_runner!(program); - let vm = vm!(); // Ref id 0 corresponds to __main__.main.x, our tempvar assert_eq!( - get_value_from_simple_reference(0, &ApTracking::default(), &runner, &vm), + get_value_from_simple_reference(0, &ApTracking::default(), &runner), None ) } @@ -1108,10 +1075,9 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) // This program uses a tempvar inside an error attribute // This reference should be rejected when substituting the error attribute references let runner = cairo_runner!(program); - let vm = vm!(); let attribute = &program.shared_program_data.error_message_attributes[0]; assert_eq!( - substitute_error_message_references(attribute, &runner, &vm), + substitute_error_message_references(attribute, &runner), format!( "{} (Cannot evaluate ap-based or complex references: ['x'])", attribute.value @@ -1130,10 +1096,9 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) // This program uses a struct inside an error attribute // This reference should be rejected when substituting the error attribute references let runner = cairo_runner!(program); - let vm = vm!(); // Ref id 0 corresponds to __main__.main.cat, our struct assert_eq!( - get_value_from_simple_reference(0, &ApTracking::default(), &runner, &vm), + get_value_from_simple_reference(0, &ApTracking::default(), &runner), None ) } @@ -1149,10 +1114,9 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) // This program uses a struct inside an error attribute // This reference should be rejected when substituting the error attribute references let runner = cairo_runner!(program); - let vm = vm!(); let attribute = &program.shared_program_data.error_message_attributes[0]; assert_eq!( - substitute_error_message_references(attribute, &runner, &vm), + substitute_error_message_references(attribute, &runner), format!( "{} (Cannot evaluate ap-based or complex references: ['cat'])", attribute.value @@ -1180,11 +1144,10 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12) }; let program = program!(instruction_locations = Some(HashMap::from([(5, instruction_location)])),); - let runner = cairo_runner!(program); - let mut vm = vm!(); - vm.set_pc(pc); + let mut runner = cairo_runner!(program); + runner.vm.set_pc(pc); assert_matches!( - VmException::from_vm_error(&runner, &vm, VirtualMachineError::NoImm,), + VmException::from_vm_error(&runner, VirtualMachineError::NoImm,), VmException { pc: x, inst_location: None, diff --git a/vm/src/vm/hooks.rs b/vm/src/vm/hooks.rs index 13c7bb86ce..2d2d825d26 100644 --- a/vm/src/vm/hooks.rs +++ b/vm/src/vm/hooks.rs @@ -17,19 +17,10 @@ use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, }; -use super::{ - errors::vm_errors::VirtualMachineError, runners::cairo_runner::CairoRunner, - vm_core::VirtualMachine, -}; +use super::{errors::vm_errors::VirtualMachineError, vm_core::VirtualMachine}; type BeforeFirstStepHookFunc = Arc< - dyn Fn( - &mut VirtualMachine, - &mut CairoRunner, - &[Box], - ) -> Result<(), VirtualMachineError> - + Sync - + Send, + dyn Fn(&mut VirtualMachine, &[Box]) -> Result<(), VirtualMachineError> + Sync + Send, >; type StepHookFunc = Arc< @@ -71,11 +62,10 @@ impl Hooks { impl VirtualMachine { pub fn execute_before_first_step( &mut self, - runner: &mut CairoRunner, hint_data: &[Box], ) -> Result<(), VirtualMachineError> { if let Some(hook_func) = self.hooks.clone().before_first_step { - (hook_func)(self, runner, hint_data)?; + (hook_func)(self, hint_data)?; } Ok(()) @@ -115,8 +105,7 @@ mod tests { use super::*; use crate::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, - types::program::Program, - utils::test_utils::{cairo_runner, vm}, + types::program::Program, utils::test_utils::cairo_runner, }; #[test] fn empty_hooks() { @@ -128,13 +117,10 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.hooks = Hooks::new(None, None, None); + cairo_runner.vm.hooks = Hooks::new(None, None, None); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_ok()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok()); } #[test] @@ -147,7 +133,6 @@ mod tests { fn before_first_step_hook( _vm: &mut VirtualMachine, - _runner: &mut CairoRunner, _hint_data: &[Box], ) -> Result<(), VirtualMachineError> { Err(VirtualMachineError::Unexpected) @@ -176,35 +161,26 @@ mod tests { // Before first fail let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.hooks = Hooks::new(Some(Arc::new(before_first_step_hook)), None, None); + cairo_runner.vm.hooks = Hooks::new(Some(Arc::new(before_first_step_hook)), None, None); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); // Pre step fail let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.hooks = Hooks::new(None, Some(Arc::new(pre_step_hook)), None); + cairo_runner.vm.hooks = Hooks::new(None, Some(Arc::new(pre_step_hook)), None); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); // Post step fail let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.hooks = Hooks::new(None, None, Some(Arc::new(post_step_hook))); + cairo_runner.vm.hooks = Hooks::new(None, None, Some(Arc::new(post_step_hook))); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); } #[test] @@ -217,7 +193,6 @@ mod tests { fn before_first_step_hook( _vm: &mut VirtualMachine, - _runner: &mut CairoRunner, _hint_data: &[Box], ) -> Result<(), VirtualMachineError> { Ok(()) @@ -245,16 +220,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.hooks = Hooks::new( + cairo_runner.vm.hooks = Hooks::new( Some(Arc::new(before_first_step_hook)), Some(Arc::new(pre_step_hook)), Some(Arc::new(post_step_hook)), ); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_ok()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_ok()); } } diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index a37c6e5cd2..82e6879fdb 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -171,11 +171,10 @@ mod tests { use crate::types::builtin_name::BuiltinName; use crate::vm::errors::memory_errors::MemoryError; use crate::vm::runners::builtin_runner::BuiltinRunner; - use crate::vm::vm_core::VirtualMachine; use crate::Felt252; use crate::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, - types::program::Program, utils::test_utils::*, vm::runners::cairo_runner::CairoRunner, + types::program::Program, utils::test_utils::*, }; #[cfg(target_arch = "wasm32")] @@ -185,7 +184,6 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_instances() { let builtin = BitwiseBuiltinRunner::new(Some(10), true); - let mut vm = vm!(); vm.segments = segments![ @@ -204,7 +202,6 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack() { let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); vm.segments = segments![ @@ -228,7 +225,6 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_stop_pointer() { let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); vm.segments = segments![ @@ -256,7 +252,6 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_when_notincluded() { let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), false).into(); - let mut vm = vm!(); vm.segments = segments![ @@ -280,7 +275,6 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_non_relocatable() { let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); vm.segments = segments![ @@ -305,10 +299,6 @@ mod tests { fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - - vm.segments.segment_used_sizes = Some(vec![0]); - let program = program!( builtins = vec![BuiltinName::bitwise], data = vec_data!( @@ -334,16 +324,20 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0]); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_used_cells_and_allocated_size(&vm), Ok((0, 5))); + assert_eq!( + builtin.get_used_cells_and_allocated_size(&cairo_runner.vm), + Ok((0, 5)) + ); } #[test] @@ -351,8 +345,6 @@ mod tests { fn get_allocated_memory_units() { let builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::pedersen, BuiltinName::bitwise], data = vec_data!( @@ -381,13 +373,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(5)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(5)); } #[test] diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 5f07e819ad..84b07fe2d6 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -247,13 +247,11 @@ mod tests { use crate::utils::test_utils::*; use crate::vm::errors::cairo_run_errors::CairoRunError; use crate::vm::errors::vm_errors::VirtualMachineError; - use crate::vm::runners::cairo_runner::CairoRunner; use crate::{felt_hex, felt_str, relocatable}; use crate::vm::{ errors::{memory_errors::MemoryError, runner_errors::RunnerError}, runners::builtin_runner::BuiltinRunner, - vm_core::VirtualMachine, }; use EcOpBuiltinRunner; @@ -376,10 +374,6 @@ mod tests { fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - - vm.segments.segment_used_sizes = Some(vec![0]); - let program = program!( builtins = vec![BuiltinName::pedersen], data = vec_data!( @@ -405,15 +399,20 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0]); + let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_used_cells_and_allocated_size(&vm), Ok((0, 7))); + assert_eq!( + builtin.get_used_cells_and_allocated_size(&cairo_runner.vm), + Ok((0, 7)) + ); } #[test] @@ -421,8 +420,6 @@ mod tests { fn get_allocated_memory_units() { let builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::ec_op], data = vec_data!( @@ -451,13 +448,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(7)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(7)); } #[test] diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index 2ed138ec4d..a2183902ad 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -173,13 +173,9 @@ mod tests { use crate::types::builtin_name::BuiltinName; use crate::types::program::Program; use crate::utils::test_utils::*; - use crate::vm::runners::cairo_runner::CairoRunner; use crate::{felt_hex, relocatable}; - use crate::vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::BuiltinRunner, - vm_core::VirtualMachine, - }; + use crate::vm::{errors::memory_errors::MemoryError, runners::builtin_runner::BuiltinRunner}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -300,10 +296,6 @@ mod tests { fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = HashBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - - vm.segments.segment_used_sizes = Some(vec![0]); - let program = program!( builtins = vec![BuiltinName::ec_op], data = vec_data!( @@ -330,15 +322,20 @@ mod tests { let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0]); + let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_used_cells_and_allocated_size(&vm), Ok((0, 3))); + assert_eq!( + builtin.get_used_cells_and_allocated_size(&cairo_runner.vm), + Ok((0, 3)) + ); } #[test] @@ -346,8 +343,6 @@ mod tests { fn get_allocated_memory_units() { let builtin: BuiltinRunner = HashBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::ec_op], data = vec_data!( @@ -376,13 +371,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(3)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(3)); } #[test] diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index b80bb9061f..f6131935cc 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -214,13 +214,11 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; use crate::types::program::Program; use crate::utils::test_utils::*; - use crate::vm::runners::cairo_runner::CairoRunner; use crate::{felt_hex, relocatable}; use crate::vm::{ errors::{memory_errors::MemoryError, runner_errors::RunnerError}, runners::builtin_runner::BuiltinRunner, - vm_core::VirtualMachine, }; #[cfg(target_arch = "wasm32")] @@ -341,9 +339,6 @@ mod tests { fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - - vm.segments.segment_used_sizes = Some(vec![0]); let program = Program::from_bytes( include_bytes!("../../../../../cairo_programs/keccak.json"), Some("main"), @@ -351,17 +346,18 @@ mod tests { .unwrap(); let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0]); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); assert_eq!( - builtin.get_used_cells_and_allocated_size(&vm), + builtin.get_used_cells_and_allocated_size(&cairo_runner.vm), Ok((0, 1072)) ); } diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 8b9b4bb2e5..755c01ade0 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -663,10 +663,9 @@ mod tests { use crate::relocatable; use crate::types::builtin_name::BuiltinName; use crate::types::program::Program; + use crate::utils::test_utils::*; use crate::vm::errors::memory_errors::InsufficientAllocatedCellsError; - use crate::vm::runners::cairo_runner::CairoRunner; use crate::vm::vm_memory::memory::MemoryCell; - use crate::{utils::test_utils::*, vm::vm_core::VirtualMachine}; use assert_matches::assert_matches; #[cfg(target_arch = "wasm32")] @@ -805,8 +804,6 @@ mod tests { fn get_allocated_memory_units_bitwise_with_items() { let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(10), true)); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::bitwise], data = vec_data!( @@ -835,13 +832,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(5)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(5)); } #[test] @@ -849,8 +846,6 @@ mod tests { fn get_allocated_memory_units_ec_op_with_items() { let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(10), true)); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::ec_op], data = vec_data!( @@ -879,13 +874,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(7)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(7)); } #[test] @@ -893,8 +888,6 @@ mod tests { fn get_allocated_memory_units_hash_with_items() { let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(10), true)); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::pedersen], data = vec_data!( @@ -923,13 +916,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(3)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(3)); } #[test] @@ -939,8 +932,6 @@ mod tests { RangeCheckBuiltinRunner::::new(Some(10), true), ); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::range_check], data = vec_data!( @@ -969,13 +960,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(1)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(1)); } #[test] diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 6183c2b88a..c5d2dc56b5 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -697,30 +697,23 @@ 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, LayoutName::all_cairo, true).unwrap(); + let mut runner = CairoRunner::new(&program, LayoutName::all_cairo, true, false).unwrap(); - let mut vm = VirtualMachine::new(false); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); // Modify add_mod & mul_mod params - for runner in vm.get_builtin_runners_as_mut() { + for runner in runner.vm.get_builtin_runners_as_mut() { if let BuiltinRunner::Mod(runner) = runner { runner.override_layout_params(1, 3) } } - runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .unwrap(); - runner - .run_for_steps(1, &mut vm, &mut hint_processor) - .unwrap(); - runner - .end_run(false, false, &mut vm, &mut hint_processor) - .unwrap(); - runner.read_return_values(&mut vm, false).unwrap(); - runner.finalize_segments(&mut vm).unwrap(); - - let air_private_input = runner.get_air_private_input(&vm); + runner.run_until_pc(end, &mut hint_processor).unwrap(); + runner.run_for_steps(1, &mut hint_processor).unwrap(); + runner.end_run(false, false, &mut hint_processor).unwrap(); + runner.read_return_values(false).unwrap(); + runner.finalize_segments().unwrap(); + + let air_private_input = runner.get_air_private_input(); assert_eq!( air_private_input.0.get(&BuiltinName::add_mod).unwrap()[0], PrivateInput::Mod(ModInput { diff --git a/vm/src/vm/runners/builtin_runner/output.rs b/vm/src/vm/runners/builtin_runner/output.rs index a8486fe2d9..ae54908565 100644 --- a/vm/src/vm/runners/builtin_runner/output.rs +++ b/vm/src/vm/runners/builtin_runner/output.rs @@ -207,10 +207,7 @@ mod tests { use crate::{ utils::test_utils::*, - vm::{ - errors::memory_errors::MemoryError, runners::builtin_runner::BuiltinRunner, - vm_core::VirtualMachine, - }, + vm::{errors::memory_errors::MemoryError, runners::builtin_runner::BuiltinRunner}, }; #[cfg(target_arch = "wasm32")] diff --git a/vm/src/vm/runners/builtin_runner/poseidon.rs b/vm/src/vm/runners/builtin_runner/poseidon.rs index 7886d98e84..ee8d6c7e4f 100644 --- a/vm/src/vm/runners/builtin_runner/poseidon.rs +++ b/vm/src/vm/runners/builtin_runner/poseidon.rs @@ -152,9 +152,8 @@ mod tests { use crate::types::builtin_name::BuiltinName; use crate::types::program::Program; use crate::utils::test_utils::*; - use crate::vm::runners::cairo_runner::CairoRunner; - use crate::vm::{runners::builtin_runner::BuiltinRunner, vm_core::VirtualMachine}; + use crate::vm::runners::builtin_runner::BuiltinRunner; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -285,10 +284,6 @@ mod tests { fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = PoseidonBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - - vm.segments.segment_used_sizes = Some(vec![0]); - let program = program!( builtins = vec![BuiltinName::poseidon], data = vec_data!( @@ -314,16 +309,20 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0]); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_used_cells_and_allocated_size(&vm), Ok((0, 6))); + assert_eq!( + builtin.get_used_cells_and_allocated_size(&cairo_runner.vm), + Ok((0, 6)) + ); } #[test] @@ -331,8 +330,6 @@ mod tests { fn get_allocated_memory_units() { let builtin: BuiltinRunner = PoseidonBuiltinRunner::new(Some(10), true).into(); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::poseidon], data = vec_data!( @@ -361,13 +358,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(6)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(6)); } #[test] diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index e9749e8b8a..9c19de3309 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -169,12 +169,7 @@ mod tests { use crate::vm::vm_memory::memory::Memory; use crate::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, - types::program::Program, - utils::test_utils::*, - vm::{ - runners::{builtin_runner::BuiltinRunner, cairo_runner::CairoRunner}, - vm_core::VirtualMachine, - }, + types::program::Program, utils::test_utils::*, vm::runners::builtin_runner::BuiltinRunner, }; #[cfg(target_arch = "wasm32")] @@ -303,10 +298,6 @@ mod tests { let builtin: BuiltinRunner = RangeCheckBuiltinRunner::::new(Some(10), true).into(); - let mut vm = vm!(); - - vm.segments.segment_used_sizes = Some(vec![0]); - let program = program!( builtins = vec![BuiltinName::range_check], data = vec_data!( @@ -333,15 +324,20 @@ mod tests { let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0]); + let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_used_cells_and_allocated_size(&vm), Ok((0, 1))); + assert_eq!( + builtin.get_used_cells_and_allocated_size(&cairo_runner.vm), + Ok((0, 1)) + ); } #[test] @@ -350,8 +346,6 @@ mod tests { let builtin: BuiltinRunner = RangeCheckBuiltinRunner::::new(Some(10), true).into(); - let mut vm = vm!(); - let program = program!( builtins = vec![BuiltinName::range_check], data = vec_data!( @@ -380,13 +374,13 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); - let address = cairo_runner.initialize(&mut vm, false).unwrap(); + let address = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(address, &mut vm, &mut hint_processor) + .run_until_pc(address, &mut hint_processor) .unwrap(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(1)); + assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(1)); } #[test] diff --git a/vm/src/vm/runners/builtin_runner/segment_arena.rs b/vm/src/vm/runners/builtin_runner/segment_arena.rs index 6b9da00c54..e8b442dc5a 100644 --- a/vm/src/vm/runners/builtin_runner/segment_arena.rs +++ b/vm/src/vm/runners/builtin_runner/segment_arena.rs @@ -90,7 +90,6 @@ mod tests { use super::*; use crate::types::builtin_name::BuiltinName; use crate::vm::errors::runner_errors::RunnerError; - use crate::vm::vm_core::VirtualMachine; use crate::{relocatable, utils::test_utils::*, vm::runners::builtin_runner::BuiltinRunner}; #[cfg(not(feature = "std"))] use alloc::boxed::Box; diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index 762e46f639..24eaa46541 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -255,7 +255,6 @@ mod tests { runner_errors::RunnerError, }, runners::builtin_runner::BuiltinRunner, - vm_core::VirtualMachine, vm_memory::{memory::Memory, memory_segments::MemorySegmentManager}, }, }; diff --git a/vm/src/vm/runners/cairo_pie.rs b/vm/src/vm/runners/cairo_pie.rs index cca0bcb8fd..228f4048c6 100644 --- a/vm/src/vm/runners/cairo_pie.rs +++ b/vm/src/vm/runners/cairo_pie.rs @@ -752,13 +752,13 @@ mod test { layout: LayoutName::starknet_with_keccak, ..Default::default() }; - let (runner, vm) = crate::cairo_run::cairo_run( + let runner = crate::cairo_run::cairo_run( program_content, &cairo_run_config, &mut BuiltinHintProcessor::new_empty(), ) .unwrap(); - runner.get_cairo_pie(&vm).unwrap() + runner.get_cairo_pie().unwrap() }; // Serialize the CairoPie into a zip file let filename = format!("temp_file_{}", identifier); // Identifier used to avoid name clashes diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 9592d77f2a..0f4d2ab0fc 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -140,8 +140,8 @@ impl ResourceTracker for RunResources { } } -#[derive(Debug)] pub struct CairoRunner { + pub vm: VirtualMachine, pub(crate) program: Program, layout: CairoLayout, final_pc: Option, @@ -172,6 +172,7 @@ impl CairoRunner { program: &Program, layout: LayoutName, mode: RunnerMode, + trace_enabled: bool, ) -> Result { let cairo_layout = match layout { LayoutName::plain => CairoLayout::plain_instance(), @@ -188,6 +189,7 @@ impl CairoRunner { }; Ok(CairoRunner { program: program.clone(), + vm: VirtualMachine::new(trace_enabled), layout: cairo_layout, final_pc: None, program_base: None, @@ -214,23 +216,25 @@ impl CairoRunner { program: &Program, layout: LayoutName, proof_mode: bool, + trace_enabled: bool, ) -> Result { if proof_mode { - Self::new_v2(program, layout, RunnerMode::ProofModeCanonical) + Self::new_v2( + program, + layout, + RunnerMode::ProofModeCanonical, + trace_enabled, + ) } else { - Self::new_v2(program, layout, RunnerMode::ExecutionMode) + Self::new_v2(program, layout, RunnerMode::ExecutionMode, trace_enabled) } } - pub fn initialize( - &mut self, - vm: &mut VirtualMachine, - allow_missing_builtins: bool, - ) -> Result { - self.initialize_builtins(vm, allow_missing_builtins)?; - self.initialize_segments(vm, None); - let end = self.initialize_main_entrypoint(vm)?; - self.initialize_vm(vm)?; + pub fn initialize(&mut self, allow_missing_builtins: bool) -> Result { + self.initialize_builtins(allow_missing_builtins)?; + self.initialize_segments(None); + let end = self.initialize_main_entrypoint()?; + self.initialize_vm()?; Ok(end) } @@ -238,11 +242,7 @@ impl CairoRunner { /// When running in proof_mode, all builtins in the layout will be created, and only those in the program will be included /// When not running in proof_mode, only program builtins will be created and included /// Unless `allow_missing_builtins` is set to true, an error will be returned if a builtin is included in the program but not on the layout - pub fn initialize_builtins( - &self, - vm: &mut VirtualMachine, - allow_missing_builtins: bool, - ) -> Result<(), RunnerError> { + pub fn initialize_builtins(&mut self, allow_missing_builtins: bool) -> Result<(), RunnerError> { let builtin_ordered_list = vec![ BuiltinName::output, BuiltinName::pedersen, @@ -260,26 +260,29 @@ impl CairoRunner { return Err(RunnerError::DisorderedBuiltins); }; let mut program_builtins: HashSet<&BuiltinName> = self.program.builtins.iter().collect(); - let mut builtin_runners = Vec::::new(); if self.layout.builtins.output { let included = program_builtins.remove(&BuiltinName::output); if included || self.is_proof_mode() { - builtin_runners.push(OutputBuiltinRunner::new(included).into()); + self.vm + .builtin_runners + .push(OutputBuiltinRunner::new(included).into()); } } if let Some(instance_def) = self.layout.builtins.pedersen.as_ref() { let included = program_builtins.remove(&BuiltinName::pedersen); if included || self.is_proof_mode() { - builtin_runners.push(HashBuiltinRunner::new(instance_def.ratio, included).into()); + self.vm + .builtin_runners + .push(HashBuiltinRunner::new(instance_def.ratio, included).into()); } } if let Some(instance_def) = self.layout.builtins.range_check.as_ref() { let included = program_builtins.remove(&BuiltinName::range_check); if included || self.is_proof_mode() { - builtin_runners.push( + self.vm.builtin_runners.push( RangeCheckBuiltinRunner::::new( instance_def.ratio, included, @@ -292,7 +295,8 @@ impl CairoRunner { if let Some(instance_def) = self.layout.builtins.ecdsa.as_ref() { let included = program_builtins.remove(&BuiltinName::ecdsa); if included || self.is_proof_mode() { - builtin_runners + self.vm + .builtin_runners .push(SignatureBuiltinRunner::new(instance_def.ratio, included).into()); } } @@ -300,7 +304,8 @@ impl CairoRunner { if let Some(instance_def) = self.layout.builtins.bitwise.as_ref() { let included = program_builtins.remove(&BuiltinName::bitwise); if included || self.is_proof_mode() { - builtin_runners + self.vm + .builtin_runners .push(BitwiseBuiltinRunner::new(instance_def.ratio, included).into()); } } @@ -308,21 +313,26 @@ impl CairoRunner { if let Some(instance_def) = self.layout.builtins.ec_op.as_ref() { let included = program_builtins.remove(&BuiltinName::ec_op); if included || self.is_proof_mode() { - builtin_runners.push(EcOpBuiltinRunner::new(instance_def.ratio, included).into()); + self.vm + .builtin_runners + .push(EcOpBuiltinRunner::new(instance_def.ratio, included).into()); } } if let Some(instance_def) = self.layout.builtins.keccak.as_ref() { let included = program_builtins.remove(&BuiltinName::keccak); if included || self.is_proof_mode() { - builtin_runners.push(KeccakBuiltinRunner::new(instance_def.ratio, included).into()); + self.vm + .builtin_runners + .push(KeccakBuiltinRunner::new(instance_def.ratio, included).into()); } } if let Some(instance_def) = self.layout.builtins.poseidon.as_ref() { let included = program_builtins.remove(&BuiltinName::poseidon); if included || self.is_proof_mode() { - builtin_runners + self.vm + .builtin_runners .push(PoseidonBuiltinRunner::new(instance_def.ratio, included).into()); } } @@ -330,7 +340,7 @@ impl CairoRunner { if let Some(instance_def) = self.layout.builtins.range_check96.as_ref() { let included = program_builtins.remove(&BuiltinName::range_check96); if included || self.is_proof_mode() { - builtin_runners.push( + self.vm.builtin_runners.push( RangeCheckBuiltinRunner::::new(instance_def.ratio, included) .into(), ); @@ -339,13 +349,17 @@ impl CairoRunner { if let Some(instance_def) = self.layout.builtins.add_mod.as_ref() { let included = program_builtins.remove(&BuiltinName::add_mod); if included || self.is_proof_mode() { - builtin_runners.push(ModBuiltinRunner::new_add_mod(instance_def, included).into()); + self.vm + .builtin_runners + .push(ModBuiltinRunner::new_add_mod(instance_def, included).into()); } } if let Some(instance_def) = self.layout.builtins.mul_mod.as_ref() { let included = program_builtins.remove(&BuiltinName::mul_mod); if included || self.is_proof_mode() { - builtin_runners.push(ModBuiltinRunner::new_mul_mod(instance_def, included).into()); + self.vm + .builtin_runners + .push(ModBuiltinRunner::new_mul_mod(instance_def, included).into()); } } if !program_builtins.is_empty() && !allow_missing_builtins { @@ -355,7 +369,6 @@ impl CairoRunner { )))); } - vm.builtin_runners = builtin_runners; Ok(()) } @@ -367,8 +380,7 @@ impl CairoRunner { // Initialize all the builtins. Values used are the original one from the CairoFunctionRunner // Values extracted from here: https://github.com/starkware-libs/cairo-lang/blob/4fb83010ab77aa7ead0c9df4b0c05e030bc70b87/src/starkware/cairo/common/cairo_function_runner.py#L28 fn initialize_all_builtins( - &self, - vm: &mut VirtualMachine, + &mut self, add_segment_arena_builtin: bool, ) -> Result<(), RunnerError> { let starknet_preset_builtins = vec![ @@ -433,49 +445,46 @@ impl CairoRunner { } for builtin_name in &self.program.builtins { - initialize_builtin(*builtin_name, vm, add_segment_arena_builtin); + initialize_builtin(*builtin_name, &mut self.vm, add_segment_arena_builtin); } for builtin_name in starknet_preset_builtins { if !self.program.builtins.contains(&builtin_name) { - initialize_builtin(builtin_name, vm, add_segment_arena_builtin) + initialize_builtin(builtin_name, &mut self.vm, add_segment_arena_builtin) } } Ok(()) } ///Creates the necessary segments for the program, execution, and each builtin on the MemorySegmentManager and stores the first adress of each of this new segments as each owner's base - pub fn initialize_segments( - &mut self, - vm: &mut VirtualMachine, - program_base: Option, - ) { + pub fn initialize_segments(&mut self, program_base: Option) { self.program_base = match program_base { Some(base) => Some(base), - None => Some(vm.segments.add()), + None => Some(self.vm.add_memory_segment()), }; - self.execution_base = Some(vm.segments.add()); - for builtin_runner in vm.builtin_runners.iter_mut() { - builtin_runner.initialize_segments(&mut vm.segments); + self.execution_base = Some(self.vm.add_memory_segment()); + for builtin_runner in self.vm.builtin_runners.iter_mut() { + builtin_runner.initialize_segments(&mut self.vm.segments); } } fn initialize_state( &mut self, - vm: &mut VirtualMachine, entrypoint: usize, stack: Vec, ) -> Result<(), RunnerError> { let prog_base = self.program_base.ok_or(RunnerError::NoProgBase)?; let exec_base = self.execution_base.ok_or(RunnerError::NoExecBase)?; self.initial_pc = Some((prog_base + entrypoint)?); - vm.load_data(prog_base, &self.program.shared_program_data.data) + self.vm + .load_data(prog_base, &self.program.shared_program_data.data) .map_err(RunnerError::MemoryInitializationError)?; // Mark all addresses from the program segment as accessed for i in 0..self.program.shared_program_data.data.len() { - vm.segments.memory.mark_as_accessed((prog_base + i)?); + self.vm.segments.memory.mark_as_accessed((prog_base + i)?); } - vm.segments + self.vm + .segments .load_data(exec_base, &stack) .map_err(RunnerError::MemoryInitializationError)?; Ok(()) @@ -483,12 +492,11 @@ impl CairoRunner { pub fn initialize_function_entrypoint( &mut self, - vm: &mut VirtualMachine, entrypoint: usize, mut stack: Vec, return_fp: MaybeRelocatable, ) -> Result { - let end = vm.segments.add(); + let end = self.vm.add_memory_segment(); stack.append(&mut vec![ return_fp, MaybeRelocatable::RelocatableValue(end), @@ -502,7 +510,7 @@ impl CairoRunner { } else { return Err(RunnerError::NoExecBase); } - self.initialize_state(vm, entrypoint, stack)?; + self.initialize_state(entrypoint, stack)?; self.final_pc = Some(end); Ok(end) } @@ -510,13 +518,11 @@ impl CairoRunner { ///Initializes state for running a program from the main() entrypoint. ///If self.is_proof_mode() == True, the execution starts from the start label rather then the main() function. ///Returns the value of the program counter after returning from main. - fn initialize_main_entrypoint( - &mut self, - vm: &mut VirtualMachine, - ) -> Result { + fn initialize_main_entrypoint(&mut self) -> Result { let mut stack = Vec::new(); { - let builtin_runners = vm + let builtin_runners = self + .vm .builtin_runners .iter() .map(|b| (b.name(), b)) @@ -543,15 +549,14 @@ impl CairoRunner { target_offset = stack.len() + 2; // This values shouldn't be needed with a canonical proof mode - let return_fp = vm.segments.add(); - let end = vm.segments.add(); + let return_fp = self.vm.add_memory_segment(); + let end = self.vm.add_memory_segment(); stack.append(&mut vec![ MaybeRelocatable::RelocatableValue(return_fp), MaybeRelocatable::RelocatableValue(end), ]); self.initialize_state( - vm, self.program .shared_program_data .start @@ -570,7 +575,6 @@ impl CairoRunner { self.execution_public_memory = Some(Vec::from_iter(0..stack_prefix.len())); self.initialize_state( - vm, self.program .shared_program_data .start @@ -591,11 +595,10 @@ impl CairoRunner { .ok_or(RunnerError::NoProgramEnd)?)?); } - let return_fp = vm.segments.add(); + let return_fp = self.vm.add_memory_segment(); if let Some(main) = &self.entrypoint { let main_clone = *main; Ok(self.initialize_function_entrypoint( - vm, main_clone, stack, MaybeRelocatable::RelocatableValue(return_fp), @@ -605,15 +608,16 @@ impl CairoRunner { } } - pub fn initialize_vm(&mut self, vm: &mut VirtualMachine) -> Result<(), RunnerError> { - vm.run_context.pc = *self.initial_pc.as_ref().ok_or(RunnerError::NoPC)?; - vm.run_context.ap = self.initial_ap.as_ref().ok_or(RunnerError::NoAP)?.offset; - vm.run_context.fp = self.initial_fp.as_ref().ok_or(RunnerError::NoFP)?.offset; - for builtin in vm.builtin_runners.iter() { - builtin.add_validation_rule(&mut vm.segments.memory); + pub fn initialize_vm(&mut self) -> Result<(), RunnerError> { + self.vm.run_context.pc = *self.initial_pc.as_ref().ok_or(RunnerError::NoPC)?; + self.vm.run_context.ap = self.initial_ap.as_ref().ok_or(RunnerError::NoAP)?.offset; + self.vm.run_context.fp = self.initial_fp.as_ref().ok_or(RunnerError::NoFP)?.offset; + for builtin in self.vm.builtin_runners.iter() { + builtin.add_validation_rule(&mut self.vm.segments.memory); } - vm.segments + self.vm + .segments .memory .validate_existing_memory() .map_err(RunnerError::MemoryValidationError) @@ -657,7 +661,6 @@ impl CairoRunner { pub fn run_until_pc( &mut self, address: Relocatable, - vm: &mut VirtualMachine, hint_processor: &mut dyn HintProcessor, ) -> Result<(), VirtualMachineError> { let references = &self.program.shared_program_data.reference_manager; @@ -673,9 +676,9 @@ impl CairoRunner { .hints_ranges .clone(); #[cfg(feature = "test_utils")] - vm.execute_before_first_step(self, &hint_data)?; - while vm.run_context.pc != address && !hint_processor.consumed() { - vm.step( + self.vm.execute_before_first_step(&hint_data)?; + while self.vm.get_pc() != address && !hint_processor.consumed() { + self.vm.step( hint_processor, &mut self.exec_scopes, #[cfg(feature = "extensive_hints")] @@ -684,7 +687,7 @@ impl CairoRunner { self.program .shared_program_data .hints_collection - .get_hint_range_for_pc(vm.run_context.pc.offset) + .get_hint_range_for_pc(self.vm.get_pc().offset) .and_then(|range| { range.and_then(|(start, length)| hint_data.get(start..start + length.get())) }) @@ -697,7 +700,7 @@ impl CairoRunner { hint_processor.consume_step(); } - if vm.run_context.pc != address { + if self.vm.get_pc() != address { return Err(VirtualMachineError::UnfinishedExecution); } @@ -708,7 +711,6 @@ impl CairoRunner { pub fn run_for_steps( &mut self, steps: usize, - vm: &mut VirtualMachine, hint_processor: &mut dyn HintProcessor, ) -> Result<(), VirtualMachineError> { let references = &self.program.shared_program_data.reference_manager; @@ -728,18 +730,18 @@ impl CairoRunner { .program .shared_program_data .hints_collection - .get_hint_range_for_pc(vm.run_context.pc.offset) + .get_hint_range_for_pc(self.vm.get_pc().offset) .and_then(|range| { range.and_then(|(start, length)| hint_data.get(start..start + length.get())) }) .unwrap_or(&[]); for remaining_steps in (1..=steps).rev() { - if self.final_pc.as_ref() == Some(&vm.run_context.pc) { + if self.final_pc.as_ref() == Some(&self.vm.get_pc()) { return Err(VirtualMachineError::EndOfProgram(remaining_steps)); } - vm.step( + self.vm.step( hint_processor, &mut self.exec_scopes, #[cfg(feature = "extensive_hints")] @@ -759,47 +761,47 @@ impl CairoRunner { pub fn run_until_steps( &mut self, steps: usize, - vm: &mut VirtualMachine, hint_processor: &mut dyn HintProcessor, ) -> Result<(), VirtualMachineError> { - self.run_for_steps(steps.saturating_sub(vm.current_step), vm, hint_processor) + self.run_for_steps(steps.saturating_sub(self.vm.current_step), hint_processor) } /// Execute steps until the step counter reaches a power of two. pub fn run_until_next_power_of_2( &mut self, - vm: &mut VirtualMachine, hint_processor: &mut dyn HintProcessor, ) -> Result<(), VirtualMachineError> { - self.run_until_steps(vm.current_step.next_power_of_two(), vm, hint_processor) + self.run_until_steps(self.vm.current_step.next_power_of_two(), hint_processor) } - pub fn get_perm_range_check_limits(&self, vm: &VirtualMachine) -> Option<(isize, isize)> { - let runner_usages = vm + pub fn get_perm_range_check_limits(&self) -> Option<(isize, isize)> { + let runner_usages = self + .vm .builtin_runners .iter() - .filter_map(|runner| runner.get_range_check_usage(&vm.segments.memory)) + .filter_map(|runner| runner.get_range_check_usage(&self.vm.segments.memory)) .map(|(rc_min, rc_max)| (rc_min as isize, rc_max as isize)); - let rc_bounds = vm.rc_limits.iter().copied().chain(runner_usages); + let rc_bounds = self.vm.rc_limits.iter().copied().chain(runner_usages); rc_bounds.reduce(|(min1, max1), (min2, max2)| (min1.min(min2), max1.max(max2))) } /// Checks that there are enough trace cells to fill the entire range check /// range. - pub fn check_range_check_usage(&self, vm: &VirtualMachine) -> Result<(), VirtualMachineError> { - let Some((rc_min, rc_max)) = self.get_perm_range_check_limits(vm) else { + pub fn check_range_check_usage(&self) -> Result<(), VirtualMachineError> { + let Some((rc_min, rc_max)) = self.get_perm_range_check_limits() else { return Ok(()); }; - let rc_units_used_by_builtins: usize = vm + let rc_units_used_by_builtins: usize = self + .vm .builtin_runners .iter() - .map(|runner| runner.get_used_perm_range_check_units(vm)) + .map(|runner| runner.get_used_perm_range_check_units(&self.vm)) .sum::>() .map_err(Into::::into)?; let unused_rc_units = - (self.layout.rc_units as usize - 3) * vm.current_step - rc_units_used_by_builtins; + (self.layout.rc_units as usize - 3) * self.vm.current_step - rc_units_used_by_builtins; if unused_rc_units < (rc_max - rc_min) as usize { return Err(MemoryError::InsufficientAllocatedCells( InsufficientAllocatedCellsError::RangeCheckUnits(Box::new(( @@ -814,38 +816,35 @@ impl CairoRunner { } /// Count the number of holes present in the segments. - pub fn get_memory_holes(&self, vm: &VirtualMachine) -> Result { - vm.segments.get_memory_holes( - vm.builtin_runners.len(), + pub fn get_memory_holes(&self) -> Result { + self.vm.segments.get_memory_holes( + self.vm.builtin_runners.len(), self.program.builtins.contains(&BuiltinName::output), ) } /// Check if there are enough trace cells to fill the entire diluted checks. - pub fn check_diluted_check_usage( - &self, - vm: &VirtualMachine, - ) -> Result<(), VirtualMachineError> { + pub fn check_diluted_check_usage(&self) -> Result<(), VirtualMachineError> { let diluted_pool_instance = match &self.layout.diluted_pool_instance_def { Some(x) => x, None => return Ok(()), }; let mut used_units_by_builtins = 0; - for builtin_runner in &vm.builtin_runners { + for builtin_runner in &self.vm.builtin_runners { let used_units = builtin_runner.get_used_diluted_check_units( diluted_pool_instance.spacing, diluted_pool_instance.n_bits, ); let multiplier = safe_div_usize( - vm.current_step, + self.vm.current_step, builtin_runner.ratio().unwrap_or(1) as usize, )?; used_units_by_builtins += used_units * multiplier; } - let diluted_units = diluted_pool_instance.units_per_step as usize * vm.current_step; + let diluted_units = diluted_pool_instance.units_per_step as usize * self.vm.current_step; let unused_diluted_units = diluted_units.saturating_sub(used_units_by_builtins); let diluted_usage_upper_bound = 1usize << diluted_pool_instance.n_bits; @@ -866,25 +865,24 @@ impl CairoRunner { &mut self, disable_trace_padding: bool, disable_finalize_all: bool, - vm: &mut VirtualMachine, hint_processor: &mut dyn HintProcessor, ) -> Result<(), VirtualMachineError> { if self.run_ended { return Err(RunnerError::EndRunCalledTwice.into()); } - vm.segments.memory.relocate_memory()?; - vm.end_run(&self.exec_scopes)?; + self.vm.segments.memory.relocate_memory()?; + self.vm.end_run(&self.exec_scopes)?; if disable_finalize_all { return Ok(()); } - vm.segments.compute_effective_sizes(); + self.vm.segments.compute_effective_sizes(); if self.is_proof_mode() && !disable_trace_padding { - self.run_until_next_power_of_2(vm, hint_processor)?; + self.run_until_next_power_of_2(hint_processor)?; loop { - match self.check_used_cells(vm) { + match self.check_used_cells() { Ok(_) => break, Err(e) => match e { VirtualMachineError::Memory(MemoryError::InsufficientAllocatedCells(_)) => { @@ -893,8 +891,8 @@ impl CairoRunner { }, } - self.run_for_steps(1, vm, hint_processor)?; - self.run_until_next_power_of_2(vm, hint_processor)?; + self.run_for_steps(1, hint_processor)?; + self.run_until_next_power_of_2(hint_processor)?; } } @@ -903,16 +901,17 @@ impl CairoRunner { } ///Relocates the VM's trace, turning relocatable registers to numbered ones - pub fn relocate_trace( - &mut self, - vm: &VirtualMachine, - relocation_table: &Vec, - ) -> Result<(), TraceError> { + pub fn relocate_trace(&mut self, relocation_table: &Vec) -> Result<(), TraceError> { if self.relocated_trace.is_some() { return Err(TraceError::AlreadyRelocated); } - let trace = vm.trace.as_ref().ok_or(TraceError::TraceNotEnabled)?.iter(); + let trace = self + .vm + .trace + .as_ref() + .ok_or(TraceError::TraceNotEnabled)? + .iter(); let mut relocated_trace = Vec::::with_capacity(trace.len()); let segment_1_base = relocation_table .get(1) @@ -932,17 +931,13 @@ impl CairoRunner { /// Relocates the VM's memory, turning bidimensional indexes into contiguous numbers, and values /// into Felt252s. Uses the relocation_table to asign each index a number according to the value /// on its segment number. - fn relocate_memory( - &mut self, - vm: &VirtualMachine, - relocation_table: &Vec, - ) -> Result<(), MemoryError> { + fn relocate_memory(&mut self, relocation_table: &Vec) -> Result<(), MemoryError> { if !(self.relocated_memory.is_empty()) { return Err(MemoryError::Relocation); } //Relocated addresses start at 1 self.relocated_memory.push(None); - for (index, segment) in vm.segments.memory.data.iter().enumerate() { + for (index, segment) in self.vm.segments.memory.data.iter().enumerate() { for (seg_offset, cell) in segment.iter().enumerate() { match cell.get_value() { Some(cell) => { @@ -963,43 +958,37 @@ impl CairoRunner { Ok(()) } - pub fn relocate( - &mut self, - vm: &mut VirtualMachine, - relocate_mem: bool, - ) -> Result<(), TraceError> { - vm.segments.compute_effective_sizes(); - if !relocate_mem && vm.trace.is_none() { + pub fn relocate(&mut self, relocate_mem: bool) -> Result<(), TraceError> { + self.vm.segments.compute_effective_sizes(); + if !relocate_mem && self.vm.trace.is_none() { return Ok(()); } // relocate_segments can fail if compute_effective_sizes is not called before. // The expect should be unreachable. - let relocation_table = vm + let relocation_table = self + .vm .segments .relocate_segments() .expect("compute_effective_sizes called but relocate_memory still returned error"); if relocate_mem { - if let Err(memory_error) = self.relocate_memory(vm, &relocation_table) { + if let Err(memory_error) = self.relocate_memory(&relocation_table) { return Err(TraceError::MemoryError(memory_error)); } } - if vm.trace.is_some() { - self.relocate_trace(vm, &relocation_table)?; + if self.vm.trace.is_some() { + self.relocate_trace(&relocation_table)?; } - vm.relocation_table = Some(relocation_table); + self.vm.relocation_table = Some(relocation_table); Ok(()) } // Returns a map from builtin base's segment index to stop_ptr offset // Aka the builtin's segment number and its maximum offset - pub fn get_builtin_segments_info( - &self, - vm: &VirtualMachine, - ) -> Result, RunnerError> { + pub fn get_builtin_segments_info(&self) -> Result, RunnerError> { let mut builtin_segment_info = Vec::new(); - for builtin in &vm.builtin_runners { + for builtin in &self.vm.builtin_runners { let (index, stop_ptr) = builtin.get_memory_segment_addresses(); builtin_segment_info.push(( @@ -1015,11 +1004,10 @@ impl CairoRunner { // Aka the builtin's segment number and its maximum offset pub fn get_builtin_segment_info_for_pie( &self, - vm: &VirtualMachine, ) -> Result, RunnerError> { let mut builtin_segment_info = HashMap::new(); - for builtin in &vm.builtin_runners { + for builtin in &self.vm.builtin_runners { let (index, stop_ptr) = builtin.get_memory_segment_addresses(); builtin_segment_info.insert( @@ -1035,22 +1023,20 @@ impl CairoRunner { Ok(builtin_segment_info) } - pub fn get_execution_resources( - &self, - vm: &VirtualMachine, - ) -> Result { - let n_steps = vm + pub fn get_execution_resources(&self) -> Result { + let n_steps = self + .vm .trace .as_ref() .map(|x| x.len()) - .unwrap_or(vm.current_step); - let n_memory_holes = self.get_memory_holes(vm)?; + .unwrap_or(self.vm.current_step); + let n_memory_holes = self.get_memory_holes()?; let mut builtin_instance_counter = HashMap::new(); - for builtin_runner in &vm.builtin_runners { + for builtin_runner in &self.vm.builtin_runners { builtin_instance_counter.insert( builtin_runner.name(), - builtin_runner.get_used_instances(&vm.segments)?, + builtin_runner.get_used_instances(&self.vm.segments)?, ); } @@ -1066,7 +1052,7 @@ impl CairoRunner { // 1. end_run() must precede a call to this method. // 2. Call read_return_values() *before* finalize_segments(), otherwise the return values // will not be included in the public memory. - pub fn finalize_segments(&mut self, vm: &mut VirtualMachine) -> Result<(), RunnerError> { + pub fn finalize_segments(&mut self) -> Result<(), RunnerError> { if self.segments_finalized { return Ok(()); } @@ -1078,7 +1064,7 @@ impl CairoRunner { for i in 0..size { public_memory.push((i, 0_usize)) } - vm.segments.finalize( + self.vm.segments.finalize( Some(size), self.program_base .as_ref() @@ -1099,22 +1085,25 @@ impl CairoRunner { { public_memory.push((elem + exec_base.offset, 0)) } - vm.segments + self.vm + .segments .finalize(None, exec_base.segment_index as usize, Some(&public_memory)); - for builtin_runner in vm.builtin_runners.iter() { + for builtin_runner in self.vm.builtin_runners.iter() { let (_, size) = builtin_runner - .get_used_cells_and_allocated_size(vm) + .get_used_cells_and_allocated_size(&self.vm) .map_err(RunnerError::FinalizeSegements)?; if let BuiltinRunner::Output(output_builtin) = builtin_runner { - let public_memory = output_builtin.get_public_memory(&vm.segments)?; - vm.segments + let public_memory = output_builtin.get_public_memory(&self.vm.segments)?; + self.vm + .segments .finalize(Some(size), builtin_runner.base(), Some(&public_memory)) } else { - vm.segments + self.vm + .segments .finalize(Some(size), builtin_runner.base(), None) } } - vm.segments.finalize_zero_segment(); + self.vm.segments.finalize_zero_segment(); self.segments_finalized = true; Ok(()) } @@ -1122,31 +1111,29 @@ impl CairoRunner { /// Runs a cairo program from a give entrypoint, indicated by its pc offset, with the given arguments. /// If `verify_secure` is set to true, [verify_secure_runner] will be called to run extra verifications. /// `program_segment_size` is only used by the [verify_secure_runner] function and will be ignored if `verify_secure` is set to false. - #[allow(clippy::too_many_arguments)] pub fn run_from_entrypoint( &mut self, entrypoint: usize, args: &[&CairoArg], verify_secure: bool, program_segment_size: Option, - vm: &mut VirtualMachine, hint_processor: &mut dyn HintProcessor, ) -> Result<(), CairoRunError> { let stack = args .iter() - .map(|arg| vm.segments.gen_cairo_arg(arg)) + .map(|arg| self.vm.segments.gen_cairo_arg(arg)) .collect::, VirtualMachineError>>()?; let return_fp = MaybeRelocatable::from(0); - let end = self.initialize_function_entrypoint(vm, entrypoint, stack, return_fp)?; + let end = self.initialize_function_entrypoint(entrypoint, stack, return_fp)?; - self.initialize_vm(vm)?; + self.initialize_vm()?; - self.run_until_pc(end, vm, hint_processor) - .map_err(|err| VmException::from_vm_error(self, vm, err))?; - self.end_run(true, false, vm, hint_processor)?; + self.run_until_pc(end, hint_processor) + .map_err(|err| VmException::from_vm_error(self, err))?; + self.end_run(true, false, hint_processor)?; if verify_secure { - verify_secure_runner(self, false, program_segment_size, vm)?; + verify_secure_runner(self, false, program_segment_size)?; } Ok(()) @@ -1154,32 +1141,34 @@ impl CairoRunner { // Returns Ok(()) if there are enough allocated cells for the builtins. // If not, the number of steps should be increased or a different layout should be used. - pub fn check_used_cells(&self, vm: &VirtualMachine) -> Result<(), VirtualMachineError> { - vm.builtin_runners + pub fn check_used_cells(&self) -> Result<(), VirtualMachineError> { + self.vm + .builtin_runners .iter() - .map(|builtin_runner| builtin_runner.get_used_cells_and_allocated_size(vm)) + .map(|builtin_runner| builtin_runner.get_used_cells_and_allocated_size(&self.vm)) .collect::, MemoryError>>()?; - self.check_range_check_usage(vm)?; - self.check_memory_usage(vm)?; - self.check_diluted_check_usage(vm)?; + self.check_range_check_usage()?; + self.check_memory_usage()?; + self.check_diluted_check_usage()?; Ok(()) } // Checks that there are enough trace cells to fill the entire memory range. - pub fn check_memory_usage(&self, vm: &VirtualMachine) -> Result<(), VirtualMachineError> { + pub fn check_memory_usage(&self) -> Result<(), VirtualMachineError> { let instance = &self.layout; - let builtins_memory_units: usize = vm + let builtins_memory_units: usize = self + .vm .builtin_runners .iter() - .map(|builtin_runner| builtin_runner.get_allocated_memory_units(vm)) + .map(|builtin_runner| builtin_runner.get_allocated_memory_units(&self.vm)) .collect::, MemoryError>>()? .iter() .sum(); let builtins_memory_units = builtins_memory_units as u32; - let vm_current_step_u32 = vm.current_step as u32; + let vm_current_step_u32 = self.vm.current_step as u32; // Out of the memory units available per step, a fraction is used for public memory, and // four are used for the instruction. @@ -1198,7 +1187,7 @@ impl CairoRunner { let unused_memory_units = total_memory_units - (public_memory_units + instruction_memory_units + builtins_memory_units); - let memory_address_holes = self.get_memory_holes(vm)?; + let memory_address_holes = self.get_memory_holes()?; if unused_memory_units < memory_address_holes as u32 { Err(MemoryError::InsufficientAllocatedCells( InsufficientAllocatedCellsError::MemoryAddresses(Box::new(( @@ -1217,24 +1206,20 @@ impl CairoRunner { /// Adds the segment_arena builtin if present in the program_builtins pub fn initialize_function_runner_cairo_1( &mut self, - vm: &mut VirtualMachine, program_builtins: &[BuiltinName], ) -> Result<(), RunnerError> { self.program.builtins = program_builtins.to_vec(); - self.initialize_all_builtins(vm, true)?; - self.initialize_segments(vm, self.program_base); + self.initialize_all_builtins(true)?; + self.initialize_segments(self.program_base); Ok(()) } /// Intitializes the runner in order to run cairo 0 contract entrypoints /// Initializes builtins & segments /// All builtins are initialized regardless of use - pub fn initialize_function_runner( - &mut self, - vm: &mut VirtualMachine, - ) -> Result<(), RunnerError> { - self.initialize_all_builtins(vm, false)?; - self.initialize_segments(vm, self.program_base); + pub fn initialize_function_runner(&mut self) -> Result<(), RunnerError> { + self.initialize_all_builtins(false)?; + self.initialize_segments(self.program_base); Ok(()) } @@ -1254,22 +1239,19 @@ impl CairoRunner { Ok(()) } - pub fn read_return_values( - &mut self, - vm: &mut VirtualMachine, - allow_missing_builtins: bool, - ) -> Result<(), RunnerError> { + pub fn read_return_values(&mut self, allow_missing_builtins: bool) -> Result<(), RunnerError> { if !self.run_ended { return Err(RunnerError::ReadReturnValuesNoEndRun); } - let mut pointer = vm.get_ap(); + let mut pointer = self.vm.get_ap(); for builtin_name in self.program.builtins.iter().rev() { - if let Some(builtin_runner) = vm + if let Some(builtin_runner) = self + .vm .builtin_runners .iter_mut() .find(|b| b.name() == *builtin_name) { - let new_pointer = builtin_runner.final_stack(&vm.segments, pointer)?; + let new_pointer = builtin_runner.final_stack(&self.vm.segments, pointer)?; pointer = new_pointer; } else { if !allow_missing_builtins { @@ -1277,7 +1259,7 @@ impl CairoRunner { } pointer.offset = pointer.offset.saturating_sub(1); - if !vm.get_integer(pointer)?.is_zero() { + if !self.vm.get_integer(pointer)?.is_zero() { return Err(RunnerError::MissingBuiltinStopPtrNotZero(*builtin_name)); } } @@ -1291,7 +1273,7 @@ impl CairoRunner { .as_ref() .ok_or(RunnerError::NoExecBase)?; let begin = pointer.offset - exec_base.offset; - let ap = vm.get_ap(); + let ap = self.vm.get_ap(); let end = ap.offset - exec_base.offset; self.execution_public_memory .as_mut() @@ -1304,22 +1286,23 @@ impl CairoRunner { // Iterates over the program builtins in reverse, calling BuiltinRunner::final_stack on each of them and returns the final pointer // This method is used by cairo-vm-py to replace starknet functionality pub fn get_builtins_final_stack( - &self, - vm: &mut VirtualMachine, + &mut self, stack_ptr: Relocatable, ) -> Result { let mut stack_ptr = Relocatable::from(&stack_ptr); - for runner in vm + for runner in self + .vm .builtin_runners .iter_mut() .rev() .filter(|builtin_runner| { - self.get_program_builtins() + self.program + .builtins .iter() .any(|bn| *bn == builtin_runner.name()) }) { - stack_ptr = runner.final_stack(&vm.segments, stack_ptr)? + stack_ptr = runner.final_stack(&self.vm.segments, stack_ptr)? } Ok(stack_ptr) } @@ -1330,24 +1313,24 @@ impl CairoRunner { } // Constructs and returns a CairoPie representing the current VM run. - pub fn get_cairo_pie(&self, vm: &VirtualMachine) -> Result { + pub fn get_cairo_pie(&self) -> Result { let program_base = self.program_base.ok_or(RunnerError::NoProgBase)?; let execution_base = self.execution_base.ok_or(RunnerError::NoExecBase)?; - let builtin_segments = self.get_builtin_segment_info_for_pie(vm)?; + let builtin_segments = self.get_builtin_segment_info_for_pie()?; let mut known_segment_indices = HashSet::new(); for info in builtin_segments.values() { known_segment_indices.insert(info.index); } let n_used_builtins = self.program.builtins_len(); let return_fp_addr = (execution_base + n_used_builtins)?; - let return_fp = vm.segments.memory.get_relocatable(return_fp_addr)?; - let return_pc = vm.segments.memory.get_relocatable((return_fp_addr + 1)?)?; + let return_fp = self.vm.get_relocatable(return_fp_addr)?; + let return_pc = self.vm.get_relocatable((return_fp_addr + 1)?)?; if let None | Some(false) = return_fp .segment_index .to_usize() - .and_then(|u| vm.segments.get_segment_size(u)) + .and_then(|u| self.vm.get_segment_size(u)) .map(|u| u.is_zero()) { // return_fp negative index / no size / size is zero @@ -1357,7 +1340,7 @@ impl CairoRunner { if let None | Some(false) = return_pc .segment_index .to_usize() - .and_then(|u| vm.segments.get_segment_size(u)) + .and_then(|u| self.vm.get_segment_size(u)) .map(|u| u.is_zero()) { // return_pc negative index / no size / size is zero @@ -1386,12 +1369,13 @@ impl CairoRunner { // Put all the remaining segments in extra_segments. let mut extra_segments = Vec::default(); - for index in 0..vm.segments.num_segments() { + for index in 0..self.vm.segments.num_segments() { if !known_segment_indices.contains(&(index as isize)) { extra_segments.push( ( index as isize, - vm.get_segment_size(index) + self.vm + .get_segment_size(index) .ok_or(MemoryError::MissingSegmentUsedSizes)?, ) .into(), @@ -1399,7 +1383,7 @@ impl CairoRunner { } } - let execution_size = (vm.get_ap() - execution_base)?; + let execution_size = (self.vm.get_ap() - execution_base)?; let metadata = CairoPieMetadata { program: self .get_program() @@ -1415,10 +1399,11 @@ impl CairoRunner { Ok(CairoPie { metadata, - memory: (&vm.segments.memory).into(), - execution_resources: self.get_execution_resources(vm)?, + memory: (&self.vm.segments.memory).into(), + execution_resources: self.get_execution_resources()?, additional_data: CairoPieAdditionalData( - vm.builtin_runners + self.vm + .builtin_runners .iter() .map(|b| (b.name(), b.get_additional_data())) .collect(), @@ -1427,36 +1412,33 @@ impl CairoRunner { }) } - pub fn get_air_public_input( - &self, - vm: &VirtualMachine, - ) -> Result { + pub fn get_air_public_input(&self) -> Result { PublicInput::new( &self.relocated_memory, self.layout.name.to_str(), - &vm.get_public_memory_addresses()?, - self.get_memory_segment_addresses(vm)?, + &self.vm.get_public_memory_addresses()?, + self.get_memory_segment_addresses()?, self.relocated_trace .as_ref() .ok_or(PublicInputError::EmptyTrace)?, - self.get_perm_range_check_limits(vm) + self.get_perm_range_check_limits() .ok_or(PublicInputError::NoRangeCheckLimits)?, ) } - pub fn get_air_private_input(&self, vm: &VirtualMachine) -> AirPrivateInput { + pub fn get_air_private_input(&self) -> AirPrivateInput { let mut private_inputs = HashMap::new(); - for builtin in vm.builtin_runners.iter() { - private_inputs.insert(builtin.name(), builtin.air_private_input(&vm.segments)); + for builtin in self.vm.builtin_runners.iter() { + private_inputs.insert(builtin.name(), builtin.air_private_input(&self.vm.segments)); } AirPrivateInput(private_inputs) } pub fn get_memory_segment_addresses( &self, - vm: &VirtualMachine, ) -> Result, VirtualMachineError> { - let relocation_table = vm + let relocation_table = self + .vm .relocation_table .as_ref() .ok_or(MemoryError::UnrelocatedMemory)?; @@ -1469,7 +1451,8 @@ impl CairoRunner { Ok((*base, base + stop_ptr_offset)) }; - vm.builtin_runners + self.vm + .builtin_runners .iter() .map(|builtin| -> Result<_, VirtualMachineError> { let (base, stop_ptr) = builtin.get_memory_segment_addresses(); @@ -1608,11 +1591,10 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn check_memory_usage_ok_case() { let program = program![BuiltinName::range_check, BuiltinName::output]; - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.segment_used_sizes = Some(vec![4]); + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); - assert_matches!(cairo_runner.check_memory_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_memory_usage(), Ok(())); } #[test] @@ -1620,19 +1602,22 @@ mod tests { fn check_memory_usage_err_case() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.builtin_runners = vec![{ + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner.initialize_segments(&mut cairo_runner.vm.segments); builtin_runner }]; - vm.segments.segment_used_sizes = Some(vec![4, 12]); - vm.segments.memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 1)]; - vm.segments.memory.mark_as_accessed((0, 0).into()); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4, 12]); + cairo_runner.vm.segments.memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 1)]; + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((0, 0).into()); assert_matches!( - cairo_runner.check_memory_usage(&vm), + cairo_runner.check_memory_usage(), Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) @@ -1643,19 +1628,17 @@ 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, LayoutName::plain); - let mut vm = vm!(); - assert!(cairo_runner.initialize_builtins(&mut vm, false).is_err()); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain); + assert!(cairo_runner.initialize_builtins(false).is_err()); } #[test] #[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, LayoutName::plain); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain); assert_matches!( - cairo_runner.initialize_builtins(&mut vm, false), + cairo_runner.initialize_builtins(false), Err(RunnerError::NoBuiltinForInstance(_)) ) } @@ -1664,9 +1647,8 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initialize_builtins_missing_builtins_allow_missing() { let program = program![BuiltinName::output, BuiltinName::ecdsa]; - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - assert!(cairo_runner.initialize_builtins(&mut vm, true).is_ok()) + let mut cairo_runner = cairo_runner!(program); + assert!(cairo_runner.initialize_builtins(true).is_ok()) } #[test] @@ -1674,14 +1656,13 @@ mod tests { fn initialize_segments_with_base() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); let program_base = Some(Relocatable { segment_index: 5, offset: 9, }); - add_segments!(vm, 6); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, program_base); + add_segments!(&mut cairo_runner.vm, 6); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(program_base); assert_eq!( cairo_runner.program_base, Some(Relocatable { @@ -1696,10 +1677,13 @@ mod tests { offset: 0, }) ); - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[0].base(), 7); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 7); - assert_eq!(vm.segments.num_segments(), 8); + assert_eq!(cairo_runner.vm.segments.num_segments(), 8); } #[test] @@ -1707,9 +1691,8 @@ mod tests { fn initialize_segments_no_base() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); assert_eq!( cairo_runner.program_base, Some(Relocatable { @@ -1724,10 +1707,13 @@ mod tests { offset: 0 }) ); - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[0].base(), 2); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); - assert_eq!(vm.segments.num_segments(), 3); + assert_eq!(cairo_runner.vm.segments.num_segments(), 3); } #[test] @@ -1735,12 +1721,11 @@ mod tests { fn initialize_state_empty_data_and_stack() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner.program_base = Some(relocatable!(1, 0)); cairo_runner.execution_base = Some(relocatable!(2, 0)); let stack = Vec::new(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_state(&mut vm, 1, stack).unwrap(); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_state(1, stack).unwrap(); assert_eq!( cairo_runner.initial_pc, Some(Relocatable { @@ -1758,9 +1743,8 @@ mod tests { data = vec_data!((4), (6)), ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..2 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.program_base = Some(Relocatable { segment_index: 1, @@ -1768,8 +1752,8 @@ mod tests { }); cairo_runner.execution_base = Some(relocatable!(2, 0)); let stack = Vec::new(); - cairo_runner.initialize_state(&mut vm, 1, stack).unwrap(); - check_memory!(vm.segments.memory, ((1, 0), 4), ((1, 1), 6)); + cairo_runner.initialize_state(1, stack).unwrap(); + check_memory!(cairo_runner.vm.segments.memory, ((1, 0), 4), ((1, 1), 6)); } #[test] @@ -1777,15 +1761,14 @@ mod tests { fn initialize_state_empty_data_some_stack() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..3 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.program_base = Some(relocatable!(1, 0)); cairo_runner.execution_base = Some(relocatable!(2, 0)); let stack = vec![mayberelocatable!(4), mayberelocatable!(6)]; - cairo_runner.initialize_state(&mut vm, 1, stack).unwrap(); - check_memory!(vm.segments.memory, ((2, 0), 4), ((2, 1), 6)); + cairo_runner.initialize_state(1, stack).unwrap(); + check_memory!(cairo_runner.vm.segments.memory, ((2, 0), 4), ((2, 1), 6)); } #[test] @@ -1793,9 +1776,8 @@ mod tests { fn initialize_state_no_program_base() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..2 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.execution_base = Some(Relocatable { segment_index: 2, @@ -1805,7 +1787,7 @@ mod tests { MaybeRelocatable::from(Felt252::from(4_i32)), MaybeRelocatable::from(Felt252::from(6_i32)), ]; - assert!(cairo_runner.initialize_state(&mut vm, 1, stack).is_err()); + assert!(cairo_runner.initialize_state(1, stack).is_err()); } #[test] @@ -1813,16 +1795,15 @@ mod tests { fn initialize_state_no_execution_base() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..2 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.program_base = Some(relocatable!(1, 0)); let stack = vec![ MaybeRelocatable::from(Felt252::from(4_i32)), MaybeRelocatable::from(Felt252::from(6_i32)), ]; - cairo_runner.initialize_state(&mut vm, 1, stack).unwrap(); + cairo_runner.initialize_state(1, stack).unwrap(); } #[test] @@ -1830,20 +1811,23 @@ mod tests { fn initialize_function_entrypoint_empty_stack() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..2 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.program_base = Some(relocatable!(0, 0)); cairo_runner.execution_base = Some(relocatable!(1, 0)); let stack = Vec::new(); let return_fp = MaybeRelocatable::from(Felt252::from(9_i32)); cairo_runner - .initialize_function_entrypoint(&mut vm, 0, stack, return_fp) + .initialize_function_entrypoint(0, stack, return_fp) .unwrap(); assert_eq!(cairo_runner.initial_fp, cairo_runner.initial_ap); assert_eq!(cairo_runner.initial_fp, Some(relocatable!(1, 2))); - check_memory!(vm.segments.memory, ((1, 0), 9), ((1, 1), (2, 0))); + check_memory!( + cairo_runner.vm.segments.memory, + ((1, 0), 9), + ((1, 1), (2, 0)) + ); } #[test] @@ -1851,21 +1835,20 @@ mod tests { fn initialize_function_entrypoint_some_stack() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..2 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.program_base = Some(relocatable!(0, 0)); cairo_runner.execution_base = Some(relocatable!(1, 0)); let stack = vec![MaybeRelocatable::from(Felt252::from(7_i32))]; let return_fp = MaybeRelocatable::from(Felt252::from(9_i32)); cairo_runner - .initialize_function_entrypoint(&mut vm, 1, stack, return_fp) + .initialize_function_entrypoint(1, stack, return_fp) .unwrap(); assert_eq!(cairo_runner.initial_fp, cairo_runner.initial_ap); assert_eq!(cairo_runner.initial_fp, Some(relocatable!(1, 3))); check_memory!( - vm.segments.memory, + cairo_runner.vm.segments.memory, ((1, 0), 7), ((1, 1), 9), ((1, 2), (2, 0)) @@ -1877,11 +1860,10 @@ mod tests { fn initialize_function_entrypoint_no_execution_base() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); let stack = vec![MaybeRelocatable::from(Felt252::from(7_i32))]; let return_fp = MaybeRelocatable::from(Felt252::from(9_i32)); cairo_runner - .initialize_function_entrypoint(&mut vm, 1, stack, return_fp) + .initialize_function_entrypoint(1, stack, return_fp) .unwrap(); } @@ -1890,8 +1872,7 @@ mod tests { fn initialize_main_entrypoint_no_main() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); + cairo_runner.initialize_main_entrypoint().unwrap(); } #[test] @@ -1899,10 +1880,9 @@ mod tests { fn initialize_main_entrypoint() { let program = program!(main = Some(1),); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner.program_base = Some(relocatable!(0, 0)); cairo_runner.execution_base = Some(relocatable!(0, 0)); - let return_pc = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); + let return_pc = cairo_runner.initialize_main_entrypoint().unwrap(); assert_eq!(return_pc, Relocatable::from((1, 0))); } @@ -1919,11 +1899,12 @@ mod tests { .unwrap(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize(&mut vm, false).unwrap(); + cairo_runner.initialize(false).unwrap(); assert_eq!( - vm.segments + cairo_runner + .vm + .segments .memory .get_amount_of_accessed_addresses_for_segment(0), Some(24) @@ -1935,15 +1916,14 @@ mod tests { fn initialize_vm_no_builtins() { let program = program!(main = Some(1),); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner.program_base = Some(relocatable!(0, 0)); cairo_runner.initial_pc = Some(relocatable!(0, 1)); cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); - cairo_runner.initialize_vm(&mut vm).unwrap(); - assert_eq!(vm.run_context.pc, relocatable!(0, 1)); - assert_eq!(vm.run_context.ap, 2); - assert_eq!(vm.run_context.fp, 2); + cairo_runner.initialize_vm().unwrap(); + assert_eq!(cairo_runner.vm.run_context.pc, relocatable!(0, 1)); + assert_eq!(cairo_runner.vm.run_context.ap, 2); + assert_eq!(cairo_runner.vm.run_context.fp, 2); } #[test] @@ -1951,27 +1931,31 @@ mod tests { fn initialize_vm_with_range_check_valid() { let program = program!(builtins = vec![BuiltinName::range_check], main = Some(1),); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner.initial_pc = Some(relocatable!(0, 1)); cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - vm.segments = segments![((2, 0), 23), ((2, 1), 233)]; - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::range_check); - assert_eq!(vm.builtin_runners[0].base(), 2); - cairo_runner.initialize_vm(&mut vm).unwrap(); - assert!(vm + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + cairo_runner.vm.segments = segments![((2, 0), 23), ((2, 1), 233)]; + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::range_check + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); + cairo_runner.initialize_vm().unwrap(); + assert!(cairo_runner + .vm .segments .memory .validated_addresses .contains(&Relocatable::from((2, 0)))); - assert!(vm + assert!(cairo_runner + .vm .segments .memory .validated_addresses .contains(&Relocatable::from((2, 1)))); - assert_eq!(vm.segments.memory.validated_addresses.len(), 2); + assert_eq!(cairo_runner.vm.segments.memory.validated_addresses.len(), 2); } #[test] @@ -1979,16 +1963,15 @@ mod tests { fn initialize_vm_with_range_check_invalid() { let program = program!(builtins = vec![BuiltinName::range_check], main = Some(1),); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner.initial_pc = Some(relocatable!(0, 1)); cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - vm.segments = segments![((2, 1), 23), ((2, 4), (-1))]; + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + cairo_runner.vm.segments = segments![((2, 1), 23), ((2, 4), (-1))]; assert_eq!( - cairo_runner.initialize_vm(&mut vm), + cairo_runner.initialize_vm(), Err(RunnerError::MemoryValidationError( MemoryError::RangeCheckFoundNonInt(Box::new((2, 0).into())) )) @@ -2032,10 +2015,9 @@ mod tests { main = Some(3), ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_segments(&mut vm, None); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_segments(None); + cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); assert_eq!(cairo_runner.program_base, Some(relocatable!(0, 0))); assert_eq!(cairo_runner.execution_base, Some(relocatable!(1, 0))); @@ -2043,12 +2025,12 @@ mod tests { //RunContext check //Registers - assert_eq!(vm.run_context.pc, relocatable!(0, 3)); - assert_eq!(vm.run_context.ap, 2); - assert_eq!(vm.run_context.fp, 2); + assert_eq!(cairo_runner.vm.run_context.pc, relocatable!(0, 3)); + assert_eq!(cairo_runner.vm.run_context.ap, 2); + assert_eq!(cairo_runner.vm.run_context.fp, 2); //Memory check_memory!( - vm.segments.memory, + cairo_runner.vm.segments.memory, ((0, 0), 5207990763031199744_u64), ((0, 1), 2), ((0, 2), 2345108766317314046_u64), @@ -2105,12 +2087,11 @@ mod tests { main = Some(4), ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); assert_eq!(cairo_runner.program_base, Some(relocatable!(0, 0))); assert_eq!(cairo_runner.execution_base, Some(relocatable!(1, 0))); @@ -2118,12 +2099,12 @@ mod tests { //RunContext check //Registers - assert_eq!(vm.run_context.pc, relocatable!(0, 4)); - assert_eq!(vm.run_context.ap, 3); - assert_eq!(vm.run_context.fp, 3); + assert_eq!(cairo_runner.vm.run_context.pc, relocatable!(0, 4)); + assert_eq!(cairo_runner.vm.run_context.ap, 3); + assert_eq!(cairo_runner.vm.run_context.fp, 3); //Memory check_memory!( - vm.segments.memory, + cairo_runner.vm.segments.memory, ((0, 0), 4612671182993129469_u64), ((0, 1), 5198983563776393216_u64), ((0, 2), 1), @@ -2194,12 +2175,11 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); assert_eq!(cairo_runner.program_base, Some(relocatable!(0, 0))); assert_eq!(cairo_runner.execution_base, Some(relocatable!(1, 0))); @@ -2207,12 +2187,12 @@ mod tests { //RunContext check //Registers - assert_eq!(vm.run_context.pc, relocatable!(0, 8)); - assert_eq!(vm.run_context.ap, 3); - assert_eq!(vm.run_context.fp, 3); + assert_eq!(cairo_runner.vm.run_context.pc, relocatable!(0, 8)); + assert_eq!(cairo_runner.vm.run_context.ap, 3); + assert_eq!(cairo_runner.vm.run_context.fp, 3); //Memory check_memory!( - vm.segments.memory, + cairo_runner.vm.segments.memory, ((0, 0), 4612671182993129469_u64), ((0, 1), 5189976364521848832_u64), ((0, 2), 18446744073709551615_u128), @@ -2277,27 +2257,23 @@ mod tests { main = Some(3), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); assert_eq!(end, Relocatable::from((3, 0))); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_vm().unwrap(); //Execution Phase - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); //Check final values against Python VM //Check final register values - assert_eq!(vm.run_context.pc, Relocatable::from((3, 0))); + assert_eq!(cairo_runner.vm.run_context.pc, Relocatable::from((3, 0))); - assert_eq!(vm.run_context.ap, 6); + assert_eq!(cairo_runner.vm.run_context.ap, 6); - assert_eq!(vm.run_context.fp, 0); + assert_eq!(cairo_runner.vm.run_context.fp, 0); //Check each TraceEntry in trace - let trace = vm.trace.unwrap(); + let trace = cairo_runner.vm.trace.unwrap(); assert_eq!(trace.len(), 5); trace_check( &trace, @@ -2359,27 +2335,23 @@ mod tests { main = Some(8), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); //Execution Phase - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); //Check final values against Python VM //Check final register values - assert_eq!(vm.run_context.pc, Relocatable::from((4, 0))); + assert_eq!(cairo_runner.vm.run_context.pc, Relocatable::from((4, 0))); - assert_eq!(vm.run_context.ap, 10); + assert_eq!(cairo_runner.vm.run_context.ap, 10); - assert_eq!(vm.run_context.fp, 0); + assert_eq!(cairo_runner.vm.run_context.fp, 0); //Check each TraceEntry in trace - let trace = vm.trace.unwrap(); + let trace = cairo_runner.vm.trace.unwrap(); assert_eq!(trace.len(), 10); trace_check( &trace, @@ -2397,15 +2369,19 @@ mod tests { ], ); //Check the range_check builtin segment - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::range_check); - assert_eq!(vm.builtin_runners[0].base(), 2); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::range_check + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); check_memory!( - vm.segments.memory, + cairo_runner.vm.segments.memory, ((2, 0), 7), ((2, 1), 18446744073709551608_i128) ); - assert!(vm + assert!(cairo_runner + .vm .segments .memory .get(&MaybeRelocatable::from((2, 2))) @@ -2474,28 +2450,24 @@ mod tests { main = Some(4), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); //Execution Phase - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); //Check final values against Python VM //Check final register values //todo - assert_eq!(vm.run_context.pc, Relocatable::from((4, 0))); + assert_eq!(cairo_runner.vm.run_context.pc, Relocatable::from((4, 0))); - assert_eq!(vm.run_context.ap, 12); + assert_eq!(cairo_runner.vm.run_context.ap, 12); - assert_eq!(vm.run_context.fp, 0); + assert_eq!(cairo_runner.vm.run_context.fp, 0); //Check each TraceEntry in trace - let trace = vm.trace.unwrap(); + let trace = cairo_runner.vm.trace.unwrap(); assert_eq!(trace.len(), 12); trace_check( &trace, @@ -2515,10 +2487,14 @@ mod tests { ], ); //Check that the output to be printed is correct - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[0].base(), 2); - check_memory!(vm.segments.memory, ((2, 0), 1), ((2, 1), 17)); - assert!(vm + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); + check_memory!(cairo_runner.vm.segments.memory, ((2, 0), 1), ((2, 1), 17)); + assert!(cairo_runner + .vm .segments .memory .get(&MaybeRelocatable::from((2, 2))) @@ -2613,27 +2589,23 @@ mod tests { main = Some(13), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); //Execution Phase - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); //Check final values against Python VM //Check final register values - assert_eq!(vm.run_context.pc, Relocatable::from((5, 0))); + assert_eq!(cairo_runner.vm.run_context.pc, Relocatable::from((5, 0))); - assert_eq!(vm.run_context.ap, 18); + assert_eq!(cairo_runner.vm.run_context.ap, 18); - assert_eq!(vm.run_context.fp, 0); + assert_eq!(cairo_runner.vm.run_context.fp, 0); //Check each TraceEntry in trace - let trace = vm.trace.unwrap(); + let trace = cairo_runner.vm.trace.unwrap(); assert_eq!(trace.len(), 18); trace_check( &trace, @@ -2659,26 +2631,34 @@ mod tests { ], ); //Check the range_check builtin segment - assert_eq!(vm.builtin_runners[1].name(), BuiltinName::range_check); - assert_eq!(vm.builtin_runners[1].base(), 3); + assert_eq!( + cairo_runner.vm.builtin_runners[1].name(), + BuiltinName::range_check + ); + assert_eq!(cairo_runner.vm.builtin_runners[1].base(), 3); check_memory!( - vm.segments.memory, + cairo_runner.vm.segments.memory, ((3, 0), 7), ((3, 1), 18446744073709551608_i128) ); - assert!(vm + assert!(cairo_runner + .vm .segments .memory .get(&MaybeRelocatable::from((2, 2))) .is_none()); //Check the output segment - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[0].base(), 2); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); - check_memory!(vm.segments.memory, ((2, 0), 7)); - assert!(vm + check_memory!(cairo_runner.vm.segments.memory, ((2, 0), 7)); + assert!(cairo_runner + .vm .segments .memory .get(&(MaybeRelocatable::from((2, 1)))) @@ -2713,54 +2693,66 @@ mod tests { */ fn relocate_memory_with_gap() { let program = program!(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); for _ in 0..4 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } // Memory initialization without macro - vm.segments + cairo_runner + .vm + .segments .memory .insert( Relocatable::from((0, 0)), &MaybeRelocatable::from(Felt252::from(4613515612218425347_i64)), ) .unwrap(); - vm.segments + cairo_runner + .vm + .segments .memory .insert( Relocatable::from((0, 1)), &MaybeRelocatable::from(Felt252::from(5)), ) .unwrap(); - vm.segments + cairo_runner + .vm + .segments .memory .insert( Relocatable::from((0, 2)), &MaybeRelocatable::from(Felt252::from(2345108766317314046_i64)), ) .unwrap(); - vm.segments + cairo_runner + .vm + .segments .memory .insert(Relocatable::from((1, 0)), &MaybeRelocatable::from((2, 0))) .unwrap(); - vm.segments + cairo_runner + .vm + .segments .memory .insert(Relocatable::from((1, 1)), &MaybeRelocatable::from((3, 0))) .unwrap(); - vm.segments + cairo_runner + .vm + .segments .memory .insert( Relocatable::from((1, 5)), &MaybeRelocatable::from(Felt252::from(5)), ) .unwrap(); - vm.segments.compute_effective_sizes(); - let rel_table = vm + cairo_runner.vm.segments.compute_effective_sizes(); + let rel_table = cairo_runner + .vm .segments .relocate_segments() .expect("Couldn't relocate after compute effective sizes"); - assert_eq!(cairo_runner.relocate_memory(&vm, &rel_table), Ok(())); + assert_eq!(cairo_runner.relocate_memory(&rel_table), Ok(())); assert_eq!(cairo_runner.relocated_memory[0], None); assert_eq!( cairo_runner.relocated_memory[1], @@ -2851,22 +2843,19 @@ mod tests { main = Some(4), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); - vm.segments.compute_effective_sizes(); - let rel_table = vm + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); + cairo_runner.vm.segments.compute_effective_sizes(); + let rel_table = cairo_runner + .vm .segments .relocate_segments() .expect("Couldn't relocate after compute effective sizes"); - assert_eq!(cairo_runner.relocate_memory(&vm, &rel_table), Ok(())); + assert_eq!(cairo_runner.relocate_memory(&rel_table), Ok(())); assert_eq!(cairo_runner.relocated_memory[0], None); assert_eq!( cairo_runner.relocated_memory[1], @@ -2990,22 +2979,19 @@ mod tests { main = Some(4), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); - vm.segments.compute_effective_sizes(); - let rel_table = vm + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); + cairo_runner.vm.segments.compute_effective_sizes(); + let rel_table = cairo_runner + .vm .segments .relocate_segments() .expect("Couldn't relocate after compute effective sizes"); - cairo_runner.relocate_trace(&vm, &rel_table).unwrap(); + cairo_runner.relocate_trace(&rel_table).unwrap(); let relocated_trace = cairo_runner.relocated_trace.unwrap(); assert_eq!(relocated_trace.len(), 12); assert_eq!( @@ -3111,17 +3097,19 @@ mod tests { fn write_output_from_preset_memory() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[0].base(), 2); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); - vm.segments = segments![((2, 0), 1), ((2, 1), 2)]; - vm.segments.segment_used_sizes = Some(vec![0, 0, 2]); + cairo_runner.vm.segments = segments![((2, 0), 1), ((2, 1), 2)]; + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0, 0, 2]); let mut output_buffer = String::new(); - vm.write_output(&mut output_buffer).unwrap(); + cairo_runner.vm.write_output(&mut output_buffer).unwrap(); assert_eq!(&output_buffer, "1\n2\n"); } @@ -3166,20 +3154,16 @@ mod tests { main = Some(4), ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); //Execution Phase let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); let mut output_buffer = String::new(); - vm.write_output(&mut output_buffer).unwrap(); + cairo_runner.vm.write_output(&mut output_buffer).unwrap(); assert_eq!(&output_buffer, "1\n17\n"); } @@ -3207,20 +3191,16 @@ mod tests { main = Some(0), ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + let end = cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); //Execution Phase let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); let mut output_buffer = String::new(); - vm.write_output(&mut output_buffer).unwrap(); + cairo_runner.vm.write_output(&mut output_buffer).unwrap(); assert_eq!(&output_buffer, "\n2:0\n"); } @@ -3229,22 +3209,24 @@ mod tests { fn write_output_from_preset_memory_neg_output() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[0].base(), 2); - vm.segments = segments![( + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!(cairo_runner.vm.builtin_runners[0].base(), 2); + cairo_runner.vm.segments = segments![( (2, 0), ( "800000000000011000000000000000000000000000000000000000000000000", 16 ) )]; - vm.segments.segment_used_sizes = Some(vec![0, 0, 1]); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0, 0, 1]); let mut output_buffer = String::new(); - vm.write_output(&mut output_buffer).unwrap(); + cairo_runner.vm.write_output(&mut output_buffer).unwrap(); assert_eq!(&output_buffer, "-1\n"); } @@ -3281,33 +3263,29 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner - .initialize_builtins(&mut vm, false) + .initialize_builtins(false) .expect("Couldn't initialize builtins."); // Swap the first and second builtins (first should be `output`). - vm.builtin_runners.swap(0, 1); + cairo_runner.vm.builtin_runners.swap(0, 1); cairo_runner.program.builtins.swap(0, 1); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(None); let end = cairo_runner - .initialize_main_entrypoint(&mut vm) + .initialize_main_entrypoint() .expect("Couldn't initialize the main entrypoint."); cairo_runner - .initialize_vm(&mut vm) - .expect("Couldn't initialize the VM."); + .initialize_vm() + .expect("Couldn't initialize the cairo_runner.VM."); let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_matches!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(cairo_runner.run_until_pc(end, &mut hint_processor), Ok(())); let mut output_buffer = String::new(); - vm.write_output(&mut output_buffer).unwrap(); + cairo_runner.vm.write_output(&mut output_buffer).unwrap(); assert_eq!(&output_buffer, "1\n17\n"); } @@ -3321,14 +3299,28 @@ mod tests { BuiltinName::bitwise, BuiltinName::ec_op ]; - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - assert_eq!(vm.builtin_runners[0].name(), BuiltinName::output); - assert_eq!(vm.builtin_runners[1].name(), BuiltinName::pedersen); - assert_eq!(vm.builtin_runners[2].name(), BuiltinName::range_check); - assert_eq!(vm.builtin_runners[3].name(), BuiltinName::bitwise); - assert_eq!(vm.builtin_runners[4].name(), BuiltinName::ec_op); + let mut cairo_runner = cairo_runner!(program); + cairo_runner.initialize_builtins(false).unwrap(); + assert_eq!( + cairo_runner.vm.builtin_runners[0].name(), + BuiltinName::output + ); + assert_eq!( + cairo_runner.vm.builtin_runners[1].name(), + BuiltinName::pedersen + ); + assert_eq!( + cairo_runner.vm.builtin_runners[2].name(), + BuiltinName::range_check + ); + assert_eq!( + cairo_runner.vm.builtin_runners[3].name(), + BuiltinName::bitwise + ); + assert_eq!( + cairo_runner.vm.builtin_runners[4].name(), + BuiltinName::ec_op + ); } #[test] @@ -3378,22 +3370,17 @@ mod tests { ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(&program); - - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); // Full takes 10 steps. + assert_matches!(cairo_runner.run_for_steps(8, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_for_steps(8, &mut vm, &mut hint_processor), - Ok(()) - ); - assert_matches!( - cairo_runner.run_for_steps(8, &mut vm, &mut hint_processor), + cairo_runner.run_for_steps(8, &mut hint_processor), Err(VirtualMachineError::EndOfProgram(x)) if x == 8 - 2 ); } @@ -3402,10 +3389,9 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_empty() { let program = program!(); - let mut cairo_runner = cairo_runner!(&program); - let mut vm = vm!(true); + let mut cairo_runner = cairo_runner!(&program, LayoutName::all_cairo, false, true); assert_matches!( - cairo_runner.initialize(&mut vm, false), + cairo_runner.initialize(false), Err(RunnerError::MissingMain) ); } @@ -3457,26 +3443,21 @@ mod tests { ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(&program); - - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); // Full takes 10 steps. + assert_matches!(cairo_runner.run_until_steps(8, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_until_steps(8, &mut vm, &mut hint_processor), + cairo_runner.run_until_steps(10, &mut hint_processor), Ok(()) ); assert_matches!( - cairo_runner.run_until_steps(10, &mut vm, &mut hint_processor), - Ok(()) - ); - assert_matches!( - cairo_runner.run_until_steps(11, &mut vm, &mut hint_processor), + cairo_runner.run_until_steps(11, &mut hint_processor), Err(VirtualMachineError::EndOfProgram(1)) ); } @@ -3530,65 +3511,48 @@ mod tests { ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(&program); - - let mut vm = vm!(true); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); - cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); - cairo_runner.initialize_vm(&mut vm).unwrap(); + cairo_runner.initialize_main_entrypoint().unwrap(); + cairo_runner.initialize_vm().unwrap(); // Full takes 10 steps. + assert_matches!(cairo_runner.run_for_steps(1, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), + cairo_runner.run_until_next_power_of_2(&mut hint_processor), Ok(()) ); - assert_matches!( - cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), - Ok(()) - ); - assert_eq!(vm.current_step, 1); + assert_eq!(cairo_runner.vm.current_step, 1); + assert_matches!(cairo_runner.run_for_steps(1, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), + cairo_runner.run_until_next_power_of_2(&mut hint_processor), Ok(()) ); - assert_matches!( - cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), - Ok(()) - ); - assert_eq!(vm.current_step, 2); + assert_eq!(cairo_runner.vm.current_step, 2); + assert_matches!(cairo_runner.run_for_steps(1, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()) - ); - assert_matches!( - cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), + cairo_runner.run_until_next_power_of_2(&mut hint_processor), Ok(()) ); - assert_eq!(vm.current_step, 4); + assert_eq!(cairo_runner.vm.current_step, 4); + assert_matches!(cairo_runner.run_for_steps(1, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()) - ); - assert_matches!( - cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), + cairo_runner.run_until_next_power_of_2(&mut hint_processor), Ok(()) ); - assert_eq!(vm.current_step, 8); + assert_eq!(cairo_runner.vm.current_step, 8); + assert_matches!(cairo_runner.run_for_steps(1, &mut hint_processor), Ok(())); assert_matches!( - cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()) - ); - assert_matches!( - cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), + cairo_runner.run_until_next_power_of_2(&mut hint_processor), Err(VirtualMachineError::EndOfProgram(6)) ); - assert_eq!(vm.current_step, 10); + assert_eq!(cairo_runner.vm.current_step, 10); } #[test] @@ -3608,15 +3572,18 @@ mod tests { fn get_memory_holes_missing_segment_used_sizes() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); // Add element into memory and mark it as accessed so that get_memory_holes tries to access a segment size - vm.segments.memory = memory![((0, 0), 9)]; - vm.segments.memory.mark_as_accessed((0, 0).into()); + cairo_runner.vm.segments.memory = memory![((0, 0), 9)]; + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((0, 0).into()); - vm.builtin_runners = Vec::new(); + cairo_runner.vm.builtin_runners = Vec::new(); assert_eq!( - cairo_runner.get_memory_holes(&vm), + cairo_runner.get_memory_holes(), Err(MemoryError::MissingSegmentUsedSizes), ); } @@ -3626,12 +3593,11 @@ mod tests { fn get_memory_holes_empty() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.builtin_runners = Vec::new(); - vm.segments.segment_used_sizes = Some(Vec::new()); - assert_eq!(cairo_runner.get_memory_holes(&vm), Ok(0)); + cairo_runner.vm.builtin_runners = Vec::new(); + cairo_runner.vm.segments.segment_used_sizes = Some(Vec::new()); + assert_eq!(cairo_runner.get_memory_holes(), Ok(0)); } #[test] @@ -3639,14 +3605,21 @@ mod tests { fn get_memory_holes_empty_builtins() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.memory = memory![((0, 0), 0), ((0, 2), 0)]; - vm.segments.memory.mark_as_accessed((0, 0).into()); - vm.segments.memory.mark_as_accessed((0, 2).into()); - vm.builtin_runners = Vec::new(); - vm.segments.segment_used_sizes = Some(vec![4]); - assert_eq!(cairo_runner.get_memory_holes(&vm), Ok(2)); + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.memory = memory![((0, 0), 0), ((0, 2), 0)]; + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((0, 0).into()); + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((0, 2).into()); + cairo_runner.vm.builtin_runners = Vec::new(); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); + assert_eq!(cairo_runner.get_memory_holes(), Ok(2)); } #[test] @@ -3654,17 +3627,16 @@ mod tests { fn get_memory_holes_empty_accesses() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.builtin_runners = vec![{ + cairo_runner.vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner.initialize_segments(&mut cairo_runner.vm.segments); builtin_runner }]; - vm.segments.segment_used_sizes = Some(vec![4]); - assert_eq!(cairo_runner.get_memory_holes(&vm), Ok(0)); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); + assert_eq!(cairo_runner.get_memory_holes(), Ok(0)); } #[test] @@ -3672,19 +3644,26 @@ mod tests { fn get_memory_holes() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.memory = memory![((1, 0), 0), ((1, 2), 2)]; - vm.segments.memory.mark_as_accessed((1, 0).into()); - vm.segments.memory.mark_as_accessed((1, 2).into()); - vm.builtin_runners = vec![{ + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.memory = memory![((1, 0), 0), ((1, 2), 2)]; + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((1, 0).into()); + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((1, 2).into()); + cairo_runner.vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner.initialize_segments(&mut cairo_runner.vm.segments); builtin_runner }]; - vm.segments.segment_used_sizes = Some(vec![4, 4]); - assert_eq!(cairo_runner.get_memory_holes(&vm), Ok(2)); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4, 4]); + assert_eq!(cairo_runner.get_memory_holes(), Ok(2)); } /// Test that check_diluted_check_usage() works without a diluted pool @@ -3695,10 +3674,9 @@ mod tests { let program = program!(); let mut cairo_runner = cairo_runner!(program); - let vm = vm!(); cairo_runner.layout.diluted_pool_instance_def = None; - assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_diluted_check_usage(), Ok(())); } /// Test that check_diluted_check_usage() works without builtin runners. @@ -3707,12 +3685,11 @@ mod tests { fn check_diluted_check_usage_without_builtin_runners() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.current_step = 10000; - vm.builtin_runners = vec![]; - assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); + cairo_runner.vm.current_step = 10000; + cairo_runner.vm.builtin_runners = vec![]; + assert_matches!(cairo_runner.check_diluted_check_usage(), Ok(())); } /// Test that check_diluted_check_usage() fails when there aren't enough @@ -3722,13 +3699,12 @@ mod tests { fn check_diluted_check_usage_insufficient_allocated_cells() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.current_step = 100; - vm.builtin_runners = vec![]; + cairo_runner.vm.current_step = 100; + cairo_runner.vm.builtin_runners = vec![]; assert_matches!( - cairo_runner.check_diluted_check_usage(&vm), + cairo_runner.check_diluted_check_usage(), Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) @@ -3742,12 +3718,11 @@ mod tests { fn check_diluted_check_usage() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.current_step = 8192; - vm.builtin_runners = vec![BitwiseBuiltinRunner::new(Some(256), true).into()]; - assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); + cairo_runner.vm.current_step = 8192; + cairo_runner.vm.builtin_runners = vec![BitwiseBuiltinRunner::new(Some(256), true).into()]; + assert_matches!(cairo_runner.check_diluted_check_usage(), Ok(())); } #[test] @@ -3757,11 +3732,10 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner.run_ended = true; assert_matches!( - cairo_runner.end_run(true, false, &mut vm, &mut hint_processor), + cairo_runner.end_run(true, false, &mut hint_processor), Err(VirtualMachineError::RunnerError( RunnerError::EndRunCalledTwice )) @@ -3775,17 +3749,16 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); assert_matches!( - cairo_runner.end_run(true, false, &mut vm, &mut hint_processor), + cairo_runner.end_run(true, false, &mut hint_processor), Ok(()) ); cairo_runner.run_ended = false; cairo_runner.relocated_memory.clear(); assert_matches!( - cairo_runner.end_run(true, true, &mut vm, &mut hint_processor), + cairo_runner.end_run(true, true, &mut hint_processor), Ok(()) ); assert!(!cairo_runner.run_ended); @@ -3801,15 +3774,14 @@ mod tests { .unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, true); - let mut vm = vm!(true); + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, true, true); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); + let end = cairo_runner.initialize(false).unwrap(); cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) + .run_until_pc(end, &mut hint_processor) .expect("Call to `CairoRunner::run_until_pc()` failed."); assert_matches!( - cairo_runner.end_run(false, false, &mut vm, &mut hint_processor), + cairo_runner.end_run(false, false, &mut hint_processor), Ok(()) ); } @@ -3820,9 +3792,8 @@ mod tests { let program = program!(); let cairo_runner = cairo_runner!(program); - let vm = vm!(); - assert_eq!(cairo_runner.get_builtin_segments_info(&vm), Ok(Vec::new()),); + assert_eq!(cairo_runner.get_builtin_segments_info(), Ok(Vec::new()),); } #[test] @@ -3830,12 +3801,12 @@ mod tests { fn get_builtin_segments_info_base_not_finished() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.builtin_runners = vec![BuiltinRunner::Output(OutputBuiltinRunner::new(true))]; + cairo_runner.vm.builtin_runners = + vec![BuiltinRunner::Output(OutputBuiltinRunner::new(true))]; assert_eq!( - cairo_runner.get_builtin_segments_info(&vm), + cairo_runner.get_builtin_segments_info(), Err(RunnerError::NoStopPointer(Box::new(BuiltinName::output))), ); } @@ -3845,13 +3816,12 @@ mod tests { fn get_execution_resources_trace_not_enabled() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm: VirtualMachine = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.segments.segment_used_sizes = Some(vec![4]); - vm.current_step = 10; + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); + cairo_runner.vm.current_step = 10; assert_eq!( - cairo_runner.get_execution_resources(&vm), + cairo_runner.get_execution_resources(), Ok(ExecutionResources { n_steps: 10, n_memory_holes: 0, @@ -3874,8 +3844,8 @@ mod tests { ..Default::default() }; let mut hint_executor = BuiltinHintProcessor::new_empty(); - let (runner, vm) = cairo_run(program_data, &cairo_run_config, &mut hint_executor).unwrap(); - assert_eq!(runner.get_execution_resources(&vm).unwrap().n_steps, 80); + let runner = cairo_run(program_data, &cairo_run_config, &mut hint_executor).unwrap(); + assert_eq!(runner.get_execution_resources().unwrap().n_steps, 80); } #[test] @@ -3892,8 +3862,8 @@ mod tests { ..Default::default() }; let mut hint_executor = BuiltinHintProcessor::new_empty(); - let (runner, vm) = cairo_run(program_data, &cairo_run_config, &mut hint_executor).unwrap(); - assert_eq!(runner.get_execution_resources(&vm).unwrap().n_steps, 80); + let runner = cairo_run(program_data, &cairo_run_config, &mut hint_executor).unwrap(); + assert_eq!(runner.get_execution_resources().unwrap().n_steps, 80); } #[test] @@ -3901,13 +3871,12 @@ mod tests { fn get_execution_resources_empty_builtins() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.current_step = 10; - vm.segments.segment_used_sizes = Some(vec![4]); + cairo_runner.vm.current_step = 10; + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); assert_eq!( - cairo_runner.get_execution_resources(&vm), + cairo_runner.get_execution_resources(), Ok(ExecutionResources { n_steps: 10, n_memory_holes: 0, @@ -3921,19 +3890,18 @@ mod tests { fn get_execution_resources() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.current_step = 10; - vm.segments.segment_used_sizes = Some(vec![4]); - vm.builtin_runners = vec![{ + cairo_runner.vm.current_step = 10; + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); + cairo_runner.vm.builtin_runners = vec![{ let mut builtin = OutputBuiltinRunner::new(true); - builtin.initialize_segments(&mut vm.segments); + builtin.initialize_segments(&mut cairo_runner.vm.segments); BuiltinRunner::Output(builtin) }]; assert_eq!( - cairo_runner.get_execution_resources(&vm), + cairo_runner.get_execution_resources(), Ok(ExecutionResources { n_steps: 10, n_memory_holes: 0, @@ -3947,9 +3915,8 @@ mod tests { fn finalize_segments_run_not_ended() { let program = program!(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); assert_eq!( - cairo_runner.finalize_segments(&mut vm), + cairo_runner.finalize_segments(), Err(RunnerError::FinalizeNoEndRun) ) } @@ -3961,9 +3928,8 @@ mod tests { let mut cairo_runner = cairo_runner!(program); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; - let mut vm = vm!(); assert_eq!( - cairo_runner.finalize_segments(&mut vm), + cairo_runner.finalize_segments(), Err(RunnerError::NoProgBase) ) } @@ -3976,9 +3942,8 @@ mod tests { cairo_runner.runner_mode = RunnerMode::ProofModeCanonical; cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.run_ended = true; - let mut vm = vm!(); assert_eq!( - cairo_runner.finalize_segments(&mut vm), + cairo_runner.finalize_segments(), Err(RunnerError::NoExecBase) ) } @@ -3991,9 +3956,8 @@ mod tests { cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; - let mut vm = vm!(); assert_eq!( - cairo_runner.finalize_segments(&mut vm), + cairo_runner.finalize_segments(), Err(RunnerError::FinalizeSegmentsNoProofMode) ) } @@ -4006,8 +3970,7 @@ mod tests { cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; - let mut vm = vm!(); - assert_eq!(cairo_runner.finalize_segments(&mut vm), Ok(())); + assert_eq!(cairo_runner.finalize_segments(), Ok(())); assert!(cairo_runner.segments_finalized); assert!(cairo_runner.execution_public_memory.unwrap().is_empty()) } @@ -4023,13 +3986,15 @@ mod tests { cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; - let mut vm = vm!(); - assert_eq!(cairo_runner.finalize_segments(&mut vm), Ok(())); + assert_eq!(cairo_runner.finalize_segments(), Ok(())); assert!(cairo_runner.segments_finalized); //Check values written by first call to segments.finalize() - assert_eq!(vm.segments.segment_sizes.get(&0), Some(&8_usize)); assert_eq!( - vm.segments.public_memory_offsets.get(&0), + cairo_runner.vm.segments.segment_sizes.get(&0), + Some(&8_usize) + ); + assert_eq!( + cairo_runner.vm.segments.public_memory_offsets.get(&0), Some(&vec![ (0_usize, 0_usize), (1_usize, 0_usize), @@ -4042,8 +4007,11 @@ mod tests { ]) ); //Check values written by second call to segments.finalize() - assert_eq!(vm.segments.segment_sizes.get(&1), None); - assert_eq!(vm.segments.public_memory_offsets.get(&1), Some(&vec![])); + assert_eq!(cairo_runner.vm.segments.segment_sizes.get(&1), None); + assert_eq!( + cairo_runner.vm.segments.public_memory_offsets.get(&1), + Some(&vec![]) + ); } #[test] @@ -4058,13 +4026,15 @@ mod tests { cairo_runner.execution_base = Some(Relocatable::from((1, 1))); cairo_runner.execution_public_memory = Some(vec![1_usize, 3_usize, 5_usize, 4_usize]); cairo_runner.run_ended = true; - let mut vm = vm!(); - assert_eq!(cairo_runner.finalize_segments(&mut vm), Ok(())); + assert_eq!(cairo_runner.finalize_segments(), Ok(())); assert!(cairo_runner.segments_finalized); //Check values written by first call to segments.finalize() - assert_eq!(vm.segments.segment_sizes.get(&0), Some(&4_usize)); assert_eq!( - vm.segments.public_memory_offsets.get(&0), + cairo_runner.vm.segments.segment_sizes.get(&0), + Some(&4_usize) + ); + assert_eq!( + cairo_runner.vm.segments.public_memory_offsets.get(&0), Some(&vec![ (0_usize, 0_usize), (1_usize, 0_usize), @@ -4073,9 +4043,9 @@ mod tests { ]) ); //Check values written by second call to segments.finalize() - assert_eq!(vm.segments.segment_sizes.get(&1), None); + assert_eq!(cairo_runner.vm.segments.segment_sizes.get(&1), None); assert_eq!( - vm.segments.public_memory_offsets.get(&1), + cairo_runner.vm.segments.public_memory_offsets.get(&1), Some(&vec![ (2_usize, 0_usize), (4_usize, 0_usize), @@ -4094,9 +4064,8 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new(HashMap::new(), RunResources::default()); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.memory.data = vec![ + cairo_runner.vm.segments.memory.data = vec![ vec![ MemoryCell::new(Felt252::from(0x8000_8023_8012u64).into()), MemoryCell::new(Felt252::from(0xBFFF_8000_0620u64).into()), @@ -4105,12 +4074,10 @@ mod tests { vec![MemoryCell::new((0isize, 0usize).into()); 128 * 1024], ]; - cairo_runner - .run_for_steps(1, &mut vm, &mut hint_processor) - .unwrap(); + cairo_runner.run_for_steps(1, &mut hint_processor).unwrap(); assert_matches!( - cairo_runner.get_perm_range_check_limits(&vm), + cairo_runner.get_perm_range_check_limits(), Some((32768, 32803)) ); } @@ -4122,19 +4089,15 @@ mod tests { fn get_perm_range_check_limits() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); - vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( + cairo_runner.vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( 0x80FF_8000_0530u64 ))]]; - vm.builtin_runners = + cairo_runner.vm.builtin_runners = vec![RangeCheckBuiltinRunner::::new(Some(12), true).into()]; - assert_matches!( - cairo_runner.get_perm_range_check_limits(&vm), - Some((0, 33023)) - ); + assert_matches!(cairo_runner.get_perm_range_check_limits(), Some((0, 33023))); } /// Test that check_range_check_usage() returns successfully when trace is @@ -4144,11 +4107,10 @@ mod tests { fn check_range_check_usage_perm_range_limits_none() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.trace = Some(vec![]); + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.trace = Some(vec![]); - assert_matches!(cairo_runner.check_range_check_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_range_check_usage(), Ok(())); } /// Test that check_range_check_usage() returns successfully when all the @@ -4158,20 +4120,19 @@ mod tests { fn check_range_check_usage_without_builtins() { let program = program!(); - let cairo_runner = cairo_runner!(program, LayoutName::plain); - let mut vm = vm!(); - vm.builtin_runners = vec![]; - vm.current_step = 10000; - vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( + let mut cairo_runner = cairo_runner!(program, LayoutName::plain); + cairo_runner.vm.builtin_runners = vec![]; + cairo_runner.vm.current_step = 10000; + cairo_runner.vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( 0x80FF_8000_0530u64 ))]]; - vm.trace = Some(vec![TraceEntry { + cairo_runner.vm.trace = Some(vec![TraceEntry { pc: (0, 0).into(), ap: 0, fp: 0, }]); - assert_matches!(cairo_runner.check_range_check_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_range_check_usage(), Ok(())); } /// Test that check_range_check_usage() returns an error if there are @@ -4181,22 +4142,21 @@ mod tests { fn check_range_check_usage_insufficient_allocated_cells() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.builtin_runners = + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.builtin_runners = vec![RangeCheckBuiltinRunner::::new(Some(8), true).into()]; - vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( + cairo_runner.vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( 0x80FF_8000_0530u64 ))]]; - vm.trace = Some(vec![TraceEntry { + cairo_runner.vm.trace = Some(vec![TraceEntry { pc: (0, 0).into(), ap: 0, fp: 0, }]); - vm.segments.compute_effective_sizes(); + cairo_runner.vm.segments.compute_effective_sizes(); assert_matches!( - cairo_runner.check_range_check_usage(&vm), + cairo_runner.check_range_check_usage(), Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) @@ -4218,15 +4178,14 @@ mod tests { fn get_initial_fp_can_be_obtained() { let program = program![BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); for _ in 0..2 { - vm.segments.add(); + cairo_runner.vm.segments.add(); } cairo_runner.program_base = Some(relocatable!(0, 0)); cairo_runner.execution_base = Some(relocatable!(1, 0)); let return_fp = Felt252::from(9_i32).into(); cairo_runner - .initialize_function_entrypoint(&mut vm, 0, vec![], return_fp) + .initialize_function_entrypoint(0, vec![], return_fp) .unwrap(); assert_eq!(Some(relocatable!(1, 2)), cairo_runner.get_initial_fp()); } @@ -4236,12 +4195,11 @@ mod tests { fn check_used_cells_valid_case() { let program = program![BuiltinName::range_check, BuiltinName::output]; let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.segment_used_sizes = Some(vec![4]); - vm.trace = Some(vec![]); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); + cairo_runner.vm.trace = Some(vec![]); cairo_runner.layout.diluted_pool_instance_def = None; - assert_matches!(cairo_runner.check_used_cells(&vm), Ok(())); + assert_matches!(cairo_runner.check_used_cells(), Ok(())); } #[test] @@ -4249,21 +4207,20 @@ mod tests { fn check_used_cells_get_used_cells_and_allocated_size_error() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.builtin_runners = + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.builtin_runners = vec![RangeCheckBuiltinRunner::::new(Some(8), true).into()]; - vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( + cairo_runner.vm.segments.memory.data = vec![vec![MemoryCell::new(mayberelocatable!( 0x80FF_8000_0530u64 ))]]; - vm.trace = Some(vec![TraceEntry { + cairo_runner.vm.trace = Some(vec![TraceEntry { pc: (0, 0).into(), ap: 0, fp: 0, }]); - vm.segments.compute_effective_sizes(); + cairo_runner.vm.segments.compute_effective_sizes(); assert_matches!( - cairo_runner.check_used_cells(&vm), + cairo_runner.check_used_cells(), Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) @@ -4275,21 +4232,28 @@ mod tests { fn check_used_cells_check_memory_usage_error() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.memory.mark_as_accessed((1, 0).into()); - vm.segments.memory.mark_as_accessed((1, 3).into()); - vm.builtin_runners = vec![{ + let mut cairo_runner = cairo_runner!(program); + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((1, 0).into()); + cairo_runner + .vm + .segments + .memory + .mark_as_accessed((1, 3).into()); + cairo_runner.vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner.initialize_segments(&mut cairo_runner.vm.segments); builtin_runner }]; - vm.segments.segment_used_sizes = Some(vec![4, 12]); - vm.trace = Some(vec![]); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4, 12]); + cairo_runner.vm.trace = Some(vec![]); assert_matches!( - cairo_runner.check_used_cells(&vm), + cairo_runner.check_used_cells(), Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) @@ -4300,13 +4264,12 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn check_used_cells_check_diluted_check_usage_error() { let program = program![BuiltinName::range_check, BuiltinName::output]; - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); - vm.segments.segment_used_sizes = Some(vec![4]); - vm.trace = Some(vec![]); + let mut cairo_runner = cairo_runner!(program); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![4]); + cairo_runner.vm.trace = Some(vec![]); assert_matches!( - cairo_runner.check_used_cells(&vm), + cairo_runner.check_used_cells(), Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) @@ -4318,14 +4281,13 @@ mod tests { fn initialize_all_builtins() { let program = program!(); - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); cairo_runner - .initialize_all_builtins(&mut vm, false) + .initialize_all_builtins(false) .expect("Builtin initialization failed."); - let given_output = vm.get_builtin_runners(); + let given_output = cairo_runner.vm.get_builtin_runners(); assert_eq!(given_output[0].name(), BuiltinName::pedersen); assert_eq!(given_output[1].name(), BuiltinName::range_check); @@ -4346,14 +4308,13 @@ mod tests { BuiltinName::ecdsa ]; - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); cairo_runner - .initialize_all_builtins(&mut vm, false) + .initialize_all_builtins(false) .expect("Builtin initialization failed."); - let given_output = vm.get_builtin_runners(); + let given_output = cairo_runner.vm.get_builtin_runners(); assert_eq!(given_output[0].name(), BuiltinName::pedersen); assert_eq!(given_output[1].name(), BuiltinName::range_check); @@ -4375,14 +4336,13 @@ mod tests { BuiltinName::segment_arena ]; - let cairo_runner = cairo_runner!(program); - let mut vm = vm!(); + let mut cairo_runner = cairo_runner!(program); cairo_runner - .initialize_all_builtins(&mut vm, true) + .initialize_all_builtins(true) .expect("Builtin initialization failed."); - let given_output = vm.get_builtin_runners(); + let given_output = cairo_runner.vm.get_builtin_runners(); assert_eq!(given_output[0].name(), BuiltinName::pedersen); assert_eq!(given_output[1].name(), BuiltinName::range_check); @@ -4400,13 +4360,12 @@ mod tests { let program = program!(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner - .initialize_function_runner(&mut vm) + .initialize_function_runner() .expect("initialize_function_runner failed."); - let builtin_runners = vm.get_builtin_runners(); + let builtin_runners = cairo_runner.vm.get_builtin_runners(); assert_eq!(builtin_runners[0].name(), BuiltinName::pedersen); assert_eq!(builtin_runners[1].name(), BuiltinName::range_check); @@ -4431,7 +4390,7 @@ mod tests { offset: 0, }) ); - assert_eq!(vm.segments.num_segments(), 10); + assert_eq!(cairo_runner.vm.segments.num_segments(), 10); } #[test] @@ -4440,13 +4399,12 @@ mod tests { let program = program!(); let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(); cairo_runner - .initialize_function_runner_cairo_1(&mut vm, &[BuiltinName::segment_arena]) + .initialize_function_runner_cairo_1(&[BuiltinName::segment_arena]) .expect("initialize_function_runner failed."); - let builtin_runners = vm.get_builtin_runners(); + let builtin_runners = cairo_runner.vm.get_builtin_runners(); assert_eq!(builtin_runners[0].name(), BuiltinName::segment_arena); assert_eq!(builtin_runners[1].name(), BuiltinName::pedersen); @@ -4472,17 +4430,16 @@ mod tests { offset: 0, }) ); - assert_eq!(vm.segments.num_segments(), 12); + assert_eq!(cairo_runner.vm.segments.num_segments(), 12); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initialize_segments_incorrect_layout_plain_one_builtin() { let program = program![BuiltinName::output]; - let mut vm = vm!(); - let cairo_runner = cairo_runner!(program, LayoutName::plain); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain); assert_eq!( - cairo_runner.initialize_builtins(&mut vm, false), + cairo_runner.initialize_builtins(false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::output]), LayoutName::plain @@ -4494,10 +4451,9 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] 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, LayoutName::plain); + let mut cairo_runner = cairo_runner!(program, LayoutName::plain); assert_eq!( - cairo_runner.initialize_builtins(&mut vm, false), + cairo_runner.initialize_builtins(false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::output, BuiltinName::pedersen]), LayoutName::plain @@ -4509,10 +4465,9 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] 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, LayoutName::small); + let mut cairo_runner = cairo_runner!(program, LayoutName::small); assert_eq!( - cairo_runner.initialize_builtins(&mut vm, false), + cairo_runner.initialize_builtins(false), Err(RunnerError::NoBuiltinForInstance(Box::new(( HashSet::from([BuiltinName::bitwise]), LayoutName::small, @@ -4525,12 +4480,11 @@ mod tests { let program = program!(start = Some(0), end = Some(0), main = Some(8),); let mut runner = cairo_runner!(program); runner.runner_mode = RunnerMode::ProofModeCanonical; - let mut vm = vm!(); - runner.initialize_segments(&mut vm, None); + runner.initialize_segments(None); assert_eq!(runner.execution_base, Some(Relocatable::from((1, 0)))); assert_eq!(runner.program_base, Some(Relocatable::from((0, 0)))); assert_eq!( - runner.initialize_main_entrypoint(&mut vm), + runner.initialize_main_entrypoint(), Ok(Relocatable::from((0, 0))) ); assert_eq!(runner.initial_ap, Some(Relocatable::from((1, 2)))); @@ -4549,13 +4503,12 @@ mod tests { ); let mut runner = cairo_runner!(program); runner.runner_mode = RunnerMode::ProofModeCanonical; - let mut vm = vm!(); - runner.initialize_builtins(&mut vm, false).unwrap(); - runner.initialize_segments(&mut vm, None); + runner.initialize_builtins(false).unwrap(); + runner.initialize_segments(None); assert_eq!(runner.execution_base, Some(Relocatable::from((1, 0)))); assert_eq!(runner.program_base, Some(Relocatable::from((0, 0)))); assert_eq!( - runner.initialize_main_entrypoint(&mut vm), + runner.initialize_main_entrypoint(), Ok(Relocatable::from((0, 0))) ); assert_eq!(runner.initial_ap, Some(Relocatable::from((1, 2)))); @@ -4684,10 +4637,9 @@ mod tests { cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; cairo_runner.segments_finalized = false; - let mut vm = vm!(); //Check values written by first call to segments.finalize() - assert_eq!(cairo_runner.read_return_values(&mut vm, false), Ok(())); + assert_eq!(cairo_runner.read_return_values(false), Ok(())); assert_eq!( cairo_runner .execution_public_memory @@ -4707,9 +4659,8 @@ mod tests { cairo_runner.program_base = Some(Relocatable::from((0, 0))); cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = false; - let mut vm = vm!(); assert_eq!( - cairo_runner.read_return_values(&mut vm, false), + cairo_runner.read_return_values(false), Err(RunnerError::ReadReturnValuesNoEndRun) ); } @@ -4726,9 +4677,8 @@ mod tests { cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; cairo_runner.segments_finalized = true; - let mut vm = vm!(); assert_eq!( - cairo_runner.read_return_values(&mut vm, false), + cairo_runner.read_return_values(false), Err(RunnerError::FailedAddingReturnValues) ); } @@ -4745,19 +4695,18 @@ mod tests { cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; cairo_runner.segments_finalized = false; - let mut vm = vm!(); let output_builtin = OutputBuiltinRunner::new(true); - vm.builtin_runners.push(output_builtin.into()); - vm.segments.memory.data = vec![ + cairo_runner.vm.builtin_runners.push(output_builtin.into()); + cairo_runner.vm.segments.memory.data = vec![ vec![], vec![MemoryCell::new(MaybeRelocatable::from((0, 0)))], vec![], ]; - vm.set_ap(1); - vm.segments.segment_used_sizes = Some(vec![0, 1, 0]); + cairo_runner.vm.set_ap(1); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0, 1, 0]); //Check values written by first call to segments.finalize() - assert_eq!(cairo_runner.read_return_values(&mut vm, false), Ok(())); - let output_builtin = match &vm.builtin_runners[0] { + assert_eq!(cairo_runner.read_return_values(false), Ok(())); + let output_builtin = match &cairo_runner.vm.builtin_runners[0] { BuiltinRunner::Output(runner) => runner, _ => unreachable!(), }; @@ -4776,19 +4725,18 @@ mod tests { cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; cairo_runner.segments_finalized = false; - let mut vm = vm!(); let output_builtin = OutputBuiltinRunner::new(true); - vm.builtin_runners.push(output_builtin.into()); - vm.segments.memory.data = vec![ + cairo_runner.vm.builtin_runners.push(output_builtin.into()); + cairo_runner.vm.segments.memory.data = vec![ vec![MemoryCell::new(MaybeRelocatable::from((0, 0)))], vec![MemoryCell::new(MaybeRelocatable::from((0, 1)))], vec![], ]; - vm.set_ap(1); - vm.segments.segment_used_sizes = Some(vec![1, 1, 0]); + cairo_runner.vm.set_ap(1); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![1, 1, 0]); //Check values written by first call to segments.finalize() - assert_eq!(cairo_runner.read_return_values(&mut vm, false), Ok(())); - let output_builtin = match &vm.builtin_runners[0] { + assert_eq!(cairo_runner.read_return_values(false), Ok(())); + let output_builtin = match &cairo_runner.vm.builtin_runners[0] { BuiltinRunner::Output(runner) => runner, _ => unreachable!(), }; @@ -4807,13 +4755,12 @@ mod tests { cairo_runner.execution_base = Some(Relocatable::from((1, 0))); cairo_runner.run_ended = true; cairo_runner.segments_finalized = false; - let mut vm = vm!(); let output_builtin = OutputBuiltinRunner::new(true); let bitwise_builtin = BitwiseBuiltinRunner::new(Some(256), true); - vm.builtin_runners.push(output_builtin.into()); - vm.builtin_runners.push(bitwise_builtin.into()); - cairo_runner.initialize_segments(&mut vm, None); - vm.segments.memory.data = vec![ + cairo_runner.vm.builtin_runners.push(output_builtin.into()); + cairo_runner.vm.builtin_runners.push(bitwise_builtin.into()); + cairo_runner.initialize_segments(None); + cairo_runner.vm.segments.memory.data = vec![ vec![MemoryCell::new(MaybeRelocatable::from((0, 0)))], vec![ MemoryCell::new(MaybeRelocatable::from((2, 0))), @@ -4821,18 +4768,18 @@ mod tests { ], vec![], ]; - vm.set_ap(2); + cairo_runner.vm.set_ap(2); // We use 5 as bitwise builtin's segment size as a bitwise instance is 5 cells - vm.segments.segment_used_sizes = Some(vec![0, 2, 0, 5]); + cairo_runner.vm.segments.segment_used_sizes = Some(vec![0, 2, 0, 5]); //Check values written by first call to segments.finalize() - assert_eq!(cairo_runner.read_return_values(&mut vm, false), Ok(())); - let output_builtin = match &vm.builtin_runners[0] { + assert_eq!(cairo_runner.read_return_values(false), Ok(())); + let output_builtin = match &cairo_runner.vm.builtin_runners[0] { BuiltinRunner::Output(runner) => runner, _ => unreachable!(), }; assert_eq!(output_builtin.stop_ptr, Some(0)); - assert_eq!(cairo_runner.read_return_values(&mut vm, false), Ok(())); - let bitwise_builtin = match &vm.builtin_runners[1] { + assert_eq!(cairo_runner.read_return_values(false), Ok(())); + let bitwise_builtin = match &cairo_runner.vm.builtin_runners[1] { BuiltinRunner::Bitwise(runner) => runner, _ => unreachable!(), }; @@ -4847,8 +4794,7 @@ mod tests { None, ) .unwrap(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); //this true expression dictates that the trace is enabled + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); let mut hint_processor = BuiltinHintProcessor::new_empty(); //this entrypoint tells which function to run in the cairo program @@ -4860,8 +4806,8 @@ mod tests { .pc .unwrap(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); assert_matches!( cairo_runner.run_from_entrypoint( main_entrypoint, @@ -4871,20 +4817,16 @@ mod tests { ], //range_check_ptr true, None, - &mut vm, &mut hint_processor, ), Ok(()) ); - let mut new_cairo_runner = cairo_runner!(program); - let mut new_vm = vm!(true); //this true expression dictates that the trace is enabled + let mut new_cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); let mut hint_processor = BuiltinHintProcessor::new_empty(); - new_cairo_runner - .initialize_builtins(&mut new_vm, false) - .unwrap(); - new_cairo_runner.initialize_segments(&mut new_vm, None); + new_cairo_runner.initialize_builtins(false).unwrap(); + new_cairo_runner.initialize_segments(None); let fib_entrypoint = program .shared_program_data @@ -4903,7 +4845,6 @@ mod tests { ], true, None, - &mut new_vm, &mut hint_processor, ), Ok(()) @@ -4918,8 +4859,7 @@ mod tests { None, ) .unwrap(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); //this true expression dictates that the trace is enabled + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); let mut hint_processor = BuiltinHintProcessor::new_empty(); //this entrypoint tells which function to run in the cairo program @@ -4931,7 +4871,7 @@ mod tests { .pc .unwrap(); - cairo_runner.initialize_function_runner(&mut vm).unwrap(); + cairo_runner.initialize_function_runner().unwrap(); assert!(cairo_runner .run_from_entrypoint( @@ -4941,13 +4881,12 @@ mod tests { ], true, None, - &mut vm, &mut hint_processor, ) .is_ok()); // Check that memory_holes == 0 - assert!(cairo_runner.get_memory_holes(&vm).unwrap().is_zero()); + assert!(cairo_runner.get_memory_holes().unwrap().is_zero()); } #[test] @@ -5037,8 +4976,7 @@ mod tests { None, ) .unwrap(); - let mut cairo_runner = cairo_runner!(program); - let mut vm = vm!(true); //this true expression dictates that the trace is enabled + let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false, true); let mut hint_processor = BuiltinHintProcessor::new_empty(); //this entrypoint tells which function to run in the cairo program @@ -5050,17 +4988,11 @@ mod tests { .pc .unwrap(); - cairo_runner.initialize_builtins(&mut vm, false).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_builtins(false).unwrap(); + cairo_runner.initialize_segments(None); - let result = cairo_runner.run_from_entrypoint( - main_entrypoint, - &[], - true, - None, - &mut vm, - &mut hint_processor, - ); + let result = + cairo_runner.run_from_entrypoint(main_entrypoint, &[], true, None, &mut hint_processor); match result { Err(CairoRunError::VmException(exception)) => { assert_eq!( @@ -5082,16 +5014,15 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); runner - .run_until_pc(end, &mut vm, &mut BuiltinHintProcessor::new_empty()) + .run_until_pc(end, &mut BuiltinHintProcessor::new_empty()) .unwrap(); - vm.segments.compute_effective_sizes(); - let initial_pointer = vm.get_ap(); - let expected_pointer = (vm.get_ap() - 1).unwrap(); + runner.vm.segments.compute_effective_sizes(); + let initial_pointer = runner.vm.get_ap(); + let expected_pointer = (runner.vm.get_ap() - 1).unwrap(); assert_eq!( - runner.get_builtins_final_stack(&mut vm, initial_pointer), + runner.get_builtins_final_stack(initial_pointer), Ok(expected_pointer) ); } @@ -5105,16 +5036,15 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); runner - .run_until_pc(end, &mut vm, &mut BuiltinHintProcessor::new_empty()) + .run_until_pc(end, &mut BuiltinHintProcessor::new_empty()) .unwrap(); - vm.segments.compute_effective_sizes(); - let initial_pointer = vm.get_ap(); - let expected_pointer = (vm.get_ap() - 4).unwrap(); + runner.vm.segments.compute_effective_sizes(); + let initial_pointer = runner.vm.get_ap(); + let expected_pointer = (runner.vm.get_ap() - 4).unwrap(); assert_eq!( - runner.get_builtins_final_stack(&mut vm, initial_pointer), + runner.get_builtins_final_stack(initial_pointer), Ok(expected_pointer) ); } @@ -5128,16 +5058,15 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); runner - .run_until_pc(end, &mut vm, &mut BuiltinHintProcessor::new_empty()) + .run_until_pc(end, &mut BuiltinHintProcessor::new_empty()) .unwrap(); - vm.segments.compute_effective_sizes(); - let initial_pointer = vm.get_ap(); - let expected_pointer = vm.get_ap(); + runner.vm.segments.compute_effective_sizes(); + let initial_pointer = runner.vm.get_ap(); + let expected_pointer = runner.vm.get_ap(); assert_eq!( - runner.get_builtins_final_stack(&mut vm, initial_pointer), + runner.get_builtins_final_stack(initial_pointer), Ok(expected_pointer) ); } @@ -5152,13 +5081,12 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); runner - .run_until_pc(end, &mut vm, &mut BuiltinHintProcessor::new_empty()) + .run_until_pc(end, &mut BuiltinHintProcessor::new_empty()) .unwrap(); - vm.segments.compute_effective_sizes(); - let mut exec = runner.get_execution_resources(&vm).unwrap(); + runner.vm.segments.compute_effective_sizes(); + let mut exec = runner.get_execution_resources().unwrap(); exec.builtin_instance_counter.insert(BuiltinName::keccak, 0); assert_eq!(exec.builtin_instance_counter.len(), 5); let rsc = exec.filter_unused_builtins(); @@ -5239,12 +5167,11 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); // program takes 80 steps assert_matches!( - runner.run_until_pc(end, &mut vm, &mut BuiltinHintProcessor::new_empty(),), + runner.run_until_pc(end, &mut BuiltinHintProcessor::new_empty(),), Ok(()) ) } @@ -5258,14 +5185,10 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); let mut hint_processor = BuiltinHintProcessor::new(HashMap::new(), RunResources::new(81)); // program takes 81 steps - assert_matches!( - runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(runner.run_until_pc(end, &mut hint_processor), Ok(())); assert_eq!(hint_processor.run_resources().get_n_steps(), Some(1)); } @@ -5279,14 +5202,10 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); let mut hint_processor = BuiltinHintProcessor::new(HashMap::new(), RunResources::new(80)); // program takes 80 steps - assert_matches!( - runner.run_until_pc(end, &mut vm, &mut hint_processor), - Ok(()) - ); + assert_matches!(runner.run_until_pc(end, &mut hint_processor), Ok(())); assert_eq!(hint_processor.run_resources(), &RunResources::new(0)); } @@ -5300,12 +5219,11 @@ mod tests { ) .unwrap(); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - let end = runner.initialize(&mut vm, false).unwrap(); + let end = runner.initialize(false).unwrap(); let mut hint_processor = BuiltinHintProcessor::new(HashMap::new(), RunResources::new(9)); // program takes 9 steps assert_matches!( - runner.run_until_pc(end, &mut vm, &mut hint_processor,), + runner.run_until_pc(end, &mut hint_processor,), Err(VirtualMachineError::UnfinishedExecution) ); assert_eq!(hint_processor.run_resources(), &RunResources::new(0)); @@ -5313,180 +5231,185 @@ mod tests { #[test] fn get_cairo_pie_no_program_base() { - let runner = CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let vm = vm!(); - assert_eq!(runner.get_cairo_pie(&vm), Err(RunnerError::NoProgBase)) + let runner = cairo_runner!(Default::default()); + assert_eq!(runner.get_cairo_pie(), Err(RunnerError::NoProgBase)) } #[test] fn get_cairo_pie_no_execution_base() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); - assert_eq!(runner.get_cairo_pie(&vm), Err(RunnerError::NoExecBase)) + assert_eq!(runner.get_cairo_pie(), Err(RunnerError::NoExecBase)) } #[test] fn get_cairo_pie_no_segment_sizes() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 0).into(), (2, 0).into()) + runner + .vm + .insert_value::((1, 0).into(), (2, 0).into()) .unwrap(); // return_pc - vm.insert_value::((1, 1).into(), (3, 0).into()) + runner + .vm + .insert_value::((1, 1).into(), (3, 0).into()) .unwrap(); assert_eq!( - runner.get_cairo_pie(&vm), + runner.get_cairo_pie(), Err(RunnerError::UnexpectedRetFpSegmentSize) ); } #[test] fn get_cairo_pie_ret_pc_segment_size_not_zero() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 0).into(), (2, 0).into()) + runner + .vm + .insert_value::((1, 0).into(), (2, 0).into()) .unwrap(); // return_pc - vm.insert_value::((1, 1).into(), (3, 0).into()) + runner + .vm + .insert_value::((1, 1).into(), (3, 0).into()) .unwrap(); // segment sizes - vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 1)]); + runner.vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 1)]); assert_eq!( - runner.get_cairo_pie(&vm), + runner.get_cairo_pie(), Err(RunnerError::UnexpectedRetPcSegmentSize) ); } #[test] fn get_cairo_pie_program_base_offset_not_zero() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 1))); runner.execution_base = Some(Relocatable::from((1, 0))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 0).into(), (2, 0).into()) + runner + .vm + .insert_value::((1, 0).into(), (2, 0).into()) .unwrap(); // return_pc - vm.insert_value::((1, 1).into(), (3, 0).into()) + runner + .vm + .insert_value::((1, 1).into(), (3, 0).into()) .unwrap(); // segment sizes - vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); + runner.vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); assert_eq!( - runner.get_cairo_pie(&vm), + runner.get_cairo_pie(), Err(RunnerError::ProgramBaseOffsetNotZero) ); } #[test] fn get_cairo_pie_execution_base_offset_not_zero() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 1))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 1).into(), (2, 0).into()) + runner + .vm + .insert_value::((1, 1).into(), (2, 0).into()) .unwrap(); // return_pc - vm.insert_value::((1, 2).into(), (3, 0).into()) + runner + .vm + .insert_value::((1, 2).into(), (3, 0).into()) .unwrap(); // segment sizes - vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); + runner.vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); assert_eq!( - runner.get_cairo_pie(&vm), + runner.get_cairo_pie(), Err(RunnerError::ExecBaseOffsetNotZero) ); } #[test] fn get_cairo_pie_ret_fp_offset_not_zero() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 0).into(), (2, 1).into()) + runner + .vm + .insert_value::((1, 0).into(), (2, 1).into()) .unwrap(); // return_pc - vm.insert_value::((1, 1).into(), (3, 0).into()) + runner + .vm + .insert_value::((1, 1).into(), (3, 0).into()) .unwrap(); // segment sizes - vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); - assert_eq!( - runner.get_cairo_pie(&vm), - Err(RunnerError::RetFpOffsetNotZero) - ); + runner.vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); + assert_eq!(runner.get_cairo_pie(), Err(RunnerError::RetFpOffsetNotZero)); } #[test] fn get_cairo_pie_ret_pc_offset_not_zero() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 0).into(), (2, 0).into()) + runner + .vm + .insert_value::((1, 0).into(), (2, 0).into()) .unwrap(); // return_pc - vm.insert_value::((1, 1).into(), (3, 1).into()) + runner + .vm + .insert_value::((1, 1).into(), (3, 1).into()) .unwrap(); // segment sizes - vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); - assert_eq!( - runner.get_cairo_pie(&vm), - Err(RunnerError::RetPcOffsetNotZero) - ); + runner.vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); + assert_eq!(runner.get_cairo_pie(), Err(RunnerError::RetPcOffsetNotZero)); } #[test] fn get_cairo_pie_ok() { - let mut runner = - CairoRunner::new(&Program::default(), LayoutName::all_cairo, false).unwrap(); - let mut vm = vm!(); + let mut runner = cairo_runner!(Default::default()); runner.program_base = Some(Relocatable::from((0, 0))); runner.execution_base = Some(Relocatable::from((1, 0))); - vm.add_memory_segment(); - vm.add_memory_segment(); + runner.vm.add_memory_segment(); + runner.vm.add_memory_segment(); // return_fp - vm.insert_value::((1, 0).into(), (2, 0).into()) + runner + .vm + .insert_value::((1, 0).into(), (2, 0).into()) .unwrap(); // return_pc - vm.insert_value::((1, 1).into(), (3, 0).into()) + runner + .vm + .insert_value::((1, 1).into(), (3, 0).into()) .unwrap(); // segment sizes - vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); + runner.vm.segments.segment_sizes = HashMap::from([(0, 0), (1, 2), (2, 0), (3, 0)]); } #[test] fn get_air_private_input() { let program_content = include_bytes!("../../../../cairo_programs/proof_programs/common_signature.json"); - let (runner, vm) = crate::cairo_run::cairo_run( + let runner = crate::cairo_run::cairo_run( program_content, &CairoRunConfig { proof_mode: true, @@ -5496,7 +5419,7 @@ mod tests { &mut BuiltinHintProcessor::new_empty(), ) .unwrap(); - let air_private_input = runner.get_air_private_input(&vm); + let air_private_input = runner.get_air_private_input(); assert!(air_private_input.0[&BuiltinName::pedersen].is_empty()); assert!(air_private_input.0[&BuiltinName::range_check].is_empty()); assert!(air_private_input.0[&BuiltinName::bitwise].is_empty()); diff --git a/vm/src/vm/security.rs b/vm/src/vm/security.rs index 0fd1b3c566..2967a641bf 100644 --- a/vm/src/vm/security.rs +++ b/vm/src/vm/security.rs @@ -5,7 +5,6 @@ use num_traits::ToPrimitive; use super::{ errors::{runner_errors::RunnerError, vm_errors::VirtualMachineError}, runners::cairo_runner::CairoRunner, - vm_core::VirtualMachine, }; use crate::types::relocatable::MaybeRelocatable; @@ -24,15 +23,15 @@ pub fn verify_secure_runner( runner: &CairoRunner, verify_builtins: bool, program_segment_size: Option, - vm: &mut VirtualMachine, ) -> Result<(), VirtualMachineError> { let builtins_segment_info = match verify_builtins { - true => runner.get_builtin_segments_info(vm)?, + true => runner.get_builtin_segments_info()?, false => Vec::new(), }; // Check builtin segment out of bounds. for (index, stop_ptr) in builtins_segment_info { - let current_size = vm + let current_size = runner + .vm .segments .memory .data @@ -50,7 +49,8 @@ pub fn verify_secure_runner( .ok_or(RunnerError::NoProgBase)?; let program_segment_size = program_segment_size.unwrap_or(runner.program.shared_program_data.data.len()); - let program_length = vm + let program_length = runner + .vm .segments .memory .data @@ -63,8 +63,8 @@ pub fn verify_secure_runner( // Check that the addresses in memory are valid // This means that every temporary address has been properly relocated to a real address // Asumption: If temporary memory is empty, this means no temporary memory addresses were generated and all addresses in memory are real - if !vm.segments.memory.temp_data.is_empty() { - for value in vm.segments.memory.data.iter().flatten() { + if !runner.vm.segments.memory.temp_data.is_empty() { + for value in runner.vm.segments.memory.data.iter().flatten() { match value.get_value() { Some(MaybeRelocatable::RelocatableValue(addr)) if addr.segment_index < 0 => { return Err(VirtualMachineError::InvalidMemoryValueTemporaryAddress( @@ -75,8 +75,8 @@ pub fn verify_secure_runner( } } } - for builtin in vm.builtin_runners.iter() { - builtin.run_security_checks(vm)?; + for builtin in runner.vm.builtin_runners.iter() { + builtin.run_security_checks(&runner.vm)?; } Ok(()) @@ -103,10 +103,9 @@ mod test { let program = program!(); let runner = cairo_runner!(program); - let mut vm = vm!(); assert_matches!( - verify_secure_runner(&runner, true, None, &mut vm), + verify_secure_runner(&runner, true, None), Err(VirtualMachineError::RunnerError(RunnerError::NoProgBase)) ); } @@ -115,30 +114,26 @@ mod test { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_secure_runner_empty_memory() { let program = program!(main = Some(0),); - let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); - vm.segments.compute_effective_sizes(); - assert_matches!(verify_secure_runner(&runner, true, None, &mut vm), Ok(())); + runner.initialize(false).unwrap(); + runner.vm.segments.compute_effective_sizes(); + assert_matches!(verify_secure_runner(&runner, true, None), Ok(())); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_secure_runner_program_access_out_of_bounds() { let program = program!(main = Some(0),); - let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); + runner.initialize(false).unwrap(); - vm.segments = segments![((0, 0), 100)]; - vm.segments.segment_used_sizes = Some(vec![1]); + runner.vm.segments = segments![((0, 0), 100)]; + runner.vm.segments.segment_used_sizes = Some(vec![1]); assert_matches!( - verify_secure_runner(&runner, true, None, &mut vm), + verify_secure_runner(&runner, true, None), Err(VirtualMachineError::OutOfBoundsProgramSegmentAccess) ); } @@ -147,35 +142,29 @@ mod test { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_secure_runner_program_with_program_size() { let program = program!(main = Some(0),); - let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); + runner.initialize(false).unwrap(); - vm.segments = segments![((0, 0), 100)]; - vm.segments.segment_used_sizes = Some(vec![1]); + runner.vm.segments = segments![((0, 0), 100)]; + runner.vm.segments.segment_used_sizes = Some(vec![1]); - assert_matches!( - verify_secure_runner(&runner, true, Some(1), &mut vm), - Ok(()) - ); + assert_matches!(verify_secure_runner(&runner, true, Some(1)), Ok(())); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_secure_runner_builtin_access_out_of_bounds() { let program = program!(main = Some(0), builtins = vec![BuiltinName::range_check],); - let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); - vm.builtin_runners[0].set_stop_ptr(0); - vm.segments.memory = memory![((2, 0), 1)]; - vm.segments.segment_used_sizes = Some(vec![0, 0, 0, 0]); + + runner.initialize(false).unwrap(); + runner.vm.builtin_runners[0].set_stop_ptr(0); + runner.vm.segments.memory = memory![((2, 0), 1)]; + runner.vm.segments.segment_used_sizes = Some(vec![0, 0, 0, 0]); assert_matches!( - verify_secure_runner(&runner, true, None, &mut vm), + verify_secure_runner(&runner, true, None), Err(VirtualMachineError::OutOfBoundsBuiltinSegmentAccess) ); } @@ -184,20 +173,17 @@ mod test { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_secure_runner_builtin_access_correct() { let program = program!(main = Some(0), builtins = vec![BuiltinName::range_check],); - let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); + + runner.initialize(false).unwrap(); let mut hint_processor = BuiltinHintProcessor::new_empty(); - runner - .end_run(false, false, &mut vm, &mut hint_processor) - .unwrap(); - vm.builtin_runners[0].set_stop_ptr(1); + runner.end_run(false, false, &mut hint_processor).unwrap(); + runner.vm.builtin_runners[0].set_stop_ptr(1); - vm.segments.memory = memory![((2, 0), 1)]; - vm.segments.segment_used_sizes = Some(vec![0, 0, 1, 0]); + runner.vm.segments.memory = memory![((2, 0), 1)]; + runner.vm.segments.segment_used_sizes = Some(vec![0, 0, 1, 0]); - assert_matches!(verify_secure_runner(&runner, true, None, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, None), Ok(())); } #[test] @@ -214,18 +200,17 @@ mod test { ); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); - vm.segments.memory = memory![ + runner.initialize(false).unwrap(); + runner.vm.segments.memory = memory![ ((0, 0), (1, 0)), ((0, 1), (2, 1)), ((0, 2), (3, 2)), ((0, 3), (4, 3)) ]; - vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); + runner.vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); - assert_matches!(verify_secure_runner(&runner, true, None, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, None), Ok(())); } #[test] @@ -242,18 +227,17 @@ mod test { ); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); - vm.segments.memory = memory![ + runner.initialize(false).unwrap(); + runner.vm.segments.memory = memory![ ((0, 1), (1, 0)), ((0, 2), (2, 1)), ((0, 3), (3, 2)), ((-1, 0), (1, 2)) ]; - vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); + runner.vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); - assert_matches!(verify_secure_runner(&runner, true, None, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, None), Ok(())); } #[test] @@ -270,20 +254,19 @@ mod test { ); let mut runner = cairo_runner!(program); - let mut vm = vm!(); - runner.initialize(&mut vm, false).unwrap(); - vm.segments.memory = memory![ + runner.initialize(false).unwrap(); + runner.vm.segments.memory = memory![ ((0, 0), (1, 0)), ((0, 1), (2, 1)), ((0, 2), (-3, 2)), ((0, 3), (4, 3)), ((-1, 0), (1, 2)) ]; - vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); + runner.vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); assert_matches!( - verify_secure_runner(&runner, true, None, &mut vm), + verify_secure_runner(&runner, true, None), Err(VirtualMachineError::InvalidMemoryValueTemporaryAddress( bx )) if *bx == relocatable!(-3, 2) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index 34cccf7438..5b54b18b5d 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -1274,10 +1274,7 @@ mod tests { utils::test_utils::*, vm::{ errors::memory_errors::MemoryError, - runners::{ - builtin_runner::{BitwiseBuiltinRunner, EcOpBuiltinRunner, HashBuiltinRunner}, - cairo_runner::CairoRunner, - }, + runners::builtin_runner::{BitwiseBuiltinRunner, EcOpBuiltinRunner, HashBuiltinRunner}, }, }; use assert_matches::assert_matches; @@ -4323,18 +4320,15 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); let expected_traceback = vec![ (Relocatable::from((1, 3)), Relocatable::from((0, 97))), (Relocatable::from((1, 14)), Relocatable::from((0, 30))), (Relocatable::from((1, 26)), Relocatable::from((0, 60))), ]; - assert_eq!(vm.get_traceback_entries(), expected_traceback); + assert_eq!(cairo_runner.vm.get_traceback_entries(), expected_traceback); } #[test] @@ -4348,14 +4342,11 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program, LayoutName::all_cairo, false); - let mut vm = vm!(); - let end = cairo_runner.initialize(&mut vm, false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut vm, &mut hint_processor) - .is_err()); + let end = cairo_runner.initialize(false).unwrap(); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); let expected_traceback = vec![(Relocatable::from((1, 2)), Relocatable::from((0, 34)))]; - assert_eq!(vm.get_traceback_entries(), expected_traceback); + assert_eq!(cairo_runner.vm.get_traceback_entries(), expected_traceback); } #[test] @@ -4387,7 +4378,6 @@ mod tests { #[cfg(feature = "test_utils")] fn before_first_step_hook( _vm: &mut VirtualMachine, - _runner: &mut CairoRunner, _hint_data: &[Box], ) -> Result<(), VirtualMachineError> { Err(VirtualMachineError::Unexpected) @@ -4435,13 +4425,10 @@ mod tests { .expect("Call to `Program::from_file()` failed."); let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); - let end = cairo_runner - .initialize(&mut virtual_machine_from_builder, false) - .unwrap(); + cairo_runner.vm = virtual_machine_from_builder; + let end = cairo_runner.initialize(false).unwrap(); - assert!(cairo_runner - .run_until_pc(end, &mut virtual_machine_from_builder, &mut hint_processor) - .is_err()); + assert!(cairo_runner.run_until_pc(end, &mut hint_processor).is_err()); } }