Skip to content

Commit 4af2806

Browse files
FrancoGiachettagabrielbosio
authored andcommitted
Fix CantWriteReturnFp and other issues related to the initial gas loading (#2015)
* fix issues like WriteReturnFp due to the gas builtin * try fix tests and update changelog * fix test
1 parent 8092c91 commit 4af2806

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
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+
* fix: Fix `WriteReturnFp` error due to a bad loading of initial gas [#2015](https://github.com/lambdaclass/cairo-vm/pull/2015)
6+
57
* refactor: Replaces security anyhow errors with enum variants [#1946](https://github.com/lambdaclass/cairo-vm/pull/1946)
68

79
* fix: `mod_builtin_fill_memory` could be stuck in an infinite loop [#1975](https://github.com/lambdaclass/cairo-vm/issues/1975)

cairo1-run/src/cairo_run.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ pub fn cairo_run_program(
143143

144144
let main_func = find_function(sierra_program, "::main")?;
145145

146-
let initial_gas = 9999999999999_usize;
147-
148146
// Fetch return type data
149147
let return_type_id = match main_func.signature.ret_types.last() {
150148
// We need to check if the last return type is indeed the function's return value and not an implicit return value
@@ -261,7 +259,7 @@ pub fn cairo_run_program(
261259
false,
262260
)?;
263261
let end = runner.initialize(cairo_run_config.proof_mode)?;
264-
load_arguments(&mut runner, &cairo_run_config, main_func, initial_gas)?;
262+
load_arguments(&mut runner, &cairo_run_config, main_func)?;
265263

266264
// Run it until the end / infinite loop in proof_mode
267265
runner.run_until_pc(end, &mut hint_processor)?;
@@ -465,7 +463,6 @@ fn load_arguments(
465463
runner: &mut CairoRunner,
466464
cairo_run_config: &Cairo1RunConfig,
467465
main_func: &Function,
468-
initial_gas: usize,
469466
) -> Result<(), Error> {
470467
let got_gas_builtin = main_func
471468
.signature
@@ -497,12 +494,7 @@ fn load_arguments(
497494
if got_segment_arena {
498495
ap_offset += 4;
499496
}
500-
// Load initial gas if GasBuiltin is present
501497
if got_gas_builtin {
502-
runner.vm.insert_value(
503-
(runner.vm.get_ap() + ap_offset).map_err(VirtualMachineError::Math)?,
504-
Felt252::from(initial_gas),
505-
)?;
506498
ap_offset += 1;
507499
}
508500
for arg in cairo_run_config.args {
@@ -617,9 +609,10 @@ fn create_entry_code(
617609
ap += 1;
618610
};
619611
} else if generic_ty == &GasBuiltinType::ID {
620-
// We already loaded the inital gas so we just advance AP
612+
// Load initial gas
621613
casm_build_extend! {ctx,
622-
ap += 1;
614+
const initial_gas = 9999999999999_usize;
615+
tempvar gas = initial_gas;
623616
};
624617
} else {
625618
let ty_size = type_sizes[ty];

cairo1-run/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ mod tests {
447447
None,
448448
None
449449
))]
450+
#[case(
451+
"gas_builtin_loading.cairo",
452+
"939340725154356279478212603733403581890242362232206720294887278547043341575",
453+
"[939340725154356279478212603733403581890242362232206720294887278547043341575]",
454+
None,
455+
None
456+
)]
450457
fn test_run_program(
451458
#[case] program: &str,
452459
#[case] expected_output: &str,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use core::poseidon::PoseidonTrait;
2+
use core::hash::{HashStateTrait, HashStateExTrait};
3+
use core::testing::get_available_gas;
4+
5+
fn main() -> felt252 {
6+
let a: u128 = get_available_gas();
7+
let b = PoseidonTrait::new().update_with(a).finalize();
8+
9+
b
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use core::poseidon::PoseidonTrait;
2+
use core::hash::{HashStateTrait, HashStateExTrait};
3+
use core::testing::get_available_gas;
4+
5+
fn main() -> Array<felt252> {
6+
let a: u128 = get_available_gas();
7+
let b = PoseidonTrait::new().update_with(a).finalize();
8+
let mut ser_output: Array<felt252> = ArrayTrait::new();
9+
10+
b.serialize(ref ser_output);
11+
12+
ser_output
13+
}

0 commit comments

Comments
 (0)