From 8f81113fec41f7c4ad499999ac0047c9eec95829 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 29 May 2024 17:07:33 -0300 Subject: [PATCH 1/5] Add boolean method copy_to_output --- cairo1-run/src/cairo_run.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index dd187d1802..963cab8164 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -108,6 +108,12 @@ impl Default for Cairo1RunConfig<'_> { } } +impl Cairo1RunConfig<'_> { + fn copy_to_output(&self) -> bool { + self.append_return_values || self.proof_mode + } +} + // Runs a Cairo 1 program // Returns the runner after execution + the return values + the serialized return values (if serialize_output is enabled) // The return values will contain the memory values just as they appear in the VM, after removing the PanicResult enum (if present). @@ -144,7 +150,7 @@ pub fn cairo_run_program( _ => None, }; - if (cairo_run_config.proof_mode || cairo_run_config.append_return_values) + if cairo_run_config.copy_to_output() && !check_only_array_felt_input_type( &main_func.signature.param_types, &sierra_program_registry, @@ -152,7 +158,7 @@ pub fn cairo_run_program( { return Err(Error::IlegalInputValue); }; - if (cairo_run_config.proof_mode || cairo_run_config.append_return_values) + if cairo_run_config.copy_to_output() && !check_only_array_felt_return_type(return_type_id, &sierra_program_registry) { return Err(Error::IlegalReturnValue); @@ -249,8 +255,6 @@ pub fn cairo_run_program( runner.end_run(false, false, &mut hint_processor)?; - let skip_output = cairo_run_config.proof_mode || cairo_run_config.append_return_values; - let result_inner_type_size = result_inner_type_size(return_type_id, &sierra_program_registry, &type_sizes); // Fetch return values @@ -259,11 +263,11 @@ pub fn cairo_run_program( result_inner_type_size, &runner.vm, builtin_count, - skip_output, + cairo_run_config.copy_to_output(), )?; let serialized_output = if cairo_run_config.serialize_output { - if cairo_run_config.append_return_values || cairo_run_config.proof_mode { + if cairo_run_config.copy_to_output() { // The return value is already serialized, so we can just print the array values let mut output_string = String::from("["); // Skip array_len @@ -288,7 +292,7 @@ pub fn cairo_run_program( // Set stop pointers for builtins so we can obtain the air public input if cairo_run_config.finalize_builtins { - if skip_output { + if cairo_run_config.copy_to_output() { // Set stop pointer for each builtin runner.vm.builtins_final_stack_from_stack_pointer_dict( &builtins @@ -449,7 +453,6 @@ fn load_arguments( .param_types .iter() .any(|ty| ty.debug_name.as_ref().is_some_and(|n| n == "SegmentArena")); - let append_output = cairo_run_config.append_return_values || cairo_run_config.proof_mode; // This AP correction represents the memory slots taken up by the values created by `create_entry_code`: // These include: // * The builtin bases (not including output) @@ -459,7 +462,7 @@ fn load_arguments( // * info_segment_ptr // * 0 let mut ap_offset = runner.get_program().builtins_len(); - if append_output { + if cairo_run_config.copy_to_output() { ap_offset += runner.get_program().builtins_len() - 1; } if got_segment_arena { @@ -507,7 +510,7 @@ fn create_entry_code( initial_gas: usize, config: &Cairo1RunConfig, ) -> Result<(CasmContext, Vec), Error> { - let copy_to_output_builtin = config.proof_mode || config.append_return_values; + let copy_to_output_builtin = config.copy_to_output(); let signature = &func.signature; // The builtins in the formatting expected by the runner. let (builtins, builtin_offset) = From 96844b9e7c98b3b17016b4bac8f29494311f8c20 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 29 May 2024 17:13:51 -0300 Subject: [PATCH 2/5] Update doc --- cairo1-run/README.md | 4 ++-- cairo1-run/src/cairo_run.rs | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cairo1-run/README.md b/cairo1-run/README.md index c1095dcbf4..9271d860bb 100644 --- a/cairo1-run/README.md +++ b/cairo1-run/README.md @@ -61,7 +61,7 @@ The cairo1-run cli supports the following optional arguments: * `--memory_file `: Receives the name of a file and outputs the relocated memory into it -* `--proof_mode`: Runs the program in proof_mode. Only allows `Array` as return value. +* `--proof_mode`: Runs the program in proof_mode. Only allows `Array` as return and input value. * `--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. @@ -69,7 +69,7 @@ The cairo1-run cli supports the following optional arguments: * `--cairo_pie_output `: Receives the name of a file and outputs the Cairo PIE into it. Can only be used if proof_mode, is not enabled. -* `--append_return_values`: Adds extra instructions to the program in order to append the return values to the output builtin's segment. This is the default behaviour for proof_mode. Only allows `Array` as return value. +* `--append_return_values`: Adds extra instructions to the program in order to append the return and input values to the output builtin's segment. This is the default behaviour for proof_mode. Only allows `Array` as return and input value. # Running scarb projects diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 963cab8164..7557c64f17 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -109,15 +109,17 @@ impl Default for Cairo1RunConfig<'_> { } impl Cairo1RunConfig<'_> { + // Returns true if the flags in the config enable adding the output builtin and + // copying input and output values into it's segment fn copy_to_output(&self) -> bool { self.append_return_values || self.proof_mode } } -// Runs a Cairo 1 program -// Returns the runner after execution + the return values + the serialized return values (if serialize_output is enabled) -// The return values will contain the memory values just as they appear in the VM, after removing the PanicResult enum (if present). -// Except if either the flag append_return_values or proof_mode are enabled, in which case the return values will consist of its serialized form: [array_len, array[0], array[1], ..., array[array_len -1]] +/// Runs a Cairo 1 program +/// Returns the runner after execution + the return values + the serialized return values (if serialize_output is enabled) +/// The return values will contain the memory values just as they appear in the VM, after removing the PanicResult enum (if present). +/// Except if either the flag append_return_values or proof_mode are enabled, in which case the return values will consist of its serialized form: [array_len, array[0], array[1], ..., array[array_len -1]] pub fn cairo_run_program( sierra_program: &SierraProgram, cairo_run_config: Cairo1RunConfig, From d50fa45eade2902d337c93f36757394e56a50434 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 29 May 2024 17:14:38 -0300 Subject: [PATCH 3/5] Update doc --- cairo1-run/src/cairo_run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 7557c64f17..24268a3764 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -89,7 +89,7 @@ pub struct Cairo1RunConfig<'a> { /// Should be true if either air_public_input or cairo_pie_output are needed /// Sets builtins stop_ptr by calling `final_stack` on each builtin pub finalize_builtins: bool, - /// Appends return values to the output segment. This is performed by default when running in proof_mode + /// Appends the return and input values to the output segment. This is performed by default when running in proof_mode pub append_return_values: bool, } From 733b139aaba1d0de8fbd24364b4d7197746b24dd Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 29 May 2024 17:15:53 -0300 Subject: [PATCH 4/5] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7fc218aaa..c5a02fa4df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* refactor: Add boolean method Cairo1RunConfig::copy_to_output + Update Doc [#1778](https://github.com/lambdaclass/cairo-vm/pull/1778) + * feat: Filter implicit arguments from return value in cairo1-run crate [#1775](https://github.com/lambdaclass/cairo-vm/pull/1775) * feat(BREAKING): Serialize inputs into output segment in cairo1-run crate: From d9c0c21736d2bc03210da422915b79a807231504 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 29 May 2024 17:19:04 -0300 Subject: [PATCH 5/5] fmt --- cairo1-run/src/cairo_run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 24268a3764..81339ce170 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -109,7 +109,7 @@ impl Default for Cairo1RunConfig<'_> { } impl Cairo1RunConfig<'_> { - // Returns true if the flags in the config enable adding the output builtin and + // Returns true if the flags in the config enable adding the output builtin and // copying input and output values into it's segment fn copy_to_output(&self) -> bool { self.append_return_values || self.proof_mode