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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
use ark_ff::{BigInteger, PrimeField};
use ark_poly::{EvaluationDomain, MixedRadixEvaluationDomain, Radix2EvaluationDomain};
use ark_std::rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
#[cfg(feature = "parallel")]
use rayon::prelude::{IntoParallelRefMutIterator, ParallelIterator};
use crate::utils::serialization::{ark_deserialize, ark_serialize};
/// Field polynomial.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct FpPolynomial<F: PrimeField> {
/// Coefficients (or evaluations) of the polynomial
#[serde(serialize_with = "ark_serialize", deserialize_with = "ark_deserialize")]
pub coefs: Vec<F>,
}
impl<F: PrimeField> FpPolynomial<F> {
/// Return the polynomial coefs reference.
pub fn get_coefs_ref(&self) -> &[F] {
self.coefs.as_slice()
}
/// Return the little-endian byte representations of the field size
pub fn get_field_size(&self) -> Vec<u8> {
F::BasePrimeField::MODULUS.to_bytes_le()
}
/// Return the constant zero polynomial
/// # Example
/// ```
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
/// let poly = FpPolynomial::<Fr>::zero();
/// let zero = Fr::ZERO;
/// assert_eq!(poly.degree(), 0);
/// assert_eq!(poly.eval(&zero), zero);
/// assert_eq!(poly.eval(&Fr::ONE), zero);
/// ```
pub fn zero() -> Self {
Self::from_coefs(vec![F::ZERO])
}
/// Return the constant one polynomial
/// # Example
/// ```
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
/// let poly = FpPolynomial::<Fr>::one();
/// let one = Fr::ONE;
/// assert_eq!(poly.degree(), 0);
/// assert_eq!(poly.eval(&one), one);
/// assert_eq!(poly.eval(&Fr::ZERO), one);
/// ```
pub fn one() -> Self {
Self::from_coefs(vec![F::ONE])
}
/// Build a polynomial from the coefficient vector, low-order coefficient first.
/// High-order zero coefficient are trimmed.
/// # Example
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let five = two.add(&two).add(&one);
/// let coefs = vec![one, zero, one];
/// let poly = FpPolynomial::from_coefs(coefs);
/// assert_eq!(poly.degree(), 2);
/// assert_eq!(poly.eval(&zero), one);
/// assert_eq!(poly.eval(&one), two);
/// assert_eq!(poly.eval(&two), five);
/// let coefs2 = vec![one, zero, one, zero, zero, zero];
/// let poly2 = FpPolynomial::from_coefs(coefs2);
/// assert_eq!(poly2.degree(), 2);
/// assert_eq!(poly, poly2);
/// ```
pub fn from_coefs(coefs: Vec<F>) -> Self {
let mut p = FpPolynomial { coefs };
p.trim_coefs();
p
}
/// Build a polynomial from its zeroes/roots.
/// # Example
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let five = two.add(&two).add(&one);
/// let zeroes = [one, zero, five, two];
/// let poly = FpPolynomial::from_zeroes(&zeroes[..]);
/// assert_eq!(poly.degree(), 4);
/// assert_eq!(poly.eval(&zero), zero);
/// assert_eq!(poly.eval(&one), zero);
/// assert_eq!(poly.eval(&two), zero);
/// assert_eq!(poly.eval(&five), zero);
/// ```
pub fn from_zeroes(zeroes: &[F]) -> Self {
let roots_ref: Vec<&F> = zeroes.iter().collect();
Self::from_zeroes_ref(&roots_ref[..])
}
/// Build a polynomial from its zeroes/roots given as reference.
pub fn from_zeroes_ref(zeroes: &[&F]) -> Self {
let mut r = Self::one();
for root in zeroes.iter() {
let mut p = r.clone();
r.coefs.insert(0, F::ZERO); // multiply by X
p.mul_scalar_assign(*root); // x_0 * r
r.sub_assign(&p); // r = r * (X - x_0)
}
r.trim_coefs();
r
}
/// Return a polynomial of `degree` + 1 uniformly random coefficients. Note that for each
/// coffiecient with probability 1/q is zero, and hence the degree could be less than `degree`
/// # Example:
/// ```
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::UniformRand;
/// use ark_std::rand::SeedableRng;
/// use ark_std::test_rng;
///
/// use rand_chacha::ChaChaRng;
///
/// let poly = FpPolynomial::<Fr>::random(&mut ChaChaRng::from_seed([0u8; 32]), 10);
/// assert!(poly.degree() <= 10)
/// ```
pub fn random<R: CryptoRng + RngCore>(prng: &mut R, degree: usize) -> FpPolynomial<F> {
let mut coefs = Vec::with_capacity(degree + 1);
for _ in 0..degree + 1 {
coefs.push(F::rand(prng));
}
Self::from_coefs(coefs)
}
/// Remove high degree zero-coefficients
fn trim_coefs(&mut self) {
while self.coefs.len() > 1 && self.coefs.last().unwrap().is_zero() {
// safe unwrap
self.coefs.pop().unwrap(); // safe unwrap
}
}
/// Return degree of the polynomial
/// # Example:
/// ```
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let poly = FpPolynomial::<Fr>::from_coefs(vec![Fr::ONE; 10]);
/// assert_eq!(poly.degree(), 9);
/// let poly = FpPolynomial::<Fr>::from_coefs(vec![Fr::ZERO; 10]);
/// assert_eq!(poly.degree(), 0)
/// ```
pub fn degree(&self) -> usize {
if self.coefs.len() == 0 {
0
} else {
self.coefs.len() - 1
}
}
/// Test if polynomial is the zero polynomial.
/// # Example:
/// ```
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let poly = FpPolynomial::<Fr>::from_coefs(vec![Fr::ONE; 10]);
/// assert!(!poly.is_zero());
/// let poly = FpPolynomial::<Fr>::from_coefs(vec![Fr::ZERO; 10]);
/// assert!(poly.is_zero());
/// ```
pub fn is_zero(&self) -> bool {
self.degree() == 0 && self.coefs[0].is_zero()
}
/// Evaluate a polynomial on a point.
pub fn eval(&self, point: &F) -> F {
let mut result = F::ZERO;
let mut variable = F::ONE;
let num_coefs = self.coefs.len();
for coef in self.coefs[0..num_coefs].iter() {
let mut a = variable;
a.mul_assign(coef);
result.add_assign(&a);
variable.mul_assign(point);
}
result
}
/// Add another polynomial to self.
pub fn add_assign(&mut self, other: &Self) {
for (self_coef, other_coef) in self.coefs.iter_mut().zip(other.coefs.iter()) {
self_coef.add_assign(other_coef);
}
let n = self.coefs.len();
if n < other.coefs.len() {
for other_coef in other.coefs[n..].iter() {
self.coefs.push(*other_coef);
}
}
self.trim_coefs();
}
/// Add with another polynomial, producing a new polynomial.
/// # Example:
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let three = two.add(&one);
/// let poly1 = FpPolynomial::from_coefs(vec![zero, one, two, three]);
/// let poly2 = FpPolynomial::from_coefs(vec![three, two, one, zero, one]);
/// let poly_add = poly1.add(&poly2);
/// let poly_add2 = poly2.add(&poly1);
/// assert_eq!(poly_add, poly_add2);
/// let poly_expected = FpPolynomial::from_coefs(vec![three, three, three, three, one]);
/// assert_eq!(poly_add, poly_expected);
/// ```
pub fn add(&self, other: &Self) -> Self {
let mut new = self.clone();
new.add_assign(other);
new
}
/// Subtracts another polynomial from self.
/// # Example:
/// ```
/// use ark_std::ops::Neg;
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let three = two.add(&one);
/// let mut poly1 = FpPolynomial::from_coefs(vec![three, three, two, one]);
/// let poly2 = FpPolynomial::from_coefs(vec![three, two, one, one]);
/// poly1.sub_assign(&poly2);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, one, one]);
/// assert_eq!(poly1, poly_expected);
/// // second polynomial is of lower degree
/// let mut poly1 = FpPolynomial::from_coefs(vec![three, three, two, one]);
/// let poly2 = FpPolynomial::from_coefs(vec![three, two, one]);
/// poly1.sub_assign(&poly2);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, one, one, one]);
/// assert_eq!(poly1, poly_expected);
/// // first polynomial is of lower degree
/// let mut poly1 = FpPolynomial::from_coefs(vec![three, three, two]);
/// let poly2 = FpPolynomial::from_coefs(vec![three, two, one, one]);
/// poly1.sub_assign(&poly2);
/// let mut minus_one = one.neg();
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, one, one, minus_one]);
/// assert_eq!(poly1, poly_expected);
/// ```
pub fn sub_assign(&mut self, other: &Self) {
for (self_coef, other_coef) in self.coefs.iter_mut().zip(other.coefs.iter()) {
self_coef.sub_assign(other_coef);
}
let n = self.coefs.len();
if other.coefs.len() > n {
for other_coef in other.coefs[n..].iter() {
self.coefs.push(other_coef.neg());
}
}
self.trim_coefs();
}
/// Subtract another polynomial from self, producing a new polynomial.
/// # Example:
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let three = two.add(&one);
/// let poly1 = FpPolynomial::from_coefs(vec![three, three, two, one]);
/// let poly2 = FpPolynomial::from_coefs(vec![three, two, one, one]);
/// let poly_sub = poly1.sub(&poly2);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, one, one]);
/// assert_eq!(poly_sub, poly_expected);
/// ```
pub fn sub(&self, other: &Self) -> Self {
let mut new = self.clone();
new.sub_assign(other);
new
}
/// Negate the coefficients.
/// # Example:
/// ```
/// use ark_std::ops::Neg;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let minus_one = one.neg();
/// let mut poly = FpPolynomial::from_coefs(vec![zero, one]);
/// poly.neg_assign();
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, minus_one]);
/// assert_eq!(poly, poly_expected);
/// ```
pub fn neg_assign(&mut self) {
let minus_one = F::ONE.neg();
self.mul_scalar_assign(&minus_one);
}
/// negate the coefficients.
/// # Example:
/// ```
/// use ark_std::ops::Neg;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let minus_one = one.neg();
/// let poly = FpPolynomial::from_coefs(vec![zero, one]);
/// let negated = poly.neg();
/// let expected = FpPolynomial::from_coefs(vec![zero, minus_one]);
/// assert_eq!(negated, expected);
/// ```
pub fn neg(&self) -> Self {
let mut new = self.clone();
new.neg_assign();
new
}
/// Add `coef` to the coefficient of order `order`.
/// # Example:
/// ```
/// use ark_std::ops::Neg;
/// use ark_std::ops::AddAssign;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let mut two = one;
/// two.add_assign(&one);
/// let mut poly = FpPolynomial::from_coefs(vec![zero, one, one]);
/// poly.add_coef_assign(&one, 1);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, two, one]);
/// assert_eq!(poly, poly_expected);
/// poly.add_coef_assign(&one, 3);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, two, one, one]);
/// let mut minus_one = one.neg();
/// poly.add_coef_assign(&minus_one, 3);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, two, one]);
/// assert_eq!(poly, poly_expected);
/// ```
pub fn add_coef_assign(&mut self, coef: &F, order: usize) {
while self.degree() < order {
self.coefs.push(F::ZERO);
}
self.coefs[order].add_assign(coef);
self.trim_coefs();
}
/// Multiply polynomial by a constant scalar.
/// # Example:
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let mut poly = FpPolynomial::from_coefs(vec![zero, one, one]);
/// poly.mul_scalar_assign(&two);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, two, two]);
/// // multiply y zero
/// assert_eq!(poly, poly_expected);
/// poly.mul_scalar_assign(&zero);
/// assert!(poly.is_zero());
/// ```
pub fn mul_scalar_assign(&mut self, scalar: &F) {
#[cfg(not(feature = "parallel"))]
{
for coef in self.coefs.iter_mut() {
coef.mul_assign(scalar)
}
}
#[cfg(feature = "parallel")]
{
self.coefs
.par_iter_mut()
.for_each(|coef| coef.mul_assign(scalar));
}
self.trim_coefs();
}
/// Multiply polynomial by a constant scalar into a new polynomial.
/// # Example:
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let mut poly = FpPolynomial::from_coefs(vec![zero, one, one]);
/// let new = poly.mul_scalar(&two);
/// let poly_expected = FpPolynomial::from_coefs(vec![zero, two, two]);
/// assert_eq!(new, poly_expected);
/// ```
pub fn mul_scalar(&self, scalar: &F) -> Self {
let mut new = self.clone();
new.mul_scalar_assign(scalar);
new
}
/// Multiply the polynomial variable by a scalar.
/// mul_var(\sum a_i X^i, b) = \sum a_i b^i X^i
/// # Example:
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let four = two.add(&two);
/// let mut poly = FpPolynomial::from_coefs(vec![zero, one, one]);
/// poly.mul_var_assign(&two);
/// let expected = FpPolynomial::from_coefs(vec![zero, two, four]);
/// assert_eq!(poly, expected);
/// ```
pub fn mul_var_assign(&mut self, scalar: &F) {
let mut r = F::ONE;
for coefs in self.coefs.iter_mut() {
coefs.mul_assign(&r);
r.mul_assign(scalar);
}
self.trim_coefs();
}
/// Multiply polynomial variable by a scalar
/// # Example:
/// ```
/// use ark_std::ops::Add;
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let two = one.add(&one);
/// let four = two.add(&two);
/// let mut poly = FpPolynomial::from_coefs(vec![zero, one, one]);
/// let result = poly.mul_var(&two);
/// let expected = FpPolynomial::from_coefs(vec![zero, two, four]);
/// assert_eq!(result, expected);
/// ```
pub fn mul_var(&self, scalar: &F) -> Self {
let mut new = self.clone();
new.mul_var_assign(scalar);
new
}
/// Divide polynomial to produce the quotient and remainder polynomials.
/// # Example:
/// ```
/// use turboplonk::poly_commit::field_polynomial::FpPolynomial;
/// use ark_bn254::Fr;
/// use ark_ff::{Zero, One, Field, AdditiveGroup};
///
/// let zero = Fr::ZERO;
/// let one = Fr::ONE;
/// let mut poly = FpPolynomial::from_coefs(vec![one, one, one]);
/// let mut divisor = FpPolynomial::from_coefs(vec![one, one]);
/// let expected_quo = FpPolynomial::from_coefs(vec![zero, one]);
/// let expected_rem = FpPolynomial::from_coefs(vec![one]);
/// let (q, r) = poly.div_rem(&divisor);
/// assert_eq!(q, expected_quo);
/// assert_eq!(r, expected_rem);
/// ```
pub fn div_rem(&self, divisor: &Self) -> (Self, Self) {
let k = self.coefs.len();
let l = divisor.coefs.len();
if l > k {
return (Self::zero(), self.clone());
}
let divisor_coefs = &divisor.coefs[..];
let bl_inv = divisor_coefs.last().unwrap().clone().inverse().unwrap();
let mut rem = self.coefs.clone();
let mut quo: Vec<F> = (0..k - l + 1).map(|_| F::ZERO).collect();
for i in (0..(k - l + 1)).rev() {
let mut qi = bl_inv;
qi.mul_assign(&rem[i + l - 1]);
for j in 0..l {
let mut a = qi;
a.mul_assign(&divisor_coefs[j]);
rem[i + j].sub_assign(&a);
}
quo[i] = qi;
}
for _ in 0..k - l + 1 {
rem.pop();
}
if rem.is_empty() {
rem.push(F::ZERO);
}
let mut q = FpPolynomial::from_coefs(quo);
q.trim_coefs();
let mut r = FpPolynomial::from_coefs(rem);
r.trim_coefs();
(q, r)
}
/// Construct a domain for evaluations of a polynomial having `num_coeffs` coefficients,
/// where `num_coeffs` is with the form 2^k.
pub fn evaluation_domain(num_coeffs: usize) -> Option<Radix2EvaluationDomain<F>> {
assert!(num_coeffs.is_power_of_two());
Radix2EvaluationDomain::<F>::new(num_coeffs)
}
/// Construct a domain for evaluations of a polynomial having `num_coeffs` coefficients,
/// where `num_coeffs` is with the form 2^k or 3 * 2^k.
pub fn quotient_evaluation_domain(num_coeffs: usize) -> Option<MixedRadixEvaluationDomain<F>> {
assert!(
num_coeffs.is_power_of_two()
|| ((num_coeffs % 3 == 0) && (num_coeffs / 3).is_power_of_two())
);
MixedRadixEvaluationDomain::<F>::new(num_coeffs)
}
/// Compute the FFT of the polynomial, the parameter `num_coeffs` is with the form 2^k or 3 * 2^k.
pub fn fft(&self, num_coeffs: usize) -> Option<Vec<F>> {
assert!(num_coeffs > self.degree());
if num_coeffs.is_power_of_two() {
let domain = Self::evaluation_domain(num_coeffs)?;
Some(self.fft_with_domain(&domain))
} else {
let domain = Self::quotient_evaluation_domain(num_coeffs)?;
Some(self.fft_with_domain(&domain))
}
}
/// Compute the FFT of the polynomial with the given domain.
pub fn fft_with_domain<E: EvaluationDomain<F>>(&self, domain: &E) -> Vec<F> {
assert!(domain.size() > self.degree());
domain.fft(&self.coefs)
}
/// Compute the FFT of the polynomial on the set k * <root>.
pub fn coset_fft_with_domain<E: EvaluationDomain<F>>(&self, domain: &E, k: &F) -> Vec<F> {
self.mul_var(k).fft_with_domain(domain)
}
/// Compute the polynomial given its evaluation values and domain.
pub fn ifft_with_domain<E: EvaluationDomain<F>>(domain: &E, values: &[F]) -> Self {
let coefs = domain.ifft(&values);
Self::from_coefs(coefs)
}
/// Compute the polynomial given its evaluation values at a coset k * H,
/// where H is evaluation domain and k_inv is the inverse of k.
pub fn coset_ifft_with_domain<E: EvaluationDomain<F>>(
domain: &E,
values: &[F],
k_inv: &F,
) -> Self {
Self::ifft_with_domain(domain, values).mul_var(k_inv)
}
}
macro_rules! _test_polynomial {
($scalar: ty) => {
#[test]
fn from_zeroes() {
let n = 10;
let mut zeroes = vec![];
let mut prng = test_rng();
for _ in 0..n {
zeroes.push(<$scalar>::rand(&mut prng));
}
let poly = FpPolynomial::from_zeroes(&zeroes[..]);
for root in zeroes.iter() {
assert_eq!(<$scalar>::ZERO, poly.eval(root));
}
let zeroes_ref: Vec<&$scalar> = zeroes.iter().collect();
let poly = FpPolynomial::from_zeroes_ref(&zeroes_ref);
for root in zeroes.iter() {
assert_eq!(<$scalar>::ZERO, poly.eval(root));
}
}
fn check_fft<F: PrimeField>(poly: &FpPolynomial<F>, root: &F, fft: &[F]) -> bool {
assert!(
fft.len().is_power_of_two()
|| ((fft.len() % 3 == 0) && (fft.len() / 3).is_power_of_two())
);
let mut omega = F::ONE;
for fft_elem in fft {
if *fft_elem != poly.eval(&omega) {
return false;
}
omega.mul_assign(root)
}
true
}
#[test]
fn test_fft() {
let mut prng = test_rng();
let zero = <$scalar>::ZERO;
let one = <$scalar>::ONE;
let polynomial = FpPolynomial::from_coefs(vec![one]);
let fft = polynomial.fft(1).unwrap();
let domain = FpPolynomial::<$scalar>::evaluation_domain(1).unwrap();
assert!(check_fft(&polynomial, &domain.group_gen, &fft));
let polynomial = FpPolynomial::from_coefs(vec![one, one]);
let fft = polynomial.fft(2).unwrap();
let domain = FpPolynomial::<$scalar>::evaluation_domain(2).unwrap();
assert!(check_fft(&polynomial, &domain.group_gen, &fft));
let polynomial = FpPolynomial::from_coefs(vec![one, zero]);
let fft = polynomial.fft(2).unwrap();
assert!(check_fft(&polynomial, &domain.group_gen, &fft));
let polynomial = FpPolynomial::from_coefs(vec![zero, one]);
let fft = polynomial.fft(2).unwrap();
assert!(check_fft(&polynomial, &domain.group_gen, &fft));
let polynomial = FpPolynomial::from_coefs(vec![zero, one, one]);
let fft = polynomial.fft(3).unwrap();
let domain = FpPolynomial::<$scalar>::quotient_evaluation_domain(3).unwrap();
assert!(check_fft(&polynomial, &domain.group_gen, &fft));
let ffti_polynomial = FpPolynomial::ifft_with_domain(&domain, &fft);
assert_eq!(ffti_polynomial, polynomial);
let mut coefs = vec![];
for _ in 0..16 {
coefs.push(<$scalar>::rand(&mut prng));
}
let polynomial = FpPolynomial::from_coefs(coefs);
let fft = polynomial.fft(16).unwrap();
let domain = FpPolynomial::<$scalar>::evaluation_domain(16).unwrap();
let ffti_polynomial = FpPolynomial::ifft_with_domain(&domain, &fft);
assert_eq!(ffti_polynomial, polynomial);
let mut coefs = vec![];
for _ in 0..32 {
coefs.push(<$scalar>::rand(&mut prng));
}
let polynomial = FpPolynomial::from_coefs(coefs);
let domain = FpPolynomial::<$scalar>::evaluation_domain(32).unwrap();
let fft = polynomial.fft_with_domain(&domain);
let ffti_polynomial = FpPolynomial::ifft_with_domain(&domain, &fft);
assert_eq!(ffti_polynomial, polynomial);
let mut coefs = vec![];
for _ in 0..3 {
coefs.push(<$scalar>::rand(&mut prng));
}
let polynomial = FpPolynomial::from_coefs(coefs);
let domain = FpPolynomial::<$scalar>::quotient_evaluation_domain(3).unwrap();
let fft = polynomial.fft_with_domain(&domain);
let ffti_polynomial = FpPolynomial::ifft_with_domain(&domain, &fft);
assert_eq!(ffti_polynomial, polynomial);
let mut coefs = vec![];
for _ in 0..48 {
coefs.push(<$scalar>::rand(&mut prng));
}
let polynomial = FpPolynomial::from_coefs(coefs);
let domain = FpPolynomial::<$scalar>::quotient_evaluation_domain(48).unwrap();
let fft = polynomial.fft_with_domain(&domain);
let ffti_polynomial = FpPolynomial::ifft_with_domain(&domain, &fft);
assert_eq!(ffti_polynomial, polynomial);
}
};
}
#[cfg(test)]
mod test_polynomial_bn254 {
use crate::poly_commit::field_polynomial::FpPolynomial;
use ark_bn254::Fr;
use ark_ff::{AdditiveGroup, Field, PrimeField, UniformRand};
use ark_std::test_rng;
_test_polynomial!(Fr);
}