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
use std::collections::BTreeMap;

use super::{
    rw::{RwFingerprints, ToVec},
    ExecStep, Rw, RwMap, Transaction,
};
use crate::{
    evm_circuit::{detect_fixed_table_tags, EvmCircuit},
    exp_circuit::param::OFFSET_INCREMENT,
    instance::public_data_convert,
    table::BlockContextFieldTag,
    util::{log2_ceil, unwrap_value, word::WordLoHi, SubCircuit},
    witness::Chunk,
};
use bus_mapping::{
    circuit_input_builder::{
        self, CopyEvent, ExpEvent, FeatureConfig, FixedCParams, PrecompileEvents, Withdrawal,
    },
    state_db::CodeDB,
    Error,
};
use eth_types::{sign_types::SignData, Address, Field, ToScalar, Word, H256};

use gadgets::permutation::get_permutation_fingerprints;
use halo2_proofs::circuit::Value;
use itertools::Itertools;

// TODO: Remove fields that are duplicated in`eth_block`
/// [`Block`] is the struct used by all circuits, which contains blockwise
/// data for witness generation. Used with [`Chunk`] for the i-th chunk witness.
#[derive(Debug, Clone, Default)]
pub struct Block<F> {
    /// The randomness for random linear combination
    pub randomness: F,
    /// Transactions in the block
    pub txs: Vec<Transaction>,
    /// Padding step that is repeated after the last transaction and before
    /// reaching the last EVM row.
    pub end_block: ExecStep,
    /// Read write events in the RwTable
    pub rws: RwMap,
    /// Read write events in the RwTable, sorted by address
    pub by_address_rws: Vec<Rw>,
    /// Bytecode used in the block
    pub bytecodes: CodeDB,
    /// The block context
    pub context: BlockContext,
    /// Copy events for the copy circuit's table.
    pub copy_events: Vec<CopyEvent>,
    /// Exponentiation traces for the exponentiation circuit's table.
    pub exp_events: Vec<ExpEvent>,
    /// Pad exponentiation circuit to make selectors fixed.
    pub exp_circuit_pad_to: usize,
    /// Circuit Setup Parameters
    pub circuits_params: FixedCParams,
    /// Feature Config
    pub feature_config: FeatureConfig,
    /// Inputs to the SHA3 opcode
    pub sha3_inputs: Vec<Vec<u8>>,
    /// State root of the previous block
    pub prev_state_root: Word, // TODO: Make this H256
    /// Keccak inputs
    pub keccak_inputs: Vec<Vec<u8>>,
    /// IO to/from the precompiled contract calls.
    pub precompile_events: PrecompileEvents,
    /// Original Block from geth
    pub eth_block: eth_types::Block<eth_types::Transaction>,
    /// rw_table padding meta data
    pub rw_padding_meta: BTreeMap<usize, i32>,
}

impl<F: Field> Block<F> {
    /// For each tx, for each step, print the rwc at the beginning of the step,
    /// and all the rw operations of the step.
    #[allow(dead_code, reason = "useful debug function")]
    pub(crate) fn debug_print_txs_steps_rw_ops(&self) {
        for (tx_idx, tx) in self.txs.iter().enumerate() {
            println!("tx {}", tx_idx);
            for step in tx.steps() {
                println!("> Step {:?}", step.exec_state);
                for rw_idx in 0..step.bus_mapping_instance.len() {
                    let rw = self.get_rws(step, rw_idx);
                    let rw_str = if rw.is_write() { "WRIT" } else { "READ" };
                    println!("  {} {} {:?}", rw.rw_counter(), rw_str, rw);
                }
            }
        }
    }

    /// Get signature (witness) from the block for tx signatures and ecRecover calls.
    pub(crate) fn get_sign_data(&self, padding: bool) -> Vec<SignData> {
        let mut signatures: Vec<SignData> = self
            .txs
            .iter()
            .map(|tx| tx.tx.sign_data(self.context.chain_id.as_u64()))
            .filter_map(|res| res.ok())
            .collect::<Vec<SignData>>();
        signatures.extend_from_slice(&self.precompile_events.get_ecrecover_events());
        if padding && self.txs.len() < self.circuits_params.max_txs {
            // padding tx's sign data
            signatures.push(
                Transaction::dummy()
                    .sign_data(self.context.chain_id.as_u64())
                    .unwrap(),
            );
        }
        signatures
    }

    /// Get a read-write record
    pub(crate) fn get_rws(&self, step: &ExecStep, index: usize) -> Rw {
        self.rws[step.rw_index(index)]
    }

    /// Return the list of withdrawals of this block.
    pub fn withdrawals(&self) -> Vec<Withdrawal> {
        let eth_withdrawals = self.eth_block.withdrawals.clone().unwrap_or_default();
        eth_withdrawals
            .iter()
            .map({
                |w| {
                    Withdrawal::new(
                        w.index.as_u64(),
                        w.validator_index.as_u64(),
                        w.address,
                        w.amount.as_u64(),
                    )
                    .unwrap()
                }
            })
            .collect_vec()
    }

    /// Return the root of withdrawals in this block
    pub fn withdrawals_root(&self) -> H256 {
        self.eth_block.withdrawals_root.unwrap_or_default()
    }

    /// Obtains the expected Circuit degree needed in order to be able to test
    /// the EvmCircuit with this block without needing to configure the
    /// `ConstraintSystem`.
    pub fn get_test_degree(&self, chunk: &Chunk<F>) -> u32 {
        let num_rows_required_for_execution_steps: usize =
            EvmCircuit::<F>::get_num_rows_required(self, chunk);
        let num_rows_required_for_rw_table: usize = self.circuits_params.max_rws;
        let num_rows_required_for_fixed_table: usize = detect_fixed_table_tags(self)
            .iter()
            .map(|tag| tag.build::<F>().count())
            .sum();
        let num_rows_required_for_bytecode_table =
            self.bytecodes.num_rows_required_for_bytecode_table();
        let num_rows_required_for_copy_table: usize =
            self.copy_events.iter().map(|c| c.bytes.len() * 2).sum();
        let num_rows_required_for_keccak_table: usize = self.keccak_inputs.len();
        let num_rows_required_for_tx_table: usize =
            self.txs.iter().map(|tx| 9 + tx.call_data.len()).sum();
        let num_rows_required_for_exp_table: usize = self
            .exp_events
            .iter()
            .map(|e| e.steps.len() * OFFSET_INCREMENT)
            .sum();

        let rows_needed: usize = itertools::max([
            num_rows_required_for_execution_steps,
            num_rows_required_for_rw_table,
            num_rows_required_for_fixed_table,
            num_rows_required_for_bytecode_table,
            num_rows_required_for_copy_table,
            num_rows_required_for_keccak_table,
            num_rows_required_for_tx_table,
            num_rows_required_for_exp_table,
            1 << 16, // u16 range lookup
        ])
        .unwrap();

        let k = log2_ceil(EvmCircuit::<F>::unusable_rows() + rows_needed);
        log::debug!(
            "num_rows_required_for rw_table={}, fixed_table={}, bytecode_table={}, \
            copy_table={}, keccak_table={}, tx_table={}, exp_table={}",
            num_rows_required_for_rw_table,
            num_rows_required_for_fixed_table,
            num_rows_required_for_bytecode_table,
            num_rows_required_for_copy_table,
            num_rows_required_for_keccak_table,
            num_rows_required_for_tx_table,
            num_rows_required_for_exp_table
        );
        log::debug!("evm circuit uses k = {}, rows = {}", k, rows_needed);
        k
    }
}

/// Block context for execution
#[derive(Debug, Default, Clone)]
pub struct BlockContext {
    /// The address of the miner for the block
    pub coinbase: Address,
    /// The gas limit of the block
    pub gas_limit: u64,
    /// The number of the block
    pub number: Word,
    /// The timestamp of the block
    pub timestamp: Word,
    /// The difficulty of the block
    pub difficulty: Word,
    /// The base fee, the minimum amount of gas fee for a transaction
    pub base_fee: Word,
    /// The hash of previous blocks
    pub history_hashes: Vec<Word>,
    /// The chain id
    pub chain_id: Word,
    /// The withdrawal root
    pub withdrawals_root: Word,
}

impl BlockContext {
    /// Assignments for block table
    pub fn table_assignments<F: Field>(&self) -> Vec<[Value<F>; 4]> {
        [
            vec![
                [
                    Value::known(F::from(BlockContextFieldTag::Coinbase as u64)),
                    Value::known(F::ZERO),
                    Value::known(WordLoHi::from(self.coinbase).lo()),
                    Value::known(WordLoHi::from(self.coinbase).hi()),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::Timestamp as u64)),
                    Value::known(F::ZERO),
                    Value::known(self.timestamp.to_scalar().unwrap()),
                    Value::known(F::ZERO),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::Number as u64)),
                    Value::known(F::ZERO),
                    Value::known(self.number.to_scalar().unwrap()),
                    Value::known(F::ZERO),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::Difficulty as u64)),
                    Value::known(F::ZERO),
                    Value::known(WordLoHi::from(self.difficulty).lo()),
                    Value::known(WordLoHi::from(self.difficulty).hi()),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::GasLimit as u64)),
                    Value::known(F::ZERO),
                    Value::known(F::from(self.gas_limit)),
                    Value::known(F::ZERO),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::BaseFee as u64)),
                    Value::known(F::ZERO),
                    Value::known(WordLoHi::from(self.base_fee).lo()),
                    Value::known(WordLoHi::from(self.base_fee).hi()),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::ChainId as u64)),
                    Value::known(F::ZERO),
                    Value::known(WordLoHi::from(self.chain_id).lo()),
                    Value::known(WordLoHi::from(self.chain_id).hi()),
                ],
                [
                    Value::known(F::from(BlockContextFieldTag::WithdrawalRoot as u64)),
                    Value::known(F::ZERO),
                    Value::known(WordLoHi::from(self.withdrawals_root).lo()),
                    Value::known(WordLoHi::from(self.withdrawals_root).hi()),
                ],
            ],
            {
                let len_history = self.history_hashes.len();
                self.history_hashes
                    .iter()
                    .enumerate()
                    .map(|(idx, hash)| {
                        [
                            Value::known(F::from(BlockContextFieldTag::BlockHash as u64)),
                            Value::known((self.number - len_history + idx).to_scalar().unwrap()),
                            Value::known(WordLoHi::from(*hash).lo()),
                            Value::known(WordLoHi::from(*hash).hi()),
                        ]
                    })
                    .collect()
            },
        ]
        .concat()
    }
}

impl From<&circuit_input_builder::Block> for BlockContext {
    fn from(block: &circuit_input_builder::Block) -> Self {
        Self {
            coinbase: block.coinbase,
            gas_limit: block.gas_limit,
            number: block.number,
            timestamp: block.timestamp,
            difficulty: block.difficulty,
            base_fee: block.base_fee,
            history_hashes: block.history_hashes.clone(),
            chain_id: block.chain_id,
            withdrawals_root: block.withdrawals_root().as_fixed_bytes().into(),
        }
    }
}

/// Convert a block struct in bus-mapping to a witness block used in circuits
pub fn block_convert<F: Field>(
    builder: &circuit_input_builder::CircuitInputBuilder<FixedCParams>,
) -> Result<Block<F>, Error> {
    let block = &builder.block;
    let code_db = &builder.code_db;
    let rws = RwMap::from(&block.container);
    let by_address_rws = rws.table_assignments(false);
    rws.check_value();

    // get padding statistics data via BtreeMap
    // TODO we can implement it in more efficient version via range sum
    let rw_padding_meta = builder
        .chunks
        .iter()
        .fold(BTreeMap::new(), |mut map, chunk| {
            assert!(
                chunk.ctx.rwc.0.saturating_sub(1) <= builder.circuits_params.max_rws,
                "max_rws size {} must larger than chunk rws size {}",
                builder.circuits_params.max_rws,
                chunk.ctx.rwc.0.saturating_sub(1),
            );
            // [chunk.ctx.rwc.0, builder.circuits_params.max_rws)
            (chunk.ctx.rwc.0..builder.circuits_params.max_rws).for_each(|padding_rw_counter| {
                *map.entry(padding_rw_counter).or_insert(0) += 1;
            });
            map
        });

    let mut block = Block {
        // randomness: F::from(0x100), // Special value to reveal elements after RLC
        randomness: F::from(0xcafeu64),
        context: block.into(),
        rws,
        by_address_rws,
        txs: block.txs().to_vec(),
        bytecodes: code_db.clone(),
        copy_events: block.copy_events.clone(),
        exp_events: block.exp_events.clone(),
        sha3_inputs: block.sha3_inputs.clone(),
        circuits_params: builder.circuits_params,
        feature_config: builder.feature_config,
        exp_circuit_pad_to: <usize>::default(),
        prev_state_root: block.prev_state_root,
        keccak_inputs: circuit_input_builder::keccak_inputs(block, code_db)?,
        precompile_events: block.precompile_events.clone(),
        eth_block: block.eth_block.clone(),
        end_block: block.end_block.clone(),
        rw_padding_meta,
    };
    let public_data = public_data_convert(&block);

    // We can use params from block
    // because max_txs and max_calldata are independent from Chunk
    let rpi_bytes = public_data.get_pi_bytes(
        block.circuits_params.max_txs,
        block.circuits_params.max_withdrawals,
        block.circuits_params.max_calldata,
    );
    // PI Circuit
    block.keccak_inputs.extend_from_slice(&[rpi_bytes]);

    Ok(block)
}

#[allow(dead_code)]
fn get_rwtable_fingerprints<F: Field>(
    alpha: F,
    gamma: F,
    prev_continuous_fingerprint: F,
    rows: &Vec<Rw>,
) -> RwFingerprints<F> {
    let x = rows.to2dvec();
    let fingerprints = get_permutation_fingerprints(
        &x,
        Value::known(alpha),
        Value::known(gamma),
        Value::known(prev_continuous_fingerprint),
    );

    fingerprints
        .first()
        .zip(fingerprints.last())
        .map(|((first_acc, first_row), (last_acc, last_row))| {
            RwFingerprints::new(
                unwrap_value(*first_row),
                unwrap_value(*last_row),
                unwrap_value(*first_acc),
                unwrap_value(*last_acc),
            )
        })
        .unwrap_or_default()
}