Skip to content

Commit 6edc0b0

Browse files
author
Kunming Jiang
committed
Remove array copy for vector multiplication
1 parent abe0522 commit 6edc0b0

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

spartan_parallel/src/r1csinstance.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rayon::prelude::*;
33
use std::cmp::{max, min};
44
use std::collections::HashMap;
55

6+
use crate::custom_dense_mlpoly::rev_bits;
67
use crate::scalar::SpartanExtensionField;
78
use crate::transcript::AppendToTranscript;
89

@@ -250,8 +251,13 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
250251

251252
Az[p] = (0..num_proofs[p])
252253
.into_par_iter()
253-
.map(|q| {
254+
.map(|q_rev| {
255+
// Reverse the bits of q
256+
let q_step = max_num_proofs / num_proofs[p];
257+
let q = rev_bits(q_rev * q_step, max_num_proofs);
258+
254259
vec![self.A_list[p_inst].multiply_vec_disjoint_rounds(
260+
max_num_cons,
255261
num_cons[p_inst].clone(),
256262
max_num_inputs,
257263
num_inputs[p],
@@ -261,8 +267,13 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
261267
.collect();
262268
Bz[p] = (0..num_proofs[p])
263269
.into_par_iter()
264-
.map(|q| {
270+
.map(|q_rev| {
271+
// Reverse the bits of q
272+
let q_step = max_num_proofs / num_proofs[p];
273+
let q = rev_bits(q_rev * q_step, max_num_proofs);
274+
265275
vec![self.B_list[p_inst].multiply_vec_disjoint_rounds(
276+
max_num_cons,
266277
num_cons[p_inst].clone(),
267278
max_num_inputs,
268279
num_inputs[p],
@@ -272,8 +283,13 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
272283
.collect();
273284
Cz[p] = (0..num_proofs[p])
274285
.into_par_iter()
275-
.map(|q| {
286+
.map(|q_rev| {
287+
// Reverse the bits of q
288+
let q_step = max_num_proofs / num_proofs[p];
289+
let q = rev_bits(q_rev * q_step, max_num_proofs);
290+
276291
vec![self.C_list[p_inst].multiply_vec_disjoint_rounds(
292+
max_num_cons,
277293
num_cons[p_inst].clone(),
278294
max_num_inputs,
279295
num_inputs[p],
@@ -284,22 +300,22 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
284300
}
285301

286302
(
287-
DensePolynomialPqx::new_rev(
288-
&Az,
303+
DensePolynomialPqx::new(
304+
Az,
289305
num_proofs.clone(),
290306
max_num_proofs,
291307
num_cons.clone(),
292308
max_num_cons,
293309
),
294-
DensePolynomialPqx::new_rev(
295-
&Bz,
310+
DensePolynomialPqx::new(
311+
Bz,
296312
num_proofs.clone(),
297313
max_num_proofs,
298314
num_cons.clone(),
299315
max_num_cons,
300316
),
301-
DensePolynomialPqx::new_rev(
302-
&Cz,
317+
DensePolynomialPqx::new(
318+
Cz,
303319
num_proofs,
304320
max_num_proofs,
305321
num_cons.clone(),

spartan_parallel/src/r1csproof.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::dense_mlpoly::{DensePolynomial, EqPolynomial};
44
use super::errors::ProofVerifyError;
55
use super::math::Math;
66
use super::r1csinstance::R1CSInstance;
7-
use super::random::RandomTape;
87
use super::sumcheck::SumcheckInstanceProof;
98
use super::timer::Timer;
109
use super::transcript::ProofTranscript;

spartan_parallel/src/sparse_mlpoly.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::type_complexity)]
22
#![allow(clippy::too_many_arguments)]
33
#![allow(clippy::needless_range_loop)]
4+
use crate::custom_dense_mlpoly::rev_bits;
45
use crate::scalar::SpartanExtensionField;
56

67
use super::dense_mlpoly::DensePolynomial;
@@ -403,21 +404,27 @@ impl<S: SpartanExtensionField> SparseMatPolynomial<S> {
403404
// Z is consisted of vector segments
404405
// Z[i] contains entries i * max_num_cols ~ i * max_num_cols + num_cols
405406
pub fn multiply_vec_disjoint_rounds(
406-
&self,
407-
num_rows: usize,
408-
max_num_cols: usize,
409-
_num_cols: usize,
410-
z: &Vec<Vec<S>>,
407+
&self,
408+
max_num_rows: usize,
409+
num_rows: usize,
410+
max_num_cols: usize,
411+
_num_cols: usize,
412+
z: &Vec<Vec<S>>
411413
) -> Vec<S> {
414+
let step_r = max_num_rows / num_rows;
412415
(0..self.M.len())
413416
.map(|i| {
414417
let row = self.M[i].row;
415418
let col = self.M[i].col;
416-
let val = &self.M[i].val;
417-
(row, *val * z[col / max_num_cols][col % max_num_cols])
419+
let val = self.M[i].val.clone();
420+
(row, val * z[col / max_num_cols][col % max_num_cols])
418421
})
419422
.fold(vec![S::field_zero(); num_rows], |mut Mz, (r, v)| {
420-
Mz[r] = Mz[r] + v;
423+
// Reverse the bits of r. r_rev is a multiple of step_r
424+
let r_rev = rev_bits(r, max_num_rows);
425+
// Now r_rev is between 0 to num_inputs[p]
426+
let r_rev = r_rev / step_r;
427+
Mz[r_rev] += v;
421428
Mz
422429
})
423430
}

0 commit comments

Comments
 (0)