Skip to content

Commit e1f7c8a

Browse files
Modify functions that already used get_constant_from_var
1 parent 186ae4d commit e1f7c8a

File tree

7 files changed

+132
-80
lines changed

7 files changed

+132
-80
lines changed

vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,20 @@ impl HintProcessorLogic for BuiltinHintProcessor {
231231
exec_scopes,
232232
&hint_data.ids_data,
233233
&hint_data.ap_tracking,
234-
constants,
234+
&hint_data.identifiers,
235+
&hint_data.accessible_scopes,
235236
),
236237
hint_code::ASSERT_LE_FELT_EXCLUDED_2 => assert_le_felt_excluded_2(exec_scopes),
237238
hint_code::ASSERT_LE_FELT_EXCLUDED_1 => assert_le_felt_excluded_1(vm, exec_scopes),
238239
hint_code::ASSERT_LE_FELT_EXCLUDED_0 => assert_le_felt_excluded_0(vm, exec_scopes),
239240
hint_code::IS_LE_FELT => is_le_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking),
240-
hint_code::ASSERT_250_BITS => {
241-
assert_250_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
242-
}
241+
hint_code::ASSERT_250_BITS => assert_250_bit(
242+
vm,
243+
&hint_data.ids_data,
244+
&hint_data.ap_tracking,
245+
&hint_data.identifiers,
246+
&hint_data.accessible_scopes,
247+
),
243248
hint_code::IS_250_BITS => is_250_bits(vm, &hint_data.ids_data, &hint_data.ap_tracking),
244249
hint_code::IS_ADDR_BOUNDED => {
245250
is_addr_bounded(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
@@ -281,9 +286,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
281286
&hint_data.ap_tracking,
282287
"continue_loop",
283288
),
284-
hint_code::SPLIT_FELT => {
285-
split_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
286-
}
289+
hint_code::SPLIT_FELT => split_felt(
290+
vm,
291+
&hint_data.ids_data,
292+
&hint_data.ap_tracking,
293+
&hint_data.identifiers,
294+
&hint_data.accessible_scopes,
295+
),
287296
hint_code::UNSIGNED_DIV_REM => {
288297
unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking)
289298
}
@@ -679,13 +688,15 @@ impl HintProcessorLogic for BuiltinHintProcessor {
679688
vm,
680689
&hint_data.ids_data,
681690
&hint_data.ap_tracking,
682-
constants,
691+
identifiers,
692+
&hint_data.accessible_scopes,
683693
),
684694
hint_code::SHA256_MAIN_ARBITRARY_INPUT_LENGTH => sha256_main_arbitrary_input_length(
685695
vm,
686696
&hint_data.ids_data,
687697
&hint_data.ap_tracking,
688-
constants,
698+
identifiers,
699+
&hint_data.accessible_scopes,
689700
),
690701
hint_code::SHA256_INPUT => {
691702
sha256_input(vm, &hint_data.ids_data, &hint_data.ap_tracking)
@@ -812,9 +823,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
812823
hint_code::UINT384_SPLIT_128 => {
813824
uint384_split_128(vm, &hint_data.ids_data, &hint_data.ap_tracking)
814825
}
815-
hint_code::ADD_NO_UINT384_CHECK => {
816-
add_no_uint384_check(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
817-
}
826+
hint_code::ADD_NO_UINT384_CHECK => add_no_uint384_check(
827+
vm,
828+
&hint_data.ids_data,
829+
&hint_data.ap_tracking,
830+
&hint_data.identifiers,
831+
&hint_data.accessible_scopes,
832+
),
818833
hint_code::UINT384_SQRT => {
819834
uint384_sqrt(vm, &hint_data.ids_data, &hint_data.ap_tracking)
820835
}
@@ -905,7 +920,8 @@ impl HintProcessorLogic for BuiltinHintProcessor {
905920
vm,
906921
&hint_data.ids_data,
907922
&hint_data.ap_tracking,
908-
constants,
923+
identifiers,
924+
&hint_data.accessible_scopes,
909925
exec_scopes,
910926
),
911927
#[cfg(feature = "cairo-0-secp-hints")]
@@ -922,7 +938,8 @@ impl HintProcessorLogic for BuiltinHintProcessor {
922938
exec_scopes,
923939
&hint_data.ids_data,
924940
&hint_data.ap_tracking,
925-
constants,
941+
identifiers,
942+
&hint_data.accessible_scopes,
926943
),
927944
#[cfg(feature = "cairo-0-secp-hints")]
928945
cairo0_hints::SECP_DOUBLE_ASSIGN_NEW_X => cairo0_hints::secp_double_assign_new_x(

vm/src/hint_processor/builtin_hint_processor/excess_balance.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
hint_processor::hint_processor_definition::HintReference,
3-
serde::deserialize_program::ApTracking,
3+
serde::deserialize_program::{ApTracking, Identifier},
44
stdlib::collections::HashMap,
55
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
66
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
@@ -262,13 +262,15 @@ pub fn excess_balance_hint(
262262
vm: &mut VirtualMachine,
263263
ids_data: &HashMap<String, HintReference>,
264264
ap_tracking: &ApTracking,
265-
constants: &HashMap<String, Felt252>,
265+
identifiers: &HashMap<String, Identifier>,
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", identifiers, 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,11 @@ 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 identifiers = HashMap::from([(
599+
"__main__.MARGIN_CHECK_INITIAL".to_string(),
600+
const_identifier(1),
601+
)]);
602+
let accessible_scopes = vec!["__main__".to_string()];
597603
// IDS
598604
vm.segments = segments!(
599605
((1, 0), 1), // ids.margin_check_type
@@ -826,7 +832,8 @@ mod tests {
826832
&mut vm,
827833
&ids,
828834
&ApTracking::default(),
829-
&constants,
835+
&identifiers,
836+
&accessible_scopes,
830837
&exec_scopes
831838
)
832839
.is_ok());
@@ -935,7 +942,11 @@ mod tests {
935942
// SETUP
936943
let mut vm = vm!();
937944
// CONSTANTS
938-
let constants = HashMap::from([("MARGIN_CHECK_INITIAL".to_string(), Felt252::ONE)]);
945+
let identifiers = HashMap::from([(
946+
"__main__.MARGIN_CHECK_INITIAL".to_string(),
947+
const_identifier(1),
948+
)]);
949+
let accessible_scopes = vec!["__main__".to_string()];
939950
// IDS
940951
vm.segments = segments!(
941952
((1, 0), 1), // ids.margin_check_type
@@ -1192,7 +1203,8 @@ mod tests {
11921203
&mut vm,
11931204
&ids,
11941205
&ApTracking::default(),
1195-
&constants,
1206+
&identifiers,
1207+
&accessible_scopes,
11961208
&exec_scopes
11971209
)
11981210
.is_ok());

vm/src/hint_processor/builtin_hint_processor/hint_utils.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::hint_processor::hint_processor_utils::{
99
use crate::hint_processor::hint_processor_utils::{
1010
get_integer_from_reference, get_maybe_relocatable_from_reference,
1111
};
12-
use crate::serde::deserialize_program::ApTracking;
12+
use crate::serde::deserialize_program::{ApTracking, Identifier};
1313
use crate::types::relocatable::MaybeRelocatable;
1414
use crate::types::relocatable::Relocatable;
1515
use crate::vm::errors::hint_errors::HintError;
@@ -176,12 +176,20 @@ pub fn get_reference_from_var_name<'a>(
176176

177177
pub fn get_constant_from_var_name<'a>(
178178
var_name: &'static str,
179-
constants: &'a HashMap<String, Felt252>,
179+
identifiers: &'a HashMap<String, Identifier>,
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| {
186+
let identifier = identifiers.get(&format!("{}.{}", scope, var_name))?;
187+
if identifier.type_.as_ref()? == "const" {
188+
identifier.value.as_ref()
189+
} else {
190+
None
191+
}
192+
})
185193
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name)))
186194
}
187195

vm/src/hint_processor/builtin_hint_processor/math_utils.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
hint_processor::builtin_hint_processor::hint_utils::get_constant_from_var_name,
33
math_utils::signed_felt,
4+
serde::deserialize_program::Identifier,
45
stdlib::{boxed::Box, collections::HashMap, prelude::*},
56
types::errors::math_errors::MathError,
67
};
@@ -90,17 +91,13 @@ pub fn assert_le_felt(
9091
exec_scopes: &mut ExecutionScopes,
9192
ids_data: &HashMap<String, HintReference>,
9293
ap_tracking: &ApTracking,
93-
constants: &HashMap<String, Felt252>,
94+
identifiers: &HashMap<String, Identifier>,
95+
accessible_scopes: &[String],
9496
) -> Result<(), HintError> {
95-
const PRIME_OVER_3_HIGH: &str = "starkware.cairo.common.math.assert_le_felt.PRIME_OVER_3_HIGH";
96-
const PRIME_OVER_2_HIGH: &str = "starkware.cairo.common.math.assert_le_felt.PRIME_OVER_2_HIGH";
97-
98-
let prime_over_3_high = constants
99-
.get(PRIME_OVER_3_HIGH)
100-
.ok_or_else(|| HintError::MissingConstant(Box::new(PRIME_OVER_3_HIGH)))?;
101-
let prime_over_2_high = constants
102-
.get(PRIME_OVER_2_HIGH)
103-
.ok_or_else(|| HintError::MissingConstant(Box::new(PRIME_OVER_2_HIGH)))?;
97+
let prime_over_3_high =
98+
get_constant_from_var_name("PRIME_OVER_3_HIGH", identifiers, accessible_scopes)?;
99+
let prime_over_2_high =
100+
get_constant_from_var_name("PRIME_OVER_2_HIGH", identifiers, accessible_scopes)?;
104101
let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?.to_biguint();
105102
let b = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?.to_biguint();
106103
let range_check_ptr = get_ptr_from_var_name("range_check_ptr", vm, ids_data, ap_tracking)?;
@@ -384,15 +381,16 @@ pub fn split_felt(
384381
vm: &mut VirtualMachine,
385382
ids_data: &HashMap<String, HintReference>,
386383
ap_tracking: &ApTracking,
387-
constants: &HashMap<String, Felt252>,
384+
identifiers: &HashMap<String, Identifier>,
385+
accessible_scopes: &[String],
388386
) -> Result<(), HintError> {
389387
let assert = |b: bool, msg: &str| {
390388
b.then_some(())
391389
.ok_or_else(|| HintError::AssertionFailed(msg.to_string().into_boxed_str()))
392390
};
393391
let bound = pow2_const(128);
394-
let max_high = get_constant_from_var_name("MAX_HIGH", constants)?;
395-
let max_low = get_constant_from_var_name("MAX_LOW", constants)?;
392+
let max_high = get_constant_from_var_name("MAX_HIGH", identifiers, accessible_scopes)?;
393+
let max_low = get_constant_from_var_name("MAX_LOW", identifiers, accessible_scopes)?;
396394
assert(
397395
max_high < &bound && max_low < &bound,
398396
"assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128",
@@ -521,17 +519,12 @@ pub fn assert_250_bit(
521519
vm: &mut VirtualMachine,
522520
ids_data: &HashMap<String, HintReference>,
523521
ap_tracking: &ApTracking,
524-
constants: &HashMap<String, Felt252>,
522+
identifiers: &HashMap<String, Identifier>,
523+
accessible_scopes: &[String],
525524
) -> Result<(), HintError> {
526-
const UPPER_BOUND: &str = "starkware.cairo.common.math.assert_250_bit.UPPER_BOUND";
527-
const SHIFT: &str = "starkware.cairo.common.math.assert_250_bit.SHIFT";
528525
//Declare constant values
529-
let upper_bound = constants
530-
.get(UPPER_BOUND)
531-
.map_or_else(|| get_constant_from_var_name("UPPER_BOUND", constants), Ok)?;
532-
let shift = constants
533-
.get(SHIFT)
534-
.map_or_else(|| get_constant_from_var_name("SHIFT", constants), Ok)?;
526+
let upper_bound = get_constant_from_var_name("UPPER_BOUND", identifiers, accessible_scopes)?;
527+
let shift = get_constant_from_var_name("SHIFT", identifiers, accessible_scopes)?;
535528
let value = Felt252::from(&signed_felt(get_integer_from_var_name(
536529
"value",
537530
vm,

vm/src/hint_processor/builtin_hint_processor/secp/cairo0_hints.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use crate::stdlib::{
2-
collections::HashMap,
3-
ops::Deref,
4-
ops::{Add, Mul, Rem},
5-
prelude::*,
1+
use crate::{
2+
serde::deserialize_program::Identifier,
3+
stdlib::{
4+
collections::HashMap,
5+
ops::{Add, Deref, Mul, Rem},
6+
prelude::*,
7+
},
68
};
79

810
use crate::define_hint_string_map;
@@ -166,19 +168,13 @@ pub fn compute_ids_high_low(
166168
exec_scopes: &mut ExecutionScopes,
167169
ids_data: &HashMap<String, HintReference>,
168170
ap_tracking: &ApTracking,
169-
constants: &HashMap<String, Felt252>,
171+
identifiers: &HashMap<String, Identifier>,
172+
accessible_scopes: &[String],
170173
) -> Result<(), HintError> {
171174
exec_scopes.insert_value::<BigInt>("SECP256R1_P", SECP256R1_P.clone());
172175

173-
const UPPER_BOUND: &str = "starkware.cairo.common.secp256r1.field.assert_165_bit.UPPER_BOUND";
174-
const SHIFT: &str = "starkware.cairo.common.secp256r1.field.assert_165_bit.SHIFT";
175-
176-
let upper_bound = constants
177-
.get(UPPER_BOUND)
178-
.map_or_else(|| get_constant_from_var_name("UPPER_BOUND", constants), Ok)?;
179-
let shift = constants
180-
.get(SHIFT)
181-
.map_or_else(|| get_constant_from_var_name("SHIFT", constants), Ok)?;
176+
let upper_bound = get_constant_from_var_name("UPPER_BOUND", identifiers, accessible_scopes)?;
177+
let shift = get_constant_from_var_name("SHIFT", identifiers, accessible_scopes)?;
182178
let value = Felt252::from(&signed_felt(get_integer_from_var_name(
183179
"value",
184180
vm,
@@ -503,19 +499,21 @@ mod tests {
503499

504500
let mut exec_scopes = ExecutionScopes::new();
505501

506-
let constants = HashMap::from([
502+
let identifiers = HashMap::from([
507503
(
508-
"UPPER_BOUND".to_string(),
509-
Felt252::from(18446744069414584321_u128),
504+
"__main__.UPPER_BOUND".to_string(),
505+
const_identifier(18446744069414584321_u128),
510506
),
511-
("SHIFT".to_string(), Felt252::from(12)),
507+
("__main__.SHIFT".to_string(), const_identifier(12)),
512508
]);
509+
let accessible_scopes = vec!["__main__".to_string()];
513510
compute_ids_high_low(
514511
&mut vm,
515512
&mut exec_scopes,
516513
&ids_data,
517514
&ap_tracking,
518-
&constants,
515+
&identifiers,
516+
&accessible_scopes,
519517
)
520518
.expect("compute_ids_high_low() failed");
521519

0 commit comments

Comments
 (0)