Skip to content

Commit fd0e950

Browse files
Fix lookup to use accessible scopes
1 parent dfa4ee8 commit fd0e950

File tree

12 files changed

+240
-104
lines changed

12 files changed

+240
-104
lines changed

vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ impl HintProcessorLogic for BuiltinHintProcessor {
196196
.downcast_ref::<HintProcessorData>()
197197
.ok_or(HintError::WrongHintData)?;
198198
let constants = hint_data.constants.as_ref();
199+
let accessible_scopes = hint_data.accessible_scopes.as_slice();
199200

200201
if let Some(hint_func) = self.extra_hints.get(&hint_data.code) {
201202
return hint_func.0(
@@ -223,9 +224,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
223224
hint_code::ASSERT_LE_FELT_EXCLUDED_1 => assert_le_felt_excluded_1(vm, exec_scopes),
224225
hint_code::ASSERT_LE_FELT_EXCLUDED_0 => assert_le_felt_excluded_0(vm, exec_scopes),
225226
hint_code::IS_LE_FELT => is_le_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking),
226-
hint_code::ASSERT_250_BITS => {
227-
assert_250_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
228-
}
227+
hint_code::ASSERT_250_BITS => assert_250_bit(
228+
vm,
229+
&hint_data.ids_data,
230+
&hint_data.ap_tracking,
231+
constants,
232+
accessible_scopes,
233+
),
229234
hint_code::IS_250_BITS => is_250_bits(vm, &hint_data.ids_data, &hint_data.ap_tracking),
230235
hint_code::IS_ADDR_BOUNDED => {
231236
is_addr_bounded(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
@@ -267,9 +272,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
267272
&hint_data.ap_tracking,
268273
"continue_loop",
269274
),
270-
hint_code::SPLIT_FELT => {
271-
split_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
272-
}
275+
hint_code::SPLIT_FELT => split_felt(
276+
vm,
277+
&hint_data.ids_data,
278+
&hint_data.ap_tracking,
279+
constants,
280+
accessible_scopes,
281+
),
273282
hint_code::UNSIGNED_DIV_REM => {
274283
unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking)
275284
}
@@ -666,12 +675,14 @@ impl HintProcessorLogic for BuiltinHintProcessor {
666675
&hint_data.ids_data,
667676
&hint_data.ap_tracking,
668677
constants,
678+
accessible_scopes,
669679
),
670680
hint_code::SHA256_MAIN_ARBITRARY_INPUT_LENGTH => sha256_main_arbitrary_input_length(
671681
vm,
672682
&hint_data.ids_data,
673683
&hint_data.ap_tracking,
674684
constants,
685+
accessible_scopes,
675686
),
676687
hint_code::SHA256_INPUT => {
677688
sha256_input(vm, &hint_data.ids_data, &hint_data.ap_tracking)
@@ -798,9 +809,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
798809
hint_code::UINT384_SPLIT_128 => {
799810
uint384_split_128(vm, &hint_data.ids_data, &hint_data.ap_tracking)
800811
}
801-
hint_code::ADD_NO_UINT384_CHECK => {
802-
add_no_uint384_check(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
803-
}
812+
hint_code::ADD_NO_UINT384_CHECK => add_no_uint384_check(
813+
vm,
814+
&hint_data.ids_data,
815+
&hint_data.ap_tracking,
816+
constants,
817+
accessible_scopes,
818+
),
804819
hint_code::UINT384_SQRT => {
805820
uint384_sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking)
806821
}
@@ -892,6 +907,7 @@ impl HintProcessorLogic for BuiltinHintProcessor {
892907
&hint_data.ids_data,
893908
&hint_data.ap_tracking,
894909
constants,
910+
accessible_scopes,
895911
exec_scopes,
896912
),
897913
#[cfg(feature = "cairo-0-secp-hints")]
@@ -909,6 +925,7 @@ impl HintProcessorLogic for BuiltinHintProcessor {
909925
&hint_data.ids_data,
910926
&hint_data.ap_tracking,
911927
constants,
928+
accessible_scopes,
912929
),
913930
#[cfg(feature = "cairo-0-secp-hints")]
914931
cairo0_hints::SECP_DOUBLE_ASSIGN_NEW_X => cairo0_hints::secp_double_assign_new_x(

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ pub fn u64_array_to_mayberelocatable_vec(array: &[u64]) -> Vec<MaybeRelocatable>
365365
#[cfg(test)]
366366
mod tests {
367367
use super::*;
368-
use crate::stdlib::string::ToString;
369368

370369
use crate::{
371370
any_box,
@@ -457,7 +456,8 @@ mod tests {
457456
&[(KECCAK_FULL_RATE_IN_BYTES_CAIRO_KECCAK, Felt252::from(136))]
458457
.into_iter()
459458
.map(|(k, v)| (k.to_string(), v))
460-
.collect()
459+
.collect(),
460+
&["starkware.cairo.common.cairo_keccak.keccak".to_string()]
461461
),
462462
Ok(())
463463
);
@@ -486,7 +486,8 @@ mod tests {
486486
&[(KECCAK_FULL_RATE_IN_BYTES_CAIRO_KECCAK, Felt252::from(136))]
487487
.into_iter()
488488
.map(|(k, v)| (k.to_string(), v))
489-
.collect()
489+
.collect(),
490+
&["starkware.cairo.common.cairo_keccak.keccak".to_string()]
490491
),
491492
Ok(())
492493
);
@@ -514,7 +515,8 @@ mod tests {
514515
&[(KECCAK_FULL_RATE_IN_BYTES_CAIRO_KECCAK, Felt252::from(136))]
515516
.into_iter()
516517
.map(|(k, v)| (k.to_string(), v))
517-
.collect()
518+
.collect(),
519+
&["starkware.cairo.common.cairo_keccak.keccak".to_string()]
518520
),
519521
Ok(())
520522
);

vm/src/hint_processor/builtin_hint_processor/excess_balance.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,14 @@ pub fn excess_balance_hint(
263263
ids_data: &HashMap<String, HintReference>,
264264
ap_tracking: &ApTracking,
265265
constants: &HashMap<String, Felt252>,
266+
accessible_scopes: &[String],
266267
exec_scopes: &ExecutionScopes,
267268
) -> Result<(), HintError> {
268269
// Fetch constants & variables
269270
let margin_check_type =
270271
get_integer_from_var_name("margin_check_type", vm, ids_data, ap_tracking)?;
271-
let margin_check_initial = get_constant_from_var_name("MARGIN_CHECK_INITIAL", constants)?;
272+
let margin_check_initial =
273+
get_constant_from_var_name("MARGIN_CHECK_INITIAL", constants, accessible_scopes)?;
272274
let token_assets_value_d =
273275
get_integer_from_var_name("token_assets_value_d", vm, ids_data, ap_tracking)?;
274276
let account = get_integer_from_var_name("account", vm, ids_data, ap_tracking)?;
@@ -593,7 +595,10 @@ mod tests {
593595
// SETUP
594596
let mut vm = vm!();
595597
// CONSTANTS
596-
let constants = HashMap::from([("MARGIN_CHECK_INITIAL".to_string(), Felt252::ONE)]);
598+
let constants = HashMap::from([(
599+
"__main__.main.MARGIN_CHECK_INITIAL".to_string(),
600+
Felt252::ONE,
601+
)]);
597602
// IDS
598603
vm.segments = segments!(
599604
((1, 0), 1), // ids.margin_check_type
@@ -827,6 +832,7 @@ mod tests {
827832
&ids,
828833
&ApTracking::default(),
829834
&constants,
835+
&["__main__.main".to_string()],
830836
&exec_scopes
831837
)
832838
.is_ok());
@@ -935,7 +941,10 @@ mod tests {
935941
// SETUP
936942
let mut vm = vm!();
937943
// CONSTANTS
938-
let constants = HashMap::from([("MARGIN_CHECK_INITIAL".to_string(), Felt252::ONE)]);
944+
let constants = HashMap::from([(
945+
"__main__.main.MARGIN_CHECK_INITIAL".to_string(),
946+
Felt252::ONE,
947+
)]);
939948
// IDS
940949
vm.segments = segments!(
941950
((1, 0), 1), // ids.margin_check_type
@@ -1188,14 +1197,15 @@ mod tests {
11881197
exec_scopes.insert_value("dict_manager", Rc::new(RefCell::new(dict_manager)));
11891198

11901199
// EXECUTION
1191-
assert!(excess_balance_hint(
1200+
excess_balance_hint(
11921201
&mut vm,
11931202
&ids,
11941203
&ApTracking::default(),
11951204
&constants,
1196-
&exec_scopes
1205+
&["__main__.main".to_string()],
1206+
&exec_scopes,
11971207
)
1198-
.is_ok());
1208+
.unwrap();
11991209

12001210
// CHECK MEMORY VALUES
12011211
check_memory![

vm/src/hint_processor/builtin_hint_processor/hint_utils.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ pub fn get_reference_from_var_name<'a>(
177177
pub fn get_constant_from_var_name<'a>(
178178
var_name: &'static str,
179179
constants: &'a HashMap<String, Felt252>,
180+
accessible_scopes: &[String],
180181
) -> Result<&'a Felt252, HintError> {
181-
constants
182+
accessible_scopes
182183
.iter()
183-
.find(|(k, _)| k.rsplit('.').next() == Some(var_name))
184-
.map(|(_, n)| n)
184+
.rev()
185+
.find_map(|scope| constants.get(&format!("{}.{}", scope, var_name)))
185186
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name)))
186187
}
187188

vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ mod tests {
363363
ids_data,
364364
hint_code::SPLIT_N_BYTES,
365365
exec_scopes_ref!(),
366-
&HashMap::from([(String::from(BYTES_IN_WORD), Felt252::from(8))])
366+
&HashMap::from([(String::from(BYTES_IN_WORD), Felt252::from(8))]),
367+
&["starkware.cairo.common.builtin_keccak.keccak".to_string()]
367368
),
368369
Ok(())
369370
);
@@ -382,7 +383,8 @@ mod tests {
382383
ids_data,
383384
hint_code::SPLIT_OUTPUT_MID_LOW_HIGH,
384385
exec_scopes_ref!(),
385-
&HashMap::from([(String::from(BYTES_IN_WORD), Felt252::from(8))])
386+
&HashMap::from([(String::from(BYTES_IN_WORD), Felt252::from(8))]),
387+
&["starkware.cairo.common.builtin_keccak.keccak".to_string()]
386388
),
387389
Ok(())
388390
);

0 commit comments

Comments
 (0)