Skip to content

Commit

Permalink
feat(bus-mapping): add sanity check for jumpi (#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
lispc authored Apr 10, 2024
1 parent b84dfdf commit 2df6162
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod extcodecopy;
mod extcodehash;
mod extcodesize;
mod gasprice;
mod jumpi;
mod logs;
mod mload;
mod mstore;
Expand Down Expand Up @@ -78,7 +79,7 @@ mod memory_expansion_test;
#[cfg(feature = "test")]
pub use callop::tests::PrecompileCallArgs;

use self::{pushn::PushN, sha3::Sha3};
use self::{jumpi::Jumpi, pushn::PushN, sha3::Sha3};

use address::Address;
use arithmetic::ArithmeticOpcode;
Expand Down Expand Up @@ -234,7 +235,7 @@ fn fn_gen_associated_ops(opcode_id: &OpcodeId) -> FnGenAssociatedOps {
OpcodeId::SLOAD => Sload::gen_associated_ops,
OpcodeId::SSTORE => Sstore::gen_associated_ops,
OpcodeId::JUMP => StackPopOnlyOpcode::<1>::gen_associated_ops,
OpcodeId::JUMPI => StackPopOnlyOpcode::<2>::gen_associated_ops,
OpcodeId::JUMPI => Jumpi::gen_associated_ops,
OpcodeId::PC => Pc::gen_associated_ops,
OpcodeId::MSIZE => Msize::gen_associated_ops,
OpcodeId::GAS => Gas::gen_associated_ops,
Expand Down
49 changes: 49 additions & 0 deletions bus-mapping/src/evm/opcodes/jumpi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use super::Opcode;
use crate::{
circuit_input_builder::{CircuitInputStateRef, ExecStep},
Error,
};
use eth_types::GethExecStep;

#[derive(Debug, Copy, Clone)]
pub(crate) struct Jumpi;

impl Opcode for Jumpi {
fn gen_associated_ops(
state: &mut CircuitInputStateRef,
geth_steps: &[GethExecStep],
) -> Result<Vec<ExecStep>, Error> {
let geth_step = &geth_steps[0];
let mut exec_step = state.new_step(geth_step)?;

let pc = state.stack_pop(&mut exec_step)?;
let condition = state.stack_pop(&mut exec_step)?;

if let Some(next_step) = geth_steps.get(1) {
if condition == 0.into() {
assert_eq!(
next_step.pc.0,
geth_step.pc.0 + 1,
"jumpi should not jump: current step {:?} next step {:?}",
geth_step,
next_step
);
} else {
assert_eq!(
next_step.pc.0 as u64,
pc.low_u64(),
"jumpi should jump: current step {:?} next step {:?}",
geth_step,
next_step
);
}
}

#[cfg(feature = "enable-stack")]
for (i, v) in [pc, condition].into_iter().enumerate() {
assert_eq!(v, geth_step.stack.nth_last(i)?);
}

Ok(vec![exec_step])
}
}

0 comments on commit 2df6162

Please sign in to comment.