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

* fix: add support for arrays shorter than 2 as arguments for cairo1-run [#1737](https://github.com/lambdaclass/cairo-vm/pull/1737)

* bugfix: Fix BuiltinRunner::final_stack for SegmentArena[#1747](https://github.com/lambdaclass/cairo-vm/pull/1747)
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 @@ -120,14 +120,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 @@ -249,7 +249,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
6 changes: 3 additions & 3 deletions vm/src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ pub fn is_addr_bounded(
) -> Result<(), HintError> {
let addr = get_integer_from_var_name("addr", vm, ids_data, ap_tracking)?;

let addr_bound = constants
let addr_bound = constants
.get(ADDR_BOUND)
.ok_or_else(|| HintError::MissingConstant(Box::new(ADDR_BOUND)))?
.to_biguint();
Expand Down Expand Up @@ -2036,7 +2036,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
);
}

Expand Down Expand Up @@ -2260,7 +2260,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
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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
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 @@ -542,7 +542,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"
);
}

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