Refactor to use fold() in multiple places

This commit is contained in:
therealyingtong 2020-10-09 21:34:53 +08:00
parent 79cabb3d8d
commit b62d113031
1 changed files with 23 additions and 27 deletions

View File

@ -85,29 +85,23 @@ impl<'a, C: CurveAffine> Proof<C> {
C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap();
transcript.absorb(transcript_scalar_point); transcript.absorb(transcript_scalar_point);
// lagrange_Interpolate polynomial for evaluations at each set
let mut r_evals = vec![C::Scalar::zero(); point_sets.len()];
let mut r_polys: Vec<Vec<C::Scalar>> = Vec::with_capacity(point_sets.len());
for (points, evals) in point_sets.clone().iter().zip(q_eval_sets.clone().iter()) {
r_polys.push(lagrange_interpolate(&points, &evals));
}
for (r_eval, r_poly) in r_evals.iter_mut().zip(r_polys.iter()) {
*r_eval = eval_polynomial(r_poly, x_6);
}
// We can compute the expected msm_eval at x_6 using the q_evals provided // We can compute the expected msm_eval at x_6 using the q_evals provided
// by the prover and from x_5 // by the prover and from x_5
let mut msm_eval = C::Scalar::zero(); let msm_eval = point_sets
for (set_idx, points) in point_sets.iter().enumerate() { .iter()
let mut eval = self.q_evals[set_idx]; .zip(q_eval_sets.iter())
eval -= &r_evals[set_idx]; .zip(self.q_evals.iter())
for point in points { .fold(
eval = eval * &(x_6 - &point).invert().unwrap(); C::Scalar::zero(),
} |msm_eval, ((points, evals), proof_eval)| {
let r_poly = lagrange_interpolate(points, evals);
msm_eval *= &x_5; let r_eval = eval_polynomial(&r_poly, x_6);
msm_eval += &eval; let eval = points.iter().fold(*proof_eval - &r_eval, |eval, point| {
} eval * &(x_6 - &point).invert().unwrap()
});
msm_eval * &x_5 + &eval
},
);
// Sample a challenge x_7 that we will use to collapse the openings of // Sample a challenge x_7 that we will use to collapse the openings of
// the various remaining polynomials at x_6 together. // the various remaining polynomials at x_6 together.
@ -116,12 +110,14 @@ impl<'a, C: CurveAffine> Proof<C> {
// Compute the final commitment that has to be opened // Compute the final commitment that has to be opened
let mut commitment_msm = params.empty_msm(); let mut commitment_msm = params.empty_msm();
commitment_msm.add_term(C::Scalar::one(), self.f_commitment); commitment_msm.add_term(C::Scalar::one(), self.f_commitment);
for (set_idx, _) in point_sets.iter().enumerate() { let (commitment_msm, msm_eval) = q_commitments.iter().zip(self.q_evals.iter()).fold(
(commitment_msm, msm_eval),
|(mut commitment_msm, msm_eval), (q_commitment, q_eval)| {
commitment_msm.scale(x_7); commitment_msm.scale(x_7);
commitment_msm.add_msm(&q_commitments[set_idx]); commitment_msm.add_msm(&q_commitment);
msm_eval *= &x_7; (commitment_msm, msm_eval * &x_7 + &q_eval)
msm_eval += &self.q_evals[set_idx]; },
} );
// Verify the opening proof // Verify the opening proof
self.opening self.opening