From e0aa3f7a948bd0b9cc52f1666e686fd4f0270766 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 Jan 2024 17:14:22 -0300 Subject: [PATCH 01/84] Add proof_mode flag to cairo1-run --- cairo1-run/src/main.rs | 129 ++++++++++++++++++++++++++++------------- 1 file changed, 90 insertions(+), 39 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 2518d2af6e..4cd3f02a89 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -77,6 +77,8 @@ struct Args { memory_file: Option, #[clap(long = "layout", default_value = "plain", value_parser=validate_layout)] layout: String, + #[clap(long = "proof_mode", value_parser)] + proof_mode: bool, } fn validate_layout(value: &str) -> Result { @@ -202,35 +204,41 @@ fn run(args: impl Iterator) -> Result, Erro &type_sizes, main_func, initial_gas, + args.proof_mode, )?; - println!("Compiling with proof mode and running ..."); - - // This information can be useful for the users using the prover. - println!("Builtins used: {:?}", builtins); - - // Prepare "canonical" proof mode instructions. These are usually added by the compiler in cairo 0 - let mut ctx = casm! {}; - casm_extend! {ctx, - call rel 4; - jmp rel 0; - }; - let proof_mode_header = ctx.instructions; - // Get the user program instructions let program_instructions = casm_program.instructions.iter(); // This footer is used by lib funcs let libfunc_footer = create_code_footer(); - // This is the program we are actually proving - // With embedded proof mode, cairo1 header and the libfunc footer + let proof_mode_header = if args.proof_mode { + println!("Compiling with proof mode and running ..."); + + // This information can be useful for the users using the prover. + println!("Builtins used: {:?}", builtins); + + // Prepare "canonical" proof mode instructions. These are usually added by the compiler in cairo 0 + let mut ctx = casm! {}; + casm_extend! {ctx, + call rel 4; + jmp rel 0; + }; + ctx.instructions + } else { + casm! {}.instructions + }; + + // This is the program we are actually running/proving + // With (embedded proof mode), cairo1 header and the libfunc footer let instructions = chain!( proof_mode_header.iter(), entry_code.iter(), program_instructions, libfunc_footer.iter() - ); + ) + .into_iter(); let (processor_hints, program_hints) = build_hints_vec(instructions.clone()); @@ -244,36 +252,59 @@ fn run(args: impl Iterator) -> Result, Erro let data_len = data.len(); - let starting_pc = 0; - - let program = Program::new_for_proof( - builtins, - data, - starting_pc, - // Proof mode is on top - // jmp rel 0 is on PC == 2 - 2, - program_hints, - ReferenceManager { - references: Vec::new(), - }, - HashMap::new(), - vec![], - None, - )?; + let program = if args.proof_mode { + Program::new_for_proof( + builtins, + data, + 0, + // Proof mode is on top + // jmp rel 0 is on PC == 2 + 2, + program_hints, + ReferenceManager { + references: Vec::new(), + }, + HashMap::new(), + vec![], + None, + )? + } else { + Program::new( + builtins, + data, + Some(0), + program_hints, + ReferenceManager { + references: Vec::new(), + }, + HashMap::new(), + vec![], + None, + )? + }; - let mut runner = CairoRunner::new_v2(&program, &args.layout, RunnerMode::ProofModeCairo1)?; + let runner_mode = if args.proof_mode { + RunnerMode::ProofModeCairo1 + } else { + RunnerMode::ExecutionMode + }; + + let mut runner = CairoRunner::new_v2(&program, &args.layout, runner_mode)?; let mut vm = VirtualMachine::new(args.trace_file.is_some()); let end = runner.initialize(&mut vm)?; additional_initialization(&mut vm, data_len)?; - // Run it until the infinite loop + // Run it until the end/ infinite loop in proof_mode runner.run_until_pc(end, &mut vm, &mut hint_processor)?; - // Then pad it to the power of 2 - runner.run_until_next_power_of_2(&mut vm, &mut hint_processor)?; + if args.proof_mode { + // Then pad it to the power of 2 + runner.run_until_next_power_of_2(&mut vm, &mut hint_processor)?; + } else { + runner.end_run(true, false, &mut vm, &mut hint_processor)?; + } // Fetch return type data let return_type_id = main_func @@ -465,6 +496,7 @@ fn create_entry_code( type_sizes: &UnorderedHashMap, func: &Function, initial_gas: usize, + proof_mode: bool, ) -> Result<(Vec, Vec), Error> { let mut ctx = casm! {}; // The builtins in the formatting expected by the runner. @@ -497,8 +529,11 @@ fn create_entry_code( let ty_size = type_sizes[ty]; let generic_ty = &info.long_id.generic_id; if let Some(offset) = builtin_offset.get(generic_ty) { - // Everything is off by 2 due to the proof mode header - let offset = offset + 2; + let mut offset = *offset; + if proof_mode { + // Everything is off by 2 due to the proof mode header + offset = offset + 2; + } casm_extend! {ctx, [ap + 0] = [fp - offset], ap++; } @@ -661,6 +696,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_fibonacci_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(89)]); @@ -668,6 +704,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_factorial_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3628800)]); @@ -675,6 +712,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_array_get_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3)]); @@ -682,6 +720,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_enum_flow_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(300)]); @@ -689,6 +728,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_enum_match_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(10), MaybeRelocatable::from(felt_str("3618502788666131213697322783095070105623107215331596699973092056135872020471"))]); @@ -696,6 +736,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_hello_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1), MaybeRelocatable::from(1234)]); @@ -703,6 +744,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_ops_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(6)]); @@ -710,6 +752,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_print_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![]); @@ -717,6 +760,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_recursion_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1154076154663935037074198317650845438095734251249125412074882362667803016453"))]); @@ -724,6 +768,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_sample_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("5050"))]); @@ -731,6 +776,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_poseidon_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1099385018355113290651252669115094675591288647745213771718157553170111442461"))]); @@ -738,6 +784,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_poseidon_pedersen_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1036257840396636296853154602823055519264738423488122322497453114874087006398"))]); @@ -745,6 +792,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_pedersen_example_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1089549915800264549621536909767699778745926517555586332772759280702396009108"))]); @@ -752,6 +800,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_simple_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); @@ -759,6 +808,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_simple_struct_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(100)]); @@ -766,6 +816,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_dictionaries(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1024)]); From 2357010553713bd1d01f9b9beaac00163de1d166 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 Jan 2024 17:20:25 -0300 Subject: [PATCH 02/84] Add Changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40f85f5d5f..ac6dd24a22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ #### Upcoming Changes +* feat: Add `proof_mode` flag to `cairo1-run` [#1537] (https://github.com/lambdaclass/cairo-vm/pull/1537) + * The cairo1-run crate no longer compiles and executes in proof_mode by default + * Add flag `proof_mode` to cairo1-run crate. Activating this flag will enable proof_mode compilation and execution + * feat(BREAKING): Replace `cairo-felt` crate with `starknet-types-core` (0.0.5) [#1408](https://github.com/lambdaclass/cairo-vm/pull/1408) * feat(BREAKING): Add Cairo 1 proof mode compilation and execution [#1517] (https://github.com/lambdaclass/cairo-vm/pull/1517) From 7e3b34c663cf1e7259d571c43e4dceb59a227cb6 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 Jan 2024 17:22:19 -0300 Subject: [PATCH 03/84] Clippy --- cairo1-run/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 4cd3f02a89..40aa77775f 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -237,8 +237,7 @@ fn run(args: impl Iterator) -> Result, Erro entry_code.iter(), program_instructions, libfunc_footer.iter() - ) - .into_iter(); + ); let (processor_hints, program_hints) = build_hints_vec(instructions.clone()); From 07c112f51dd57761628296de33dabcc764adff1e Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 Jan 2024 17:28:54 -0300 Subject: [PATCH 04/84] clippy --- cairo1-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 40aa77775f..1d8dae79e5 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -531,7 +531,7 @@ fn create_entry_code( let mut offset = *offset; if proof_mode { // Everything is off by 2 due to the proof mode header - offset = offset + 2; + offset += 2; } casm_extend! {ctx, [ap + 0] = [fp - offset], ap++; From 43dd6ed5c017f2b8a9a15f4293f749486e6cfe1d Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 Jan 2024 18:03:13 -0300 Subject: [PATCH 05/84] Add `air_public_input` flag --- cairo1-run/src/main.rs | 55 ++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 1d8dae79e5..1eca8a1860 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -79,6 +79,8 @@ struct Args { layout: String, #[clap(long = "proof_mode", value_parser)] proof_mode: bool, + #[clap(long = "air_public_input", value_parser)] + air_public_input: Option, } fn validate_layout(value: &str) -> Result { @@ -173,6 +175,13 @@ impl FileWriter { fn run(args: impl Iterator) -> Result, Error> { let args = Args::try_parse_from(args)?; + if args.air_public_input.is_some() && !args.proof_mode { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--air_public_input can only be used in proof_mode.", + ); + return Err(Error::Cli(error)); + } let compiler_config = CompilerConfig { replace_ids: true, @@ -297,12 +306,11 @@ fn run(args: impl Iterator) -> Result, Erro // Run it until the end/ infinite loop in proof_mode runner.run_until_pc(end, &mut vm, &mut hint_processor)?; - + runner.end_run(false, false, &mut vm, &mut hint_processor)?; + vm.verify_auto_deductions()?; + runner.read_return_values(&mut vm)?; if args.proof_mode { - // Then pad it to the power of 2 - runner.run_until_next_power_of_2(&mut vm, &mut hint_processor)?; - } else { - runner.end_run(true, false, &mut vm, &mut hint_processor)?; + runner.finalize_segments(&mut vm)?; } // Fetch return type data @@ -354,6 +362,11 @@ fn run(args: impl Iterator) -> Result, Erro runner.relocate(&mut vm, true)?; + if let Some(file_path) = args.air_public_input { + let json = runner.get_air_public_input(&vm)?.serialize_json()?; + std::fs::write(file_path, json)?; + } + if let Some(trace_path) = args.trace_file { let relocated_trace = runner .relocated_trace @@ -695,7 +708,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_fibonacci_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(89)]); @@ -703,7 +716,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_factorial_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3628800)]); @@ -711,7 +724,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_array_get_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3)]); @@ -719,7 +732,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_enum_flow_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(300)]); @@ -727,7 +740,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_enum_match_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(10), MaybeRelocatable::from(felt_str("3618502788666131213697322783095070105623107215331596699973092056135872020471"))]); @@ -735,7 +748,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_hello_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1), MaybeRelocatable::from(1234)]); @@ -743,7 +756,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_ops_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(6)]); @@ -751,7 +764,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_print_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![]); @@ -759,7 +772,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_recursion_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1154076154663935037074198317650845438095734251249125412074882362667803016453"))]); @@ -767,7 +780,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_sample_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("5050"))]); @@ -775,7 +788,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_poseidon_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1099385018355113290651252669115094675591288647745213771718157553170111442461"))]); @@ -783,7 +796,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_poseidon_pedersen_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1036257840396636296853154602823055519264738423488122322497453114874087006398"))]); @@ -791,7 +804,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_pedersen_example_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1089549915800264549621536909767699778745926517555586332772759280702396009108"))]); @@ -799,7 +812,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_simple_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); @@ -807,7 +820,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_simple_struct_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(100)]); @@ -815,7 +828,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] fn test_run_dictionaries(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1024)]); From b3c4cbdda24f7add5772bb1c3f3b61dccf558972 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 12:34:23 -0300 Subject: [PATCH 06/84] Progress --- cairo1-run/src/main.rs | 44 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 1eca8a1860..f17d6ca6e7 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -307,11 +307,6 @@ fn run(args: impl Iterator) -> Result, Erro // Run it until the end/ infinite loop in proof_mode runner.run_until_pc(end, &mut vm, &mut hint_processor)?; runner.end_run(false, false, &mut vm, &mut hint_processor)?; - vm.verify_auto_deductions()?; - runner.read_return_values(&mut vm)?; - if args.proof_mode { - runner.finalize_segments(&mut vm)?; - } // Fetch return type data let return_type_id = main_func @@ -360,6 +355,45 @@ fn run(args: impl Iterator) -> Result, Erro } } + // Set stop pointers for builtins + if args.proof_mode { + let ret_types_sizes = main_func + .signature + .ret_types + .iter() + .map(|id| type_sizes.get(id).cloned().unwrap_or_default()); + let ret_types_and_sizes = main_func + .signature + .ret_types + .iter() + .zip(ret_types_sizes.clone()); + let full_ret_types_size: i16 = ret_types_sizes.sum(); + let mut stack_pointer = (vm.get_ap() - (full_ret_types_size - 1) as usize) + .map_err(VirtualMachineError::Math)?; + for (id, size) in ret_types_and_sizes { + if let Some(ref name) = id.debug_name { + let mut builtin_name = name.to_lowercase(); + if builtin_name == "rangecheck" { + builtin_name = "range_check".to_string(); + } + builtin_name = format!("{}_builtin", builtin_name.to_lowercase()); + if let Some(builtin) = vm + .builtin_runners + .iter_mut() + .find(|b| dbg!(b.name()) == dbg!(&builtin_name)) + { + // Fetch pointer from vm_ret_values + builtin.final_stack(&vm.segments, stack_pointer)?; + } + } + stack_pointer.offset += size as usize; + } + // Set stop pointer for builtins that are not included + // for builtin in vm.get_builtin_runners_as_mut() { + // if builtin.include + // } + } + runner.relocate(&mut vm, true)?; if let Some(file_path) = args.air_public_input { From e41994be2c018065e17e8797316e052d079c3ac1 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 12:43:09 -0300 Subject: [PATCH 07/84] Update builtins stop ptrs when running cairo 1 --- cairo1-run/src/main.rs | 31 +++++++++++++++++-------------- vm/src/types/relocatable.rs | 4 +++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index f17d6ca6e7..12131245c7 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -355,8 +355,8 @@ fn run(args: impl Iterator) -> Result, Erro } } - // Set stop pointers for builtins - if args.proof_mode { + // Set stop pointers for builtins so we can obtain the air public input + if args.air_public_input.is_some() { let ret_types_sizes = main_func .signature .ret_types @@ -367,9 +367,13 @@ fn run(args: impl Iterator) -> Result, Erro .ret_types .iter() .zip(ret_types_sizes.clone()); + let full_ret_types_size: i16 = ret_types_sizes.sum(); let mut stack_pointer = (vm.get_ap() - (full_ret_types_size - 1) as usize) .map_err(VirtualMachineError::Math)?; + + // Calculate the stack_ptr for each return value as if it were a builtin + let mut builtin_name_to_stack_pointer = HashMap::new(); for (id, size) in ret_types_and_sizes { if let Some(ref name) = id.debug_name { let mut builtin_name = name.to_lowercase(); @@ -377,21 +381,20 @@ fn run(args: impl Iterator) -> Result, Erro builtin_name = "range_check".to_string(); } builtin_name = format!("{}_builtin", builtin_name.to_lowercase()); - if let Some(builtin) = vm - .builtin_runners - .iter_mut() - .find(|b| dbg!(b.name()) == dbg!(&builtin_name)) - { - // Fetch pointer from vm_ret_values - builtin.final_stack(&vm.segments, stack_pointer)?; - } + builtin_name_to_stack_pointer.insert(builtin_name, stack_pointer); } stack_pointer.offset += size as usize; } - // Set stop pointer for builtins that are not included - // for builtin in vm.get_builtin_runners_as_mut() { - // if builtin.include - // } + // Set stop pointer for each builtin + for builtin in vm.builtin_runners.iter_mut() { + builtin.final_stack( + &vm.segments, + builtin_name_to_stack_pointer + .get(builtin.name()) + .cloned() + .unwrap_or_default(), + )?; + } } runner.relocate(&mut vm, true)?; diff --git a/vm/src/types/relocatable.rs b/vm/src/types/relocatable.rs index 353be453ae..3ffe9d8c9f 100644 --- a/vm/src/types/relocatable.rs +++ b/vm/src/types/relocatable.rs @@ -15,7 +15,9 @@ use serde::{Deserialize, Serialize}; use arbitrary::Arbitrary; #[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(Arbitrary))] -#[derive(Eq, Ord, Hash, PartialEq, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize)] +#[derive( + Eq, Ord, Hash, PartialEq, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize, Default, +)] pub struct Relocatable { pub segment_index: isize, pub offset: usize, From 378a0fe9e355251363d2fc82bd738f0474a2c144 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 12:43:56 -0300 Subject: [PATCH 08/84] Change visibility --- vm/src/vm/vm_core.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index 98b0c32904..a33fd37aa2 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -78,7 +78,7 @@ impl DeducedOperands { pub struct VirtualMachine { pub(crate) run_context: RunContext, - pub(crate) builtin_runners: Vec, + pub builtin_runners: Vec, pub(crate) segments: MemorySegmentManager, pub(crate) trace: Option>, pub(crate) current_step: usize, From f633fc480e5992eb0b9a055bf7eb575ee227f009 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 12:44:41 -0300 Subject: [PATCH 09/84] Change visibility --- vm/src/vm/vm_core.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index a33fd37aa2..100169c87b 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -79,7 +79,7 @@ impl DeducedOperands { pub struct VirtualMachine { pub(crate) run_context: RunContext, pub builtin_runners: Vec, - pub(crate) segments: MemorySegmentManager, + pub segments: MemorySegmentManager, pub(crate) trace: Option>, pub(crate) current_step: usize, pub(crate) rc_limits: Option<(isize, isize)>, From 20776b49d4c80bed75b394281663bd1dc493a4d5 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 12:51:27 -0300 Subject: [PATCH 10/84] Fix --- cairo1-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 12131245c7..6267275b4d 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -369,7 +369,7 @@ fn run(args: impl Iterator) -> Result, Erro .zip(ret_types_sizes.clone()); let full_ret_types_size: i16 = ret_types_sizes.sum(); - let mut stack_pointer = (vm.get_ap() - (full_ret_types_size - 1) as usize) + let mut stack_pointer = (vm.get_ap() - (full_ret_types_size as usize).saturating_sub(1)) .map_err(VirtualMachineError::Math)?; // Calculate the stack_ptr for each return value as if it were a builtin From 9fbb0c648d18d39f92764230639d62c5d500a4d8 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 12:54:53 -0300 Subject: [PATCH 11/84] Fix trace not enables --- cairo1-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 6267275b4d..3fe6b0d7fe 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -299,7 +299,7 @@ fn run(args: impl Iterator) -> Result, Erro let mut runner = CairoRunner::new_v2(&program, &args.layout, runner_mode)?; - let mut vm = VirtualMachine::new(args.trace_file.is_some()); + let mut vm = VirtualMachine::new(args.trace_file.is_some() || args.air_public_input.is_some()); let end = runner.initialize(&mut vm)?; additional_initialization(&mut vm, data_len)?; From c1779db44049be229e6620fc2d872f27867baf6a Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 Jan 2024 13:02:25 -0300 Subject: [PATCH 12/84] Build execution public memory --- cairo1-run/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 3fe6b0d7fe..27e885dde0 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -395,6 +395,9 @@ fn run(args: impl Iterator) -> Result, Erro .unwrap_or_default(), )?; } + + // Build execution public memory + runner.finalize_segments(&mut vm)?; } runner.relocate(&mut vm, true)?; From 02df22eb324c2dafaa3232161545a4ba2801fe4e Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 Jan 2024 16:17:19 -0300 Subject: [PATCH 13/84] Handle EcOp case --- cairo1-run/src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 27e885dde0..6d87644d9b 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -377,9 +377,14 @@ fn run(args: impl Iterator) -> Result, Erro for (id, size) in ret_types_and_sizes { if let Some(ref name) = id.debug_name { let mut builtin_name = name.to_lowercase(); + // This could be avoided by propery converting between UpperCamelCase & snake_case + // But given the limited amount of cases it is possible to handle it manually instead of adding an external dependency if builtin_name == "rangecheck" { builtin_name = "range_check".to_string(); } + if builtin_name == "ecop" { + builtin_name = "ec_op".to_string(); + } builtin_name = format!("{}_builtin", builtin_name.to_lowercase()); builtin_name_to_stack_pointer.insert(builtin_name, stack_pointer); } From efda66e8253b383765dd4a23feba054109b44ca8 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 Jan 2024 16:19:30 -0300 Subject: [PATCH 14/84] Fix typo --- cairo1-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 6d87644d9b..e5e864ab95 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -377,7 +377,7 @@ fn run(args: impl Iterator) -> Result, Erro for (id, size) in ret_types_and_sizes { if let Some(ref name) = id.debug_name { let mut builtin_name = name.to_lowercase(); - // This could be avoided by propery converting between UpperCamelCase & snake_case + // This could be avoided by properly converting between UpperCamelCase & snake_case // But given the limited amount of cases it is possible to handle it manually instead of adding an external dependency if builtin_name == "rangecheck" { builtin_name = "range_check".to_string(); From b3f71d00e671e260be2d09b330c23fd7386a5989 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 Jan 2024 16:31:58 -0300 Subject: [PATCH 15/84] Use cleaner solution --- cairo1-run/src/main.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index e5e864ab95..cab9b0235b 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -47,6 +47,10 @@ use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::errors::runner_errors::RunnerError; use cairo_vm::vm::errors::trace_errors::TraceError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; +use cairo_vm::vm::runners::builtin_runner::{ + BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, OUTPUT_BUILTIN_NAME, + POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, +}; use cairo_vm::vm::runners::cairo_runner::RunnerMode; use cairo_vm::{ serde::deserialize_program::ReferenceManager, @@ -376,16 +380,16 @@ fn run(args: impl Iterator) -> Result, Erro let mut builtin_name_to_stack_pointer = HashMap::new(); for (id, size) in ret_types_and_sizes { if let Some(ref name) = id.debug_name { - let mut builtin_name = name.to_lowercase(); - // This could be avoided by properly converting between UpperCamelCase & snake_case - // But given the limited amount of cases it is possible to handle it manually instead of adding an external dependency - if builtin_name == "rangecheck" { - builtin_name = "range_check".to_string(); - } - if builtin_name == "ecop" { - builtin_name = "ec_op".to_string(); - } - builtin_name = format!("{}_builtin", builtin_name.to_lowercase()); + let builtin_name = match &*name.to_string() { + "RangeCheck" => RANGE_CHECK_BUILTIN_NAME, + "Poseidon" => POSEIDON_BUILTIN_NAME, + "EcOp" => EC_OP_BUILTIN_NAME, + "Bitwise" => BITWISE_BUILTIN_NAME, + "Pedersen" => HASH_BUILTIN_NAME, + "Output" => OUTPUT_BUILTIN_NAME, + "Ecdsa" => SIGNATURE_BUILTIN_NAME, + _ => break, + }; builtin_name_to_stack_pointer.insert(builtin_name, stack_pointer); } stack_pointer.offset += size as usize; From a9c67d53b6991fb6c8d172e6488352c2a3d8ce98 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 Jan 2024 16:36:19 -0300 Subject: [PATCH 16/84] Fix --- cairo1-run/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index cab9b0235b..45810d3ad7 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -388,7 +388,10 @@ fn run(args: impl Iterator) -> Result, Erro "Pedersen" => HASH_BUILTIN_NAME, "Output" => OUTPUT_BUILTIN_NAME, "Ecdsa" => SIGNATURE_BUILTIN_NAME, - _ => break, + _ => { + stack_pointer.offset += size as usize; + continue; + } }; builtin_name_to_stack_pointer.insert(builtin_name, stack_pointer); } From d821708b18cd7912a8a9403b016d1c47ea268d1d Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 Jan 2024 16:45:55 -0300 Subject: [PATCH 17/84] Improve comments --- cairo1-run/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 45810d3ad7..b8d4c64a0c 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -361,6 +361,7 @@ fn run(args: impl Iterator) -> Result, Erro // Set stop pointers for builtins so we can obtain the air public input if args.air_public_input.is_some() { + // Cairo 1 programs have other return values aside from the used builtin's final pointers, so we need to hand-pick them let ret_types_sizes = main_func .signature .ret_types @@ -376,7 +377,7 @@ fn run(args: impl Iterator) -> Result, Erro let mut stack_pointer = (vm.get_ap() - (full_ret_types_size as usize).saturating_sub(1)) .map_err(VirtualMachineError::Math)?; - // Calculate the stack_ptr for each return value as if it were a builtin + // Calculate the stack_ptr for each return builtin in the return values let mut builtin_name_to_stack_pointer = HashMap::new(); for (id, size) in ret_types_and_sizes { if let Some(ref name) = id.debug_name { From b3bd50cd8bbd81fbf41c8f1721ca48aa3abb0d8b Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 Jan 2024 16:50:41 -0300 Subject: [PATCH 18/84] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac6dd24a22..604f6a5abe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Add `air_public_input` flag to `cairo1-run` [#1539] (https://github.com/lambdaclass/cairo-vm/pull/1539) + * feat: Add `proof_mode` flag to `cairo1-run` [#1537] (https://github.com/lambdaclass/cairo-vm/pull/1537) * The cairo1-run crate no longer compiles and executes in proof_mode by default * Add flag `proof_mode` to cairo1-run crate. Activating this flag will enable proof_mode compilation and execution From 4ee531a0b837b1995bf1a9b01a20cd20a3df79cc Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 14:24:06 -0300 Subject: [PATCH 19/84] Add args flag --- cairo1-run/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index b8d4c64a0c..8f01bbdadc 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -85,6 +85,8 @@ struct Args { proof_mode: bool, #[clap(long = "air_public_input", value_parser)] air_public_input: Option, + #[clap(long = "args", num_args=1.., value_delimiter = ' ')] + args: Vec, } fn validate_layout(value: &str) -> Result { From ab2d47f73b870e951919abaf2a39943ca538ec33 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 15:04:40 -0300 Subject: [PATCH 20/84] Handle arguments --- cairo1-run/src/main.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 8f01bbdadc..2fd9280bc0 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -220,6 +220,7 @@ fn run(args: impl Iterator) -> Result, Erro main_func, initial_gas, args.proof_mode, + &args.args, )?; // Get the user program instructions @@ -564,6 +565,7 @@ fn create_entry_code( func: &Function, initial_gas: usize, proof_mode: bool, + args: &Vec, ) -> Result<(Vec, Vec), Error> { let mut ctx = casm! {}; // The builtins in the formatting expected by the runner. @@ -571,6 +573,7 @@ fn create_entry_code( // Load all vecs to memory. let mut ap_offset: i16 = 0; let after_vecs_offset = ap_offset; + let mut arg_iter = args.iter(); if func.signature.param_types.iter().any(|ty| { get_info(sierra_program_registry, ty) .map(|x| x.long_id.generic_id == SegmentArenaType::ID) @@ -618,7 +621,8 @@ fn create_entry_code( casm_extend! {ctx, [ap + 0] = [ap + offset] + 3, ap++; } - // This code should be re enabled to make the programs work with arguments + + // This cli's args flag only supports single values, so we skip the array handling // } else if let Some(Arg::Array(_)) = arg_iter.peek() { // let values = extract_matches!(arg_iter.next().unwrap(), Arg::Array); @@ -639,6 +643,17 @@ fn create_entry_code( // } // } // } + } else { + let arg_size = ty_size; + //expected_arguments_size += arg_size as usize; + for _ in 0..arg_size { + if let Some(value) = arg_iter.next() { + let value = Felt252::from_dec_str(value).unwrap(); + casm_extend! {ctx, + [ap + 0] = (value.to_bigint()), ap++; + } + } + } }; ap_offset += ty_size; } From 169ed181446e5c979ae28c4e199eec3fa3bc663c Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 15:11:42 -0300 Subject: [PATCH 21/84] Handle arg size & add tests --- cairo1-run/src/main.rs | 49 ++++++++++++++++--- .../cairo-1-programs/branching.cairo | 7 +++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 cairo_programs/cairo-1-programs/branching.cairo diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 2fd9280bc0..19c49d2aeb 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -144,6 +144,8 @@ enum Error { NoInfoForType(ConcreteTypeId), #[error("Failed to extract return values from VM")] FailedToExtractReturnValues, + #[error("Function expects arguments of size {expected} and received {actual} instead.")] + ArgumentsSizeMismatch { expected: usize, actual: usize }, } pub struct FileWriter { @@ -574,6 +576,7 @@ fn create_entry_code( let mut ap_offset: i16 = 0; let after_vecs_offset = ap_offset; let mut arg_iter = args.iter(); + let mut expected_arguments_size = 0; if func.signature.param_types.iter().any(|ty| { get_info(sierra_program_registry, ty) .map(|x| x.long_id.generic_id == SegmentArenaType::ID) @@ -645,7 +648,7 @@ fn create_entry_code( // } } else { let arg_size = ty_size; - //expected_arguments_size += arg_size as usize; + expected_arguments_size += arg_size as usize; for _ in 0..arg_size { if let Some(value) = arg_iter.next() { let value = Felt252::from_dec_str(value).unwrap(); @@ -657,12 +660,12 @@ fn create_entry_code( }; ap_offset += ty_size; } - // if expected_arguments_size != args.len() { - // return Err(RunnerError::ArgumentsSizeMismatch { - // expected: expected_arguments_size, - // actual: args.len(), - // }); - // } + if expected_arguments_size != args.len() { + return Err(Error::ArgumentsSizeMismatch { + expected: expected_arguments_size, + actual: args.len(), + }); + } let before_final_call = ctx.current_code_offset; let final_call_size = 3; @@ -903,4 +906,36 @@ mod tests { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1024)]); } + + #[rstest] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "0"].as_slice())] + fn test_run_branching_0(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); + } + + #[rstest] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "96"].as_slice())] + fn test_run_branching_not_0(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(0)]); + } + + #[rstest] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo", "--proof_mode"].as_slice())] + fn test_run_branching_no_args(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Err(Error::ArgumentsSizeMismatch { expected, actual }) if expected == 1 && actual == 0); + } + + #[rstest] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo","--args", "1, 2, 3"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo", "--proof_mode", "--args", "1, 2, 3"].as_slice())] + fn test_run_branching_too_many_args(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Err(Error::ArgumentsSizeMismatch { expected, actual }) if expected == 1 && actual == 3); + } } diff --git a/cairo_programs/cairo-1-programs/branching.cairo b/cairo_programs/cairo-1-programs/branching.cairo new file mode 100644 index 0000000000..639763b849 --- /dev/null +++ b/cairo_programs/cairo-1-programs/branching.cairo @@ -0,0 +1,7 @@ +fn main(argc: u32) -> u8 { + if argc == 0 { + 1_u8 + } else { + 0_u8 + } +} From 482ead4a223c6daccf1e60ac0cb7c0eb4d098466 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 17:37:03 -0300 Subject: [PATCH 22/84] Parse array arguments --- cairo1-run/README.md | 21 ++- cairo1-run/src/main.rs | 139 +++++++++++++----- .../cairo-1-programs/array_input_sum.cairo | 5 + 3 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 cairo_programs/cairo-1-programs/array_input_sum.cairo diff --git a/cairo1-run/README.md b/cairo1-run/README.md index 95be3b5ecb..df881d3d6d 100644 --- a/cairo1-run/README.md +++ b/cairo1-run/README.md @@ -5,9 +5,10 @@ A cairo-vm crate to run Cairo 1 Programs Once you are inside the `./cairo1-run` folder, use the CLI with the following commands To install the required dependencies(cairo corelib) run + ```bash make deps -``` +``` Now that you have the dependencies necessary to run the tests, you can run: @@ -16,16 +17,32 @@ make test ``` To execute a cairo 1 program + ```bash cargo run ../cairo_programs/cairo-1-programs/fibonacci.cairo ``` Arguments to generate the trace and memory files + ```bash cargo run ../cairo_programs/cairo-1-programs/fibonacci.cairo --trace_file ../cairo_programs/cairo-1-programs/fibonacci.trace --memory_file ../cairo_programs/cairo-1-programs/fibonacci.memory ``` +To pass arguments to `main` + +* Separate arguments with a space in between +* In order to pass arrays, wrap array values inside brackets + +Example: + +```bash + +cargo run ../cairo_programs/cairo-1-programs/array_input_sum.cairo --args '2 [1 2 3 4] 0 [9 8]' + +``` + To execute all the cairo 1 programs inside `../cairo_programs/cairo-1-programs/` and generate the corresponding trace and the memory files + ```bash make run -``` +``` diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 19c49d2aeb..a3bbe9b622 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -4,6 +4,7 @@ use cairo_lang_casm::casm; use cairo_lang_casm::casm_extend; use cairo_lang_casm::hints::Hint; use cairo_lang_casm::instructions::Instruction; +use cairo_lang_compiler::db; use cairo_lang_compiler::{compile_cairo_project_at_path, CompilerConfig}; use cairo_lang_sierra::extensions::bitwise::BitwiseType; use cairo_lang_sierra::extensions::core::{CoreLibfunc, CoreType}; @@ -30,6 +31,7 @@ use cairo_lang_sierra_to_casm::metadata::MetadataComputationConfig; use cairo_lang_sierra_to_casm::metadata::MetadataError; use cairo_lang_sierra_to_casm::{compiler::compile, metadata::calc_metadata}; use cairo_lang_sierra_type_size::get_type_size_map; +use cairo_lang_utils::extract_matches; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use cairo_lang_utils::unordered_hash_map::UnorderedHashMap; use cairo_vm::air_public_input::PublicInputError; @@ -85,8 +87,54 @@ struct Args { proof_mode: bool, #[clap(long = "air_public_input", value_parser)] air_public_input: Option, - #[clap(long = "args", num_args=1.., value_delimiter = ' ')] - args: Vec, + // Arguments should be spaced, with array elements placed between brackets + // For example " --args '1 2 [1 2 3]'" will yield 3 arguments, with the last one being an array of 3 elements + #[clap(long = "args", default_value = "", value_parser=process_args)] + args: FuncArgs, +} + +#[derive(Debug, Clone)] +enum FuncArg { + Array(Vec), + Single(Felt252), +} + +#[derive(Debug, Clone, Default)] +struct FuncArgs(Vec); + +fn process_args(value: &str) -> Result { + let mut args = Vec::new(); + if value.is_empty() { + return Ok(FuncArgs::default()); + } + let mut input = value.split(' '); + while let Some(value) = input.next() { + // First argument in an array + if value.starts_with('[') { + let mut array_arg = + vec![Felt252::from_dec_str(value.strip_prefix('[').unwrap()).unwrap()]; + // Process following args in array + let mut array_end = false; + while !array_end { + if let Some(value) = input.next() { + // Last arg in array + if value.ends_with(']') { + array_arg + .push(Felt252::from_dec_str(value.strip_suffix(']').unwrap()).unwrap()); + array_end = true; + } else { + array_arg.push(Felt252::from_dec_str(value).unwrap()) + } + } + } + // Finalize array + args.push(FuncArg::Array(array_arg)) + } else { + // Single argument + args.push(FuncArg::Single(Felt252::from_dec_str(value).unwrap())) + } + } + Ok(FuncArgs(args)) } fn validate_layout(value: &str) -> Result { @@ -222,7 +270,7 @@ fn run(args: impl Iterator) -> Result, Erro main_func, initial_gas, args.proof_mode, - &args.args, + &args.args.0, )?; // Get the user program instructions @@ -338,7 +386,7 @@ fn run(args: impl Iterator) -> Result, Erro { // Check the failure flag (aka first return value) if return_values.first() != Some(&MaybeRelocatable::from(0)) { - // In case of failure, extract the error from teh return values (aka last two values) + // In case of failure, extract the error from the return values (aka last two values) let panic_data_end = return_values .last() .ok_or(Error::FailedToExtractReturnValues)? @@ -567,15 +615,34 @@ fn create_entry_code( func: &Function, initial_gas: usize, proof_mode: bool, - args: &Vec, + args: &Vec, ) -> Result<(Vec, Vec), Error> { let mut ctx = casm! {}; // The builtins in the formatting expected by the runner. let (builtins, builtin_offset) = get_function_builtins(func); // Load all vecs to memory. + // Load all array args content to memory. + let mut array_args_data = vec![]; let mut ap_offset: i16 = 0; - let after_vecs_offset = ap_offset; - let mut arg_iter = args.iter(); + for arg in args { + let FuncArg::Array(values) = arg else { continue }; + array_args_data.push(ap_offset); + casm_extend! {ctx, + %{ memory[ap + 0] = segments.add() %} + ap += 1; + } + for (i, v) in values.iter().enumerate() { + let arr_at = (i + 1) as i16; + casm_extend! {ctx, + [ap + 0] = (v.to_bigint()); + [ap + 0] = [[ap - arr_at] + (i as i16)], ap++; + }; + } + ap_offset += (1 + values.len()) as i16; + } + let mut array_args_data_iter = array_args_data.iter(); + let after_arrays_data_offset = ap_offset; + let mut arg_iter = args.iter().peekable(); let mut expected_arguments_size = 0; if func.signature.param_types.iter().any(|ty| { get_info(sierra_program_registry, ty) @@ -620,38 +687,24 @@ fn create_entry_code( [ap + 0] = initial_gas, ap++; } } else if generic_ty == &SegmentArenaType::ID { - let offset = -ap_offset + after_vecs_offset; + let offset = -ap_offset + after_arrays_data_offset; casm_extend! {ctx, [ap + 0] = [ap + offset] + 3, ap++; } - - // This cli's args flag only supports single values, so we skip the array handling - - // } else if let Some(Arg::Array(_)) = arg_iter.peek() { - // let values = extract_matches!(arg_iter.next().unwrap(), Arg::Array); - // let offset = -ap_offset + vecs.pop().unwrap(); - // expected_arguments_size += 1; - // casm_extend! {ctx, - // [ap + 0] = [ap + (offset)], ap++; - // [ap + 0] = [ap - 1] + (values.len()), ap++; - // } - // } else { - // let arg_size = ty_size; - // expected_arguments_size += arg_size as usize; - // for _ in 0..arg_size { - // if let Some(value) = arg_iter.next() { - // let value = extract_matches!(value, Arg::Value); - // casm_extend! {ctx, - // [ap + 0] = (value.to_bigint()), ap++; - // } - // } - // } + } else if let Some(FuncArg::Array(_)) = arg_iter.peek() { + let values = extract_matches!(arg_iter.next().unwrap(), FuncArg::Array); + let offset = -ap_offset + array_args_data_iter.next().unwrap(); + expected_arguments_size += 1; + casm_extend! {ctx, + [ap + 0] = [ap + (offset)], ap++; + [ap + 0] = [ap - 1] + (values.len()), ap++; + } } else { let arg_size = ty_size; expected_arguments_size += arg_size as usize; for _ in 0..arg_size { if let Some(value) = arg_iter.next() { - let value = Felt252::from_dec_str(value).unwrap(); + let value = extract_matches!(value, FuncArg::Single); casm_extend! {ctx, [ap + 0] = (value.to_bigint()), ap++; } @@ -908,34 +961,42 @@ mod tests { } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "0"].as_slice())] fn test_run_branching_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "96"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "96"].as_slice())] fn test_run_branching_not_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(0)]); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_branching_no_args(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Err(Error::ArgumentsSizeMismatch { expected, actual }) if expected == 1 && actual == 0); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo","--args", "1, 2, 3"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--layout", "all_cairo", "--proof_mode", "--args", "1, 2, 3"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo","--args", "1 2 3"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo", "--proof_mode", "--args", "1 2 3"].as_slice())] fn test_run_branching_too_many_args(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Err(Error::ArgumentsSizeMismatch { expected, actual }) if expected == 1 && actual == 3); } + + #[rstest] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + fn test_array_input_sum(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(12)]); + } } diff --git a/cairo_programs/cairo-1-programs/array_input_sum.cairo b/cairo_programs/cairo-1-programs/array_input_sum.cairo new file mode 100644 index 0000000000..1c75b21549 --- /dev/null +++ b/cairo_programs/cairo-1-programs/array_input_sum.cairo @@ -0,0 +1,5 @@ +use array::ArrayTrait; + +fn main(index_a: u32, array_a: Array, index_b: u32, array_b: Array) -> u32 { + *array_a.at(index_a) + *array_b.at(index_b) +} From 3b81dd30ef58fd0ea1f5566a255c2d222447e2cd Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 17:46:39 -0300 Subject: [PATCH 23/84] Fix language --- cairo1-run/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cairo1-run/README.md b/cairo1-run/README.md index df881d3d6d..374cbec981 100644 --- a/cairo1-run/README.md +++ b/cairo1-run/README.md @@ -30,8 +30,8 @@ cargo run ../cairo_programs/cairo-1-programs/fibonacci.cairo --trace_file ../cai To pass arguments to `main` -* Separate arguments with a space in between -* In order to pass arrays, wrap array values inside brackets +* Separate arguments with a whitespace inbetween +* In order to pass arrays, wrap array values between brackets Example: From a73b023c11d7506b8c51ff02e57dcae7a985be72 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 17:48:51 -0300 Subject: [PATCH 24/84] reorder --- cairo1-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index a3bbe9b622..0425297579 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -103,10 +103,10 @@ enum FuncArg { struct FuncArgs(Vec); fn process_args(value: &str) -> Result { - let mut args = Vec::new(); if value.is_empty() { return Ok(FuncArgs::default()); } + let mut args = Vec::new(); let mut input = value.split(' '); while let Some(value) = input.next() { // First argument in an array From fd93a34725c1c82329c1f769a275606f0672b8e6 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 17:50:53 -0300 Subject: [PATCH 25/84] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 604f6a5abe..fc8f15ebb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Add `args` flag to `cairo1-run` [#15551] (https://github.com/lambdaclass/cairo-vm/pull/15551) + * feat: Add `air_public_input` flag to `cairo1-run` [#1539] (https://github.com/lambdaclass/cairo-vm/pull/1539) * feat: Add `proof_mode` flag to `cairo1-run` [#1537] (https://github.com/lambdaclass/cairo-vm/pull/1537) From 38070d8fb6d1d2e0da83db5827f228bc77d950c7 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 18:18:55 -0300 Subject: [PATCH 26/84] Move programs with args to an inner folder --- cairo1-run/src/main.rs | 20 +++++++++---------- .../{ => with_input}/array_input_sum.cairo | 0 .../{ => with_input}/branching.cairo | 0 3 files changed, 10 insertions(+), 10 deletions(-) rename cairo_programs/cairo-1-programs/{ => with_input}/array_input_sum.cairo (100%) rename cairo_programs/cairo-1-programs/{ => with_input}/branching.cairo (100%) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 0425297579..6ef3932dff 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -961,40 +961,40 @@ mod tests { } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "0"].as_slice())] fn test_run_branching_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "96"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "96"].as_slice())] fn test_run_branching_not_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(0)]); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo", "--proof_mode"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--layout", "all_cairo", "--proof_mode"].as_slice())] fn test_run_branching_no_args(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Err(Error::ArgumentsSizeMismatch { expected, actual }) if expected == 1 && actual == 0); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo","--args", "1 2 3"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/branching.cairo", "--layout", "all_cairo", "--proof_mode", "--args", "1 2 3"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--layout", "all_cairo","--args", "1 2 3"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--layout", "all_cairo", "--proof_mode", "--args", "1 2 3"].as_slice())] fn test_run_branching_too_many_args(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Err(Error::ArgumentsSizeMismatch { expected, actual }) if expected == 1 && actual == 3); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] fn test_array_input_sum(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(12)]); diff --git a/cairo_programs/cairo-1-programs/array_input_sum.cairo b/cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo similarity index 100% rename from cairo_programs/cairo-1-programs/array_input_sum.cairo rename to cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo diff --git a/cairo_programs/cairo-1-programs/branching.cairo b/cairo_programs/cairo-1-programs/with_input/branching.cairo similarity index 100% rename from cairo_programs/cairo-1-programs/branching.cairo rename to cairo_programs/cairo-1-programs/with_input/branching.cairo From fe145e840d5319a81c58184f9d07788fb1a3c465 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 9 Jan 2024 18:57:42 -0300 Subject: [PATCH 27/84] Implement get_air_private_input for RangeCheck; --- vm/src/air_private_input.rs | 17 +++++++++++++++ vm/src/lib.rs | 1 + vm/src/types/relocatable.rs | 9 ++++++++ .../vm/runners/builtin_runner/range_check.rs | 21 ++++++++++++++++--- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 vm/src/air_private_input.rs diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs new file mode 100644 index 0000000000..df221f46bc --- /dev/null +++ b/vm/src/air_private_input.rs @@ -0,0 +1,17 @@ +use crate::Felt252; + +pub enum PrivateInput { + Value(PrivateInputValue), + Pair(PrivateInputPair), +} + +pub struct PrivateInputValue { + pub index: usize, + pub value: Felt252, +} + +pub struct PrivateInputPair { + pub index: usize, + pub x: Felt252, + pub y: Felt252, +} diff --git a/vm/src/lib.rs b/vm/src/lib.rs index a1138650a7..88415c121c 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -53,6 +53,7 @@ pub mod stdlib { pub use crate::without_std::*; } +pub mod air_private_input; pub mod air_public_input; pub mod cairo_run; pub mod hint_processor; diff --git a/vm/src/types/relocatable.rs b/vm/src/types/relocatable.rs index 353be453ae..e903b1670c 100644 --- a/vm/src/types/relocatable.rs +++ b/vm/src/types/relocatable.rs @@ -320,6 +320,7 @@ impl MaybeRelocatable { } } + // TODO: Check if its more performant to use get_int instead /// Returns a reference to the inner value if it is a Felt252, returns None otherwise. pub fn get_int_ref(&self) -> Option<&Felt252> { match self { @@ -328,6 +329,14 @@ impl MaybeRelocatable { } } + /// Returns the inner value if it is a Felt252, returns None otherwise. + pub fn get_int(&self) -> Option { + match self { + MaybeRelocatable::Int(num) => Some(*num), + MaybeRelocatable::RelocatableValue(_) => None, + } + } + /// Returns the inner value if it is a Relocatable, returns None otherwise. pub fn get_relocatable(&self) -> Option { match self { diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index 1bee53200a..d15e704bbc 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -1,6 +1,9 @@ -use crate::stdlib::{ - cmp::{max, min}, - prelude::*, +use crate::{ + air_private_input::{PrivateInput, PrivateInputValue}, + stdlib::{ + cmp::{max, min}, + prelude::*, + }, }; use crate::Felt252; @@ -192,6 +195,18 @@ impl RangeCheckBuiltinRunner { Ok(pointer) } } + + fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + if let Some(segment) = memory.data.get(self.base) { + for (index, val) in segment.iter().enumerate() { + if let Some(value) = val.as_ref().and_then(|cell| cell.get_value().get_int()) { + private_inputs.push(PrivateInput::Value(PrivateInputValue { index, value })) + } + } + } + private_inputs + } } #[cfg(test)] From dff4d39068c8b8deb8c963bc3fa1537609f06fa6 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 11:15:31 -0300 Subject: [PATCH 28/84] Implement air_private_input for Bitwise; --- fuzzer/Cargo.lock | 2 +- vm/src/air_private_input.rs | 5 ++ vm/src/vm/runners/builtin_runner/bitwise.rs | 66 +++++++++++++++++++ vm/src/vm/runners/builtin_runner/mod.rs | 9 +++ .../vm/runners/builtin_runner/range_check.rs | 2 +- 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/fuzzer/Cargo.lock b/fuzzer/Cargo.lock index 5d9996922c..236551e8c5 100644 --- a/fuzzer/Cargo.lock +++ b/fuzzer/Cargo.lock @@ -160,7 +160,7 @@ checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "cairo-vm" -version = "0.9.1" +version = "1.0.0-rc0" dependencies = [ "anyhow", "arbitrary", diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index df221f46bc..f3d6639eba 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,15 +1,20 @@ +use serde::{Deserialize, Serialize}; + use crate::Felt252; +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub enum PrivateInput { Value(PrivateInputValue), Pair(PrivateInputPair), } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct PrivateInputValue { pub index: usize, pub value: Felt252, } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct PrivateInputPair { pub index: usize, pub x: Felt252, diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index fea49fa89a..6d7e337aa0 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::{PrivateInput, PrivateInputPair}; use crate::stdlib::{boxed::Box, vec::Vec}; use crate::Felt252; use crate::{ @@ -191,6 +192,27 @@ impl BitwiseBuiltinRunner { let used_cells = self.get_used_cells(segments)?; Ok(div_ceil(used_cells, self.cells_per_instance as usize)) } + + pub fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + if let Some(segment) = memory.data.get(self.base) { + let segment_len = segment.len(); + for (index, off) in (0..segment_len).step_by(5).enumerate() { + // Add the input cells of each bitwise instance to the private inputs + if let (Ok(x), Ok(y)) = ( + memory.get_integer((self.base as isize, off).into()), + memory.get_integer((self.base as isize, off + 1).into()), + ) { + private_inputs.push(PrivateInput::Pair(PrivateInputPair { + index, + x: *x, + y: *y, + })) + } + } + } + private_inputs + } } #[cfg(test)] @@ -594,4 +616,48 @@ mod tests { )); assert_eq!(builtin.get_used_diluted_check_units(50, 25), 250); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7), + ((0, 8), 8), + ((0, 9), 9), + ((0, 10), 10), + ((0, 11), 11), + ((0, 12), 12), + ((0, 13), 13), + ((0, 14), 14) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::Pair(PrivateInputPair { + index: 0, + x: 0.into(), + y: 1.into() + }), + PrivateInput::Pair(PrivateInputPair { + index: 1, + x: 5.into(), + y: 6.into() + }), + PrivateInput::Pair(PrivateInputPair { + index: 2, + x: 10.into(), + y: 11.into() + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 4b27c80eb4..dcd11a6068 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::PrivateInput; use crate::math_utils::safe_div_usize; use crate::stdlib::prelude::*; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; @@ -483,6 +484,14 @@ impl BuiltinRunner { } } + pub fn air_private_input(&self, memory: &Memory) -> Vec { + match self { + BuiltinRunner::RangeCheck(builtin) => builtin.air_private_input(memory), + BuiltinRunner::Bitwise(builtin) => builtin.air_private_input(memory), + _ => todo!(), + } + } + #[cfg(test)] pub(crate) fn set_stop_ptr(&mut self, stop_ptr: usize) { match self { diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index d15e704bbc..9ea9d9c76a 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -196,7 +196,7 @@ impl RangeCheckBuiltinRunner { } } - fn air_private_input(&self, memory: &Memory) -> Vec { + pub fn air_private_input(&self, memory: &Memory) -> Vec { let mut private_inputs = vec![]; if let Some(segment) = memory.data.get(self.base) { for (index, val) in segment.iter().enumerate() { From c6b85fd750c7e6004c9cee9fb3f7e711fc862fca Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 12:08:56 -0300 Subject: [PATCH 29/84] Implement air_private_input for Hash --- vm/src/vm/runners/builtin_runner/bitwise.rs | 2 +- vm/src/vm/runners/builtin_runner/hash.rs | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index 6d7e337aa0..251c5af514 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -197,7 +197,7 @@ impl BitwiseBuiltinRunner { let mut private_inputs = vec![]; if let Some(segment) = memory.data.get(self.base) { let segment_len = segment.len(); - for (index, off) in (0..segment_len).step_by(5).enumerate() { + for (index, off) in (0..segment_len).step_by(CELLS_PER_BITWISE as usize).enumerate() { // Add the input cells of each bitwise instance to the private inputs if let (Ok(x), Ok(y)) = ( memory.get_integer((self.base as isize, off).into()), diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index 555f71f473..42534e7fe7 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::{PrivateInput, PrivateInputPair}; use crate::stdlib::{cell::RefCell, prelude::*}; use crate::types::errors::math_errors::MathError; use crate::types::instance_definitions::pedersen_instance_def::{ @@ -187,6 +188,27 @@ impl HashBuiltinRunner { } BuiltinAdditionalData::Hash(verified_addresses) } + + pub fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + if let Some(segment) = memory.data.get(self.base) { + let segment_len = segment.len(); + for (index, off) in (0..segment_len).step_by(CELLS_PER_HASH as usize).enumerate() { + // Add the input cells of each hash instance to the private inputs + if let (Ok(x), Ok(y)) = ( + memory.get_integer((self.base as isize, off).into()), + memory.get_integer((self.base as isize, off + 1).into()), + ) { + private_inputs.push(PrivateInput::Pair(PrivateInputPair { + index, + x: *x, + y: *y, + })) + } + } + } + private_inputs + } } #[cfg(test)] From 5ee533a433c5f2486849f7bb59e9b8766a339def Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 12:16:32 -0300 Subject: [PATCH 30/84] Update proof_programs symlinks --- cairo_programs/proof_programs/_keccak_alternative_hint.cairo | 1 + cairo_programs/proof_programs/assert_le_felt_old.cairo | 1 + cairo_programs/proof_programs/bigint.cairo | 1 + cairo_programs/proof_programs/bitand_hint.cairo | 1 + .../proof_programs/cairo_finalize_keccak_block_size_1000.cairo | 1 + cairo_programs/proof_programs/chained_ec_op.cairo | 1 + cairo_programs/proof_programs/common_signature.cairo | 1 + cairo_programs/proof_programs/compute_doubling_slope_v2.cairo | 1 + cairo_programs/proof_programs/compute_slope_v2.cairo | 1 + cairo_programs/proof_programs/dict_store_cast_ptr.cairo | 1 + cairo_programs/proof_programs/div_mod_n.cairo | 1 + cairo_programs/proof_programs/ec_double_assign_new_x_v3.cairo | 1 + cairo_programs/proof_programs/ec_double_slope.cairo | 1 + cairo_programs/proof_programs/ec_double_v4.cairo | 1 + cairo_programs/proof_programs/ec_negate.cairo | 1 + cairo_programs/proof_programs/ec_op.cairo | 1 + cairo_programs/proof_programs/ec_recover.cairo | 1 + cairo_programs/proof_programs/ed25519_ec.cairo | 1 + cairo_programs/proof_programs/ed25519_field.cairo | 1 + cairo_programs/proof_programs/efficient_secp256r1_ec.cairo | 1 + cairo_programs/proof_programs/example_blake2s.cairo | 1 + cairo_programs/proof_programs/example_program.cairo | 1 + cairo_programs/proof_programs/fast_ec_add_v2.cairo | 1 + cairo_programs/proof_programs/fast_ec_add_v3.cairo | 1 + cairo_programs/proof_programs/field_arithmetic.cairo | 1 + cairo_programs/proof_programs/finalize_blake2s_v2_hint.cairo | 1 + cairo_programs/proof_programs/fq.cairo | 1 + cairo_programs/proof_programs/fq_test.cairo | 1 + cairo_programs/proof_programs/garaga.cairo | 1 + cairo_programs/proof_programs/highest_bitlen.cairo | 1 + cairo_programs/proof_programs/if_reloc_equal.cairo | 1 + cairo_programs/proof_programs/inv_mod_p_uint512.cairo | 1 + cairo_programs/proof_programs/is_quad_residue_test.cairo | 1 + cairo_programs/proof_programs/is_zero.cairo | 1 + cairo_programs/proof_programs/is_zero_pack.cairo | 1 + cairo_programs/proof_programs/keccak_builtin.cairo | 1 + cairo_programs/proof_programs/keccak_uint256.cairo | 1 + cairo_programs/proof_programs/memory_holes.cairo | 1 + cairo_programs/proof_programs/mul_s_inv.cairo | 1 + cairo_programs/proof_programs/multiplicative_inverse.cairo | 1 + cairo_programs/proof_programs/n_bit.cairo | 1 + cairo_programs/proof_programs/nondet_bigint3_v2.cairo | 1 + cairo_programs/proof_programs/normalize_address.cairo | 1 + cairo_programs/proof_programs/packed_sha256_test.cairo | 1 + cairo_programs/proof_programs/poseidon_builtin.cairo | 1 + cairo_programs/proof_programs/poseidon_hash.cairo | 1 + cairo_programs/proof_programs/poseidon_multirun.cairo | 1 + cairo_programs/proof_programs/recover_y.cairo | 1 + cairo_programs/proof_programs/reduce.cairo | 1 + .../proof_programs/relocate_temporary_segment_append.cairo | 1 + .../proof_programs/relocate_temporary_segment_into_new.cairo | 1 + cairo_programs/proof_programs/secp256r1_div_mod_n.cairo | 1 + cairo_programs/proof_programs/secp256r1_fast_ec_add.cairo | 1 + cairo_programs/proof_programs/secp256r1_slope.cairo | 1 + cairo_programs/proof_programs/sha256_test.cairo | 1 + cairo_programs/proof_programs/split_xx_hint.cairo | 1 + cairo_programs/proof_programs/uint256_improvements.cairo | 1 + cairo_programs/proof_programs/uint384.cairo | 1 + cairo_programs/proof_programs/uint384_extension.cairo | 1 + cairo_programs/proof_programs/uint384_extension_test.cairo | 1 + cairo_programs/proof_programs/uint384_test.cairo | 1 + 61 files changed, 61 insertions(+) create mode 120000 cairo_programs/proof_programs/_keccak_alternative_hint.cairo create mode 120000 cairo_programs/proof_programs/assert_le_felt_old.cairo create mode 120000 cairo_programs/proof_programs/bigint.cairo create mode 120000 cairo_programs/proof_programs/bitand_hint.cairo create mode 120000 cairo_programs/proof_programs/cairo_finalize_keccak_block_size_1000.cairo create mode 120000 cairo_programs/proof_programs/chained_ec_op.cairo create mode 120000 cairo_programs/proof_programs/common_signature.cairo create mode 120000 cairo_programs/proof_programs/compute_doubling_slope_v2.cairo create mode 120000 cairo_programs/proof_programs/compute_slope_v2.cairo create mode 120000 cairo_programs/proof_programs/dict_store_cast_ptr.cairo create mode 120000 cairo_programs/proof_programs/div_mod_n.cairo create mode 120000 cairo_programs/proof_programs/ec_double_assign_new_x_v3.cairo create mode 120000 cairo_programs/proof_programs/ec_double_slope.cairo create mode 120000 cairo_programs/proof_programs/ec_double_v4.cairo create mode 120000 cairo_programs/proof_programs/ec_negate.cairo create mode 120000 cairo_programs/proof_programs/ec_op.cairo create mode 120000 cairo_programs/proof_programs/ec_recover.cairo create mode 120000 cairo_programs/proof_programs/ed25519_ec.cairo create mode 120000 cairo_programs/proof_programs/ed25519_field.cairo create mode 120000 cairo_programs/proof_programs/efficient_secp256r1_ec.cairo create mode 120000 cairo_programs/proof_programs/example_blake2s.cairo create mode 120000 cairo_programs/proof_programs/example_program.cairo create mode 120000 cairo_programs/proof_programs/fast_ec_add_v2.cairo create mode 120000 cairo_programs/proof_programs/fast_ec_add_v3.cairo create mode 120000 cairo_programs/proof_programs/field_arithmetic.cairo create mode 120000 cairo_programs/proof_programs/finalize_blake2s_v2_hint.cairo create mode 120000 cairo_programs/proof_programs/fq.cairo create mode 120000 cairo_programs/proof_programs/fq_test.cairo create mode 120000 cairo_programs/proof_programs/garaga.cairo create mode 120000 cairo_programs/proof_programs/highest_bitlen.cairo create mode 120000 cairo_programs/proof_programs/if_reloc_equal.cairo create mode 120000 cairo_programs/proof_programs/inv_mod_p_uint512.cairo create mode 120000 cairo_programs/proof_programs/is_quad_residue_test.cairo create mode 120000 cairo_programs/proof_programs/is_zero.cairo create mode 120000 cairo_programs/proof_programs/is_zero_pack.cairo create mode 120000 cairo_programs/proof_programs/keccak_builtin.cairo create mode 120000 cairo_programs/proof_programs/keccak_uint256.cairo create mode 120000 cairo_programs/proof_programs/memory_holes.cairo create mode 120000 cairo_programs/proof_programs/mul_s_inv.cairo create mode 120000 cairo_programs/proof_programs/multiplicative_inverse.cairo create mode 120000 cairo_programs/proof_programs/n_bit.cairo create mode 120000 cairo_programs/proof_programs/nondet_bigint3_v2.cairo create mode 120000 cairo_programs/proof_programs/normalize_address.cairo create mode 120000 cairo_programs/proof_programs/packed_sha256_test.cairo create mode 120000 cairo_programs/proof_programs/poseidon_builtin.cairo create mode 120000 cairo_programs/proof_programs/poseidon_hash.cairo create mode 120000 cairo_programs/proof_programs/poseidon_multirun.cairo create mode 120000 cairo_programs/proof_programs/recover_y.cairo create mode 120000 cairo_programs/proof_programs/reduce.cairo create mode 120000 cairo_programs/proof_programs/relocate_temporary_segment_append.cairo create mode 120000 cairo_programs/proof_programs/relocate_temporary_segment_into_new.cairo create mode 120000 cairo_programs/proof_programs/secp256r1_div_mod_n.cairo create mode 120000 cairo_programs/proof_programs/secp256r1_fast_ec_add.cairo create mode 120000 cairo_programs/proof_programs/secp256r1_slope.cairo create mode 120000 cairo_programs/proof_programs/sha256_test.cairo create mode 120000 cairo_programs/proof_programs/split_xx_hint.cairo create mode 120000 cairo_programs/proof_programs/uint256_improvements.cairo create mode 120000 cairo_programs/proof_programs/uint384.cairo create mode 120000 cairo_programs/proof_programs/uint384_extension.cairo create mode 120000 cairo_programs/proof_programs/uint384_extension_test.cairo create mode 120000 cairo_programs/proof_programs/uint384_test.cairo diff --git a/cairo_programs/proof_programs/_keccak_alternative_hint.cairo b/cairo_programs/proof_programs/_keccak_alternative_hint.cairo new file mode 120000 index 0000000000..188837a642 --- /dev/null +++ b/cairo_programs/proof_programs/_keccak_alternative_hint.cairo @@ -0,0 +1 @@ +../_keccak_alternative_hint.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/assert_le_felt_old.cairo b/cairo_programs/proof_programs/assert_le_felt_old.cairo new file mode 120000 index 0000000000..578084defe --- /dev/null +++ b/cairo_programs/proof_programs/assert_le_felt_old.cairo @@ -0,0 +1 @@ +../assert_le_felt_old.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/bigint.cairo b/cairo_programs/proof_programs/bigint.cairo new file mode 120000 index 0000000000..81d408cee4 --- /dev/null +++ b/cairo_programs/proof_programs/bigint.cairo @@ -0,0 +1 @@ +../bigint.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/bitand_hint.cairo b/cairo_programs/proof_programs/bitand_hint.cairo new file mode 120000 index 0000000000..1ba1561a2d --- /dev/null +++ b/cairo_programs/proof_programs/bitand_hint.cairo @@ -0,0 +1 @@ +../bitand_hint.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/cairo_finalize_keccak_block_size_1000.cairo b/cairo_programs/proof_programs/cairo_finalize_keccak_block_size_1000.cairo new file mode 120000 index 0000000000..69fa41ec38 --- /dev/null +++ b/cairo_programs/proof_programs/cairo_finalize_keccak_block_size_1000.cairo @@ -0,0 +1 @@ +../cairo_finalize_keccak_block_size_1000.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/chained_ec_op.cairo b/cairo_programs/proof_programs/chained_ec_op.cairo new file mode 120000 index 0000000000..562981744b --- /dev/null +++ b/cairo_programs/proof_programs/chained_ec_op.cairo @@ -0,0 +1 @@ +../chained_ec_op.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/common_signature.cairo b/cairo_programs/proof_programs/common_signature.cairo new file mode 120000 index 0000000000..9a92f8aa35 --- /dev/null +++ b/cairo_programs/proof_programs/common_signature.cairo @@ -0,0 +1 @@ +../common_signature.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/compute_doubling_slope_v2.cairo b/cairo_programs/proof_programs/compute_doubling_slope_v2.cairo new file mode 120000 index 0000000000..e3400f66cb --- /dev/null +++ b/cairo_programs/proof_programs/compute_doubling_slope_v2.cairo @@ -0,0 +1 @@ +../compute_doubling_slope_v2.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/compute_slope_v2.cairo b/cairo_programs/proof_programs/compute_slope_v2.cairo new file mode 120000 index 0000000000..99e22890e9 --- /dev/null +++ b/cairo_programs/proof_programs/compute_slope_v2.cairo @@ -0,0 +1 @@ +../compute_slope_v2.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/dict_store_cast_ptr.cairo b/cairo_programs/proof_programs/dict_store_cast_ptr.cairo new file mode 120000 index 0000000000..8d1e6a7409 --- /dev/null +++ b/cairo_programs/proof_programs/dict_store_cast_ptr.cairo @@ -0,0 +1 @@ +../dict_store_cast_ptr.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/div_mod_n.cairo b/cairo_programs/proof_programs/div_mod_n.cairo new file mode 120000 index 0000000000..e262b1292a --- /dev/null +++ b/cairo_programs/proof_programs/div_mod_n.cairo @@ -0,0 +1 @@ +../div_mod_n.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ec_double_assign_new_x_v3.cairo b/cairo_programs/proof_programs/ec_double_assign_new_x_v3.cairo new file mode 120000 index 0000000000..8a7e7d4150 --- /dev/null +++ b/cairo_programs/proof_programs/ec_double_assign_new_x_v3.cairo @@ -0,0 +1 @@ +../ec_double_assign_new_x_v3.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ec_double_slope.cairo b/cairo_programs/proof_programs/ec_double_slope.cairo new file mode 120000 index 0000000000..ff9fbd912f --- /dev/null +++ b/cairo_programs/proof_programs/ec_double_slope.cairo @@ -0,0 +1 @@ +../ec_double_slope.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ec_double_v4.cairo b/cairo_programs/proof_programs/ec_double_v4.cairo new file mode 120000 index 0000000000..ec8291916a --- /dev/null +++ b/cairo_programs/proof_programs/ec_double_v4.cairo @@ -0,0 +1 @@ +../ec_double_v4.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ec_negate.cairo b/cairo_programs/proof_programs/ec_negate.cairo new file mode 120000 index 0000000000..c8a8af8d5c --- /dev/null +++ b/cairo_programs/proof_programs/ec_negate.cairo @@ -0,0 +1 @@ +../ec_negate.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ec_op.cairo b/cairo_programs/proof_programs/ec_op.cairo new file mode 120000 index 0000000000..ed20b661af --- /dev/null +++ b/cairo_programs/proof_programs/ec_op.cairo @@ -0,0 +1 @@ +../ec_op.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ec_recover.cairo b/cairo_programs/proof_programs/ec_recover.cairo new file mode 120000 index 0000000000..a0629529c6 --- /dev/null +++ b/cairo_programs/proof_programs/ec_recover.cairo @@ -0,0 +1 @@ +../ec_recover.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ed25519_ec.cairo b/cairo_programs/proof_programs/ed25519_ec.cairo new file mode 120000 index 0000000000..d60a0f17c4 --- /dev/null +++ b/cairo_programs/proof_programs/ed25519_ec.cairo @@ -0,0 +1 @@ +../ed25519_ec.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/ed25519_field.cairo b/cairo_programs/proof_programs/ed25519_field.cairo new file mode 120000 index 0000000000..55fb9bae0e --- /dev/null +++ b/cairo_programs/proof_programs/ed25519_field.cairo @@ -0,0 +1 @@ +../ed25519_field.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/efficient_secp256r1_ec.cairo b/cairo_programs/proof_programs/efficient_secp256r1_ec.cairo new file mode 120000 index 0000000000..cd0e2fb3f9 --- /dev/null +++ b/cairo_programs/proof_programs/efficient_secp256r1_ec.cairo @@ -0,0 +1 @@ +../efficient_secp256r1_ec.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/example_blake2s.cairo b/cairo_programs/proof_programs/example_blake2s.cairo new file mode 120000 index 0000000000..77d4b53f0f --- /dev/null +++ b/cairo_programs/proof_programs/example_blake2s.cairo @@ -0,0 +1 @@ +../example_blake2s.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/example_program.cairo b/cairo_programs/proof_programs/example_program.cairo new file mode 120000 index 0000000000..9bf1899363 --- /dev/null +++ b/cairo_programs/proof_programs/example_program.cairo @@ -0,0 +1 @@ +../example_program.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/fast_ec_add_v2.cairo b/cairo_programs/proof_programs/fast_ec_add_v2.cairo new file mode 120000 index 0000000000..a6012da8d3 --- /dev/null +++ b/cairo_programs/proof_programs/fast_ec_add_v2.cairo @@ -0,0 +1 @@ +../fast_ec_add_v2.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/fast_ec_add_v3.cairo b/cairo_programs/proof_programs/fast_ec_add_v3.cairo new file mode 120000 index 0000000000..ed334949da --- /dev/null +++ b/cairo_programs/proof_programs/fast_ec_add_v3.cairo @@ -0,0 +1 @@ +../fast_ec_add_v3.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/field_arithmetic.cairo b/cairo_programs/proof_programs/field_arithmetic.cairo new file mode 120000 index 0000000000..2ad65272a2 --- /dev/null +++ b/cairo_programs/proof_programs/field_arithmetic.cairo @@ -0,0 +1 @@ +../field_arithmetic.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/finalize_blake2s_v2_hint.cairo b/cairo_programs/proof_programs/finalize_blake2s_v2_hint.cairo new file mode 120000 index 0000000000..36ff9954b3 --- /dev/null +++ b/cairo_programs/proof_programs/finalize_blake2s_v2_hint.cairo @@ -0,0 +1 @@ +../finalize_blake2s_v2_hint.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/fq.cairo b/cairo_programs/proof_programs/fq.cairo new file mode 120000 index 0000000000..d67bd7ebd5 --- /dev/null +++ b/cairo_programs/proof_programs/fq.cairo @@ -0,0 +1 @@ +../fq.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/fq_test.cairo b/cairo_programs/proof_programs/fq_test.cairo new file mode 120000 index 0000000000..7d8b5f7573 --- /dev/null +++ b/cairo_programs/proof_programs/fq_test.cairo @@ -0,0 +1 @@ +../fq_test.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/garaga.cairo b/cairo_programs/proof_programs/garaga.cairo new file mode 120000 index 0000000000..2c29bb6184 --- /dev/null +++ b/cairo_programs/proof_programs/garaga.cairo @@ -0,0 +1 @@ +../garaga.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/highest_bitlen.cairo b/cairo_programs/proof_programs/highest_bitlen.cairo new file mode 120000 index 0000000000..1b72bcb0b6 --- /dev/null +++ b/cairo_programs/proof_programs/highest_bitlen.cairo @@ -0,0 +1 @@ +../highest_bitlen.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/if_reloc_equal.cairo b/cairo_programs/proof_programs/if_reloc_equal.cairo new file mode 120000 index 0000000000..bec0afd84d --- /dev/null +++ b/cairo_programs/proof_programs/if_reloc_equal.cairo @@ -0,0 +1 @@ +../if_reloc_equal.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/inv_mod_p_uint512.cairo b/cairo_programs/proof_programs/inv_mod_p_uint512.cairo new file mode 120000 index 0000000000..cc8662e6db --- /dev/null +++ b/cairo_programs/proof_programs/inv_mod_p_uint512.cairo @@ -0,0 +1 @@ +../inv_mod_p_uint512.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/is_quad_residue_test.cairo b/cairo_programs/proof_programs/is_quad_residue_test.cairo new file mode 120000 index 0000000000..1ea92a4976 --- /dev/null +++ b/cairo_programs/proof_programs/is_quad_residue_test.cairo @@ -0,0 +1 @@ +../is_quad_residue_test.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/is_zero.cairo b/cairo_programs/proof_programs/is_zero.cairo new file mode 120000 index 0000000000..c564d75006 --- /dev/null +++ b/cairo_programs/proof_programs/is_zero.cairo @@ -0,0 +1 @@ +../is_zero.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/is_zero_pack.cairo b/cairo_programs/proof_programs/is_zero_pack.cairo new file mode 120000 index 0000000000..afa37d803c --- /dev/null +++ b/cairo_programs/proof_programs/is_zero_pack.cairo @@ -0,0 +1 @@ +../is_zero_pack.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/keccak_builtin.cairo b/cairo_programs/proof_programs/keccak_builtin.cairo new file mode 120000 index 0000000000..71b14b43ff --- /dev/null +++ b/cairo_programs/proof_programs/keccak_builtin.cairo @@ -0,0 +1 @@ +../keccak_builtin.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/keccak_uint256.cairo b/cairo_programs/proof_programs/keccak_uint256.cairo new file mode 120000 index 0000000000..a2da091a09 --- /dev/null +++ b/cairo_programs/proof_programs/keccak_uint256.cairo @@ -0,0 +1 @@ +../keccak_uint256.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/memory_holes.cairo b/cairo_programs/proof_programs/memory_holes.cairo new file mode 120000 index 0000000000..e1fadbb4fb --- /dev/null +++ b/cairo_programs/proof_programs/memory_holes.cairo @@ -0,0 +1 @@ +../memory_holes.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/mul_s_inv.cairo b/cairo_programs/proof_programs/mul_s_inv.cairo new file mode 120000 index 0000000000..e1a7cd0deb --- /dev/null +++ b/cairo_programs/proof_programs/mul_s_inv.cairo @@ -0,0 +1 @@ +../mul_s_inv.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/multiplicative_inverse.cairo b/cairo_programs/proof_programs/multiplicative_inverse.cairo new file mode 120000 index 0000000000..e281f13e2d --- /dev/null +++ b/cairo_programs/proof_programs/multiplicative_inverse.cairo @@ -0,0 +1 @@ +../multiplicative_inverse.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/n_bit.cairo b/cairo_programs/proof_programs/n_bit.cairo new file mode 120000 index 0000000000..49b68729a0 --- /dev/null +++ b/cairo_programs/proof_programs/n_bit.cairo @@ -0,0 +1 @@ +../n_bit.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/nondet_bigint3_v2.cairo b/cairo_programs/proof_programs/nondet_bigint3_v2.cairo new file mode 120000 index 0000000000..62d24de45b --- /dev/null +++ b/cairo_programs/proof_programs/nondet_bigint3_v2.cairo @@ -0,0 +1 @@ +../nondet_bigint3_v2.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/normalize_address.cairo b/cairo_programs/proof_programs/normalize_address.cairo new file mode 120000 index 0000000000..52b57a98f6 --- /dev/null +++ b/cairo_programs/proof_programs/normalize_address.cairo @@ -0,0 +1 @@ +../normalize_address.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/packed_sha256_test.cairo b/cairo_programs/proof_programs/packed_sha256_test.cairo new file mode 120000 index 0000000000..dfb0a9b318 --- /dev/null +++ b/cairo_programs/proof_programs/packed_sha256_test.cairo @@ -0,0 +1 @@ +../packed_sha256_test.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/poseidon_builtin.cairo b/cairo_programs/proof_programs/poseidon_builtin.cairo new file mode 120000 index 0000000000..bc4fb83e23 --- /dev/null +++ b/cairo_programs/proof_programs/poseidon_builtin.cairo @@ -0,0 +1 @@ +../poseidon_builtin.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/poseidon_hash.cairo b/cairo_programs/proof_programs/poseidon_hash.cairo new file mode 120000 index 0000000000..190a72caae --- /dev/null +++ b/cairo_programs/proof_programs/poseidon_hash.cairo @@ -0,0 +1 @@ +../poseidon_hash.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/poseidon_multirun.cairo b/cairo_programs/proof_programs/poseidon_multirun.cairo new file mode 120000 index 0000000000..30c32a782d --- /dev/null +++ b/cairo_programs/proof_programs/poseidon_multirun.cairo @@ -0,0 +1 @@ +../poseidon_multirun.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/recover_y.cairo b/cairo_programs/proof_programs/recover_y.cairo new file mode 120000 index 0000000000..4fdd3f9e3d --- /dev/null +++ b/cairo_programs/proof_programs/recover_y.cairo @@ -0,0 +1 @@ +../recover_y.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/reduce.cairo b/cairo_programs/proof_programs/reduce.cairo new file mode 120000 index 0000000000..42dbc59c35 --- /dev/null +++ b/cairo_programs/proof_programs/reduce.cairo @@ -0,0 +1 @@ +../reduce.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/relocate_temporary_segment_append.cairo b/cairo_programs/proof_programs/relocate_temporary_segment_append.cairo new file mode 120000 index 0000000000..1f0e8ac7cf --- /dev/null +++ b/cairo_programs/proof_programs/relocate_temporary_segment_append.cairo @@ -0,0 +1 @@ +../relocate_temporary_segment_append.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/relocate_temporary_segment_into_new.cairo b/cairo_programs/proof_programs/relocate_temporary_segment_into_new.cairo new file mode 120000 index 0000000000..02d22be9c6 --- /dev/null +++ b/cairo_programs/proof_programs/relocate_temporary_segment_into_new.cairo @@ -0,0 +1 @@ +../relocate_temporary_segment_into_new.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/secp256r1_div_mod_n.cairo b/cairo_programs/proof_programs/secp256r1_div_mod_n.cairo new file mode 120000 index 0000000000..2049ea105b --- /dev/null +++ b/cairo_programs/proof_programs/secp256r1_div_mod_n.cairo @@ -0,0 +1 @@ +../secp256r1_div_mod_n.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/secp256r1_fast_ec_add.cairo b/cairo_programs/proof_programs/secp256r1_fast_ec_add.cairo new file mode 120000 index 0000000000..9e8a00dff3 --- /dev/null +++ b/cairo_programs/proof_programs/secp256r1_fast_ec_add.cairo @@ -0,0 +1 @@ +../secp256r1_fast_ec_add.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/secp256r1_slope.cairo b/cairo_programs/proof_programs/secp256r1_slope.cairo new file mode 120000 index 0000000000..7e44c983fa --- /dev/null +++ b/cairo_programs/proof_programs/secp256r1_slope.cairo @@ -0,0 +1 @@ +../secp256r1_slope.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/sha256_test.cairo b/cairo_programs/proof_programs/sha256_test.cairo new file mode 120000 index 0000000000..63f8a51ea5 --- /dev/null +++ b/cairo_programs/proof_programs/sha256_test.cairo @@ -0,0 +1 @@ +../sha256_test.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/split_xx_hint.cairo b/cairo_programs/proof_programs/split_xx_hint.cairo new file mode 120000 index 0000000000..a816a67cf3 --- /dev/null +++ b/cairo_programs/proof_programs/split_xx_hint.cairo @@ -0,0 +1 @@ +../split_xx_hint.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/uint256_improvements.cairo b/cairo_programs/proof_programs/uint256_improvements.cairo new file mode 120000 index 0000000000..d44d558e6d --- /dev/null +++ b/cairo_programs/proof_programs/uint256_improvements.cairo @@ -0,0 +1 @@ +../uint256_improvements.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/uint384.cairo b/cairo_programs/proof_programs/uint384.cairo new file mode 120000 index 0000000000..1bf594c3ae --- /dev/null +++ b/cairo_programs/proof_programs/uint384.cairo @@ -0,0 +1 @@ +../uint384.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/uint384_extension.cairo b/cairo_programs/proof_programs/uint384_extension.cairo new file mode 120000 index 0000000000..aa817df393 --- /dev/null +++ b/cairo_programs/proof_programs/uint384_extension.cairo @@ -0,0 +1 @@ +../uint384_extension.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/uint384_extension_test.cairo b/cairo_programs/proof_programs/uint384_extension_test.cairo new file mode 120000 index 0000000000..74fd0c8187 --- /dev/null +++ b/cairo_programs/proof_programs/uint384_extension_test.cairo @@ -0,0 +1 @@ +../uint384_extension_test.cairo \ No newline at end of file diff --git a/cairo_programs/proof_programs/uint384_test.cairo b/cairo_programs/proof_programs/uint384_test.cairo new file mode 120000 index 0000000000..d956e6dae8 --- /dev/null +++ b/cairo_programs/proof_programs/uint384_test.cairo @@ -0,0 +1 @@ +../uint384_test.cairo \ No newline at end of file From b32f9dc2dfa1c217717917238be36dca77be02cc Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 14:28:07 -0300 Subject: [PATCH 31/84] Add EcOp priv input variant --- vm/src/air_private_input.rs | 12 ++++++++++++ vm/src/vm/runners/builtin_runner/bitwise.rs | 5 ++++- vm/src/vm/runners/builtin_runner/hash.rs | 5 ++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index f3d6639eba..e636a9ef60 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -3,9 +3,11 @@ use serde::{Deserialize, Serialize}; use crate::Felt252; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[serde(untagged)] pub enum PrivateInput { Value(PrivateInputValue), Pair(PrivateInputPair), + EcOp(PrivateInputEcOp), } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -20,3 +22,13 @@ pub struct PrivateInputPair { pub x: Felt252, pub y: Felt252, } + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct PrivateInputEcOp { + pub index: usize, + pub p_x: Felt252, + pub p_y: Felt252, + pub m: Felt252, + pub q_x: Felt252, + pub q_y: Felt252, +} diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index 251c5af514..ef95b2da1c 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -197,7 +197,10 @@ impl BitwiseBuiltinRunner { let mut private_inputs = vec![]; if let Some(segment) = memory.data.get(self.base) { let segment_len = segment.len(); - for (index, off) in (0..segment_len).step_by(CELLS_PER_BITWISE as usize).enumerate() { + for (index, off) in (0..segment_len) + .step_by(CELLS_PER_BITWISE as usize) + .enumerate() + { // Add the input cells of each bitwise instance to the private inputs if let (Ok(x), Ok(y)) = ( memory.get_integer((self.base as isize, off).into()), diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index 42534e7fe7..ed828c71d4 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -193,7 +193,10 @@ impl HashBuiltinRunner { let mut private_inputs = vec![]; if let Some(segment) = memory.data.get(self.base) { let segment_len = segment.len(); - for (index, off) in (0..segment_len).step_by(CELLS_PER_HASH as usize).enumerate() { + for (index, off) in (0..segment_len) + .step_by(CELLS_PER_HASH as usize) + .enumerate() + { // Add the input cells of each hash instance to the private inputs if let (Ok(x), Ok(y)) = ( memory.get_integer((self.base as isize, off).into()), From aa1d4f12cd085fb05aacf8337076f292c509ea2c Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 15:55:02 -0300 Subject: [PATCH 32/84] Implement air_private_input for EcOp --- vm/src/vm/runners/builtin_runner/ec_op.rs | 31 +++++++++++++++++++++++ vm/src/vm/runners/builtin_runner/mod.rs | 2 ++ 2 files changed, 33 insertions(+) diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 938cd390fb..5261c98eec 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::{PrivateInput, PrivateInputEcOp}; use crate::stdlib::{borrow::Cow, prelude::*}; use crate::stdlib::{cell::RefCell, collections::HashMap}; use crate::types::instance_definitions::ec_op_instance_def::{ @@ -260,6 +261,36 @@ impl EcOpBuiltinRunner { m = {m:?}\n Q = {q:?}.") } + + pub fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + if let Some(segment) = memory.data.get(self.base) { + let segment_len = segment.len(); + for (index, off) in (0..segment_len) + .step_by(CELLS_PER_EC_OP as usize) + .enumerate() + { + // Add the input cells of each ec_op instance to the private inputs + if let (Ok(p_x), Ok(p_y), Ok(m), Ok(q_x), Ok(q_y)) = ( + memory.get_integer((self.base as isize, off).into()), + memory.get_integer((self.base as isize, off + 1).into()), + memory.get_integer((self.base as isize, off + 2).into()), + memory.get_integer((self.base as isize, off + 3).into()), + memory.get_integer((self.base as isize, off + 4).into()), + ) { + private_inputs.push(PrivateInput::EcOp(PrivateInputEcOp { + index, + p_x: *p_x, + p_y: *p_y, + m: *m, + q_x: *q_x, + q_y: *q_y, + })) + } + } + } + private_inputs + } } #[cfg(test)] diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index dcd11a6068..41c25dc402 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -488,6 +488,8 @@ impl BuiltinRunner { match self { BuiltinRunner::RangeCheck(builtin) => builtin.air_private_input(memory), BuiltinRunner::Bitwise(builtin) => builtin.air_private_input(memory), + BuiltinRunner::Hash(builtin) => builtin.air_private_input(memory), + BuiltinRunner::EcOp(builtin) => builtin.air_private_input(memory), _ => todo!(), } } From 548bbaabb12c41f6badfce7d6d222b5d3b786d7e Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 17:21:58 -0300 Subject: [PATCH 33/84] Implement air_private_input for Poseidon & Signature --- vm/src/air_private_input.rs | 24 ++++++++++++++ vm/src/vm/runners/builtin_runner/mod.rs | 2 ++ vm/src/vm/runners/builtin_runner/poseidon.rs | 27 +++++++++++++++ vm/src/vm/runners/builtin_runner/signature.rs | 33 +++++++++++++++++++ 4 files changed, 86 insertions(+) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index e636a9ef60..09d30c8d5a 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -8,6 +8,8 @@ pub enum PrivateInput { Value(PrivateInputValue), Pair(PrivateInputPair), EcOp(PrivateInputEcOp), + PoseidonState(PrivateInputPoseidonState), + Signature(PrivateInputSignature), } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -32,3 +34,25 @@ pub struct PrivateInputEcOp { pub q_x: Felt252, pub q_y: Felt252, } + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct PrivateInputPoseidonState { + pub index: usize, + pub input_s0: Felt252, + pub input_s1: Felt252, + pub input_s2: Felt252, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct PrivateInputSignature { + pub index: usize, + pub pubkey: Felt252, + pub msg: Felt252, + pub signature_input: SignatureInput, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct SignatureInput { + pub r: Felt252, + pub w: Felt252, +} diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 41c25dc402..ec5997c9ea 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -490,6 +490,8 @@ impl BuiltinRunner { BuiltinRunner::Bitwise(builtin) => builtin.air_private_input(memory), BuiltinRunner::Hash(builtin) => builtin.air_private_input(memory), BuiltinRunner::EcOp(builtin) => builtin.air_private_input(memory), + BuiltinRunner::Poseidon(builtin) => builtin.air_private_input(memory), + BuiltinRunner::Signature(builtin) => builtin.air_private_input(memory), _ => todo!(), } } diff --git a/vm/src/vm/runners/builtin_runner/poseidon.rs b/vm/src/vm/runners/builtin_runner/poseidon.rs index 75c809cd7f..6cab5980a8 100644 --- a/vm/src/vm/runners/builtin_runner/poseidon.rs +++ b/vm/src/vm/runners/builtin_runner/poseidon.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::{PrivateInput, PrivateInputPoseidonState}; use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*}; use crate::types::errors::math_errors::MathError; use crate::types::instance_definitions::poseidon_instance_def::{ @@ -163,6 +164,32 @@ impl PoseidonBuiltinRunner { Ok(pointer) } } + + pub fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + if let Some(segment) = memory.data.get(self.base) { + let segment_len = segment.len(); + for (index, off) in (0..segment_len) + .step_by(CELLS_PER_POSEIDON as usize) + .enumerate() + { + // Add the input cells of each poseidon instance to the private inputs + if let (Ok(input_s0), Ok(input_s1), Ok(input_s2)) = ( + memory.get_integer((self.base as isize, off).into()), + memory.get_integer((self.base as isize, off + 1).into()), + memory.get_integer((self.base as isize, off + 2).into()), + ) { + private_inputs.push(PrivateInput::PoseidonState(PrivateInputPoseidonState { + index, + input_s0: *input_s0, + input_s1: *input_s1, + input_s2: *input_s2, + })) + } + } + } + private_inputs + } } #[cfg(test)] diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index f5d256103a..1b1ee81be3 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::{PrivateInput, PrivateInputSignature, SignatureInput}; use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*, rc::Rc}; use crate::types::errors::math_errors::MathError; @@ -16,8 +17,19 @@ use crate::{ }, }, }; +use lazy_static::lazy_static; use num_integer::div_ceil; use starknet_crypto::{verify, FieldElement, Signature}; +use starknet_types_core::felt::NonZeroFelt; + +lazy_static! { + static ref EC_ORDER: NonZeroFelt = Felt252::from_dec_str( + "3618502788666131213697322783095070105526743751716087489154079457884512865583" + ) + .unwrap() + .try_into() + .unwrap(); +} use super::SIGNATURE_BUILTIN_NAME; @@ -227,6 +239,27 @@ impl SignatureBuiltinRunner { .collect(); BuiltinAdditionalData::Signature(signatures) } + + pub fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + for (addr, signature) in self.signatures.borrow().iter() { + if let (Ok(pubkey), Ok(msg)) = (memory.get_integer(*addr), memory.get_integer(addr + 1)) + { + private_inputs.push(PrivateInput::Signature(PrivateInputSignature { + index: addr.offset.saturating_sub(self.base), + pubkey: *pubkey, + msg: *msg, + signature_input: SignatureInput { + r: Felt252::from_bytes_be(&signature.r.to_bytes_be()), + w: Felt252::from_bytes_be(&signature.r.to_bytes_be()) + .mod_inverse(&EC_ORDER) + .unwrap_or_default(), + }, + })) + } + } + private_inputs + } } #[cfg(test)] From cca1f55bc593ad01cb7a3f5ac51a12f536a647a9 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 17:31:01 -0300 Subject: [PATCH 34/84] Implement air_private_input for Keccak --- vm/src/air_private_input.rs | 14 ++++++ vm/src/vm/runners/builtin_runner/keccak.rs | 46 +++++++++++++++++++ vm/src/vm/runners/builtin_runner/mod.rs | 1 + vm/src/vm/runners/builtin_runner/signature.rs | 7 ++- 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 09d30c8d5a..6e85c68cde 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -9,6 +9,7 @@ pub enum PrivateInput { Pair(PrivateInputPair), EcOp(PrivateInputEcOp), PoseidonState(PrivateInputPoseidonState), + KeccakState(PrivateInputKeccakState), Signature(PrivateInputSignature), } @@ -43,6 +44,19 @@ pub struct PrivateInputPoseidonState { pub input_s2: Felt252, } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct PrivateInputKeccakState { + pub index: usize, + pub input_s0: Felt252, + pub input_s1: Felt252, + pub input_s2: Felt252, + pub input_s3: Felt252, + pub input_s4: Felt252, + pub input_s5: Felt252, + pub input_s6: Felt252, + pub input_s7: Felt252, +} + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct PrivateInputSignature { pub index: usize, diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index cf5c1868db..c7a00292fe 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -1,3 +1,4 @@ +use crate::air_private_input::{PrivateInput, PrivateInputKeccakState}; use crate::math_utils::safe_div_usize; use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*}; use crate::types::instance_definitions::keccak_instance_def::KeccakInstanceDef; @@ -220,6 +221,51 @@ impl KeccakBuiltinRunner { keccak::f1600(&mut keccak_input); Ok(keccak_input.iter().flat_map(|x| x.to_le_bytes()).collect()) } + + pub fn air_private_input(&self, memory: &Memory) -> Vec { + let mut private_inputs = vec![]; + if let Some(segment) = memory.data.get(self.base) { + let segment_len = segment.len(); + for (index, off) in (0..segment_len) + .step_by(self.cells_per_instance as usize) + .enumerate() + { + // Add the input cells of each keccak instance to the private inputs + if let ( + Ok(input_s0), + Ok(input_s1), + Ok(input_s2), + Ok(input_s3), + Ok(input_s4), + Ok(input_s5), + Ok(input_s6), + Ok(input_s7), + ) = ( + memory.get_integer((self.base as isize, off).into()), + memory.get_integer((self.base as isize, off + 1).into()), + memory.get_integer((self.base as isize, off + 2).into()), + memory.get_integer((self.base as isize, off + 3).into()), + memory.get_integer((self.base as isize, off + 4).into()), + memory.get_integer((self.base as isize, off + 5).into()), + memory.get_integer((self.base as isize, off + 6).into()), + memory.get_integer((self.base as isize, off + 7).into()), + ) { + private_inputs.push(PrivateInput::KeccakState(PrivateInputKeccakState { + index, + input_s0: *input_s0, + input_s1: *input_s1, + input_s2: *input_s2, + input_s3: *input_s3, + input_s4: *input_s4, + input_s5: *input_s5, + input_s6: *input_s6, + input_s7: *input_s7, + })) + } + } + } + private_inputs + } } #[cfg(test)] diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index ec5997c9ea..cdd0a68fd1 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -492,6 +492,7 @@ impl BuiltinRunner { BuiltinRunner::EcOp(builtin) => builtin.air_private_input(memory), BuiltinRunner::Poseidon(builtin) => builtin.air_private_input(memory), BuiltinRunner::Signature(builtin) => builtin.air_private_input(memory), + BuiltinRunner::Keccak(builtin) => builtin.air_private_input(memory), _ => todo!(), } } diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index 1b1ee81be3..9dfcf03d37 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -2,6 +2,7 @@ use crate::air_private_input::{PrivateInput, PrivateInputSignature, SignatureInp use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*, rc::Rc}; use crate::types::errors::math_errors::MathError; +use crate::types::instance_definitions::ecdsa_instance_def::CELLS_PER_SIGNATURE; use crate::vm::runners::cairo_pie::BuiltinAdditionalData; use crate::Felt252; use crate::{ @@ -246,7 +247,11 @@ impl SignatureBuiltinRunner { if let (Ok(pubkey), Ok(msg)) = (memory.get_integer(*addr), memory.get_integer(addr + 1)) { private_inputs.push(PrivateInput::Signature(PrivateInputSignature { - index: addr.offset.saturating_sub(self.base), + index: addr + .offset + .saturating_sub(self.base) + .checked_div(CELLS_PER_SIGNATURE as usize) + .unwrap_or_default(), pubkey: *pubkey, msg: *msg, signature_input: SignatureInput { From df18495824eb80c00f4933982c682bafd055473b Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 18:44:19 -0300 Subject: [PATCH 35/84] Add AirPrivateInput serialization --- cairo-vm-cli/src/main.rs | 39 ++++++++++++++++++- vm/src/air_private_input.rs | 51 +++++++++++++++++++++++++ vm/src/vm/runners/builtin_runner/mod.rs | 3 +- vm/src/vm/runners/cairo_runner.rs | 12 ++++++ 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 6bfd81db4c..e3be8d3d99 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -40,6 +40,8 @@ struct Args { secure_run: Option, #[clap(long = "air_public_input")] air_public_input: Option, + #[clap(long = "air_private_input")] + air_private_input: Option, } fn validate_layout(value: &str) -> Result { @@ -119,6 +121,30 @@ fn run(args: impl Iterator) -> Result<(), Error> { return Err(Error::Cli(error)); } + if args.air_public_input.is_some() && !args.proof_mode { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--air_private_input can only be used in proof_mode.", + ); + return Err(Error::Cli(error)); + } + + if args.air_private_input.is_some() && args.trace_file.is_none() { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--trace_file must be set when --air_private_input is set.", + ); + return Err(Error::Cli(error)); + } + + if args.air_private_input.is_some() && args.memory_file.is_none() { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--memory_file must be set when --air_private_input is set.", + ); + return Err(Error::Cli(error)); + } + let trace_enabled = args.trace_file.is_some() || args.air_public_input.is_some(); let mut hint_executor = BuiltinHintProcessor::new_empty(); let cairo_run_config = cairo_run::CairoRunConfig { @@ -148,7 +174,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { print!("{output_buffer}"); } - if let Some(trace_path) = args.trace_file { + if let Some(ref trace_path) = args.trace_file { let relocated_trace = cairo_runner .relocated_trace .as_ref() @@ -162,7 +188,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { trace_writer.flush()?; } - if let Some(memory_path) = args.memory_file { + if let Some(ref memory_path) = args.memory_file { let memory_file = std::fs::File::create(memory_path)?; let mut memory_writer = FileWriter::new(io::BufWriter::with_capacity(5 * 1024 * 1024, memory_file)); @@ -176,6 +202,15 @@ fn run(args: impl Iterator) -> Result<(), Error> { std::fs::write(file_path, json)?; } + if let Some(file_path) = args.air_private_input { + let json = cairo_runner + .get_air_private_input(&vm) + .to_serializable(args.trace_file.unwrap(), args.memory_file.unwrap()) + .serialize_json() + .unwrap(); + std::fs::write(file_path, json)?; + } + Ok(()) } diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 6e85c68cde..4dc00db6cb 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,7 +1,30 @@ +#[cfg(feature = "std")] +use std::path::PathBuf; + +use crate::{stdlib::collections::HashMap, vm::runners::builtin_runner::{HASH_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, KECCAK_BUILTIN_NAME, POSEIDON_BUILTIN_NAME}}; use serde::{Deserialize, Serialize}; use crate::Felt252; +// Serializable format, matches the file output of the python implementation +#[cfg(feature = "std")] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct AirPrivateInputSerializable { + trace_path: PathBuf, + memory_path: PathBuf, + pedersen: Vec, + range_check: Vec, + ecdsa: Vec, + bitwise: Vec, + ec_op: Vec, + keccak: Vec, + poseidon: Vec, +} + +// Contains only builtin public inputs, useful for library users +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AirPrivateInput(pub HashMap<&'static str, Vec>); + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(untagged)] pub enum PrivateInput { @@ -70,3 +93,31 @@ pub struct SignatureInput { pub r: Felt252, pub w: Felt252, } + +#[cfg(feature = "std")] +impl AirPrivateInput { + pub fn to_serializable( + &self, + trace_file: PathBuf, + memory_file: PathBuf, + ) -> AirPrivateInputSerializable { + AirPrivateInputSerializable { + trace_path: trace_file.as_path().canonicalize().unwrap_or(trace_file), + memory_path: memory_file.as_path().canonicalize().unwrap_or(memory_file), + pedersen: self.0.get(HASH_BUILTIN_NAME).cloned().unwrap_or_default(), + range_check: self.0.get(RANGE_CHECK_BUILTIN_NAME).cloned().unwrap_or_default(), + ecdsa: self.0.get(SIGNATURE_BUILTIN_NAME).cloned().unwrap_or_default(), + bitwise: self.0.get(BITWISE_BUILTIN_NAME).cloned().unwrap_or_default(), + ec_op: self.0.get(EC_OP_BUILTIN_NAME).cloned().unwrap_or_default(), + keccak: self.0.get(KECCAK_BUILTIN_NAME).cloned().unwrap_or_default(), + poseidon: self.0.get(POSEIDON_BUILTIN_NAME).cloned().unwrap_or_default(), + } + } +} + +#[cfg(feature = "std")] +impl AirPrivateInputSerializable { + pub fn serialize_json(&self) -> Result { + serde_json::to_string_pretty(&self) + } +} diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index cdd0a68fd1..99778c6333 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -484,6 +484,7 @@ impl BuiltinRunner { } } + // Returns information about the builtin that should be added to the AIR private input. pub fn air_private_input(&self, memory: &Memory) -> Vec { match self { BuiltinRunner::RangeCheck(builtin) => builtin.air_private_input(memory), @@ -493,7 +494,7 @@ impl BuiltinRunner { BuiltinRunner::Poseidon(builtin) => builtin.air_private_input(memory), BuiltinRunner::Signature(builtin) => builtin.air_private_input(memory), BuiltinRunner::Keccak(builtin) => builtin.air_private_input(memory), - _ => todo!(), + _ => vec![], } } diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 57b72fe8b7..2c4c6c4c2a 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,4 +1,5 @@ use crate::{ + air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, stdlib::{ any::Any, @@ -1408,6 +1409,17 @@ impl CairoRunner { .ok_or(PublicInputError::NoRangeCheckLimits)?, ) } + + pub fn get_air_private_input(&self, vm: &VirtualMachine) -> 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.memory), + ); + } + AirPrivateInput(private_inputs) + } } #[derive(Clone, Debug, Eq, PartialEq)] From fd26554f81754f0466e7ada305a203e8d73af9c1 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 18:47:54 -0300 Subject: [PATCH 36/84] Remove unwrap --- cairo-vm-cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index e3be8d3d99..295e4519a4 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -207,7 +207,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { .get_air_private_input(&vm) .to_serializable(args.trace_file.unwrap(), args.memory_file.unwrap()) .serialize_json() - .unwrap(); + .map_err(PublicInputError::Serde)?; std::fs::write(file_path, json)?; } From 8efc27b9113e7030ac749aaff07d3a290a027327 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 10 Jan 2024 19:06:35 -0300 Subject: [PATCH 37/84] Add targets to compare private inputs against python vm --- Makefile | 19 ++++++++++++------- vm/src/tests/compare_vm_state.sh | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index bf17e6743a..33167244cc 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ endif deps deps-macos cargo-deps build run check test clippy coverage benchmark flamegraph \ compare_benchmarks_deps compare_benchmarks docs clean \ compare_vm_output compare_trace_memory compare_trace compare_memory \ - compare_trace_memory_proof compare_all_proof compare_trace_proof compare_memory_proof compare_air_public_input \ + compare_trace_memory_proof compare_all_proof compare_trace_proof compare_memory_proof compare_air_public_input compare_air_private_input\ cairo_bench_programs cairo_proof_programs cairo_test_programs cairo_1_test_contracts cairo_2_test_contracts \ cairo_trace cairo-vm_trace cairo_proof_trace cairo-vm_proof_trace \ fuzzer-deps fuzzer-run-cairo-compiled fuzzer-run-hint-diff build-cairo-lang hint-accountant \ @@ -37,10 +37,12 @@ COMPILED_PROOF_TESTS:=$(patsubst $(TEST_PROOF_DIR)/%.cairo, $(TEST_PROOF_DIR)/%. CAIRO_MEM_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.memory, $(COMPILED_PROOF_TESTS)) CAIRO_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.trace, $(COMPILED_PROOF_TESTS)) CAIRO_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.air_public_input, $(COMPILED_PROOF_TESTS)) +CAIRO_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.air_private_input, $(COMPILED_PROOF_TESTS)) CAIRO_RS_MEM_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.memory, $(COMPILED_PROOF_TESTS)) CAIRO_RS_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.trace, $(COMPILED_PROOF_TESTS)) CAIRO_RS_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.air_public_input, $(COMPILED_PROOF_TESTS)) +CAIRO_RS_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.air_private_input, $(COMPILED_PROOF_TESTS)) PROOF_BENCH_DIR=cairo_programs/benchmarks PROOF_BENCH_FILES:=$(wildcard $(PROOF_BENCH_DIR)/*.cairo) @@ -49,11 +51,11 @@ PROOF_COMPILED_BENCHES:=$(patsubst $(PROOF_BENCH_DIR)/%.cairo, $(PROOF_BENCH_DIR $(TEST_PROOF_DIR)/%.json: $(TEST_PROOF_DIR)/%.cairo cairo-compile --cairo_path="$(TEST_PROOF_DIR):$(PROOF_BENCH_DIR)" $< --output $@ --proof_mode -$(TEST_PROOF_DIR)/%.rs.trace $(TEST_PROOF_DIR)/%.rs.memory $(TEST_PROOF_DIR)/%.rs.air_public_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) - cargo llvm-cov run -p cairo-vm-cli --release --no-report -- --layout starknet_with_keccak --proof_mode $< --trace_file $@ --memory_file $(@D)/$(*F).rs.memory --air_public_input $(@D)/$(*F).rs.air_public_input +$(TEST_PROOF_DIR)/%.rs.trace $(TEST_PROOF_DIR)/%.rs.memory $(TEST_PROOF_DIR)/%.rs.air_public_input $(TEST_PROOF_DIR)/%.rs.air_private_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) + cargo llvm-cov run -p cairo-vm-cli --release --no-report -- --layout starknet_with_keccak --proof_mode $< --trace_file $@ --memory_file $(@D)/$(*F).rs.memory --air_public_input $(@D)/$(*F).rs.air_public_input --air_private_input $(@D)/$(*F).rs.air_private_input -$(TEST_PROOF_DIR)/%.trace $(TEST_PROOF_DIR)/%.memory $(TEST_PROOF_DIR)/%.air_public_input: $(TEST_PROOF_DIR)/%.json - cairo-run --layout starknet_with_keccak --proof_mode --program $< --trace_file $(@D)/$(*F).trace --air_public_input $(@D)/$(*F).air_public_input --memory_file $(@D)/$(*F).memory +$(TEST_PROOF_DIR)/%.trace $(TEST_PROOF_DIR)/%.memory $(TEST_PROOF_DIR)/%.air_public_input $(TEST_PROOF_DIR)/%.air_private_input: $(TEST_PROOF_DIR)/%.json + cairo-run --layout starknet_with_keccak --proof_mode --program $< --trace_file $(@D)/$(*F).trace --air_public_input $(@D)/$(*F).air_public_input --memory_file $(@D)/$(*F).memory --air_private_input $(@D)/$(*F).air_private_input $(PROOF_BENCH_DIR)/%.json: $(PROOF_BENCH_DIR)/%.cairo cairo-compile --cairo_path="$(TEST_PROOF_DIR):$(PROOF_BENCH_DIR)" $< --output $@ --proof_mode @@ -287,8 +289,8 @@ compare_memory: $(CAIRO_RS_MEM) $(CAIRO_MEM) compare_trace_memory_proof: $(COMPILED_PROOF_TESTS) $(CAIRO_RS_TRACE_PROOF) $(CAIRO_TRACE_PROOF) $(CAIRO_RS_MEM_PROOF) $(CAIRO_MEM_PROOF) cd vm/src/tests; ./compare_vm_state.sh trace memory proof_mode -compare_all_proof: $(COMPILED_PROOF_TESTS) $(CAIRO_RS_TRACE_PROOF) $(CAIRO_TRACE_PROOF) $(CAIRO_RS_MEM_PROOF) $(CAIRO_MEM_PROOF) $(CAIRO_RS_AIR_PUBLIC_INPUT) $(CAIRO_AIR_PUBLIC_INPUT) - cd vm/src/tests; ./compare_vm_state.sh trace memory proof_mode air_public_input +compare_all_proof: $(COMPILED_PROOF_TESTS) $(CAIRO_RS_TRACE_PROOF) $(CAIRO_TRACE_PROOF) $(CAIRO_RS_MEM_PROOF) $(CAIRO_MEM_PROOF) $(CAIRO_RS_AIR_PUBLIC_INPUT) $(CAIRO_AIR_PUBLIC_INPUT) $(CAIRO_RS_AIR_PRIVATE_INPUT) $(CAIRO_AIR_PRIVATE_INPUT) + cd vm/src/tests; ./compare_vm_state.sh trace memory proof_mode air_public_input air_private_input compare_trace_proof: $(CAIRO_RS_TRACE_PROOF) $(CAIRO_TRACE_PROOF) cd vm/src/tests; ./compare_vm_state.sh trace proof_mode @@ -299,6 +301,9 @@ compare_memory_proof: $(CAIRO_RS_MEM_PROOF) $(CAIRO_MEM_PROOF) compare_air_public_input: $(CAIRO_RS_AIR_PUBLIC_INPUT) $(CAIRO_AIR_PUBLIC_INPUT) cd vm/src/tests; ./compare_vm_state.sh memory proof_mode air_public_input +compare_air_private_input: $(CAIRO_RS_AIR_PRIVATE_INPUT) $(CAIRO_AIR_PRIVATE_INPUT) + cd vm/src/tests; ./compare_vm_state.sh memory proof_mode air_private_input + # Run with nightly enable the `doc_cfg` feature wich let us provide clear explaination about which parts of the code are behind a feature flag docs: RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --verbose --release --locked --no-deps --all-features --open diff --git a/vm/src/tests/compare_vm_state.sh b/vm/src/tests/compare_vm_state.sh index a4ad23b0ab..20eda930ef 100755 --- a/vm/src/tests/compare_vm_state.sh +++ b/vm/src/tests/compare_vm_state.sh @@ -9,6 +9,7 @@ exit_code=0 trace=false memory=false air_public_input=false +air_private_input=false passed_tests=0 failed_tests=0 @@ -26,6 +27,9 @@ for i in $@; do "air_public_input") air_public_input=true echo "Requested air_public_input comparison" ;; + "air_private_input") air_private_input=true + echo "Requested air_private_input comparison" + ;; *) ;; esac @@ -76,6 +80,16 @@ for file in $(ls $tests_path | grep .cairo$ | sed -E 's/\.cairo$//'); do passed_tests=$((passed_tests + 1)) fi fi + + if $air_private_input; then + if ! ./air_public_input_comparator.py $path_file.air_private_input $path_file.rs.air_private_input; then + echo "Air Private Input differs for $file" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + fi done if test $failed_tests != 0; then From b6228c45f3c5f4a3399a1963177eff6a71a757ef Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 10:18:37 -0300 Subject: [PATCH 38/84] Add separate script to compare private inputs --- vm/src/tests/air_private_input_comparator.py | 26 ++++++++++++++++++++ vm/src/tests/compare_vm_state.sh | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100755 vm/src/tests/air_private_input_comparator.py diff --git a/vm/src/tests/air_private_input_comparator.py b/vm/src/tests/air_private_input_comparator.py new file mode 100755 index 0000000000..92ebf987f3 --- /dev/null +++ b/vm/src/tests/air_private_input_comparator.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys +import json + + +filename1 = sys.argv[1] +filename2 = sys.argv[2] + +with open(filename1, 'r') as cairo_lang_input_file, open(filename2, 'r') as cairo_vm_input_file: + cairo_lang_input = json.load(cairo_lang_input_file) + cairo_vm_input = json.load(cairo_vm_input_file) + # The trace_path & memory_path fields contain the path of each file which will differ + # as we use a different extension to differentiate between python & rust vm outputs + cairo_lang_input["trace_path"] = "" + cairo_lang_input["memory_path"] = "" + + cairo_vm_input["trace_path"] = "" + cairo_vm_input["memory_path"] = "" + + if cairo_lang_input == cairo_vm_input: + + print(f"Comparison succesful for {filename1} vs {filename2}") + else: + print(f"Comparison unsuccesful for {filename1} vs {filename2}") + exit(1) diff --git a/vm/src/tests/compare_vm_state.sh b/vm/src/tests/compare_vm_state.sh index 20eda930ef..24388d20bf 100755 --- a/vm/src/tests/compare_vm_state.sh +++ b/vm/src/tests/compare_vm_state.sh @@ -82,7 +82,7 @@ for file in $(ls $tests_path | grep .cairo$ | sed -E 's/\.cairo$//'); do fi if $air_private_input; then - if ! ./air_public_input_comparator.py $path_file.air_private_input $path_file.rs.air_private_input; then + if ! ./air_private_input_comparator.py $path_file.air_private_input $path_file.rs.air_private_input; then echo "Air Private Input differs for $file" exit_code=1 failed_tests=$((failed_tests + 1)) From 4a32062c7b90f2be363d99dba2b8cf9ed82132b6 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 10:19:51 -0300 Subject: [PATCH 39/84] Ignore & Clean output files --- .gitignore | 1 + Makefile | 1 + 2 files changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 98e6148071..a6f4053583 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ **/*.trace **/*.memory **/*.air_public_input +**/*.air_private_input **/*.swp bench/results .python-version diff --git a/Makefile b/Makefile index 33167244cc..1b3881f5da 100644 --- a/Makefile +++ b/Makefile @@ -321,6 +321,7 @@ clean: rm -f $(TEST_PROOF_DIR)/*.memory rm -f $(TEST_PROOF_DIR)/*.trace rm -f $(TEST_PROOF_DIR)/*.air_public_input + rm -f $(TEST_PROOF_DIR)/*.air_private_input rm -rf cairo-vm-env rm -rf cairo-vm-pypy-env rm -rf cairo From b2ba10b252536775453003694263755b178154c8 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 10:26:48 -0300 Subject: [PATCH 40/84] Fix target --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1b3881f5da..f26997521b 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ $(TEST_PROOF_DIR)/%.json: $(TEST_PROOF_DIR)/%.cairo cairo-compile --cairo_path="$(TEST_PROOF_DIR):$(PROOF_BENCH_DIR)" $< --output $@ --proof_mode $(TEST_PROOF_DIR)/%.rs.trace $(TEST_PROOF_DIR)/%.rs.memory $(TEST_PROOF_DIR)/%.rs.air_public_input $(TEST_PROOF_DIR)/%.rs.air_private_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) - cargo llvm-cov run -p cairo-vm-cli --release --no-report -- --layout starknet_with_keccak --proof_mode $< --trace_file $@ --memory_file $(@D)/$(*F).rs.memory --air_public_input $(@D)/$(*F).rs.air_public_input --air_private_input $(@D)/$(*F).rs.air_private_input + cargo llvm-cov run -p cairo-vm-cli --release --no-report -- --layout starknet_with_keccak --proof_mode $< --trace_file $(@D)/$(*F).rs.trace --memory_file $(@D)/$(*F).rs.memory --air_public_input $(@D)/$(*F).rs.air_public_input --air_private_input $(@D)/$(*F).rs.air_private_input $(TEST_PROOF_DIR)/%.trace $(TEST_PROOF_DIR)/%.memory $(TEST_PROOF_DIR)/%.air_public_input $(TEST_PROOF_DIR)/%.air_private_input: $(TEST_PROOF_DIR)/%.json cairo-run --layout starknet_with_keccak --proof_mode --program $< --trace_file $(@D)/$(*F).trace --air_public_input $(@D)/$(*F).air_public_input --memory_file $(@D)/$(*F).memory --air_private_input $(@D)/$(*F).air_private_input From fce43d24a2c14259f017113b91e4f0118495243d Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 10:32:57 -0300 Subject: [PATCH 41/84] Fix README example --- cairo1-run/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/README.md b/cairo1-run/README.md index 374cbec981..dfa835af8c 100644 --- a/cairo1-run/README.md +++ b/cairo1-run/README.md @@ -37,7 +37,7 @@ Example: ```bash -cargo run ../cairo_programs/cairo-1-programs/array_input_sum.cairo --args '2 [1 2 3 4] 0 [9 8]' +cargo run ../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo --layout all_cairo --args '2 [1 2 3 4] 0 [9 8]' ``` From e91b5234d113db2a592c5cdb2ffd2f9c78e371bf Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 10:56:57 -0300 Subject: [PATCH 42/84] Fix + fmt --- vm/src/air_private_input.rs | 32 +++++++++++++++++++---- vm/src/vm/runners/builtin_runner/ec_op.rs | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 4dc00db6cb..b9a9431841 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,7 +1,13 @@ #[cfg(feature = "std")] use std::path::PathBuf; -use crate::{stdlib::collections::HashMap, vm::runners::builtin_runner::{HASH_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, KECCAK_BUILTIN_NAME, POSEIDON_BUILTIN_NAME}}; +use crate::{ + stdlib::collections::HashMap, + vm::runners::builtin_runner::{ + BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME, + POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, + }, +}; use serde::{Deserialize, Serialize}; use crate::Felt252; @@ -105,12 +111,28 @@ impl AirPrivateInput { trace_path: trace_file.as_path().canonicalize().unwrap_or(trace_file), memory_path: memory_file.as_path().canonicalize().unwrap_or(memory_file), pedersen: self.0.get(HASH_BUILTIN_NAME).cloned().unwrap_or_default(), - range_check: self.0.get(RANGE_CHECK_BUILTIN_NAME).cloned().unwrap_or_default(), - ecdsa: self.0.get(SIGNATURE_BUILTIN_NAME).cloned().unwrap_or_default(), - bitwise: self.0.get(BITWISE_BUILTIN_NAME).cloned().unwrap_or_default(), + range_check: self + .0 + .get(RANGE_CHECK_BUILTIN_NAME) + .cloned() + .unwrap_or_default(), + ecdsa: self + .0 + .get(SIGNATURE_BUILTIN_NAME) + .cloned() + .unwrap_or_default(), + bitwise: self + .0 + .get(BITWISE_BUILTIN_NAME) + .cloned() + .unwrap_or_default(), ec_op: self.0.get(EC_OP_BUILTIN_NAME).cloned().unwrap_or_default(), keccak: self.0.get(KECCAK_BUILTIN_NAME).cloned().unwrap_or_default(), - poseidon: self.0.get(POSEIDON_BUILTIN_NAME).cloned().unwrap_or_default(), + poseidon: self + .0 + .get(POSEIDON_BUILTIN_NAME) + .cloned() + .unwrap_or_default(), } } } diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 5261c98eec..3e985f259a 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -271,7 +271,7 @@ impl EcOpBuiltinRunner { .enumerate() { // Add the input cells of each ec_op instance to the private inputs - if let (Ok(p_x), Ok(p_y), Ok(m), Ok(q_x), Ok(q_y)) = ( + if let (Ok(p_x), Ok(p_y), Ok(q_x), Ok(q_y), Ok(m)) = ( memory.get_integer((self.base as isize, off).into()), memory.get_integer((self.base as isize, off + 1).into()), memory.get_integer((self.base as isize, off + 2).into()), From 93ee579781f5a803e79166dc81f77ab952114ad5 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 11:25:44 -0300 Subject: [PATCH 43/84] Fetch absolute paths in cli and remove feature-gate --- cairo-vm-cli/src/main.rs | 22 +++++++++++++++++++++- vm/src/air_private_input.rs | 18 ++++++------------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 295e4519a4..ba61037294 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -203,9 +203,29 @@ fn run(args: impl Iterator) -> Result<(), Error> { } if let Some(file_path) = args.air_private_input { + // Get absolute paths of trace_file & memory_file + let trace_path = args + .trace_file + .clone() + .unwrap() + .as_path() + .canonicalize() + .unwrap_or(args.trace_file.unwrap()) + .to_string_lossy() + .to_string(); + let memory_path = args + .memory_file + .clone() + .unwrap() + .as_path() + .canonicalize() + .unwrap_or(args.memory_file.unwrap()) + .to_string_lossy() + .to_string(); + let json = cairo_runner .get_air_private_input(&vm) - .to_serializable(args.trace_file.unwrap(), args.memory_file.unwrap()) + .to_serializable(trace_path, memory_path) .serialize_json() .map_err(PublicInputError::Serde)?; std::fs::write(file_path, json)?; diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index b9a9431841..696fe0162c 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,6 +1,3 @@ -#[cfg(feature = "std")] -use std::path::PathBuf; - use crate::{ stdlib::collections::HashMap, vm::runners::builtin_runner::{ @@ -13,11 +10,10 @@ use serde::{Deserialize, Serialize}; use crate::Felt252; // Serializable format, matches the file output of the python implementation -#[cfg(feature = "std")] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct AirPrivateInputSerializable { - trace_path: PathBuf, - memory_path: PathBuf, + trace_path: String, + memory_path: String, pedersen: Vec, range_check: Vec, ecdsa: Vec, @@ -100,16 +96,15 @@ pub struct SignatureInput { pub w: Felt252, } -#[cfg(feature = "std")] impl AirPrivateInput { pub fn to_serializable( &self, - trace_file: PathBuf, - memory_file: PathBuf, + trace_path: String, + memory_path: String, ) -> AirPrivateInputSerializable { AirPrivateInputSerializable { - trace_path: trace_file.as_path().canonicalize().unwrap_or(trace_file), - memory_path: memory_file.as_path().canonicalize().unwrap_or(memory_file), + trace_path, + memory_path, pedersen: self.0.get(HASH_BUILTIN_NAME).cloned().unwrap_or_default(), range_check: self .0 @@ -137,7 +132,6 @@ impl AirPrivateInput { } } -#[cfg(feature = "std")] impl AirPrivateInputSerializable { pub fn serialize_json(&self) -> Result { serde_json::to_string_pretty(&self) From a0f2eb7f0dc31afe15ccf206d019878acc05963e Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 12:27:49 -0300 Subject: [PATCH 44/84] Fix ecdsa private input --- vm/src/vm/runners/builtin_runner/signature.rs | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index 9dfcf03d37..d854afa05a 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -1,4 +1,5 @@ use crate::air_private_input::{PrivateInput, PrivateInputSignature, SignatureInput}; +use crate::math_utils::div_mod; use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*, rc::Rc}; use crate::types::errors::math_errors::MathError; @@ -19,16 +20,16 @@ use crate::{ }, }; use lazy_static::lazy_static; +use num_bigint::{BigInt, Sign}; use num_integer::div_ceil; +use num_traits::{Num, One}; use starknet_crypto::{verify, FieldElement, Signature}; -use starknet_types_core::felt::NonZeroFelt; lazy_static! { - static ref EC_ORDER: NonZeroFelt = Felt252::from_dec_str( - "3618502788666131213697322783095070105526743751716087489154079457884512865583" + static ref EC_ORDER: BigInt = BigInt::from_str_radix( + "3618502788666131213697322783095070105526743751716087489154079457884512865583", + 10 ) - .unwrap() - .try_into() .unwrap(); } @@ -256,9 +257,14 @@ impl SignatureBuiltinRunner { msg: *msg, signature_input: SignatureInput { r: Felt252::from_bytes_be(&signature.r.to_bytes_be()), - w: Felt252::from_bytes_be(&signature.r.to_bytes_be()) - .mod_inverse(&EC_ORDER) + w: Felt252::from( + &div_mod( + &BigInt::one(), + &BigInt::from_bytes_be(Sign::Plus, &signature.s.to_bytes_be()), + &EC_ORDER, + ) .unwrap_or_default(), + ), }, })) } From c92928fa5eb87731f657801b8f0e2d580a5c5335 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:06:31 -0300 Subject: [PATCH 45/84] Add no-std import --- vm/src/air_private_input.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 696fe0162c..9a0b8bc230 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -6,6 +6,7 @@ use crate::{ }, }; use serde::{Deserialize, Serialize}; +use crate::stdlib::prelude::String; use crate::Felt252; From 977059772bbe9c22a6bb6fabddea02971b7f96ba Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:07:52 -0300 Subject: [PATCH 46/84] Add Chaneglog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1f9aa3816..d74fa171ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Implement air_private_input [#1552](https://github.com/lambdaclass/cairo-vm/pull/1552) + #### [1.0.0-rc0] - 2024-1-5 * feat: Use `ProjectivePoint` from types-rs in ec_op builtin impl [#1532](https://github.com/lambdaclass/cairo-vm/pull/1532) From f2b19a1696d9b6f09d316ae9411e250654194d76 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:09:32 -0300 Subject: [PATCH 47/84] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 52c2dd4043..1e45ad3f50 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,8 @@ The cairo-vm-cli supports the following optional arguments: - `--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 `: 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. + For example, to obtain the air public inputs from a fibonacci program run, we can run : ```bash From ff44b021d680a4319a98283f0871e6ac32ca92dc Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:23:57 -0300 Subject: [PATCH 48/84] Add cli tests --- cairo-vm-cli/src/main.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index ba61037294..8facb04c34 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -267,6 +267,27 @@ mod tests { assert_matches!(run(args), Err(Error::Cli(_))); } + #[rstest] + #[case(["cairo-vm-cli", "../cairo_programs/fibonacci.json", "--air_private_input", "/dev/null", "--proof_mode", "--memory_file", "/dev/null"].as_slice())] + fn test_run_air_private_input_no_trace(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Err(Error::Cli(_))); + } + + #[rstest] + #[case(["cairo-vm-cli", "../cairo_programs/fibonacci.json", "--air_private_input", "/dev/null", "--proof_mode", "--trace_file", "/dev/null"].as_slice())] + fn test_run_air_private_input_no_memory(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Err(Error::Cli(_))); + } + + #[rstest] + #[case(["cairo-vm-cli", "../cairo_programs/fibonacci.json", "--air_private_input", "/dev/null", "--trace_file", "/dev/null", "--memory_file", "/dev/null"].as_slice())] + fn test_run_air_private_input_no_proof(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Err(Error::Cli(_))); + } + #[rstest] fn test_run_ok( #[values(None, From 912bda9de8689d195b63589efb102f7568c16a2d Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:26:41 -0300 Subject: [PATCH 49/84] Add case to cli test --- cairo-vm-cli/src/main.rs | 13 ++++++++----- vm/src/air_private_input.rs | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 8facb04c34..2004b4c931 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -310,16 +310,17 @@ mod tests { #[values(false, true)] print_output: bool, #[values(false, true)] entrypoint: bool, #[values(false, true)] air_public_input: bool, + #[values(false, true)] air_private_input: bool, ) { let mut args = vec!["cairo-vm-cli".to_string()]; if let Some(layout) = layout { args.extend_from_slice(&["--layout".to_string(), layout.to_string()]); } if air_public_input { - args.extend_from_slice(&[ - "--air_public_input".to_string(), - "air_input.pub".to_string(), - ]); + args.extend_from_slice(&["--air_public_input".to_string(), "/dev/null".to_string()]); + } + if air_private_input { + args.extend_from_slice(&["--air_private_input".to_string(), "/dev/null".to_string()]); } if proof_mode { trace_file = true; @@ -342,7 +343,9 @@ mod tests { } args.push("../cairo_programs/proof_programs/fibonacci.json".to_string()); - if air_public_input && !proof_mode { + if air_public_input && !proof_mode + || air_private_input && !proof_mode && !trace_file && !memory_file + { assert_matches!(run(args.into_iter()), Err(_)); } else { assert_matches!(run(args.into_iter()), Ok(_)); diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 9a0b8bc230..0b06420658 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,3 +1,4 @@ +use crate::stdlib::prelude::String; use crate::{ stdlib::collections::HashMap, vm::runners::builtin_runner::{ @@ -6,7 +7,6 @@ use crate::{ }, }; use serde::{Deserialize, Serialize}; -use crate::stdlib::prelude::String; use crate::Felt252; From ebc249e95ec9b1e3c093b44a478c3ad405d1af9c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:29:04 -0300 Subject: [PATCH 50/84] Fix conditional --- cairo-vm-cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 2004b4c931..8b4aa5bb8b 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -344,7 +344,7 @@ mod tests { args.push("../cairo_programs/proof_programs/fibonacci.json".to_string()); if air_public_input && !proof_mode - || air_private_input && !proof_mode && !trace_file && !memory_file + || (air_private_input && (!proof_mode || !trace_file || !memory_file)) { assert_matches!(run(args.into_iter()), Err(_)); } else { From 1c77bd429b6bf6d8c85ce63fd88b5e2f5b74aa95 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:35:26 -0300 Subject: [PATCH 51/84] Add no-std import --- vm/src/air_private_input.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 0b06420658..2b02d03dfc 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,6 +1,5 @@ -use crate::stdlib::prelude::String; use crate::{ - stdlib::collections::HashMap, + stdlib::{collections::HashMap, prelude::{String, Vec}}, vm::runners::builtin_runner::{ BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME, POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, From 4bcc3e38d4185f179ba41c190c5173ccbb3767b3 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:39:37 -0300 Subject: [PATCH 52/84] fmt --- vm/src/air_private_input.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 2b02d03dfc..4ea30d0d44 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -1,5 +1,8 @@ use crate::{ - stdlib::{collections::HashMap, prelude::{String, Vec}}, + stdlib::{ + collections::HashMap, + prelude::{String, Vec}, + }, vm::runners::builtin_runner::{ BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME, POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, From 37434aa8066b52545d7c44ad4147a138194bd368 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:49:24 -0300 Subject: [PATCH 53/84] fix --- cairo-vm-cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 8b4aa5bb8b..a8179d6d44 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -121,7 +121,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { return Err(Error::Cli(error)); } - if args.air_public_input.is_some() && !args.proof_mode { + if args.air_private_input.is_some() && !args.proof_mode { let error = Args::command().error( clap::error::ErrorKind::ArgumentConflict, "--air_private_input can only be used in proof_mode.", From 11f6b2a108945838f9445ebbaebf35d97b79f18e Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:53:00 -0300 Subject: [PATCH 54/84] Add air_private_input flag --- cairo1-run/src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 6ef3932dff..0934d417af 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -87,6 +87,8 @@ struct Args { proof_mode: bool, #[clap(long = "air_public_input", value_parser)] air_public_input: Option, + #[clap(long = "air_private_input", value_parser)] + air_private_input: Option, // Arguments should be spaced, with array elements placed between brackets // For example " --args '1 2 [1 2 3]'" will yield 3 arguments, with the last one being an array of 3 elements #[clap(long = "args", default_value = "", value_parser=process_args)] @@ -239,6 +241,30 @@ fn run(args: impl Iterator) -> Result, Erro return Err(Error::Cli(error)); } + if args.air_private_input.is_some() && !args.proof_mode { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--air_private_input can only be used in proof_mode.", + ); + return Err(Error::Cli(error)); + } + + if args.air_private_input.is_some() && args.trace_file.is_none() { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--trace_file must be set when --air_private_input is set.", + ); + return Err(Error::Cli(error)); + } + + if args.air_private_input.is_some() && args.memory_file.is_none() { + let error = Args::command().error( + clap::error::ErrorKind::ArgumentConflict, + "--memory_file must be set when --air_private_input is set.", + ); + return Err(Error::Cli(error)); + } + let compiler_config = CompilerConfig { replace_ids: true, ..CompilerConfig::default() @@ -473,6 +499,35 @@ fn run(args: impl Iterator) -> Result, Erro std::fs::write(file_path, json)?; } + if let Some(file_path) = args.air_private_input { + // Get absolute paths of trace_file & memory_file + let trace_path = args + .trace_file + .clone() + .unwrap() + .as_path() + .canonicalize() + .unwrap_or(args.trace_file.clone().unwrap()) + .to_string_lossy() + .to_string(); + let memory_path = args + .memory_file + .clone() + .unwrap() + .as_path() + .canonicalize() + .unwrap_or(args.memory_file.clone().unwrap()) + .to_string_lossy() + .to_string(); + + let json = runner + .get_air_private_input(&vm) + .to_serializable(trace_path, memory_path) + .serialize_json() + .map_err(PublicInputError::Serde)?; + std::fs::write(file_path, json)?; + } + if let Some(trace_path) = args.trace_file { let relocated_trace = runner .relocated_trace From b6b18fe6c7e024f7d65837d7815026703ff2ada0 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:56:20 -0300 Subject: [PATCH 55/84] Remove unwraps --- cairo-vm-cli/src/main.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index a8179d6d44..a24ef7bb92 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -202,24 +202,20 @@ fn run(args: impl Iterator) -> Result<(), Error> { std::fs::write(file_path, json)?; } - if let Some(file_path) = args.air_private_input { + if let (Some(file_path), Some(ref trace_file), Some(ref memory_file)) = + (args.air_private_input, args.trace_file, args.memory_file) + { // Get absolute paths of trace_file & memory_file - let trace_path = args - .trace_file - .clone() - .unwrap() + let trace_path = trace_file .as_path() .canonicalize() - .unwrap_or(args.trace_file.unwrap()) + .unwrap_or(trace_file.clone()) .to_string_lossy() .to_string(); - let memory_path = args - .memory_file - .clone() - .unwrap() + let memory_path = memory_file .as_path() .canonicalize() - .unwrap_or(args.memory_file.unwrap()) + .unwrap_or(memory_file.clone()) .to_string_lossy() .to_string(); From 731d5f82d66fedb6801ab655a0a9f139b1e70335 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 13:58:44 -0300 Subject: [PATCH 56/84] fix --- cairo1-run/src/main.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 0934d417af..12fa9e35c7 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -499,24 +499,20 @@ fn run(args: impl Iterator) -> Result, Erro std::fs::write(file_path, json)?; } - if let Some(file_path) = args.air_private_input { + if let (Some(file_path), Some(trace_file), Some(memory_file)) = + (args.air_private_input, args.trace_file.clone(), args.memory_file.clone()) + { // Get absolute paths of trace_file & memory_file - let trace_path = args - .trace_file - .clone() - .unwrap() + let trace_path = trace_file .as_path() .canonicalize() - .unwrap_or(args.trace_file.clone().unwrap()) + .unwrap_or(trace_file.clone()) .to_string_lossy() .to_string(); - let memory_path = args - .memory_file - .clone() - .unwrap() + let memory_path = memory_file .as_path() .canonicalize() - .unwrap_or(args.memory_file.clone().unwrap()) + .unwrap_or(memory_file.clone()) .to_string_lossy() .to_string(); From 1a9566ddce71ee539ee0494b03785df00431de4f Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 14:00:12 -0300 Subject: [PATCH 57/84] Add new flag to tests --- cairo1-run/src/main.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 12fa9e35c7..ddede13534 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -885,7 +885,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_fibonacci_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(89)]); @@ -893,7 +893,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_factorial_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3628800)]); @@ -901,7 +901,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_array_get_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3)]); @@ -909,7 +909,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_enum_flow_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(300)]); @@ -917,7 +917,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_enum_match_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(10), MaybeRelocatable::from(felt_str("3618502788666131213697322783095070105623107215331596699973092056135872020471"))]); @@ -925,7 +925,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_hello_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1), MaybeRelocatable::from(1234)]); @@ -933,7 +933,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_ops_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(6)]); @@ -941,7 +941,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_print_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![]); @@ -949,7 +949,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_recursion_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1154076154663935037074198317650845438095734251249125412074882362667803016453"))]); @@ -957,7 +957,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_sample_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("5050"))]); @@ -965,7 +965,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_poseidon_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1099385018355113290651252669115094675591288647745213771718157553170111442461"))]); @@ -973,7 +973,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_poseidon_pedersen_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1036257840396636296853154602823055519264738423488122322497453114874087006398"))]); @@ -981,7 +981,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_pedersen_example_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1089549915800264549621536909767699778745926517555586332772759280702396009108"))]); @@ -989,7 +989,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_simple_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); @@ -997,7 +997,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_simple_struct_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(100)]); @@ -1005,7 +1005,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_dictionaries(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1024)]); @@ -1013,7 +1013,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "0"].as_slice())] fn test_run_branching_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); @@ -1021,7 +1021,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "96"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "96"].as_slice())] fn test_run_branching_not_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(0)]); @@ -1045,7 +1045,7 @@ mod tests { #[rstest] #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] fn test_array_input_sum(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(12)]); From 9ab5e8e866434318857b5ae2d6090e72f6cad133 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 14:08:23 -0300 Subject: [PATCH 58/84] Add Cli argument list to README --- cairo1-run/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cairo1-run/README.md b/cairo1-run/README.md index dfa835af8c..a980ed1f5a 100644 --- a/cairo1-run/README.md +++ b/cairo1-run/README.md @@ -46,3 +46,21 @@ To execute all the cairo 1 programs inside `../cairo_programs/cairo-1-programs/` ```bash make run ``` + +## CLI argument list + +The cairo1-run cli supports the following optional arguments: + +* `--layout `: Sets the layout for the cairo_run. This will limit the available builtins. The deafult layout is `plain`, which has no builtins. For general purpose, the `all_cairo` layout contains all currently available builtins. + +* `--args `: Receives the arguments to be passed to the program's main function. Receives whitespace-separated values which can be numbers or arrays, with arrays consisting of whitespace-separated numbers wrapped between brackets + +* `--trace_file `: Receives the name of a file and outputs the relocated trace into it + +* `--memory_file `: Receives the name of a file and outputs the relocated memory into it + +* `--proof_mode`: Runs the program in proof_mode + +* `--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 `: 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. From e621d31085cb9de0008fa78e55a861234957cf63 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 14:12:07 -0300 Subject: [PATCH 59/84] Add Chaneglog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f4b73ddaf..06669cc9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Add `air_private_input` flag to `cairo1-run` [#1559] (https://github.com/lambdaclass/cairo-vm/pull/1559) + * feat: Implement air_private_input [#1552](https://github.com/lambdaclass/cairo-vm/pull/1552) * feat: Add `air_public_input` flag to `cairo1-run` [#1539] (https://github.com/lambdaclass/cairo-vm/pull/1539) From 8c4a18d0e2e47c1594d3a1c2cc2467bb2da639b6 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 15:35:20 -0300 Subject: [PATCH 60/84] Fix changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06669cc9b7..00bfc1efae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * feat: Implement air_private_input [#1552](https://github.com/lambdaclass/cairo-vm/pull/1552) +* feat: Add `args` flag to `cairo1-run` [#15551] (https://github.com/lambdaclass/cairo-vm/pull/15551) + * feat: Add `air_public_input` flag to `cairo1-run` [#1539] (https://github.com/lambdaclass/cairo-vm/pull/1539) * feat: Add `proof_mode` flag to `cairo1-run` [#1537] (https://github.com/lambdaclass/cairo-vm/pull/1537) From cb2ec418b9b5c44555e727a809e7e587b0ebe605 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:08:38 -0300 Subject: [PATCH 61/84] Add tests so coverage doesnt sink --- cairo1-run/src/main.rs | 8 ++-- .../proof_programs/common_signature.cairo | 1 + vm/src/vm/runners/builtin_runner/bitwise.rs | 3 +- vm/src/vm/runners/builtin_runner/ec_op.rs | 26 +++++++++++ vm/src/vm/runners/builtin_runner/hash.rs | 44 +++++++++++++++++++ vm/src/vm/runners/builtin_runner/keccak.rs | 32 ++++++++++++++ vm/src/vm/runners/builtin_runner/output.rs | 9 ++++ vm/src/vm/runners/builtin_runner/poseidon.rs | 38 ++++++++++++++++ .../vm/runners/builtin_runner/range_check.rs | 25 +++++++++++ .../runners/builtin_runner/segment_arena.rs | 9 ++++ vm/src/vm/runners/cairo_runner.rs | 42 ++++++++++++++++++ 11 files changed, 233 insertions(+), 4 deletions(-) create mode 120000 cairo_programs/proof_programs/common_signature.cairo diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index ddede13534..044a5b8f84 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -499,9 +499,11 @@ fn run(args: impl Iterator) -> Result, Erro std::fs::write(file_path, json)?; } - if let (Some(file_path), Some(trace_file), Some(memory_file)) = - (args.air_private_input, args.trace_file.clone(), args.memory_file.clone()) - { + if let (Some(file_path), Some(trace_file), Some(memory_file)) = ( + args.air_private_input, + args.trace_file.clone(), + args.memory_file.clone(), + ) { // Get absolute paths of trace_file & memory_file let trace_path = trace_file .as_path() diff --git a/cairo_programs/proof_programs/common_signature.cairo b/cairo_programs/proof_programs/common_signature.cairo new file mode 120000 index 0000000000..f2767a1b03 --- /dev/null +++ b/cairo_programs/proof_programs/common_signature.cairo @@ -0,0 +1 @@ +/Users/federica/cairo-vm/cairo_programs/common_signature.cairo \ No newline at end of file diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index ef95b2da1c..825aab3942 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -623,7 +623,8 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_air_private_input() { - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin: BuiltinRunner = + BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); let memory = memory![ ((0, 0), 0), diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 3e985f259a..c824f0153d 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -990,4 +990,30 @@ mod tests { Ok(_) => panic!("Expected run to fail"), } } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = + EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![PrivateInput::EcOp(PrivateInputEcOp { + index: 0, + p_x: 0.into(), + p_y: 1.into(), + m: 4.into(), + q_x: 2.into(), + q_y: 3.into(), + })]) + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index ed828c71d4..7aed7237e5 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -572,4 +572,48 @@ mod tests { BuiltinAdditionalData::Hash(verified_addresses) ) } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = HashBuiltinRunner::new(None, true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7), + ((0, 8), 8), + ((0, 9), 9), + ((0, 10), 10), + ((0, 11), 11), + ((0, 12), 12), + ((0, 13), 13), + ((0, 14), 14) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::Pair(PrivateInputPair { + index: 0, + x: 0.into(), + y: 1.into() + }), + PrivateInput::Pair(PrivateInputPair { + index: 1, + x: 5.into(), + y: 6.into() + }), + PrivateInput::Pair(PrivateInputPair { + index: 2, + x: 10.into(), + y: 11.into() + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index c7a00292fe..40e59f8c9d 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -726,4 +726,36 @@ mod tests { let output_bytes = KeccakBuiltinRunner::keccak_f(input_bytes); assert_eq!(output_bytes, Ok(expected_output_bytes.to_vec())); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = + KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![PrivateInput::KeccakState(PrivateInputKeccakState { + index: 0, + input_s0: 1.into(), + input_s1: 1.into(), + input_s2: 2.into(), + input_s3: 3.into(), + input_s4: 4.into(), + input_s5: 5.into(), + input_s6: 6.into(), + input_s7: 7.into() + }),]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/output.rs b/vm/src/vm/runners/builtin_runner/output.rs index 75fce41506..1e3bfe3ad4 100644 --- a/vm/src/vm/runners/builtin_runner/output.rs +++ b/vm/src/vm/runners/builtin_runner/output.rs @@ -450,4 +450,13 @@ mod tests { }) ) } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = OutputBuiltinRunner::new(true).into(); + + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + assert!(builtin.air_private_input(&memory).is_empty()); + } } diff --git a/vm/src/vm/runners/builtin_runner/poseidon.rs b/vm/src/vm/runners/builtin_runner/poseidon.rs index 6cab5980a8..a668ba324b 100644 --- a/vm/src/vm/runners/builtin_runner/poseidon.rs +++ b/vm/src/vm/runners/builtin_runner/poseidon.rs @@ -437,4 +437,42 @@ mod tests { Ok(None) ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = PoseidonBuiltinRunner::new(None, true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7), + ((0, 8), 8), + ((0, 9), 9), + ((0, 10), 10), + ((0, 11), 11) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::PoseidonState(PrivateInputPoseidonState { + index: 0, + input_s0: 0.into(), + input_s1: 1.into(), + input_s2: 2.into(), + }), + PrivateInput::PoseidonState(PrivateInputPoseidonState { + index: 1, + input_s0: 6.into(), + input_s1: 7.into(), + input_s2: 8.into(), + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index 9ea9d9c76a..70ffce1abf 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -603,4 +603,29 @@ mod tests { vm.segments.segment_used_sizes = Some(vec![1]); assert_eq!(builtin_runner.get_used_perm_range_check_units(&vm), Ok(8)); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(None, 4, true).into(); + + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::Value(PrivateInputValue { + index: 0, + value: 0.into(), + }), + PrivateInput::Value(PrivateInputValue { + index: 1, + value: 1.into(), + }), + PrivateInput::Value(PrivateInputValue { + index: 2, + value: 2.into(), + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/segment_arena.rs b/vm/src/vm/runners/builtin_runner/segment_arena.rs index fd8bd75599..82c9e72aea 100644 --- a/vm/src/vm/runners/builtin_runner/segment_arena.rs +++ b/vm/src/vm/runners/builtin_runner/segment_arena.rs @@ -496,4 +496,13 @@ mod tests { let builtin: BuiltinRunner = SegmentArenaBuiltinRunner::new(true).into(); assert_eq!(builtin.instances_per_component(), 1) } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = SegmentArenaBuiltinRunner::new(true).into(); + + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + assert!(builtin.air_private_input(&memory).is_empty()); + } } diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 2c4c6c4c2a..11cc89dc3c 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1522,6 +1522,7 @@ impl MulAssign for ExecutionResources { #[cfg(test)] mod tests { use super::*; + use crate::air_private_input::{PrivateInput, PrivateInputSignature, SignatureInput}; use crate::cairo_run::{cairo_run, CairoRunConfig}; use crate::stdlib::collections::{HashMap, HashSet}; use crate::vm::runners::builtin_runner::{ @@ -5447,4 +5448,45 @@ mod tests { // segment sizes 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( + program_content, + &CairoRunConfig { + proof_mode: true, + layout: "all_cairo", + ..Default::default() + }, + &mut BuiltinHintProcessor::new_empty(), + ) + .unwrap(); + let air_private_input = runner.get_air_private_input(&vm); + assert!(air_private_input.0[HASH_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[RANGE_CHECK_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[BITWISE_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[EC_OP_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[KECCAK_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[POSEIDON_BUILTIN_NAME].is_empty()); + assert_eq!( + air_private_input.0[SIGNATURE_BUILTIN_NAME], + vec![PrivateInput::Signature(PrivateInputSignature { + index: 0, + pubkey: felt_hex!( + "0x3d60886c2353d93ec2862e91e23036cd9999a534481166e5a616a983070434d" + ), + msg: felt_hex!("0xa9e"), + signature_input: SignatureInput { + r: felt_hex!( + "0x6d2e2e00dfceffd6a375db04764da249a5a1534c7584738dfe01cb3944a33ee" + ), + w: felt_hex!( + "0x396362a34ff391372fca63f691e27753ce8f0c2271a614cbd240e1dc1596b28" + ) + } + })] + ); + } } From 602d76ab9e415ad576187313645968d5e495198e Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:12:32 -0300 Subject: [PATCH 62/84] Fix test --- vm/src/vm/runners/builtin_runner/hash.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index 7aed7237e5..f8e8fbe333 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -588,12 +588,7 @@ mod tests { ((0, 6), 6), ((0, 7), 7), ((0, 8), 8), - ((0, 9), 9), - ((0, 10), 10), - ((0, 11), 11), - ((0, 12), 12), - ((0, 13), 13), - ((0, 14), 14) + ((0, 9), 9) ]; assert_eq!( builtin.air_private_input(&memory), @@ -605,13 +600,13 @@ mod tests { }), PrivateInput::Pair(PrivateInputPair { index: 1, - x: 5.into(), - y: 6.into() + x: 3.into(), + y: 4.into() }), PrivateInput::Pair(PrivateInputPair { index: 2, - x: 10.into(), - y: 11.into() + x: 6.into(), + y: 7.into() }), ]), ); From 0afdec81ae558992e7c40d90ba4809c90b4dcca6 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:14:41 -0300 Subject: [PATCH 63/84] Fix test --- vm/src/vm/runners/builtin_runner/keccak.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index 40e59f8c9d..3eed4a61b6 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -747,7 +747,7 @@ mod tests { builtin.air_private_input(&memory), (vec![PrivateInput::KeccakState(PrivateInputKeccakState { index: 0, - input_s0: 1.into(), + input_s0: 0.into(), input_s1: 1.into(), input_s2: 2.into(), input_s3: 3.into(), From 873e2764a94ceb3a2642a34cd72873bfe78c5dae Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:33:05 -0300 Subject: [PATCH 64/84] Fix test --- vm/src/vm/runners/builtin_runner/range_check.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index 70ffce1abf..ba07f6f253 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -609,7 +609,7 @@ mod tests { fn get_air_private_input() { let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(None, 4, true).into(); - let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2)]; assert_eq!( builtin.air_private_input(&memory), (vec![ From 783b3338135861501e2b00d47a7143155edac483 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:08:38 -0300 Subject: [PATCH 65/84] Add tests so coverage doesnt sink --- .../proof_programs/common_signature.cairo | 1 + vm/src/vm/runners/builtin_runner/bitwise.rs | 3 +- vm/src/vm/runners/builtin_runner/ec_op.rs | 26 +++++++++++ vm/src/vm/runners/builtin_runner/hash.rs | 44 +++++++++++++++++++ vm/src/vm/runners/builtin_runner/keccak.rs | 32 ++++++++++++++ vm/src/vm/runners/builtin_runner/output.rs | 9 ++++ vm/src/vm/runners/builtin_runner/poseidon.rs | 38 ++++++++++++++++ .../vm/runners/builtin_runner/range_check.rs | 25 +++++++++++ .../runners/builtin_runner/segment_arena.rs | 9 ++++ vm/src/vm/runners/cairo_runner.rs | 42 ++++++++++++++++++ 10 files changed, 228 insertions(+), 1 deletion(-) create mode 120000 cairo_programs/proof_programs/common_signature.cairo diff --git a/cairo_programs/proof_programs/common_signature.cairo b/cairo_programs/proof_programs/common_signature.cairo new file mode 120000 index 0000000000..f2767a1b03 --- /dev/null +++ b/cairo_programs/proof_programs/common_signature.cairo @@ -0,0 +1 @@ +/Users/federica/cairo-vm/cairo_programs/common_signature.cairo \ No newline at end of file diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index ef95b2da1c..825aab3942 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -623,7 +623,8 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_air_private_input() { - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin: BuiltinRunner = + BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); let memory = memory![ ((0, 0), 0), diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 3e985f259a..c824f0153d 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -990,4 +990,30 @@ mod tests { Ok(_) => panic!("Expected run to fail"), } } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = + EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![PrivateInput::EcOp(PrivateInputEcOp { + index: 0, + p_x: 0.into(), + p_y: 1.into(), + m: 4.into(), + q_x: 2.into(), + q_y: 3.into(), + })]) + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index ed828c71d4..7aed7237e5 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -572,4 +572,48 @@ mod tests { BuiltinAdditionalData::Hash(verified_addresses) ) } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = HashBuiltinRunner::new(None, true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7), + ((0, 8), 8), + ((0, 9), 9), + ((0, 10), 10), + ((0, 11), 11), + ((0, 12), 12), + ((0, 13), 13), + ((0, 14), 14) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::Pair(PrivateInputPair { + index: 0, + x: 0.into(), + y: 1.into() + }), + PrivateInput::Pair(PrivateInputPair { + index: 1, + x: 5.into(), + y: 6.into() + }), + PrivateInput::Pair(PrivateInputPair { + index: 2, + x: 10.into(), + y: 11.into() + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index c7a00292fe..40e59f8c9d 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -726,4 +726,36 @@ mod tests { let output_bytes = KeccakBuiltinRunner::keccak_f(input_bytes); assert_eq!(output_bytes, Ok(expected_output_bytes.to_vec())); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = + KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![PrivateInput::KeccakState(PrivateInputKeccakState { + index: 0, + input_s0: 1.into(), + input_s1: 1.into(), + input_s2: 2.into(), + input_s3: 3.into(), + input_s4: 4.into(), + input_s5: 5.into(), + input_s6: 6.into(), + input_s7: 7.into() + }),]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/output.rs b/vm/src/vm/runners/builtin_runner/output.rs index 75fce41506..1e3bfe3ad4 100644 --- a/vm/src/vm/runners/builtin_runner/output.rs +++ b/vm/src/vm/runners/builtin_runner/output.rs @@ -450,4 +450,13 @@ mod tests { }) ) } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = OutputBuiltinRunner::new(true).into(); + + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + assert!(builtin.air_private_input(&memory).is_empty()); + } } diff --git a/vm/src/vm/runners/builtin_runner/poseidon.rs b/vm/src/vm/runners/builtin_runner/poseidon.rs index 6cab5980a8..a668ba324b 100644 --- a/vm/src/vm/runners/builtin_runner/poseidon.rs +++ b/vm/src/vm/runners/builtin_runner/poseidon.rs @@ -437,4 +437,42 @@ mod tests { Ok(None) ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = PoseidonBuiltinRunner::new(None, true).into(); + + let memory = memory![ + ((0, 0), 0), + ((0, 1), 1), + ((0, 2), 2), + ((0, 3), 3), + ((0, 4), 4), + ((0, 5), 5), + ((0, 6), 6), + ((0, 7), 7), + ((0, 8), 8), + ((0, 9), 9), + ((0, 10), 10), + ((0, 11), 11) + ]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::PoseidonState(PrivateInputPoseidonState { + index: 0, + input_s0: 0.into(), + input_s1: 1.into(), + input_s2: 2.into(), + }), + PrivateInput::PoseidonState(PrivateInputPoseidonState { + index: 1, + input_s0: 6.into(), + input_s1: 7.into(), + input_s2: 8.into(), + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index 9ea9d9c76a..70ffce1abf 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -603,4 +603,29 @@ mod tests { vm.segments.segment_used_sizes = Some(vec![1]); assert_eq!(builtin_runner.get_used_perm_range_check_units(&vm), Ok(8)); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(None, 4, true).into(); + + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + assert_eq!( + builtin.air_private_input(&memory), + (vec![ + PrivateInput::Value(PrivateInputValue { + index: 0, + value: 0.into(), + }), + PrivateInput::Value(PrivateInputValue { + index: 1, + value: 1.into(), + }), + PrivateInput::Value(PrivateInputValue { + index: 2, + value: 2.into(), + }), + ]), + ); + } } diff --git a/vm/src/vm/runners/builtin_runner/segment_arena.rs b/vm/src/vm/runners/builtin_runner/segment_arena.rs index fd8bd75599..82c9e72aea 100644 --- a/vm/src/vm/runners/builtin_runner/segment_arena.rs +++ b/vm/src/vm/runners/builtin_runner/segment_arena.rs @@ -496,4 +496,13 @@ mod tests { let builtin: BuiltinRunner = SegmentArenaBuiltinRunner::new(true).into(); assert_eq!(builtin.instances_per_component(), 1) } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_air_private_input() { + let builtin: BuiltinRunner = SegmentArenaBuiltinRunner::new(true).into(); + + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + assert!(builtin.air_private_input(&memory).is_empty()); + } } diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 2c4c6c4c2a..11cc89dc3c 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1522,6 +1522,7 @@ impl MulAssign for ExecutionResources { #[cfg(test)] mod tests { use super::*; + use crate::air_private_input::{PrivateInput, PrivateInputSignature, SignatureInput}; use crate::cairo_run::{cairo_run, CairoRunConfig}; use crate::stdlib::collections::{HashMap, HashSet}; use crate::vm::runners::builtin_runner::{ @@ -5447,4 +5448,45 @@ mod tests { // segment sizes 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( + program_content, + &CairoRunConfig { + proof_mode: true, + layout: "all_cairo", + ..Default::default() + }, + &mut BuiltinHintProcessor::new_empty(), + ) + .unwrap(); + let air_private_input = runner.get_air_private_input(&vm); + assert!(air_private_input.0[HASH_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[RANGE_CHECK_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[BITWISE_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[EC_OP_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[KECCAK_BUILTIN_NAME].is_empty()); + assert!(air_private_input.0[POSEIDON_BUILTIN_NAME].is_empty()); + assert_eq!( + air_private_input.0[SIGNATURE_BUILTIN_NAME], + vec![PrivateInput::Signature(PrivateInputSignature { + index: 0, + pubkey: felt_hex!( + "0x3d60886c2353d93ec2862e91e23036cd9999a534481166e5a616a983070434d" + ), + msg: felt_hex!("0xa9e"), + signature_input: SignatureInput { + r: felt_hex!( + "0x6d2e2e00dfceffd6a375db04764da249a5a1534c7584738dfe01cb3944a33ee" + ), + w: felt_hex!( + "0x396362a34ff391372fca63f691e27753ce8f0c2271a614cbd240e1dc1596b28" + ) + } + })] + ); + } } From 2a5bed1995a544595f5fef50df6508536135353c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:12:32 -0300 Subject: [PATCH 66/84] Fix test --- vm/src/vm/runners/builtin_runner/hash.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index 7aed7237e5..f8e8fbe333 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -588,12 +588,7 @@ mod tests { ((0, 6), 6), ((0, 7), 7), ((0, 8), 8), - ((0, 9), 9), - ((0, 10), 10), - ((0, 11), 11), - ((0, 12), 12), - ((0, 13), 13), - ((0, 14), 14) + ((0, 9), 9) ]; assert_eq!( builtin.air_private_input(&memory), @@ -605,13 +600,13 @@ mod tests { }), PrivateInput::Pair(PrivateInputPair { index: 1, - x: 5.into(), - y: 6.into() + x: 3.into(), + y: 4.into() }), PrivateInput::Pair(PrivateInputPair { index: 2, - x: 10.into(), - y: 11.into() + x: 6.into(), + y: 7.into() }), ]), ); From 95ec39f6bc6639084051503e739fd45ec9788723 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:14:41 -0300 Subject: [PATCH 67/84] Fix test --- vm/src/vm/runners/builtin_runner/keccak.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index 40e59f8c9d..3eed4a61b6 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -747,7 +747,7 @@ mod tests { builtin.air_private_input(&memory), (vec![PrivateInput::KeccakState(PrivateInputKeccakState { index: 0, - input_s0: 1.into(), + input_s0: 0.into(), input_s1: 1.into(), input_s2: 2.into(), input_s3: 3.into(), From 614938f037bcd052b33d0c7a1279da66943c2500 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 16:33:05 -0300 Subject: [PATCH 68/84] Fix test --- vm/src/vm/runners/builtin_runner/range_check.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index 70ffce1abf..ba07f6f253 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -609,7 +609,7 @@ mod tests { fn get_air_private_input() { let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(None, 4, true).into(); - let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]; + let memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 2)]; assert_eq!( builtin.air_private_input(&memory), (vec![ From fe33a2b894c83cf5c14b8cbb15b7b15b4642f374 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Thu, 11 Jan 2024 17:07:01 -0300 Subject: [PATCH 69/84] Fix symlink --- cairo_programs/proof_programs/common_signature.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo_programs/proof_programs/common_signature.cairo b/cairo_programs/proof_programs/common_signature.cairo index f2767a1b03..46ced35d4a 120000 --- a/cairo_programs/proof_programs/common_signature.cairo +++ b/cairo_programs/proof_programs/common_signature.cairo @@ -1 +1 @@ -/Users/federica/cairo-vm/cairo_programs/common_signature.cairo \ No newline at end of file +../common_signature.cairo From 4a71f3c832267b6213d4bf5443cfaa173bc802bc Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 11 Jan 2024 17:23:57 -0300 Subject: [PATCH 70/84] Remove broken file --- cairo_programs/proof_programs/common_signature.cairo | 1 - 1 file changed, 1 deletion(-) delete mode 120000 cairo_programs/proof_programs/common_signature.cairo diff --git a/cairo_programs/proof_programs/common_signature.cairo b/cairo_programs/proof_programs/common_signature.cairo deleted file mode 120000 index 46ced35d4a..0000000000 --- a/cairo_programs/proof_programs/common_signature.cairo +++ /dev/null @@ -1 +0,0 @@ -../common_signature.cairo From 1674f734c722a2684240932168bf72ee76969a8a Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 15:42:24 -0300 Subject: [PATCH 71/84] Rename enum --- cairo1-run/src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 044a5b8f84..1238f273b9 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -96,13 +96,13 @@ struct Args { } #[derive(Debug, Clone)] -enum FuncArg { +enum CairoArg { Array(Vec), Single(Felt252), } #[derive(Debug, Clone, Default)] -struct FuncArgs(Vec); +struct FuncArgs(Vec); fn process_args(value: &str) -> Result { if value.is_empty() { @@ -130,10 +130,10 @@ fn process_args(value: &str) -> Result { } } // Finalize array - args.push(FuncArg::Array(array_arg)) + args.push(CairoArg::Array(array_arg)) } else { // Single argument - args.push(FuncArg::Single(Felt252::from_dec_str(value).unwrap())) + args.push(CairoArg::Single(Felt252::from_dec_str(value).unwrap())) } } Ok(FuncArgs(args)) @@ -668,7 +668,7 @@ fn create_entry_code( func: &Function, initial_gas: usize, proof_mode: bool, - args: &Vec, + args: &Vec, ) -> Result<(Vec, Vec), Error> { let mut ctx = casm! {}; // The builtins in the formatting expected by the runner. @@ -678,7 +678,7 @@ fn create_entry_code( let mut array_args_data = vec![]; let mut ap_offset: i16 = 0; for arg in args { - let FuncArg::Array(values) = arg else { continue }; + let CairoArg::Array(values) = arg else { continue }; array_args_data.push(ap_offset); casm_extend! {ctx, %{ memory[ap + 0] = segments.add() %} @@ -744,8 +744,8 @@ fn create_entry_code( casm_extend! {ctx, [ap + 0] = [ap + offset] + 3, ap++; } - } else if let Some(FuncArg::Array(_)) = arg_iter.peek() { - let values = extract_matches!(arg_iter.next().unwrap(), FuncArg::Array); + } else if let Some(CairoArg::Array(_)) = arg_iter.peek() { + let values = extract_matches!(arg_iter.next().unwrap(), CairoArg::Array); let offset = -ap_offset + array_args_data_iter.next().unwrap(); expected_arguments_size += 1; casm_extend! {ctx, @@ -757,7 +757,7 @@ fn create_entry_code( expected_arguments_size += arg_size as usize; for _ in 0..arg_size { if let Some(value) = arg_iter.next() { - let value = extract_matches!(value, FuncArg::Single); + let value = extract_matches!(value, CairoArg::Single); casm_extend! {ctx, [ap + 0] = (value.to_bigint()), ap++; } From a6e52f0017556f1223c24e04e37afd4a7a6a2bdc Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 15:59:13 -0300 Subject: [PATCH 72/84] Revert "Rename enum" This reverts commit 1674f734c722a2684240932168bf72ee76969a8a. --- cairo1-run/src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 1238f273b9..044a5b8f84 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -96,13 +96,13 @@ struct Args { } #[derive(Debug, Clone)] -enum CairoArg { +enum FuncArg { Array(Vec), Single(Felt252), } #[derive(Debug, Clone, Default)] -struct FuncArgs(Vec); +struct FuncArgs(Vec); fn process_args(value: &str) -> Result { if value.is_empty() { @@ -130,10 +130,10 @@ fn process_args(value: &str) -> Result { } } // Finalize array - args.push(CairoArg::Array(array_arg)) + args.push(FuncArg::Array(array_arg)) } else { // Single argument - args.push(CairoArg::Single(Felt252::from_dec_str(value).unwrap())) + args.push(FuncArg::Single(Felt252::from_dec_str(value).unwrap())) } } Ok(FuncArgs(args)) @@ -668,7 +668,7 @@ fn create_entry_code( func: &Function, initial_gas: usize, proof_mode: bool, - args: &Vec, + args: &Vec, ) -> Result<(Vec, Vec), Error> { let mut ctx = casm! {}; // The builtins in the formatting expected by the runner. @@ -678,7 +678,7 @@ fn create_entry_code( let mut array_args_data = vec![]; let mut ap_offset: i16 = 0; for arg in args { - let CairoArg::Array(values) = arg else { continue }; + let FuncArg::Array(values) = arg else { continue }; array_args_data.push(ap_offset); casm_extend! {ctx, %{ memory[ap + 0] = segments.add() %} @@ -744,8 +744,8 @@ fn create_entry_code( casm_extend! {ctx, [ap + 0] = [ap + offset] + 3, ap++; } - } else if let Some(CairoArg::Array(_)) = arg_iter.peek() { - let values = extract_matches!(arg_iter.next().unwrap(), CairoArg::Array); + } else if let Some(FuncArg::Array(_)) = arg_iter.peek() { + let values = extract_matches!(arg_iter.next().unwrap(), FuncArg::Array); let offset = -ap_offset + array_args_data_iter.next().unwrap(); expected_arguments_size += 1; casm_extend! {ctx, @@ -757,7 +757,7 @@ fn create_entry_code( expected_arguments_size += arg_size as usize; for _ in 0..arg_size { if let Some(value) = arg_iter.next() { - let value = extract_matches!(value, CairoArg::Single); + let value = extract_matches!(value, FuncArg::Single); casm_extend! {ctx, [ap + 0] = (value.to_bigint()), ap++; } From 88e0572576a8de5642d905478eebefb1c44823b9 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 17:38:41 -0300 Subject: [PATCH 73/84] Recursively process ouputs to string --- cairo1-run/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 044a5b8f84..8e518cd356 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -53,7 +53,9 @@ use cairo_vm::vm::runners::builtin_runner::{ BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, OUTPUT_BUILTIN_NAME, POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, }; +use cairo_vm::vm::runners::cairo_runner::CairoArg; use cairo_vm::vm::runners::cairo_runner::RunnerMode; +use cairo_vm::vm::vm_memory::memory::Memory; use cairo_vm::{ serde::deserialize_program::ReferenceManager, types::{program::Program, relocatable::MaybeRelocatable}, @@ -68,7 +70,9 @@ use itertools::{chain, Itertools}; use std::borrow::Cow; use std::io::BufWriter; use std::io::Write; +use std::iter::Peekable; use std::path::PathBuf; +use std::slice::Iter; use std::{collections::HashMap, io, path::Path}; use thiserror::Error; @@ -438,6 +442,43 @@ fn run(args: impl Iterator) -> Result, Erro } } + let mut output_string = String::new(); + let mut return_values_iter: Peekable> = return_values.iter().peekable(); + process_output(&mut return_values_iter, &mut output_string, &vm); + fn process_output( + iter: &mut Peekable>, + output_string: &mut String, + vm: &VirtualMachine, + ) { + while let Some(val) = iter.next() { + match val { + MaybeRelocatable::Int(x) => { + output_string.push_str(&format!("{}", x)); + } + MaybeRelocatable::RelocatableValue(x) => { + // Check if the next value is a relocatable of the same index + if let Some(MaybeRelocatable::RelocatableValue(y)) = iter.peek() { + // Check if the two relocatable values represent a valid array in memory + if x.segment_index == y.segment_index && x.offset <= y.offset { + // Fetch the y value from the iterator so we don't process it twice + iter.next(); + // Fetch array + output_string.push_str("["); + let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); + let mut array_iter: Peekable> = + array.iter().peekable(); + process_output(&mut array_iter, output_string, vm); + output_string.push_str("]"); + continue; + } + } + output_string.push_str(&format!(" {}", x)); + } + } + } + } + dbg!(output_string); + // Set stop pointers for builtins so we can obtain the air public input if args.air_public_input.is_some() { // Cairo 1 programs have other return values aside from the used builtin's final pointers, so we need to hand-pick them From b8932516bd9fb07376dc321076a7c1dd8f7db9fb Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 17:39:22 -0300 Subject: [PATCH 74/84] Update name --- cairo1-run/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 8e518cd356..a467b08b74 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -444,8 +444,8 @@ fn run(args: impl Iterator) -> Result, Erro let mut output_string = String::new(); let mut return_values_iter: Peekable> = return_values.iter().peekable(); - process_output(&mut return_values_iter, &mut output_string, &vm); - fn process_output( + serialize_output(&mut return_values_iter, &mut output_string, &vm); + fn serialize_output( iter: &mut Peekable>, output_string: &mut String, vm: &VirtualMachine, @@ -460,14 +460,14 @@ fn run(args: impl Iterator) -> Result, Erro if let Some(MaybeRelocatable::RelocatableValue(y)) = iter.peek() { // Check if the two relocatable values represent a valid array in memory if x.segment_index == y.segment_index && x.offset <= y.offset { - // Fetch the y value from the iterator so we don't process it twice + // Fetch the y value from the iterator so we don't serialize it twice iter.next(); // Fetch array output_string.push_str("["); let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); let mut array_iter: Peekable> = array.iter().peekable(); - process_output(&mut array_iter, output_string, vm); + serialize_output(&mut array_iter, output_string, vm); output_string.push_str("]"); continue; } From 3d4c24cd02b0b14c130f20c9422729187add89a6 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 17:45:33 -0300 Subject: [PATCH 75/84] Fix whitespace handling --- cairo1-run/src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index a467b08b74..1244e59d2e 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -453,6 +453,7 @@ fn run(args: impl Iterator) -> Result, Erro while let Some(val) = iter.next() { match val { MaybeRelocatable::Int(x) => { + maybe_add_whitespace(output_string); output_string.push_str(&format!("{}", x)); } MaybeRelocatable::RelocatableValue(x) => { @@ -472,11 +473,18 @@ fn run(args: impl Iterator) -> Result, Erro continue; } } + maybe_add_whitespace(output_string); output_string.push_str(&format!(" {}", x)); } } } } + + fn maybe_add_whitespace(string: &mut String) { + if !string.is_empty() && !string.ends_with("[") { + string.push(' '); + } + } dbg!(output_string); // Set stop pointers for builtins so we can obtain the air public input From a9ddadbef7909d48d67b29c01a5826b3154b25a0 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 17:50:05 -0300 Subject: [PATCH 76/84] Simplify logic --- cairo1-run/src/main.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 1244e59d2e..d0071f7f92 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -451,32 +451,26 @@ fn run(args: impl Iterator) -> Result, Erro vm: &VirtualMachine, ) { while let Some(val) = iter.next() { - match val { - MaybeRelocatable::Int(x) => { - maybe_add_whitespace(output_string); - output_string.push_str(&format!("{}", x)); - } - MaybeRelocatable::RelocatableValue(x) => { - // Check if the next value is a relocatable of the same index - if let Some(MaybeRelocatable::RelocatableValue(y)) = iter.peek() { - // Check if the two relocatable values represent a valid array in memory - if x.segment_index == y.segment_index && x.offset <= y.offset { - // Fetch the y value from the iterator so we don't serialize it twice - iter.next(); - // Fetch array - output_string.push_str("["); - let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); - let mut array_iter: Peekable> = - array.iter().peekable(); - serialize_output(&mut array_iter, output_string, vm); - output_string.push_str("]"); - continue; - } + if let MaybeRelocatable::RelocatableValue(x) = val { + // Check if the next value is a relocatable of the same index + if let Some(MaybeRelocatable::RelocatableValue(y)) = iter.peek() { + // Check if the two relocatable values represent a valid array in memory + if x.segment_index == y.segment_index && x.offset <= y.offset { + // Fetch the y value from the iterator so we don't serialize it twice + iter.next(); + // Fetch array + output_string.push_str("["); + let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); + let mut array_iter: Peekable> = + array.iter().peekable(); + serialize_output(&mut array_iter, output_string, vm); + output_string.push_str("]"); + continue; } - maybe_add_whitespace(output_string); - output_string.push_str(&format!(" {}", x)); } } + maybe_add_whitespace(output_string); + output_string.push_str(&val.to_string()); } } From 682bba9be986471821cb5af63fe9513b87c559ba Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 18:18:33 -0300 Subject: [PATCH 77/84] Add print_output flag --- cairo1-run/src/main.rs | 225 +++++++++++++++++++++-------------------- 1 file changed, 118 insertions(+), 107 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index d0071f7f92..e1ea748c24 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -97,6 +97,8 @@ struct Args { // For example " --args '1 2 [1 2 3]'" will yield 3 arguments, with the last one being an array of 3 elements #[clap(long = "args", default_value = "", value_parser=process_args)] args: FuncArgs, + #[clap(long = "print_output", value_parser)] + print_output: bool, } #[derive(Debug, Clone)] @@ -235,7 +237,7 @@ impl FileWriter { } } -fn run(args: impl Iterator) -> Result, Error> { +fn run(args: impl Iterator) -> Result, Error> { let args = Args::try_parse_from(args)?; if args.air_public_input.is_some() && !args.proof_mode { let error = Args::command().error( @@ -442,44 +444,11 @@ fn run(args: impl Iterator) -> Result, Erro } } - let mut output_string = String::new(); - let mut return_values_iter: Peekable> = return_values.iter().peekable(); - serialize_output(&mut return_values_iter, &mut output_string, &vm); - fn serialize_output( - iter: &mut Peekable>, - output_string: &mut String, - vm: &VirtualMachine, - ) { - while let Some(val) = iter.next() { - if let MaybeRelocatable::RelocatableValue(x) = val { - // Check if the next value is a relocatable of the same index - if let Some(MaybeRelocatable::RelocatableValue(y)) = iter.peek() { - // Check if the two relocatable values represent a valid array in memory - if x.segment_index == y.segment_index && x.offset <= y.offset { - // Fetch the y value from the iterator so we don't serialize it twice - iter.next(); - // Fetch array - output_string.push_str("["); - let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); - let mut array_iter: Peekable> = - array.iter().peekable(); - serialize_output(&mut array_iter, output_string, vm); - output_string.push_str("]"); - continue; - } - } - } - maybe_add_whitespace(output_string); - output_string.push_str(&val.to_string()); - } - } - - fn maybe_add_whitespace(string: &mut String) { - if !string.is_empty() && !string.ends_with("[") { - string.push(' '); - } - } - dbg!(output_string); + let output_string = if args.print_output { + Some(serialize_output(&vm, &return_values)) + } else { + None + }; // Set stop pointers for builtins so we can obtain the air public input if args.air_public_input.is_some() { @@ -589,7 +558,7 @@ fn run(args: impl Iterator) -> Result, Erro memory_writer.flush()?; } - Ok(return_values) + Ok(output_string) } fn additional_initialization(vm: &mut VirtualMachine, data_len: usize) -> Result<(), Error> { @@ -615,11 +584,9 @@ fn additional_initialization(vm: &mut VirtualMachine, data_len: usize) -> Result fn main() -> Result<(), Error> { match run(std::env::args()) { Err(Error::Cli(err)) => err.exit(), - Ok(return_values) => { - if !return_values.is_empty() { - let return_values_string_list = - return_values.iter().map(|m| m.to_string()).join(", "); - println!("Return values : [{}]", return_values_string_list); + Ok(output) => { + if let Some(output_string) = output { + println!("Program Output : {}", output_string); } Ok(()) } @@ -916,6 +883,47 @@ fn get_function_builtins( (builtins, builtin_offset) } +fn serialize_output(vm: &VirtualMachine, return_values: &Vec) -> String { + let mut output_string = String::new(); + let mut return_values_iter: Peekable> = return_values.iter().peekable(); + serialize_output_inner(&mut return_values_iter, &mut output_string, &vm); + fn serialize_output_inner( + iter: &mut Peekable>, + output_string: &mut String, + vm: &VirtualMachine, + ) { + while let Some(val) = iter.next() { + if let MaybeRelocatable::RelocatableValue(x) = val { + // Check if the next value is a relocatable of the same index + if let Some(MaybeRelocatable::RelocatableValue(y)) = iter.peek() { + // Check if the two relocatable values represent a valid array in memory + if x.segment_index == y.segment_index && x.offset <= y.offset { + // Fetch the y value from the iterator so we don't serialize it twice + iter.next(); + // Fetch array + output_string.push_str("["); + let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); + let mut array_iter: Peekable> = + array.iter().peekable(); + serialize_output_inner(&mut array_iter, output_string, vm); + output_string.push_str("]"); + continue; + } + } + } + maybe_add_whitespace(output_string); + output_string.push_str(&val.to_string()); + } + } + + fn maybe_add_whitespace(string: &mut String) { + if !string.is_empty() && !string.ends_with("[") { + string.push(' '); + } + } + output_string +} + #[cfg(test)] mod tests { #![allow(clippy::too_many_arguments)] @@ -923,153 +931,148 @@ mod tests { use assert_matches::assert_matches; use rstest::rstest; - // FIXME: bit of copy-paste to avoid dealing with visibility issues - fn felt_str(x: impl AsRef) -> Felt252 { - crate::Felt252::from_dec_str(x.as_ref()).expect("Couldn't parse bytes") - } - #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/fibonacci.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_fibonacci_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(89)]); + assert_matches!(run(args), Ok(Some(res)) if res == "89"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/factorial.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_factorial_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3628800)]); + assert_matches!(run(args), Ok(Some(res)) if res == "3628800"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/array_get.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_array_get_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(3)]); + assert_matches!(run(args), Ok(Some(res)) if res == "3"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_flow.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_enum_flow_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(300)]); + assert_matches!(run(args), Ok(Some(res)) if res == "300"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/enum_match.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_enum_match_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(10), MaybeRelocatable::from(felt_str("3618502788666131213697322783095070105623107215331596699973092056135872020471"))]); + assert_matches!(run(args), Ok(Some(res)) if res == "10 3618502788666131213697322783095070105623107215331596699973092056135872020471"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/hello.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_hello_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1), MaybeRelocatable::from(1234)]); + assert_matches!(run(args), Ok(Some(res)) if res == "1 1234"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/ops.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_ops_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(6)]); + assert_matches!(run(args), Ok(Some(res)) if res == "6"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_print_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![]); + assert_matches!(run(args), Ok(Some(res)) if res == ""); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/recursion.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_recursion_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1154076154663935037074198317650845438095734251249125412074882362667803016453"))]); + assert_matches!(run(args), Ok(Some(res)) if res == "1154076154663935037074198317650845438095734251249125412074882362667803016453"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/sample.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_sample_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("5050"))]); + assert_matches!(run(args), Ok(Some(res)) if res == "5050"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_poseidon_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1099385018355113290651252669115094675591288647745213771718157553170111442461"))]); + assert_matches!(run(args), Ok(Some(res)) if res == "1099385018355113290651252669115094675591288647745213771718157553170111442461"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/poseidon_pedersen.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_poseidon_pedersen_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1036257840396636296853154602823055519264738423488122322497453114874087006398"))]); + assert_matches!(run(args), Ok(Some(res)) if res == "1036257840396636296853154602823055519264738423488122322497453114874087006398"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/pedersen_example.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_pedersen_example_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(felt_str("1089549915800264549621536909767699778745926517555586332772759280702396009108"))]); + assert_matches!(run(args), Ok(Some(res)) if res == "1089549915800264549621536909767699778745926517555586332772759280702396009108"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_simple_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); + assert_matches!(run(args), Ok(Some(res)) if res == "1"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/simple_struct.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_simple_struct_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(100)]); + assert_matches!(run(args), Ok(Some(res)) if res == "100"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/dictionaries.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_dictionaries(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1024)]); + assert_matches!(run(args), Ok(Some(res)) if res == "1024"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "0"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "0"].as_slice())] fn test_run_branching_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(1)]); + assert_matches!(run(args), Ok(Some(res)) if res == "1"); } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "96"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "17"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/branching.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "96"].as_slice())] fn test_run_branching_not_0(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(0)]); + assert_matches!(run(args), Ok(Some(res)) if res == "0"); } #[rstest] @@ -1089,10 +1092,18 @@ mod tests { } #[rstest] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] - #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/with_input/array_input_sum.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null", "--args", "2 [1 2 3 4] 0 [9 8]"].as_slice())] fn test_array_input_sum(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(res) if res == vec![MaybeRelocatable::from(12)]); + assert_matches!(run(args), Ok(Some(res)) if res == "12"); + } + + #[rstest] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/struct_span_return.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo"].as_slice())] + #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/struct_span_return.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] + fn test_struct_span_return(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Ok(Some(res)) if res == "[[4 3][2 1]]"); } } From 5b797528b0e586043919c9825a608d5f91d1fb55 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 18:29:10 -0300 Subject: [PATCH 78/84] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00bfc1efae..e4971c11d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Add `print_output` flag to `cairo-1` crate [#1575] (https://github.com/lambdaclass/cairo-vm/pull/1575) + * feat: Add `air_private_input` flag to `cairo1-run` [#1559] (https://github.com/lambdaclass/cairo-vm/pull/1559) * feat: Implement air_private_input [#1552](https://github.com/lambdaclass/cairo-vm/pull/1552) From 9a115a28fa899de9e71cb65a7f725b200276a415 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 18:30:58 -0300 Subject: [PATCH 79/84] Add test file --- .../cairo-1-programs/struct_span_return.cairo | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 cairo_programs/cairo-1-programs/struct_span_return.cairo diff --git a/cairo_programs/cairo-1-programs/struct_span_return.cairo b/cairo_programs/cairo-1-programs/struct_span_return.cairo new file mode 100644 index 0000000000..5d40ffb65c --- /dev/null +++ b/cairo_programs/cairo-1-programs/struct_span_return.cairo @@ -0,0 +1,17 @@ +use core::array::SpanTrait; +use core::array::ArrayTrait; + + +fn main() -> Array> { + let mut numbers = ArrayTrait::new(); + let mut numbers_a = ArrayTrait::new(); + let mut numbers_b = ArrayTrait::new(); + numbers_a.append(4_u32); + numbers_a.append(3_u32); + numbers_b.append(2_u32); + numbers_b.append(1_u32); + numbers.append(numbers_a); + numbers.append(numbers_b); + + numbers +} From 5b95a5d4cf72b2837971a9440b473af260b08fe0 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 18:34:41 -0300 Subject: [PATCH 80/84] Add whitespace --- cairo1-run/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index e1ea748c24..b9bc8cc293 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -901,6 +901,7 @@ fn serialize_output(vm: &VirtualMachine, return_values: &Vec) // Fetch the y value from the iterator so we don't serialize it twice iter.next(); // Fetch array + maybe_add_whitespace(output_string); output_string.push_str("["); let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); let mut array_iter: Peekable> = From 0d4cff54a76baa9cedb26f9df09c5bd063f3aa3d Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 18:35:07 -0300 Subject: [PATCH 81/84] Update test --- cairo1-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index b9bc8cc293..c24aff166e 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -1105,6 +1105,6 @@ mod tests { #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/struct_span_return.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_struct_span_return(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(Some(res)) if res == "[[4 3][2 1]]"); + assert_matches!(run(args), Ok(Some(res)) if res == "[[4 3] [2 1]]"); } } From 1e4b65779e628ca00fb75d56ce2573cfe48e16bc Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 15 Jan 2024 18:51:43 -0300 Subject: [PATCH 82/84] Clippy --- cairo1-run/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index c24aff166e..9bfdca103b 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -883,10 +883,10 @@ fn get_function_builtins( (builtins, builtin_offset) } -fn serialize_output(vm: &VirtualMachine, return_values: &Vec) -> String { +fn serialize_output(vm: &VirtualMachine, return_values: &[MaybeRelocatable]) -> String { let mut output_string = String::new(); let mut return_values_iter: Peekable> = return_values.iter().peekable(); - serialize_output_inner(&mut return_values_iter, &mut output_string, &vm); + serialize_output_inner(&mut return_values_iter, &mut output_string, vm); fn serialize_output_inner( iter: &mut Peekable>, output_string: &mut String, @@ -902,12 +902,12 @@ fn serialize_output(vm: &VirtualMachine, return_values: &Vec) iter.next(); // Fetch array maybe_add_whitespace(output_string); - output_string.push_str("["); + output_string.push('['); let array = vm.get_continuous_range(*x, y.offset - x.offset).unwrap(); let mut array_iter: Peekable> = array.iter().peekable(); serialize_output_inner(&mut array_iter, output_string, vm); - output_string.push_str("]"); + output_string.push(']'); continue; } } @@ -918,7 +918,7 @@ fn serialize_output(vm: &VirtualMachine, return_values: &Vec) } fn maybe_add_whitespace(string: &mut String) { - if !string.is_empty() && !string.ends_with("[") { + if !string.is_empty() && !string.ends_with('[') { string.push(' '); } } @@ -993,7 +993,7 @@ mod tests { #[case(["cairo1-run", "../cairo_programs/cairo-1-programs/print.cairo", "--print_output", "--trace_file", "/dev/null", "--memory_file", "/dev/null", "--layout", "all_cairo", "--proof_mode", "--air_public_input", "/dev/null", "--air_private_input", "/dev/null"].as_slice())] fn test_run_print_ok(#[case] args: &[&str]) { let args = args.iter().cloned().map(String::from); - assert_matches!(run(args), Ok(Some(res)) if res == ""); + assert_matches!(run(args), Ok(Some(res)) if res.is_empty()); } #[rstest] From b888efb68c9d155fcaae1fef26ab943cf998ae82 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 16 Jan 2024 10:40:36 -0300 Subject: [PATCH 83/84] Add air_private_input check to CI --- .github/workflows/rust.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8376576e86..20c9ec31fc 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -321,6 +321,7 @@ jobs: cairo_programs/**/*.memory cairo_programs/**/*.trace cairo_programs/**/*.air_public_input + cairo_programs/**/*.air_private_input key: ${{ matrix.program-target }}-reference-trace-cache-${{ hashFiles('cairo_programs/**/*.cairo', 'examples/wasm-demo/src/array_sum.cairo') }} restore-keys: ${{ matrix.program-target }}-reference-trace-cache- @@ -360,7 +361,7 @@ jobs: include: - program-target: cairo_proof_programs programs-dir: cairo_programs/proof_programs - extra-args: '--proof_mode --air_public_input {program}.rs.air_public_input ' + extra-args: '--proof_mode --air_public_input {program}.rs.air_public_input --air_private_input {program}.rs.air_private_input ' - program-target: cairo_test_programs programs-dir: cairo_programs extra-args: '' @@ -399,6 +400,7 @@ jobs: cairo_programs/**/*.memory cairo_programs/**/*.trace cairo_programs/**/*.air_public_input + cairo_programs/**/*.air_private_input key: ${{ matrix.program-target }}-release-trace-cache-${{ github.sha }} @@ -498,6 +500,7 @@ jobs: cairo_programs/**/*.memory cairo_programs/**/*.trace cairo_programs/**/*.air_public_input + cairo_programs/**/*.air_private_input key: ${{ matrix.program-target }}-reference-trace-cache-${{ hashFiles('cairo_programs/**/*.cairo', 'examples/wasm-demo/src/array_sum.cairo') }} fail-on-cache-miss: true @@ -508,6 +511,7 @@ jobs: cairo_programs/**/*.memory cairo_programs/**/*.trace cairo_programs/**/*.air_public_input + cairo_programs/**/*.air_private_input key: ${{ matrix.program-target }}-release-trace-cache-${{ github.sha }} fail-on-cache-miss: true @@ -516,8 +520,9 @@ jobs: if [ ${{ matrix.program-target }} = cairo_proof_programs ]; then PROOF=proof_mode AIR_PUBLIC_INPUT=air_public_input + AIR_PRIVATE_INPUT=air_private_input fi - ./vm/src/tests/compare_vm_state.sh trace memory $PROOF $AIR_PUBLIC_INPUT + ./vm/src/tests/compare_vm_state.sh trace memory $PROOF $AIR_PUBLIC_INPUT $AIR_PRIVATE_INPUT ./vm/src/tests/compare_vm_state.sh trace memory $PROOF wasm-demo: From 31ef3f8584436fd68e40de268b5e072b25a88891 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:43:54 -0300 Subject: [PATCH 84/84] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b261ba412..98b10d14ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,6 @@ #### [1.0.0-rc0] - 2024-1-5 * feat: Use `ProjectivePoint` from types-rs in ec_op builtin impl [#1532](https://github.com/lambdaclass/cairo-vm/pull/1532) -* feat: Add `args` flag to `cairo1-run` [#15551] (https://github.com/lambdaclass/cairo-vm/pull/15551) * feat(BREAKING): Replace `cairo-felt` crate with `starknet-types-core` (0.0.5) [#1408](https://github.com/lambdaclass/cairo-vm/pull/1408)