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 5, 2025
1 parent 87c10b3 commit 1af457b
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 12 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
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
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
13 changes: 9 additions & 4 deletions vm/src/vm/runners/builtin_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
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
7 changes: 5 additions & 2 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ 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<Option<Instruction>>,
#[cfg(feature = "test_utils")]
pub(crate) hooks: crate::vm::hooks::Hooks,
pub(crate) relocation_table: Option<Vec<usize>>,
}

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,
Expand All @@ -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(),
Expand Down Expand Up @@ -1255,6 +1257,7 @@ impl VirtualMachineBuilder {
#[cfg(feature = "test_utils")]
hooks: self.hooks,
relocation_table: None,
disable_trace_padding: false,
}
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1af457b

Please sign in to comment.