diff --git a/CHANGELOG.md b/CHANGELOG.md index d0cd50c54b..45e962607b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* fix: Fix no trace padding flow in proof mode [#1909](https://github.com/lambdaclass/cairo-vm/pull/1909) + * feat: implement `kzg` data availability hints [#1887](https://github.com/lambdaclass/cairo-vm/pull/1887) #### [2.0.0-rc3] - 2024-12-26 diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index b196bda230..e0b417c321 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -258,6 +258,7 @@ pub fn cairo_run_program( cairo_run_config.dynamic_layout_params.clone(), runner_mode, cairo_run_config.trace_enabled, + false, )?; let end = runner.initialize(cairo_run_config.proof_mode)?; load_arguments(&mut runner, &cairo_run_config, main_func, initial_gas)?; diff --git a/hint_accountant/src/main.rs b/hint_accountant/src/main.rs index dcb5ca05a2..d639dd3e4f 100644 --- a/hint_accountant/src/main.rs +++ b/hint_accountant/src/main.rs @@ -49,7 +49,7 @@ fn run() { whitelists.push(whitelist_file.allowed_hint_expressions); } } - let mut vm = VirtualMachine::new(false); + let mut vm = VirtualMachine::new(false, false); let mut hint_executor = BuiltinHintProcessor::new_empty(); let (ap_tracking_data, reference_ids, references, mut exec_scopes, constants) = ( ApTracking::default(), diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 0e61856a3b..85680aa4a1 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -75,6 +75,7 @@ pub fn cairo_run_program_with_initial_scope( cairo_run_config.dynamic_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, + cairo_run_config.disable_trace_padding, )?; cairo_runner.exec_scopes = exec_scopes; @@ -162,6 +163,7 @@ pub fn cairo_run_pie( cairo_run_config.dynamic_layout_params.clone(), false, cairo_run_config.trace_enabled, + cairo_run_config.disable_trace_padding, )?; let end = cairo_runner.initialize(allow_missing_builtins)?; @@ -235,6 +237,7 @@ pub fn cairo_run_fuzzed_program( cairo_run_config.dynamic_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, + cairo_run_config.disable_trace_padding, )?; let _end = cairo_runner.initialize(allow_missing_builtins)?; diff --git a/vm/src/tests/mod.rs b/vm/src/tests/mod.rs index 9141c3a0f9..ecf44b2c71 100644 --- a/vm/src/tests/mod.rs +++ b/vm/src/tests/mod.rs @@ -116,6 +116,7 @@ fn run_cairo_1_entrypoint( None, false, false, + false, ) .unwrap(); @@ -221,6 +222,7 @@ fn run_cairo_1_entrypoint_with_run_resources( None, false, false, + false, ) .unwrap(); diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 760c5201ad..6a6a284ee6 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -234,7 +234,7 @@ pub mod test_utils { macro_rules! vm_with_range_check { () => {{ - let mut vm = VirtualMachine::new(false); + let mut vm = VirtualMachine::new(false, false); vm.builtin_runners = vec![ $crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner::<8>::new( Some(8), @@ -255,12 +255,13 @@ pub mod test_utils { None, false, false, + false, ) .unwrap() }; ($program:expr, $layout:expr) => { crate::vm::runners::cairo_runner::CairoRunner::new( - &$program, $layout, None, false, false, + &$program, $layout, None, false, false, false, ) .unwrap() }; @@ -271,6 +272,7 @@ pub mod test_utils { None, $proof_mode, false, + false, ) .unwrap() }; @@ -281,6 +283,7 @@ pub mod test_utils { None, $proof_mode, $trace_enabled, + false, ) .unwrap() }; @@ -405,11 +408,11 @@ pub mod test_utils { macro_rules! vm { () => {{ - crate::vm::vm_core::VirtualMachine::new(false) + crate::vm::vm_core::VirtualMachine::new(false, false) }}; ($use_trace:expr) => {{ - crate::vm::vm_core::VirtualMachine::new($use_trace) + crate::vm::vm_core::VirtualMachine::new($use_trace, false) }}; } pub(crate) use vm; diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 2374f43f3d..f7585bfbdb 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -212,14 +212,19 @@ impl BuiltinRunner { ) .into()); }; + let numerator = if let Some(ratio_den) = self.ratio_den() { + vm.current_step * ratio_den as usize + } else { + vm.current_step + }; - let allocated_instances = if let Some(ratio_den) = self.ratio_den() { - safe_div_usize(vm.current_step * ratio_den as usize, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)? + let allocated_instances = if vm.disable_trace_padding { + div_ceil(numerator, ratio as usize) } else { - safe_div_usize(vm.current_step, ratio as usize) + safe_div_usize(numerator, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)? }; + Ok(allocated_instances) } } diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 65209c35de..266a3eb1e5 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -176,6 +176,7 @@ impl CairoRunner { dynamic_layout_params: Option, mode: RunnerMode, trace_enabled: bool, + disable_trace_padding: bool, ) -> Result { let cairo_layout = match layout { LayoutName::plain => CairoLayout::plain_instance(), @@ -197,7 +198,7 @@ impl CairoRunner { }; Ok(CairoRunner { program: program.clone(), - vm: VirtualMachine::new(trace_enabled), + vm: VirtualMachine::new(trace_enabled, disable_trace_padding), layout: cairo_layout, final_pc: None, program_base: None, @@ -226,6 +227,7 @@ impl CairoRunner { dynamic_layout_params: Option, proof_mode: bool, trace_enabled: bool, + disable_trace_padding: bool, ) -> Result { if proof_mode { Self::new_v2( @@ -234,6 +236,7 @@ impl CairoRunner { dynamic_layout_params, RunnerMode::ProofModeCanonical, trace_enabled, + disable_trace_padding, ) } else { Self::new_v2( @@ -242,6 +245,7 @@ impl CairoRunner { dynamic_layout_params, RunnerMode::ExecutionMode, trace_enabled, + disable_trace_padding, ) } } diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index 5862aee093..bd49e02b88 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -89,6 +89,7 @@ pub struct VirtualMachine { pub(crate) rc_limits: Option<(isize, isize)>, skip_instruction_execution: bool, run_finished: bool, + pub(crate) disable_trace_padding: bool, instruction_cache: Vec>, #[cfg(feature = "test_utils")] pub(crate) hooks: crate::vm::hooks::Hooks, @@ -96,7 +97,7 @@ pub struct VirtualMachine { } impl VirtualMachine { - pub fn new(trace_enabled: bool) -> VirtualMachine { + pub fn new(trace_enabled: bool, disable_trace_padding: bool) -> VirtualMachine { let run_context = RunContext { pc: Relocatable::from((0, 0)), ap: 0, @@ -118,6 +119,7 @@ impl VirtualMachine { segments: MemorySegmentManager::new(), rc_limits: None, run_finished: false, + disable_trace_padding, instruction_cache: Vec::new(), #[cfg(feature = "test_utils")] hooks: Default::default(), @@ -1255,6 +1257,7 @@ impl VirtualMachineBuilder { #[cfg(feature = "test_utils")] hooks: self.hooks, relocation_table: None, + disable_trace_padding: false, } } } @@ -1444,7 +1447,7 @@ mod tests { op1: MaybeRelocatable::Int(Felt252::from(10)), }; - let mut vm = VirtualMachine::new(false); + let mut vm = VirtualMachine::new(false, false); vm.run_context.pc = Relocatable::from((0, 4)); vm.run_context.ap = 5; vm.run_context.fp = 6;