Skip to content

Commit

Permalink
Implement srl
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmr1993 committed Dec 5, 2023
1 parent e11a19c commit 845fae3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 25 additions & 1 deletion optimism/src/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,19 @@ pub trait InterpreterEnv {
position: Self::Position,
) -> Self::Variable;

/// Return the result of shifting `x` by `by`, storing the result in `position`.
///
/// # Safety
///
/// There are no constraints on the returned value; callers must assert the relationship with
/// the source variable `x` and the shift amount `by`.
unsafe fn shift_right(
&mut self,
x: &Self::Variable,
by: &Self::Variable,
position: Self::Position,
) -> Self::Variable;

/// Returns 1 if `x` is 0, or 0 otherwise, storing the result in `position`.
///
/// # Safety
Expand Down Expand Up @@ -665,7 +678,18 @@ pub fn interpret_rtype<Env: InterpreterEnv>(env: &mut Env, instr: RTypeInstructi
env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32));
return;
}
RTypeInstruction::ShiftRightLogical => (),
RTypeInstruction::ShiftRightLogical => {
let rt = env.read_register(&rt);
// FIXME: Constrain this value
let shifted = unsafe {
let pos = env.alloc_scratch();
env.shift_right(&rt, &shamt, pos)
};
env.write_register(&rd, shifted);
env.set_instruction_pointer(next_instruction_pointer.clone());
env.set_next_instruction_pointer(next_instruction_pointer + Env::constant(4u32));
return;
}
RTypeInstruction::ShiftRightArithmetic => (),
RTypeInstruction::ShiftLeftLogicalVariable => (),
RTypeInstruction::ShiftRightLogicalVariable => (),
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 @@ -227,6 +227,17 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
res
}

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

unsafe fn test_zero(&mut self, x: &Self::Variable, position: Self::Position) -> Self::Variable {
let res = if *x == 0 { 1 } else { 0 };
self.write_column(position, res.into());
Expand Down

0 comments on commit 845fae3

Please sign in to comment.