Cleanups from code review

Co-authored-by: Kris Nuttycombe <kris.nuttycombe@gmail.com>
Co-authored-by: Sean Bowe <ewillbefull@gmail.com>
This commit is contained in:
therealyingtong 2021-01-26 15:23:29 +08:00
parent de86391f0e
commit a00d7c2fa6
2 changed files with 140 additions and 196 deletions

View file

@ -34,13 +34,13 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
let mut meta = ConstraintSystem::default(); let mut meta = ConstraintSystem::default();
let config = ConcreteCircuit::configure(&mut meta); let config = ConcreteCircuit::configure(&mut meta);
struct AuxSingle<C: CurveAffine> { struct AuxSingle<'a, C: CurveAffine> {
pub aux_values: Vec<Polynomial<C::Scalar, LagrangeCoeff>>, pub aux_values: &'a [Polynomial<C::Scalar, LagrangeCoeff>],
pub aux_polys: Vec<Polynomial<C::Scalar, Coeff>>, pub aux_polys: Vec<Polynomial<C::Scalar, Coeff>>,
pub aux_cosets: Vec<Polynomial<C::Scalar, ExtendedLagrangeCoeff>>, pub aux_cosets: Vec<Polynomial<C::Scalar, ExtendedLagrangeCoeff>>,
} }
let aux_vec: Result<Vec<_>, _> = auxs let aux_vec: Vec<AuxSingle<C>> = auxs
.iter() .iter()
.map(|aux| -> Result<AuxSingle<C>, Error> { .map(|aux| -> Result<AuxSingle<C>, Error> {
let aux_commitments_projective: Vec<_> = aux let aux_commitments_projective: Vec<_> = aux
@ -77,17 +77,12 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
.collect(); .collect();
Ok(AuxSingle { Ok(AuxSingle {
aux_values: aux.to_vec(), aux_values: *aux,
aux_polys, aux_polys,
aux_cosets, aux_cosets,
}) })
}) })
.collect(); .collect::<Result<Vec<_>, _>>()?;
let aux_vec = match aux_vec {
Ok(aux_vec) => aux_vec,
Err(err) => return Err(err),
};
struct AdviceSingle<C: CurveAffine> { struct AdviceSingle<C: CurveAffine> {
pub advice_values: Vec<Polynomial<C::Scalar, LagrangeCoeff>>, pub advice_values: Vec<Polynomial<C::Scalar, LagrangeCoeff>>,
@ -96,7 +91,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
pub advice_blinds: Vec<Blind<C::Scalar>>, pub advice_blinds: Vec<Blind<C::Scalar>>,
} }
let advice_vec: Result<Vec<_>, _> = circuits let advice_vec: Vec<AdviceSingle<C>> = circuits
.iter() .iter()
.map(|circuit| -> Result<AdviceSingle<C>, Error> { .map(|circuit| -> Result<AdviceSingle<C>, Error> {
struct WitnessCollection<F: Field> { struct WitnessCollection<F: Field> {
@ -202,50 +197,38 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
advice_blinds, advice_blinds,
}) })
}) })
.collect(); .collect::<Result<Vec<_>, _>>()?;
let advice_vec = match advice_vec {
Ok(advice_vec) => advice_vec,
Err(err) => return Err(err),
};
// Sample theta challenge for keeping lookup columns linearly independent // Sample theta challenge for keeping lookup columns linearly independent
let theta = ChallengeTheta::get(transcript); let theta = ChallengeTheta::get(transcript);
let lookups_vec: Result<Vec<Vec<_>>, _> = aux_vec let lookups_vec: Vec<Vec<lookup::prover::Permuted<'_, C>>> = aux_vec
.iter() .iter()
.zip(advice_vec.iter()) .zip(advice_vec.iter())
.map( .map(|(aux, advice)| -> Result<Vec<_>, Error> {
|(aux, advice)| -> Result<Vec<lookup::prover::Permuted<'_, C>>, Error> { // Construct and commit to permuted values for each lookup
// Construct and commit to permuted values for each lookup pk.vk
pk.vk .cs
.cs .lookups
.lookups .iter()
.iter() .map(|lookup| {
.map(|lookup| { lookup.commit_permuted(
lookup.commit_permuted( &pk,
&pk, &params,
&params, &domain,
&domain, theta,
theta, &advice.advice_values,
&advice.advice_values, &pk.fixed_values,
&pk.fixed_values, &aux.aux_values,
&aux.aux_values, &advice.advice_cosets,
&advice.advice_cosets, &pk.fixed_cosets,
&pk.fixed_cosets, &aux.aux_cosets,
&aux.aux_cosets, transcript,
transcript, )
) })
}) .collect()
.collect() })
}, .collect::<Result<Vec<_>, _>>()?;
)
.collect();
let lookups_vec = match lookups_vec {
Ok(lookups_vec) => lookups_vec,
Err(err) => return Err(err),
};
// Sample beta challenge // Sample beta challenge
let beta = ChallengeBeta::get(transcript); let beta = ChallengeBeta::get(transcript);
@ -253,38 +236,31 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
// Sample gamma challenge // Sample gamma challenge
let gamma = ChallengeGamma::get(transcript); let gamma = ChallengeGamma::get(transcript);
let permutations_vec: Result<Vec<Vec<_>>, _> = advice_vec let permutations_vec: Vec<Vec<permutation::prover::Committed<C>>> = advice_vec
.iter() .iter()
.map( .map(|advice| -> Result<Vec<_>, Error> {
|advice| -> Result<Vec<permutation::prover::Committed<C>>, Error> { // Commit to permutations, if any.
// Commit to permutations, if any. pk.vk
pk.vk .cs
.cs .permutations
.permutations .iter()
.iter() .zip(pk.permutations.iter())
.zip(pk.permutations.iter()) .map(|(p, pkey)| {
.map(|(p, pkey)| { p.commit(
p.commit( params,
params, pk,
pk, pkey,
pkey, &advice.advice_values,
&advice.advice_values, beta,
beta, gamma,
gamma, transcript,
transcript, )
) })
}) .collect()
.collect() })
}, .collect::<Result<Vec<_>, _>>()?;
)
.collect();
let permutations_vec = match permutations_vec { let lookups_vec: Vec<Vec<lookup::prover::Committed<'_, C>>> = lookups_vec
Ok(permutations_vec) => permutations_vec,
Err(err) => return Err(err),
};
let lookups_vec: Result<Vec<Vec<lookup::prover::Committed<'_, C>>>, _> = lookups_vec
.into_iter() .into_iter()
.map(|lookups| -> Result<Vec<_>, _> { .map(|lookups| -> Result<Vec<_>, _> {
// Construct and commit to products for each lookup // Construct and commit to products for each lookup
@ -293,12 +269,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
.map(|lookup| lookup.commit_product(&pk, &params, theta, beta, gamma, transcript)) .map(|lookup| lookup.commit_product(&pk, &params, theta, beta, gamma, transcript))
.collect::<Result<Vec<_>, _>>() .collect::<Result<Vec<_>, _>>()
}) })
.collect(); .collect::<Result<Vec<_>, _>>()?;
let lookups_vec = match lookups_vec {
Ok(lookups_vec) => lookups_vec,
Err(err) => return Err(err),
};
// Obtain challenge for keeping all separate gates linearly independent // Obtain challenge for keeping all separate gates linearly independent
let y = ChallengeY::get(transcript); let y = ChallengeY::get(transcript);
@ -308,6 +279,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
.into_iter() .into_iter()
.zip(advice_vec.iter()) .zip(advice_vec.iter())
.map(|(permutations, advice)| { .map(|(permutations, advice)| {
// Evaluate the h(X) polynomial's constraint system expressions for the permutation constraints, if any.
let tmp: Vec<_> = permutations let tmp: Vec<_> = permutations
.into_iter() .into_iter()
.zip(pk.vk.cs.permutations.iter()) .zip(pk.vk.cs.permutations.iter())
@ -362,9 +334,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
// Lookup constraints, if any. // Lookup constraints, if any.
.chain(lookup_expressions.into_iter().flatten()) .chain(lookup_expressions.into_iter().flatten())
}, },
) );
.collect::<Vec<_>>()
.into_iter();
// Construct the vanishing argument // Construct the vanishing argument
let vanishing = vanishing::Argument::construct(params, domain, expressions, y, transcript)?; let vanishing = vanishing::Argument::construct(params, domain, expressions, y, transcript)?;
@ -431,7 +401,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
let vanishing = vanishing.evaluate(x, transcript)?; let vanishing = vanishing.evaluate(x, transcript)?;
// Evaluate the permutations, if any, at omega^i x. // Evaluate the permutations, if any, at omega^i x.
let permutations_vec: Result<Vec<Vec<permutation::prover::Evaluated<C>>>, _> = permutations_vec let permutations_vec: Vec<Vec<permutation::prover::Evaluated<C>>> = permutations_vec
.into_iter() .into_iter()
.map(|permutations| -> Result<Vec<_>, _> { .map(|permutations| -> Result<Vec<_>, _> {
permutations permutations
@ -440,15 +410,10 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
.map(|(p, pkey)| p.evaluate(pk, pkey, x, transcript)) .map(|(p, pkey)| p.evaluate(pk, pkey, x, transcript))
.collect::<Result<Vec<_>, _>>() .collect::<Result<Vec<_>, _>>()
}) })
.collect(); .collect::<Result<Vec<_>, _>>()?;
let permutations_vec = match permutations_vec {
Ok(permutations_vec) => permutations_vec,
Err(err) => return Err(err),
};
// Evaluate the lookups, if any, at omega^i x. // Evaluate the lookups, if any, at omega^i x.
let lookups_vec: Result<Vec<Vec<lookup::prover::Evaluated<C>>>, _> = lookups_vec let lookups_vec: Vec<Vec<lookup::prover::Evaluated<C>>> = lookups_vec
.into_iter() .into_iter()
.map(|lookups| -> Result<Vec<_>, _> { .map(|lookups| -> Result<Vec<_>, _> {
lookups lookups
@ -456,12 +421,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
.map(|p| p.evaluate(pk, x, transcript)) .map(|p| p.evaluate(pk, x, transcript))
.collect::<Result<Vec<_>, _>>() .collect::<Result<Vec<_>, _>>()
}) })
.collect(); .collect::<Result<Vec<_>, _>>()?;
let lookups_vec = match lookups_vec {
Ok(lookups_vec) => lookups_vec,
Err(err) => return Err(err),
};
let instances = aux_vec let instances = aux_vec
.iter() .iter()
@ -496,17 +456,10 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
permutations permutations
.iter() .iter()
.zip(pk.permutations.iter()) .zip(pk.permutations.iter())
.map(move |(p, pkey)| p.open(pk, pkey, x)) .flat_map(move |(p, pkey)| p.open(pk, pkey, x))
.into_iter() .into_iter(),
.flatten(),
)
.chain(
lookups
.iter()
.map(move |p| p.open(pk, x))
.into_iter()
.flatten(),
) )
.chain(lookups.iter().flat_map(move |p| p.open(pk, x)).into_iter())
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.into_iter() .into_iter()

View file

@ -38,28 +38,26 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
} }
} }
let mut advice_commitments_vec = Vec::with_capacity(num_proofs); let advice_commitments_vec = (0..num_proofs)
for _ in 0..num_proofs { .map(|_| -> Result<Vec<_>, _> {
// Hash the prover's advice commitments into the transcript // Hash the prover's advice commitments into the transcript
let advice_commitments = read_n_points(transcript, vk.cs.num_advice_columns) read_n_points(transcript, vk.cs.num_advice_columns).map_err(|_| Error::TranscriptError)
.map_err(|_| Error::TranscriptError)?; })
advice_commitments_vec.push(advice_commitments); .collect::<Result<Vec<_>, _>>()?;
}
// Sample theta challenge for keeping lookup columns linearly independent // Sample theta challenge for keeping lookup columns linearly independent
let theta = ChallengeTheta::get(transcript); let theta = ChallengeTheta::get(transcript);
let mut lookups_permuted_vec = Vec::with_capacity(num_proofs); let lookups_permuted_vec = (0..num_proofs)
for _ in 0..num_proofs { .map(|_| -> Result<Vec<_>, _> {
// Hash each lookup permuted commitment // Hash each lookup permuted commitment
let lookups = vk vk.cs
.cs .lookups
.lookups .iter()
.iter() .map(|argument| argument.read_permuted_commitments(transcript))
.map(|argument| argument.read_permuted_commitments(transcript)) .collect::<Result<Vec<_>, _>>()
.collect::<Result<Vec<_>, _>>()?; })
lookups_permuted_vec.push(lookups); .collect::<Result<Vec<_>, _>>()?;
}
// Sample beta challenge // Sample beta challenge
let beta = ChallengeBeta::get(transcript); let beta = ChallengeBeta::get(transcript);
@ -67,27 +65,27 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
// Sample gamma challenge // Sample gamma challenge
let gamma = ChallengeGamma::get(transcript); let gamma = ChallengeGamma::get(transcript);
let mut permutations_committed_vec = Vec::with_capacity(num_proofs); let permutations_committed_vec = (0..num_proofs)
for _ in 0..num_proofs { .map(|_| -> Result<Vec<_>, _> {
// Hash each permutation product commitment // Hash each permutation product commitment
let permutations = vk vk.cs
.cs .permutations
.permutations .iter()
.iter() .map(|argument| argument.read_product_commitment(transcript))
.map(|argument| argument.read_product_commitment(transcript)) .collect::<Result<Vec<_>, _>>()
.collect::<Result<Vec<_>, _>>()?; })
permutations_committed_vec.push(permutations); .collect::<Result<Vec<_>, _>>()?;
}
let mut lookups_committed_vec = Vec::with_capacity(num_proofs); let lookups_committed_vec = lookups_permuted_vec
for lookups in lookups_permuted_vec.into_iter() { .into_iter()
// Hash each lookup product commitment .map(|lookups| {
let lookups = lookups // Hash each lookup product commitment
.into_iter() lookups
.map(|lookup| lookup.read_product_commitment(transcript)) .into_iter()
.collect::<Result<Vec<_>, _>>()?; .map(|lookup| lookup.read_product_commitment(transcript))
lookups_committed_vec.push(lookups); .collect::<Result<Vec<_>, _>>()
} })
.collect::<Result<Vec<_>, _>>()?;
// Sample y challenge, which keeps the gates linearly independent. // Sample y challenge, which keeps the gates linearly independent.
let y = ChallengeY::get(transcript); let y = ChallengeY::get(transcript);
@ -98,43 +96,44 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
// satisfied with high probability. // satisfied with high probability.
let x = ChallengeX::get(transcript); let x = ChallengeX::get(transcript);
let mut aux_evals_vec = Vec::with_capacity(num_proofs); let aux_evals_vec = (0..num_proofs)
for _ in 0..num_proofs { .map(|_| -> Result<Vec<_>, _> {
let aux_evals = read_n_scalars(transcript, vk.cs.aux_queries.len()) read_n_scalars(transcript, vk.cs.aux_queries.len()).map_err(|_| Error::TranscriptError)
.map_err(|_| Error::TranscriptError)?; })
aux_evals_vec.push(aux_evals); .collect::<Result<Vec<_>, _>>()?;
}
let mut advice_evals_vec = Vec::with_capacity(num_proofs); let advice_evals_vec = (0..num_proofs)
for _ in 0..num_proofs { .map(|_| -> Result<Vec<_>, _> {
let advice_evals = read_n_scalars(transcript, vk.cs.advice_queries.len()) read_n_scalars(transcript, vk.cs.advice_queries.len())
.map_err(|_| Error::TranscriptError)?; .map_err(|_| Error::TranscriptError)
advice_evals_vec.push(advice_evals); })
} .collect::<Result<Vec<_>, _>>()?;
let fixed_evals = read_n_scalars(transcript, vk.cs.fixed_queries.len()) let fixed_evals = read_n_scalars(transcript, vk.cs.fixed_queries.len())
.map_err(|_| Error::TranscriptError)?; .map_err(|_| Error::TranscriptError)?;
let vanishing = vanishing.evaluate(transcript)?; let vanishing = vanishing.evaluate(transcript)?;
let mut permutations_evaluated_vec = Vec::with_capacity(num_proofs); let permutations_evaluated_vec = permutations_committed_vec
for permutations in permutations_committed_vec.into_iter() { .into_iter()
let permutations = permutations .map(|permutations| -> Result<Vec<_>, _> {
.into_iter() permutations
.zip(vk.permutations.iter()) .into_iter()
.map(|(permutation, vkey)| permutation.evaluate(vkey, transcript)) .zip(vk.permutations.iter())
.collect::<Result<Vec<_>, _>>()?; .map(|(permutation, vkey)| permutation.evaluate(vkey, transcript))
permutations_evaluated_vec.push(permutations); .collect::<Result<Vec<_>, _>>()
} })
.collect::<Result<Vec<_>, _>>()?;
let mut lookups_evaluated_vec = Vec::with_capacity(num_proofs); let lookups_evaluated_vec = lookups_committed_vec
for lookups in lookups_committed_vec.into_iter() { .into_iter()
let lookups = lookups .map(|lookups| -> Result<Vec<_>, _> {
.into_iter() lookups
.map(|lookup| lookup.evaluate(transcript)) .into_iter()
.collect::<Result<Vec<_>, _>>()?; .map(|lookup| lookup.evaluate(transcript))
lookups_evaluated_vec.push(lookups); .collect::<Result<Vec<_>, _>>()
} })
.collect::<Result<Vec<_>, _>>()?;
// This check ensures the circuit is satisfied so long as the polynomial // This check ensures the circuit is satisfied so long as the polynomial
// commitments open to the correct values. // commitments open to the correct values.
@ -174,17 +173,16 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
permutations permutations
.iter() .iter()
.zip(vk.cs.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) p.expressions(vk, argument, &advice_evals, l_0, beta, gamma, x)
}) })
.into_iter() .into_iter(),
.flatten(),
) )
.chain( .chain(
lookups lookups
.iter() .iter()
.zip(vk.cs.lookups.iter()) .zip(vk.cs.lookups.iter())
.map(move |(p, argument)| { .flat_map(move |(p, argument)| {
p.expressions( p.expressions(
vk, vk,
l_0, l_0,
@ -197,12 +195,9 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
&aux_evals, &aux_evals,
) )
}) })
.into_iter() .into_iter(),
.flatten(),
) )
}) });
.collect::<Vec<_>>()
.into_iter();
vanishing.verify(expressions, y, xn)?; vanishing.verify(expressions, y, xn)?;
} }
@ -238,21 +233,17 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
permutations permutations
.iter() .iter()
.zip(vk.permutations.iter()) .zip(vk.permutations.iter())
.map(move |(p, vkey)| p.queries(vk, vkey, x)) .flat_map(move |(p, vkey)| p.queries(vk, vkey, x))
.into_iter() .into_iter(),
.flatten(),
) )
.chain( .chain(
lookups lookups
.iter() .iter()
.map(move |p| p.queries(vk, x)) .flat_map(move |p| p.queries(vk, x))
.into_iter() .into_iter(),
.flatten(),
) )
}, },
) )
.collect::<Vec<_>>()
.into_iter()
.chain( .chain(
vk.cs vk.cs
.fixed_queries .fixed_queries