From a00d7c2fa64278df466b7c3e29f2874e8eae659b Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Tue, 26 Jan 2021 15:23:29 +0800 Subject: [PATCH] Cleanups from code review Co-authored-by: Kris Nuttycombe Co-authored-by: Sean Bowe --- src/plonk/prover.rs | 177 ++++++++++++++++-------------------------- src/plonk/verifier.rs | 159 ++++++++++++++++++------------------- 2 files changed, 140 insertions(+), 196 deletions(-) diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 0514147..31cb15e 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -34,13 +34,13 @@ pub fn create_proof, ConcreteCircuit: Circ let mut meta = ConstraintSystem::default(); let config = ConcreteCircuit::configure(&mut meta); - struct AuxSingle { - pub aux_values: Vec>, + struct AuxSingle<'a, C: CurveAffine> { + pub aux_values: &'a [Polynomial], pub aux_polys: Vec>, pub aux_cosets: Vec>, } - let aux_vec: Result, _> = auxs + let aux_vec: Vec> = auxs .iter() .map(|aux| -> Result, Error> { let aux_commitments_projective: Vec<_> = aux @@ -77,17 +77,12 @@ pub fn create_proof, ConcreteCircuit: Circ .collect(); Ok(AuxSingle { - aux_values: aux.to_vec(), + aux_values: *aux, aux_polys, aux_cosets, }) }) - .collect(); - - let aux_vec = match aux_vec { - Ok(aux_vec) => aux_vec, - Err(err) => return Err(err), - }; + .collect::, _>>()?; struct AdviceSingle { pub advice_values: Vec>, @@ -96,7 +91,7 @@ pub fn create_proof, ConcreteCircuit: Circ pub advice_blinds: Vec>, } - let advice_vec: Result, _> = circuits + let advice_vec: Vec> = circuits .iter() .map(|circuit| -> Result, Error> { struct WitnessCollection { @@ -202,50 +197,38 @@ pub fn create_proof, ConcreteCircuit: Circ advice_blinds, }) }) - .collect(); - - let advice_vec = match advice_vec { - Ok(advice_vec) => advice_vec, - Err(err) => return Err(err), - }; + .collect::, _>>()?; // Sample theta challenge for keeping lookup columns linearly independent let theta = ChallengeTheta::get(transcript); - let lookups_vec: Result>, _> = aux_vec + let lookups_vec: Vec>> = aux_vec .iter() .zip(advice_vec.iter()) - .map( - |(aux, advice)| -> Result>, Error> { - // Construct and commit to permuted values for each lookup - pk.vk - .cs - .lookups - .iter() - .map(|lookup| { - lookup.commit_permuted( - &pk, - ¶ms, - &domain, - theta, - &advice.advice_values, - &pk.fixed_values, - &aux.aux_values, - &advice.advice_cosets, - &pk.fixed_cosets, - &aux.aux_cosets, - transcript, - ) - }) - .collect() - }, - ) - .collect(); - - let lookups_vec = match lookups_vec { - Ok(lookups_vec) => lookups_vec, - Err(err) => return Err(err), - }; + .map(|(aux, advice)| -> Result, Error> { + // Construct and commit to permuted values for each lookup + pk.vk + .cs + .lookups + .iter() + .map(|lookup| { + lookup.commit_permuted( + &pk, + ¶ms, + &domain, + theta, + &advice.advice_values, + &pk.fixed_values, + &aux.aux_values, + &advice.advice_cosets, + &pk.fixed_cosets, + &aux.aux_cosets, + transcript, + ) + }) + .collect() + }) + .collect::, _>>()?; // Sample beta challenge let beta = ChallengeBeta::get(transcript); @@ -253,38 +236,31 @@ pub fn create_proof, ConcreteCircuit: Circ // Sample gamma challenge let gamma = ChallengeGamma::get(transcript); - let permutations_vec: Result>, _> = advice_vec + let permutations_vec: Vec>> = advice_vec .iter() - .map( - |advice| -> Result>, Error> { - // Commit to permutations, if any. - pk.vk - .cs - .permutations - .iter() - .zip(pk.permutations.iter()) - .map(|(p, pkey)| { - p.commit( - params, - pk, - pkey, - &advice.advice_values, - beta, - gamma, - transcript, - ) - }) - .collect() - }, - ) - .collect(); + .map(|advice| -> Result, Error> { + // Commit to permutations, if any. + pk.vk + .cs + .permutations + .iter() + .zip(pk.permutations.iter()) + .map(|(p, pkey)| { + p.commit( + params, + pk, + pkey, + &advice.advice_values, + beta, + gamma, + transcript, + ) + }) + .collect() + }) + .collect::, _>>()?; - let permutations_vec = match permutations_vec { - Ok(permutations_vec) => permutations_vec, - Err(err) => return Err(err), - }; - - let lookups_vec: Result>>, _> = lookups_vec + let lookups_vec: Vec>> = lookups_vec .into_iter() .map(|lookups| -> Result, _> { // Construct and commit to products for each lookup @@ -293,12 +269,7 @@ pub fn create_proof, ConcreteCircuit: Circ .map(|lookup| lookup.commit_product(&pk, ¶ms, theta, beta, gamma, transcript)) .collect::, _>>() }) - .collect(); - - let lookups_vec = match lookups_vec { - Ok(lookups_vec) => lookups_vec, - Err(err) => return Err(err), - }; + .collect::, _>>()?; // Obtain challenge for keeping all separate gates linearly independent let y = ChallengeY::get(transcript); @@ -308,6 +279,7 @@ pub fn create_proof, ConcreteCircuit: Circ .into_iter() .zip(advice_vec.iter()) .map(|(permutations, advice)| { + // Evaluate the h(X) polynomial's constraint system expressions for the permutation constraints, if any. let tmp: Vec<_> = permutations .into_iter() .zip(pk.vk.cs.permutations.iter()) @@ -362,9 +334,7 @@ pub fn create_proof, ConcreteCircuit: Circ // Lookup constraints, if any. .chain(lookup_expressions.into_iter().flatten()) }, - ) - .collect::>() - .into_iter(); + ); // Construct the vanishing argument let vanishing = vanishing::Argument::construct(params, domain, expressions, y, transcript)?; @@ -431,7 +401,7 @@ pub fn create_proof, ConcreteCircuit: Circ let vanishing = vanishing.evaluate(x, transcript)?; // Evaluate the permutations, if any, at omega^i x. - let permutations_vec: Result>>, _> = permutations_vec + let permutations_vec: Vec>> = permutations_vec .into_iter() .map(|permutations| -> Result, _> { permutations @@ -440,15 +410,10 @@ pub fn create_proof, ConcreteCircuit: Circ .map(|(p, pkey)| p.evaluate(pk, pkey, x, transcript)) .collect::, _>>() }) - .collect(); - - let permutations_vec = match permutations_vec { - Ok(permutations_vec) => permutations_vec, - Err(err) => return Err(err), - }; + .collect::, _>>()?; // Evaluate the lookups, if any, at omega^i x. - let lookups_vec: Result>>, _> = lookups_vec + let lookups_vec: Vec>> = lookups_vec .into_iter() .map(|lookups| -> Result, _> { lookups @@ -456,12 +421,7 @@ pub fn create_proof, ConcreteCircuit: Circ .map(|p| p.evaluate(pk, x, transcript)) .collect::, _>>() }) - .collect(); - - let lookups_vec = match lookups_vec { - Ok(lookups_vec) => lookups_vec, - Err(err) => return Err(err), - }; + .collect::, _>>()?; let instances = aux_vec .iter() @@ -496,17 +456,10 @@ pub fn create_proof, ConcreteCircuit: Circ permutations .iter() .zip(pk.permutations.iter()) - .map(move |(p, pkey)| p.open(pk, pkey, x)) - .into_iter() - .flatten(), - ) - .chain( - lookups - .iter() - .map(move |p| p.open(pk, x)) - .into_iter() - .flatten(), + .flat_map(move |(p, pkey)| p.open(pk, pkey, x)) + .into_iter(), ) + .chain(lookups.iter().flat_map(move |p| p.open(pk, x)).into_iter()) }) .collect::>() .into_iter() diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index 2b6548e..5f3abb6 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -38,28 +38,26 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( } } - let mut advice_commitments_vec = Vec::with_capacity(num_proofs); - for _ in 0..num_proofs { - // Hash the prover's advice commitments into the transcript - let advice_commitments = read_n_points(transcript, vk.cs.num_advice_columns) - .map_err(|_| Error::TranscriptError)?; - advice_commitments_vec.push(advice_commitments); - } + let advice_commitments_vec = (0..num_proofs) + .map(|_| -> Result, _> { + // Hash the prover's advice commitments into the transcript + read_n_points(transcript, vk.cs.num_advice_columns).map_err(|_| Error::TranscriptError) + }) + .collect::, _>>()?; // Sample theta challenge for keeping lookup columns linearly independent let theta = ChallengeTheta::get(transcript); - let mut lookups_permuted_vec = Vec::with_capacity(num_proofs); - for _ in 0..num_proofs { - // Hash each lookup permuted commitment - let lookups = vk - .cs - .lookups - .iter() - .map(|argument| argument.read_permuted_commitments(transcript)) - .collect::, _>>()?; - lookups_permuted_vec.push(lookups); - } + let lookups_permuted_vec = (0..num_proofs) + .map(|_| -> Result, _> { + // Hash each lookup permuted commitment + vk.cs + .lookups + .iter() + .map(|argument| argument.read_permuted_commitments(transcript)) + .collect::, _>>() + }) + .collect::, _>>()?; // Sample beta challenge let beta = ChallengeBeta::get(transcript); @@ -67,27 +65,27 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( // Sample gamma challenge let gamma = ChallengeGamma::get(transcript); - let mut permutations_committed_vec = Vec::with_capacity(num_proofs); - for _ in 0..num_proofs { - // Hash each permutation product commitment - let permutations = vk - .cs - .permutations - .iter() - .map(|argument| argument.read_product_commitment(transcript)) - .collect::, _>>()?; - permutations_committed_vec.push(permutations); - } + let permutations_committed_vec = (0..num_proofs) + .map(|_| -> Result, _> { + // Hash each permutation product commitment + vk.cs + .permutations + .iter() + .map(|argument| argument.read_product_commitment(transcript)) + .collect::, _>>() + }) + .collect::, _>>()?; - let mut lookups_committed_vec = Vec::with_capacity(num_proofs); - for lookups in lookups_permuted_vec.into_iter() { - // Hash each lookup product commitment - let lookups = lookups - .into_iter() - .map(|lookup| lookup.read_product_commitment(transcript)) - .collect::, _>>()?; - lookups_committed_vec.push(lookups); - } + let lookups_committed_vec = lookups_permuted_vec + .into_iter() + .map(|lookups| { + // Hash each lookup product commitment + lookups + .into_iter() + .map(|lookup| lookup.read_product_commitment(transcript)) + .collect::, _>>() + }) + .collect::, _>>()?; // Sample y challenge, which keeps the gates linearly independent. let y = ChallengeY::get(transcript); @@ -98,43 +96,44 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( // satisfied with high probability. let x = ChallengeX::get(transcript); - let mut aux_evals_vec = Vec::with_capacity(num_proofs); - for _ in 0..num_proofs { - let aux_evals = read_n_scalars(transcript, vk.cs.aux_queries.len()) - .map_err(|_| Error::TranscriptError)?; - aux_evals_vec.push(aux_evals); - } + let aux_evals_vec = (0..num_proofs) + .map(|_| -> Result, _> { + read_n_scalars(transcript, vk.cs.aux_queries.len()).map_err(|_| Error::TranscriptError) + }) + .collect::, _>>()?; - let mut advice_evals_vec = Vec::with_capacity(num_proofs); - for _ in 0..num_proofs { - let advice_evals = read_n_scalars(transcript, vk.cs.advice_queries.len()) - .map_err(|_| Error::TranscriptError)?; - advice_evals_vec.push(advice_evals); - } + let advice_evals_vec = (0..num_proofs) + .map(|_| -> Result, _> { + read_n_scalars(transcript, vk.cs.advice_queries.len()) + .map_err(|_| Error::TranscriptError) + }) + .collect::, _>>()?; let fixed_evals = read_n_scalars(transcript, vk.cs.fixed_queries.len()) .map_err(|_| Error::TranscriptError)?; let vanishing = vanishing.evaluate(transcript)?; - let mut permutations_evaluated_vec = Vec::with_capacity(num_proofs); - for permutations in permutations_committed_vec.into_iter() { - let permutations = permutations - .into_iter() - .zip(vk.permutations.iter()) - .map(|(permutation, vkey)| permutation.evaluate(vkey, transcript)) - .collect::, _>>()?; - permutations_evaluated_vec.push(permutations); - } + let permutations_evaluated_vec = permutations_committed_vec + .into_iter() + .map(|permutations| -> Result, _> { + permutations + .into_iter() + .zip(vk.permutations.iter()) + .map(|(permutation, vkey)| permutation.evaluate(vkey, transcript)) + .collect::, _>>() + }) + .collect::, _>>()?; - let mut lookups_evaluated_vec = Vec::with_capacity(num_proofs); - for lookups in lookups_committed_vec.into_iter() { - let lookups = lookups - .into_iter() - .map(|lookup| lookup.evaluate(transcript)) - .collect::, _>>()?; - lookups_evaluated_vec.push(lookups); - } + let lookups_evaluated_vec = lookups_committed_vec + .into_iter() + .map(|lookups| -> Result, _> { + lookups + .into_iter() + .map(|lookup| lookup.evaluate(transcript)) + .collect::, _>>() + }) + .collect::, _>>()?; // This check ensures the circuit is satisfied so long as the polynomial // commitments open to the correct values. @@ -174,17 +173,16 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( permutations .iter() .zip(vk.cs.permutations.iter()) - .map(move |(p, argument)| { + .flat_map(move |(p, argument)| { p.expressions(vk, argument, &advice_evals, l_0, beta, gamma, x) }) - .into_iter() - .flatten(), + .into_iter(), ) .chain( lookups .iter() .zip(vk.cs.lookups.iter()) - .map(move |(p, argument)| { + .flat_map(move |(p, argument)| { p.expressions( vk, l_0, @@ -197,12 +195,9 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( &aux_evals, ) }) - .into_iter() - .flatten(), + .into_iter(), ) - }) - .collect::>() - .into_iter(); + }); vanishing.verify(expressions, y, xn)?; } @@ -238,21 +233,17 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( permutations .iter() .zip(vk.permutations.iter()) - .map(move |(p, vkey)| p.queries(vk, vkey, x)) - .into_iter() - .flatten(), + .flat_map(move |(p, vkey)| p.queries(vk, vkey, x)) + .into_iter(), ) .chain( lookups .iter() - .map(move |p| p.queries(vk, x)) - .into_iter() - .flatten(), + .flat_map(move |p| p.queries(vk, x)) + .into_iter(), ) }, ) - .collect::>() - .into_iter() .chain( vk.cs .fixed_queries