From 717c300c7002811259efd6f15dbd6059022410bf Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Thu, 21 Dec 2023 16:45:08 +0100 Subject: [PATCH 1/3] fix: remove static lifetime for name str parameter requirement for constant getter --- .../cairo_keccak/keccak_hints.rs | 26 ++++++++++--------- .../builtin_hint_processor/hint_utils.rs | 4 +-- .../builtin_hint_processor/keccak_utils.rs | 2 +- .../builtin_hint_processor/math_utils.rs | 18 ++++++------- .../secp/bigint_utils.rs | 2 +- .../builtin_hint_processor/secp/signature.rs | 2 +- .../builtin_hint_processor/uint384.rs | 2 +- vm/src/vm/errors/hint_errors.rs | 2 +- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs b/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs index 0beea39ee9..1000698fac 100644 --- a/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs +++ b/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs @@ -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) } @@ -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) } @@ -149,9 +151,9 @@ pub(crate) fn block_permutation_v1( ap_tracking: &ApTracking, constants: &HashMap, ) -> 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, @@ -216,9 +218,9 @@ pub(crate) fn block_permutation_v2( ap_tracking: &ApTracking, constants: &HashMap, ) -> 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, @@ -253,12 +255,12 @@ fn cairo_keccak_finalize( constants: &HashMap, 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( diff --git a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs index 3a1808b5a0..24152b3f3d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -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, ) -> 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)] diff --git a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs index 8a22530f9b..8b01d24064 100644 --- a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs @@ -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", diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index 1265c8ed8f..68cb30fc15 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -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)?; @@ -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; @@ -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() ); } @@ -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() ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index c0e4a6d07a..5b96208dc7 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -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) diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs index f8000d465b..a81ab1677e 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs @@ -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)? diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384.rs b/vm/src/hint_processor/builtin_hint_processor/uint384.rs index c7d6ba3465..4e2bb44123 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384.rs @@ -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() ); } diff --git a/vm/src/vm/errors/hint_errors.rs b/vm/src/vm/errors/hint_errors.rs index 0ba9c973c3..c31ae077f5 100644 --- a/vm/src/vm/errors/hint_errors.rs +++ b/vm/src/vm/errors/hint_errors.rs @@ -45,7 +45,7 @@ pub enum HintError { #[error("Hint Error: {0}")] CustomHint(Box), #[error("Missing constant: {0}")] - MissingConstant(Box<&'static str>), + MissingConstant(Box), #[error("Fail to get constants for hint execution")] FailedToGetConstant, #[error("Arc too big, {} must be <= {} and {} <= {}", (*.0).0, (*.0).1, (*.0).2, (*.0).3)] From 42db06f5b56f369cdeabf536245786e3f97a109a Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Thu, 21 Dec 2023 18:25:06 +0100 Subject: [PATCH 2/3] fix: fix PR comments --- CHANGELOG.md | 2 ++ vm/src/hint_processor/builtin_hint_processor/hint_utils.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b627a8f9eb..f4a3b28de3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs index 24152b3f3d..a1ef39e017 100644 --- a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -124,7 +124,7 @@ pub fn get_reference_from_var_name<'a>( } pub fn get_constant_from_var_name<'a>( - var_name: &'a str, + var_name: &str, constants: &'a HashMap, ) -> Result<&'a Felt252, HintError> { constants From c721103d5c1988bdcc9ee34ac358d635ee49a68c Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Tue, 23 Jan 2024 09:03:06 +0100 Subject: [PATCH 3/3] fix: fix linter --- vm/src/hint_processor/builtin_hint_processor/math_utils.rs | 4 ++-- vm/src/hint_processor/builtin_hint_processor/uint384.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index 68cb30fc15..bbe18b5d71 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -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.to_string() + Err(HintError::MissingConstant(bx)) if &*bx == ADDR_BOUND ); } @@ -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".to_string() + Err(HintError::MissingConstant(x)) if &*x == "MAX_HIGH" ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384.rs b/vm/src/hint_processor/builtin_hint_processor/uint384.rs index 4e2bb44123..9f6646dd3e 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384.rs @@ -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".to_string() + Err(HintError::MissingConstant(bx)) if &*bx == "SHIFT" ); }