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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Fixed lookup tables and dynamic lookup tables for the EVM circuit

use crate::{
    evm_circuit::step::{ExecutionState, ResponsibleOp},
    impl_expr,
    util::word::WordLoHi,
};
use bus_mapping::{evm::OpcodeId, precompile::PrecompileCalls};
use eth_types::Field;
use gadgets::util::Expr;
use halo2_proofs::plonk::Expression;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

#[derive(Clone, Copy, Debug, EnumIter)]
/// Tags for different fixed tables
pub enum FixedTableTag {
    /// x == 0
    Zero = 0,
    /// 0 <= x < 5
    Range5,
    /// 0 <= x < 16
    Range16,
    /// 0 <= x < 32
    Range32,
    /// 0 <= x < 64
    Range64,
    /// 0 <= x < 128
    Range128,
    /// 0 <= x < 256
    Range256,
    /// 0 <= x < 512
    Range512,
    /// 0 <= x < 1024
    Range1024,
    /// -128 <= x < 128
    SignByte,
    /// bitwise AND
    BitwiseAnd,
    /// bitwise OR
    BitwiseOr,
    /// bitwise XOR
    BitwiseXor,
    /// lookup for corresponding opcode
    ResponsibleOpcode,
    /// power of 2
    Pow2,
    /// Lookup constant gas cost for opcodes
    ConstantGasCost,
    /// Precompile information
    PrecompileInfo,
}
impl_expr!(FixedTableTag);

impl FixedTableTag {
    /// build up the fixed table row values
    pub(crate) fn build<F: Field>(&self) -> Box<dyn Iterator<Item = [F; 4]>> {
        let tag = F::from(*self as u64);
        match self {
            Self::Zero => Box::new((0..1).map(move |_| [tag, F::ZERO, F::ZERO, F::ZERO])),
            Self::Range5 => {
                Box::new((0..5).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range16 => {
                Box::new((0..16).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range32 => {
                Box::new((0..32).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range64 => {
                Box::new((0..64).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range128 => {
                Box::new((0..128).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range256 => {
                Box::new((0..256).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range512 => {
                Box::new((0..512).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::Range1024 => {
                Box::new((0..1024).map(move |value| [tag, F::from(value), F::ZERO, F::ZERO]))
            }
            Self::SignByte => Box::new((0..256).map(move |value| {
                [
                    tag,
                    F::from(value),
                    F::from((value >> 7) * 0xFFu64),
                    F::ZERO,
                ]
            })),
            Self::BitwiseAnd => Box::new((0..256).flat_map(move |lhs| {
                (0..256).map(move |rhs| [tag, F::from(lhs), F::from(rhs), F::from(lhs & rhs)])
            })),
            Self::BitwiseOr => Box::new((0..256).flat_map(move |lhs| {
                (0..256).map(move |rhs| [tag, F::from(lhs), F::from(rhs), F::from(lhs | rhs)])
            })),
            Self::BitwiseXor => Box::new((0..256).flat_map(move |lhs| {
                (0..256).map(move |rhs| [tag, F::from(lhs), F::from(rhs), F::from(lhs ^ rhs)])
            })),
            Self::ResponsibleOpcode => {
                Box::new(ExecutionState::iter().flat_map(move |execution_state| {
                    execution_state.responsible_opcodes().into_iter().map(
                        move |responsible_opcode| {
                            let (op, aux) = match responsible_opcode {
                                ResponsibleOp::Op(op) => (op, F::ZERO),
                                ResponsibleOp::InvalidStackPtr(op, stack_ptr) => {
                                    (op, F::from(u64::from(stack_ptr)))
                                }
                            };
                            [
                                tag,
                                F::from(execution_state.as_u64()),
                                F::from(op.as_u64()),
                                aux,
                            ]
                        },
                    )
                }))
            }
            Self::Pow2 => Box::new((0..256).map(move |value| {
                let (pow_lo, pow_hi) = if value < 128 {
                    (F::from_u128(1_u128 << value), F::from(0))
                } else {
                    (F::from(0), F::from_u128(1 << (value - 128)))
                };
                [tag, F::from(value), pow_lo, pow_hi]
            })),
            Self::ConstantGasCost => Box::new(
                OpcodeId::iter()
                    .filter(move |opcode| opcode.constant_gas_cost() > 0)
                    .map(move |opcode| {
                        [
                            tag,
                            F::from(opcode.as_u64()),
                            F::from(opcode.constant_gas_cost()),
                            F::ZERO,
                        ]
                    }),
            ),
            Self::PrecompileInfo => Box::new(
                vec![
                    PrecompileCalls::Ecrecover,
                    PrecompileCalls::Sha256,
                    PrecompileCalls::Ripemd160,
                    PrecompileCalls::Identity,
                    PrecompileCalls::Modexp,
                    PrecompileCalls::Bn128Add,
                    PrecompileCalls::Bn128Mul,
                    PrecompileCalls::Bn128Pairing,
                    PrecompileCalls::Blake2F,
                ]
                .into_iter()
                .map(move |precompile| {
                    [
                        tag,
                        F::from({
                            let state: ExecutionState = precompile.into();
                            state.as_u64()
                        }),
                        F::from(u64::from(precompile)),
                        F::from(precompile.base_gas_cost()),
                    ]
                }),
            ),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, EnumIter)]
/// Each item represents the lookup table to query
pub enum Table {
    /// The range check table for u8
    U8,
    /// The range check table for u16
    U16,
    /// The rest of the fixed table. See [`FixedTableTag`]
    Fixed,
    /// Lookup for transactions
    Tx,
    /// Lookup for read write operations
    Rw,
    /// Lookup for bytecode table
    Bytecode,
    /// Lookup for block constants
    Block,
    /// Lookup for copy table
    Copy,
    /// Lookup for keccak table
    Keccak,
    /// Lookup for exp table
    Exp,
    /// Lookup for sig table
    Sig,
    /// Lookup for chunk context
    ChunkCtx,
}

#[derive(Clone, Debug)]
/// Read-Write Table fields
pub(crate) struct RwValues<F> {
    /// The unique identifier for the Read or Write. Depending on context, this field could be used
    /// for Transaction ID or call ID
    id: Expression<F>,
    /// The position to Stack, Memory, or account, where the read or write takes place, depending
    /// on the cell value of the [`bus_mapping::operation::Target`].
    address: Expression<F>,
    /// Could be [`crate::table::CallContextFieldTag`], [`crate::table::AccountFieldTag`],
    /// [`crate::table::TxLogFieldTag`], or [`crate::table::TxReceiptFieldTag`] depending on
    /// the cell value of the [`bus_mapping::operation::Target`]
    field_tag: Expression<F>,
    /// Storage key of two limbs
    storage_key: WordLoHi<Expression<F>>,
    /// The current storage value
    value: WordLoHi<Expression<F>>,
    /// The previous storage value
    value_prev: WordLoHi<Expression<F>>,
    /// The initial storage value before the current transaction
    init_val: WordLoHi<Expression<F>>,
}

impl<F: Field> RwValues<F> {
    /// Constructor for RwValues
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        id: Expression<F>,
        address: Expression<F>,
        field_tag: Expression<F>,
        storage_key: WordLoHi<Expression<F>>,
        value: WordLoHi<Expression<F>>,
        value_prev: WordLoHi<Expression<F>>,
        init_val: WordLoHi<Expression<F>>,
    ) -> Self {
        Self {
            id,
            address,
            field_tag,
            storage_key,
            value,
            value_prev,
            init_val,
        }
    }

    pub(crate) fn revert_value(&self) -> Self {
        let new_self = self.clone();
        Self {
            value_prev: new_self.value,
            value: new_self.value_prev,
            ..new_self
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) enum Lookup<F> {
    /// Lookup to fixed table, which contains several pre-built tables such as
    /// range tables or bitwise tables.
    Fixed {
        /// Tag to specify which table to lookup.
        tag: Expression<F>,
        /// Values that must satisfy the pre-built relationship.
        values: [Expression<F>; 3],
    },
    /// Lookup to tx table, which contains transactions of this block.
    Tx {
        /// Id of transaction, the first transaction has id = 1.
        id: Expression<F>,
        /// Tag to specify which field to read.
        field_tag: Expression<F>,
        /// Index to specify which byte of calldata, which is only used when
        /// field_tag is Calldata, otherwise should be set to 0.
        index: Expression<F>,
        /// Value of the field.
        value: WordLoHi<Expression<F>>,
    },
    /// Lookup to read-write table, which contains read-write access records of
    /// time-aware data.
    Rw {
        /// Counter for how much read-write have been done, which stands for
        /// the sequential timestamp.
        counter: Expression<F>,
        /// A boolean value to specify if the access record is a read or write.
        is_write: Expression<F>,
        /// Tag to specify which read-write data to access, see RwTableTag for
        /// all tags. RwTableTag is bus_mapping::operation::Target (PR#1406)
        tag: Expression<F>,
        /// Values corresponding to the tag.
        values: RwValues<F>,
    },
    /// Lookup to bytecode table, which contains all used creation code and
    /// contract code.
    Bytecode {
        /// Hash to specify which code to read.
        hash: WordLoHi<Expression<F>>,
        /// Tag to specify whether its the bytecode length or byte value in the
        /// bytecode.
        tag: Expression<F>,
        /// Index to specify which byte of bytecode.
        index: Expression<F>,
        /// A boolean value to specify if the value is executable opcode or the
        /// data portion of PUSH* operations.
        is_code: Expression<F>,
        /// Value corresponding to the tag.
        value: Expression<F>,
    },
    /// Lookup to block table, which contains constants of this block.
    Block {
        /// Tag to specify which field to read.
        field_tag: Expression<F>,
        /// Stores the block number only when field_tag is BlockHash, otherwise
        /// should be set to 0.
        number: Expression<F>,
        /// Value of the field.
        value: WordLoHi<Expression<F>>,
    },
    /// Lookup to copy table.
    CopyTable {
        /// Whether the row is the first row of the copy event.
        is_first: Expression<F>,
        /// The source ID for the copy event.
        src_id: WordLoHi<Expression<F>>,
        /// The source tag for the copy event.
        src_tag: Expression<F>,
        /// The destination ID for the copy event.
        dst_id: WordLoHi<Expression<F>>,
        /// The destination tag for the copy event.
        dst_tag: Expression<F>,
        /// The source address where bytes are copied from.
        src_addr: Expression<F>,
        /// The source address where all source-side bytes have been copied.
        /// This does not necessarily mean there no more bytes to be copied, but
        /// any bytes following this address will indicating padding.
        src_addr_end: Expression<F>,
        /// The destination address at which bytes are copied.
        dst_addr: Expression<F>,
        /// The number of bytes to be copied in this copy event.
        length: Expression<F>,
        /// The RLC accumulator value, which is used for SHA3 opcode.
        rlc_acc: Expression<F>,
        /// The RW counter at the start of the copy event.
        rw_counter: Expression<F>,
        /// The RW counter that is incremented by the time all bytes have been
        /// copied specific to this copy event.
        rwc_inc: Expression<F>,
    },
    /// Lookup to keccak table.
    KeccakTable {
        /// Accumulator to the input.
        input_rlc: Expression<F>,
        /// Length of input that is being hashed.
        input_len: Expression<F>,
        /// Output (hash) until this state. hash will be split into multiple expression in little
        /// endian.
        output: WordLoHi<Expression<F>>,
    },
    /// Lookup to exponentiation table.
    ExpTable {
        identifier: Expression<F>,
        is_last: Expression<F>,
        base_limbs: [Expression<F>; 4],
        exponent_lo_hi: [Expression<F>; 2],
        exponentiation_lo_hi: [Expression<F>; 2],
    },
    SigTable {
        msg_hash: WordLoHi<Expression<F>>,
        sig_v: Expression<F>,
        sig_r: WordLoHi<Expression<F>>,
        sig_s: WordLoHi<Expression<F>>,
        recovered_addr: Expression<F>,
        is_valid: Expression<F>,
    },
    /// Lookup to block table, which contains constants of this block.
    ChunkCtx {
        /// Tag to specify which field to read.
        field_tag: Expression<F>,
        /// value
        value: Expression<F>,
    },

    /// Conditional lookup enabled by the first element.
    Conditional(Expression<F>, Box<Lookup<F>>),
}

impl<F: Field> Lookup<F> {
    pub(crate) fn conditional(self, condition: Expression<F>) -> Self {
        Self::Conditional(condition, self.into())
    }

    pub(crate) fn table(&self) -> Table {
        match self {
            Self::Fixed { .. } => Table::Fixed,
            Self::ChunkCtx { .. } => Table::ChunkCtx,
            Self::Tx { .. } => Table::Tx,
            Self::Rw { .. } => Table::Rw,
            Self::Bytecode { .. } => Table::Bytecode,
            Self::Block { .. } => Table::Block,
            Self::CopyTable { .. } => Table::Copy,
            Self::KeccakTable { .. } => Table::Keccak,
            Self::ExpTable { .. } => Table::Exp,
            Self::SigTable { .. } => Table::Sig,
            Self::Conditional(_, lookup) => lookup.table(),
        }
    }

    pub(crate) fn input_exprs(&self) -> Vec<Expression<F>> {
        match self {
            Self::Fixed { tag, values } => [vec![tag.clone()], values.to_vec()].concat(),
            Self::Tx {
                id,
                field_tag,
                index,
                value,
            } => vec![
                id.clone(),
                field_tag.clone(),
                index.clone(),
                value.lo(),
                value.hi(),
            ],
            Self::Rw {
                counter,
                is_write,
                tag,
                values,
            } => vec![
                counter.clone(),
                is_write.clone(),
                tag.clone(),
                values.id.clone(),
                values.address.clone(),
                values.field_tag.clone(),
                values.storage_key.lo(),
                values.storage_key.hi(),
                values.value.lo(),
                values.value.hi(),
                values.value_prev.lo(),
                values.value_prev.hi(),
                values.init_val.lo(),
                values.init_val.hi(),
            ],
            Self::Bytecode {
                hash,
                tag,
                index,
                is_code,
                value,
            } => vec![
                hash.lo(),
                hash.hi(),
                tag.clone(),
                index.clone(),
                is_code.clone(),
                value.clone(),
            ],
            Self::Block {
                field_tag,
                number,
                value,
            } => vec![field_tag.clone(), number.clone(), value.lo(), value.hi()],
            Self::CopyTable {
                is_first,
                src_id,
                src_tag,
                dst_id,
                dst_tag,
                src_addr,
                src_addr_end,
                dst_addr,
                length,
                rlc_acc,
                rw_counter,
                rwc_inc,
            } => vec![
                is_first.clone(),
                src_id.lo(),
                src_id.hi(),
                src_tag.clone(),
                dst_id.lo(),
                dst_id.hi(),
                dst_tag.clone(),
                src_addr.clone(),
                src_addr_end.clone(),
                dst_addr.clone(),
                length.clone(),
                rlc_acc.clone(),
                rw_counter.clone(),
                rwc_inc.clone(),
            ],
            Self::KeccakTable {
                input_rlc,
                input_len,
                output,
            } => vec![
                1.expr(), // is_enabled
                input_rlc.clone(),
                input_len.clone(),
                output.lo(),
                output.hi(),
            ],
            Self::ExpTable {
                identifier,
                is_last,
                base_limbs,
                exponent_lo_hi,
                exponentiation_lo_hi,
            } => vec![
                1.expr(), // is_step
                identifier.clone(),
                is_last.clone(),
                base_limbs[0].clone(),
                base_limbs[1].clone(),
                base_limbs[2].clone(),
                base_limbs[3].clone(),
                exponent_lo_hi[0].clone(),
                exponent_lo_hi[1].clone(),
                exponentiation_lo_hi[0].clone(),
                exponentiation_lo_hi[1].clone(),
            ],
            Self::SigTable {
                msg_hash,
                sig_v,
                sig_r,
                sig_s,
                recovered_addr,
                is_valid,
            } => vec![
                1.expr(), // q_enable
                msg_hash.lo(),
                msg_hash.hi(),
                sig_v.clone(),
                sig_r.lo(),
                sig_r.hi(),
                sig_s.lo(),
                sig_s.hi(),
                recovered_addr.clone(),
                is_valid.clone(),
            ],
            Self::ChunkCtx { field_tag, value } => vec![field_tag.clone(), value.clone()],
            Self::Conditional(condition, lookup) => lookup
                .input_exprs()
                .into_iter()
                .map(|expr| condition.clone() * expr)
                .collect(),
        }
    }
}