From ecc805fa354096559a1993cde1a3fc44dc20c0a5 Mon Sep 17 00:00:00 2001 From: ying tong Date: Fri, 4 Dec 2020 09:18:28 +0800 Subject: [PATCH] Correct privacy of lookup structs + minor cleanups Co-authored-by: str4d --- src/plonk/lookup/prover.rs | 54 ++++++++++++++---------------------- src/plonk/lookup/verifier.rs | 54 +++++++++++++++--------------------- src/plonk/prover.rs | 4 +-- 3 files changed, 46 insertions(+), 66 deletions(-) diff --git a/src/plonk/lookup/prover.rs b/src/plonk/lookup/prover.rs index c39294c..31b6c1d 100644 --- a/src/plonk/lookup/prover.rs +++ b/src/plonk/lookup/prover.rs @@ -1,5 +1,5 @@ use super::super::{ - circuit::{Advice, Any, Aux, Column, Fixed}, + circuit::{Any, Column}, ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, Error, ProvingKey, }; use super::{Argument, Proof}; @@ -13,10 +13,10 @@ use crate::{ transcript::{Hasher, Transcript}, }; use ff::Field; -use std::{collections::BTreeMap, convert::TryFrom, iter}; +use std::{collections::BTreeMap, iter}; #[derive(Debug)] -pub(crate) struct Permuted<'a, C: CurveAffine> { +pub(in crate::plonk) struct Permuted<'a, C: CurveAffine> { unpermuted_input_columns: Vec<&'a Polynomial>, unpermuted_input_cosets: Vec<&'a Polynomial>, permuted_input_column: Polynomial, @@ -35,7 +35,7 @@ pub(crate) struct Permuted<'a, C: CurveAffine> { } #[derive(Debug)] -pub(crate) struct Committed<'a, C: CurveAffine> { +pub(in crate::plonk) struct Committed<'a, C: CurveAffine> { permuted: Permuted<'a, C>, product_poly: Polynomial, product_coset: Polynomial, @@ -44,7 +44,7 @@ pub(crate) struct Committed<'a, C: CurveAffine> { product_commitment: C, } -pub(crate) struct Constructed { +pub(in crate::plonk) struct Constructed { permuted_input_poly: Polynomial, permuted_input_blind: Blind, permuted_input_commitment: C, @@ -56,13 +56,13 @@ pub(crate) struct Constructed { product_commitment: C, } -pub(crate) struct Evaluated { +pub(in crate::plonk) struct Evaluated { constructed: Constructed, - pub product_eval: C::Scalar, - pub product_inv_eval: C::Scalar, - pub permuted_input_eval: C::Scalar, - pub permuted_input_inv_eval: C::Scalar, - pub permuted_table_eval: C::Scalar, + product_eval: C::Scalar, + product_inv_eval: C::Scalar, + permuted_input_eval: C::Scalar, + permuted_input_inv_eval: C::Scalar, + permuted_table_eval: C::Scalar, } impl Argument { @@ -99,28 +99,16 @@ impl Argument { // Values of input columns involved in the lookup let (unpermuted_columns, unpermuted_cosets): (Vec<_>, Vec<_>) = columns .iter() - .map(|&column| match column.column_type() { - Any::Advice => ( - &advice_values[column.index()], - &advice_cosets[pk.vk.cs.get_advice_query_index( - Column::::try_from(column).unwrap(), - 0, - )], - ), - Any::Fixed => ( - &fixed_values[column.index()], - &fixed_cosets[pk - .vk - .cs - .get_fixed_query_index(Column::::try_from(column).unwrap(), 0)], - ), - Any::Aux => ( - &aux_values[column.index()], - &aux_cosets[pk - .vk - .cs - .get_aux_query_index(Column::::try_from(column).unwrap(), 0)], - ), + .map(|&column| { + let (values, cosets) = match column.column_type() { + Any::Advice => (advice_values, advice_cosets), + Any::Fixed => (fixed_values, fixed_cosets), + Any::Aux => (aux_values, aux_cosets), + }; + ( + &values[column.index()], + &cosets[pk.vk.cs.get_any_query_index(column, 0)], + ) }) .unzip(); diff --git a/src/plonk/lookup/verifier.rs b/src/plonk/lookup/verifier.rs index 8bce162..150c439 100644 --- a/src/plonk/lookup/verifier.rs +++ b/src/plonk/lookup/verifier.rs @@ -1,6 +1,6 @@ use std::iter; -use super::super::circuit::Any; +use super::super::circuit::{Any, Column}; use super::{Argument, Proof}; use crate::{ arithmetic::CurveAffine, @@ -11,7 +11,7 @@ use crate::{ use ff::Field; impl Proof { - pub(crate) fn absorb_permuted_commitments< + pub(in crate::plonk) fn absorb_permuted_commitments< HBase: Hasher, HScalar: Hasher, >( @@ -26,7 +26,10 @@ impl Proof { .map_err(|_| Error::TranscriptError) } - pub(crate) fn absorb_product_commitment, HScalar: Hasher>( + pub(in crate::plonk) fn absorb_product_commitment< + HBase: Hasher, + HScalar: Hasher, + >( &self, transcript: &mut Transcript, ) -> Result<(), Error> { @@ -54,34 +57,23 @@ impl Proof { * &(self.permuted_input_eval + &beta) * &(self.permuted_table_eval + &gamma); - let mut right = self.product_inv_eval; - let mut input_term = C::Scalar::zero(); - for &input in argument.input_columns.iter() { - let index = vk.cs.get_any_query_index(input, 0); - let eval = match input.column_type() { - Any::Advice => advice_evals[index], - Any::Fixed => fixed_evals[index], - Any::Aux => aux_evals[index], - }; - input_term *= θ - input_term += &eval; - } - input_term += β + let compress_columns = |columns: &[Column]| { + columns + .iter() + .map(|column| { + let index = vk.cs.get_any_query_index(*column, 0); + match column.column_type() { + Any::Advice => advice_evals[index], + Any::Fixed => fixed_evals[index], + Any::Aux => aux_evals[index], + } + }) + .fold(C::Scalar::zero(), |acc, eval| acc * &theta + &eval) + }; + let right = self.product_inv_eval + * &(compress_columns(&argument.input_columns) + &beta) + * &(compress_columns(&argument.table_columns) + &gamma); - let mut table_term = C::Scalar::zero(); - for &table in argument.table_columns.iter() { - let index = vk.cs.get_any_query_index(table, 0); - let eval = match table.column_type() { - Any::Advice => advice_evals[index], - Any::Fixed => fixed_evals[index], - Any::Aux => aux_evals[index], - }; - table_term *= θ - table_term += &eval; - } - table_term += γ - - right *= &(input_term * &table_term); left - &right }; @@ -106,7 +98,7 @@ impl Proof { )) } - pub(crate) fn evals(&self) -> impl Iterator { + pub(in crate::plonk) fn evals(&self) -> impl Iterator { iter::empty() .chain(Some(&self.product_eval)) .chain(Some(&self.product_inv_eval)) diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 964b3fb..8b28990 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -170,7 +170,7 @@ impl Proof { .collect(); // Sample theta challenge for keeping lookup columns linearly independent - let theta = ChallengeTheta::::get(&mut transcript); + let theta = ChallengeTheta::get(&mut transcript); // Construct and commit to permuted values for each lookup let lookups = pk @@ -402,7 +402,7 @@ impl Proof { advice_commitments, h_commitments, permutations: permutations.map(|p| p.build()), - lookups: lookups.into_iter().map(|p| p.build()).collect::>(), + lookups: lookups.into_iter().map(|p| p.build()).collect(), advice_evals, fixed_evals, aux_evals,