From 190242a4e9241126cad8b4b5dd06434a31725e29 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Sun, 6 Sep 2020 14:18:05 -0600 Subject: [PATCH] Remove redundant permutation_queries vector. --- src/plonk/circuit.rs | 27 ++++++++++++--------------- src/plonk/prover.rs | 10 +++++----- src/plonk/verifier.rs | 10 +++++----- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index eab4517..89a5f57 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -163,13 +163,13 @@ pub struct MetaCircuit { pub(crate) rotations: BTreeMap, // Vector of permutation arguments, where each corresponds to a set of wires - // that are involved in a permutation argument. As an example, we could have - // a permutation argument between wires (A, B, C) which allows copy - // constraints to be enforced between advice wire values in A, B and C, and - // another permutation between wires (B, C, D) which allows the same with D - // instead of A. - pub(crate) permutations: Vec>, - pub(crate) permutation_queries: Vec>, + // that are involved in a permutation argument, as well as the corresponding + // query index for each wire. As an example, we could have a permutation + // argument between wires (A, B, C) which allows copy constraints to be + // enforced between advice wire values in A, B and C, and another + // permutation between wires (B, C, D) which allows the same with D instead + // of A. + pub(crate) permutations: Vec>, } impl Default for MetaCircuit { @@ -185,7 +185,6 @@ impl Default for MetaCircuit { advice_queries: Vec::new(), rotations, permutations: Vec::new(), - permutation_queries: Vec::new(), } } } @@ -199,13 +198,11 @@ impl MetaCircuit { let len = self.rotations.len(); self.rotations.entry(at).or_insert(PointIndex(len)); } - self.permutations.push(wires.to_vec()); - - let mut queries = vec![]; - for wire in wires { - queries.push(self.query_advice_index(*wire, 0)); - } - self.permutation_queries.push(queries); + let wires = wires + .iter() + .map(|&wire| (wire, self.query_advice_index(wire, 0))) + .collect(); + self.permutations.push(wires); index } diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index acc1a5b..89ea5ee 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -142,7 +142,7 @@ impl Proof { let mut modified_advice = Vec::with_capacity(wires.len()); // Iterate over each wire of the permutation - for (wire, permuted_wire_values) in wires.iter().zip(permuted_values.iter()) { + for (&(wire, _), permuted_wire_values) in wires.iter().zip(permuted_values.iter()) { // Grab the advice wire's values from the witness let mut tmp_advice_values = witness.advice[wire.0].clone(); @@ -179,7 +179,7 @@ impl Proof { // Iterate over each wire again, this time finishing the computation // of the entire fraction by computing the numerators let mut deltaomega = C::Scalar::one(); - for (wire, modified_advice) in wires.iter().zip(modified_advice.iter_mut()) { + for (&(wire, _), modified_advice) in wires.iter().zip(modified_advice.iter_mut()) { // For each row i, we compute // p_j(\omega^i) + \delta^j \omega^i \beta + \gamma // for the jth wire of the permutation @@ -319,7 +319,7 @@ impl Proof { } // z(X) \prod (p(X) + \beta s_i(X) + \gamma) - z(omega^{-1} X) \prod (p(X) + \delta^i \beta X + \gamma) - for (permutation_index, wires) in srs.meta.permutation_queries.iter().enumerate() { + for (permutation_index, wires) in srs.meta.permutations.iter().enumerate() { parallelize(&mut h_poly, |a, _| { for a in a.iter_mut() { *a *= &x_2; @@ -329,7 +329,7 @@ impl Proof { let mut left = permutation_product_cosets[permutation_index].clone(); for (advice, permutation) in wires .iter() - .map(|&wire| &advice_cosets[wire]) + .map(|&(_, index)| &advice_cosets[index]) .zip(srs.permutation_cosets[permutation_index].iter()) { parallelize(&mut left, |left, start| { @@ -346,7 +346,7 @@ impl Proof { let mut right = permutation_product_cosets_inv[permutation_index].clone(); let mut current_delta = x_0 * &C::Scalar::ZETA; let step = domain.get_extended_omega(); - for advice in wires.iter().map(|&wire| &advice_cosets[wire]) { + for advice in wires.iter().map(|&(_, index)| &advice_cosets[index]) { parallelize(&mut right, move |right, start| { let mut beta_term = current_delta * &step.pow_vartime(&[start as u64, 0, 0, 0]); for (right, advice) in right.iter_mut().zip(advice[start..].iter()) { diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index 3a03393..95743c0 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -98,13 +98,13 @@ impl Proof { } // z(X) \prod (p(X) + \beta s_i(X) + \gamma) - z(omega^{-1} X) \prod (p(X) + \delta^i \beta X + \gamma) - for (permutation_index, queries) in srs.meta.permutation_queries.iter().enumerate() { + for (permutation_index, wires) in srs.meta.permutations.iter().enumerate() { h_eval *= &x_2; let mut left = self.permutation_product_evals[permutation_index]; - for (advice_eval, permutation_eval) in queries + for (advice_eval, permutation_eval) in wires .iter() - .map(|&query_index| self.advice_evals[query_index]) + .map(|&(_, query_index)| self.advice_evals[query_index]) .zip(self.permutation_evals[permutation_index].iter()) { left *= &(advice_eval + &(x_0 * permutation_eval) + &x_1); @@ -112,9 +112,9 @@ impl Proof { let mut right = self.permutation_product_inv_evals[permutation_index]; let mut current_delta = x_0 * &x_3; - for advice_eval in queries + for advice_eval in wires .iter() - .map(|&query_index| self.advice_evals[query_index]) + .map(|&(_, query_index)| self.advice_evals[query_index]) { right *= &(advice_eval + ¤t_delta + &x_1); current_delta *= &C::Scalar::DELTA;