Skip to content

Commit

Permalink
feat: implement 0x52 - MSTORE Opcode (#290)
Browse files Browse the repository at this point in the history
* mstore tests impl

* made exec_mstore panic

* corrected test case error

* mstore opcode impl

* review correction + add 2 tests

* correction test case

* used pop_n instead

* corrected #263 succeed into succeeded

* reverted to single pop + removed tests
  • Loading branch information
Quentash authored Sep 8, 2023
1 parent 1a65503 commit 5558fbd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use evm::context::{
};
use evm::errors::EVMError;
use evm::stack::StackTrait;

use evm::memory::MemoryTrait;
use evm::helpers::U256IntoResultU32;

#[generate_trait]
impl MemoryOperation of MemoryOperationTrait {
Expand All @@ -19,6 +20,10 @@ impl MemoryOperation of MemoryOperationTrait {
/// Save word to memory.
/// # Specification: https://www.evm.codes/#52?fork=shanghai
fn exec_mstore(ref self: ExecutionContext) -> Result<(), EVMError> {
let offset: u32 = Into::<u256, Result<u32, EVMError>>::into((self.stack.pop()?))?;
let value: u256 = self.stack.pop()?;

self.memory.store(value, offset);
Result::Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use evm::errors::{EVMError, STACK_UNDERFLOW};
use evm::context::{
ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct, CallContextTrait,
};
use integer::BoundedInt;


#[test]
Expand Down Expand Up @@ -57,7 +58,7 @@ fn test_exec_pop_should_pop_an_item_from_stack() {
let result = ctx.exec_pop();

// Then
assert(result.is_ok(), 'should have succeed');
assert(result.is_ok(), 'should have succeeded');
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.peek().unwrap() == 0x01, 'stack peek should return 0x01');
}
Expand All @@ -77,3 +78,41 @@ fn test_exec_pop_should_stack_underflow() {
result.unwrap_err() == EVMError::StackError(STACK_UNDERFLOW), 'should return StackUnderflow'
);
}

#[test]
#[available_gas(20000000)]
fn test_exec_mstore_should_store_max_uint256_offset_0() {
// Given
let mut ctx = setup_execution_context();

ctx.stack.push(BoundedInt::<u256>::max());
ctx.stack.push(0x00);

// When
let result = ctx.exec_mstore();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(ctx.memory.bytes_len == 32, 'memory should be 32 bytes long');
let (stored, _) = ctx.memory.load(0);
assert(stored == BoundedInt::<u256>::max(), 'should have stored max_uint256');
}

#[test]
#[available_gas(20000000)]
fn test_exec_mstore_should_store_max_uint256_offset_1() {
// Given
let mut ctx = setup_execution_context();

ctx.stack.push(BoundedInt::<u256>::max());
ctx.stack.push(0x01);

// When
let result = ctx.exec_mstore();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(ctx.memory.bytes_len == 64, 'memory should be 64 bytes long');
let (stored, _) = ctx.memory.load(1);
assert(stored == BoundedInt::<u256>::max(), 'should have stored max_uint256');
}

0 comments on commit 5558fbd

Please sign in to comment.