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

Optimizes conversion of bytes to words #985

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/evm/src/create_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::stack::StackTrait;
use crate::state::StateTrait;
use utils::address::{compute_contract_address, compute_create2_contract_address};
use utils::constants;
use utils::helpers::ceil32;
use utils::helpers::bytes_32_words_size;
use utils::set::SetTrait;
use utils::traits::{
BoolIntoNumeric, EthAddressIntoU256, U256TryIntoResult, SpanU8TryIntoResultEthAddress
Expand Down 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 = ceil32(size) / 32;
let calldata_words = bytes_32_words_size(size) / 32;
lordshashank marked this conversation as resolved.
Show resolved Hide resolved
gas::CREATE
+ gas::KECCAK256WORD * calldata_words.into()
+ memory_expansion.expansion_cost
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn memory_expansion(
}
}?;

let new_size = helpers::ceil32(max_size);
let new_size = helpers::bytes_32_words_size(max_size) * 32;

if new_size <= current_size {
return Result::Ok(MemoryExpansion { new_size: current_size, expansion_cost: 0 });
Expand All @@ -218,7 +218,7 @@ pub fn memory_expansion(
/// * `init_code_gas` - The gas to be charged for the init code.
#[inline(always)]
pub fn init_code_cost(code_size: usize) -> u64 {
let code_size_in_words = helpers::ceil32(code_size) / 32;
let code_size_in_words = helpers::bytes_32_words_size(code_size);
code_size_in_words.into() * INITCODE_WORD_COST
}

Expand Down
12 changes: 6 additions & 6 deletions crates/evm/src/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::model::vm::{VM, VMTrait};
use crate::model::{AddressTrait};
use crate::stack::StackTrait;
use crate::state::StateTrait;
use utils::helpers::{ceil32, load_word};
use utils::helpers::{bytes_32_words_size, load_word};
use utils::set::SetTrait;
use utils::traits::{EthAddressIntoU256};

Expand Down Expand Up @@ -115,7 +115,7 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait {
let offset = self.stack.pop_saturating_usize()?;
let size = self.stack.pop_usize()?;

let words_size = (ceil32(size) / 32).into();
let words_size = bytes_32_words_size(size).into();
let copy_gas_cost = gas::COPY * words_size;
let memory_expansion = gas::memory_expansion(
self.memory.size(), [(dest_offset, size)].span()
Expand Down Expand Up @@ -145,7 +145,7 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait {
let offset = self.stack.pop_saturating_usize()?;
let size = self.stack.pop_usize()?;

let words_size = (ceil32(size) / 32).into();
let words_size = bytes_32_words_size(size).into();
let copy_gas_cost = gas::COPY * words_size;
let memory_expansion = gas::memory_expansion(
self.memory.size(), [(dest_offset, size)].span()
Expand Down Expand Up @@ -195,7 +195,7 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait {
let size = self.stack.pop_usize()?;

// GAS
let words_size = (ceil32(size) / 32).into();
let words_size = bytes_32_words_size(size).into();
let memory_expansion = gas::memory_expansion(
self.memory.size(), [(dest_offset, size)].span()
)?;
Expand Down Expand Up @@ -238,8 +238,8 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait {
}
ensure(!(last_returndata_index > return_data.len()), EVMError::ReturnDataOutOfBounds)?;

//TODO: handle overflow in ceil32 function.
let words_size = (ceil32(size.into()) / 32).into();
//TODO: handle overflow in bytes_32_words_size function.
lordshashank marked this conversation as resolved.
Show resolved Hide resolved
let words_size = bytes_32_words_size(size).into();
let copy_gas_cost = gas::COPY * words_size;

let memory_expansion = gas::memory_expansion(
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::memory::MemoryTrait;
use crate::model::vm::{VM, VMTrait};
use crate::stack::StackTrait;
use crate::state::StateTrait;
use utils::helpers::ceil32;
use utils::helpers::bytes_32_words_size;
use utils::set::SetTrait;

#[inline(always)]
Expand Down Expand Up @@ -282,7 +282,7 @@ pub impl MemoryOperation of MemoryOperationTrait {
let source_offset = self.stack.pop_usize()?;
let size = self.stack.pop_usize()?;

let words_size = (ceil32(size) / 32).into();
let words_size = bytes_32_words_size(size).into();
let copy_gas_cost = gas::COPY * words_size;
let memory_expansion = gas::memory_expansion(
self.memory.size(), [(max(dest_offset, source_offset), size)].span()
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/instructions/sha3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::gas;
use crate::memory::MemoryTrait;
use crate::model::vm::{VM, VMTrait};
use crate::stack::StackTrait;
use utils::helpers::ceil32;
use utils::helpers::bytes_32_words_size;
use utils::traits::array::ArrayExtTrait;
use utils::traits::integer::U256Trait;

Expand All @@ -26,7 +26,7 @@ pub impl Sha3Impl of Sha3Trait {
let offset: usize = self.stack.pop_usize()?;
let mut size: usize = self.stack.pop_usize()?;

let words_size = (ceil32(size) / 32).into();
let words_size = bytes_32_words_size(size).into();
let word_gas_cost = gas::KECCAK256WORD * words_size;
let memory_expansion = gas::memory_expansion(self.memory.size(), [(offset, size)].span())?;
self.memory.ensure_length(memory_expansion.new_size);
Expand Down
16 changes: 16 additions & 0 deletions crates/utils/src/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ pub fn ceil32(value: usize) -> usize {
}
}

/// Computes the number of 32-byte words required to represent `size` bytes
///
/// # Arguments
/// * `size` - The size in bytes
///
/// # Returns
/// The number of 32-byte words required to represent `size` bytes
///
/// # Examples
/// bytes_32_words_size(2) = 1
/// bytes_32_words_size(34) = 2
#[inline(always)]
pub fn bytes_32_words_size(size: usize) -> usize {
(size + 31) / 32
}

lordshashank marked this conversation as resolved.
Show resolved Hide resolved
/// Computes 256 ** (16 - i) for 0 <= i <= 16.
pub fn pow256_rev(i: usize) -> u256 {
if (i > 16) {
Expand Down
Loading