Skip to content

Commit 2cf8f1d

Browse files
authored
Modify RunResources usage (#1241)
* Add RunResources.consume_steps && modify CairoRuner::run_until_pc and CairoRunner::run_from_entrypoint API * Add RunResources as an input of HintProcessor::execute_hint * fix test * Add doc * Modify field RunResources.n_steps: Option<usize> * fix unit tests * minor code refactors * update changelog * typo
1 parent ad25b63 commit 2cf8f1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+392
-114
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
* fix(security): avoid denial of service on malicious input exploiting the scientific notation parser [#1239](https://github.com/lambdaclass/cairo-rs/pull/1239)
88

9+
* BREAKING: Change `RunResources` usage:
10+
* Modify field type `RunResources.n_steps: Option<usize>,`
11+
12+
* Public Api Changes:
13+
* CairoRunner::run_until_pc: Now receive a `&mut RunResources` instead of an `&mut Option<RunResources>`
14+
* CairoRunner::run_from_entrypoint: Now receive a `&mut RunResources` instead of an `&mut Option<RunResources>`
15+
* VirtualMachine::Step: Add `&mut RunResources` as input
16+
* Trait HintProcessor::execute_hint: Add `&mut RunResources` as an input
17+
918
* perf: accumulate `min` and `max` instruction offsets during run to speed up range check [#1080](https://github.com/lambdaclass/cairo-rs/pull/)
1019
BREAKING: `Cairo_runner::get_perm_range_check_limits` no longer returns an error when called without trace enabled, as it no longer depends on it
1120

hint_accountant/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use cairo_vm::{
77
},
88
serde::deserialize_program::ApTracking,
99
types::exec_scope::ExecutionScopes,
10-
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
10+
vm::{
11+
errors::hint_errors::HintError, runners::cairo_runner::RunResources,
12+
vm_core::VirtualMachine,
13+
},
1114
with_std::collections::{HashMap, HashSet},
1215
};
1316
use serde::Deserialize;
@@ -67,7 +70,13 @@ fn run() {
6770
.compile_hint(h, &ap_tracking_data, &reference_ids, &references)
6871
.expect("this implementation is infallible");
6972
matches!(
70-
hint_executor.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants),
73+
hint_executor.execute_hint(
74+
&mut vm,
75+
&mut exec_scopes,
76+
&hint_data,
77+
&constants,
78+
&mut RunResources::default()
79+
),
7180
Err(HintError::UnknownHint(_)),
7281
)
7382
})

vm/src/cairo_run.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
types::program::Program,
44
vm::{
55
errors::{cairo_run_errors::CairoRunError, vm_exception::VmException},
6-
runners::cairo_runner::CairoRunner,
6+
runners::cairo_runner::{CairoRunner, RunResources},
77
security::verify_secure_runner,
88
vm_core::VirtualMachine,
99
},
@@ -59,7 +59,7 @@ pub fn cairo_run(
5959
let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled);
6060
let end = cairo_runner.initialize(&mut vm)?;
6161
// check step calculation
62-
let mut run_resources = None;
62+
let mut run_resources = RunResources::default();
6363

6464
cairo_runner
6565
.run_until_pc(end, &mut run_resources, &mut vm, hint_executor)
@@ -156,7 +156,7 @@ mod tests {
156156
.map_err(CairoRunError::Runner)?;
157157

158158
assert!(cairo_runner
159-
.run_until_pc(end, &mut None, &mut vm, hint_processor)
159+
.run_until_pc(end, &mut RunResources::default(), &mut vm, hint_processor)
160160
.is_ok());
161161

162162
Ok((cairo_runner, vm))
@@ -176,7 +176,12 @@ mod tests {
176176

177177
let end = cairo_runner.initialize(&mut vm).unwrap();
178178
assert!(cairo_runner
179-
.run_until_pc(end, &mut None, &mut vm, &mut hint_processor)
179+
.run_until_pc(
180+
end,
181+
&mut RunResources::default(),
182+
&mut vm,
183+
&mut hint_processor
184+
)
180185
.is_ok());
181186
assert!(cairo_runner.relocate(&mut vm, true).is_ok());
182187
// `main` returns without doing nothing, but `not_main` sets `[ap]` to `1`
@@ -296,7 +301,12 @@ mod tests {
296301
let mut vm = vm!();
297302
let end = cairo_runner.initialize(&mut vm).unwrap();
298303
assert!(cairo_runner
299-
.run_until_pc(end, &mut None, &mut vm, &mut hint_processor)
304+
.run_until_pc(
305+
end,
306+
&mut RunResources::default(),
307+
&mut vm,
308+
&mut hint_processor
309+
)
300310
.is_ok());
301311
assert!(cairo_runner.relocate(&mut vm, false).is_ok());
302312
assert!(vm.get_relocated_trace().is_err());

vm/src/hint_processor/builtin_hint_processor/bigint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ mod test {
105105
use crate::stdlib::collections::HashMap;
106106
use crate::types::exec_scope::ExecutionScopes;
107107
use crate::utils::test_utils::*;
108+
use crate::vm::runners::cairo_runner::RunResources;
108109
use crate::vm::vm_core::VirtualMachine;
109110
use assert_matches::assert_matches;
110111
use num_bigint::BigInt;

vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ mod tests {
306306
use super::*;
307307
use crate::hint_processor::builtin_hint_processor::hint_code;
308308
use crate::types::errors::math_errors::MathError;
309+
use crate::vm::runners::cairo_runner::RunResources;
309310
use crate::{
310311
any_box,
311312
hint_processor::{

vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ use super::{
2020
pack::*,
2121
},
2222
};
23-
use crate::hint_processor::builtin_hint_processor::secp::ec_utils::{
24-
ec_double_assign_new_x, ec_double_assign_new_x_v2,
23+
use crate::{
24+
hint_processor::builtin_hint_processor::secp::ec_utils::{
25+
ec_double_assign_new_x, ec_double_assign_new_x_v2,
26+
},
27+
vm::runners::cairo_runner::RunResources,
2528
};
2629
use crate::{
2730
hint_processor::{
@@ -169,6 +172,7 @@ impl HintProcessor for BuiltinHintProcessor {
169172
exec_scopes: &mut ExecutionScopes,
170173
hint_data: &Box<dyn Any>,
171174
constants: &HashMap<String, Felt252>,
175+
_run_resources: &mut RunResources,
172176
) -> Result<(), HintError> {
173177
let hint_data = hint_data
174178
.downcast_ref::<HintProcessorData>()
@@ -807,6 +811,7 @@ mod tests {
807811
use crate::stdlib::any::Any;
808812
use crate::types::relocatable::Relocatable;
809813

814+
use crate::vm::runners::cairo_runner::RunResources;
810815
use crate::{
811816
any_box,
812817
hint_processor::hint_processor_definition::HintProcessor,
@@ -1222,7 +1227,8 @@ mod tests {
12221227
&mut vm,
12231228
exec_scopes,
12241229
&any_box!(hint_data),
1225-
&HashMap::new()
1230+
&HashMap::new(),
1231+
&mut RunResources::default(),
12261232
),
12271233
Ok(())
12281234
);
@@ -1234,7 +1240,8 @@ mod tests {
12341240
&mut vm,
12351241
exec_scopes,
12361242
&any_box!(hint_data),
1237-
&HashMap::new()
1243+
&HashMap::new(),
1244+
&mut RunResources::default(),
12381245
),
12391246
Ok(())
12401247
);

vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ pub fn u64_array_to_mayberelocatable_vec(array: &[u64]) -> Vec<MaybeRelocatable>
368368
mod tests {
369369
use super::*;
370370
use crate::stdlib::string::ToString;
371+
use crate::vm::runners::cairo_runner::RunResources;
371372

372373
use crate::{
373374
any_box,

vm/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ mod tests {
263263
use crate::stdlib::collections::HashMap;
264264
use crate::types::exec_scope::ExecutionScopes;
265265

266+
use crate::vm::runners::cairo_runner::RunResources;
266267
use crate::{
267268
hint_processor::builtin_hint_processor::dict_manager::{DictManager, DictTracker},
268269
relocatable,

vm/src/hint_processor/builtin_hint_processor/ec_recover.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ mod tests {
118118
use crate::hint_processor::builtin_hint_processor::hint_code;
119119
use crate::hint_processor::hint_processor_definition::HintReference;
120120
use crate::utils::test_utils::*;
121+
use crate::vm::runners::cairo_runner::RunResources;
121122
use crate::vm::vm_core::VirtualMachine;
122123
use crate::{
123124
any_box,

vm/src/hint_processor/builtin_hint_processor/ec_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ mod tests {
233233

234234
use crate::hint_processor::builtin_hint_processor::hint_code;
235235
use crate::utils::test_utils::*;
236+
use crate::vm::runners::cairo_runner::RunResources;
236237
use assert_matches::assert_matches;
237238

238239
use super::*;

0 commit comments

Comments
 (0)