Skip to content

Commit

Permalink
overflow and nits
Browse files Browse the repository at this point in the history
  • Loading branch information
lordshashank committed Sep 27, 2024
1 parent c14c6d7 commit fe0a8a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/evm/src/create_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
33 changes: 8 additions & 25 deletions crates/utils/src/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}

0 comments on commit fe0a8a1

Please sign in to comment.