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
use crate::domain::DomainCoeff;
use ark_ff::{FftField, Field};
use ark_std::vec::Vec;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
#[allow(unused)]
#[cfg(feature = "parallel")]
const MIN_PARALLEL_CHUNK_SIZE: usize = 1 << 7;
#[inline]
pub(crate) fn bitreverse(mut n: u32, l: u32) -> u32 {
let mut r = 0;
for _ in 0..l {
r = (r << 1) | (n & 1);
n >>= 1;
}
r
}
pub(crate) fn compute_powers_serial<F: Field>(size: usize, root: F) -> Vec<F> {
compute_powers_and_mul_by_const_serial(size, root, F::one())
}
pub(crate) fn compute_powers_and_mul_by_const_serial<F: Field>(
size: usize,
root: F,
c: F,
) -> Vec<F> {
let mut value = c;
(0..size)
.map(|_| {
let old_value = value;
value *= root;
old_value
})
.collect()
}
#[allow(unused)]
#[cfg(feature = "parallel")]
pub(crate) fn compute_powers<F: Field>(size: usize, g: F) -> Vec<F> {
if size < MIN_PARALLEL_CHUNK_SIZE {
return compute_powers_serial(size, g);
}
use ark_std::cmp::{max, min};
let num_cpus_available = rayon::current_num_threads();
let num_elem_per_thread = max(size / num_cpus_available, MIN_PARALLEL_CHUNK_SIZE);
let num_cpus_used = size / num_elem_per_thread;
let res: Vec<F> = (0..num_cpus_used)
.into_par_iter()
.flat_map(|i| {
let offset = g.pow(&[(i * num_elem_per_thread) as u64]);
let num_elements_to_compute = min(size - i * num_elem_per_thread, num_elem_per_thread);
let res = compute_powers_and_mul_by_const_serial(num_elements_to_compute, g, offset);
res
})
.collect();
res
}
#[cfg(feature = "parallel")]
fn log2_floor(num: usize) -> u32 {
if num == 0 {
0
} else {
1usize.leading_zeros() - num.leading_zeros()
}
}
#[cfg(feature = "parallel")]
pub(crate) fn best_fft<T: DomainCoeff<F>, F: FftField>(
a: &mut [T],
omega: F,
log_n: u32,
serial_fft: fn(&mut [T], F, u32),
) {
let num_cpus = rayon::current_num_threads();
let log_cpus = log2_floor(num_cpus);
if log_n <= log_cpus {
serial_fft(a, omega, log_n);
} else {
parallel_fft(a, omega, log_n, log_cpus, serial_fft);
}
}
#[cfg(not(feature = "parallel"))]
#[inline]
pub(crate) fn best_fft<T: DomainCoeff<F>, F: FftField>(
a: &mut [T],
omega: F,
log_n: u32,
serial_fft: fn(&mut [T], F, u32),
) {
serial_fft(a, omega, log_n)
}
#[cfg(feature = "parallel")]
pub(crate) fn parallel_fft<T: DomainCoeff<F>, F: FftField>(
a: &mut [T],
omega: F,
log_n: u32,
log_cpus: u32,
serial_fft: fn(&mut [T], F, u32),
) {
assert!(log_n >= log_cpus);
let m = a.len();
let num_threads = 1 << (log_cpus as usize);
let num_cosets = num_threads;
assert_eq!(m % num_threads, 0);
let coset_size = m / num_threads;
let mut tmp = vec![vec![T::zero(); coset_size]; num_cosets];
let new_omega = omega.pow(&[num_cosets as u64]);
let new_two_adicity = ark_ff::utils::k_adicity(2, coset_size as u64);
tmp.par_iter_mut()
.enumerate()
.for_each(|(k, kth_poly_coeffs)| {
let omega_k = omega.pow(&[k as u64]);
let omega_step = omega.pow(&[(k * coset_size) as u64]);
let mut elt = F::one();
for i in 0..coset_size {
for c in 0..num_threads {
let idx = i + (c * coset_size);
let mut t = a[idx];
t *= elt;
kth_poly_coeffs[i] += t;
elt *= &omega_step;
}
elt *= &omega_k;
}
serial_fft(kth_poly_coeffs, new_omega, new_two_adicity);
});
a.iter_mut()
.enumerate()
.for_each(|(i, a)| *a = tmp[i % num_cosets][i / num_cosets]);
}
pub struct Elements<F: FftField> {
pub(crate) cur_elem: F,
pub(crate) cur_pow: u64,
pub(crate) size: u64,
pub(crate) group_gen: F,
}
impl<F: FftField> Iterator for Elements<F> {
type Item = F;
fn next(&mut self) -> Option<F> {
if self.cur_pow == self.size {
None
} else {
let cur_elem = self.cur_elem;
self.cur_elem *= &self.group_gen;
self.cur_pow += 1;
Some(cur_elem)
}
}
}