Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: Add boolean method Cairo1RunConfig::copy_to_output + Update Doc #1778

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions cairo1-run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ The cairo1-run cli supports the following optional arguments:

* `--memory_file <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<felt252>` as return value.
* `--proof_mode`: Runs the program in proof_mode. Only allows `Array<felt252>` as return and input value.

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

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

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

* `--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.
* `--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.

# Running scarb projects

Expand Down
35 changes: 20 additions & 15 deletions cairo1-run/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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,
Expand Down Expand Up @@ -144,15 +152,15 @@ 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,
)
{
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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -507,7 +512,7 @@ fn create_entry_code(
initial_gas: usize,
config: &Cairo1RunConfig,
) -> Result<(CasmContext, Vec<BuiltinName>), 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) =
Expand Down
Loading