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
use halo2_proofs::{
    self,
    circuit::{AssignedCell, SimpleFloorPlanner},
    halo2curves::ff::{BatchInvert, WithSmallOrderMulGroup},
};

use halo2_proofs::{
    halo2curves::{
        bn256::Fr,
        group::{prime::PrimeCurveAffine, Curve},
        CurveAffine,
    },
    plonk::{Advice, Assigned, Assignment, Challenge, Fixed, FloorPlanner, Instance, Selector},
    poly::{
        commitment::{Blind, CommitmentScheme, Params},
        EvaluationDomain, LagrangeCoeff, Polynomial,
    },
};

use super::*;

/// The RwTable shared between EVM Circuit and State Circuit, which contains
/// traces of the EVM state operations.
#[derive(Clone, Copy, Debug)]
pub struct RwTable {
    /// Read Write Counter
    pub rw_counter: Column<Advice>,
    /// Is Write
    pub is_write: Column<Advice>,
    /// Tag (bus_mapping::operation::Target)
    pub tag: Column<Advice>,
    /// Key1 (Id)
    pub id: Column<Advice>,
    /// Key2 (Address)
    pub address: Column<Advice>,
    /// Key3 (FieldTag)
    pub field_tag: Column<Advice>,
    /// Key3 (StorageKey)
    pub storage_key: WordLoHi<Column<Advice>>,
    /// Value
    pub value: WordLoHi<Column<Advice>>,
    /// Value Previous
    pub value_prev: WordLoHi<Column<Advice>>,
    /// InitVal (Committed Value)
    pub init_val: WordLoHi<Column<Advice>>,
}

impl<F: Field> LookupTable<F> for RwTable {
    fn columns(&self) -> Vec<Column<Any>> {
        vec![
            self.rw_counter.into(),
            self.is_write.into(),
            self.tag.into(),
            self.id.into(),
            self.address.into(),
            self.field_tag.into(),
            self.storage_key.lo().into(),
            self.storage_key.hi().into(),
            self.value.lo().into(),
            self.value.hi().into(),
            self.value_prev.lo().into(),
            self.value_prev.hi().into(),
            self.init_val.lo().into(),
            self.init_val.hi().into(),
        ]
    }

    fn annotations(&self) -> Vec<String> {
        vec![
            String::from("rw_counter"),
            String::from("is_write"),
            String::from("tag"),
            String::from("id"),
            String::from("address"),
            String::from("field_tag"),
            String::from("storage_key_lo"),
            String::from("storage_key_hi"),
            String::from("value_lo"),
            String::from("value_hi"),
            String::from("value_prev_lo"),
            String::from("value_prev_hi"),
            String::from("init_val_lo"),
            String::from("init_val_hi"),
        ]
    }
}

impl RwTable {
    /// Construct a new RwTable
    pub fn construct<F: Field>(meta: &mut ConstraintSystem<F>) -> Self {
        Self {
            rw_counter: meta.unblinded_advice_column(),
            is_write: meta.unblinded_advice_column(),
            tag: meta.unblinded_advice_column(),
            id: meta.unblinded_advice_column(),
            address: meta.unblinded_advice_column(),
            field_tag: meta.unblinded_advice_column(),
            storage_key: WordLoHi::new([
                meta.unblinded_advice_column(),
                meta.unblinded_advice_column(),
            ]),
            value: WordLoHi::new([
                meta.unblinded_advice_column(),
                meta.unblinded_advice_column(),
            ]),
            value_prev: WordLoHi::new([
                meta.unblinded_advice_column(),
                meta.unblinded_advice_column(),
            ]),
            init_val: WordLoHi::new([
                meta.unblinded_advice_column(),
                meta.unblinded_advice_column(),
            ]),
        }
    }
    pub(crate) fn assign<F: Field>(
        &self,
        region: &mut Region<'_, F>,
        offset: usize,
        row: &RwRow<Value<F>>,
    ) -> Result<Vec<AssignedCell<F, F>>, Error> {
        let mut assigned_cells = vec![];
        for (column, value) in [
            (self.rw_counter, row.rw_counter),
            (self.is_write, row.is_write),
            (self.tag, row.tag),
            (self.id, row.id),
            (self.address, row.address),
            (self.field_tag, row.field_tag),
        ] {
            assigned_cells.push(region.assign_advice(
                || "assign rw row on rw table",
                column,
                offset,
                || value,
            )?);
        }
        for (column, value) in [
            (self.storage_key, row.storage_key),
            (self.value, row.value),
            (self.value_prev, row.value_prev),
            (self.init_val, row.init_val),
        ] {
            assigned_cells.extend(
                value
                    .assign_advice(region, || "assign rw row on rw table", column, offset)?
                    .limbs
                    .clone(),
            );
        }

        Ok(assigned_cells)
    }

    /// Assign the `RwTable` from a `RwMap`, following the same
    /// table layout that the State Circuit uses.
    pub fn load<F: Field>(
        &self,
        layouter: &mut impl Layouter<F>,
        rws: &[Rw],
        n_rows: usize,
        prev_chunk_last_rw: Option<Rw>,
    ) -> Result<(), Error> {
        layouter.assign_region(
            || "rw table",
            |mut region| {
                self.load_with_region(&mut region, rws, n_rows, prev_chunk_last_rw)
                    .map(|_| ())
            },
        )
    }

    pub(crate) fn load_with_region<F: Field>(
        &self,
        region: &mut Region<'_, F>,
        rws: &[Rw],
        n_rows: usize,
        prev_chunk_last_rw: Option<Rw>,
    ) -> Result<Vec<Rw>, Error> {
        let (rows, _) = RwMap::table_assignments_padding(rws, n_rows, prev_chunk_last_rw);
        for (offset, row) in rows.iter().enumerate() {
            self.assign(region, offset, &row.table_assignment())?;
        }
        Ok(rows)
    }
}

/// get rw table column commitment
/// implementation snippet from halo2 `create_proof` https://github.com/privacy-scaling-explorations/halo2/blob/9b33f9ce524dbb9133fc8b9638b2afd0571659a8/halo2_proofs/src/plonk/prover.rs#L37
#[allow(unused)]
pub fn get_rwtable_cols_commitment<Scheme: CommitmentScheme>(
    degree: usize,
    rws: &[Rw],
    n_rows: usize,
    params_prover: &Scheme::ParamsProver,
) -> Vec<<Scheme as CommitmentScheme>::Curve>
where
    <Scheme as CommitmentScheme>::Scalar: WithSmallOrderMulGroup<3> + Field,
{
    struct WitnessCollection<F: Field> {
        advice: Vec<Polynomial<Assigned<F>, LagrangeCoeff>>,
        _marker: std::marker::PhantomData<F>,
    }

    impl<F: Field> Assignment<F> for WitnessCollection<F> {
        fn enter_region<NR, N>(&mut self, _: N)
        where
            NR: Into<String>,
            N: FnOnce() -> NR,
        {
            // Do nothing; we don't care about regions in this context.
        }

        fn exit_region(&mut self) {
            // Do nothing; we don't care about regions in this context.
        }

        fn enable_selector<A, AR>(&mut self, _: A, _: &Selector, _: usize) -> Result<(), Error>
        where
            A: FnOnce() -> AR,
            AR: Into<String>,
        {
            // We only care about advice columns here

            Ok(())
        }

        fn annotate_column<A, AR>(&mut self, _annotation: A, _column: Column<Any>)
        where
            A: FnOnce() -> AR,
            AR: Into<String>,
        {
            // Do nothing
        }

        fn query_instance(
            &self,
            _column: Column<Instance>,
            _row: usize,
        ) -> Result<Value<F>, Error> {
            Err(Error::BoundsFailure)
        }

        fn assign_advice<V, VR, A, AR>(
            &mut self,
            _: A,
            column: Column<Advice>,
            row: usize,
            to: V,
        ) -> Result<(), Error>
        where
            V: FnOnce() -> Value<VR>,
            VR: Into<Assigned<F>>,
            A: FnOnce() -> AR,
            AR: Into<String>,
        {
            to().into_field().map(|v| {
                *self
                    .advice
                    .get_mut(column.index())
                    .and_then(|v| v.get_mut(row))
                    .ok_or(Error::BoundsFailure)
                    .unwrap() = v;
            });
            Ok(())
        }

        fn assign_fixed<V, VR, A, AR>(
            &mut self,
            _: A,
            _: Column<Fixed>,
            _: usize,
            _: V,
        ) -> Result<(), Error>
        where
            V: FnOnce() -> Value<VR>,
            VR: Into<Assigned<F>>,
            A: FnOnce() -> AR,
            AR: Into<String>,
        {
            // We only care about advice columns here

            Ok(())
        }

        fn copy(
            &mut self,
            _: Column<Any>,
            _: usize,
            _: Column<Any>,
            _: usize,
        ) -> Result<(), Error> {
            // We only care about advice columns here

            Ok(())
        }

        fn fill_from_row(
            &mut self,
            _: Column<Fixed>,
            _: usize,
            _: Value<Assigned<F>>,
        ) -> Result<(), Error> {
            Ok(())
        }

        fn get_challenge(&self, _challenge: Challenge) -> Value<F> {
            Value::unknown()
        }

        fn push_namespace<NR, N>(&mut self, _: N)
        where
            NR: Into<String>,
            N: FnOnce() -> NR,
        {
            // Do nothing; we don't care about namespaces in this context.
        }

        fn pop_namespace(&mut self, _: Option<String>) {
            // Do nothing; we don't care about namespaces in this context.
        }
    }

    let rwtable_circuit = RwTableCircuit::new(rws, n_rows, None);

    let domain = EvaluationDomain::<<Scheme as CommitmentScheme>::Scalar>::new(
        degree as u32,
        params_prover.k(),
    );

    let mut cs = ConstraintSystem::<<Scheme as CommitmentScheme>::Scalar>::default();
    let rwtable_circuit_config = RwTableCircuit::configure(&mut cs);
    let mut witness = WitnessCollection {
        advice: vec![
            domain.empty_lagrange_assigned();
            <RwTable as LookupTable<<Scheme as CommitmentScheme>::Scalar>>::advice_columns(
                &rwtable_circuit_config.rw_table
            )
            .len()
        ],
        _marker: std::marker::PhantomData,
    };

    // Synthesize the circuit to obtain the witness and other information.
    <RwTableCircuit<'_> as Circuit<Fr>>::FloorPlanner::synthesize(
        &mut witness,
        &rwtable_circuit,
        rwtable_circuit_config,
        cs.constants().clone(),
    )
    .unwrap();

    let len = witness.advice.len();
    let advice_values =
        batch_invert_assigned::<Scheme::Scalar>(domain, witness.advice.into_iter().collect());

    // Compute commitments to advice column polynomials
    let blinds = vec![Blind::default(); len];
    let advice_commitments_projective: Vec<_> = advice_values
        .iter()
        .zip(blinds.iter())
        .map(|(poly, blind)| params_prover.commit_lagrange(poly, *blind))
        .collect();
    let mut advice_commitments =
        vec![Scheme::Curve::identity(); advice_commitments_projective.len()];

    <Scheme::Curve as CurveAffine>::CurveExt::batch_normalize(
        &advice_commitments_projective,
        &mut advice_commitments,
    );

    advice_commitments
}

struct RwTableCircuit<'a> {
    rws: &'a [Rw],
    n_rows: usize,
    prev_chunk_last_rw: Option<Rw>,
}

impl<'a> RwTableCircuit<'a> {
    #[allow(dead_code)]
    pub(crate) fn new(rws: &'a [Rw], n_rows: usize, prev_chunk_last_rw: Option<Rw>) -> Self {
        Self {
            rws,
            n_rows,
            prev_chunk_last_rw,
        }
    }
}

#[derive(Clone)]
pub(crate) struct RwTableCircuitConfig {
    pub rw_table: RwTable,
}

impl RwTableCircuitConfig {}

impl<'a, F: Field> Circuit<F> for RwTableCircuit<'a> {
    type Config = RwTableCircuitConfig;

    type FloorPlanner = SimpleFloorPlanner;

    type Params = ();

    fn without_witnesses(&self) -> Self {
        todo!()
    }

    fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
        RwTableCircuitConfig {
            rw_table: RwTable::construct(meta),
        }
    }

    fn synthesize(
        &self,
        config: Self::Config,
        mut layouter: impl Layouter<F>,
    ) -> Result<(), Error> {
        layouter.assign_region(
            || "XXXX",
            |mut region| {
                config.rw_table.load_with_region(
                    &mut region,
                    self.rws,
                    self.n_rows,
                    self.prev_chunk_last_rw,
                )
            },
        )?;
        Ok(())
    }
}

// migrate from halo2 library
#[allow(unused)]
fn batch_invert_assigned<F: Field + WithSmallOrderMulGroup<3>>(
    domain: EvaluationDomain<F>,
    assigned: Vec<Polynomial<Assigned<F>, LagrangeCoeff>>,
) -> Vec<Polynomial<F, LagrangeCoeff>> {
    let mut assigned_denominators: Vec<_> = assigned
        .iter()
        .map(|f| {
            f.iter()
                .map(|value| value.denominator())
                .collect::<Vec<_>>()
        })
        .collect();

    assigned_denominators
        .iter_mut()
        .flat_map(|f| {
            f.iter_mut()
                // If the denominator is trivial, we can skip it, reducing the
                // size of the batch inversion.
                .filter_map(|d| d.as_mut())
        })
        .batch_invert();

    assigned
        .iter()
        .zip(assigned_denominators)
        .map(|(poly, inv_denoms)| {
            let inv_denoms = inv_denoms.into_iter().map(|d| d.unwrap_or(F::ONE));
            domain.lagrange_from_vec(
                poly.iter()
                    .zip(inv_denoms)
                    .map(|(a, inv_den)| a.numerator() * inv_den)
                    .collect(),
            )
        })
        .collect()
}