-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathconcurrent.rs
236 lines (204 loc) · 8.32 KB
/
concurrent.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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
use alloc::vec::Vec;
use utils::{iterators::*, rayon, uninit_vector};
use super::fft_inputs::FftInputs;
use crate::field::{FieldElement, StarkField};
// POLYNOMIAL EVALUATION
// ================================================================================================
/// Evaluates polynomial `p` using FFT algorithm; the evaluation is done in-place, meaning
/// `p` is updated with results of the evaluation.
pub fn evaluate_poly<B: StarkField, E: FieldElement<BaseField = B>>(p: &mut [E], twiddles: &[B]) {
split_radix_fft(p, twiddles);
permute(p);
}
/// Evaluates polynomial `p` using FFT algorithm and returns the result. The polynomial is
/// evaluated over domain specified by `twiddles`, expanded by the `blowup_factor`, and shifted
/// by the `domain_offset`.
pub fn evaluate_poly_with_offset<B: StarkField, E: FieldElement<BaseField = B>>(
p: &[E],
twiddles: &[B],
domain_offset: B,
blowup_factor: usize,
) -> Vec<E> {
let domain_size = p.len() * blowup_factor;
let g = B::get_root_of_unity(domain_size.ilog2());
let mut result = unsafe { uninit_vector(domain_size) };
result
.as_mut_slice()
.par_chunks_mut(p.len())
.enumerate()
.for_each(|(i, chunk)| {
let idx = super::permute_index(blowup_factor, i) as u64;
let offset = g.exp(idx.into()) * domain_offset;
clone_and_shift(p, chunk, offset);
split_radix_fft(chunk, twiddles);
});
permute(&mut result);
result
}
// POLYNOMIAL INTERPOLATION
// ================================================================================================
/// Uses FFT algorithm to interpolate a polynomial from provided `values`; the interpolation
/// is done in-place, meaning `values` are updated with polynomial coefficients.
///
/// # Panics
/// Panics if the length of `values` is greater than [u32::MAX].
pub fn interpolate_poly<B, E>(values: &mut [E], inv_twiddles: &[B])
where
B: StarkField,
E: FieldElement<BaseField = B>,
{
assert!(values.len() <= u32::MAX as usize, "too many values");
split_radix_fft(values, inv_twiddles);
let inv_length = E::inv((values.len() as u32).into());
values.par_iter_mut().for_each(|e| *e *= inv_length);
permute(values);
}
/// Uses FFT algorithm to interpolate a polynomial from provided `values` over the domain defined
/// by `inv_twiddles` and offset by `domain_offset` factor.
///
///
/// # Panics
/// Panics if the length of `values` is greater than [u32::MAX].
pub fn interpolate_poly_with_offset<B, E>(values: &mut [E], inv_twiddles: &[B], domain_offset: B)
where
B: StarkField,
E: FieldElement<BaseField = B>,
{
assert!(values.len() <= u32::MAX as usize, "too many values");
split_radix_fft(values, inv_twiddles);
permute(values);
let domain_offset = E::inv(domain_offset.into());
let inv_len = E::inv((values.len() as u32).into());
let batch_size = values.len() / rayon::current_num_threads().next_power_of_two();
values.par_chunks_mut(batch_size).enumerate().for_each(|(i, batch)| {
let mut offset = domain_offset.exp(((i * batch_size) as u64).into()) * inv_len;
for coeff in batch.iter_mut() {
*coeff *= offset;
offset *= domain_offset;
}
});
}
// PERMUTATIONS
// ================================================================================================
pub fn permute<E: FieldElement>(v: &mut [E]) {
let n = v.len();
let num_batches = rayon::current_num_threads().next_power_of_two();
let batch_size = n / num_batches;
rayon::scope(|s| {
for batch_idx in 0..num_batches {
// create another mutable reference to the slice of values to use in a new thread; this
// is OK because we never write the same positions in the slice from different threads
let values = unsafe { &mut *(&mut v[..] as *mut [E]) };
s.spawn(move |_| {
let batch_start = batch_idx * batch_size;
let batch_end = batch_start + batch_size;
for i in batch_start..batch_end {
let j = super::permute_index(n, i);
if j > i {
values.swap(i, j);
}
}
});
}
});
}
// SPLIT-RADIX FFT
// ================================================================================================
/// In-place recursive FFT with permuted output.
/// Adapted from: https://github.com/0xProject/OpenZKP/tree/master/algebra/primefield/src/fft
pub(super) fn split_radix_fft<B: StarkField, E: FieldElement<BaseField = B>>(
values: &mut [E],
twiddles: &[B],
) {
// generator of the domain should be in the middle of twiddles
let n = values.len();
let g = twiddles[twiddles.len() / 2];
debug_assert_eq!(g.exp((n as u32).into()), E::BaseField::ONE);
let inner_len = 1_usize << (n.ilog2() / 2);
let outer_len = n / inner_len;
let stretch = outer_len / inner_len;
debug_assert!(outer_len == inner_len || outer_len == 2 * inner_len);
debug_assert_eq!(outer_len * inner_len, n);
// transpose inner x inner x stretch square matrix
transpose_square_stretch(values, inner_len, stretch);
// apply inner FFTs
values
.par_chunks_mut(outer_len)
.for_each(|row| row.fft_in_place_raw(twiddles, stretch, stretch, 0));
// transpose inner x inner x stretch square matrix
transpose_square_stretch(values, inner_len, stretch);
// apply outer FFTs
values.par_chunks_mut(outer_len).enumerate().for_each(|(i, row)| {
if i > 0 {
let i = super::permute_index(inner_len, i);
let inner_twiddle = g.exp((i as u32).into());
let mut outer_twiddle = inner_twiddle;
for element in row.iter_mut().skip(1) {
*element = (*element).mul_base(outer_twiddle);
outer_twiddle *= inner_twiddle;
}
}
row.fft_in_place(twiddles);
});
}
// TRANSPOSING
// ================================================================================================
fn transpose_square_stretch<T>(matrix: &mut [T], size: usize, stretch: usize) {
assert_eq!(matrix.len(), size * size * stretch);
match stretch {
1 => transpose_square_1(matrix, size),
2 => transpose_square_2(matrix, size),
_ => unimplemented!("only stretch sizes 1 and 2 are supported"),
}
}
fn transpose_square_1<T>(matrix: &mut [T], size: usize) {
debug_assert_eq!(matrix.len(), size * size);
if size % 2 != 0 {
unimplemented!("odd sizes are not supported");
}
// iterate over upper-left triangle, working in 2x2 blocks
for row in (0..size).step_by(2) {
let i = row * size + row;
matrix.swap(i + 1, i + size);
for col in (row..size).step_by(2).skip(1) {
let i = row * size + col;
let j = col * size + row;
matrix.swap(i, j);
matrix.swap(i + 1, j + size);
matrix.swap(i + size, j + 1);
matrix.swap(i + size + 1, j + size + 1);
}
}
}
fn transpose_square_2<T>(matrix: &mut [T], size: usize) {
debug_assert_eq!(matrix.len(), 2 * size * size);
// iterate over upper-left triangle, working in 1x2 blocks
for row in 0..size {
for col in (row..size).skip(1) {
let i = (row * size + col) * 2;
let j = (col * size + row) * 2;
matrix.swap(i, j);
matrix.swap(i + 1, j + 1);
}
}
}
// HELPER FUNCTIONS
// ================================================================================================
fn clone_and_shift<E: FieldElement>(source: &[E], destination: &mut [E], offset: E::BaseField) {
let batch_size = source.len() / rayon::current_num_threads().next_power_of_two();
source
.par_chunks(batch_size)
.zip(destination.par_chunks_mut(batch_size))
.enumerate()
.for_each(|(i, (source, destination))| {
let mut factor = offset.exp(((i * batch_size) as u64).into());
for (s, d) in source.iter().zip(destination.iter_mut()) {
*d = (*s).mul_base(factor);
factor *= offset;
}
});
}