Skip to content

Commit

Permalink
Fix_no_trace_padding_flow_in_proof_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
YairVaknin-starkware committed Jan 6, 2025
1 parent 87c10b3 commit 3036bc4
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bench/criterion_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn build_many_runners(c: &mut Criterion) {
black_box(None),
black_box(false),
black_box(false),
black_box(false),
)
.unwrap(),
);
Expand All @@ -53,6 +54,7 @@ fn load_program_data(c: &mut Criterion) {
None,
false,
false,
false,
)
.unwrap()
},
Expand Down
2 changes: 2 additions & 0 deletions bench/iai_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn build_runner() {
None,
false,
false,
false,
)
.unwrap();
core::mem::drop(black_box(runner));
Expand All @@ -54,6 +55,7 @@ fn build_runner_helper() -> CairoRunner {
None,
false,
false,
false,
)
.unwrap()
}
Expand Down
1 change: 1 addition & 0 deletions cairo1-run/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion hint_accountant/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions vm/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_is_on_curve_2() {
let mut vm = VirtualMachine::new(false);
let mut vm = VirtualMachine::new(false, false);
vm.set_fp(1);
let ids_data = non_continuous_ids_data![("is_on_curve", -1)];
vm.segments = segments![((1, 0), 1)];
Expand Down Expand Up @@ -404,7 +404,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_compute_q_mod_prime() {
let mut vm = VirtualMachine::new(false);
let mut vm = VirtualMachine::new(false, false);

let ap_tracking = ApTracking::default();

Expand All @@ -431,7 +431,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_compute_ids_high_low() {
let mut vm = VirtualMachine::new(false);
let mut vm = VirtualMachine::new(false, false);

let value = BigInt::from(25);
let shift = BigInt::from(12);
Expand Down Expand Up @@ -501,7 +501,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_r1_get_point_from_x() {
let mut vm = VirtualMachine::new(false);
let mut vm = VirtualMachine::new(false, false);
vm.set_fp(10);

let ids_data = non_continuous_ids_data![("x", -10), ("v", -7)];
Expand Down Expand Up @@ -560,7 +560,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_reduce_value() {
let mut vm = VirtualMachine::new(false);
let mut vm = VirtualMachine::new(false, false);

//Initialize fp
vm.run_context.fp = 10;
Expand Down Expand Up @@ -616,7 +616,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_reduce_x() {
let mut vm = VirtualMachine::new(false);
let mut vm = VirtualMachine::new(false, false);

//Initialize fp
vm.run_context.fp = 10;
Expand Down
1 change: 1 addition & 0 deletions vm/src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ fn run_program_with_custom_mod_builtin_params(
cairo_run_config.dynamic_layout_params,
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
cairo_run_config.disable_trace_padding,
)
.unwrap();

Expand Down
2 changes: 2 additions & 0 deletions vm/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn run_cairo_1_entrypoint(
None,
false,
false,
false,
)
.unwrap();

Expand Down Expand Up @@ -221,6 +222,7 @@ fn run_cairo_1_entrypoint_with_run_resources(
None,
false,
false,
false,
)
.unwrap();

Expand Down
11 changes: 7 additions & 4 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()
};
Expand All @@ -271,6 +272,7 @@ pub mod test_utils {
None,
$proof_mode,
false,
false,
)
.unwrap()
};
Expand All @@ -281,6 +283,7 @@ pub mod test_utils {
None,
$proof_mode,
$trace_enabled,
false,
)
.unwrap()
};
Expand Down Expand Up @@ -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;
Expand Down
70 changes: 65 additions & 5 deletions vm/src/vm/runners/builtin_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,25 @@ impl BuiltinRunner {
min_step_num
};

if vm.current_step < min_step {
if vm.current_step < min_step && !vm.disable_trace_padding {
return Err(InsufficientAllocatedCellsError::MinStepNotReached(
Box::new((min_step, self.name())),
)
.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)
}
}
Expand Down Expand Up @@ -693,11 +698,13 @@ impl From<ModBuiltinRunner> for BuiltinRunner {
#[cfg(test)]
mod tests {
use super::*;
use crate::cairo_run::{cairo_run, CairoRunConfig};
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::relocatable;
use crate::types::builtin_name::BuiltinName;
use crate::types::instance_definitions::mod_instance_def::ModInstanceDef;
use crate::types::instance_definitions::LowRatio;
use crate::types::layout_name::LayoutName;
use crate::types::program::Program;
use crate::utils::test_utils::*;
use crate::vm::errors::memory_errors::InsufficientAllocatedCellsError;
Expand Down Expand Up @@ -877,6 +884,59 @@ mod tests {
assert_eq!(builtin.get_allocated_memory_units(&cairo_runner.vm), Ok(5));
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn proof_mode_with_and_without_disable_trace_padding() {
let program_data =
include_bytes!("../../../../../cairo_programs/proof_programs/pedersen_test.json");

let config_false = CairoRunConfig {
disable_trace_padding: false,
proof_mode: true,
layout: LayoutName::all_cairo,
..Default::default()
};
let mut hint_processor_false = BuiltinHintProcessor::new_empty();
let runner_false =
cairo_run(program_data, &config_false, &mut hint_processor_false).unwrap();
let last_step_false = runner_false.vm.current_step;

assert!(last_step_false.is_power_of_two());

let config_true = CairoRunConfig {
disable_trace_padding: true,
proof_mode: true,
layout: LayoutName::all_cairo,
..Default::default()
};
let mut hint_processor_true = BuiltinHintProcessor::new_empty();
let runner_true = cairo_run(program_data, &config_true, &mut hint_processor_true).unwrap();
let last_step_true = runner_true.vm.current_step;

// Ensure the last step is not a power of two - true for this specific program, not always.
assert!(!last_step_true.is_power_of_two());

assert!(last_step_true < last_step_false);

let builtin_runners_false = &runner_false.vm.builtin_runners;
let builtin_runners_true = &runner_true.vm.builtin_runners;

// Compare allocated instances for each pair of builtin runners
for (builtin_runner_false, builtin_runner_true) in builtin_runners_false
.iter()
.zip(builtin_runners_true.iter())
{
let allocated_false = builtin_runner_false
.get_allocated_instances(&runner_false.vm)
.unwrap_or(0);
let allocated_true = builtin_runner_true
.get_allocated_instances(&runner_true.vm)
.unwrap_or(0);

assert!(allocated_false >= allocated_true);
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_allocated_memory_units_ec_op_with_items() {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm/runners/builtin_runner/modulo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ mod tests {
let mut hint_processor = BuiltinHintProcessor::new_empty();
let program = Program::from_bytes(program_data, Some("main")).unwrap();
let mut runner =
CairoRunner::new(&program, LayoutName::all_cairo, None, true, false).unwrap();
CairoRunner::new(&program, LayoutName::all_cairo, None, true, false, false).unwrap();

let end = runner.initialize(false).unwrap();
// Modify add_mod & mul_mod params
Expand Down
6 changes: 5 additions & 1 deletion vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl CairoRunner {
dynamic_layout_params: Option<CairoLayoutParams>,
mode: RunnerMode,
trace_enabled: bool,
disable_trace_padding: bool,
) -> Result<CairoRunner, RunnerError> {
let cairo_layout = match layout {
LayoutName::plain => CairoLayout::plain_instance(),
Expand All @@ -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,
Expand Down Expand Up @@ -226,6 +227,7 @@ impl CairoRunner {
dynamic_layout_params: Option<CairoLayoutParams>,
proof_mode: bool,
trace_enabled: bool,
disable_trace_padding: bool,
) -> Result<CairoRunner, RunnerError> {
if proof_mode {
Self::new_v2(
Expand All @@ -234,6 +236,7 @@ impl CairoRunner {
dynamic_layout_params,
RunnerMode::ProofModeCanonical,
trace_enabled,
disable_trace_padding,
)
} else {
Self::new_v2(
Expand All @@ -242,6 +245,7 @@ impl CairoRunner {
dynamic_layout_params,
RunnerMode::ExecutionMode,
trace_enabled,
disable_trace_padding,
)
}
}
Expand Down
Loading

0 comments on commit 3036bc4

Please sign in to comment.