From f6b3406669ea06d38f7248f356a6948c67c6593d Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 5 Dec 2023 23:44:57 +0000 Subject: [PATCH] Implement `and` --- optimism/src/mips/interpreter.rs | 27 ++++++++++++++++++++++++++- optimism/src/mips/witness.rs | 11 +++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 5bbfee7a3b..aaf2320efc 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -589,6 +589,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 @@ -784,7 +797,19 @@ pub fn interpret_rtype(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); diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 1ac0217dd4..2c2fad8264 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -277,6 +277,17 @@ impl InterpreterEnv for Env { 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,