Skip to content

Commit

Permalink
fix: memory expansion timing
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 6, 2024
1 parent 81a950b commit 5b1816d
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 175 deletions.
28 changes: 14 additions & 14 deletions crates/evm/src/instructions/logging_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ mod tests {
use evm::memory::MemoryTrait;
use evm::stack::StackTrait;
use evm::state::StateTrait;
use evm::test_utils::{VMBuilderTrait};
use evm::test_utils::{VMBuilderTrait, MemoryTestUtilsTrait};
use utils::helpers::u256_to_bytes_array;

#[test]
fn test_exec_log0() {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(0x1F).expect('push failed');
vm.stack.push(0x00).expect('push failed');
Expand Down Expand Up @@ -134,7 +134,7 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(0x0123456789ABCDEF).expect('push failed');
vm.stack.push(0x20).expect('push failed');
Expand Down Expand Up @@ -164,7 +164,7 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(Bounded::<u256>::MAX).expect('push failed');
vm.stack.push(0x0123456789ABCDEF).expect('push failed');
Expand Down Expand Up @@ -196,8 +196,8 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store(0x0123456789ABCDEF000000000000000000000000000000000000000000000000, 0x20);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(0x0123456789ABCDEF000000000000000000000000000000000000000000000000, 0x20);

vm.stack.push(0x00).expect('push failed');
vm.stack.push(Bounded::<u256>::MAX).expect('push failed');
Expand Down Expand Up @@ -233,8 +233,8 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store(0x0123456789ABCDEF000000000000000000000000000000000000000000000000, 0x20);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(0x0123456789ABCDEF000000000000000000000000000000000000000000000000, 0x20);

vm.stack.push(Bounded::<u256>::MAX).expect('push failed');
vm.stack.push(0x00).expect('push failed');
Expand Down Expand Up @@ -270,7 +270,7 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new().with_read_only().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(0x0123456789ABCDEF).expect('push failed');
vm.stack.push(0x20).expect('push failed');
Expand All @@ -291,7 +291,7 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(0x0123456789ABCDEF).expect('push failed');
vm.stack.push(0x00).expect('push failed');
Expand Down Expand Up @@ -319,7 +319,7 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(0x0123456789ABCDEF).expect('push failed');
vm.stack.push(Bounded::<u256>::MAX).expect('push failed');
Expand All @@ -341,7 +341,7 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);

vm.stack.push(0x0123456789ABCDEF).expect('push failed');
vm.stack.push(0x20).expect('push failed');
Expand All @@ -363,8 +363,8 @@ mod tests {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.memory.store(Bounded::<u256>::MAX, 0);
vm.memory.store(0x0123456789ABCDEF000000000000000000000000000000000000000000000000, 0x20);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0);
vm.memory.store_with_expansion(0x0123456789ABCDEF000000000000000000000000000000000000000000000000, 0x20);

vm.stack.push(Bounded::<u256>::MAX).expect('push failed');
vm.stack.push(0x00).expect('push failed');
Expand Down
33 changes: 10 additions & 23 deletions crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ mod tests {
use evm::stack::StackTrait;
use evm::state::{StateTrait, compute_storage_address};
use evm::test_utils::{
evm_address, VMBuilderTrait, setup_test_storages, register_account, uninitialized_account,
native_token
evm_address, VMBuilderTrait, MemoryTestUtilsTrait, setup_test_storages, register_account,
uninitialized_account, native_token
};
use snforge_std::{test_address, start_mock_call, store};
use snforge_utils::snforge_utils::store_evm;
Expand Down Expand Up @@ -381,7 +381,7 @@ mod tests {
fn assert_mload(value: u256, offset: u256, expected_value: u256, expected_memory_size: u32) {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();
vm.memory.store(value, 0);
vm.memory.store_with_expansion(value, 0);

vm.stack.push(offset).expect('push failed');

Expand Down Expand Up @@ -569,33 +569,18 @@ mod tests {
}

#[test]
fn test_exec_msize_store_max_offset_0() {
fn test_exec_msize_should_return_size_of_memory() {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();
vm.memory.store(Bounded::<u256>::MAX, 0x00);
vm.memory.store_with_expansion(Bounded::<u256>::MAX, 0x00);

// When
let result = vm.exec_msize();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 1, 'stack should have one element');
assert(vm.stack.pop().unwrap() == 32, 'should 32 bytes after MSTORE');
}

#[test]
fn test_exec_msize_store_max_offset_1() {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();
vm.memory.store(Bounded::<u256>::MAX, 0x01);

// When
let result = vm.exec_msize();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 1, 'stack should have one element');
assert(vm.stack.pop().unwrap() == 64, 'should 64 bytes after MSTORE');
assert(vm.stack.pop().unwrap() == 32, 'should 32 bytes after MSIZE');
}

#[test]
Expand Down Expand Up @@ -1049,7 +1034,7 @@ mod tests {
let mut i = 0;
for element in values
.span() {
vm.memory.store((*element).into(), source_offset + 0x20 * i);
vm.memory.store_with_expansion((*element).into(), source_offset + 0x20 * i);
i += 1;
};
vm.stack.push(dest_offset.into()).expect('push failed');
Expand All @@ -1058,7 +1043,9 @@ mod tests {

// When
let expected_gas = gas::VERYLOW
+ gas::memory_expansion(vm.memory.size(), [(max(dest_offset, source_offset), size)].span())
+ gas::memory_expansion(
vm.memory.size(), [(max(dest_offset, source_offset), size)].span()
)
.expansion_cost;
let gas_before = vm.gas_left();
let result = vm.exec_mcopy();
Expand Down
49 changes: 23 additions & 26 deletions crates/evm/src/instructions/sha3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod tests {
use evm::instructions::sha3::internal;
use evm::memory::{InternalMemoryTrait, MemoryTrait};
use evm::stack::StackTrait;
use evm::test_utils::VMBuilderTrait;
use evm::test_utils::{VMBuilderTrait, MemoryTestUtilsTrait};

#[test]
fn test_exec_sha3_size_0_offset_0() {
Expand All @@ -169,7 +169,7 @@ mod tests {
vm.stack.push(0x00).expect('push failed');
vm.stack.push(0x00).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -186,14 +186,14 @@ mod tests {


#[test]
fn test_exec_sha3_size_5_offset_4() {
fn test_exec_sha3_should_not_expand_memory() {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.stack.push(0x05).expect('push failed');
vm.stack.push(0x04).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -204,29 +204,26 @@ mod tests {
result == 0xc41589e7559804ea4a2080dad19d876a024ccb05117835447d72ce08c1d020ec,
'wrong result'
);
assert(vm.memory.size() == 64, 'wrong memory size');
assert_eq!(vm.memory.size(), 32, 'wrong memory size');
}

#[test]
fn test_exec_sha3_size_10_offset_10() {
fn test_exec_sha3_should_expand_memory() {
// Given
let mut vm = VMBuilderTrait::new_with_presets().build();

vm.stack.push(10).expect('push failed');
vm.stack.push(24).expect('push failed');
vm.stack.push(10).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');

// Then
let result = vm.stack.peek().unwrap();
assert(
result == 0x6bd2dd6bd408cbee33429358bf24fdc64612fbf8b1b4db604518f40ffd34b607,
'wrong result'
);
assert(vm.memory.size() == 64, 'wrong memory size');
assert_eq!(result, 0x827b659bbda2a0bdecce2c91b8b68462545758f3eba2dbefef18e0daf84f5ccd);
assert_eq!(vm.memory.size(), 64);
}

#[test]
Expand All @@ -237,7 +234,7 @@ mod tests {
vm.stack.push(0xFFFFF).expect('push failed');
vm.stack.push(1000).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -259,7 +256,7 @@ mod tests {
vm.stack.push(1000000).expect('push failed');
vm.stack.push(2).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -281,8 +278,8 @@ mod tests {
vm.stack.push(1000000).expect('push failed');
vm.stack.push(2).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -304,7 +301,7 @@ mod tests {
vm.stack.push(1).expect('push failed');
vm.stack.push(2048).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -326,7 +323,7 @@ mod tests {
vm.stack.push(0).expect('push failed');
vm.stack.push(1024).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -348,7 +345,7 @@ mod tests {
vm.stack.push(32).expect('push failed');
vm.stack.push(2016).expect('push failed');

vm.memory.store(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);
vm.memory.store_with_expansion(0xFFFFFFFF00000000000000000000000000000000000000000000000000000000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -370,7 +367,7 @@ mod tests {
vm.stack.push(32).expect('push failed');
vm.stack.push(0).expect('push failed');

vm.memory.store(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
vm.memory.store_with_expansion(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -392,7 +389,7 @@ mod tests {
vm.stack.push(31).expect('push failed');
vm.stack.push(0).expect('push failed');

vm.memory.store(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
vm.memory.store_with_expansion(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -414,7 +411,7 @@ mod tests {
vm.stack.push(33).expect('push failed');
vm.stack.push(0).expect('push failed');

vm.memory.store(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
vm.memory.store_with_expansion(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);

// When
vm.exec_sha3().expect('exec_sha3 failed');
Expand All @@ -440,7 +437,7 @@ mod tests {
while mem_dst <= 0x0C80 {
vm
.memory
.store(0xFAFAFAFA00000000000000000000000000000000000000000000000000000000, mem_dst);
.store_with_expansion(0xFAFAFAFA00000000000000000000000000000000000000000000000000000000, mem_dst);
mem_dst += 0x20;
};

Expand All @@ -462,7 +459,7 @@ mod tests {
let mut vm = VMBuilderTrait::new_with_presets().build();
let mut to_hash: Array<u64> = Default::default();

vm.memory.store(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
vm.memory.store_with_expansion(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
let mut size = 32;
let mut offset = 0;

Expand All @@ -486,7 +483,7 @@ mod tests {
let mut vm = VMBuilderTrait::new_with_presets().build();
let mut to_hash: Array<u64> = Default::default();

vm.memory.store(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
vm.memory.store_with_expansion(0xFAFFFFFF000000E500000077000000DEAD0000000004200000FADE0000450000, 0);
let mut size = 33;
let mut offset = 0;

Expand Down
Loading

0 comments on commit 5b1816d

Please sign in to comment.