From 08a4d23ee5a5dbdf0da2576060c6402659fa4180 Mon Sep 17 00:00:00 2001 From: Marcos Nicolau Date: Wed, 7 Aug 2024 11:05:04 -0300 Subject: [PATCH 1/2] Implement aux_mutating0 opcode --- src/op_handlers/context.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/op_handlers/context.rs b/src/op_handlers/context.rs index 56afd88d..303cb6bb 100644 --- a/src/op_handlers/context.rs +++ b/src/op_handlers/context.rs @@ -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> { From 58c21a7ee682484ead81200f7c50452cbd3022c2 Mon Sep 17 00:00:00 2001 From: Marcos Nicolau Date: Wed, 7 Aug 2024 13:25:32 -0300 Subject: [PATCH 2/2] Implement static_memory read/write opcodes --- src/lib.rs | 5 +- src/op_handlers/mod.rs | 1 + src/op_handlers/static_memory.rs | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/op_handlers/static_memory.rs diff --git a/src/lib.rs b/src/lib.rs index 0c0498c2..0fdfb66b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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 { diff --git a/src/op_handlers/mod.rs b/src/op_handlers/mod.rs index c99a3f7d..7286f567 100644 --- a/src/op_handlers/mod.rs +++ b/src/op_handlers/mod.rs @@ -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; diff --git a/src/op_handlers/static_memory.rs b/src/op_handlers/static_memory.rs new file mode 100644 index 00000000..9e8a4890 --- /dev/null +++ b/src/op_handlers/static_memory.rs @@ -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(()) +}