Skip to content

Commit 3d8f012

Browse files
committed
Add parallelism to z_mat_gen
1 parent 0da6d07 commit 3d8f012

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

spartan_parallel/src/r1csproof.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ use crate::scalar::SpartanExtensionField;
1111
use crate::{ProverWitnessSecInfo, VerifierWitnessSecInfo};
1212
use merlin::Transcript;
1313
use serde::{Deserialize, Serialize};
14-
use std::cmp::min;
14+
use std::cmp::max;
1515
use std::iter::zip;
16+
use rayon::prelude::*;
17+
use std::sync::{Arc, Mutex};
1618

1719
#[derive(Serialize, Deserialize, Debug)]
1820
pub struct R1CSProof<S: SpartanExtensionField> {
@@ -181,21 +183,36 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
181183

182184
// append input to variables to create a single vector z
183185
let timer_tmp = Timer::new("prove_z_mat_gen");
184-
let mut z_mat: Vec<Vec<Vec<Vec<S>>>> = Vec::new();
185-
for p in 0..num_instances {
186-
z_mat.push(vec![vec![vec![ZERO; num_inputs[p]]; num_witness_secs]; num_proofs[p]]);
187-
for q in 0..num_proofs[p] {
188-
for w in 0..witness_secs.len() {
189-
let ws = witness_secs[w];
190-
let p_w = if ws.w_mat.len() == 1 { 0 } else { p };
191-
let q_w = if ws.w_mat[p_w].len() == 1 { 0 } else { q };
192-
// Only append the first num_inputs_entries of w_mat[p][q]
193-
for i in 0..min(ws.num_inputs[p_w], num_inputs[p]) {
194-
z_mat[p][q][w][i] = ws.w_mat[p_w][q_w][i];
195-
}
196-
}
197-
}
198-
}
186+
187+
let z_mat = (0..num_instances)
188+
.into_par_iter()
189+
.map(|p| {
190+
(0..num_proofs[p])
191+
.into_par_iter()
192+
.map(|q| {
193+
(0..witness_secs.len())
194+
.into_par_iter()
195+
.map(|w| {
196+
let ws = witness_secs[w];
197+
let p_w = if ws.w_mat.len() == 1 { 0 } else { p };
198+
let q_w = if ws.w_mat[p_w].len() == 1 { 0 } else { q };
199+
200+
let r_w = if ws.num_inputs[p_w] < num_inputs[p] {
201+
let padding = std::iter::repeat(S::field_zero()).take(num_inputs[p] - ws.num_inputs[p_w]).collect::<Vec<S>>();
202+
let mut r = ws.w_mat[p_w][q_w].clone();
203+
r.extend(padding);
204+
r
205+
} else {
206+
ws.w_mat[p_w][q_w].iter().take(num_inputs[p]).cloned().collect::<Vec<S>>()
207+
};
208+
209+
r_w
210+
})
211+
.collect::<Vec<Vec<S>>>()
212+
})
213+
.collect::<Vec<Vec<Vec<S>>>>()
214+
})
215+
.collect::<Vec<Vec<Vec<Vec<S>>>>>();
199216
timer_tmp.stop();
200217

201218
// derive the verifier's challenge \tau
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const u32 REPETITION = 10000
1+
const u32 REPETITION = 1000

0 commit comments

Comments
 (0)