From 88031c749e994e6d210ec3613c2f7ca20b0954e0 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Thu, 30 May 2024 10:38:57 -0300 Subject: [PATCH] refactor: Add boolean method `Cairo1RunConfig::copy_to_output` + Update Doc (#1778) * Add boolean method copy_to_output * Update doc * Update doc * Add changelog entry * fmt --- CHANGELOG.md | 2 ++ cairo1-run/README.md | 4 ++-- cairo1-run/src/cairo_run.rs | 35 ++++++++++++++++++++--------------- 3 files changed, 24 insertions(+), 17 deletions(-) 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: 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 dd187d1802..81339ce170 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, } @@ -108,10 +108,18 @@ impl Default for Cairo1RunConfig<'_> { } } -// 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]] +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]] pub fn cairo_run_program( sierra_program: &SierraProgram, cairo_run_config: Cairo1RunConfig, @@ -144,7 +152,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 +160,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 +257,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 +265,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 +294,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 +455,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 +464,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 +512,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) =