Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allow_missing_builtins flag #1600

Merged
merged 14 commits into from
Feb 6, 2024
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#### Upcoming Changes

* feat(BREAKING): Add `allow_missing_builtins` flag [#1600](https://github.com/lambdaclass/cairo-vm/pull/1600)

This new flag will skip the check that all builtins used by the program need to be present in the selected layout if enabled. It will also be enabled by default when running in proof_mode.

* Add `allow_missing_builtins` flag to `cairo-vm-cli` crate
* Add `allow_missing_builtins` field to `CairoRunConfig` struct
* Add `allow_missing_builtins` boolean argument to `CairoRunner` methods `initialize` & `initialize_builtins`

* feat: Append return values to the output segment when running cairo1-run in proof_mode [#1597](https://github.com/lambdaclass/cairo-vm/pull/1597)
* Add instructions to the proof_mode header to copy return values to the output segment before initiating the infinite loop
* Output builtin is now always included when running cairo 1 programs in proof_mode
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ The cairo-vm-cli supports the following optional arguments:

- `--proof_mode`: Runs the program in proof_mode

- `--secure_run`: Runs security checks after execution. Enabled by default when not in proof_mode
- `--secure_run`: Runs security checks after execution. Enabled by default when not in proof_mode.

- `--air_public_input <AIR_PUBLIC_INPUT>`: Receives the name of a file and outputs the AIR public inputs into it. Can only be used if proof_mode is also enabled.

- `--air_private_input <AIR_PRIVATE_INPUT>`: Receives the name of a file and outputs the AIR private inputs into it. Can only be used if proof_mode, trace_file & memory_file are also enabled.

- `--cairo_pie_output <CAIRO_PIE_OUTPUT>`: Receives the name of a file and outputs the Cairo PIE into it. Can only be used if proof_mode, is not enabled.

- `--allow_missing_builtins`: Disables the check that all builtins used by the program need to be included in the selected layout. Enabled by default when in proof_mode.

For example, to obtain the air public inputs from a fibonacci program run, we can run :

```bash
Expand Down
4 changes: 3 additions & 1 deletion bench/criterion_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ fn load_program_data(c: &mut Criterion) {
VirtualMachine::new(false),
)
},
|(mut runner, mut vm)| _ = black_box(runner.initialize(black_box(&mut vm)).unwrap()),
|(mut runner, mut vm)| {
_ = black_box(runner.initialize(black_box(&mut vm), false).unwrap())
},
BatchSize::SmallInput,
)
});
Expand Down
2 changes: 1 addition & 1 deletion bench/iai_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn build_runner_helper() -> (CairoRunner, VirtualMachine) {
#[inline(never)]
fn load_program_data() {
let (mut runner, mut vm) = build_runner_helper();
_ = black_box(runner.initialize(black_box(&mut vm)).unwrap());
_ = black_box(runner.initialize(black_box(&mut vm), false).unwrap());
}

main!(
Expand Down
3 changes: 3 additions & 0 deletions cairo-vm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct Args {
conflicts_with_all = ["proof_mode", "air_private_input", "air_public_input"]
)]
cairo_pie_output: Option<String>,
#[structopt(long = "allow_missing_builtins")]
allow_missing_builtins: Option<bool>,
}

fn validate_layout(value: &str) -> Result<String, String> {
Expand Down Expand Up @@ -132,6 +134,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
layout: &args.layout,
proof_mode: args.proof_mode,
secure_run: args.secure_run,
allow_missing_builtins: args.allow_missing_builtins,
..Default::default()
};

Expand Down
2 changes: 1 addition & 1 deletion cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {

let mut runner = CairoRunner::new_v2(&program, &args.layout, runner_mode)?;
let mut vm = VirtualMachine::new(args.trace_file.is_some() || args.air_public_input.is_some());
let end = runner.initialize(&mut vm)?;
let end = runner.initialize(&mut vm, args.proof_mode)?;

additional_initialization(&mut vm, data_len)?;

Expand Down
21 changes: 16 additions & 5 deletions vm/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct CairoRunConfig<'a> {
pub proof_mode: bool,
pub secure_run: Option<bool>,
pub disable_trace_padding: bool,
pub allow_missing_builtins: Option<bool>,
}

#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -56,6 +57,7 @@ impl<'a> Default for CairoRunConfig<'a> {
proof_mode: false,
secure_run: None,
disable_trace_padding: false,
allow_missing_builtins: None,
}
}
}
Expand All @@ -69,14 +71,18 @@ pub fn cairo_run_program(
.secure_run
.unwrap_or(!cairo_run_config.proof_mode);

let allow_missing_builtins = cairo_run_config
.allow_missing_builtins
.unwrap_or(cairo_run_config.proof_mode);

let mut cairo_runner = CairoRunner::new(
program,
cairo_run_config.layout,
cairo_run_config.proof_mode,
)?;

let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled);
let end = cairo_runner.initialize(&mut vm)?;
let end = cairo_runner.initialize(&mut vm, allow_missing_builtins)?;
// check step calculation

cairo_runner
Expand Down Expand Up @@ -133,7 +139,12 @@ pub fn cairo_run_fuzzed_program(

let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled);

let _end = cairo_runner.initialize(&mut vm)?;
let _end = cairo_runner.initialize(
&mut vm,
cairo_run_config
.allow_missing_builtins
.unwrap_or(cairo_run_config.proof_mode),
)?;

let res = match cairo_runner.run_until_steps(steps_limit, &mut vm, hint_executor) {
Err(VirtualMachineError::EndOfProgram(_remaining)) => Ok(()), // program ran OK but ended before steps limit
Expand Down Expand Up @@ -230,7 +241,7 @@ mod tests {
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!(true);
let end = cairo_runner
.initialize(&mut vm)
.initialize(&mut vm, false)
.map_err(CairoRunError::Runner)?;

assert!(cairo_runner
Expand All @@ -252,7 +263,7 @@ mod tests {
let mut hint_processor = BuiltinHintProcessor::new_empty();
let mut cairo_runner = cairo_runner!(program);

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
Expand Down Expand Up @@ -372,7 +383,7 @@ 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).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
Expand Down
18 changes: 9 additions & 9 deletions vm/src/vm/errors/vm_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ mod test {
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_err());
Expand All @@ -673,7 +673,7 @@ mod test {
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_err());
Expand Down Expand Up @@ -711,7 +711,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60)
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_err());
Expand Down Expand Up @@ -870,7 +870,7 @@ cairo_programs/bad_programs/bad_range_check.cairo:11:5: (pc=0:6)
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
let error = cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.unwrap_err();
Expand Down Expand Up @@ -915,7 +915,7 @@ cairo_programs/bad_programs/bad_usort.cairo:64:5: (pc=0:60)
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
let error = cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.unwrap_err();
Expand Down Expand Up @@ -954,7 +954,7 @@ cairo_programs/bad_programs/ec_recover_product_mod_m_zero.cairo:11:5: (pc=0:18)
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
let error = cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.unwrap_err();
Expand Down Expand Up @@ -993,7 +993,7 @@ cairo_programs/bad_programs/ec_recover_div_mod_n_packed_n_zero.cairo:11:5: (pc=0
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
let error = cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.unwrap_err();
Expand Down Expand Up @@ -1032,7 +1032,7 @@ cairo_programs/bad_programs/uint512_unsigned_div_rem_div_is_zero.cairo:15:2: (pc
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
let error = cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.unwrap_err();
Expand Down Expand Up @@ -1069,7 +1069,7 @@ cairo_programs/bad_programs/uint256_sub_b_gt_256.cairo:10:2: (pc=0:12)
let mut cairo_runner = cairo_runner!(program, "all_cairo", false);
let mut vm = vm!();

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
let error = cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.unwrap_err();
Expand Down
10 changes: 5 additions & 5 deletions vm/src/vm/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod tests {
let mut vm = vm!();
vm.hooks = Hooks::new(None, None, None);

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
let mut vm = vm!();
vm.hooks = Hooks::new(Some(Arc::new(before_first_step_hook)), None, None);

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_err());
Expand All @@ -190,7 +190,7 @@ mod tests {
let mut vm = vm!();
vm.hooks = Hooks::new(None, Some(Arc::new(pre_step_hook)), None);

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_err());
Expand All @@ -201,7 +201,7 @@ mod tests {
let mut vm = vm!();
vm.hooks = Hooks::new(None, None, Some(Arc::new(post_step_hook)));

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_err());
Expand Down Expand Up @@ -252,7 +252,7 @@ mod tests {
Some(Arc::new(post_step_hook)),
);

let end = cairo_runner.initialize(&mut vm).unwrap();
let end = cairo_runner.initialize(&mut vm, false).unwrap();
assert!(cairo_runner
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/runners/builtin_runner/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -438,7 +438,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/runners/builtin_runner/ec_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -509,7 +509,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/runners/builtin_runner/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -425,7 +425,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm/runners/builtin_runner/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down
8 changes: 4 additions & 4 deletions vm/src/vm/runners/builtin_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -873,7 +873,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -917,7 +917,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -961,7 +961,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/runners/builtin_runner/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down Expand Up @@ -409,7 +409,7 @@ mod tests {

let mut hint_processor = BuiltinHintProcessor::new_empty();

let address = cairo_runner.initialize(&mut vm).unwrap();
let address = cairo_runner.initialize(&mut vm, false).unwrap();

cairo_runner
.run_until_pc(address, &mut vm, &mut hint_processor)
Expand Down
Loading
Loading