forked from filecoin-project/bellperson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain.rs
646 lines (545 loc) · 20.2 KB
/
domain.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
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
//! This module contains an [`EvaluationDomain`] abstraction for performing
//! various kinds of polynomial arithmetic on top of the scalar field.
//!
//! In pairing-based SNARKs like [Groth16], we need to calculate a quotient
//! polynomial over a target polynomial with roots at distinct points associated
//! with each constraint of the constraint system. In order to be efficient, we
//! choose these roots to be the powers of a 2<sup>n</sup> root of unity in the
//! field. This allows us to perform polynomial operations in O(n) by performing
//! an O(n log n) FFT over such a domain.
//!
//! [`EvaluationDomain`]: crate::domain::EvaluationDomain
//! [Groth16]: https://eprint.iacr.org/2016/260
use ff::{Field, PrimeField};
use pairing::Engine;
use super::multicore::Worker;
use super::SynthesisError;
use crate::gpu;
use log::{info, warn};
pub struct EvaluationDomain<E: Engine + gpu::GpuEngine> {
coeffs: Vec<E::Fr>,
exp: u32,
omega: E::Fr,
omegainv: E::Fr,
geninv: E::Fr,
minv: E::Fr,
}
impl<E: Engine + gpu::GpuEngine> AsRef<[E::Fr]> for EvaluationDomain<E> {
fn as_ref(&self) -> &[E::Fr] {
&self.coeffs
}
}
impl<E: Engine + gpu::GpuEngine> AsMut<[E::Fr]> for EvaluationDomain<E> {
fn as_mut(&mut self) -> &mut [E::Fr] {
&mut self.coeffs
}
}
impl<E: Engine + gpu::GpuEngine> EvaluationDomain<E> {
pub fn into_coeffs(self) -> Vec<E::Fr> {
self.coeffs
}
pub fn from_coeffs(mut coeffs: Vec<E::Fr>) -> Result<EvaluationDomain<E>, SynthesisError> {
// Compute the size of our evaluation domain
let mut m = 1;
let mut exp = 0;
while m < coeffs.len() {
m *= 2;
exp += 1;
// The pairing-friendly curve may not be able to support
// large enough (radix2) evaluation domains.
if exp >= E::Fr::S {
return Err(SynthesisError::PolynomialDegreeTooLarge);
}
}
// Compute omega, the 2^exp primitive root of unity
let mut omega = E::Fr::root_of_unity();
for _ in exp..E::Fr::S {
omega = omega.square();
}
// Extend the coeffs vector with zeroes if necessary
coeffs.resize(m, E::Fr::zero());
Ok(EvaluationDomain {
coeffs,
exp,
omega,
omegainv: omega.invert().unwrap(),
geninv: E::Fr::multiplicative_generator().invert().unwrap(),
minv: E::Fr::from(m as u64).invert().unwrap(),
})
}
pub fn fft(
&mut self,
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
best_fft::<E>(
kern,
worker,
&mut [&mut self.coeffs],
&[self.omega],
&[self.exp],
);
Ok(())
}
/// Execute three FFTs in parallel.
pub fn fft_many(
domains: &mut [&mut Self],
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
let (mut coeffs, rest): (Vec<_>, Vec<_>) = domains
.iter_mut()
.map(|domain| (&mut domain.coeffs[..], (domain.omega, domain.exp)))
.unzip();
let (omegas, exps): (Vec<_>, Vec<_>) = rest.into_iter().unzip();
best_fft(kern, worker, &mut coeffs[..], &omegas, &exps);
Ok(())
}
pub fn ifft(
&mut self,
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
Self::ifft_many(&mut [self], worker, kern)
}
/// Execute multiple IFFTs in parallel.
pub fn ifft_many(
domains: &mut [&mut Self],
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
let (mut coeffs, rest): (Vec<_>, Vec<_>) = domains
.iter_mut()
.map(|domain| (&mut domain.coeffs[..], (domain.omegainv, domain.exp)))
.unzip();
let (omegas, exps): (Vec<_>, Vec<_>) = rest.into_iter().unzip();
best_fft(kern, worker, &mut coeffs, &omegas, &exps);
for domain in domains {
worker.scope(domain.coeffs.len(), |scope, chunk| {
let minv = domain.minv;
for v in domain.coeffs.chunks_mut(chunk) {
scope.execute(move || {
for v in v {
*v *= minv;
}
});
}
});
}
Ok(())
}
pub fn distribute_powers(&mut self, worker: &Worker, g: E::Fr) {
worker.scope(self.coeffs.len(), |scope, chunk| {
for (i, v) in self.coeffs.chunks_mut(chunk).enumerate() {
scope.execute(move || {
let mut u = g.pow_vartime(&[(i * chunk) as u64]);
for v in v.iter_mut() {
*v *= u;
u *= g;
}
});
}
});
}
pub fn coset_fft(
&mut self,
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
Self::coset_fft_many(&mut [self], worker, kern)
}
/// Execute three Coset FFTs in parallel.
pub fn coset_fft_many(
domains: &mut [&mut Self],
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
for domain in domains.iter_mut() {
domain.distribute_powers(worker, E::Fr::multiplicative_generator());
}
Self::fft_many(domains, worker, kern)?;
Ok(())
}
pub fn icoset_fft(
&mut self,
worker: &Worker,
kern: &mut Option<gpu::LockedFFTKernel<E>>,
) -> gpu::GPUResult<()> {
let geninv = self.geninv;
self.ifft(worker, kern)?;
self.distribute_powers(worker, geninv);
Ok(())
}
/// This evaluates t(tau) for this domain, which is
/// tau^m - 1 for these radix-2 domains.
pub fn z(&self, tau: &E::Fr) -> E::Fr {
let tmp = tau.pow_vartime(&[self.coeffs.len() as u64]);
tmp - E::Fr::one()
}
/// The target polynomial is the zero polynomial in our
/// evaluation domain, so we must perform division over
/// a coset.
pub fn divide_by_z_on_coset(&mut self, worker: &Worker) {
let i = self.z(&E::Fr::multiplicative_generator()).invert().unwrap();
worker.scope(self.coeffs.len(), |scope, chunk| {
for v in self.coeffs.chunks_mut(chunk) {
scope.execute(move || {
for v in v {
*v *= i;
}
});
}
});
}
/// Perform O(n) multiplication of two polynomials in the domain.
pub fn mul_assign(&mut self, worker: &Worker, other: &EvaluationDomain<E>) {
assert_eq!(self.coeffs.len(), other.coeffs.len());
worker.scope(self.coeffs.len(), |scope, chunk| {
for (a, b) in self
.coeffs
.chunks_mut(chunk)
.zip(other.coeffs.chunks(chunk))
{
scope.execute(move || {
for (a, b) in a.iter_mut().zip(b.iter()) {
*a *= b;
}
});
}
});
}
/// Perform O(n) subtraction of one polynomial from another in the domain.
pub fn sub_assign(&mut self, worker: &Worker, other: &EvaluationDomain<E>) {
assert_eq!(self.coeffs.len(), other.coeffs.len());
worker.scope(self.coeffs.len(), |scope, chunk| {
for (a, b) in self
.coeffs
.chunks_mut(chunk)
.zip(other.coeffs.chunks(chunk))
{
scope.execute(move || {
for (a, b) in a.iter_mut().zip(b.iter()) {
*a -= b;
}
});
}
});
}
}
fn best_fft<E: Engine + gpu::GpuEngine>(
kern: &mut Option<gpu::LockedFFTKernel<E>>,
worker: &Worker,
coeffs: &mut [&mut [E::Fr]],
omegas: &[E::Fr],
log_ns: &[u32],
) {
if let Some(ref mut kern) = kern {
if kern
.with(|k: &mut gpu::FFTKernel<E>| gpu_fft(k, coeffs, omegas, log_ns))
.is_ok()
{
return;
}
}
let log_cpus = worker.log_num_cpus();
for ((a, omega), log_n) in coeffs.iter_mut().zip(omegas.iter()).zip(log_ns.iter()) {
if *log_n <= log_cpus {
serial_fft::<E>(*a, omega, *log_n);
} else {
parallel_fft::<E>(*a, worker, omega, *log_n, log_cpus);
}
}
}
pub fn gpu_fft<E: Engine + gpu::GpuEngine>(
kern: &mut gpu::FFTKernel<E>,
coeffs: &mut [&mut [E::Fr]],
omegas: &[E::Fr],
log_ns: &[u32],
) -> gpu::GPUResult<()> {
kern.radix_fft_many(coeffs, omegas, log_ns)
}
#[allow(clippy::many_single_char_names)]
pub fn serial_fft<E: Engine>(a: &mut [E::Fr], omega: &E::Fr, log_n: u32) {
fn bitreverse(mut n: u32, l: u32) -> u32 {
let mut r = 0;
for _ in 0..l {
r = (r << 1) | (n & 1);
n >>= 1;
}
r
}
let n = a.len() as u32;
assert_eq!(n, 1 << log_n);
for k in 0..n {
let rk = bitreverse(k, log_n);
if k < rk {
a.swap(rk as usize, k as usize);
}
}
let mut m = 1;
for _ in 0..log_n {
let w_m = omega.pow_vartime(&[u64::from(n / (2 * m))]);
let mut k = 0;
while k < n {
let mut w = E::Fr::one();
for j in 0..m {
let mut t = a[(k + j + m) as usize];
t *= w;
let mut tmp = a[(k + j) as usize];
tmp -= t;
a[(k + j + m) as usize] = tmp;
a[(k + j) as usize] += t;
w *= w_m;
}
k += 2 * m;
}
m *= 2;
}
}
fn parallel_fft<E: Engine>(
a: &mut [E::Fr],
worker: &Worker,
omega: &E::Fr,
log_n: u32,
log_cpus: u32,
) {
assert!(log_n >= log_cpus);
let num_cpus = 1 << log_cpus;
let log_new_n = log_n - log_cpus;
let mut tmp = vec![vec![E::Fr::zero(); 1 << log_new_n]; num_cpus];
let new_omega = omega.pow_vartime(&[num_cpus as u64]);
worker.scope(0, |scope, _| {
let a = &*a;
for (j, tmp) in tmp.iter_mut().enumerate() {
scope.execute(move || {
// Shuffle into a sub-FFT
let omega_j = omega.pow_vartime(&[j as u64]);
let omega_step = omega.pow_vartime(&[(j as u64) << log_new_n]);
let mut elt = E::Fr::one();
for (i, tmp) in tmp.iter_mut().enumerate() {
for s in 0..num_cpus {
let idx = (i + (s << log_new_n)) % (1 << log_n);
let mut t = a[idx];
t *= elt;
*tmp += t;
elt *= omega_step;
}
elt *= omega_j;
}
// Perform sub-FFT
serial_fft::<E>(tmp, &new_omega, log_new_n);
});
}
});
// TODO: does this hurt or help?
worker.scope(a.len(), |scope, chunk| {
let tmp = &tmp;
for (idx, a) in a.chunks_mut(chunk).enumerate() {
scope.execute(move || {
let mut idx = idx * chunk;
let mask = (1 << log_cpus) - 1;
for a in a {
*a = tmp[idx & mask][idx >> log_cpus];
idx += 1;
}
});
}
});
}
// Test multiplying various (low degree) polynomials together and
// comparing with naive evaluations.
#[test]
fn polynomial_arith() {
use blstrs::Bls12;
use rand_core::RngCore;
fn test_mul<E: Engine + gpu::GpuEngine, R: RngCore>(rng: &mut R) {
let worker = Worker::new();
for coeffs_a in 0..70 {
for coeffs_b in 0..70 {
let mut a: Vec<_> = (0..coeffs_a).map(|_| E::Fr::random(&mut *rng)).collect();
let mut b: Vec<_> = (0..coeffs_b).map(|_| E::Fr::random(&mut *rng)).collect();
// naive evaluation
let mut naive = vec![E::Fr::zero(); coeffs_a + coeffs_b];
for (i1, a) in a.iter().enumerate() {
for (i2, b) in b.iter().enumerate() {
naive[i1 + i2] += *a * b;
}
}
a.resize(coeffs_a + coeffs_b, E::Fr::zero());
b.resize(coeffs_a + coeffs_b, E::Fr::zero());
let mut a = EvaluationDomain::<E>::from_coeffs(a).unwrap();
let mut b = EvaluationDomain::<E>::from_coeffs(b).unwrap();
a.fft(&worker, &mut None).unwrap();
b.fft(&worker, &mut None).unwrap();
a.mul_assign(&worker, &b);
a.ifft(&worker, &mut None).unwrap();
for (naive, fft) in naive.iter().zip(a.coeffs.iter()) {
assert!(naive == fft);
}
}
}
}
let rng = &mut rand::thread_rng();
test_mul::<Bls12, _>(rng);
}
#[test]
fn fft_composition() {
use blstrs::Bls12;
use pairing::Engine;
use rand_core::RngCore;
fn test_comp<E: Engine + gpu::GpuEngine, R: RngCore>(rng: &mut R) {
let worker = Worker::new();
for coeffs in 0..10 {
let coeffs = 1 << coeffs;
let mut v = vec![];
for _ in 0..coeffs {
v.push(E::Fr::random(&mut *rng));
}
let mut domain = EvaluationDomain::<E>::from_coeffs(v.clone()).unwrap();
domain.ifft(&worker, &mut None).unwrap();
domain.fft(&worker, &mut None).unwrap();
assert!(v == domain.coeffs);
domain.fft(&worker, &mut None).unwrap();
domain.ifft(&worker, &mut None).unwrap();
assert!(v == domain.coeffs);
domain.icoset_fft(&worker, &mut None).unwrap();
domain.coset_fft(&worker, &mut None).unwrap();
assert!(v == domain.coeffs);
domain.coset_fft(&worker, &mut None).unwrap();
domain.icoset_fft(&worker, &mut None).unwrap();
assert!(v == domain.coeffs);
}
}
let rng = &mut rand::thread_rng();
test_comp::<Bls12, _>(rng);
}
#[test]
fn parallel_fft_consistency() {
use blstrs::Bls12;
use rand_core::RngCore;
use std::cmp::min;
fn test_consistency<E: Engine + gpu::GpuEngine, R: RngCore>(rng: &mut R) {
let worker = Worker::new();
for _ in 0..5 {
for log_d in 0..10 {
let d = 1 << log_d;
let v1 = (0..d).map(|_| E::Fr::random(&mut *rng)).collect::<Vec<_>>();
let mut v1 = EvaluationDomain::<E>::from_coeffs(v1).unwrap();
let mut v2 = EvaluationDomain::<E>::from_coeffs(v1.coeffs.clone()).unwrap();
for log_cpus in log_d..min(log_d + 1, 3) {
parallel_fft::<E>(&mut v1.coeffs, &worker, &v1.omega, log_d, log_cpus);
serial_fft::<E>(&mut v2.coeffs, &v2.omega, log_d);
assert!(v1.coeffs == v2.coeffs);
}
}
}
}
let rng = &mut rand::thread_rng();
test_consistency::<Bls12, _>(rng);
}
pub fn create_fft_kernel<E>(_log_d: usize, priority: bool) -> Option<gpu::FFTKernel<E>>
where
E: Engine + gpu::GpuEngine,
{
match gpu::FFTKernel::create(priority) {
Ok(k) => {
info!("GPU FFT kernel instantiated!");
Some(k)
}
Err(e) => {
warn!("Cannot instantiate GPU FFT kernel! Error: {}", e);
None
}
}
}
#[cfg(any(feature = "cuda", feature = "opencl"))]
#[cfg(test)]
mod tests {
use super::*;
use crate::gpu;
use crate::multicore::Worker;
use blstrs::{Bls12, Scalar as Fr};
use ff::Field;
use std::time::Instant;
#[test]
pub fn gpu_fft_consistency() {
let _ = env_logger::try_init();
gpu::dump_device_list();
let mut rng = rand::thread_rng();
let worker = Worker::new();
let log_cpus = worker.log_num_cpus();
let mut kern = gpu::FFTKernel::<Bls12>::create(false).expect("Cannot initialize kernel!");
for log_d in 1..=20 {
let d = 1 << log_d;
let elems = (0..d).map(|_| Fr::random(&mut rng)).collect::<Vec<_>>();
let mut v1 = EvaluationDomain::<Bls12>::from_coeffs(elems.clone()).unwrap();
let mut v2 = EvaluationDomain::<Bls12>::from_coeffs(elems.clone()).unwrap();
println!("Testing FFT for {} elements...", d);
let mut now = Instant::now();
gpu_fft(&mut kern, &mut [&mut v1.coeffs], &[v1.omega], &[log_d])
.expect("GPU FFT failed!");
let gpu_dur = now.elapsed().as_secs() * 1000 + now.elapsed().subsec_millis() as u64;
println!("GPU took {}ms.", gpu_dur);
now = Instant::now();
if log_d <= log_cpus {
serial_fft::<Bls12>(&mut v2.coeffs, &v2.omega, log_d);
} else {
parallel_fft::<Bls12>(&mut v2.coeffs, &worker, &v2.omega, log_d, log_cpus);
}
let cpu_dur = now.elapsed().as_secs() * 1000 + now.elapsed().subsec_millis() as u64;
println!("CPU ({} cores) took {}ms.", 1 << log_cpus, cpu_dur);
println!("Speedup: x{}", cpu_dur as f32 / gpu_dur as f32);
assert!(v1.coeffs == v2.coeffs);
println!("============================");
}
}
#[test]
pub fn gpu_fft3_consistency() {
let _ = env_logger::try_init();
gpu::dump_device_list();
let mut rng = rand::thread_rng();
let worker = Worker::new();
let log_cpus = worker.log_num_cpus();
let mut kern = gpu::FFTKernel::<Bls12>::create(false).expect("Cannot initialize kernel!");
for log_d in 1..=20 {
let d = 1 << log_d;
let elems1 = (0..d).map(|_| Fr::random(&mut rng)).collect::<Vec<_>>();
let elems2 = (0..d).map(|_| Fr::random(&mut rng)).collect::<Vec<_>>();
let elems3 = (0..d).map(|_| Fr::random(&mut rng)).collect::<Vec<_>>();
let mut v11 = EvaluationDomain::<Bls12>::from_coeffs(elems1.clone()).unwrap();
let mut v12 = EvaluationDomain::<Bls12>::from_coeffs(elems2.clone()).unwrap();
let mut v13 = EvaluationDomain::<Bls12>::from_coeffs(elems3.clone()).unwrap();
let mut v21 = EvaluationDomain::<Bls12>::from_coeffs(elems1.clone()).unwrap();
let mut v22 = EvaluationDomain::<Bls12>::from_coeffs(elems2.clone()).unwrap();
let mut v23 = EvaluationDomain::<Bls12>::from_coeffs(elems3.clone()).unwrap();
println!("Testing FFT3 for {} elements...", d);
let mut now = Instant::now();
gpu_fft(
&mut kern,
&mut [&mut v11.coeffs, &mut v12.coeffs, &mut v13.coeffs],
&[v11.omega, v12.omega, v13.omega],
&[log_d, log_d, log_d],
)
.expect("GPU FFT failed!");
let gpu_dur = now.elapsed().as_secs() * 1000 + now.elapsed().subsec_millis() as u64;
println!("GPU took {}ms.", gpu_dur);
now = Instant::now();
if log_d <= log_cpus {
serial_fft::<Bls12>(&mut v21.coeffs, &v21.omega, log_d);
serial_fft::<Bls12>(&mut v22.coeffs, &v22.omega, log_d);
serial_fft::<Bls12>(&mut v23.coeffs, &v23.omega, log_d);
} else {
parallel_fft::<Bls12>(&mut v21.coeffs, &worker, &v21.omega, log_d, log_cpus);
parallel_fft::<Bls12>(&mut v22.coeffs, &worker, &v22.omega, log_d, log_cpus);
parallel_fft::<Bls12>(&mut v23.coeffs, &worker, &v23.omega, log_d, log_cpus);
}
let cpu_dur = now.elapsed().as_secs() * 1000 + now.elapsed().subsec_millis() as u64;
println!("CPU ({} cores) took {}ms.", 1 << log_cpus, cpu_dur);
println!("Speedup: x{}", cpu_dur as f32 / gpu_dur as f32);
assert!(v11.coeffs == v21.coeffs);
assert!(v12.coeffs == v22.coeffs);
assert!(v13.coeffs == v23.coeffs);
println!("============================");
}
}
}