Skip to content

Commit

Permalink
Ft/fix max high (#47)
Browse files Browse the repository at this point in the history
* add default value for MAX_HIGH and MAX_LOW

* update tests
  • Loading branch information
ftheirs authored Oct 17, 2024
1 parent 3de95d9 commit 01ea1e0
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions vm/src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use super::{
};

const ADDR_BOUND: &str = "starkware.starknet.common.storage.ADDR_BOUND";
const MAX_HIGH: &str = "starkware.cairo.common.math.split_felt.MAX_HIGH";
const MAX_LOW: &str = "starkware.cairo.common.math.split_felt.MAX_LOW";

//Implements hint: memory[ap] = 0 if 0 <= (ids.a % PRIME) < range_check_builtin.bound else 1
pub fn is_nn(
Expand Down Expand Up @@ -386,13 +388,21 @@ pub fn split_felt(
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let max_high_local =
&Felt252::from_dec_str("10633823966279327296825105735305134080").map_err(|_| {
HintError::CustomHint(
String::from("pointer_size must be a positive integer").into_boxed_str(),
)
})?;
let max_low_local = &Felt252::ZERO;

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 max_high = constants.get(MAX_HIGH).unwrap_or(max_high_local);
let max_low = constants.get(MAX_LOW).unwrap_or(max_low_local);
assert(
max_high < &bound && max_low < &bound,
"assert ids.MAX_HIGH < 2**128 and ids.MAX_LOW < 2**128",
Expand Down Expand Up @@ -2064,9 +2074,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::ZERO),
(MAX_LOW, Felt252::ZERO),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
felt_str!("10633823966279327296825105735305134080")
)
])
Expand Down Expand Up @@ -2104,9 +2114,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::ZERO),
(MAX_LOW, Felt252::ZERO),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
felt_str!("10633823966279327296825105735305134080")
)
])
Expand Down Expand Up @@ -2143,9 +2153,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::ZERO),
(MAX_LOW, Felt252::ZERO),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
felt_str!("10633823966279327296825105735305134080")
)
])
Expand Down Expand Up @@ -2186,9 +2196,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::ZERO),
(MAX_LOW, Felt252::ZERO),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
felt_str!("10633823966279327296825105735305134080")
)
])
Expand Down Expand Up @@ -2224,9 +2234,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::ZERO),
(MAX_LOW, Felt252::ZERO),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
felt_str!("10633823966279327296825105735305134080")
)
])
Expand Down Expand Up @@ -2257,7 +2267,7 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::MissingConstant(x)) if (*x) == "MAX_HIGH"
Err(HintError::MissingConstant(x)) if (*x) == MAX_HIGH
);
}

Expand Down Expand Up @@ -2288,9 +2298,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::from(-1)),
(MAX_LOW, Felt252::from(-1)),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
Felt252::from(-1),
)
])
Expand Down Expand Up @@ -2326,9 +2336,9 @@ mod tests {
hint_code,
exec_scopes_ref!(),
&HashMap::from([
("MAX_LOW".to_string(), Felt252::ZERO),
(MAX_LOW, Felt252::ZERO),
(
"MAX_HIGH".to_string(),
MAX_HIGH,
Felt252::ZERO,
)
])
Expand Down

0 comments on commit 01ea1e0

Please sign in to comment.