Skip to content

Commit cc9baac

Browse files
fmolettaOppen
andauthored
[Cairo 1] Filter implicit arguments from return value (#1775)
* Filter implicit arguments from return value * Fix logic * Fix logic * Add CHANGELOG entry * Update cairo1-run/src/cairo_run.rs Co-authored-by: Mario Rugiero <[email protected]> * fmt --------- Co-authored-by: Mario Rugiero <[email protected]>
1 parent 26043e6 commit cc9baac

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
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+
* feat: Filter implicit arguments from return value in cairo1-run crate [#1775](https://github.com/lambdaclass/cairo-vm/pull/1775)
6+
57
* feat(BREAKING): Serialize inputs into output segment in cairo1-run crate:
68
* Checks that only `Array<Felt252>` can be received by the program main function when running with with either `--proof_mode` or `--append_return_values`.
79
* Copies the input value to the output segment right after the output in the format `[array_len, arr[0], arr[1],.., arr[n]]`.

cairo1-run/src/cairo_run.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,16 @@ pub fn cairo_run_program(
133133
let initial_gas = 9999999999999_usize;
134134

135135
// Fetch return type data
136-
let return_type_id = main_func.signature.ret_types.last();
136+
let return_type_id = match main_func.signature.ret_types.last() {
137+
// We need to check if the last return type is indeed the function's return value and not an implicit return value
138+
return_type @ Some(concrete_ty)
139+
if get_info(&sierra_program_registry, concrete_ty)
140+
.is_some_and(|info| !is_implicit_generic_id(&info.long_id.generic_id)) =>
141+
{
142+
return_type
143+
}
144+
_ => None,
145+
};
137146

138147
if (cairo_run_config.proof_mode || cairo_run_config.append_return_values)
139148
&& !check_only_array_felt_input_type(
@@ -862,15 +871,7 @@ fn check_only_array_felt_input_type(
862871
.filter(|ty| {
863872
let info = get_info(sierra_program_registry, ty).unwrap();
864873
let generic_ty = &info.long_id.generic_id;
865-
!(generic_ty != &SegmentArenaType::ID
866-
|| generic_ty == &GasBuiltinType::ID
867-
|| generic_ty == &BitwiseType::ID
868-
|| generic_ty == &EcOpType::ID
869-
|| generic_ty == &PedersenType::ID
870-
|| generic_ty == &PoseidonType::ID
871-
|| generic_ty == &RangeCheckType::ID
872-
|| generic_ty == &SegmentArenaType::ID
873-
|| generic_ty == &SystemType::ID)
874+
!is_implicit_generic_id(generic_ty)
874875
})
875876
.collect_vec();
876877
if arg_types.is_empty() {
@@ -885,6 +886,22 @@ fn check_only_array_felt_input_type(
885886
false
886887
}
887888
}
889+
890+
// Returns true if the generic id corresponds to an implicit argument (aka a builtin, gas, or system type)
891+
fn is_implicit_generic_id(generic_ty: &GenericTypeId) -> bool {
892+
[
893+
SegmentArenaType::ID,
894+
GasBuiltinType::ID,
895+
BitwiseType::ID,
896+
EcOpType::ID,
897+
PedersenType::ID,
898+
PoseidonType::ID,
899+
RangeCheckType::ID,
900+
SegmentArenaType::ID,
901+
SystemType::ID,
902+
]
903+
.contains(generic_ty)
904+
}
888905
// Checks that the return type is either an Array<Felt252> or a PanicResult<Array<Felt252>> type
889906
fn check_only_array_felt_return_type(
890907
return_type_id: Option<&ConcreteTypeId>,

0 commit comments

Comments
 (0)