Skip to content

Commit 93e30d1

Browse files
author
Kunming Jiang
committed
Avoid memory copy in z_mat
1 parent 6edc0b0 commit 93e30d1

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

spartan_parallel/src/custom_dense_mlpoly.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn rev_bits(q: usize, max_num_proofs: usize) -> usize {
3939
}
4040

4141
impl<S: SpartanExtensionField> DensePolynomialPqx<S> {
42-
// Assume z_mat is of form (p, q_rev, x), construct DensePoly
42+
// Assume z_mat is of form (p, q_rev, x_rev), construct DensePoly
4343
pub fn new(
4444
z_mat: Vec<Vec<Vec<Vec<S>>>>,
4545
num_proofs: Vec<usize>,

spartan_parallel/src/r1csinstance.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
251251

252252
Az[p] = (0..num_proofs[p])
253253
.into_par_iter()
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-
254+
.map(|q| {
259255
vec![self.A_list[p_inst].multiply_vec_disjoint_rounds(
260256
max_num_cons,
261257
num_cons[p_inst].clone(),
@@ -267,11 +263,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
267263
.collect();
268264
Bz[p] = (0..num_proofs[p])
269265
.into_par_iter()
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-
266+
.map(|q| {
275267
vec![self.B_list[p_inst].multiply_vec_disjoint_rounds(
276268
max_num_cons,
277269
num_cons[p_inst].clone(),
@@ -283,11 +275,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSInstance<S> {
283275
.collect();
284276
Cz[p] = (0..num_proofs[p])
285277
.into_par_iter()
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-
278+
.map(|q| {
291279
vec![self.C_list[p_inst].multiply_vec_disjoint_rounds(
292280
max_num_cons,
293281
num_cons[p_inst].clone(),

spartan_parallel/src/r1csproof.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::r1csinstance::R1CSInstance;
77
use super::sumcheck::SumcheckInstanceProof;
88
use super::timer::Timer;
99
use super::transcript::ProofTranscript;
10+
use crate::custom_dense_mlpoly::rev_bits;
1011
use crate::scalar::SpartanExtensionField;
1112
use crate::{ProverWitnessSecInfo, VerifierWitnessSecInfo};
1213
use merlin::Transcript;
@@ -178,22 +179,28 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
178179

179180
// append input to variables to create a single vector z
180181
let timer_tmp = Timer::new("prove_z_mat_gen");
181-
let mut z_mat: Vec<Vec<Vec<Vec<S>>>> = Vec::new();
182-
for p in 0..num_instances {
183-
z_mat.push(Vec::new());
184-
for q in 0..num_proofs[p] {
185-
z_mat[p].push(vec![vec![S::field_zero(); num_inputs[p]]; num_witness_secs]);
186-
for w in 0..witness_secs.len() {
187-
let ws = witness_secs[w];
188-
let p_w = if ws.w_mat.len() == 1 { 0 } else { p };
189-
let q_w = if ws.w_mat[p_w].len() == 1 { 0 } else { q };
190-
// Only append the first num_inputs_entries of w_mat[p][q]
191-
for i in 0..min(ws.num_inputs[p_w], num_inputs[p]) {
192-
z_mat[p][q][w][i] = ws.w_mat[p_w][q_w][i];
182+
let z_mat_rev = {
183+
let mut z_mat: Vec<Vec<Vec<Vec<S>>>> = Vec::new();
184+
for p in 0..num_instances {
185+
z_mat.push(vec![vec![vec![S::field_zero(); num_inputs[p]]; num_witness_secs]; num_proofs[p]]);
186+
let q_step = max_num_proofs / num_proofs[p];
187+
for q in 0..num_proofs[p] {
188+
let q_rev = rev_bits(q, max_num_proofs) / q_step;
189+
for w in 0..witness_secs.len() {
190+
let ws = witness_secs[w];
191+
let p_w = if ws.w_mat.len() == 1 { 0 } else { p };
192+
let q_w = if ws.w_mat[p_w].len() == 1 { 0 } else { q };
193+
let y_step = max_num_inputs / num_inputs[p];
194+
// Only append the first num_inputs_entries of w_mat[p][q]
195+
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];
198+
}
193199
}
194200
}
195201
}
196-
}
202+
z_mat
203+
};
197204
timer_tmp.stop();
198205

199206
// derive the verifier's challenge \tau
@@ -221,7 +228,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
221228
max_num_inputs,
222229
num_cons,
223230
block_num_cons.clone(),
224-
&z_mat,
231+
&z_mat_rev,
225232
);
226233
timer_tmp.stop();
227234

@@ -252,7 +259,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
252259
timer_tmp.stop();
253260
timer_sc_proof_phase1.stop();
254261

255-
let (tau_claim, Az_claim, Bz_claim, Cz_claim) = (
262+
let (_tau_claim, Az_claim, Bz_claim, Cz_claim) = (
256263
&(poly_tau_p[0] * poly_tau_q[0] * poly_tau_x[0]),
257264
&poly_Az.index(0, 0, 0, 0),
258265
&poly_Bz.index(0, 0, 0, 0),
@@ -320,8 +327,8 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
320327

321328
let timer_tmp = Timer::new("prove_z_gen");
322329
// Construct a p * q * len(z) matrix Z and bound it to r_q
323-
let mut Z_poly = DensePolynomialPqx::new_rev(
324-
&z_mat,
330+
let mut Z_poly = DensePolynomialPqx::new(
331+
z_mat_rev,
325332
num_proofs.clone(),
326333
max_num_proofs,
327334
num_inputs.clone(),
@@ -586,7 +593,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
586593
S::append_field_to_transcript(b"Cz_claim", transcript, Cz_claim);
587594

588595
// debug_zk
589-
// assert_eq!(taus_bound_rx * (Az_claim * Bz_claim - Cz_claim), claim_post_phase_1);
596+
assert_eq!(taus_bound_rx * (Az_claim * Bz_claim - Cz_claim), claim_post_phase_1);
590597

591598
// derive three public challenges and then derive a joint claim
592599
let r_A: S = transcript.challenge_scalar(b"challenge_Az");

spartan_parallel/src/sparse_mlpoly.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<S: SpartanExtensionField> SparseMatPolynomial<S> {
408408
max_num_rows: usize,
409409
num_rows: usize,
410410
max_num_cols: usize,
411-
_num_cols: usize,
411+
num_cols: usize,
412412
z: &Vec<Vec<S>>
413413
) -> Vec<S> {
414414
let step_r = max_num_rows / num_rows;
@@ -417,7 +417,12 @@ impl<S: SpartanExtensionField> SparseMatPolynomial<S> {
417417
let row = self.M[i].row;
418418
let col = self.M[i].col;
419419
let val = self.M[i].val.clone();
420-
(row, val * z[col / max_num_cols][col % max_num_cols])
420+
let w = col / max_num_cols;
421+
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])
421426
})
422427
.fold(vec![S::field_zero(); num_rows], |mut Mz, (r, v)| {
423428
// Reverse the bits of r. r_rev is a multiple of step_r

0 commit comments

Comments
 (0)