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
use std::{borrow::Borrow, collections::BTreeMap};
use crate::fft::domain::{FFTPrecomputation, IFFTPrecomputation};
use super::*;
use snarkvm_utilities::{cfg_iter, cfg_iter_mut, ExecutionPool};
#[derive(Default)]
pub struct PolyMultiplier<'a, F: PrimeField> {
polynomials: Vec<(String, Cow<'a, DensePolynomial<F>>)>,
evaluations: Vec<(String, Cow<'a, crate::fft::Evaluations<F>>)>,
fft_precomputation: Option<Cow<'a, FFTPrecomputation<F>>>,
ifft_precomputation: Option<Cow<'a, IFFTPrecomputation<F>>>,
}
impl<'a, F: PrimeField> PolyMultiplier<'a, F> {
#[inline]
pub fn new() -> Self {
Self { polynomials: Vec::new(), evaluations: Vec::new(), fft_precomputation: None, ifft_precomputation: None }
}
#[inline]
pub fn add_precomputation(&mut self, fft_pc: &'a FFTPrecomputation<F>, ifft_pc: &'a IFFTPrecomputation<F>) {
self.fft_precomputation = Some(Cow::Borrowed(fft_pc));
self.ifft_precomputation = Some(Cow::Borrowed(ifft_pc));
}
#[inline]
pub fn add_polynomial(&mut self, poly: DensePolynomial<F>, label: impl ToString) {
self.polynomials.push((label.to_string(), Cow::Owned(poly)))
}
#[inline]
pub fn add_evaluation(&mut self, evals: Evaluations<F>, label: impl ToString) {
self.evaluations.push((label.to_string(), Cow::Owned(evals)))
}
#[inline]
pub fn add_polynomial_ref(&mut self, poly: &'a DensePolynomial<F>, label: impl ToString) {
self.polynomials.push((label.to_string(), Cow::Borrowed(poly)))
}
#[inline]
pub fn add_evaluation_ref(&mut self, evals: &'a Evaluations<F>, label: impl ToString) {
self.evaluations.push((label.to_string(), Cow::Borrowed(evals)))
}
pub fn multiply(mut self) -> Option<DensePolynomial<F>> {
if self.polynomials.is_empty() && self.evaluations.is_empty() {
Some(DensePolynomial::zero())
} else {
let degree = self.polynomials.iter().map(|(_, p)| p.degree() + 1).sum::<usize>();
let domain = EvaluationDomain::new(degree)?;
if self.evaluations.iter().any(|(_, e)| e.domain() != domain) {
None
} else {
if self.fft_precomputation.is_none() {
self.fft_precomputation = Some(Cow::Owned(domain.precompute_fft()));
}
if self.ifft_precomputation.is_none() {
self.ifft_precomputation =
Some(Cow::Owned(self.fft_precomputation.as_ref().unwrap().to_ifft_precomputation()));
}
let fft_pc = &self.fft_precomputation.unwrap();
let ifft_pc = &self.ifft_precomputation.unwrap();
let mut pool = ExecutionPool::new();
for (_, p) in self.polynomials {
pool.add_job(move || {
let mut p = p.clone().into_owned().coeffs;
p.resize(domain.size(), F::zero());
domain.out_order_fft_in_place_with_pc(&mut p, fft_pc);
p
})
}
for (_, e) in self.evaluations {
pool.add_job(move || {
let mut e = e.clone().into_owned().evaluations;
e.resize(domain.size(), F::zero());
crate::fft::domain::derange(&mut e);
e
})
}
let results = pool.execute_all();
#[cfg(feature = "parallel")]
let mut result = results
.into_par_iter()
.reduce_with(|mut a, b| {
cfg_iter_mut!(a).zip(b).for_each(|(a, b)| *a *= b);
a
})
.unwrap();
#[cfg(not(feature = "parallel"))]
let mut result = results
.into_iter()
.reduce(|mut a, b| {
cfg_iter_mut!(a).zip(b).for_each(|(a, b)| *a *= b);
a
})
.unwrap();
domain.out_order_ifft_in_place_with_pc(&mut result, ifft_pc);
Some(DensePolynomial::from_coefficients_vec(result))
}
}
}
pub fn element_wise_arithmetic_4_over_domain<T: Borrow<str>>(
mut self,
domain: EvaluationDomain<F>,
labels: [T; 4],
f: impl Fn(F, F, F, F) -> F + Sync,
) -> Option<DensePolynomial<F>> {
if self.fft_precomputation.is_none() {
self.fft_precomputation = Some(Cow::Owned(domain.precompute_fft()));
}
if self.ifft_precomputation.is_none() {
self.ifft_precomputation =
Some(Cow::Owned(self.fft_precomputation.as_ref().unwrap().to_ifft_precomputation()));
}
let fft_pc = self.fft_precomputation.as_ref().unwrap();
let mut pool = ExecutionPool::new();
for (l, p) in self.polynomials {
pool.add_job(move || {
let mut p = p.clone().into_owned().coeffs;
p.resize(domain.size(), F::zero());
domain.out_order_fft_in_place_with_pc(&mut p, fft_pc);
(l, p)
})
}
for (l, e) in self.evaluations {
pool.add_job(move || {
let mut e = e.clone().into_owned().evaluations;
e.resize(domain.size(), F::zero());
crate::fft::domain::derange(&mut e);
(l, e)
})
}
let p = pool.execute_all().into_iter().collect::<BTreeMap<_, _>>();
assert_eq!(p.len(), 4);
let mut result = cfg_iter!(p[labels[0].borrow()])
.zip(&p[labels[1].borrow()])
.zip(&p[labels[2].borrow()])
.zip(&p[labels[3].borrow()])
.map(|(((a, b), c), d)| f(*a, *b, *c, *d))
.collect::<Vec<_>>();
drop(p);
domain.out_order_ifft_in_place_with_pc(&mut result, &self.ifft_precomputation.unwrap());
Some(DensePolynomial::from_coefficients_vec(result))
}
}