Skip to content

Commit

Permalink
Merge pull request #1412 from o1-labs/feature/mips/or
Browse files Browse the repository at this point in the history
Implement `or`
  • Loading branch information
dannywillems authored Dec 6, 2023
2 parents eee0c5c + 6524825 commit 5bdb545
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
54 changes: 52 additions & 2 deletions optimism/src/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,32 @@ pub trait InterpreterEnv {
position: Self::Position,
) -> Self::Variable;

/// Returns `x or y`, storing the result in `position`.
///
/// # Safety
///
/// There are no constraints on the returned value; callers must manually add constraints to
/// ensure that it is correctly constructed.
unsafe fn or_witness(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable;

/// Returns `x xor y`, storing the result in `position`.
///
/// # Safety
///
/// There are no constraints on the returned value; callers must manually add constraints to
/// ensure that it is correctly constructed.
unsafe fn xor_witness(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable;

fn copy(&mut self, x: &Self::Variable, position: Self::Position) -> Self::Variable;

fn set_halted(&mut self, flag: Self::Variable);
Expand Down Expand Up @@ -698,7 +724,19 @@ pub fn interpret_rtype<Env: InterpreterEnv>(env: &mut Env, instr: RTypeInstructi
RTypeInstruction::Sub => (),
RTypeInstruction::SubUnsigned => (),
RTypeInstruction::And => (),
RTypeInstruction::Or => (),
RTypeInstruction::Or => {
let rs = env.read_register(&rs);
let rt = env.read_register(&rt);
let res = {
// FIXME: Constrain
let pos = env.alloc_scratch();
unsafe { env.or_witness(&rs, &rt, pos) }
};
env.write_register(&rd, res);
env.set_instruction_pointer(next_instruction_pointer.clone());
env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32));
return;
}
RTypeInstruction::Xor => (),
RTypeInstruction::Nor => (),
RTypeInstruction::SetLessThan => {
Expand Down Expand Up @@ -874,7 +912,19 @@ pub fn interpret_itype<Env: InterpreterEnv>(env: &mut Env, instr: ITypeInstructi
ITypeInstruction::SetLessThanImmediateUnsigned => (),
ITypeInstruction::AndImmediate => (),
ITypeInstruction::OrImmediate => (),
ITypeInstruction::XorImmediate => (),
ITypeInstruction::XorImmediate => {
let rs = env.read_register(&rs);
let res = {
// FIXME: Constraint
let pos = env.alloc_scratch();
unsafe { env.xor_witness(&rs, &immediate, pos) }
};
env.write_register(&rt, res);
env.set_instruction_pointer(next_instruction_pointer.clone());
env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32));
// REMOVEME: when all itype instructions are implemented.
return;
}
ITypeInstruction::LoadUpperImmediate => {
// lui $reg, [most significant 16 bits of immediate]
let immediate_value = immediate * Env::constant(1 << 16);
Expand Down
22 changes: 22 additions & 0 deletions optimism/src/mips/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
res
}

unsafe fn or_witness(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable {
let res = x | y;
self.write_column(position, res.into());
res
}

unsafe fn xor_witness(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable {
let res = *x ^ *y;
self.write_column(position, res.into());
res
}

fn copy(&mut self, x: &Self::Variable, position: Self::Position) -> Self::Variable {
self.write_column(position, (*x).into());
*x
Expand Down

0 comments on commit 5bdb545

Please sign in to comment.