From 6f24be243254c4c0ad7a83054482051c2fc4ea46 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 02:06:10 +0000 Subject: [PATCH 1/4] Add accessor for last memory access --- optimism/src/mips/interpreter.rs | 10 ++++++++++ optimism/src/mips/witness.rs | 21 +++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 287ab080cf..18ea475525 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -216,6 +216,16 @@ pub trait InterpreterEnv { output: Self::Position, ) -> Self::Variable; + /// Fetch the last 'access index' that the memory at address `addr` was written at, and store + /// it in local position `output`. + /// + /// This is unsafe: no lookups or other constraints are added as part of this operation. + unsafe fn fetch_memory_access( + &mut self, + addr: &Self::Variable, + output: Self::Position, + ) -> Self::Variable; + fn set_instruction_pointer(&mut self, ip: Self::Variable); fn get_immediate(&self) -> Self::Variable { diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 3234a94e80..97b88bb9ee 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -47,7 +47,7 @@ pub struct Env { pub instruction_parts: InstructionParts, pub instruction_counter: usize, pub memory: Vec<(u32, Vec)>, - pub memory_write_index: Vec<(u32, Vec)>, + pub memory_write_index: Vec<(u32, Vec)>, // TODO: u32 will not be big enough.. pub registers: Registers, pub registers_write_index: Registers, pub instruction_pointer: u32, @@ -150,6 +150,23 @@ impl InterpreterEnv for Env { panic!("Could not access address") } + unsafe fn fetch_memory_access( + &mut self, + addr: &Self::Variable, + output: Self::Position, + ) -> Self::Variable { + let page = addr >> PAGE_ADDRESS_SIZE; + let page_address = (addr & PAGE_ADDRESS_MASK) as usize; + for (page_index, memory_write_index) in self.memory_write_index.iter() { + if *page_index == page { + let value = memory_write_index[page_address]; + self.write_column(output, value.into()); + return value.into(); + } + } + panic!("Could not access address") + } + fn set_instruction_pointer(&mut self, ip: Self::Variable) { self.instruction_pointer = ip; // Set next instruction pointer? @@ -212,7 +229,7 @@ impl Env { memory: initial_memory.clone(), memory_write_index: memory_offsets .iter() - .map(|offset| (*offset, vec![0usize; page_size])) + .map(|offset| (*offset, vec![0u32; page_size])) .collect(), registers: initial_registers.clone(), registers_write_index: Registers::default(), From 5f6687b196aefd3859a66fc5c1f1c4ae538be150 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 02:10:17 +0000 Subject: [PATCH 2/4] Add memory push operations to InstructionEnv --- optimism/src/mips/interpreter.rs | 10 ++++++++++ optimism/src/mips/witness.rs | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 18ea475525..205a99df49 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -216,6 +216,11 @@ pub trait InterpreterEnv { output: Self::Position, ) -> Self::Variable; + /// Set the memory value at address `addr` to `value`. + /// + /// This is unsafe: no lookups or other constraints are added as part of this operation. + unsafe fn push_memory(&mut self, addr: &Self::Variable, value: Self::Variable); + /// Fetch the last 'access index' that the memory at address `addr` was written at, and store /// it in local position `output`. /// @@ -226,6 +231,11 @@ pub trait InterpreterEnv { output: Self::Position, ) -> Self::Variable; + /// Set the last 'access index' for the memory at address `addr` to `value`. + /// + /// This is unsafe: no lookups or other constraints are added as part of this operation. + unsafe fn push_memory_access(&mut self, addr: &Self::Variable, value: Self::Variable); + fn set_instruction_pointer(&mut self, ip: Self::Variable); fn get_immediate(&self) -> Self::Variable { diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 97b88bb9ee..8de55b0407 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -150,6 +150,18 @@ impl InterpreterEnv for Env { panic!("Could not access address") } + unsafe fn push_memory(&mut self, addr: &Self::Variable, value: Self::Variable) { + let page = addr >> PAGE_ADDRESS_SIZE; + let page_address = (addr & PAGE_ADDRESS_MASK) as usize; + for (page_index, memory) in self.memory.iter_mut() { + if *page_index == page { + memory[page_address] = value.try_into().expect("push_memory values fit in a u8"); + return; + } + } + panic!("Could not write to address") + } + unsafe fn fetch_memory_access( &mut self, addr: &Self::Variable, @@ -167,6 +179,18 @@ impl InterpreterEnv for Env { panic!("Could not access address") } + unsafe fn push_memory_access(&mut self, addr: &Self::Variable, value: Self::Variable) { + let page = addr >> PAGE_ADDRESS_SIZE; + let page_address = (addr & PAGE_ADDRESS_MASK) as usize; + for (page_index, memory_write_index) in self.memory_write_index.iter_mut() { + if *page_index == page { + memory_write_index[page_address] = value.into(); + return; + } + } + panic!("Could not write to address") + } + fn set_instruction_pointer(&mut self, ip: Self::Variable) { self.instruction_pointer = ip; // Set next instruction pointer? From 4a6a78034608beea1f0605d9a6788120aa77142c Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 02:24:22 +0000 Subject: [PATCH 3/4] `#safety` --- optimism/src/mips/interpreter.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 863ac9b8ba..9e1b2c84a9 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -221,13 +221,19 @@ pub trait InterpreterEnv { /// Set the memory value at address `addr` to `value`. /// - /// This is unsafe: no lookups or other constraints are added as part of this operation. + /// # Safety + /// + /// No lookups or other constraints are added as part of this operation. The caller must + /// manually add the lookups for this memory operation. unsafe fn push_memory(&mut self, addr: &Self::Variable, value: Self::Variable); /// Fetch the last 'access index' that the memory at address `addr` was written at, and store /// it in local position `output`. /// - /// This is unsafe: no lookups or other constraints are added as part of this operation. + /// # Safety + /// + /// No lookups or other constraints are added as part of this operation. The caller must + /// manually add the lookups for this memory operation. unsafe fn fetch_memory_access( &mut self, addr: &Self::Variable, @@ -236,7 +242,10 @@ pub trait InterpreterEnv { /// Set the last 'access index' for the memory at address `addr` to `value`. /// - /// This is unsafe: no lookups or other constraints are added as part of this operation. + /// # Safety + /// + /// No lookups or other constraints are added as part of this operation. The caller must + /// manually add the lookups for this memory operation. unsafe fn push_memory_access(&mut self, addr: &Self::Variable, value: Self::Variable); fn set_instruction_pointer(&mut self, ip: Self::Variable); From 5d2697b47c4a09dbc8d773db9f62bebca4383552 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 02:24:58 +0000 Subject: [PATCH 4/4] Remove redundant `.into()`s --- optimism/src/mips/witness.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 8de55b0407..4784406c1d 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -173,7 +173,7 @@ impl InterpreterEnv for Env { if *page_index == page { let value = memory_write_index[page_address]; self.write_column(output, value.into()); - return value.into(); + return value; } } panic!("Could not access address") @@ -184,7 +184,7 @@ impl InterpreterEnv for Env { let page_address = (addr & PAGE_ADDRESS_MASK) as usize; for (page_index, memory_write_index) in self.memory_write_index.iter_mut() { if *page_index == page { - memory_write_index[page_address] = value.into(); + memory_write_index[page_address] = value; return; } }