/
chacha.rs
620 lines (569 loc) · 20.3 KB
/
chacha.rs
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! This module implements the `ChaCha` constraints.
//~ There are four chacha constraint types, corresponding to the four lines in each quarter round.
//~
//~ ```
//~ a += b; d ^= a; d <<<= 16;
//~ c += d; b ^= c; b <<<= 12;
//~ a += b; d ^= a; d <<<= 8;
//~ c += d; b ^= c; b <<<= 7;
//~ ```
//~
//~ or, written without mutation, (and where `+` is mod $2^32$),
//~
//~ ```
//~ a' = a + b ; d' = (d ⊕ a') <<< 16;
//~ c' = c + d'; b' = (b ⊕ c') <<< 12;
//~ a'' = a' + b'; d'' = (d' ⊕ a') <<< 8;
//~ c'' = c' + d''; b'' = (c'' ⊕ b') <<< 7;
//~ ```
//~
//~ We lay each line as two rows.
//~
//~ Each line has the form
//~
//~ ```
//~ x += z; y ^= x; y <<<= k
//~ ```
//~
//~ or without mutation,
//~
//~ ```
//~ x' = x + z; y' = (y ⊕ x') <<< k
//~ ```
//~
//~ which we abbreviate as
//~
//~ L(x, x', y, y', z, k)
//~
//~ In general, such a line will be laid out as the two rows
//~
//~
//~ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
//~ |---|---|---|---|---|---|---|---|---|---|----|----|----|----|----|
//~ | x | y | z | (y^x')_0 | (y^x')_1 | (y^x')_2 | (y^x')_3 | (x+z)_0 | (x+z)_1 | (x+z)_2 | (x+z)_3 | y_0 | y_1 | y_2 | y_3 |
//~ | x' | y' | (x+z)_8 | (y^x')_4 | (y^x')_5 | (y^x')_6 | (y^x')_7 | (x+z)_4 | (x+z)_5 | (x+z)_6 | (x+z)_7 | y_4 | y_5 | y_6 | y_7 |
//~
//~ where A_i indicates the i^th nybble (four-bit chunk) of the value A.
//~
//~ $(x+z)_8$ is special, since we know it is actually at most 1 bit (representing the overflow bit of x + z).
//~
//~ So the first line `L(a, a', d, d', b, 8)` for example becomes the two rows
//~
//~ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
//~ |---|---|---|---|---|---|---|---|---|---|----|----|----|----|----|
//~ | a | d | b | (d^a')_0 | (d^a')_1 | (d^a')_2 | (d^a')_3 | (a+b)_0 | (a+b)_1 | (a+b)_2 | (a+b)_3 | d_0 | d_1 | d_2 | d_3 |
//~ | a' | d' | (a+b)_8 | (d^a')_4 | (d^a')_5 | (d^a')_6 | (d^a')_7 | (a+b)_4 | (a+b)_5 | (a+b)_6 | (a+b)_7 | d_4 | d_5 | d_6 | d_7 |
//~
//~ along with the equations
//~
//~ * $(a+b)_8^2 = (a+b)_8$ (booleanity check)
//~ * $a' = \sum_{i = 0}^7 (2^4)^i (a+b)_i$
//~ * $a + b = 2^{32} (a+b)_8 + a'$
//~ * $d = \sum_{i = 0}^7 (2^4)^i d_i$
//~ * $d' = \sum_{i = 0}^7 (2^4)^{(i + 4) \mod 8} (a+b)_i$
//~
//~ The $(i + 4) \mod 8$ rotates the nybbles left by 4, which means bit-rotating by $4 \times 4 = 16$ as desired.
//~
//~ The final line is a bit more complicated as we have to rotate by 7, which is not a multiple of 4.
//~ We accomplish this as follows.
//~
//~ Let's say we want to rotate the nybbles $A_0, \cdots, A_7$ left by 7.
//~ First we'll rotate left by 4 to get
//~
//~ $$A_7, A_0, A_1, \cdots, A_6$$
//~
//~ Rename these as
//~ $$B_0, \cdots, B_7$$
//~
//~ We now want to left-rotate each $B_i$ by 3.
//~
//~ Let $b_i$ be the low bit of $B_i$.
//~ Then, the low 3 bits of $B_i$ are
//~ $(B_i - b_i) / 2$.
//~
//~ The result will thus be
//~
//~ * $2^3 b_0 + (B_7 - b_7)/2$
//~ * $2^3 b_1 + (B_0 - b_0)/2$
//~ * $2^3 b_2 + (B_1 - b_1)/2$
//~ * $\cdots$
//~ * $2^3 b_7 + (B_6 - b_6)/2$
//~
//~ or re-writing in terms of our original nybbles $A_i$,
//~
//~ * $2^3 a_7 + (A_6 - a_6)/2$
//~ * $2^3 a_0 + (A_7 - a_7)/2$
//~ * $2^3 a_1 + (A_0 - a_0)/2$
//~ * $2^3 a_2 + (A_1 - a_1)/2$
//~ * $2^3 a_3 + (A_2 - a_2)/2$
//~ * $2^3 a_4 + (A_3 - a_3)/2$
//~ * $2^3 a_5 + (A_4 - a_4)/2$
//~ * $2^3 a_6 + (A_5 - a_5)/2$
//~
//~ For neatness, letting $(x, y, z) = (c', b', d'')$, the first 2 rows for the final line will be:
//~
//~ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
//~ |---|---|---|---|---|---|---|---|---|---|----|----|----|----|----|
//~ | x | y | z | (y^x')_0 | (y^x')_1 | (y^x')_2 | (y^x')_3 | (x+z)_0 | (x+z)_1 | (x+z)_2 | (x+z)_3 | y_0 | y_1 | y_2 | y_3 |
//~ | x' | _ | (x+z)_8 | (y^x')_4 | (y^x')_5 | (y^x')_6 | (y^x')_7 | (x+z)_4 | (x+z)_5 | (x+z)_6 | (x+z)_7 | y_4 | y_5 | y_6 | y_7 |
//~
//~ but then we also need to perform the bit-rotate by 1.
//~
//~ For this we'll add an additional 2 rows. It's probably possible to do it with just 1,
//~ but I think we'd have to change our plookup setup somehow, or maybe expand the number of columns,
//~ or allow access to the previous row.
//~
//~ Let $lo(n)$ be the low bit of the nybble n. The 2 rows will be
//~
//~ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
//~ |---|---|---|---|---|---|---|---|---|---|----|----|----|----|----|
//~ | y' | (y^x')_0 | (y^x')_1 | (y^x')_2 | (y^x')_3 | lo((y^x')_0) | lo((y^x')_1) | lo((y^x')_2) | lo((y^x')_3) |
//~ | _ | (y^x')_4 | (y^x')_5 | (y^x')_6 | (y^x')_7 | lo((y^x')_4) | lo((y^x')_5) | lo((y^x')_6) | lo((y^x')_7) |
//~
//~ On each of them we'll do the plookups
//~
//~ ```
//~ ((cols[1] - cols[5])/2, (cols[1] - cols[5])/2, 0) in XOR
//~ ((cols[2] - cols[6])/2, (cols[2] - cols[6])/2, 0) in XOR
//~ ((cols[3] - cols[7])/2, (cols[3] - cols[7])/2, 0) in XOR
//~ ((cols[4] - cols[8])/2, (cols[4] - cols[8])/2, 0) in XOR
//~ ```
//~
//~ which checks that $(y^{x'})_i - lo((y^{x'})_i)$ is a nybble,
//~ which guarantees that the low bit is computed correctly.
//~
//~ There is no need to check nybbleness of $(y^x')_i$ because those will be constrained to
//~ be equal to the copies of those values from previous rows, which have already been
//~ constrained for nybbleness (by the lookup in the XOR table).
//~
//~ And we'll check that y' is the sum of the shifted nybbles.
//~
use std::marker::PhantomData;
use crate::circuits::{
argument::{Argument, ArgumentEnv, ArgumentType},
expr::constraints::{boolean, ExprOps},
gate::{CurrOrNext, GateType},
};
use ark_ff::{FftField, Field, PrimeField};
//
// Implementation internals
//
/// 8-nybble sequences that are laid out as 4 nybbles per row over the two row,
/// like y^x' or x+z
fn chunks_over_2_rows<F: PrimeField, T: ExprOps<F>>(
env: &ArgumentEnv<F, T>,
col_offset: usize,
) -> Vec<T> {
(0..8)
.map(|i| {
use CurrOrNext::{Curr, Next};
let r = if i < 4 { Curr } else { Next };
env.witness(r, col_offset + (i % 4))
})
.collect()
}
fn combine_nybbles<F: Field, T: ExprOps<F>>(ns: Vec<T>) -> T {
ns.into_iter()
.enumerate()
.fold(T::zero(), |acc: T, (i, t)| acc + T::from(1 << (4 * i)) * t)
}
/// Constraints for the line L(x, x', y, y', z, k), where k = 4 * `nybble_rotation`
fn line<F: PrimeField, T: ExprOps<F>>(env: &ArgumentEnv<F, T>, nybble_rotation: usize) -> Vec<T> {
let y_xor_xprime_nybbles = chunks_over_2_rows(env, 3);
let x_plus_z_nybbles = chunks_over_2_rows(env, 7);
let y_nybbles = chunks_over_2_rows(env, 11);
let x_plus_z_overflow_bit = env.witness_next(2);
let x = env.witness_curr(0);
let xprime = env.witness_next(0);
let y = env.witness_curr(1);
let yprime = env.witness_next(1);
let z = env.witness_curr(2);
// Because the nybbles are little-endian, rotating the vector "right"
// is equivalent to left-shifting the nybbles.
let mut y_xor_xprime_rotated = y_xor_xprime_nybbles;
y_xor_xprime_rotated.rotate_right(nybble_rotation);
vec![
// booleanity of overflow bit
boolean(&x_plus_z_overflow_bit),
// x' = x + z (mod 2^32)
combine_nybbles(x_plus_z_nybbles) - xprime.clone(),
// Correctness of x+z nybbles
xprime + T::from(1 << 32) * x_plus_z_overflow_bit - (x + z),
// Correctness of y nybbles
combine_nybbles(y_nybbles) - y,
// y' = (y ^ x') <<< 4 * nybble_rotation
combine_nybbles(y_xor_xprime_rotated) - yprime,
]
}
//
// Gates
//
/// Implementation of the `ChaCha0` gate
#[derive(Default)]
pub struct ChaCha0<F>(PhantomData<F>);
impl<F> Argument<F> for ChaCha0<F>
where
F: PrimeField,
{
const ARGUMENT_TYPE: ArgumentType = ArgumentType::Gate(GateType::ChaCha0);
const CONSTRAINTS: u32 = 5;
fn constraint_checks<T: ExprOps<F>>(env: &ArgumentEnv<F, T>) -> Vec<T> {
// a += b; d ^= a; d <<<= 16 (=4*4)
line(env, 4)
}
}
/// Implementation of the `ChaCha1` gate
#[derive(Default)]
pub struct ChaCha1<F>(PhantomData<F>);
impl<F> Argument<F> for ChaCha1<F>
where
F: PrimeField,
{
const ARGUMENT_TYPE: ArgumentType = ArgumentType::Gate(GateType::ChaCha1);
const CONSTRAINTS: u32 = 5;
fn constraint_checks<T: ExprOps<F>>(env: &ArgumentEnv<F, T>) -> Vec<T> {
// c += d; b ^= c; b <<<= 12 (=3*4)
line(env, 3)
}
}
/// Implementation of the `ChaCha2` gate
#[derive(Default)]
pub struct ChaCha2<F>(PhantomData<F>);
impl<F> Argument<F> for ChaCha2<F>
where
F: PrimeField,
{
const ARGUMENT_TYPE: ArgumentType = ArgumentType::Gate(GateType::ChaCha2);
const CONSTRAINTS: u32 = 5;
fn constraint_checks<T: ExprOps<F>>(env: &ArgumentEnv<F, T>) -> Vec<T> {
// a += b; d ^= a; d <<<= 8 (=2*4)
line(env, 2)
}
}
/// Implementation of the `ChaChaFinal` gate
#[derive(Default)]
pub struct ChaChaFinal<F>(PhantomData<F>);
impl<F> Argument<F> for ChaChaFinal<F>
where
F: PrimeField,
{
const ARGUMENT_TYPE: ArgumentType = ArgumentType::Gate(GateType::ChaChaFinal);
const CONSTRAINTS: u32 = 9;
fn constraint_checks<T: ExprOps<F>>(env: &ArgumentEnv<F, T>) -> Vec<T> {
// The last line, namely,
// c += d; b ^= c; b <<<= 7;
// is special.
// We don't use the y' value computed by this one, so we
// will use a ChaCha0 gate to compute the nybbles of
// all the relevant values, and the xors, and then do
// the shifting using a ChaChaFinal gate.
let y_xor_xprime_nybbles = chunks_over_2_rows(env, 1);
let low_bits = chunks_over_2_rows(env, 5);
let yprime = env.witness_curr(0);
let one_half = F::from(2u64).inverse().unwrap();
// (y xor xprime) <<< 7
// per the comment at the top of the file
let y_xor_xprime_rotated: Vec<_> = [7, 0, 1, 2, 3, 4, 5, 6]
.iter()
.zip([6, 7, 0, 1, 2, 3, 4, 5].iter())
.map(|(&i, &j)| -> T {
T::from(8) * low_bits[i].clone()
+ T::literal(one_half) * (y_xor_xprime_nybbles[j].clone() - low_bits[j].clone())
})
.collect();
let mut constraints: Vec<T> = low_bits.iter().map(boolean).collect();
constraints.push(combine_nybbles(y_xor_xprime_rotated) - yprime);
constraints
}
}
// TODO: move this to test file
pub mod testing {
use super::{FftField, GateType};
/// This is just for tests. It doesn't set up the permutations
pub fn chacha20_gates() -> Vec<GateType> {
let mut gs = vec![];
for _ in 0..20 {
use GateType::*;
for _ in 0..4 {
for &g in &[ChaCha0, ChaCha1, ChaCha2, ChaCha0, ChaChaFinal] {
gs.push(g);
gs.push(Zero);
}
}
}
gs
}
const CHACHA20_ROTATIONS: [u32; 4] = [16, 12, 8, 7];
const CHACHA20_QRS: [[usize; 4]; 8] = [
[0, 4, 8, 12],
[1, 5, 9, 13],
[2, 6, 10, 14],
[3, 7, 11, 15],
[0, 5, 10, 15],
[1, 6, 11, 12],
[2, 7, 8, 13],
[3, 4, 9, 14],
];
pub fn chacha20_rows<F: FftField>(s0: Vec<u32>) -> Vec<Vec<F>> {
let mut rows = vec![];
let mut s = s0;
let mut line = |x: usize, y: usize, z: usize, k: u32| {
let f = |t: u32| F::from(t);
let nyb = |t: u32, i: usize| f((t >> (4 * i)) & 0b1111);
let top_bit = ((u64::from(s[x]) + (u64::from(s[z]))) >> 32) as u32;
let xprime = u32::wrapping_add(s[x], s[z]);
let y_xor_xprime = s[y] ^ xprime;
let yprime = y_xor_xprime.rotate_left(k);
let yprime_in_row =
// When k = 7, we use a ChaCha0 gate and throw away the yprime value
// (which will need to be y_xor_xprime.rotate_left(16))
// in the second row corresponding to that gate
if k == 7 { y_xor_xprime.rotate_left(16) } else { yprime };
rows.push(vec![
f(s[x]),
f(s[y]),
f(s[z]),
nyb(y_xor_xprime, 0),
nyb(y_xor_xprime, 1),
nyb(y_xor_xprime, 2),
nyb(y_xor_xprime, 3),
nyb(xprime, 0),
nyb(xprime, 1),
nyb(xprime, 2),
nyb(xprime, 3),
nyb(s[y], 0),
nyb(s[y], 1),
nyb(s[y], 2),
nyb(s[y], 3),
]);
rows.push(vec![
f(xprime),
f(yprime_in_row),
f(top_bit),
nyb(y_xor_xprime, 4),
nyb(y_xor_xprime, 5),
nyb(y_xor_xprime, 6),
nyb(y_xor_xprime, 7),
nyb(xprime, 4),
nyb(xprime, 5),
nyb(xprime, 6),
nyb(xprime, 7),
nyb(s[y], 4),
nyb(s[y], 5),
nyb(s[y], 6),
nyb(s[y], 7),
]);
s[x] = xprime;
s[y] = yprime;
if k == 7 {
let lo = |t: u32, i: usize| f((t >> (4 * i)) & 1);
rows.push(vec![
f(yprime),
nyb(y_xor_xprime, 0),
nyb(y_xor_xprime, 1),
nyb(y_xor_xprime, 2),
nyb(y_xor_xprime, 3),
lo(y_xor_xprime, 0),
lo(y_xor_xprime, 1),
lo(y_xor_xprime, 2),
lo(y_xor_xprime, 3),
F::zero(),
F::zero(),
F::zero(),
F::zero(),
F::zero(),
F::zero(),
]);
rows.push(vec![
F::zero(),
nyb(y_xor_xprime, 4),
nyb(y_xor_xprime, 5),
nyb(y_xor_xprime, 6),
nyb(y_xor_xprime, 7),
lo(y_xor_xprime, 4),
lo(y_xor_xprime, 5),
lo(y_xor_xprime, 6),
lo(y_xor_xprime, 7),
F::zero(),
F::zero(),
F::zero(),
F::zero(),
F::zero(),
F::zero(),
]);
}
};
let mut qr = |a, b, c, d| {
line(a, d, b, CHACHA20_ROTATIONS[0]);
line(c, b, d, CHACHA20_ROTATIONS[1]);
line(a, d, b, CHACHA20_ROTATIONS[2]);
line(c, b, d, CHACHA20_ROTATIONS[3]);
};
for _ in 0..10 {
for [a, b, c, d] in CHACHA20_QRS {
qr(a, b, c, d);
}
}
rows
}
pub fn chacha20(mut s: Vec<u32>) -> Vec<u32> {
let mut line = |x, y, z, k| {
s[x] = u32::wrapping_add(s[x], s[z]);
s[y] ^= s[x];
let yy: u32 = s[y];
s[y] = yy.rotate_left(k);
};
let mut qr = |a, b, c, d| {
line(a, d, b, CHACHA20_ROTATIONS[0]);
line(c, b, d, CHACHA20_ROTATIONS[1]);
line(a, d, b, CHACHA20_ROTATIONS[2]);
line(c, b, d, CHACHA20_ROTATIONS[3]);
};
for _ in 0..10 {
for [a, b, c, d] in CHACHA20_QRS {
qr(a, b, c, d);
}
}
s
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
alphas::Alphas,
circuits::{
expr::{Column, Constants, PolishToken},
lookup::lookups::{LookupFeatures, LookupInfo, LookupPatterns},
wires::*,
},
curve::KimchiCurve,
proof::{LookupEvaluations, PointEvaluations, ProofEvaluations},
};
use ark_ff::{UniformRand, Zero};
use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as D};
use mina_curves::pasta::{Fp as F, Vesta};
use rand::{rngs::StdRng, SeedableRng};
use std::array;
use std::fmt::{Display, Formatter};
struct Polish(Vec<PolishToken<F>>);
impl Display for Polish {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
for x in self.0.iter() {
match x {
PolishToken::Literal(a) => write!(f, "{a}, ")?,
PolishToken::Add => write!(f, "+, ")?,
PolishToken::Mul => write!(f, "*, ")?,
PolishToken::Sub => write!(f, "-, ")?,
x => write!(f, "{x:?}, ")?,
}
}
write!(f, "]")?;
Ok(())
}
}
#[test]
fn chacha_linearization() {
let lookup_info = LookupInfo::create(LookupFeatures {
patterns: LookupPatterns {
xor: true,
chacha_final: true,
lookup: false,
range_check: false,
foreign_field_mul: false,
},
uses_runtime_tables: false,
joint_lookup_used: true,
});
let evaluated_cols = {
let mut h = std::collections::HashSet::new();
// use Column::*;
for i in 0..COLUMNS {
h.insert(Column::Witness(i));
}
for i in 0..(lookup_info.max_per_row + 1) {
h.insert(Column::LookupSorted(i));
}
h.insert(Column::Z);
h.insert(Column::LookupAggreg);
h.insert(Column::LookupTable);
h.insert(Column::Index(GateType::Poseidon));
h.insert(Column::Index(GateType::Generic));
h
};
let mut alphas = Alphas::<F>::default();
alphas.register(
ArgumentType::Gate(GateType::ChaChaFinal),
ChaChaFinal::<F>::CONSTRAINTS,
);
let mut expr = ChaCha0::combined_constraints(&alphas);
expr += ChaCha1::combined_constraints(&alphas);
expr += ChaCha2::combined_constraints(&alphas);
expr += ChaChaFinal::combined_constraints(&alphas);
let linearized = expr.linearize(evaluated_cols).unwrap();
let _expr_polish = expr.to_polish();
let linearized_polish = linearized.map(|e| e.to_polish());
let rng = &mut StdRng::from_seed([0u8; 32]);
let d = D::new(1024).unwrap();
let pt = F::rand(rng);
let mut rand_eval = || PointEvaluations {
zeta: F::rand(rng),
zeta_omega: F::rand(rng),
};
let eval = ProofEvaluations {
w: array::from_fn(|_| rand_eval()),
z: rand_eval(),
s: array::from_fn(|_| rand_eval()),
coefficients: array::from_fn(|_| rand_eval()),
generic_selector: PointEvaluations::default(),
poseidon_selector: PointEvaluations::default(),
lookup: Some(LookupEvaluations {
sorted: (0..(lookup_info.max_per_row + 1))
.map(|_| rand_eval())
.collect(),
aggreg: rand_eval(),
table: rand_eval(),
runtime: None,
}),
};
let constants = Constants {
alpha: F::rand(rng),
beta: F::rand(rng),
gamma: F::rand(rng),
joint_combiner: None,
endo_coefficient: F::zero(),
mds: &Vesta::sponge_params().mds,
};
assert_eq!(
linearized
.constant_term
.evaluate_(d, pt, &eval, &constants)
.unwrap(),
PolishToken::evaluate(&linearized_polish.constant_term, d, pt, &eval, &constants)
.unwrap()
);
linearized
.index_terms
.iter()
.zip(linearized_polish.index_terms.iter())
.for_each(|((c1, e1), (c2, e2))| {
assert_eq!(c1, c2);
println!("{c1:?} ?");
let x1 = e1.evaluate_(d, pt, &eval, &constants).unwrap();
let x2 = PolishToken::evaluate(e2, d, pt, &eval, &constants).unwrap();
if x1 != x2 {
println!("e1: {}", e1.ocaml_str());
println!("e2: {}", Polish(e2.clone()));
println!("Polish evaluation differed for {c1:?}: {x1} != {x2}");
} else {
println!("{c1:?} OK");
}
});
/*
assert_eq!(
expr.evaluate_(d, pt, &eval, &constants).unwrap(),
PolishToken::evaluate(&expr_polish, d, pt, &eval, &constants).unwrap());
*/
}
}