use crate::{
evm_circuit::{
execution::ExecutionGadget,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
CachedRegion,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
util::{
word::{WordExpr, WordLoHiCell},
Expr,
},
};
use eth_types::{evm_types::OpcodeId, Field};
use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct DupGadget<F> {
same_context: SameContextGadget<F>,
value: WordLoHiCell<F>,
}
impl<F: Field> ExecutionGadget<F> for DupGadget<F> {
const NAME: &'static str = "DUP";
const EXECUTION_STATE: ExecutionState = ExecutionState::DUP;
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
let value = cb.query_word_unchecked();
let dup_offset = opcode.expr() - OpcodeId::DUP1.expr();
cb.stack_lookup(false.expr(), dup_offset, value.to_word());
cb.stack_push(value.to_word());
let step_state_transition = StepStateTransition {
rw_counter: Delta(2.expr()),
program_counter: Delta(1.expr()),
stack_pointer: Delta((-1).expr()),
gas_left: Delta(-OpcodeId::DUP1.constant_gas_cost().expr()),
..Default::default()
};
let same_context = SameContextGadget::construct(cb, opcode, step_state_transition);
Self {
same_context,
value,
}
}
fn assign_exec_step(
&self,
region: &mut CachedRegion<'_, '_, F>,
offset: usize,
block: &Block<F>,
_chunk: &Chunk<F>,
_: &Transaction,
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;
let value = block.get_rws(step, 0).stack_value();
self.value.assign_u256(region, offset, value)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use crate::{evm_circuit::test::rand_word, test_util::CircuitTestBuilder};
use eth_types::{bytecode, evm_types::OpcodeId, Word};
use mock::TestContext;
fn test_ok(opcode: OpcodeId, value: Word) {
let n = opcode.postfix().expect("opcode with postfix");
let mut bytecode = bytecode! {
PUSH32(value)
};
for _ in 0..n - 1 {
bytecode.op_dup1();
}
bytecode.append(&bytecode! {
.write_op(opcode)
STOP
});
CircuitTestBuilder::new_from_test_ctx(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
)
.run();
}
#[test]
fn dup_gadget_simple() {
test_ok(OpcodeId::DUP1, Word::max_value());
test_ok(OpcodeId::DUP2, Word::max_value());
test_ok(OpcodeId::DUP15, Word::max_value());
test_ok(OpcodeId::DUP16, Word::max_value());
}
#[test]
#[ignore]
fn dup_gadget_rand() {
for opcode in vec![
OpcodeId::DUP1,
OpcodeId::DUP2,
OpcodeId::DUP3,
OpcodeId::DUP4,
OpcodeId::DUP5,
OpcodeId::DUP6,
OpcodeId::DUP7,
OpcodeId::DUP8,
OpcodeId::DUP9,
OpcodeId::DUP10,
OpcodeId::DUP11,
OpcodeId::DUP12,
OpcodeId::DUP13,
OpcodeId::DUP14,
OpcodeId::DUP15,
OpcodeId::DUP16,
]
.into_iter()
{
test_ok(opcode, rand_word());
}
}
}