Skip to content

Commit

Permalink
fix: remove static lifetime for name str parameter requirement for co…
Browse files Browse the repository at this point in the history
…nstant getter
  • Loading branch information
Eikix committed Dec 21, 2023
1 parent 8a2ef24 commit fa4729b
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn compare_bytes_in_word_nondet(
// Felt252::new(BYTES_INTO_WORD) into a lazy_static!
let bytes_in_word = constants
.get(BYTES_IN_WORD)
.ok_or_else(|| HintError::MissingConstant(Box::new(BYTES_IN_WORD)))?;
.ok_or_else(|| HintError::MissingConstant(BYTES_IN_WORD.to_string().into_boxed_str()))?;
let value = Felt252::new((n_bytes < bytes_in_word) as usize);
insert_value_into_ap(vm, value)
}
Expand All @@ -119,7 +119,9 @@ pub fn compare_keccak_full_rate_in_bytes_nondet(
let keccak_full_rate_in_bytes = constants
.get(KECCAK_FULL_RATE_IN_BYTES_CAIRO_KECCAK)
.or_else(|| constants.get(KECCAK_FULL_RATE_IN_BYTES_BUILTIN_KECCAK))
.ok_or_else(|| HintError::MissingConstant(Box::new(KECCAK_FULL_RATE_IN_BYTES)))?;
.ok_or_else(|| {
HintError::MissingConstant(KECCAK_FULL_RATE_IN_BYTES.to_string().into_boxed_str())
})?;
let value = Felt252::new((n_bytes >= keccak_full_rate_in_bytes) as usize);
insert_value_into_ap(vm, value)
}
Expand Down Expand Up @@ -151,9 +153,9 @@ pub(crate) fn block_permutation_v1(
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let keccak_state_size_felts = constants
.get(KECCAK_STATE_SIZE_FELTS)
.ok_or_else(|| HintError::MissingConstant(Box::new(KECCAK_STATE_SIZE_FELTS)))?;
let keccak_state_size_felts = constants.get(KECCAK_STATE_SIZE_FELTS).ok_or_else(|| {
HintError::MissingConstant(KECCAK_STATE_SIZE_FELTS.to_string().into_boxed_str())
})?;
if keccak_state_size_felts >= &Felt252::new(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelt252s(Box::new(
keccak_state_size_felts.clone(),
Expand Down Expand Up @@ -218,9 +220,9 @@ pub(crate) fn block_permutation_v2(
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let keccak_state_size_felts = constants
.get(KECCAK_STATE_SIZE_FELTS)
.ok_or_else(|| HintError::MissingConstant(Box::new(KECCAK_STATE_SIZE_FELTS)))?;
let keccak_state_size_felts = constants.get(KECCAK_STATE_SIZE_FELTS).ok_or_else(|| {
HintError::MissingConstant(KECCAK_STATE_SIZE_FELTS.to_string().into_boxed_str())
})?;
if keccak_state_size_felts >= &Felt252::from(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelt252s(Box::new(
keccak_state_size_felts.clone(),
Expand Down Expand Up @@ -255,12 +257,12 @@ fn cairo_keccak_finalize(
constants: &HashMap<String, Felt252>,
block_size_limit: usize,
) -> Result<(), HintError> {
let keccak_state_size_felts = constants
.get(KECCAK_STATE_SIZE_FELTS)
.ok_or_else(|| HintError::MissingConstant(Box::new(KECCAK_STATE_SIZE_FELTS)))?;
let keccak_state_size_felts = constants.get(KECCAK_STATE_SIZE_FELTS).ok_or_else(|| {
HintError::MissingConstant(KECCAK_STATE_SIZE_FELTS.to_string().into_boxed_str())
})?;
let block_size = constants
.get(BLOCK_SIZE)
.ok_or_else(|| HintError::MissingConstant(Box::new(BLOCK_SIZE)))?;
.ok_or_else(|| HintError::MissingConstant(BLOCK_SIZE.to_string().into_boxed_str()))?;

if keccak_state_size_felts >= &Felt252::new(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelt252s(Box::new(
Expand Down
4 changes: 2 additions & 2 deletions vm/src/hint_processor/builtin_hint_processor/hint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ pub fn get_reference_from_var_name<'a>(
}

pub fn get_constant_from_var_name<'a>(
var_name: &'static str,
var_name: &'a str,
constants: &'a HashMap<String, Felt252>,
) -> Result<&'a Felt252, HintError> {
constants
.iter()
.find(|(k, _)| k.rsplit('.').next() == Some(var_name))
.map(|(_, n)| n)
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name)))
.ok_or_else(|| HintError::MissingConstant(var_name.to_string().into_boxed_str()))
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub fn split_n_bytes(
let bytes_in_word = constants
.get(BYTES_IN_WORD)
.and_then(|x| x.to_u64())
.ok_or_else(|| HintError::MissingConstant(Box::new(BYTES_IN_WORD)))?;
.ok_or_else(|| HintError::MissingConstant(BYTES_IN_WORD.to_string().into_boxed_str()))?;
let (high, low) = n_bytes.div_mod_floor(&bytes_in_word);
insert_value_from_var_name(
"n_words_to_copy",
Expand Down
18 changes: 9 additions & 9 deletions vm/src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ pub fn assert_le_felt(
const PRIME_OVER_3_HIGH: &str = "starkware.cairo.common.math.assert_le_felt.PRIME_OVER_3_HIGH";
const PRIME_OVER_2_HIGH: &str = "starkware.cairo.common.math.assert_le_felt.PRIME_OVER_2_HIGH";

let prime_over_3_high = constants
.get(PRIME_OVER_3_HIGH)
.ok_or_else(|| HintError::MissingConstant(Box::new(PRIME_OVER_3_HIGH)))?;
let prime_over_2_high = constants
.get(PRIME_OVER_2_HIGH)
.ok_or_else(|| HintError::MissingConstant(Box::new(PRIME_OVER_2_HIGH)))?;
let prime_over_3_high = constants.get(PRIME_OVER_3_HIGH).ok_or_else(|| {
HintError::MissingConstant(PRIME_OVER_3_HIGH.to_string().into_boxed_str())
})?;
let prime_over_2_high = constants.get(PRIME_OVER_2_HIGH).ok_or_else(|| {
HintError::MissingConstant(PRIME_OVER_2_HIGH.to_string().into_boxed_str())
})?;
let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?.to_biguint();
let b = get_integer_from_var_name("b", vm, ids_data, ap_tracking)?.to_biguint();
let range_check_ptr = get_ptr_from_var_name("range_check_ptr", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -640,7 +640,7 @@ pub fn is_addr_bounded(

let addr_bound = constants
.get(ADDR_BOUND)
.ok_or_else(|| HintError::MissingConstant(Box::new(ADDR_BOUND)))?
.ok_or_else(|| HintError::MissingConstant(ADDR_BOUND.to_string().into_boxed_str()))?
.to_biguint();

let lower_bound = BigUint::one() << 250_u32;
Expand Down Expand Up @@ -2089,7 +2089,7 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::MissingConstant(bx)) if *bx == ADDR_BOUND
Err(HintError::MissingConstant(bx)) if *bx == ADDR_BOUND.to_string()
);
}

Expand Down Expand Up @@ -2313,7 +2313,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".to_string()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn bigint_to_uint256(
let d1 = d1.as_ref();
let base_86 = constants
.get(BASE_86)
.ok_or_else(|| HintError::MissingConstant(Box::new(BASE_86)))?;
.ok_or_else(|| HintError::MissingConstant(BASE_86.to_string().into_boxed_str()))?;
let low = (d0 + &(d1 * base_86)) & &Felt252::new(u128::MAX);
insert_value_from_var_name("low", low, vm, ids_data, ap_tracking)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn get_point_from_x(
#[allow(deprecated)]
let beta = constants
.get(BETA)
.ok_or_else(|| HintError::MissingConstant(Box::new(BETA)))?
.ok_or_else(|| HintError::MissingConstant(BETA.to_string().into_boxed_str()))?
.to_bigint();

let x_cube_int = Uint384::from_var_name("x_cube", vm, ids_data, ap_tracking)?
Expand Down
2 changes: 1 addition & 1 deletion vm/src/hint_processor/builtin_hint_processor/uint384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code::ADD_NO_UINT384_CHECK),
Err(HintError::MissingConstant(bx)) if *bx == "SHIFT"
Err(HintError::MissingConstant(bx)) if *bx == "SHIFT".to_string()
);
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm/errors/hint_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum HintError {
#[error("Hint Error: {0}")]
CustomHint(Box<str>),
#[error("Missing constant: {0}")]
MissingConstant(Box<&'static str>),
MissingConstant(Box<str>),
#[error("Fail to get constants for hint execution")]
FailedToGetConstant,
#[error("Arc too big, {} must be <= {} and {} <= {}", (*.0).0, (*.0).1, (*.0).2, (*.0).3)]
Expand Down

0 comments on commit fa4729b

Please sign in to comment.