From 95561af50ffda83272e7a053bd15a06de10a39ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 10:14:20 -0300 Subject: [PATCH 1/8] Add identifiers to hint_processor --- hint_accountant/src/main.rs | 1 + .../builtin_hint_processor_definition.rs | 3 +++ .../hint_processor/cairo_1_hint_processor/hint_processor.rs | 3 +++ vm/src/hint_processor/hint_processor_definition.rs | 5 ++++- vm/src/tests/run_deprecated_contract_class_simplified.rs | 1 + vm/src/vm/runners/cairo_runner.rs | 1 + 6 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hint_accountant/src/main.rs b/hint_accountant/src/main.rs index 83800a423d..345dc5dbd6 100644 --- a/hint_accountant/src/main.rs +++ b/hint_accountant/src/main.rs @@ -78,6 +78,7 @@ fn run() { &reference_ids, &references, &accessible_scopes, + &HashMap::default(), ) .expect("this implementation is infallible"); matches!( diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index def35751ef..20e3b85539 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -23,6 +23,7 @@ use super::{ pack::*, }, }; +use crate::serde::deserialize_program::Identifier; use crate::Felt252; use crate::{ hint_processor::builtin_hint_processor::secp::secp_utils::{SECP256R1_ALPHA, SECP256R1_P}, @@ -133,6 +134,7 @@ pub struct HintProcessorData { pub ap_tracking: ApTracking, pub ids_data: HashMap, pub accessible_scopes: Vec, + pub identifiers: HashMap, } impl HintProcessorData { @@ -142,6 +144,7 @@ impl HintProcessorData { ap_tracking: ApTracking::default(), ids_data, accessible_scopes: vec![], + identifiers: HashMap::new(), } } } diff --git a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index e79fcdbfab..924ff972da 100644 --- a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -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::serde::deserialize_program::Identifier; use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::runners::cairo_runner::ResourceTracker; @@ -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. + _identifiers: &HashMap, ) -> Result, VirtualMachineError> { let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned()) .ok_or_else(|| VirtualMachineError::CompileHintFail( diff --git a/vm/src/hint_processor/hint_processor_definition.rs b/vm/src/hint_processor/hint_processor_definition.rs index dbd4a8389c..e5b2052cfe 100644 --- a/vm/src/hint_processor/hint_processor_definition.rs +++ b/vm/src/hint_processor/hint_processor_definition.rs @@ -1,9 +1,9 @@ use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*}; use crate::any_box; -use crate::serde::deserialize_program::ApTracking; use crate::serde::deserialize_program::OffsetValue; use crate::serde::deserialize_program::Reference; +use crate::serde::deserialize_program::{ApTracking, Identifier}; use crate::types::exec_scope::ExecutionScopes; use crate::types::instruction::Register; use crate::types::relocatable::Relocatable; @@ -45,12 +45,15 @@ pub trait HintProcessorLogic { references: &[HintReference], // List of accessible scopes in the hint accessible_scopes: &[String], + // Identifiers stored in the hint's program. + identifiers: &HashMap, ) -> Result, 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(), + identifiers: identifiers.clone(), })) } diff --git a/vm/src/tests/run_deprecated_contract_class_simplified.rs b/vm/src/tests/run_deprecated_contract_class_simplified.rs index d3c07b02a2..ce0e225d24 100644 --- a/vm/src/tests/run_deprecated_contract_class_simplified.rs +++ b/vm/src/tests/run_deprecated_contract_class_simplified.rs @@ -307,6 +307,7 @@ pub fn vm_load_program( &reference_ids, &references, &accessible_scopes, + &HashMap::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: diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 435671c05e..5b466ddda4 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -658,6 +658,7 @@ impl CairoRunner { &hint.flow_tracking_data.reference_ids, references, &hint.accessible_scopes, + &self.program.shared_program_data.identifiers, ) .map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into())) }) From b46e35ff6dcf82518f764abaf70908fa28eadd47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 10:46:26 -0300 Subject: [PATCH 2/8] Use identifiers in split_felt --- .../builtin_hint_processor_definition.rs | 9 ++++++--- .../builtin_hint_processor/math_utils.rs | 12 ++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 20e3b85539..035e038fc0 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -268,9 +268,12 @@ impl HintProcessorLogic for BuiltinHintProcessor { &hint_data.ap_tracking, "continue_loop", ), - hint_code::SPLIT_FELT => { - split_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) - } + hint_code::SPLIT_FELT => split_felt( + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + &hint_data.identifiers, + ), hint_code::UNSIGNED_DIV_REM => { unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) } diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index eb65aed496..fa8bf0ad70 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -1,8 +1,9 @@ use crate::{ hint_processor::builtin_hint_processor::hint_utils::get_constant_from_var_name, math_utils::signed_felt, + serde::deserialize_program::Identifier, stdlib::{boxed::Box, collections::HashMap, prelude::*}, - types::errors::math_errors::MathError, + types::{errors::math_errors::MathError, program::Program}, }; use lazy_static::lazy_static; use num_traits::{Signed, Zero}; @@ -384,15 +385,18 @@ pub fn split_felt( vm: &mut VirtualMachine, ids_data: &HashMap, ap_tracking: &ApTracking, - constants: &HashMap, + identifiers: &HashMap, ) -> Result<(), HintError> { let assert = |b: bool, msg: &str| { b.then_some(()) .ok_or_else(|| HintError::AssertionFailed(msg.to_string().into_boxed_str())) }; let bound = pow2_const(128); - let max_high = get_constant_from_var_name("MAX_HIGH", constants)?; - let max_low = get_constant_from_var_name("MAX_LOW", constants)?; + + let constants = Program::extract_constants(identifiers).unwrap(); + let max_high = get_constant_from_var_name("MAX_HIGH", &constants)?; + let max_low = get_constant_from_var_name("MAX_LOW", &constants)?; + assert( max_high < &bound && max_low < &bound, "assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128", From 36214f3dd278256933eb93ba51042cee2b289a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 10:46:39 -0300 Subject: [PATCH 3/8] Fix tests --- .../builtin_hint_processor/math_utils.rs | 268 ++++++++++++++---- vm/src/utils.rs | 6 + 2 files changed, 225 insertions(+), 49 deletions(-) diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index fa8bf0ad70..0d6737e325 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -2066,6 +2066,34 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); + let identifiers = HashMap::from([ + ( + "MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(felt_str!("10633823966279327296825105735305134080")), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); //Execute the hint assert_matches!( run_hint!( @@ -2073,13 +2101,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080") - ) - ]) + &HashMap::default(), + identifiers ), Ok(()) ); @@ -2106,6 +2129,36 @@ mod tests { //Create incomplete ids //Create ids_data & hint_data let ids_data = ids_data!["low"]; + + let identifiers = HashMap::from([ + ( + "MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(felt_str!("10633823966279327296825105735305134080")), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); + //Execute the hint assert_matches!( run_hint!( @@ -2113,13 +2166,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080") - ) - ]) + &HashMap::default(), + identifiers ), Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "value" ); @@ -2151,6 +2199,35 @@ mod tests { ), ]); + let identifiers = HashMap::from([ + ( + "MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(felt_str!("10633823966279327296825105735305134080")), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); + //Execute the hint assert_matches!( run_hint!( @@ -2158,13 +2235,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080") - ) - ]) + &HashMap::default(), + identifiers ), Err(HintError::Memory( MemoryError::InconsistentMemory(bx) @@ -2200,6 +2272,36 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); + + let identifiers = HashMap::from([ + ( + "MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(felt_str!("10633823966279327296825105735305134080")), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); + //Execute the hint assert_matches!( run_hint!( @@ -2207,13 +2309,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080") - ) - ]) + &HashMap::default(), + identifiers ), Err(HintError::Memory( MemoryError::InconsistentMemory(bx) @@ -2244,6 +2341,34 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); + let identifiers = HashMap::from([ + ( + "MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(felt_str!("10633823966279327296825105735305134080")), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); //Execute the hint assert_matches!( run_hint!( @@ -2251,13 +2376,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080") - ) - ]) + &HashMap::default(), + identifiers ), Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "value" ); @@ -2320,6 +2440,36 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); + + let identifiers = HashMap::from([ + ( + "a.MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::from(-1)), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "a.MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::from(-1)), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); + //Execute the hint assert_matches!( run_hint!( @@ -2327,13 +2477,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::from(-1)), - ( - "MAX_HIGH".to_string(), - Felt252::from(-1), - ) - ]) + &HashMap::default(), + identifiers ), Err(HintError::AssertionFailed(x)) if &(*x) == "assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128" ); @@ -2364,6 +2509,36 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); + + let identifiers = HashMap::from([ + ( + "MAX_LOW".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ( + "MAX_HIGH".to_string(), + Identifier { + pc: None, + type_: Some("const".to_string()), + value: Some(Felt252::ZERO), + full_name: None, + members: None, + cairo_type: None, + size: None, + destination: None, + }, + ), + ]); + //Execute the hint assert_matches!( run_hint!( @@ -2371,13 +2546,8 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - Felt252::ZERO, - ) - ]) + &HashMap::default(), + identifiers ), Err(HintError::AssertionFailed(x)) if &(*x) == "assert PRIME - 1 == ids.MAX_HIGH * 2**128 + ids.MAX_LOW" ); diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 6a6a284ee6..547c743e1b 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -473,6 +473,12 @@ pub mod test_utils { pub(crate) use exec_scopes_ref; macro_rules! run_hint { + ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr, $identifiers:expr) => {{ + let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data); + hint_data.identifiers = $identifiers; + let mut hint_processor = BuiltinHintProcessor::new_empty(); + hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants) + }}; ($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_processor = BuiltinHintProcessor::new_empty(); From ea72f4e0ca8447c338165f395521fb231957c6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 14:01:57 -0300 Subject: [PATCH 4/8] Use correct data type for new parameter --- hint_accountant/src/main.rs | 3 +- .../builtin_hint_processor_definition.rs | 7 +- .../builtin_hint_processor/math_utils.rs | 192 ++---------------- .../cairo_1_hint_processor/hint_processor.rs | 4 +- .../hint_processor_definition.rs | 8 +- ...un_deprecated_contract_class_simplified.rs | 4 +- vm/src/utils.rs | 2 +- vm/src/vm/runners/cairo_runner.rs | 6 +- 8 files changed, 40 insertions(+), 186 deletions(-) diff --git a/hint_accountant/src/main.rs b/hint_accountant/src/main.rs index 345dc5dbd6..3cbe4e2897 100644 --- a/hint_accountant/src/main.rs +++ b/hint_accountant/src/main.rs @@ -2,6 +2,7 @@ #![forbid(unsafe_code)] use std::fs::{self, File}; use std::io::BufReader; +use std::rc::Rc; use cairo_vm::stdlib::collections::{HashMap, HashSet}; use cairo_vm::{ @@ -78,7 +79,7 @@ fn run() { &reference_ids, &references, &accessible_scopes, - &HashMap::default(), + Rc::default(), ) .expect("this implementation is infallible"); matches!( diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 035e038fc0..ff7aa20f64 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -23,7 +23,6 @@ use super::{ pack::*, }, }; -use crate::serde::deserialize_program::Identifier; use crate::Felt252; use crate::{ hint_processor::builtin_hint_processor::secp::secp_utils::{SECP256R1_ALPHA, SECP256R1_P}, @@ -134,7 +133,7 @@ pub struct HintProcessorData { pub ap_tracking: ApTracking, pub ids_data: HashMap, pub accessible_scopes: Vec, - pub identifiers: HashMap, + pub constants: Rc>, } impl HintProcessorData { @@ -144,7 +143,7 @@ impl HintProcessorData { ap_tracking: ApTracking::default(), ids_data, accessible_scopes: vec![], - identifiers: HashMap::new(), + constants: Rc::default(), } } } @@ -272,7 +271,7 @@ impl HintProcessorLogic for BuiltinHintProcessor { vm, &hint_data.ids_data, &hint_data.ap_tracking, - &hint_data.identifiers, + &hint_data.constants, ), hint_code::UNSIGNED_DIV_REM => { unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index 0d6737e325..4ee5c94de5 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -1,9 +1,8 @@ use crate::{ hint_processor::builtin_hint_processor::hint_utils::get_constant_from_var_name, math_utils::signed_felt, - serde::deserialize_program::Identifier, stdlib::{boxed::Box, collections::HashMap, prelude::*}, - types::{errors::math_errors::MathError, program::Program}, + types::errors::math_errors::MathError, }; use lazy_static::lazy_static; use num_traits::{Signed, Zero}; @@ -385,7 +384,7 @@ pub fn split_felt( vm: &mut VirtualMachine, ids_data: &HashMap, ap_tracking: &ApTracking, - identifiers: &HashMap, + constants: &HashMap, ) -> Result<(), HintError> { let assert = |b: bool, msg: &str| { b.then_some(()) @@ -393,9 +392,8 @@ pub fn split_felt( }; let bound = pow2_const(128); - let constants = Program::extract_constants(identifiers).unwrap(); - let max_high = get_constant_from_var_name("MAX_HIGH", &constants)?; - let max_low = get_constant_from_var_name("MAX_LOW", &constants)?; + let max_high = get_constant_from_var_name("MAX_HIGH", constants)?; + let max_low = get_constant_from_var_name("MAX_LOW", constants)?; assert( max_high < &bound && max_low < &bound, @@ -756,6 +754,7 @@ pub fn split_xx( #[cfg(test)] mod tests { use super::*; + use crate::stdlib::rc::Rc; use crate::{felt_hex, felt_str}; use core::ops::Neg; @@ -2067,31 +2066,10 @@ mod tests { ), ]); let identifiers = HashMap::from([ - ( - "MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("MAX_LOW".to_string(), Felt252::ZERO), ( "MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(felt_str!("10633823966279327296825105735305134080")), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, + felt_str!("10633823966279327296825105735305134080"), ), ]); //Execute the hint @@ -2131,31 +2109,10 @@ mod tests { let ids_data = ids_data!["low"]; let identifiers = HashMap::from([ - ( - "MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("MAX_LOW".to_string(), Felt252::ZERO), ( "MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(felt_str!("10633823966279327296825105735305134080")), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, + felt_str!("10633823966279327296825105735305134080"), ), ]); @@ -2200,31 +2157,10 @@ mod tests { ]); let identifiers = HashMap::from([ - ( - "MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("MAX_LOW".to_string(), Felt252::ZERO), ( "MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(felt_str!("10633823966279327296825105735305134080")), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, + felt_str!("10633823966279327296825105735305134080"), ), ]); @@ -2274,31 +2210,10 @@ mod tests { ]); let identifiers = HashMap::from([ - ( - "MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("MAX_LOW".to_string(), Felt252::ZERO), ( "MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(felt_str!("10633823966279327296825105735305134080")), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, + felt_str!("10633823966279327296825105735305134080"), ), ]); @@ -2342,31 +2257,10 @@ mod tests { ), ]); let identifiers = HashMap::from([ - ( - "MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("MAX_LOW".to_string(), Felt252::ZERO), ( "MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(felt_str!("10633823966279327296825105735305134080")), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, + felt_str!("10633823966279327296825105735305134080"), ), ]); //Execute the hint @@ -2442,32 +2336,8 @@ mod tests { ]); let identifiers = HashMap::from([ - ( - "a.MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::from(-1)), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), - ( - "a.MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::from(-1)), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("a.MAX_LOW".to_string(), Felt252::from(-1)), + ("a.MAX_HIGH".to_string(), Felt252::from(-1)), ]); //Execute the hint @@ -2511,32 +2381,8 @@ mod tests { ]); let identifiers = HashMap::from([ - ( - "MAX_LOW".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), - ( - "MAX_HIGH".to_string(), - Identifier { - pc: None, - type_: Some("const".to_string()), - value: Some(Felt252::ZERO), - full_name: None, - members: None, - cairo_type: None, - size: None, - destination: None, - }, - ), + ("MAX_LOW".to_string(), Felt252::ZERO), + ("MAX_HIGH".to_string(), Felt252::ZERO), ]); //Execute the hint diff --git a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 924ff972da..2ab8d25e4c 100644 --- a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -9,7 +9,6 @@ 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::serde::deserialize_program::Identifier; use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*}; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::runners::cairo_runner::ResourceTracker; @@ -31,6 +30,7 @@ use cairo_lang_casm::{ }; use core::any::Any; use core::ops::Shl; +use std::rc::Rc; use num_bigint::{BigInt, BigUint}; use num_integer::{ExtendedGcd, Integer}; @@ -1267,7 +1267,7 @@ impl HintProcessorLogic for Cairo1HintProcessor { // List of accessible scopes in the hint _accessible_scopes: &[String], // Identifiers stored in the hint's program. - _identifiers: &HashMap, + _constants: Rc>, ) -> Result, VirtualMachineError> { let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned()) .ok_or_else(|| VirtualMachineError::CompileHintFail( diff --git a/vm/src/hint_processor/hint_processor_definition.rs b/vm/src/hint_processor/hint_processor_definition.rs index e5b2052cfe..e3f2728773 100644 --- a/vm/src/hint_processor/hint_processor_definition.rs +++ b/vm/src/hint_processor/hint_processor_definition.rs @@ -1,9 +1,11 @@ +use std::rc::Rc; + use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*}; use crate::any_box; +use crate::serde::deserialize_program::ApTracking; use crate::serde::deserialize_program::OffsetValue; use crate::serde::deserialize_program::Reference; -use crate::serde::deserialize_program::{ApTracking, Identifier}; use crate::types::exec_scope::ExecutionScopes; use crate::types::instruction::Register; use crate::types::relocatable::Relocatable; @@ -46,14 +48,14 @@ pub trait HintProcessorLogic { // List of accessible scopes in the hint accessible_scopes: &[String], // Identifiers stored in the hint's program. - identifiers: &HashMap, + constants: Rc>, ) -> Result, 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(), - identifiers: identifiers.clone(), + constants: constants.clone(), })) } diff --git a/vm/src/tests/run_deprecated_contract_class_simplified.rs b/vm/src/tests/run_deprecated_contract_class_simplified.rs index ce0e225d24..9ccc9ccb68 100644 --- a/vm/src/tests/run_deprecated_contract_class_simplified.rs +++ b/vm/src/tests/run_deprecated_contract_class_simplified.rs @@ -1,4 +1,6 @@ #![cfg(feature = "extensive_hints")] +use std::rc::Rc; + /* This file contains a test that runs the program: cairo_programs/starknet_os_deprecated_cc.cairo For testsing purposes, the contract ran by this program is hardcoded, with values taken from compiling: @@ -307,7 +309,7 @@ pub fn vm_load_program( &reference_ids, &references, &accessible_scopes, - &HashMap::default(), + Rc::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: diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 547c743e1b..bbaa5b4185 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -475,7 +475,7 @@ pub mod test_utils { macro_rules! run_hint { ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr, $identifiers:expr) => {{ let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data); - hint_data.identifiers = $identifiers; + hint_data.constants = Rc::new($identifiers); let mut hint_processor = BuiltinHintProcessor::new_empty(); hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants) }}; diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 5b466ddda4..81a72f907f 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,4 +1,6 @@ use crate::vm::trace::trace_entry::TraceEntry; +use std::rc::Rc; + use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, @@ -646,6 +648,8 @@ impl CairoRunner { references: &[HintReference], hint_executor: &mut dyn HintProcessor, ) -> Result>, VirtualMachineError> { + let constants = Rc::new(self.program.constants.clone()); + self.program .shared_program_data .hints_collection @@ -658,7 +662,7 @@ impl CairoRunner { &hint.flow_tracking_data.reference_ids, references, &hint.accessible_scopes, - &self.program.shared_program_data.identifiers, + constants.clone(), ) .map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into())) }) From 2c9919a04634eefc556950d2e793f19491e042ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 14:30:17 -0300 Subject: [PATCH 5/8] Replace constants everywhere --- .../builtin_hint_processor_definition.rs | 12 +- .../builtin_hint_processor/math_utils.rs | 118 ++++++++---------- vm/src/utils.rs | 17 +-- 3 files changed, 63 insertions(+), 84 deletions(-) diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index ff7aa20f64..9cc7dc17fb 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -191,11 +191,12 @@ impl HintProcessorLogic for BuiltinHintProcessor { vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, hint_data: &Box, - constants: &HashMap, + _constants: &HashMap, ) -> Result<(), HintError> { let hint_data = hint_data .downcast_ref::() .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( @@ -267,12 +268,9 @@ impl HintProcessorLogic for BuiltinHintProcessor { &hint_data.ap_tracking, "continue_loop", ), - hint_code::SPLIT_FELT => split_felt( - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - &hint_data.constants, - ), + hint_code::SPLIT_FELT => { + split_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants) + } hint_code::UNSIGNED_DIV_REM => { unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking) } diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index 4ee5c94de5..eb65aed496 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -391,10 +391,8 @@ pub fn split_felt( .ok_or_else(|| HintError::AssertionFailed(msg.to_string().into_boxed_str())) }; let bound = pow2_const(128); - let max_high = get_constant_from_var_name("MAX_HIGH", constants)?; let max_low = get_constant_from_var_name("MAX_LOW", constants)?; - assert( max_high < &bound && max_low < &bound, "assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128", @@ -754,7 +752,6 @@ pub fn split_xx( #[cfg(test)] mod tests { use super::*; - use crate::stdlib::rc::Rc; use crate::{felt_hex, felt_str}; use core::ops::Neg; @@ -2065,13 +2062,6 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); - let identifiers = HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080"), - ), - ]); //Execute the hint assert_matches!( run_hint!( @@ -2079,8 +2069,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::ZERO), + ( + "MAX_HIGH".to_string(), + felt_str!("10633823966279327296825105735305134080") + ) + ]) ), Ok(()) ); @@ -2107,15 +2102,6 @@ mod tests { //Create incomplete ids //Create ids_data & hint_data let ids_data = ids_data!["low"]; - - let identifiers = HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080"), - ), - ]); - //Execute the hint assert_matches!( run_hint!( @@ -2123,8 +2109,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::ZERO), + ( + "MAX_HIGH".to_string(), + felt_str!("10633823966279327296825105735305134080") + ) + ]) ), Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "value" ); @@ -2156,14 +2147,6 @@ mod tests { ), ]); - let identifiers = HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080"), - ), - ]); - //Execute the hint assert_matches!( run_hint!( @@ -2171,8 +2154,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::ZERO), + ( + "MAX_HIGH".to_string(), + felt_str!("10633823966279327296825105735305134080") + ) + ]) ), Err(HintError::Memory( MemoryError::InconsistentMemory(bx) @@ -2208,15 +2196,6 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); - - let identifiers = HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080"), - ), - ]); - //Execute the hint assert_matches!( run_hint!( @@ -2224,8 +2203,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::ZERO), + ( + "MAX_HIGH".to_string(), + felt_str!("10633823966279327296825105735305134080") + ) + ]) ), Err(HintError::Memory( MemoryError::InconsistentMemory(bx) @@ -2256,13 +2240,6 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); - let identifiers = HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ( - "MAX_HIGH".to_string(), - felt_str!("10633823966279327296825105735305134080"), - ), - ]); //Execute the hint assert_matches!( run_hint!( @@ -2270,8 +2247,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::ZERO), + ( + "MAX_HIGH".to_string(), + felt_str!("10633823966279327296825105735305134080") + ) + ]) ), Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "value" ); @@ -2334,12 +2316,6 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); - - let identifiers = HashMap::from([ - ("a.MAX_LOW".to_string(), Felt252::from(-1)), - ("a.MAX_HIGH".to_string(), Felt252::from(-1)), - ]); - //Execute the hint assert_matches!( run_hint!( @@ -2347,8 +2323,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::from(-1)), + ( + "MAX_HIGH".to_string(), + Felt252::from(-1), + ) + ]) ), Err(HintError::AssertionFailed(x)) if &(*x) == "assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128" ); @@ -2379,12 +2360,6 @@ mod tests { HintReference::new(-3, 1, true, true, true), ), ]); - - let identifiers = HashMap::from([ - ("MAX_LOW".to_string(), Felt252::ZERO), - ("MAX_HIGH".to_string(), Felt252::ZERO), - ]); - //Execute the hint assert_matches!( run_hint!( @@ -2392,8 +2367,13 @@ mod tests { ids_data, hint_code, exec_scopes_ref!(), - &HashMap::default(), - identifiers + &HashMap::from([ + ("MAX_LOW".to_string(), Felt252::ZERO), + ( + "MAX_HIGH".to_string(), + Felt252::ZERO, + ) + ]) ), Err(HintError::AssertionFailed(x)) if &(*x) == "assert PRIME - 1 == ids.MAX_HIGH * 2**128 + ids.MAX_LOW" ); diff --git a/vm/src/utils.rs b/vm/src/utils.rs index bbaa5b4185..d447d44993 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -473,16 +473,17 @@ pub mod test_utils { pub(crate) use exec_scopes_ref; macro_rules! run_hint { - ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr, $identifiers:expr) => {{ - let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data); - hint_data.constants = Rc::new($identifiers); - let mut hint_processor = BuiltinHintProcessor::new_empty(); - hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants) - }}; ($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 = $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), + &HashMap::default(), + ) }}; ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr) => {{ let hint_data = HintProcessorData::new_default( From 43a0a2941006285ec4d8d6559d9d9aec8250ecce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 15:22:43 -0300 Subject: [PATCH 6/8] Remove constants parameter --- hint_accountant/src/main.rs | 12 ++--------- .../builtin_hint_processor_definition.rs | 15 ++----------- .../cairo_1_hint_processor/hint_processor.rs | 2 -- .../hint_processor_definition.rs | 6 +----- ...un_deprecated_contract_class_simplified.rs | 14 ++++--------- vm/src/utils.rs | 21 +++---------------- vm/src/vm/runners/cairo_runner.rs | 2 ++ vm/src/vm/vm_core.rs | 17 +++++++++------ 8 files changed, 25 insertions(+), 64 deletions(-) diff --git a/hint_accountant/src/main.rs b/hint_accountant/src/main.rs index 3cbe4e2897..a6ad5dfda6 100644 --- a/hint_accountant/src/main.rs +++ b/hint_accountant/src/main.rs @@ -52,19 +52,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 @@ -83,7 +75,7 @@ fn run() { ) .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(_)), ) }) diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 9cc7dc17fb..23ae07d0ee 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -191,7 +191,6 @@ impl HintProcessorLogic for BuiltinHintProcessor { vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, hint_data: &Box, - _constants: &HashMap, ) -> Result<(), HintError> { let hint_data = hint_data .downcast_ref::() @@ -1469,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); diff --git a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 2ab8d25e4c..861a01934d 100644 --- a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -1287,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, - //Constant values extracted from the program specification. - _constants: &HashMap, ) -> Result<(), HintError> { let hints: &Vec = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?; for hint in hints { diff --git a/vm/src/hint_processor/hint_processor_definition.rs b/vm/src/hint_processor/hint_processor_definition.rs index e3f2728773..8bd4957171 100644 --- a/vm/src/hint_processor/hint_processor_definition.rs +++ b/vm/src/hint_processor/hint_processor_definition.rs @@ -29,8 +29,6 @@ pub trait HintProcessorLogic { exec_scopes: &mut ExecutionScopes, //Data structure that can be downcasted to the structure generated by compile_hint hint_data: &Box, - //Constant values extracted from the program specification. - constants: &HashMap, ) -> Result<(), HintError>; //Transforms hint data outputed by the VM into whichever format will be later used by execute_hint @@ -70,10 +68,8 @@ pub trait HintProcessorLogic { exec_scopes: &mut ExecutionScopes, //Data structure that can be downcasted to the structure generated by compile_hint hint_data: &Box, - //Constant values extracted from the program specification. - constants: &HashMap, ) -> Result { - self.execute_hint(vm, exec_scopes, hint_data, constants)?; + self.execute_hint(vm, exec_scopes, hint_data)?; Ok(HintExtension::default()) } } diff --git a/vm/src/tests/run_deprecated_contract_class_simplified.rs b/vm/src/tests/run_deprecated_contract_class_simplified.rs index 9ccc9ccb68..33248f208c 100644 --- a/vm/src/tests/run_deprecated_contract_class_simplified.rs +++ b/vm/src/tests/run_deprecated_contract_class_simplified.rs @@ -82,8 +82,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, - //Constant values extracted from the program specification. - _constants: &HashMap, ) -> Result<(), crate::vm::errors::hint_errors::HintError> { // Empty impl as we are using `execute_hint_extensive` instead for this case Ok(()) @@ -95,19 +93,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, - //Constant values extracted from the program specification. - constants: &HashMap, ) -> 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, } diff --git a/vm/src/utils.rs b/vm/src/utils.rs index d447d44993..6824252fe2 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -478,12 +478,7 @@ pub mod test_utils { let constants: &HashMap = $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), - &HashMap::default(), - ) + 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( @@ -491,12 +486,7 @@ pub mod test_utils { $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( @@ -504,12 +494,7 @@ pub mod test_utils { $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; diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 81a72f907f..2b626760ba 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -713,6 +713,7 @@ impl CairoRunner { .unwrap_or(&[]), #[cfg(feature = "extensive_hints")] &mut hint_ranges, + #[cfg(feature = "test_utils")] &self.program.constants, )?; @@ -769,6 +770,7 @@ impl CairoRunner { hint_data, #[cfg(feature = "extensive_hints")] &mut hint_ranges, + #[cfg(feature = "test_utils")] &self.program.constants, )?; } diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index c073b5a73a..8afc9628be 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -536,11 +536,10 @@ impl VirtualMachine { hint_processor: &mut dyn HintProcessor, exec_scopes: &mut ExecutionScopes, hint_datas: &[Box], - constants: &HashMap, ) -> Result<(), VirtualMachineError> { for (hint_index, hint_data) in hint_datas.iter().enumerate() { hint_processor - .execute_hint(self, exec_scopes, hint_data, constants) + .execute_hint(self, exec_scopes, hint_data) .map_err(|err| VirtualMachineError::Hint(Box::new((hint_index, err))))? } Ok(()) @@ -553,7 +552,6 @@ impl VirtualMachine { exec_scopes: &mut ExecutionScopes, hint_datas: &mut Vec>, hint_ranges: &mut HashMap, - constants: &HashMap, ) -> Result<(), VirtualMachineError> { // Check if there is a hint range for the current pc if let Some((s, l)) = hint_ranges.get(&self.run_context.pc) { @@ -566,7 +564,6 @@ impl VirtualMachine { self, exec_scopes, hint_datas.get(idx).ok_or(VirtualMachineError::Unexpected)?, - constants, ) .map_err(|err| VirtualMachineError::Hint(Box::new((idx - s, err))))?; // Update the hint_ranges & hint_datas with the hints added by the executed hint @@ -627,7 +624,7 @@ impl VirtualMachine { #[cfg(feature = "extensive_hints")] hint_datas: &mut Vec>, #[cfg(not(feature = "extensive_hints"))] hint_datas: &[Box], #[cfg(feature = "extensive_hints")] hint_ranges: &mut HashMap, - constants: &HashMap, + #[cfg(feature = "test_utils")] constants: &HashMap, ) -> Result<(), VirtualMachineError> { self.step_hint( hint_processor, @@ -635,7 +632,6 @@ impl VirtualMachine { hint_datas, #[cfg(feature = "extensive_hints")] hint_ranges, - constants, )?; #[cfg(feature = "test_utils")] @@ -3338,6 +3334,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new(), ), Ok(()) @@ -3575,6 +3572,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new(), ), Ok(()) @@ -3659,6 +3657,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new() ), Ok(()) @@ -3763,6 +3762,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new() ), Ok(()) @@ -3786,6 +3786,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new() ), Ok(()) @@ -3810,6 +3811,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new() ), Ok(()) @@ -4387,6 +4389,7 @@ mod tests { Relocatable::from((0, 0)), (0_usize, NonZeroUsize::new(1).unwrap()) )]), + #[cfg(feature = "test_utils")] &HashMap::new(), ), Ok(()) @@ -5332,6 +5335,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new() ), Ok(()) @@ -5419,6 +5423,7 @@ mod tests { &mut Vec::new(), #[cfg(feature = "extensive_hints")] &mut HashMap::new(), + #[cfg(feature = "test_utils")] &HashMap::new() ), Ok(()) From 392046d86caba21a91677dc0976aa275f11193a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 15:38:42 -0300 Subject: [PATCH 7/8] Minor improvements --- hint_accountant/src/main.rs | 3 +-- .../builtin_hint_processor_definition.rs | 6 +++--- .../hint_processor/cairo_1_hint_processor/hint_processor.rs | 2 +- vm/src/hint_processor/hint_processor_definition.rs | 6 ++---- vm/src/tests/run_deprecated_contract_class_simplified.rs | 4 +--- vm/src/vm/runners/cairo_runner.rs | 2 +- 6 files changed, 9 insertions(+), 14 deletions(-) diff --git a/hint_accountant/src/main.rs b/hint_accountant/src/main.rs index a6ad5dfda6..c694245a43 100644 --- a/hint_accountant/src/main.rs +++ b/hint_accountant/src/main.rs @@ -2,7 +2,6 @@ #![forbid(unsafe_code)] use std::fs::{self, File}; use std::io::BufReader; -use std::rc::Rc; use cairo_vm::stdlib::collections::{HashMap, HashSet}; use cairo_vm::{ @@ -71,7 +70,7 @@ fn run() { &reference_ids, &references, &accessible_scopes, - Rc::default(), + Default::default(), ) .expect("this implementation is infallible"); matches!( diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 23ae07d0ee..029b9dfa3e 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -143,7 +143,7 @@ impl HintProcessorData { ap_tracking: ApTracking::default(), ids_data, accessible_scopes: vec![], - constants: Rc::default(), + constants: Default::default(), } } } @@ -1468,14 +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),), + 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),), + hint_processor.execute_hint(&mut vm, exec_scopes, &any_box!(hint_data)), Ok(()) ); assert_eq!(exec_scopes.data.len(), 3); diff --git a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 861a01934d..4f5c99faec 100644 --- a/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -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; @@ -30,7 +31,6 @@ use cairo_lang_casm::{ }; use core::any::Any; use core::ops::Shl; -use std::rc::Rc; use num_bigint::{BigInt, BigUint}; use num_integer::{ExtendedGcd, Integer}; diff --git a/vm/src/hint_processor/hint_processor_definition.rs b/vm/src/hint_processor/hint_processor_definition.rs index 8bd4957171..293642b0b0 100644 --- a/vm/src/hint_processor/hint_processor_definition.rs +++ b/vm/src/hint_processor/hint_processor_definition.rs @@ -1,6 +1,4 @@ -use std::rc::Rc; - -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; @@ -53,7 +51,7 @@ pub trait HintProcessorLogic { ap_tracking: ap_tracking_data.clone(), ids_data: get_ids_data(reference_ids, references)?, accessible_scopes: accessible_scopes.to_vec(), - constants: constants.clone(), + constants, })) } diff --git a/vm/src/tests/run_deprecated_contract_class_simplified.rs b/vm/src/tests/run_deprecated_contract_class_simplified.rs index 33248f208c..888e9f35bd 100644 --- a/vm/src/tests/run_deprecated_contract_class_simplified.rs +++ b/vm/src/tests/run_deprecated_contract_class_simplified.rs @@ -1,6 +1,4 @@ #![cfg(feature = "extensive_hints")] -use std::rc::Rc; - /* This file contains a test that runs the program: cairo_programs/starknet_os_deprecated_cc.cairo For testsing purposes, the contract ran by this program is hardcoded, with values taken from compiling: @@ -303,7 +301,7 @@ pub fn vm_load_program( &reference_ids, &references, &accessible_scopes, - Rc::default(), + 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: diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 2b626760ba..439e3596f5 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,5 +1,4 @@ use crate::vm::trace::trace_entry::TraceEntry; -use std::rc::Rc; use crate::{ air_private_input::AirPrivateInput, @@ -10,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::{ From 52de1ab1b9abf9b535a31a79fd3dfa04abfd2b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 2 Sep 2025 17:05:28 -0300 Subject: [PATCH 8/8] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e763405473..4c29a4c8b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)