Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove static lifetime for name str parameter requirement for constant getter #1523

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* refactor: remove static lifetime for name str parameter requirement for constant getter

* feat(BREAKING): Replace `cairo-felt` crate with `starknet-types-core` [#1408](https://github.com/lambdaclass/cairo-vm/pull/1408)

* feat(BREAKING): Add Cairo 1 proof mode compilation and execution [#1517] (https://github.com/lambdaclass/cairo-vm/pull/1517)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn compare_bytes_in_word_nondet(
// Felt252::from(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::from((n_bytes < bytes_in_word) as usize);
insert_value_into_ap(vm, value)
}
Expand All @@ -117,7 +117,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::from((n_bytes >= keccak_full_rate_in_bytes) as usize);
insert_value_into_ap(vm, value)
}
Expand Down Expand Up @@ -149,9 +151,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::from(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelt252s(Box::new(
*keccak_state_size_felts,
Expand Down Expand Up @@ -216,9 +218,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,
Expand Down Expand Up @@ -253,12 +255,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::from(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: &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 @@ -250,7 +250,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 @@ -106,12 +106,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 = felt_to_biguint(*get_integer_from_var_name("a", vm, ids_data, ap_tracking)?);
let b = felt_to_biguint(*get_integer_from_var_name("b", vm, ids_data, ap_tracking)?);
let range_check_ptr = get_ptr_from_var_name("range_check_ptr", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -628,7 +628,7 @@ pub fn is_addr_bounded(
let addr_bound = felt_to_biguint(
*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()))?,
);

let lower_bound = BigUint::one() << 250_usize;
Expand Down Expand Up @@ -2085,7 +2085,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 @@ -2309,7 +2309,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 @@ -151,7 +151,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 mask = pow2_const_nz(128);
let low = (d0 + (d1 * base_86)).mod_floor(mask);
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 @@ -114,7 +114,7 @@ pub fn get_point_from_x(
let beta = felt_to_bigint(
*constants
.get(BETA)
.ok_or_else(|| HintError::MissingConstant(Box::new(BETA)))?,
.ok_or_else(|| HintError::MissingConstant(BETA.to_string().into_boxed_str()))?,
);

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
Loading