|
| 1 | +use super::super::{Case, Cell, Constraint, ExecutionStep}; |
| 2 | +use super::utils; |
| 3 | +use super::utils::common_cases::OutOfGasCase; |
| 4 | +use super::utils::constraint_builder::ConstraintBuilder; |
| 5 | +use super::{ |
| 6 | + CaseAllocation, CaseConfig, CoreStateInstance, OpExecutionState, OpGadget, |
| 7 | +}; |
| 8 | +use crate::impl_op_gadget; |
| 9 | +use crate::util::Expr; |
| 10 | +use bus_mapping::evm::{GasCost, OpcodeId}; |
| 11 | +use halo2::plonk::Error; |
| 12 | +use halo2::{arithmetic::FieldExt, circuit::Region}; |
| 13 | +use std::convert::TryInto; |
| 14 | + |
| 15 | +const GC_DELTA: usize = 0; |
| 16 | +const PC_DELTA: usize = 1; |
| 17 | +const SP_DELTA: usize = 0; |
| 18 | +const GAS: GasCost = GasCost::ONE; |
| 19 | + |
| 20 | +impl_op_gadget!( |
| 21 | + [JUMPDEST] |
| 22 | + JumpdestGadget { |
| 23 | + JumpdestSuccessCase(), |
| 24 | + OutOfGasCase(GAS.as_usize()), |
| 25 | + } |
| 26 | +); |
| 27 | + |
| 28 | +#[derive(Clone, Debug)] |
| 29 | +struct JumpdestSuccessCase<F> { |
| 30 | + case_selector: Cell<F>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<F: FieldExt> JumpdestSuccessCase<F> { |
| 34 | + pub(crate) const CASE_CONFIG: &'static CaseConfig = &CaseConfig { |
| 35 | + case: Case::Success, |
| 36 | + num_word: 0, |
| 37 | + num_cell: 0, |
| 38 | + will_halt: false, |
| 39 | + }; |
| 40 | + |
| 41 | + pub(crate) fn construct(alloc: &mut CaseAllocation<F>) -> Self { |
| 42 | + Self { |
| 43 | + case_selector: alloc.selector.clone(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub(crate) fn constraint( |
| 48 | + &self, |
| 49 | + state_curr: &OpExecutionState<F>, |
| 50 | + state_next: &OpExecutionState<F>, |
| 51 | + name: &'static str, |
| 52 | + ) -> Constraint<F> { |
| 53 | + let mut cb = ConstraintBuilder::default(); |
| 54 | + // State transitions |
| 55 | + utils::StateTransitions { |
| 56 | + gc_delta: Some(GC_DELTA.expr()), |
| 57 | + sp_delta: Some(SP_DELTA.expr()), |
| 58 | + pc_delta: Some(PC_DELTA.expr()), |
| 59 | + gas_delta: Some(GAS.expr()), |
| 60 | + } |
| 61 | + .constraints(&mut cb, state_curr, state_next); |
| 62 | + |
| 63 | + // Generate the constraint |
| 64 | + cb.constraint(self.case_selector.expr(), name) |
| 65 | + } |
| 66 | + |
| 67 | + fn assign( |
| 68 | + &self, |
| 69 | + region: &mut Region<'_, F>, |
| 70 | + offset: usize, |
| 71 | + state: &mut CoreStateInstance, |
| 72 | + _step: &ExecutionStep, |
| 73 | + ) -> Result<(), Error> { |
| 74 | + state.global_counter += GC_DELTA; |
| 75 | + state.program_counter += PC_DELTA; |
| 76 | + state.stack_pointer += SP_DELTA; |
| 77 | + state.gas_counter += GAS.as_usize(); |
| 78 | + |
| 79 | + self.case_selector |
| 80 | + .assign(region, offset, Some(F::from_u64(1))) |
| 81 | + .unwrap(); |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod test { |
| 88 | + use super::super::super::{ |
| 89 | + test::TestCircuit, Case, ExecutionStep, Operation, |
| 90 | + }; |
| 91 | + use bus_mapping::{evm::OpcodeId, operation::Target}; |
| 92 | + use halo2::{arithmetic::FieldExt, dev::MockProver}; |
| 93 | + use num::BigUint; |
| 94 | + use pasta_curves::pallas::Base; |
| 95 | + |
| 96 | + macro_rules! try_test_circuit { |
| 97 | + ($execution_steps:expr, $operations:expr, $result:expr) => {{ |
| 98 | + let circuit = |
| 99 | + TestCircuit::<Base>::new($execution_steps, $operations); |
| 100 | + let prover = MockProver::<Base>::run(10, &circuit, vec![]).unwrap(); |
| 101 | + assert_eq!(prover.verify(), $result); |
| 102 | + }}; |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn jumpdest_gadget() { |
| 107 | + try_test_circuit!( |
| 108 | + vec![ |
| 109 | + ExecutionStep { |
| 110 | + opcode: OpcodeId::PUSH2, |
| 111 | + case: Case::Success, |
| 112 | + values: vec![ |
| 113 | + BigUint::from(0x02_03u64), |
| 114 | + BigUint::from(0x01_01u64), |
| 115 | + ], |
| 116 | + }, |
| 117 | + ExecutionStep { |
| 118 | + // jumpdest |
| 119 | + opcode: OpcodeId::JUMPDEST, |
| 120 | + case: Case::Success, |
| 121 | + values: vec![], |
| 122 | + } |
| 123 | + ], |
| 124 | + vec![Operation { |
| 125 | + gc: 1, |
| 126 | + target: Target::Stack, |
| 127 | + is_write: true, |
| 128 | + values: [ |
| 129 | + Base::zero(), |
| 130 | + Base::from_u64(1023), |
| 131 | + Base::from_u64(2 + 3), |
| 132 | + Base::zero(), |
| 133 | + ] |
| 134 | + }], |
| 135 | + Ok(()) |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
0 commit comments