1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use crate::{
    evm_circuit::{
        param::N_BYTES_U64,
        step::ExecutionState,
        util::{
            constraint_builder::{
                ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition, Transition::*,
            },
            math_gadget::{
                AddWordsGadget, ConstantDivisionGadget, IsEqualGadget, MulWordByU64Gadget,
            },
            CachedRegion, Cell,
        },
        witness::{Block, Transaction},
    },
    table::{CallContextFieldTag, TxContextFieldTag, TxReceiptFieldTag},
    util::word::{Word32Cell, WordLoHi, WordLoHiCell},
};
use bus_mapping::operation::Target;
use eth_types::{evm_types::GasCost, Field};
use gadgets::util::{select, Expr, Scalar};
use halo2_proofs::{
    circuit::Value,
    plonk::{Error, Expression},
};
use strum::EnumCount;

/// Gadget for beginning a tx
#[derive(Clone, Debug)]
pub(crate) struct BeginTxHelperGadget<F> {
    // tx_id is query in current scope. The range should be determined here
    pub(crate) tx_id: Cell<F>,
}

impl<F: Field> BeginTxHelperGadget<F> {
    pub(crate) fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
        // Use rw_counter of the step which triggers next call as its call_id.
        let call_id: Cell<F> = cb.curr.state.rw_counter.clone();

        let tx_id = cb.query_cell(); // already constrain `if step_first && tx_id = 1` and `tx_id += 1` at EndTx
        cb.debug_expression("tx_id", tx_id.expr());
        cb.call_context_lookup_write(
            Some(call_id.expr()),
            CallContextFieldTag::TxId,
            WordLoHi::from_lo_unchecked(tx_id.expr()),
        ); // rwc_delta += 1

        // Add first BeginTx step constraint to have id == 1
        cb.step_first(|cb| {
            cb.require_equal("tx_id is initialized to be 1", tx_id.expr(), 1.expr());
        });

        Self { tx_id }
    }

    pub(crate) fn assign(
        &self,
        region: &mut CachedRegion<'_, '_, F>,
        offset: usize,
        tx: &Transaction,
    ) -> Result<(), Error> {
        self.tx_id
            .assign(region, offset, Value::known(tx.id.scalar()))?;

        Ok(())
    }
}

/// Gadget for ending a tx
#[derive(Clone, Debug)]
pub(crate) struct EndTxHelperGadget<F> {
    current_cumulative_gas_used: Cell<F>,
    is_first_tx: IsEqualGadget<F>,
}

impl<F: Field> EndTxHelperGadget<F> {
    pub(crate) fn construct(
        cb: &mut EVMConstraintBuilder<F>,
        tx_id: Expression<F>,
        is_persistent: Expression<F>,
        gas_used: Expression<F>,
        num_rw: Expression<F>,
    ) -> Self {
        let is_first_tx = cb.is_eq(tx_id.expr(), 1.expr());

        // Constrain tx receipt fields
        cb.tx_receipt_lookup(
            1.expr(),
            tx_id.expr(),
            TxReceiptFieldTag::PostStateOrStatus,
            is_persistent.expr(),
        );
        cb.tx_receipt_lookup(
            1.expr(),
            tx_id.expr(),
            TxReceiptFieldTag::LogLength,
            cb.curr.state.log_id.expr(),
        );
        let current_cumulative_gas_used = cb.query_cell();
        cb.condition(is_first_tx.expr(), |cb| {
            cb.require_zero(
                "current_cumulative_gas_used is zero when tx is first tx",
                current_cumulative_gas_used.expr(),
            );
        });
        cb.condition(1.expr() - is_first_tx.expr(), |cb| {
            cb.tx_receipt_lookup(
                0.expr(),
                tx_id.expr() - 1.expr(),
                TxReceiptFieldTag::CumulativeGasUsed,
                current_cumulative_gas_used.expr(),
            );
        });
        cb.tx_receipt_lookup(
            1.expr(),
            tx_id.expr(),
            TxReceiptFieldTag::CumulativeGasUsed,
            gas_used + current_cumulative_gas_used.expr(),
        );

        // Transition
        let rw_counter_offset = num_rw.expr() - is_first_tx.expr();
        let next_begin = if cb.feature_config.invalid_tx {
            vec![ExecutionState::BeginTx, ExecutionState::InvalidTx]
        } else {
            vec![ExecutionState::BeginTx]
        };
        cb.condition(cb.next.execution_state_selector(next_begin), |cb| {
            let next_step_rwc = cb.next.state.rw_counter.expr();
            // lookup use next step initial rwc, thus lead to same record on rw table
            cb.call_context_lookup_write_with_counter(
                next_step_rwc.clone(),
                Some(next_step_rwc),
                CallContextFieldTag::TxId,
                // tx_id has been lookup and range_check above
                WordLoHi::from_lo_unchecked(tx_id.expr() + 1.expr()),
            );
            // minus 1.expr() because `call_context_lookup_write_with_counter` do not bump
            // rwc
            cb.require_step_state_transition(StepStateTransition {
                rw_counter: Delta(rw_counter_offset.clone()),
                ..StepStateTransition::any()
            });
        });
        cb.condition(
            cb.next
                .execution_state_selector([ExecutionState::EndBlock, ExecutionState::Padding]),
            |cb| {
                cb.require_step_state_transition(StepStateTransition {
                    rw_counter: Delta(rw_counter_offset.expr()),
                    // We propagate call_id so that EndBlock can get the last tx_id
                    // in order to count processed txs.
                    call_id: Same,
                    ..StepStateTransition::any()
                });
            },
        );

        Self {
            is_first_tx,
            current_cumulative_gas_used,
        }
    }

    pub(crate) fn assign(
        &self,
        region: &mut CachedRegion<'_, '_, F>,
        offset: usize,
        block: &Block<F>,
        tx: &Transaction,
    ) -> Result<(), Error> {
        self.is_first_tx
            .assign(region, offset, F::from(tx.id), F::ONE)?;

        let current_cumulative_gas_used: u64 = if tx.id == 1 {
            0
        } else {
            // first transaction needs TxReceiptFieldTag::COUNT(3) lookups to tx receipt,
            // while later transactions need 4 (with one extra cumulative gas read) lookups
            let rw = &block.rws[(
                Target::TxReceipt,
                (tx.id as usize - 2) * (TxReceiptFieldTag::COUNT + 1) + 2,
            )];
            rw.receipt_value()
        };
        self.current_cumulative_gas_used.assign(
            region,
            offset,
            Value::known(F::from(current_cumulative_gas_used)),
        )?;

        Ok(())
    }
}

/// Gadget for reading the tx data
#[derive(Clone, Debug)]
pub(crate) struct TxDataGadget<F> {
    pub(crate) nonce: Cell<F>,
    pub(crate) caller_address: WordLoHiCell<F>,
    pub(crate) callee_address: WordLoHiCell<F>,
    pub(crate) is_create: Cell<F>,
    pub(crate) gas: Cell<F>,
    pub(crate) call_data_length: Cell<F>,
    pub(crate) call_data_gas_cost: Cell<F>,
    pub(crate) gas_price: Word32Cell<F>,
    pub(crate) value: Word32Cell<F>,

    pub(crate) mul_gas_fee_by_gas: MulWordByU64Gadget<F>,
    pub(crate) call_data_word_length: ConstantDivisionGadget<F, N_BYTES_U64>,

    pub(crate) gas_mul_gas_price_plus_value: Option<AddWordsGadget<F, 2, false>>,
    pub(crate) cost_sum: Option<Word32Cell<F>>,
}

impl<F: Field> TxDataGadget<F> {
    pub(crate) fn configure(
        cb: &mut EVMConstraintBuilder<F>,
        tx_id: Expression<F>,
        calculate_total_cost: bool,
    ) -> Self {
        let [nonce, gas, is_create, call_data_length, call_data_gas_cost] = [
            TxContextFieldTag::Nonce,
            TxContextFieldTag::Gas,
            TxContextFieldTag::IsCreate,
            TxContextFieldTag::CallDataLength,
            TxContextFieldTag::CallDataGasCost,
        ]
        .map(|field_tag| cb.tx_context(tx_id.expr(), field_tag, None));
        let [gas_price, value] = [TxContextFieldTag::GasPrice, TxContextFieldTag::Value]
            .map(|field_tag| cb.tx_context_as_word32(tx_id.expr(), field_tag, None));

        let [caller_address, callee_address] = [
            TxContextFieldTag::CallerAddress,
            TxContextFieldTag::CalleeAddress,
        ]
        .map(|field_tag| cb.tx_context_as_word(tx_id.expr(), field_tag, None));

        // TODO: Implement EIP 1559 (currently it only supports legacy
        // transaction format)
        // Calculate transaction gas fee
        let mul_gas_fee_by_gas = MulWordByU64Gadget::construct(cb, gas_price.clone(), gas.expr());

        let call_data_word_length = cb.div_by_const(call_data_length.expr() + 31.expr(), 32);

        let (cost_sum, gas_mul_gas_price_plus_value) = if calculate_total_cost {
            let cost_sum = cb.query_word32();
            let gas_mul_gas_price_plus_value = AddWordsGadget::construct(
                cb,
                [mul_gas_fee_by_gas.product().clone(), value.clone()],
                cost_sum.clone(),
            );
            (Some(cost_sum), Some(gas_mul_gas_price_plus_value))
        } else {
            (None, None)
        };

        Self {
            nonce,
            is_create,
            gas,
            call_data_length,
            call_data_gas_cost,
            gas_price,
            value,
            mul_gas_fee_by_gas,
            call_data_word_length,
            caller_address,
            callee_address,
            gas_mul_gas_price_plus_value,
            cost_sum,
        }
    }

    pub(crate) fn intrinsic_gas(&self) -> Expression<F> {
        // Calculate gas cost of init code for EIP-3860.
        let init_code_gas_cost = select::expr(
            self.is_create.expr(),
            self.call_data_word_length.quotient().expr()
                * eth_types::evm_types::INIT_CODE_WORD_GAS.expr(),
            0.expr(),
        );

        select::expr(
            self.is_create.expr(),
            GasCost::CREATION_TX.expr(),
            GasCost::TX.expr(),
        ) + self.call_data_gas_cost.expr()
            + init_code_gas_cost.expr()
    }

    pub(crate) fn total_cost(&self) -> Word32Cell<F> {
        self.gas_mul_gas_price_plus_value
            .clone()
            .unwrap()
            .sum()
            .clone()
    }

    pub(crate) fn assign(
        &self,
        region: &mut CachedRegion<'_, '_, F>,
        offset: usize,
        tx: &Transaction,
    ) -> Result<(), Error> {
        let gas_fee = tx.gas_price * tx.gas();

        self.nonce
            .assign(region, offset, Value::known(tx.nonce.as_u64().scalar()))?;
        self.is_create
            .assign(region, offset, Value::known(tx.is_create().scalar()))?;
        self.gas
            .assign(region, offset, Value::known(tx.gas().scalar()))?;
        self.call_data_length.assign(
            region,
            offset,
            Value::known(F::from(tx.call_data.len() as u64)),
        )?;
        self.call_data_gas_cost.assign(
            region,
            offset,
            Value::known(tx.call_data_gas_cost().scalar()),
        )?;
        self.call_data_word_length
            .assign(region, offset, tx.call_data.len() as u128 + 31)?;
        self.gas_price.assign_u256(region, offset, tx.gas_price)?;
        self.value.assign_u256(region, offset, tx.value)?;
        self.callee_address
            .assign_h160(region, offset, tx.to_or_contract_addr())?;
        self.caller_address.assign_h160(region, offset, tx.from)?;
        self.mul_gas_fee_by_gas
            .assign(region, offset, tx.gas_price, tx.gas(), gas_fee)?;
        let sum = gas_fee + tx.value;

        if self.cost_sum.is_some() && self.gas_mul_gas_price_plus_value.is_some() {
            self.cost_sum
                .as_ref()
                .unwrap()
                .assign_u256(region, offset, sum)?;
            self.gas_mul_gas_price_plus_value.as_ref().unwrap().assign(
                region,
                offset,
                [gas_fee, tx.value],
                sum,
            )?;
        }

        Ok(())
    }
}