diff --git a/crates/evm/src/create_helpers.cairo b/crates/evm/src/create_helpers.cairo index 88edc223..cdf91ed2 100644 --- a/crates/evm/src/create_helpers.cairo +++ b/crates/evm/src/create_helpers.cairo @@ -48,7 +48,7 @@ pub impl CreateHelpersImpl of CreateHelpers { let charged_gas = match create_type { CreateType::Create => gas::CREATE + memory_expansion.expansion_cost + init_code_gas, CreateType::Create2 => { - let calldata_words = bytes_32_words_size(size) / 32; + let calldata_words = bytes_32_words_size(size); gas::CREATE + gas::KECCAK256WORD * calldata_words.into() + memory_expansion.expansion_cost diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index 1a564f1b..8635e9de 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -238,7 +238,6 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait { } ensure(!(last_returndata_index > return_data.len()), EVMError::ReturnDataOutOfBounds)?; - //TODO: handle overflow in bytes_32_words_size function. let words_size = bytes_32_words_size(size).into(); let copy_gas_cost = gas::COPY * words_size; diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 1416f6dc..30fd5bdb 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -2,6 +2,7 @@ use core::array::ArrayTrait; use core::array::SpanTrait; use core::cmp::min; use core::hash::{HashStateExTrait, HashStateTrait}; +use core::num::traits::SaturatingAdd; use core::panic_with_felt252; use core::pedersen::PedersenTrait; @@ -31,30 +32,6 @@ pub fn u128_split(input: u128) -> (u64, u64) { (high.try_into().unwrap(), low.try_into().unwrap()) } - -/// Converts a value to the next closest multiple of 32 -/// -/// # Arguments -/// * `value` - The value to ceil to the next multiple of 32 -/// -/// # Returns -/// The same value if it's a perfect multiple of 32 -/// else it returns the smallest multiple of 32 -/// that is greater than `value`. -/// -/// # Examples -/// ceil32(2) = 32 -/// ceil32(34) = 64 -pub fn ceil32(value: usize) -> usize { - let ceiling = 32_u32; - let (_q, r) = DivRem::div_rem(value, ceiling.try_into().unwrap()); - if r == 0_u8.into() { - return value; - } else { - return (value + ceiling - r).into(); - } -} - /// Computes the number of 32-byte words required to represent `size` bytes /// /// # Arguments @@ -68,7 +45,7 @@ pub fn ceil32(value: usize) -> usize { /// bytes_32_words_size(34) = 2 #[inline(always)] pub fn bytes_32_words_size(size: usize) -> usize { - (size + 31) / 32 + size.saturating_add(31) / 32 } /// Computes 256 ** (16 - i) for 0 <= i <= 16. @@ -383,4 +360,10 @@ mod tests { counter += 1; }; } + + #[test] + fn test_bytes_32_words_size_edge_case() { + let max_usize = usize::MAX; + assert_eq!(bytes_32_words_size(max_usize), (max_usize / 32) + 1); + } }