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},
},
table::CallContextFieldTag,
util::{
word::{WordExpr, WordLoHiCell},
Expr,
},
};
use bus_mapping::evm::OpcodeId;
use eth_types::Field;
use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct CallValueGadget<F> {
same_context: SameContextGadget<F>,
call_value: WordLoHiCell<F>,
}
impl<F: Field> ExecutionGadget<F> for CallValueGadget<F> {
const NAME: &'static str = "CALLVALUE";
const EXECUTION_STATE: ExecutionState = ExecutionState::CALLVALUE;
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let call_value = cb.query_word_unchecked();
cb.call_context_lookup_read(
None, CallContextFieldTag::Value,
call_value.to_word(),
);
cb.stack_push(call_value.to_word());
let opcode = cb.query_cell();
let step_state_transition = StepStateTransition {
rw_counter: Delta(2.expr()),
program_counter: Delta(1.expr()),
stack_pointer: Delta((-1).expr()),
gas_left: Delta(-OpcodeId::CALLVALUE.constant_gas_cost().expr()),
..Default::default()
};
let same_context = SameContextGadget::construct(cb, opcode, step_state_transition);
Self {
same_context,
call_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 call_value = block.get_rws(step, 1).stack_value();
self.call_value.assign_u256(region, offset, call_value)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use crate::test_util::CircuitTestBuilder;
use eth_types::bytecode;
use mock::TestContext;
#[test]
fn callvalue_gadget_test() {
let bytecode = bytecode! {
CALLVALUE
STOP
};
CircuitTestBuilder::new_from_test_ctx(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
)
.run();
}
}