Skip to content

Commit 5f17d48

Browse files
author
Kunming Jiang
committed
Add lookups for x_rev and q_rev
1 parent 93e30d1 commit 5f17d48

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

spartan_parallel/src/r1csinstance.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ use super::sparse_mlpoly::{
1919
use super::timer::Timer;
2020
use flate2::{write::ZlibEncoder, Compression};
2121
use merlin::Transcript;
22-
use rayon::prelude::*;
2322
use serde::{Deserialize, Serialize};
2423
use std::iter::zip;
25-
use std::sync::{Arc, Mutex};
2624

2725
#[derive(Debug, Serialize, Deserialize, Clone)]
2826
pub struct R1CSInstance<S: SpartanExtensionField> {
@@ -249,39 +247,52 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
249247
Bz.push(Vec::new());
250248
Cz.push(Vec::new());
251249

250+
// Map x and y to x_rev and y_rev so we don't have to do it everytime
251+
let x_step = max_num_cons / num_cons[p];
252+
let x_rev_map = (0..num_cons[p]).map(|x|
253+
rev_bits(x, max_num_cons) / x_step
254+
).collect();
255+
let y_step = max_num_inputs / num_inputs[p];
256+
let y_rev_map = (0..num_inputs[p]).map(|y|
257+
rev_bits(y, max_num_inputs) / y_step
258+
).collect();
259+
252260
Az[p] = (0..num_proofs[p])
253261
.into_par_iter()
254262
.map(|q| {
255263
vec![self.A_list[p_inst].multiply_vec_disjoint_rounds(
256-
max_num_cons,
257264
num_cons[p_inst].clone(),
258265
max_num_inputs,
259266
num_inputs[p],
260267
&z_list[q],
268+
&x_rev_map,
269+
&y_rev_map,
261270
)]
262271
})
263272
.collect();
264273
Bz[p] = (0..num_proofs[p])
265274
.into_par_iter()
266275
.map(|q| {
267276
vec![self.B_list[p_inst].multiply_vec_disjoint_rounds(
268-
max_num_cons,
269277
num_cons[p_inst].clone(),
270278
max_num_inputs,
271279
num_inputs[p],
272280
&z_list[q],
281+
&x_rev_map,
282+
&y_rev_map,
273283
)]
274284
})
275285
.collect();
276286
Cz[p] = (0..num_proofs[p])
277287
.into_par_iter()
278288
.map(|q| {
279289
vec![self.C_list[p_inst].multiply_vec_disjoint_rounds(
280-
max_num_cons,
281290
num_cons[p_inst].clone(),
282291
max_num_inputs,
283292
num_inputs[p],
284293
&z_list[q],
294+
&x_rev_map,
295+
&y_rev_map,
285296
)]
286297
})
287298
.collect();

spartan_parallel/src/r1csproof.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,20 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
184184
for p in 0..num_instances {
185185
z_mat.push(vec![vec![vec![S::field_zero(); num_inputs[p]]; num_witness_secs]; num_proofs[p]]);
186186
let q_step = max_num_proofs / num_proofs[p];
187+
188+
let y_step = max_num_inputs / num_inputs[p];
189+
let y_rev_map: Vec<usize> = (0..num_inputs[p]).map(|y|
190+
rev_bits(y, max_num_inputs) / y_step
191+
).collect();
187192
for q in 0..num_proofs[p] {
188193
let q_rev = rev_bits(q, max_num_proofs) / q_step;
189194
for w in 0..witness_secs.len() {
190195
let ws = witness_secs[w];
191196
let p_w = if ws.w_mat.len() == 1 { 0 } else { p };
192197
let q_w = if ws.w_mat[p_w].len() == 1 { 0 } else { q };
193-
let y_step = max_num_inputs / num_inputs[p];
194198
// Only append the first num_inputs_entries of w_mat[p][q]
195199
for i in 0..min(ws.num_inputs[p_w], num_inputs[p]) {
196-
let y_rev = rev_bits(i, max_num_inputs) / y_step;
197-
z_mat[p][q_rev][w][y_rev] = ws.w_mat[p_w][q_w][i];
200+
z_mat[p][q_rev][w][y_rev_map[i]] = ws.w_mat[p_w][q_w][i];
198201
}
199202
}
200203
}

spartan_parallel/src/sparse_mlpoly.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -405,31 +405,24 @@ impl<S: SpartanExtensionField> SparseMatPolynomial<S> {
405405
// Z[i] contains entries i * max_num_cols ~ i * max_num_cols + num_cols
406406
pub fn multiply_vec_disjoint_rounds(
407407
&self,
408-
max_num_rows: usize,
409408
num_rows: usize,
410409
max_num_cols: usize,
411-
num_cols: usize,
412-
z: &Vec<Vec<S>>
410+
_num_cols: usize,
411+
z: &Vec<Vec<S>>,
412+
x_rev_map: &Vec<usize>,
413+
y_rev_map: &Vec<usize>,
413414
) -> Vec<S> {
414-
let step_r = max_num_rows / num_rows;
415415
(0..self.M.len())
416416
.map(|i| {
417417
let row = self.M[i].row;
418418
let col = self.M[i].col;
419419
let val = self.M[i].val.clone();
420420
let w = col / max_num_cols;
421421
let y = col % max_num_cols;
422-
// Z expresses y in reverse bits order, so have to find the correct y
423-
let y_step = max_num_cols / num_cols;
424-
let y_rev = rev_bits(y, max_num_cols) / y_step;
425-
(row, val * z[w][y_rev])
422+
(row, val * z[w][y_rev_map[y]])
426423
})
427424
.fold(vec![S::field_zero(); num_rows], |mut Mz, (r, v)| {
428-
// Reverse the bits of r. r_rev is a multiple of step_r
429-
let r_rev = rev_bits(r, max_num_rows);
430-
// Now r_rev is between 0 to num_inputs[p]
431-
let r_rev = r_rev / step_r;
432-
Mz[r_rev] += v;
425+
Mz[x_rev_map[r]] += v;
433426
Mz
434427
})
435428
}

0 commit comments

Comments
 (0)