Skip to content

Commit abf34d4

Browse files
Replace constants everywhere
1 parent 41d69c7 commit abf34d4

File tree

3 files changed

+63
-84
lines changed

3 files changed

+63
-84
lines changed

vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ impl HintProcessorLogic for BuiltinHintProcessor {
189189
vm: &mut VirtualMachine,
190190
exec_scopes: &mut ExecutionScopes,
191191
hint_data: &Box<dyn Any>,
192-
constants: &HashMap<String, Felt252>,
192+
_constants: &HashMap<String, Felt252>,
193193
) -> Result<(), HintError> {
194194
let hint_data = hint_data
195195
.downcast_ref::<HintProcessorData>()
196196
.ok_or(HintError::WrongHintData)?;
197+
let constants = hint_data.constants.as_ref();
197198

198199
if let Some(hint_func) = self.extra_hints.get(&hint_data.code) {
199200
return hint_func.0(
@@ -265,12 +266,9 @@ impl HintProcessorLogic for BuiltinHintProcessor {
265266
&hint_data.ap_tracking,
266267
"continue_loop",
267268
),
268-
hint_code::SPLIT_FELT => split_felt(
269-
vm,
270-
&hint_data.ids_data,
271-
&hint_data.ap_tracking,
272-
&hint_data.constants,
273-
),
269+
hint_code::SPLIT_FELT => {
270+
split_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
271+
}
274272
hint_code::UNSIGNED_DIV_REM => {
275273
unsigned_div_rem(vm, &hint_data.ids_data, &hint_data.ap_tracking)
276274
}

vm/src/hint_processor/builtin_hint_processor/math_utils.rs

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,8 @@ pub fn split_felt(
391391
.ok_or_else(|| HintError::AssertionFailed(msg.to_string().into_boxed_str()))
392392
};
393393
let bound = pow2_const(128);
394-
395394
let max_high = get_constant_from_var_name("MAX_HIGH", constants)?;
396395
let max_low = get_constant_from_var_name("MAX_LOW", constants)?;
397-
398396
assert(
399397
max_high < &bound && max_low < &bound,
400398
"assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128",
@@ -754,7 +752,6 @@ pub fn split_xx(
754752
#[cfg(test)]
755753
mod tests {
756754
use super::*;
757-
use crate::stdlib::rc::Rc;
758755
use crate::{felt_hex, felt_str};
759756
use core::ops::Neg;
760757

@@ -2065,22 +2062,20 @@ mod tests {
20652062
HintReference::new(-3, 1, true, true, true),
20662063
),
20672064
]);
2068-
let identifiers = HashMap::from([
2069-
("MAX_LOW".to_string(), Felt252::ZERO),
2070-
(
2071-
"MAX_HIGH".to_string(),
2072-
felt_str!("10633823966279327296825105735305134080"),
2073-
),
2074-
]);
20752065
//Execute the hint
20762066
assert_matches!(
20772067
run_hint!(
20782068
vm,
20792069
ids_data,
20802070
hint_code,
20812071
exec_scopes_ref!(),
2082-
&HashMap::default(),
2083-
identifiers
2072+
&HashMap::from([
2073+
("MAX_LOW".to_string(), Felt252::ZERO),
2074+
(
2075+
"MAX_HIGH".to_string(),
2076+
felt_str!("10633823966279327296825105735305134080")
2077+
)
2078+
])
20842079
),
20852080
Ok(())
20862081
);
@@ -2107,24 +2102,20 @@ mod tests {
21072102
//Create incomplete ids
21082103
//Create ids_data & hint_data
21092104
let ids_data = ids_data!["low"];
2110-
2111-
let identifiers = HashMap::from([
2112-
("MAX_LOW".to_string(), Felt252::ZERO),
2113-
(
2114-
"MAX_HIGH".to_string(),
2115-
felt_str!("10633823966279327296825105735305134080"),
2116-
),
2117-
]);
2118-
21192105
//Execute the hint
21202106
assert_matches!(
21212107
run_hint!(
21222108
vm,
21232109
ids_data,
21242110
hint_code,
21252111
exec_scopes_ref!(),
2126-
&HashMap::default(),
2127-
identifiers
2112+
&HashMap::from([
2113+
("MAX_LOW".to_string(), Felt252::ZERO),
2114+
(
2115+
"MAX_HIGH".to_string(),
2116+
felt_str!("10633823966279327296825105735305134080")
2117+
)
2118+
])
21282119
),
21292120
Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "value"
21302121
);
@@ -2156,23 +2147,20 @@ mod tests {
21562147
),
21572148
]);
21582149

2159-
let identifiers = HashMap::from([
2160-
("MAX_LOW".to_string(), Felt252::ZERO),
2161-
(
2162-
"MAX_HIGH".to_string(),
2163-
felt_str!("10633823966279327296825105735305134080"),
2164-
),
2165-
]);
2166-
21672150
//Execute the hint
21682151
assert_matches!(
21692152
run_hint!(
21702153
vm,
21712154
ids_data,
21722155
hint_code,
21732156
exec_scopes_ref!(),
2174-
&HashMap::default(),
2175-
identifiers
2157+
&HashMap::from([
2158+
("MAX_LOW".to_string(), Felt252::ZERO),
2159+
(
2160+
"MAX_HIGH".to_string(),
2161+
felt_str!("10633823966279327296825105735305134080")
2162+
)
2163+
])
21762164
),
21772165
Err(HintError::Memory(
21782166
MemoryError::InconsistentMemory(bx)
@@ -2208,24 +2196,20 @@ mod tests {
22082196
HintReference::new(-3, 1, true, true, true),
22092197
),
22102198
]);
2211-
2212-
let identifiers = HashMap::from([
2213-
("MAX_LOW".to_string(), Felt252::ZERO),
2214-
(
2215-
"MAX_HIGH".to_string(),
2216-
felt_str!("10633823966279327296825105735305134080"),
2217-
),
2218-
]);
2219-
22202199
//Execute the hint
22212200
assert_matches!(
22222201
run_hint!(
22232202
vm,
22242203
ids_data,
22252204
hint_code,
22262205
exec_scopes_ref!(),
2227-
&HashMap::default(),
2228-
identifiers
2206+
&HashMap::from([
2207+
("MAX_LOW".to_string(), Felt252::ZERO),
2208+
(
2209+
"MAX_HIGH".to_string(),
2210+
felt_str!("10633823966279327296825105735305134080")
2211+
)
2212+
])
22292213
),
22302214
Err(HintError::Memory(
22312215
MemoryError::InconsistentMemory(bx)
@@ -2256,22 +2240,20 @@ mod tests {
22562240
HintReference::new(-3, 1, true, true, true),
22572241
),
22582242
]);
2259-
let identifiers = HashMap::from([
2260-
("MAX_LOW".to_string(), Felt252::ZERO),
2261-
(
2262-
"MAX_HIGH".to_string(),
2263-
felt_str!("10633823966279327296825105735305134080"),
2264-
),
2265-
]);
22662243
//Execute the hint
22672244
assert_matches!(
22682245
run_hint!(
22692246
vm,
22702247
ids_data,
22712248
hint_code,
22722249
exec_scopes_ref!(),
2273-
&HashMap::default(),
2274-
identifiers
2250+
&HashMap::from([
2251+
("MAX_LOW".to_string(), Felt252::ZERO),
2252+
(
2253+
"MAX_HIGH".to_string(),
2254+
felt_str!("10633823966279327296825105735305134080")
2255+
)
2256+
])
22752257
),
22762258
Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "value"
22772259
);
@@ -2334,21 +2316,20 @@ mod tests {
23342316
HintReference::new(-3, 1, true, true, true),
23352317
),
23362318
]);
2337-
2338-
let identifiers = HashMap::from([
2339-
("a.MAX_LOW".to_string(), Felt252::from(-1)),
2340-
("a.MAX_HIGH".to_string(), Felt252::from(-1)),
2341-
]);
2342-
23432319
//Execute the hint
23442320
assert_matches!(
23452321
run_hint!(
23462322
vm,
23472323
ids_data,
23482324
hint_code,
23492325
exec_scopes_ref!(),
2350-
&HashMap::default(),
2351-
identifiers
2326+
&HashMap::from([
2327+
("MAX_LOW".to_string(), Felt252::from(-1)),
2328+
(
2329+
"MAX_HIGH".to_string(),
2330+
Felt252::from(-1),
2331+
)
2332+
])
23522333
),
23532334
Err(HintError::AssertionFailed(x)) if &(*x) == "assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128"
23542335
);
@@ -2379,21 +2360,20 @@ mod tests {
23792360
HintReference::new(-3, 1, true, true, true),
23802361
),
23812362
]);
2382-
2383-
let identifiers = HashMap::from([
2384-
("MAX_LOW".to_string(), Felt252::ZERO),
2385-
("MAX_HIGH".to_string(), Felt252::ZERO),
2386-
]);
2387-
23882363
//Execute the hint
23892364
assert_matches!(
23902365
run_hint!(
23912366
vm,
23922367
ids_data,
23932368
hint_code,
23942369
exec_scopes_ref!(),
2395-
&HashMap::default(),
2396-
identifiers
2370+
&HashMap::from([
2371+
("MAX_LOW".to_string(), Felt252::ZERO),
2372+
(
2373+
"MAX_HIGH".to_string(),
2374+
Felt252::ZERO,
2375+
)
2376+
])
23972377
),
23982378
Err(HintError::AssertionFailed(x)) if &(*x) == "assert PRIME - 1 == ids.MAX_HIGH * 2**128 + ids.MAX_LOW"
23992379
);

vm/src/utils.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,17 @@ pub mod test_utils {
473473
pub(crate) use exec_scopes_ref;
474474

475475
macro_rules! run_hint {
476-
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr, $identifiers:expr) => {{
477-
let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
478-
hint_data.constants = Rc::new($identifiers);
479-
let mut hint_processor = BuiltinHintProcessor::new_empty();
480-
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants)
481-
}};
482476
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{
483-
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());
484480
let mut hint_processor = BuiltinHintProcessor::new_empty();
485-
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data), $constants)
481+
hint_processor.execute_hint(
482+
&mut $vm,
483+
$exec_scopes,
484+
&any_box!(hint_data),
485+
&HashMap::default(),
486+
)
486487
}};
487488
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr) => {{
488489
let hint_data = HintProcessorData::new_default(

0 commit comments

Comments
 (0)