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

Add other accessors necessary for zkVM memory #1383

Merged
merged 5 commits into from
Dec 5, 2023
Merged
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
29 changes: 29 additions & 0 deletions optimism/src/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,35 @@ pub trait InterpreterEnv {
output: Self::Position,
) -> Self::Variable;

/// Set the memory value at address `addr` to `value`.
///
/// # 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`.
///
/// # 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,
output: Self::Position,
) -> Self::Variable;

/// Set the last 'access index' for the memory at address `addr` to `value`.
///
/// # 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);

fn get_immediate(&self) -> Self::Variable {
Expand Down
45 changes: 43 additions & 2 deletions optimism/src/mips/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Env<Fp> {
pub instruction_parts: InstructionParts<u32>,
pub instruction_counter: usize,
pub memory: Vec<(u32, Vec<u8>)>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't the content of the memory and registers of type Variable? Or does this casting only happen inside the interpreter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Variable type is only in the interpreter, yea.

pub memory_write_index: Vec<(u32, Vec<usize>)>,
pub memory_write_index: Vec<(u32, Vec<u32>)>, // TODO: u32 will not be big enough..
pub registers: Registers<u32>,
pub registers_write_index: Registers<usize>,
pub instruction_pointer: u32,
Expand Down Expand Up @@ -150,6 +150,47 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
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,
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;
}
}
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;
return;
}
}
panic!("Could not write to address")
}

fn set_instruction_pointer(&mut self, ip: Self::Variable) {
self.instruction_pointer = ip;
// Set next instruction pointer?
Expand Down Expand Up @@ -212,7 +253,7 @@ impl<Fp: Field> Env<Fp> {
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(),
Expand Down