Skip to content
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

* breaking: Store constants in Hint Data [#2191](https://github.com/lambdaclass/cairo-vm/pull/2191)

#### [3.0.0-rc.3] - 2025-26-08

* chore: Bump types-rs to 0.2.0 [#2183](https://github.com/lambdaclass/cairo-vm/pull/2183)
Expand Down
13 changes: 3 additions & 10 deletions hint_accountant/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,11 @@ fn run() {
}
let mut vm = VirtualMachine::new(false, false);
let mut hint_executor = BuiltinHintProcessor::new_empty();
let (
ap_tracking_data,
reference_ids,
references,
mut exec_scopes,
constants,
accessible_scopes,
) = (
let (ap_tracking_data, reference_ids, references, mut exec_scopes, accessible_scopes) = (
ApTracking::default(),
HashMap::new(),
Vec::new(),
ExecutionScopes::new(),
HashMap::new(),
Vec::new(),
);
let missing_hints: HashSet<_> = whitelists
Expand All @@ -78,10 +70,11 @@ fn run() {
&reference_ids,
&references,
&accessible_scopes,
Default::default(),
)
.expect("this implementation is infallible");
matches!(
hint_executor.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants,),
hint_executor.execute_hint(&mut vm, &mut exec_scopes, &hint_data),
Err(HintError::UnknownHint(_)),
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub struct HintProcessorData {
pub ap_tracking: ApTracking,
pub ids_data: HashMap<String, HintReference>,
pub accessible_scopes: Vec<String>,
pub constants: Rc<HashMap<String, Felt252>>,
}

impl HintProcessorData {
Expand All @@ -142,6 +143,7 @@ impl HintProcessorData {
ap_tracking: ApTracking::default(),
ids_data,
accessible_scopes: vec![],
constants: Default::default(),
}
}
}
Expand Down Expand Up @@ -189,11 +191,11 @@ impl HintProcessorLogic for BuiltinHintProcessor {
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
hint_data: &Box<dyn Any>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let hint_data = hint_data
.downcast_ref::<HintProcessorData>()
.ok_or(HintError::WrongHintData)?;
let constants = hint_data.constants.as_ref();

if let Some(hint_func) = self.extra_hints.get(&hint_data.code) {
return hint_func.0(
Expand Down Expand Up @@ -1466,24 +1468,14 @@ mod tests {
let hint_data =
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
assert_matches!(
hint_processor.execute_hint(
&mut vm,
exec_scopes,
&any_box!(hint_data),
&HashMap::new(),
),
hint_processor.execute_hint(&mut vm, exec_scopes, &any_box!(hint_data)),
Ok(())
);
assert_eq!(exec_scopes.data.len(), 2);
let hint_data =
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
assert_matches!(
hint_processor.execute_hint(
&mut vm,
exec_scopes,
&any_box!(hint_data),
&HashMap::new(),
),
hint_processor.execute_hint(&mut vm, exec_scopes, &any_box!(hint_data)),
Ok(())
);
assert_eq!(exec_scopes.data.len(), 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::hint_processor_utils::*;
use crate::any_box;
use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::stdlib::rc::Rc;
use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*};
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
use crate::vm::runners::cairo_runner::ResourceTracker;
Expand Down Expand Up @@ -1265,6 +1266,8 @@ impl HintProcessorLogic for Cairo1HintProcessor {
_references: &[HintReference],
// List of accessible scopes in the hint
_accessible_scopes: &[String],
// Identifiers stored in the hint's program.
_constants: Rc<HashMap<String, Felt252>>,
) -> Result<Box<dyn Any>, VirtualMachineError> {
let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned())
.ok_or_else(|| VirtualMachineError::CompileHintFail(
Expand All @@ -1284,8 +1287,6 @@ impl HintProcessorLogic for Cairo1HintProcessor {
exec_scopes: &mut ExecutionScopes,
//Data structure that can be downcasted to the structure generated by compile_hint
hint_data: &Box<dyn Any>,
//Constant values extracted from the program specification.
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let hints: &Vec<Hint> = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?;
for hint in hints {
Expand Down
11 changes: 5 additions & 6 deletions vm/src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*};
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*, rc::Rc};

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

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

Expand All @@ -65,10 +66,8 @@ pub trait HintProcessorLogic {
exec_scopes: &mut ExecutionScopes,
//Data structure that can be downcasted to the structure generated by compile_hint
hint_data: &Box<dyn Any>,
//Constant values extracted from the program specification.
constants: &HashMap<String, Felt252>,
) -> Result<HintExtension, HintError> {
self.execute_hint(vm, exec_scopes, hint_data, constants)?;
self.execute_hint(vm, exec_scopes, hint_data)?;
Ok(HintExtension::default())
}
}
Expand Down
15 changes: 5 additions & 10 deletions vm/src/tests/run_deprecated_contract_class_simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ impl HintProcessorLogic for SimplifiedOsHintProcessor {
_exec_scopes: &mut crate::types::exec_scope::ExecutionScopes,
//Data structure that can be downcasted to the structure generated by compile_hint
_hint_data: &Box<dyn core::any::Any>,
//Constant values extracted from the program specification.
_constants: &HashMap<String, Felt252>,
) -> Result<(), crate::vm::errors::hint_errors::HintError> {
// Empty impl as we are using `execute_hint_extensive` instead for this case
Ok(())
Expand All @@ -93,19 +91,15 @@ impl HintProcessorLogic for SimplifiedOsHintProcessor {
exec_scopes: &mut crate::types::exec_scope::ExecutionScopes,
//Data structure that can be downcasted to the structure generated by compile_hint
hint_data: &Box<dyn core::any::Any>,
//Constant values extracted from the program specification.
constants: &HashMap<String, Felt252>,
) -> Result<
crate::hint_processor::hint_processor_definition::HintExtension,
crate::vm::errors::hint_errors::HintError,
> {
// First attempt to execute with builtin hint processor
match self.builtin_hint_processor.execute_hint_extensive(
vm,
exec_scopes,
hint_data,
constants,
) {
match self
.builtin_hint_processor
.execute_hint_extensive(vm, exec_scopes, hint_data)
{
Err(HintError::UnknownHint(_)) => {}
res => return res,
}
Expand Down Expand Up @@ -307,6 +301,7 @@ pub fn vm_load_program(
&reference_ids,
&references,
&accessible_scopes,
Default::default(),
)?;
// Create the hint extension
// As the hint from the compiled constract has offset 0, the hint pc will be equal to the loaded contract's program base:
Expand Down
20 changes: 6 additions & 14 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,35 +474,27 @@ pub mod test_utils {

macro_rules! run_hint {
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{
let hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
let constants: &HashMap<String, Felt252> = $constants;
hint_data.constants = crate::stdlib::rc::Rc::new(constants.clone());
let mut hint_processor = BuiltinHintProcessor::new_empty();
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants)
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data))
}};
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr) => {{
let hint_data = HintProcessorData::new_default(
crate::stdlib::string::ToString::to_string($hint_code),
$ids_data,
);
let mut hint_processor = BuiltinHintProcessor::new_empty();
hint_processor.execute_hint(
&mut $vm,
$exec_scopes,
&any_box!(hint_data),
&crate::stdlib::collections::HashMap::new(),
)
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data))
}};
($vm:expr, $ids_data:expr, $hint_code:expr) => {{
let hint_data = HintProcessorData::new_default(
crate::stdlib::string::ToString::to_string($hint_code),
$ids_data,
);
let mut hint_processor = BuiltinHintProcessor::new_empty();
hint_processor.execute_hint(
&mut $vm,
exec_scopes_ref!(),
&any_box!(hint_data),
&crate::stdlib::collections::HashMap::new(),
)
hint_processor.execute_hint(&mut $vm, exec_scopes_ref!(), &any_box!(hint_data))
}};
}
pub(crate) use run_hint;
Expand Down
7 changes: 7 additions & 0 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::vm::trace::trace_entry::TraceEntry;

use crate::{
air_private_input::AirPrivateInput,
air_public_input::{PublicInput, PublicInputError},
Expand All @@ -8,6 +9,7 @@ use crate::{
collections::{BTreeMap, HashMap, HashSet},
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
prelude::*,
rc::Rc,
},
types::{builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName},
vm::{
Expand Down Expand Up @@ -646,6 +648,8 @@ impl CairoRunner {
references: &[HintReference],
hint_executor: &mut dyn HintProcessor,
) -> Result<Vec<Box<dyn Any>>, VirtualMachineError> {
let constants = Rc::new(self.program.constants.clone());

self.program
.shared_program_data
.hints_collection
Expand All @@ -658,6 +662,7 @@ impl CairoRunner {
&hint.flow_tracking_data.reference_ids,
references,
&hint.accessible_scopes,
constants.clone(),
)
.map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into()))
})
Expand Down Expand Up @@ -708,6 +713,7 @@ impl CairoRunner {
.unwrap_or(&[]),
#[cfg(feature = "extensive_hints")]
&mut hint_ranges,
#[cfg(feature = "test_utils")]
&self.program.constants,
)?;

Expand Down Expand Up @@ -764,6 +770,7 @@ impl CairoRunner {
hint_data,
#[cfg(feature = "extensive_hints")]
&mut hint_ranges,
#[cfg(feature = "test_utils")]
&self.program.constants,
)?;
}
Expand Down
Loading
Loading