evm_arithmetization 0.5.1

Implementation of STARKs for the Ethereum Virtual Machine
Documentation
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
use core::borrow::Borrow;
use core::marker::PhantomData;

use ethereum_types::U256;
use itertools::izip;
use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::field::packed::PackedField;
use plonky2::field::polynomial::PolynomialValues;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::timed;
use plonky2::util::timing::TimingTree;
use serde::{Deserialize, Serialize};
use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use starky::evaluation_frame::StarkEvaluationFrame;
use starky::lookup::{Column, Filter};
use starky::stark::Stark;
use starky::util::trace_rows_to_poly_values;

use crate::all_stark::EvmStarkFrame;
use crate::logic::columns::{LogicColumnsView, LOGIC_COL_MAP, NUM_COLUMNS};
use crate::util::{limb_from_bits_le, limb_from_bits_le_recursive};

/// Total number of bits per input/output.
const VAL_BITS: usize = 256;
/// Number of bits stored per field element. Ensure that this fits; it is not
/// checked.
pub(crate) const PACKED_LIMB_BITS: usize = 32;
/// Number of field elements needed to store each input/output at the specified
/// packing.
const PACKED_LEN: usize = VAL_BITS.div_ceil(PACKED_LIMB_BITS);

/// `LogicStark` columns.
pub(crate) mod columns {
    use core::mem::transmute;

    use zk_evm_proc_macro::{Columns, DerefColumns};

    use super::{PACKED_LEN, VAL_BITS};
    use crate::util::indices_arr;

    /// Flag columns for the operation to perform.
    #[repr(C)]
    #[derive(DerefColumns, Clone, Copy, Debug, Eq, PartialEq)]
    pub(crate) struct OpsColumnsView<T> {
        /// 1 if this is an AND operation, 0 otherwise.
        pub is_and: T,
        /// 1 if this is an OR operation, 0 otherwise.
        pub is_or: T,
        /// 1 if this is a XOR operation, 0 otherwise.
        pub is_xor: T,
    }

    /// Columns for the `LogicStark`.
    #[repr(C)]
    #[derive(Columns, Clone, Copy, Debug, Eq, PartialEq)]
    pub(crate) struct LogicColumnsView<T> {
        /// The operation to perform.
        pub op: OpsColumnsView<T>,
        /// First input, decomposed into bits.
        pub input0: [T; VAL_BITS],
        /// Second input, decomposed into bits.
        pub input1: [T; VAL_BITS],
        /// The result is packed in limbs of `PACKED_LIMB_BITS` bits.
        pub result: [T; PACKED_LEN],
    }

    /// Total number of columns in `LogicStark`.
    /// `u8` is guaranteed to have a `size_of` of 1.
    pub(crate) const NUM_COLUMNS: usize = core::mem::size_of::<LogicColumnsView<u8>>();

    /// Mapping between [0..NUM_COLUMNS-1] and the logic columns.
    pub(crate) const LOGIC_COL_MAP: LogicColumnsView<usize> = make_col_map();

    const fn make_col_map() -> LogicColumnsView<usize> {
        let indices_arr = indices_arr::<NUM_COLUMNS>();
        unsafe { transmute::<[usize; NUM_COLUMNS], LogicColumnsView<usize>>(indices_arr) }
    }
}

/// Creates the vector of `Columns` corresponding to the opcode, the two inputs
/// and the output of the logic operation.
pub(crate) fn ctl_data<F: Field>() -> Vec<Column<F>> {
    // We scale each filter flag with the associated opcode value.
    // If a logic operation is happening on the CPU side, the CTL
    // will enforce that the reconstructed opcode value from the
    // opcode bits matches.
    let mut res = vec![Column::linear_combination([
        (LOGIC_COL_MAP.op.is_and, F::from_canonical_u8(0x16)),
        (LOGIC_COL_MAP.op.is_or, F::from_canonical_u8(0x17)),
        (LOGIC_COL_MAP.op.is_xor, F::from_canonical_u8(0x18)),
    ])];
    res.extend(
        LOGIC_COL_MAP
            .input0
            .chunks(PACKED_LIMB_BITS)
            .map(Column::le_bits),
    );
    res.extend(
        LOGIC_COL_MAP
            .input1
            .chunks(PACKED_LIMB_BITS)
            .map(Column::le_bits),
    );
    res.extend(LOGIC_COL_MAP.result.map(Column::single));
    res
}

/// CTL filter for logic operations.
pub(crate) fn ctl_filter<F: Field>() -> Filter<F> {
    Filter::new_simple(Column::sum(*LOGIC_COL_MAP.op))
}

/// Structure representing the Logic STARK, which computes all logic operations.
#[derive(Copy, Clone, Default)]
pub(crate) struct LogicStark<F, const D: usize> {
    pub f: PhantomData<F>,
}

/// Logic operations.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub(crate) enum Op {
    And,
    Or,
    Xor,
}

impl Op {
    /// Returns the output of the current Logic operation.
    pub(crate) fn result(&self, a: U256, b: U256) -> U256 {
        match self {
            Op::And => a & b,
            Op::Or => a | b,
            Op::Xor => a ^ b,
        }
    }
}

/// A logic operation over `U256`` words. It contains an operator,
/// either `AND`, `OR` or `XOR`, two inputs and its expected result.
#[derive(Debug)]
pub(crate) struct Operation {
    operator: Op,
    input0: U256,
    input1: U256,
    pub(crate) result: U256,
}

impl Operation {
    /// Computes the expected result of an operator with the two provided
    /// inputs, and returns the associated logic `Operation`.
    pub(crate) fn new(operator: Op, input0: U256, input1: U256) -> Self {
        let result = operator.result(input0, input1);
        Operation {
            operator,
            input0,
            input1,
            result,
        }
    }

    /// Given an `Operation`, fills a row with the corresponding flag, inputs
    /// and output.
    fn into_row<F: Field>(self) -> [F; NUM_COLUMNS] {
        let Operation {
            operator,
            input0,
            input1,
            result,
        } = self;
        let mut row = [F::ZERO; NUM_COLUMNS];
        row[match operator {
            Op::And => LOGIC_COL_MAP.op.is_and,
            Op::Or => LOGIC_COL_MAP.op.is_or,
            Op::Xor => LOGIC_COL_MAP.op.is_xor,
        }] = F::ONE;
        for i in 0..256 {
            row[LOGIC_COL_MAP.input0[i]] = F::from_bool(input0.bit(i));
            row[LOGIC_COL_MAP.input1[i]] = F::from_bool(input1.bit(i));
        }
        let result_limbs: &[u64] = result.as_ref();
        for (i, &limb) in result_limbs.iter().enumerate() {
            row[LOGIC_COL_MAP.result[2 * i]] = F::from_canonical_u32(limb as u32);
            row[LOGIC_COL_MAP.result[2 * i + 1]] = F::from_canonical_u32((limb >> 32) as u32);
        }
        row
    }
}

impl<F: RichField, const D: usize> LogicStark<F, D> {
    /// Generates the trace polynomials for `LogicStark`.
    pub(crate) fn generate_trace(
        &self,
        operations: Vec<Operation>,
        min_rows: usize,
        timing: &mut TimingTree,
    ) -> Vec<PolynomialValues<F>> {
        // First, turn all provided operations into rows in `LogicStark`, and pad if
        // necessary.
        let trace_rows = timed!(
            timing,
            "generate trace rows",
            self.generate_trace_rows(operations, min_rows)
        );
        // Generate the trace polynomials from the trace values.
        let trace_polys = timed!(
            timing,
            "convert to PolynomialValues",
            trace_rows_to_poly_values(trace_rows)
        );
        trace_polys
    }

    /// Generate the `LogicStark` traces based on the provided vector of
    /// operations. The trace is padded to a power of two with all-zero
    /// rows.
    fn generate_trace_rows(
        &self,
        operations: Vec<Operation>,
        min_rows: usize,
    ) -> Vec<[F; NUM_COLUMNS]> {
        let len = operations.len();
        let padded_len = len.max(min_rows).next_power_of_two();

        let mut rows = Vec::with_capacity(padded_len);
        for op in operations {
            rows.push(op.into_row());
        }

        // Pad to a power of two.
        for _ in len..padded_len {
            rows.push([F::ZERO; NUM_COLUMNS]);
        }

        rows
    }
}

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for LogicStark<F, D> {
    type EvaluationFrame<FE, P, const D2: usize>
        = EvmStarkFrame<P, FE, NUM_COLUMNS>
    where
        FE: FieldExtension<D2, BaseField = F>,
        P: PackedField<Scalar = FE>;

    type EvaluationFrameTarget = EvmStarkFrame<ExtensionTarget<D>, ExtensionTarget<D>, NUM_COLUMNS>;

    fn eval_packed_generic<FE, P, const D2: usize>(
        &self,
        vars: &Self::EvaluationFrame<FE, P, D2>,
        yield_constr: &mut ConstraintConsumer<P>,
    ) where
        FE: FieldExtension<D2, BaseField = F>,
        P: PackedField<Scalar = FE>,
    {
        let lv: &[P; NUM_COLUMNS] = vars.get_local_values().try_into().unwrap();
        let lv: &LogicColumnsView<P> = lv.borrow();

        let is_and = lv.op.is_and;
        let is_or = lv.op.is_or;
        let is_xor = lv.op.is_xor;

        // Flags must be boolean.
        for &flag in &[is_and, is_or, is_xor] {
            yield_constr.constraint(flag * (flag - P::ONES));
        }

        // Only a single flag must be activated at once.
        let all_flags = is_and + is_or + is_xor;
        yield_constr.constraint(all_flags * (all_flags - P::ONES));

        // The result will be `in0 OP in1 = sum_coeff * (in0 + in1) + and_coeff * (in0
        // AND in1)`. `AND => sum_coeff = 0, and_coeff = 1`
        // `OR  => sum_coeff = 1, and_coeff = -1`
        // `XOR => sum_coeff = 1, and_coeff = -2`
        let sum_coeff = is_or + is_xor;
        let and_coeff = is_and - is_or - is_xor * FE::TWO;

        // Ensure that all bits are indeed bits.
        for input_bits in [lv.input0, lv.input1] {
            for bit in input_bits {
                yield_constr.constraint(bit * (bit - P::ONES));
            }
        }

        // Form the result
        for (result_limb, x_bits, y_bits) in izip!(
            lv.result,
            lv.input0.chunks(PACKED_LIMB_BITS),
            lv.input1.chunks(PACKED_LIMB_BITS),
        ) {
            let x: P = limb_from_bits_le(x_bits.iter().copied());
            let y: P = limb_from_bits_le(y_bits.iter().copied());

            let x_land_y: P = izip!(0.., x_bits, y_bits)
                .map(|(i, &x_bit, &y_bit)| x_bit * y_bit * FE::from_canonical_u64(1 << i))
                .sum();
            let x_op_y = sum_coeff * (x + y) + and_coeff * x_land_y;

            yield_constr.constraint(result_limb - x_op_y);
        }
    }

    fn eval_ext_circuit(
        &self,
        builder: &mut plonky2::plonk::circuit_builder::CircuitBuilder<F, D>,
        vars: &Self::EvaluationFrameTarget,
        yield_constr: &mut RecursiveConstraintConsumer<F, D>,
    ) {
        let lv: &[ExtensionTarget<D>; NUM_COLUMNS] = vars.get_local_values().try_into().unwrap();
        let lv: &LogicColumnsView<ExtensionTarget<D>> = lv.borrow();

        let is_and = lv.op.is_and;
        let is_or = lv.op.is_or;
        let is_xor = lv.op.is_xor;

        // Flags must be boolean.
        for &flag in &[is_and, is_or, is_xor] {
            let constraint = builder.mul_sub_extension(flag, flag, flag);
            yield_constr.constraint(builder, constraint);
        }

        // Only a single flag must be activated at once.
        let all_flags = builder.add_many_extension([is_and, is_or, is_xor]);
        let constraint = builder.mul_sub_extension(all_flags, all_flags, all_flags);
        yield_constr.constraint(builder, constraint);

        // The result will be `in0 OP in1 = sum_coeff * (in0 + in1) + and_coeff * (in0
        // AND in1)`. `AND => sum_coeff = 0, and_coeff = 1`
        // `OR  => sum_coeff = 1, and_coeff = -1`
        // `XOR => sum_coeff = 1, and_coeff = -2`
        let sum_coeff = builder.add_extension(is_or, is_xor);
        let and_coeff = {
            let and_coeff = builder.sub_extension(is_and, is_or);
            builder.mul_const_add_extension(-F::TWO, is_xor, and_coeff)
        };

        // Ensure that all bits are indeed bits.
        for input_bits in [lv.input0, lv.input1] {
            for bit in input_bits {
                let constr = builder.mul_sub_extension(bit, bit, bit);
                yield_constr.constraint(builder, constr);
            }
        }

        // Form the result
        for (result_limb, x_bits, y_bits) in izip!(
            lv.result,
            lv.input0.chunks(PACKED_LIMB_BITS),
            lv.input1.chunks(PACKED_LIMB_BITS),
        ) {
            let x = limb_from_bits_le_recursive(builder, x_bits.iter().copied());
            let y = limb_from_bits_le_recursive(builder, y_bits.iter().copied());

            let x_land_y = izip!(0usize.., x_bits, y_bits).fold(
                builder.zero_extension(),
                |acc, (i, &x_bit, &y_bit)| {
                    builder.arithmetic_extension(
                        F::from_canonical_u64(1 << i),
                        F::ONE,
                        x_bit,
                        y_bit,
                        acc,
                    )
                },
            );
            let x_op_y = {
                let x_op_y = builder.mul_extension(sum_coeff, x);
                let x_op_y = builder.mul_add_extension(sum_coeff, y, x_op_y);
                builder.mul_add_extension(and_coeff, x_land_y, x_op_y)
            };
            let constr = builder.sub_extension(result_limb, x_op_y);
            yield_constr.constraint(builder, constr);
        }
    }

    fn constraint_degree(&self) -> usize {
        3
    }

    fn requires_ctls(&self) -> bool {
        true
    }
}

#[cfg(test)]
mod tests {
    use anyhow::Result;
    use itertools::Itertools;
    use plonky2::field::goldilocks_field::GoldilocksField;
    use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
    use rand::{Rng, SeedableRng};
    use rand_chacha::ChaCha8Rng;
    use starky::stark_testing::{test_stark_circuit_constraints, test_stark_low_degree};

    use super::*;
    use crate::logic::LogicStark;

    #[test]
    fn test_stark_degree() -> Result<()> {
        const D: usize = 2;
        type C = PoseidonGoldilocksConfig;
        type F = <C as GenericConfig<D>>::F;
        type S = LogicStark<F, D>;

        let stark = S {
            f: Default::default(),
        };
        test_stark_low_degree(stark)
    }

    #[test]
    fn test_stark_circuit() -> Result<()> {
        const D: usize = 2;
        type C = PoseidonGoldilocksConfig;
        type F = <C as GenericConfig<D>>::F;
        type S = LogicStark<F, D>;

        let stark = S {
            f: Default::default(),
        };
        test_stark_circuit_constraints::<F, C, S, D>(stark)
    }

    #[test]
    fn test_generate_eval_consistency() {
        const D: usize = 2;
        type C = PoseidonGoldilocksConfig;
        type F = <C as GenericConfig<D>>::F;
        type S = LogicStark<F, D>;

        let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25);
        const N_ITERS: usize = 1000;

        for _ in 0..N_ITERS {
            for op in [Op::And, Op::Or, Op::Xor] {
                // Generate a trace row from an operation on random values.
                let operation = Operation::new(op, U256(rng.gen()), U256(rng.gen()));
                let expected = operation.result;
                let row = operation.into_row::<F>();
                let lv = EvmStarkFrame::from_values(&row, &[F::ZERO; NUM_COLUMNS], &[]);

                let stark = S::default();
                let mut constraint_consumer = ConstraintConsumer::new(
                    vec![GoldilocksField(2), GoldilocksField(3), GoldilocksField(5)],
                    F::ONE,
                    F::ONE,
                    F::ONE,
                );

                // Evaluate constraints.
                stark.eval_packed_generic(&lv, &mut constraint_consumer);
                for acc in constraint_consumer.accumulators() {
                    assert_eq!(acc, F::ZERO);
                }

                // Split each expected U256 limb into two.
                let expected_limbs = expected.as_ref().iter().flat_map(|&limb| {
                    [
                        F::from_canonical_u32(limb as u32),
                        F::from_canonical_u32((limb >> 32) as u32),
                    ]
                });

                // Check that the result limbs match the expected limbs.
                assert!(expected_limbs
                    .zip_eq(&row[LOGIC_COL_MAP.result[0]..])
                    .all(|(x, &y)| x == y));
            }
        }
    }
}