Skip to content

Commit

Permalink
Merge pull request #1413 from o1-labs/feature/mips/xori
Browse files Browse the repository at this point in the history
Implement `xori`
  • Loading branch information
dannywillems authored Dec 6, 2023
2 parents 9e92dc5 + 3d7f45f commit 6524825
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
27 changes: 26 additions & 1 deletion optimism/src/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,19 @@ pub trait InterpreterEnv {
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 @@ -891,7 +904,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
11 changes: 11 additions & 0 deletions optimism/src/mips/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
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 6524825

Please sign in to comment.