Skip to content

Commit 8d716b7

Browse files
Store constants in hints (#2200)
* Add identifiers to hint_processor * Use identifiers in split_felt * Fix tests * Use correct data type for new parameter * Replace constants everywhere * Remove constants parameter * Minor improvements * Update changelog
1 parent 969da60 commit 8d716b7

File tree

9 files changed

+47
-61
lines changed

9 files changed

+47
-61
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+
* breaking: Store constants in Hint Data [#2191](https://github.com/lambdaclass/cairo-vm/pull/2191)
6+
57
#### [3.0.0-rc.3] - 2025-26-08
68

79
* chore: Bump types-rs to 0.2.0 [#2183](https://github.com/lambdaclass/cairo-vm/pull/2183)

hint_accountant/src/main.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,11 @@ fn run() {
5151
}
5252
let mut vm = VirtualMachine::new(false, false);
5353
let mut hint_executor = BuiltinHintProcessor::new_empty();
54-
let (
55-
ap_tracking_data,
56-
reference_ids,
57-
references,
58-
mut exec_scopes,
59-
constants,
60-
accessible_scopes,
61-
) = (
54+
let (ap_tracking_data, reference_ids, references, mut exec_scopes, accessible_scopes) = (
6255
ApTracking::default(),
6356
HashMap::new(),
6457
Vec::new(),
6558
ExecutionScopes::new(),
66-
HashMap::new(),
6759
Vec::new(),
6860
);
6961
let missing_hints: HashSet<_> = whitelists
@@ -78,10 +70,11 @@ fn run() {
7870
&reference_ids,
7971
&references,
8072
&accessible_scopes,
73+
Default::default(),
8174
)
8275
.expect("this implementation is infallible");
8376
matches!(
84-
hint_executor.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants,),
77+
hint_executor.execute_hint(&mut vm, &mut exec_scopes, &hint_data),
8578
Err(HintError::UnknownHint(_)),
8679
)
8780
})

vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub struct HintProcessorData {
133133
pub ap_tracking: ApTracking,
134134
pub ids_data: HashMap<String, HintReference>,
135135
pub accessible_scopes: Vec<String>,
136+
pub constants: Rc<HashMap<String, Felt252>>,
136137
}
137138

138139
impl HintProcessorData {
@@ -142,6 +143,7 @@ impl HintProcessorData {
142143
ap_tracking: ApTracking::default(),
143144
ids_data,
144145
accessible_scopes: vec![],
146+
constants: Default::default(),
145147
}
146148
}
147149
}
@@ -189,11 +191,11 @@ impl HintProcessorLogic for BuiltinHintProcessor {
189191
vm: &mut VirtualMachine,
190192
exec_scopes: &mut ExecutionScopes,
191193
hint_data: &Box<dyn Any>,
192-
constants: &HashMap<String, Felt252>,
193194
) -> Result<(), HintError> {
194195
let hint_data = hint_data
195196
.downcast_ref::<HintProcessorData>()
196197
.ok_or(HintError::WrongHintData)?;
198+
let constants = hint_data.constants.as_ref();
197199

198200
if let Some(hint_func) = self.extra_hints.get(&hint_data.code) {
199201
return hint_func.0(
@@ -1466,24 +1468,14 @@ mod tests {
14661468
let hint_data =
14671469
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
14681470
assert_matches!(
1469-
hint_processor.execute_hint(
1470-
&mut vm,
1471-
exec_scopes,
1472-
&any_box!(hint_data),
1473-
&HashMap::new(),
1474-
),
1471+
hint_processor.execute_hint(&mut vm, exec_scopes, &any_box!(hint_data)),
14751472
Ok(())
14761473
);
14771474
assert_eq!(exec_scopes.data.len(), 2);
14781475
let hint_data =
14791476
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
14801477
assert_matches!(
1481-
hint_processor.execute_hint(
1482-
&mut vm,
1483-
exec_scopes,
1484-
&any_box!(hint_data),
1485-
&HashMap::new(),
1486-
),
1478+
hint_processor.execute_hint(&mut vm, exec_scopes, &any_box!(hint_data)),
14871479
Ok(())
14881480
);
14891481
assert_eq!(exec_scopes.data.len(), 3);

vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::hint_processor_utils::*;
99
use crate::any_box;
1010
use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope;
1111
use crate::hint_processor::hint_processor_definition::HintReference;
12+
use crate::stdlib::rc::Rc;
1213
use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*};
1314
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
1415
use crate::vm::runners::cairo_runner::ResourceTracker;
@@ -1265,6 +1266,8 @@ impl HintProcessorLogic for Cairo1HintProcessor {
12651266
_references: &[HintReference],
12661267
// List of accessible scopes in the hint
12671268
_accessible_scopes: &[String],
1269+
// Identifiers stored in the hint's program.
1270+
_constants: Rc<HashMap<String, Felt252>>,
12681271
) -> Result<Box<dyn Any>, VirtualMachineError> {
12691272
let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned())
12701273
.ok_or_else(|| VirtualMachineError::CompileHintFail(
@@ -1284,8 +1287,6 @@ impl HintProcessorLogic for Cairo1HintProcessor {
12841287
exec_scopes: &mut ExecutionScopes,
12851288
//Data structure that can be downcasted to the structure generated by compile_hint
12861289
hint_data: &Box<dyn Any>,
1287-
//Constant values extracted from the program specification.
1288-
_constants: &HashMap<String, Felt252>,
12891290
) -> Result<(), HintError> {
12901291
let hints: &Vec<Hint> = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?;
12911292
for hint in hints {

vm/src/hint_processor/hint_processor_definition.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*};
1+
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*, rc::Rc};
22

33
use crate::any_box;
44
use crate::serde::deserialize_program::ApTracking;
@@ -27,8 +27,6 @@ pub trait HintProcessorLogic {
2727
exec_scopes: &mut ExecutionScopes,
2828
//Data structure that can be downcasted to the structure generated by compile_hint
2929
hint_data: &Box<dyn Any>,
30-
//Constant values extracted from the program specification.
31-
constants: &HashMap<String, Felt252>,
3230
) -> Result<(), HintError>;
3331

3432
//Transforms hint data outputed by the VM into whichever format will be later used by execute_hint
@@ -45,12 +43,15 @@ pub trait HintProcessorLogic {
4543
references: &[HintReference],
4644
// List of accessible scopes in the hint
4745
accessible_scopes: &[String],
46+
// Identifiers stored in the hint's program.
47+
constants: Rc<HashMap<String, Felt252>>,
4848
) -> Result<Box<dyn Any>, VirtualMachineError> {
4949
Ok(any_box!(HintProcessorData {
5050
code: hint_code.to_string(),
5151
ap_tracking: ap_tracking_data.clone(),
5252
ids_data: get_ids_data(reference_ids, references)?,
5353
accessible_scopes: accessible_scopes.to_vec(),
54+
constants,
5455
}))
5556
}
5657

@@ -65,10 +66,8 @@ pub trait HintProcessorLogic {
6566
exec_scopes: &mut ExecutionScopes,
6667
//Data structure that can be downcasted to the structure generated by compile_hint
6768
hint_data: &Box<dyn Any>,
68-
//Constant values extracted from the program specification.
69-
constants: &HashMap<String, Felt252>,
7069
) -> Result<HintExtension, HintError> {
71-
self.execute_hint(vm, exec_scopes, hint_data, constants)?;
70+
self.execute_hint(vm, exec_scopes, hint_data)?;
7271
Ok(HintExtension::default())
7372
}
7473
}

vm/src/tests/run_deprecated_contract_class_simplified.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ impl HintProcessorLogic for SimplifiedOsHintProcessor {
8080
_exec_scopes: &mut crate::types::exec_scope::ExecutionScopes,
8181
//Data structure that can be downcasted to the structure generated by compile_hint
8282
_hint_data: &Box<dyn core::any::Any>,
83-
//Constant values extracted from the program specification.
84-
_constants: &HashMap<String, Felt252>,
8583
) -> Result<(), crate::vm::errors::hint_errors::HintError> {
8684
// Empty impl as we are using `execute_hint_extensive` instead for this case
8785
Ok(())
@@ -93,19 +91,15 @@ impl HintProcessorLogic for SimplifiedOsHintProcessor {
9391
exec_scopes: &mut crate::types::exec_scope::ExecutionScopes,
9492
//Data structure that can be downcasted to the structure generated by compile_hint
9593
hint_data: &Box<dyn core::any::Any>,
96-
//Constant values extracted from the program specification.
97-
constants: &HashMap<String, Felt252>,
9894
) -> Result<
9995
crate::hint_processor::hint_processor_definition::HintExtension,
10096
crate::vm::errors::hint_errors::HintError,
10197
> {
10298
// First attempt to execute with builtin hint processor
103-
match self.builtin_hint_processor.execute_hint_extensive(
104-
vm,
105-
exec_scopes,
106-
hint_data,
107-
constants,
108-
) {
99+
match self
100+
.builtin_hint_processor
101+
.execute_hint_extensive(vm, exec_scopes, hint_data)
102+
{
109103
Err(HintError::UnknownHint(_)) => {}
110104
res => return res,
111105
}
@@ -307,6 +301,7 @@ pub fn vm_load_program(
307301
&reference_ids,
308302
&references,
309303
&accessible_scopes,
304+
Default::default(),
310305
)?;
311306
// Create the hint extension
312307
// As the hint from the compiled constract has offset 0, the hint pc will be equal to the loaded contract's program base:

vm/src/utils.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -474,35 +474,27 @@ pub mod test_utils {
474474

475475
macro_rules! run_hint {
476476
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{
477-
let hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
477+
let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
478+
let constants: &HashMap<String, Felt252> = $constants;
479+
hint_data.constants = crate::stdlib::rc::Rc::new(constants.clone());
478480
let mut hint_processor = BuiltinHintProcessor::new_empty();
479-
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants)
481+
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data))
480482
}};
481483
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr) => {{
482484
let hint_data = HintProcessorData::new_default(
483485
crate::stdlib::string::ToString::to_string($hint_code),
484486
$ids_data,
485487
);
486488
let mut hint_processor = BuiltinHintProcessor::new_empty();
487-
hint_processor.execute_hint(
488-
&mut $vm,
489-
$exec_scopes,
490-
&any_box!(hint_data),
491-
&crate::stdlib::collections::HashMap::new(),
492-
)
489+
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data))
493490
}};
494491
($vm:expr, $ids_data:expr, $hint_code:expr) => {{
495492
let hint_data = HintProcessorData::new_default(
496493
crate::stdlib::string::ToString::to_string($hint_code),
497494
$ids_data,
498495
);
499496
let mut hint_processor = BuiltinHintProcessor::new_empty();
500-
hint_processor.execute_hint(
501-
&mut $vm,
502-
exec_scopes_ref!(),
503-
&any_box!(hint_data),
504-
&crate::stdlib::collections::HashMap::new(),
505-
)
497+
hint_processor.execute_hint(&mut $vm, exec_scopes_ref!(), &any_box!(hint_data))
506498
}};
507499
}
508500
pub(crate) use run_hint;

vm/src/vm/runners/cairo_runner.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::vm::trace::trace_entry::TraceEntry;
2+
23
use crate::{
34
air_private_input::AirPrivateInput,
45
air_public_input::{PublicInput, PublicInputError},
@@ -8,6 +9,7 @@ use crate::{
89
collections::{BTreeMap, HashMap, HashSet},
910
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
1011
prelude::*,
12+
rc::Rc,
1113
},
1214
types::{builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName},
1315
vm::{
@@ -646,6 +648,8 @@ impl CairoRunner {
646648
references: &[HintReference],
647649
hint_executor: &mut dyn HintProcessor,
648650
) -> Result<Vec<Box<dyn Any>>, VirtualMachineError> {
651+
let constants = Rc::new(self.program.constants.clone());
652+
649653
self.program
650654
.shared_program_data
651655
.hints_collection
@@ -658,6 +662,7 @@ impl CairoRunner {
658662
&hint.flow_tracking_data.reference_ids,
659663
references,
660664
&hint.accessible_scopes,
665+
constants.clone(),
661666
)
662667
.map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into()))
663668
})
@@ -708,6 +713,7 @@ impl CairoRunner {
708713
.unwrap_or(&[]),
709714
#[cfg(feature = "extensive_hints")]
710715
&mut hint_ranges,
716+
#[cfg(feature = "test_utils")]
711717
&self.program.constants,
712718
)?;
713719

@@ -764,6 +770,7 @@ impl CairoRunner {
764770
hint_data,
765771
#[cfg(feature = "extensive_hints")]
766772
&mut hint_ranges,
773+
#[cfg(feature = "test_utils")]
767774
&self.program.constants,
768775
)?;
769776
}

0 commit comments

Comments
 (0)