Skip to content

Commit

Permalink
Merge pull request #1426 from o1-labs/feature/mips/and
Browse files Browse the repository at this point in the history
Implement `and`
  • Loading branch information
dannywillems authored Dec 6, 2023
2 parents 9a2b3e4 + e015ef9 commit a1af812
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 @@ -597,6 +597,19 @@ 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 and_witness(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable;

/// Returns `x or y`, storing the result in `position`.
///
/// # Safety
Expand Down Expand Up @@ -792,7 +805,19 @@ pub fn interpret_rtype<Env: InterpreterEnv>(env: &mut Env, instr: RTypeInstructi
}
RTypeInstruction::Sub => (),
RTypeInstruction::SubUnsigned => (),
RTypeInstruction::And => (),
RTypeInstruction::And => {
let rs = env.read_register(&rs);
let rt = env.read_register(&rt);
let res = {
// FIXME: Constrain
let pos = env.alloc_scratch();
unsafe { env.and_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::Or => {
let rs = env.read_register(&rs);
let rt = env.read_register(&rt);
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 @@ -277,6 +277,17 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
res
}

unsafe fn and_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 or_witness(
&mut self,
x: &Self::Variable,
Expand Down

0 comments on commit a1af812

Please sign in to comment.