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

Implement static memory read/write opcodes #169

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use op_handlers::shift::rol;
use op_handlers::shift::ror;
use op_handlers::shift::shl;
use op_handlers::shift::shr;
use op_handlers::static_memory::{static_memory_read, static_memory_write};
use op_handlers::sub::sub;
use op_handlers::xor::xor;
pub use opcode::Opcode;
Expand Down Expand Up @@ -222,8 +223,8 @@ pub fn run(
UMAOpcode::AuxHeapRead => aux_heap_read(&mut vm, &opcode),
UMAOpcode::AuxHeapWrite => aux_heap_write(&mut vm, &opcode),
UMAOpcode::FatPointerRead => fat_pointer_read(&mut vm, &opcode),
UMAOpcode::StaticMemoryRead => todo!(),
UMAOpcode::StaticMemoryWrite => todo!(),
UMAOpcode::StaticMemoryRead => static_memory_read(&mut vm, &opcode),
UMAOpcode::StaticMemoryWrite => static_memory_write(&mut vm, &opcode),
},
};
if let Err(_err) = result {
Expand Down
3 changes: 2 additions & 1 deletion src/op_handlers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub fn set_context_u128(vm: &mut VMState, opcode: &Opcode) -> Result<(), EraVmEr
}

pub fn aux_mutating0(_vm: &mut VMState, _opcode: &Opcode) -> Result<(), EraVmError> {
panic!("Not yet implemented");
// does nothing for now
Ok(())
}

pub fn increment_tx_number(vm: &mut VMState, _opcode: &Opcode) -> Result<(), EraVmError> {
Expand Down
1 change: 1 addition & 0 deletions src/op_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ pub mod ptr_shrink;
pub mod ptr_sub;
pub mod ret;
pub mod shift;
pub mod static_memory;
pub mod sub;
pub mod xor;
78 changes: 78 additions & 0 deletions src/op_handlers/static_memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use u256::U256;
use zkevm_opcode_defs::{MAX_OFFSET_TO_DEREF_LOW_U32, STATIC_MEMORY_PAGE};

use crate::address_operands::address_operands_read;
use crate::eravm_error::{EraVmError, HeapError, OperandError};
use crate::value::TaggedValue;
use crate::{opcode::Opcode, state::VMState};

pub fn static_memory_read(vm: &mut VMState, opcode: &Opcode) -> Result<(), EraVmError> {
let (src0, _) = address_operands_read(vm, opcode)?;
if src0.is_pointer {
return Err(OperandError::InvalidSrcPointer(opcode.variant).into());
}

if src0.value > U256::from(MAX_OFFSET_TO_DEREF_LOW_U32) {
return Err(OperandError::InvalidSrcAddress(opcode.variant).into());
}
let addr = src0.value.low_u32();

let gas_cost = vm
.heaps
.get_mut(STATIC_MEMORY_PAGE)
.ok_or(HeapError::StoreOutOfBounds)?
.expand_memory(addr + 32);

vm.decrease_gas(gas_cost)?;

let value = vm
.heaps
.get(STATIC_MEMORY_PAGE)
.ok_or(HeapError::ReadOutOfBounds)?
.read(addr);

vm.set_register(opcode.dst0_index, TaggedValue::new_raw_integer(value));

if opcode.flag0_set {
// This flag is set if .inc is present
vm.set_register(
opcode.dst1_index,
TaggedValue::new_raw_integer(U256::from(addr + 32)),
);
}
Ok(())
}

pub fn static_memory_write(vm: &mut VMState, opcode: &Opcode) -> Result<(), EraVmError> {
let (src0, src1) = address_operands_read(vm, opcode)?;
if src0.is_pointer {
return Err(OperandError::InvalidSrcPointer(opcode.variant).into());
}

if src0.value > U256::from(MAX_OFFSET_TO_DEREF_LOW_U32) {
return Err(OperandError::InvalidSrcAddress(opcode.variant).into());
}
let addr = src0.value.low_u32();

let gas_cost = vm
.heaps
.get_mut(STATIC_MEMORY_PAGE)
.ok_or(HeapError::StoreOutOfBounds)?
.expand_memory(addr + 32);

vm.decrease_gas(gas_cost)?;

vm.heaps
.get_mut(STATIC_MEMORY_PAGE)
.ok_or(HeapError::ReadOutOfBounds)?
.store(addr, src1.value);

if opcode.flag0_set {
// This flag is set if .inc is present
vm.set_register(
opcode.dst1_index,
TaggedValue::new_raw_integer(U256::from(addr + 32)),
);
}
Ok(())
}
Loading