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

feat: implement the test suite for MLOAD opcode #265

Closed
wants to merge 19 commits into from
Closed
1 change: 1 addition & 0 deletions crates/evm/src/instructions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod exchange_operations;
mod logging_operations;

mod memory_operations;
use memory_operations::MemoryOperationsTrait;

mod push_operations;
use push_operations::PushOperationsTrait;
Expand Down
126 changes: 77 additions & 49 deletions crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,92 @@
// Internal imports
use evm::context::ExecutionContext;
use evm::context::ExecutionContextTrait;
use evm::errors::EVMError;

/// MLOAD operation.
/// Load word from memory and push to stack.
fn exec_mload(ref context: ExecutionContext) {}
#[generate_trait]
impl MemoryOperationsImpl of MemoryOperationsTrait {
/// 0x51 - MLOAD operation.
/// Load word from memory and push to stack.
fn exec_mload(ref self: ExecutionContext) -> Result<(), EVMError> {
panic_with_felt252('MLOAD not implement yet')
khaeljy marked this conversation as resolved.
Show resolved Hide resolved
}

/// 0x52 - MSTORE operation.
/// Save word to memory.
/// # Specification: https://www.evm.codes/#52?fork=shanghai
fn exec_mstore(ref context: ExecutionContext) {}
/// 0x52 - MSTORE operation.
/// Save word to memory.
/// # Specification: https://www.evm.codes/#52?fork=shanghai
fn exec_mstore(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x58 - PC operation
/// Get the value of the program counter prior to the increment.
/// # Specification: https://www.evm.codes/#58?fork=shanghai
fn exec_pc(ref context: ExecutionContext) {}
/// 0x58 - PC operation
/// Get the value of the program counter prior to the increment.
/// # Specification: https://www.evm.codes/#58?fork=shanghai
fn exec_pc(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x59 - MSIZE operation.
/// Get the value of memory size.
/// # Specification: https://www.evm.codes/#59?fork=shanghai
fn exec_msize(ref context: ExecutionContext) {}
/// 0x59 - MSIZE operation.
/// Get the value of memory size.
/// # Specification: https://www.evm.codes/#59?fork=shanghai
fn exec_msize(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x56 - JUMP operation
/// The JUMP instruction changes the pc counter.
/// The new pc target has to be a JUMPDEST opcode.
/// # Specification: https://www.evm.codes/#56?fork=shanghai
fn exec_jump(ref context: ExecutionContext) {}
/// 0x56 - JUMP operation
/// The JUMP instruction changes the pc counter.
/// The new pc target has to be a JUMPDEST opcode.
/// # Specification: https://www.evm.codes/#56?fork=shanghai
fn exec_jump(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x57 - JUMPI operation.
/// Change the pc counter under a provided certain condition.
/// The new pc target has to be a JUMPDEST opcode.
/// # Specification: https://www.evm.codes/#57?fork=shanghai
fn exec_jumpi(ref context: ExecutionContext) {}
/// 0x57 - JUMPI operation.
/// Change the pc counter under a provided certain condition.
/// The new pc target has to be a JUMPDEST opcode.
/// # Specification: https://www.evm.codes/#57?fork=shanghai
fn exec_jumpi(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x5b - JUMPDEST operation
/// Serves as a check that JUMP or JUMPI was executed correctly.
/// # Specification: https://www.evm.codes/#5b?fork=shanghai
fn exec_jumpdest(ref context: ExecutionContext) {}
/// 0x5b - JUMPDEST operation
/// Serves as a check that JUMP or JUMPI was executed correctly.
/// # Specification: https://www.evm.codes/#5b?fork=shanghai
fn exec_jumpdest(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x50 - POP operation.
/// Pops the first item on the stack (top of the stack).
/// # Specification: https://www.evm.codes/#50?fork=shanghai
fn exec_pop(ref context: ExecutionContext) {}
/// 0x50 - POP operation.
/// Pops the first item on the stack (top of the stack).
/// # Specification: https://www.evm.codes/#50?fork=shanghai
fn exec_pop(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x53 - MSTORE8 operation.
/// Save single byte to memory
/// # Specification: https://www.evm.codes/#53?fork=shanghai
fn exec_mstore8(ref context: ExecutionContext) {}
/// 0x53 - MSTORE8 operation.
/// Save single byte to memory
/// # Specification: https://www.evm.codes/#53?fork=shanghai
fn exec_mstore8(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x55 - SSTORE operation
/// Save 32-byte word to storage.
/// # Specification: https://www.evm.codes/#55?fork=shanghai
fn exec_sstore(ref context: ExecutionContext) {}
/// 0x55 - SSTORE operation
/// Save 32-byte word to storage.
/// # Specification: https://www.evm.codes/#55?fork=shanghai
fn exec_sstore(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x54 - SLOAD operation
/// Load from storage.
/// # Specification: https://www.evm.codes/#54?fork=shanghai
fn exec_sload(ref context: ExecutionContext) {}
/// 0x54 - SLOAD operation
/// Load from storage.
/// # Specification: https://www.evm.codes/#54?fork=shanghai
fn exec_sload(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}

/// 0x5A - GAS operation
/// Get the amount of available gas, including the corresponding reduction for the cost of this instruction.
/// # Specification: https://www.evm.codes/#5a?fork=shanghai
fn exec_gas(ref context: ExecutionContext) {}
/// 0x5A - GAS operation
/// Get the amount of available gas, including the corresponding reduction for the cost of this instruction.
/// # Specification: https://www.evm.codes/#5a?fork=shanghai
fn exec_gas(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
}
}
26 changes: 13 additions & 13 deletions crates/evm/src/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use evm::instructions::{
duplication_operations, environmental_information, exchange_operations, logging_operations,
memory_operations, sha3, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait,
system_operations, BlockInformationTrait, DuplicationOperationsTrait,
EnvironmentInformationTrait, PushOperationsTrait
EnvironmentInformationTrait, PushOperationsTrait, MemoryOperationsTrait
};
use result::ResultTrait;

Expand Down Expand Up @@ -295,51 +295,51 @@ impl EVMInterpreterImpl of EVMInterpreterTrait {
}
if opcode == 80 {
// POP
memory_operations::exec_pop(ref context);
context.exec_pop();
}
if opcode == 81 {
// MLOAD
memory_operations::exec_mload(ref context);
context.exec_mload();
}
if opcode == 82 {
// MSTORE
memory_operations::exec_mstore(ref context);
context.exec_mstore();
}
if opcode == 83 {
// MSTORE8
memory_operations::exec_mstore8(ref context);
context.exec_mstore8();
}
if opcode == 84 {
// SLOAD
memory_operations::exec_sload(ref context);
context.exec_sload();
}
if opcode == 85 {
// SSTORE
memory_operations::exec_sstore(ref context);
context.exec_sstore();
}
if opcode == 86 {
// JUMP
memory_operations::exec_jump(ref context);
context.exec_jump();
}
if opcode == 87 {
// JUMPI
memory_operations::exec_jumpi(ref context);
context.exec_jumpi();
}
if opcode == 88 {
// PC
memory_operations::exec_pc(ref context);
context.exec_pc();
}
if opcode == 89 {
// MSIZE
memory_operations::exec_msize(ref context);
context.exec_msize();
}
if opcode == 90 {
// GAS
memory_operations::exec_gas(ref context);
context.exec_gas();
}
if opcode == 91 {
// JUMPDEST
memory_operations::exec_jumpdest(ref context);
context.exec_jumpdest();
}
if opcode == 95 {
// PUSH0
Expand Down
1 change: 1 addition & 0 deletions crates/evm/src/tests/test_instructions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod test_duplication_operations;
mod test_block_information;
mod test_environment_information;
mod test_push_operations;
mod test_memory_operations;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use evm::instructions::MemoryOperationsTrait;
use evm::tests::test_utils::setup_execution_context;
use evm::stack::StackTrait;
use evm::memory::{MemoryTrait};
use evm::context::{ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct};

// 0x51 - MLOAD

#[test]
#[available_gas(20000000000)]
#[should_panic(expected: ('MLOAD not implement yet',))]
fn test_exec_mload_should_load_a_value_from_memory() {
assert_mload(0x1, 0, 0x1, 32);
}

#[test]
#[available_gas(20000000000)]
#[should_panic(expected: ('MLOAD not implement yet',))]
fn test_exec_mload_should_load_a_value_from_memory_with_memory_expansion() {
assert_mload(0x1, 16, 0x100000000000000000000000000000000, 64);
}

#[test]
#[available_gas(20000000000)]
#[should_panic(expected: ('MLOAD not implement yet',))]
fn test_exec_mload_should_load_a_value_from_memory_with_offset_larger_than_msize() {
assert_mload(0x1, 684, 0x0, 736);
}

fn assert_mload(memory_value: u256, input: u256, expected_value: u256, expected_memory_size: u32) {
khaeljy marked this conversation as resolved.
Show resolved Hide resolved
// Given
let mut ctx = setup_execution_context();
ctx.memory.store(memory_value, 0);

ctx.stack.push(input);

// When
ctx.exec_mload();

// Then
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.pop().unwrap() == expected_value, 'mload failed');
assert(ctx.memory.bytes_len == expected_memory_size, 'memory size error');
}
Loading