Skip to content

Commit 88031c7

Browse files
authored
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
1 parent 05352b1 commit 88031c7

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* refactor: Add boolean method Cairo1RunConfig::copy_to_output + Update Doc [#1778](https://github.com/lambdaclass/cairo-vm/pull/1778)
6+
57
* feat: Filter implicit arguments from return value in cairo1-run crate [#1775](https://github.com/lambdaclass/cairo-vm/pull/1775)
68

79
* feat(BREAKING): Serialize inputs into output segment in cairo1-run crate:

cairo1-run/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ The cairo1-run cli supports the following optional arguments:
6161

6262
* `--memory_file <MEMORY_FILE>`: Receives the name of a file and outputs the relocated memory into it
6363

64-
* `--proof_mode`: Runs the program in proof_mode. Only allows `Array<felt252>` as return value.
64+
* `--proof_mode`: Runs the program in proof_mode. Only allows `Array<felt252>` as return and input value.
6565

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

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

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

72-
* `--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<felt252>` as return value.
72+
* `--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<felt252>` as return and input value.
7373

7474
# Running scarb projects
7575

cairo1-run/src/cairo_run.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub struct Cairo1RunConfig<'a> {
8989
/// Should be true if either air_public_input or cairo_pie_output are needed
9090
/// Sets builtins stop_ptr by calling `final_stack` on each builtin
9191
pub finalize_builtins: bool,
92-
/// Appends return values to the output segment. This is performed by default when running in proof_mode
92+
/// Appends the return and input values to the output segment. This is performed by default when running in proof_mode
9393
pub append_return_values: bool,
9494
}
9595

@@ -108,10 +108,18 @@ impl Default for Cairo1RunConfig<'_> {
108108
}
109109
}
110110

111-
// Runs a Cairo 1 program
112-
// Returns the runner after execution + the return values + the serialized return values (if serialize_output is enabled)
113-
// The return values will contain the memory values just as they appear in the VM, after removing the PanicResult enum (if present).
114-
// 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]]
111+
impl Cairo1RunConfig<'_> {
112+
// Returns true if the flags in the config enable adding the output builtin and
113+
// copying input and output values into it's segment
114+
fn copy_to_output(&self) -> bool {
115+
self.append_return_values || self.proof_mode
116+
}
117+
}
118+
119+
/// Runs a Cairo 1 program
120+
/// Returns the runner after execution + the return values + the serialized return values (if serialize_output is enabled)
121+
/// The return values will contain the memory values just as they appear in the VM, after removing the PanicResult enum (if present).
122+
/// 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]]
115123
pub fn cairo_run_program(
116124
sierra_program: &SierraProgram,
117125
cairo_run_config: Cairo1RunConfig,
@@ -144,15 +152,15 @@ pub fn cairo_run_program(
144152
_ => None,
145153
};
146154

147-
if (cairo_run_config.proof_mode || cairo_run_config.append_return_values)
155+
if cairo_run_config.copy_to_output()
148156
&& !check_only_array_felt_input_type(
149157
&main_func.signature.param_types,
150158
&sierra_program_registry,
151159
)
152160
{
153161
return Err(Error::IlegalInputValue);
154162
};
155-
if (cairo_run_config.proof_mode || cairo_run_config.append_return_values)
163+
if cairo_run_config.copy_to_output()
156164
&& !check_only_array_felt_return_type(return_type_id, &sierra_program_registry)
157165
{
158166
return Err(Error::IlegalReturnValue);
@@ -249,8 +257,6 @@ pub fn cairo_run_program(
249257

250258
runner.end_run(false, false, &mut hint_processor)?;
251259

252-
let skip_output = cairo_run_config.proof_mode || cairo_run_config.append_return_values;
253-
254260
let result_inner_type_size =
255261
result_inner_type_size(return_type_id, &sierra_program_registry, &type_sizes);
256262
// Fetch return values
@@ -259,11 +265,11 @@ pub fn cairo_run_program(
259265
result_inner_type_size,
260266
&runner.vm,
261267
builtin_count,
262-
skip_output,
268+
cairo_run_config.copy_to_output(),
263269
)?;
264270

265271
let serialized_output = if cairo_run_config.serialize_output {
266-
if cairo_run_config.append_return_values || cairo_run_config.proof_mode {
272+
if cairo_run_config.copy_to_output() {
267273
// The return value is already serialized, so we can just print the array values
268274
let mut output_string = String::from("[");
269275
// Skip array_len
@@ -288,7 +294,7 @@ pub fn cairo_run_program(
288294

289295
// Set stop pointers for builtins so we can obtain the air public input
290296
if cairo_run_config.finalize_builtins {
291-
if skip_output {
297+
if cairo_run_config.copy_to_output() {
292298
// Set stop pointer for each builtin
293299
runner.vm.builtins_final_stack_from_stack_pointer_dict(
294300
&builtins
@@ -449,7 +455,6 @@ fn load_arguments(
449455
.param_types
450456
.iter()
451457
.any(|ty| ty.debug_name.as_ref().is_some_and(|n| n == "SegmentArena"));
452-
let append_output = cairo_run_config.append_return_values || cairo_run_config.proof_mode;
453458
// This AP correction represents the memory slots taken up by the values created by `create_entry_code`:
454459
// These include:
455460
// * The builtin bases (not including output)
@@ -459,7 +464,7 @@ fn load_arguments(
459464
// * info_segment_ptr
460465
// * 0
461466
let mut ap_offset = runner.get_program().builtins_len();
462-
if append_output {
467+
if cairo_run_config.copy_to_output() {
463468
ap_offset += runner.get_program().builtins_len() - 1;
464469
}
465470
if got_segment_arena {
@@ -507,7 +512,7 @@ fn create_entry_code(
507512
initial_gas: usize,
508513
config: &Cairo1RunConfig,
509514
) -> Result<(CasmContext, Vec<BuiltinName>), Error> {
510-
let copy_to_output_builtin = config.proof_mode || config.append_return_values;
515+
let copy_to_output_builtin = config.copy_to_output();
511516
let signature = &func.signature;
512517
// The builtins in the formatting expected by the runner.
513518
let (builtins, builtin_offset) =

0 commit comments

Comments
 (0)