Skip to content

Commit 611efe1

Browse files
committed
Parallelize bound_poly_vars_rq
1 parent 3d8f012 commit 611efe1

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

spartan_parallel/src/custom_dense_mlpoly.rs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::cmp::min;
33

44
use crate::dense_mlpoly::DensePolynomial;
55
use crate::scalar::SpartanExtensionField;
6+
use rayon::prelude::*;
67

78
const MODE_P: usize = 1;
89
const MODE_Q: usize = 2;
@@ -128,27 +129,27 @@ impl<S: SpartanExtensionField> DensePolynomialPqx<S> {
128129

129130
// Bound the last variable of "q" section to r
130131
pub fn bound_poly_q(&mut self, r: &S) {
131-
let ONE = S::field_one();
132-
self.max_num_proofs /= 2;
132+
let ONE = S::field_one();
133+
self.max_num_proofs /= 2;
133134

134-
for p in 0..min(self.num_instances, self.Z.len()) {
135-
if self.num_proofs[p] == 1 {
136-
for w in 0..min(self.num_witness_secs, self.Z[p][0].len()) {
137-
for x in 0..self.num_inputs[p] {
138-
self.Z[p][0][w][x] *= ONE - r.clone();
139-
}
135+
for p in 0..min(self.num_instances, self.Z.len()) {
136+
if self.num_proofs[p] == 1 {
137+
for w in 0..min(self.num_witness_secs, self.Z[p][0].len()) {
138+
for x in 0..self.num_inputs[p] {
139+
self.Z[p][0][w][x] *= ONE - r.clone();
140140
}
141-
} else {
142-
self.num_proofs[p] /= 2;
143-
for q in 0..self.num_proofs[p] {
144-
for w in 0..min(self.num_witness_secs, self.Z[p][q].len()) {
145-
for x in 0..self.num_inputs[p] {
146-
self.Z[p][q][w][x] = self.Z[p][2 * q][w][x] + r.clone() * (self.Z[p][2 * q + 1][w][x] - self.Z[p][2 * q][w][x]);
147-
}
141+
}
142+
} else {
143+
self.num_proofs[p] /= 2;
144+
for q in 0..self.num_proofs[p] {
145+
for w in 0..min(self.num_witness_secs, self.Z[p][q].len()) {
146+
for x in 0..self.num_inputs[p] {
147+
self.Z[p][q][w][x] = self.Z[p][2 * q][w][x] + r.clone() * (self.Z[p][2 * q + 1][w][x] - self.Z[p][2 * q][w][x]);
148148
}
149149
}
150150
}
151151
}
152+
}
152153
}
153154

154155
// Bound the last variable of "w" section to r
@@ -208,9 +209,47 @@ impl<S: SpartanExtensionField> DensePolynomialPqx<S> {
208209
pub fn bound_poly_vars_rq(&mut self,
209210
r_q: &[S],
210211
) {
211-
for r in r_q {
212-
self.bound_poly_q(r);
213-
}
212+
let ONE = S::field_one();
213+
let num_instances = min(self.num_instances, self.Z.len());
214+
215+
self.Z = (0..num_instances)
216+
.into_par_iter()
217+
.map(|p| {
218+
let num_proofs = self.num_proofs[p];
219+
let num_witness_secs = min(self.num_witness_secs, self.Z[p][0].len());
220+
let num_inputs = self.num_inputs[p];
221+
222+
let wit = (0..num_witness_secs).into_par_iter().map(|w| {
223+
(0..num_inputs).into_par_iter().map(|x| {
224+
let mut np = num_proofs;
225+
let mut x_fold = (0..num_proofs).map(|q| self.Z[p][q][w][x]).collect::<Vec<S>>();
226+
for r in r_q {
227+
if np == 1 {
228+
x_fold[0] *= ONE - *r;
229+
} else {
230+
np /= 2;
231+
for q in 0..np {
232+
x_fold[q] = x_fold[2 * q] + *r * (x_fold[2 * q + 1] - x_fold[2 * q]);
233+
}
234+
}
235+
}
236+
237+
x_fold
238+
}).collect::<Vec<Vec<S>>>()
239+
}).collect::<Vec<Vec<Vec<S>>>>();
240+
241+
(0..num_proofs)
242+
.into_par_iter()
243+
.map(|q| {
244+
(0..wit.len()).map(|w| {
245+
(0..wit[w].len()).map(|x| {
246+
wit[w][x][q]
247+
}).collect::<Vec<S>>()
248+
}).collect::<Vec<Vec<S>>>()
249+
}).collect::<Vec<Vec<Vec<S>>>>()
250+
}).collect::<Vec<Vec<Vec<Vec<S>>>>>();
251+
252+
self.max_num_proofs /= 2usize.pow(r_q.len() as u32);
214253
}
215254

216255
// Bound the entire "w" section to r_w in reverse

0 commit comments

Comments
 (0)