From c3d0a172a7944b7cbb11c93702f946b5a49748b8 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Tue, 29 Sep 2020 15:23:41 +0800 Subject: [PATCH 01/30] Create multiopen abstraction --- src/plonk.rs | 9 +- src/plonk/prover.rs | 268 +++++++++++---------------------- src/plonk/verifier.rs | 234 +++++++++++++--------------- src/poly.rs | 1 + src/poly/multiopen.rs | 23 +++ src/poly/multiopen/prover.rs | 165 ++++++++++++++++++++ src/poly/multiopen/verifier.rs | 92 +++++++++++ 7 files changed, 476 insertions(+), 316 deletions(-) create mode 100644 src/poly/multiopen.rs create mode 100644 src/poly/multiopen/prover.rs create mode 100644 src/poly/multiopen/verifier.rs diff --git a/src/plonk.rs b/src/plonk.rs index 4e4de2e..45238c3 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -7,7 +7,7 @@ use crate::arithmetic::CurveAffine; use crate::poly::{ - commitment, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, Polynomial, + multiopen, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, Polynomial, }; use crate::transcript::Hasher; @@ -59,9 +59,7 @@ pub struct Proof { aux_evals: Vec, fixed_evals: Vec, h_evals: Vec, - f_commitment: C, - q_evals: Vec, - opening: commitment::Proof, + multiopening: multiopen::Proof, } /// This is an error that could occur during proving or circuit synthesis. @@ -96,7 +94,8 @@ impl VerifyingKey { } } -fn hash_point>( +/// Hash a point into transcript +pub fn hash_point>( transcript: &mut H, point: &C, ) -> Result<(), Error> { diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 0805238..90e2c22 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -3,12 +3,12 @@ use super::{ hash_point, Error, Proof, ProvingKey, }; use crate::arithmetic::{ - eval_polynomial, get_challenge_scalar, kate_division, parallelize, BatchInvert, Challenge, - Curve, CurveAffine, Field, + eval_polynomial, get_challenge_scalar, parallelize, BatchInvert, Challenge, Curve, CurveAffine, + Field, }; use crate::poly::{ - commitment::{self, Blind, Params}, - Coeff, LagrangeCoeff, Polynomial, Rotation, + commitment::{Blind, Params}, + multiopen, Coeff, LagrangeCoeff, Polynomial, Rotation, }; use crate::transcript::Hasher; @@ -457,194 +457,100 @@ impl Proof { C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); transcript.absorb(transcript_scalar_point); - let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + let mut instances: Vec<( + usize, + Polynomial, + Blind, + C::Scalar, + )> = Vec::with_capacity( + meta.advice_queries.len() + + meta.aux_queries.len() + + meta.fixed_queries.len() + + h_pieces.len() + + permutation_product_polys.len() + + permutation_product_polys.len() + + pk.permutation_polys.len(), + ); - // Collapse openings at same points together into single openings using - // x_4 challenge. - let mut q_polys: Vec>> = - vec![None; meta.rotations.len()]; - let mut q_blinds = vec![Blind(C::Scalar::zero()); meta.rotations.len()]; - let mut q_evals: Vec<_> = vec![C::Scalar::zero(); meta.rotations.len()]; + for (query_index, &(wire, ref at)) in meta.advice_queries.iter().enumerate() { + let point_index = (*meta.rotations.get(at).unwrap()).0; + let poly = advice_polys[wire.0].clone(); + let blind = advice_blinds[wire.0]; + let eval = advice_evals[query_index]; + instances.push((point_index, poly, blind, eval)); + } + + for (query_index, &(wire, ref at)) in meta.aux_queries.iter().enumerate() { + let point_index = (*meta.rotations.get(at).unwrap()).0; + let poly = aux_polys[wire.0].clone(); + let blind = Blind::default(); + let eval = aux_evals[query_index]; + instances.push((point_index, poly, blind, eval)); + } + + for (query_index, &(wire, ref at)) in meta.fixed_queries.iter().enumerate() { + let point_index = (*meta.rotations.get(at).unwrap()).0; + let poly = pk.fixed_polys[wire.0].clone(); + let blind = Blind::default(); + let eval = fixed_evals[query_index]; + instances.push((point_index, poly, blind, eval)); + } + + // We query the h(X) polynomial at x_3 + let current_index = (*meta.rotations.get(&Rotation::default()).unwrap()).0; + for ((h_poly, h_blind), h_eval) in h_pieces + .into_iter() + .zip(h_blinds.iter()) + .zip(h_evals.iter()) { - let mut accumulate = - |point_index: usize, new_poly: &Polynomial<_, Coeff>, blind, eval| { - q_polys[point_index] - .as_mut() - .map(|poly| { - parallelize(poly, |q, start| { - for (q, a) in q.iter_mut().zip(new_poly[start..].iter()) { - *q *= &x_4; - *q += a; - } - }); - }) - .or_else(|| { - q_polys[point_index] = Some(new_poly.clone()); - Some(()) - }); - q_blinds[point_index] *= x_4; - q_blinds[point_index] += blind; - q_evals[point_index] *= &x_4; - q_evals[point_index] += &eval; - }; + instances.push((current_index, h_poly.clone(), *h_blind, *h_eval)); + } - for (query_index, &(wire, ref at)) in meta.advice_queries.iter().enumerate() { - let point_index = (*meta.rotations.get(at).unwrap()).0; - - accumulate( - point_index, - &advice_polys[wire.0], - advice_blinds[wire.0], - advice_evals[query_index], - ); - } - - for (query_index, &(wire, ref at)) in meta.aux_queries.iter().enumerate() { - let point_index = (*meta.rotations.get(at).unwrap()).0; - - accumulate( - point_index, - &aux_polys[wire.0], - Blind::default(), - aux_evals[query_index], - ); - } - - for (query_index, &(wire, ref at)) in meta.fixed_queries.iter().enumerate() { - let point_index = (*meta.rotations.get(at).unwrap()).0; - - accumulate( - point_index, - &pk.fixed_polys[wire.0], - Blind::default(), - fixed_evals[query_index], - ); - } - - // We query the h(X) polynomial at x_3 - let current_index = (*meta.rotations.get(&Rotation::default()).unwrap()).0; - for ((h_poly, h_blind), h_eval) in h_pieces - .into_iter() - .zip(h_blinds.iter()) - .zip(h_evals.iter()) + // Handle permutation arguments, if any exist + if !pk.vk.cs.permutations.is_empty() { + // Open permutation product commitments at x_3 + for ((poly, blind), eval) in permutation_product_polys + .iter() + .zip(permutation_product_blinds.iter()) + .zip(permutation_product_evals.iter()) { - accumulate(current_index, &h_poly, *h_blind, *h_eval); + instances.push((current_index, poly.clone(), *blind, *eval)); } - // Handle permutation arguments, if any exist - if !pk.vk.cs.permutations.is_empty() { - // Open permutation product commitments at x_3 - for ((poly, blind), eval) in permutation_product_polys - .iter() - .zip(permutation_product_blinds.iter()) - .zip(permutation_product_evals.iter()) - { - accumulate(current_index, poly, *blind, *eval); - } + // Open permutation polynomial commitments at x_3 + for (poly, eval) in pk + .permutation_polys + .iter() + .zip(permutation_evals.iter()) + .flat_map(|(polys, evals)| polys.iter().zip(evals.iter())) + { + instances.push((current_index, poly.clone(), Blind::default(), *eval)); + } - // Open permutation polynomial commitments at x_3 - for (poly, eval) in pk - .permutation_polys - .iter() - .zip(permutation_evals.iter()) - .flat_map(|(polys, evals)| polys.iter().zip(evals.iter())) - { - accumulate(current_index, poly, Blind::default(), *eval); - } - - let current_index = (*pk.vk.cs.rotations.get(&Rotation(-1)).unwrap()).0; - // Open permutation product commitments at \omega^{-1} x_3 - for ((poly, blind), eval) in permutation_product_polys - .iter() - .zip(permutation_product_blinds.iter()) - .zip(permutation_product_inv_evals.iter()) - { - accumulate(current_index, poly, *blind, *eval); - } + let current_index = (*pk.vk.cs.rotations.get(&Rotation(-1)).unwrap()).0; + // Open permutation product commitments at \omega^{-1} x_3 + for ((poly, blind), eval) in permutation_product_polys + .iter() + .zip(permutation_product_blinds.iter()) + .zip(permutation_product_inv_evals.iter()) + { + instances.push((current_index, poly.clone(), *blind, *eval)); } } - let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - - let mut f_poly: Option> = None; + let mut points: Vec = vec![C::Scalar::zero(); meta.rotations.len()]; for (&row, &point_index) in meta.rotations.iter() { - let mut poly = q_polys[point_index.0].as_ref().unwrap().clone(); - let point = domain.rotate_omega(x_3, row); - poly[0] -= &q_evals[point_index.0]; - // TODO: change kate_division interface? - let mut poly = kate_division(&poly[..], point); - poly.push(C::Scalar::zero()); - let poly = domain.coeff_from_vec(poly); - - f_poly = f_poly - .map(|mut f_poly| { - parallelize(&mut f_poly, |q, start| { - for (q, a) in q.iter_mut().zip(poly[start..].iter()) { - *q *= &x_5; - *q += a; - } - }); - f_poly - }) - .or_else(|| Some(poly)); + points[point_index.0] = domain.rotate_omega(x_3, row); } - let f_poly = f_poly.unwrap(); - let mut f_blind = Blind(C::Scalar::random()); - let mut f_commitment = params.commit(&f_poly, f_blind).to_affine(); - - let (opening, q_evals) = loop { - let mut transcript = transcript.clone(); - let mut transcript_scalar = transcript_scalar.clone(); - hash_point(&mut transcript, &f_commitment)?; - - let x_6: C::Scalar = - get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - - let mut q_evals = vec![C::Scalar::zero(); meta.rotations.len()]; - - for (_, &point_index) in meta.rotations.iter() { - q_evals[point_index.0] = - eval_polynomial(&q_polys[point_index.0].as_ref().unwrap(), x_6); - } - - for eval in q_evals.iter() { - transcript_scalar.absorb(*eval); - } - - let transcript_scalar_point = - C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); - transcript.absorb(transcript_scalar_point); - - let x_7: C::Scalar = - get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - - let mut f_blind_dup = f_blind; - let mut f_poly = f_poly.clone(); - for (_, &point_index) in meta.rotations.iter() { - f_blind_dup *= x_7; - f_blind_dup += q_blinds[point_index.0]; - - parallelize(&mut f_poly, |f, start| { - for (f, a) in f - .iter_mut() - .zip(q_polys[point_index.0].as_ref().unwrap()[start..].iter()) - { - *f *= &x_7; - *f += a; - } - }); - } - - if let Ok(opening) = - commitment::Proof::create(¶ms, &mut transcript, &f_poly, f_blind_dup, x_6) - { - break (opening, q_evals); - } else { - f_blind += C::Scalar::one(); - f_commitment = (f_commitment + params.h).to_affine(); - } - }; + let multiopening = multiopen::Proof::create( + params, + &mut transcript, + &mut transcript_scalar, + points, + instances, + ) + .unwrap(); Ok(Proof { advice_commitments, @@ -657,9 +563,7 @@ impl Proof { fixed_evals, aux_evals, h_evals, - f_commitment, - q_evals, - opening, + multiopening, }) } } diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index e7866a3..f7adbc0 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -2,6 +2,7 @@ use super::{hash_point, Error, Proof, VerifyingKey}; use crate::arithmetic::{get_challenge_scalar, Challenge, CurveAffine, Field}; use crate::poly::{ commitment::{Guard, Params, MSM}, + multiopen::VerifierQuery, Rotation, }; use crate::transcript::Hasher; @@ -9,18 +10,21 @@ use crate::transcript::Hasher; impl<'a, C: CurveAffine> Proof { /// Returns a boolean indicating whether or not the proof is valid pub fn verify, HScalar: Hasher>( - &self, + &'a self, params: &'a Params, - vk: &VerifyingKey, - mut msm: MSM<'a, C>, - aux_commitments: &[C], + vk: &'a VerifyingKey, + msm: MSM<'a, C>, + aux_commitments: &'a [C], ) -> Result, Error> { self.check_lengths(vk, aux_commitments)?; - // Scale the MSM by a random factor to ensure that if the existing MSM - // has is_zero() == false then this argument won't be able to interfere - // with it to make it true, with high probability. - msm.scale(C::Scalar::random()); + // Check that aux_commitments matches the expected number of aux_wires + // and self.aux_evals + if aux_commitments.len() != vk.cs.num_aux_wires + || self.aux_evals.len() != vk.cs.num_aux_wires + { + return Err(Error::IncompatibleParams); + } // Create a transcript for obtaining Fiat-Shamir challenges. let mut transcript = HBase::init(C::Base::one()); @@ -83,134 +87,106 @@ impl<'a, C: CurveAffine> Proof { C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); transcript.absorb(transcript_scalar_point); - // Sample x_4 for compressing openings at the same points together - let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + let mut queries: Vec> = Vec::new(); - // Compress the commitments and expected evaluations at x_3 together - // using the challenge x_4 - let mut q_commitments: Vec<_> = vec![params.empty_msm(); vk.cs.rotations.len()]; - let mut q_evals: Vec<_> = vec![C::Scalar::zero(); vk.cs.rotations.len()]; + for (query_index, &(wire, at)) in vk.cs.advice_queries.iter().enumerate() { + let point = vk.domain.rotate_omega(x_3, at); + queries.push(VerifierQuery { + point, + commitment: &self.advice_commitments[wire.0], + eval: self.advice_evals[query_index], + }); + } + + for (query_index, &(wire, at)) in vk.cs.aux_queries.iter().enumerate() { + let point = vk.domain.rotate_omega(x_3, at); + queries.push(VerifierQuery { + point, + commitment: &aux_commitments[wire.0], + eval: self.aux_evals[query_index], + }); + } + + for (query_index, &(wire, at)) in vk.cs.fixed_queries.iter().enumerate() { + let point = vk.domain.rotate_omega(x_3, at); + queries.push(VerifierQuery { + point, + commitment: &vk.fixed_commitments[wire.0], + eval: self.fixed_evals[query_index], + }); + } + + for ((idx, _), &eval) in self + .h_commitments + .iter() + .enumerate() + .zip(self.h_evals.iter()) { - let mut accumulate = |point_index: usize, new_commitment, eval| { - q_commitments[point_index].scale(x_4); - q_commitments[point_index].add_term(C::Scalar::one(), new_commitment); - q_evals[point_index] *= &x_4; - q_evals[point_index] += &eval; - }; + let commitment = &self.h_commitments[idx]; + queries.push(VerifierQuery { + point: x_3, + commitment, + eval, + }); + } - for (query_index, &(wire, ref at)) in vk.cs.advice_queries.iter().enumerate() { - let point_index = (*vk.cs.rotations.get(at).unwrap()).0; - accumulate( - point_index, - self.advice_commitments[wire.0], - self.advice_evals[query_index], - ); + // Handle permutation arguments, if any exist + if !vk.cs.permutations.is_empty() { + // Open permutation product commitments at x_3 + for ((idx, _), &eval) in self + .permutation_product_commitments + .iter() + .enumerate() + .zip(self.permutation_product_evals.iter()) + { + let commitment = &self.permutation_product_commitments[idx]; + queries.push(VerifierQuery { + point: x_3, + commitment, + eval, + }); } - - for (query_index, &(wire, ref at)) in vk.cs.aux_queries.iter().enumerate() { - let point_index = (*vk.cs.rotations.get(at).unwrap()).0; - accumulate( - point_index, - aux_commitments[wire.0], - self.aux_evals[query_index], - ); - } - - for (query_index, &(wire, ref at)) in vk.cs.fixed_queries.iter().enumerate() { - let point_index = (*vk.cs.rotations.get(at).unwrap()).0; - accumulate( - point_index, - vk.fixed_commitments[wire.0], - self.fixed_evals[query_index], - ); - } - - let current_index = (*vk.cs.rotations.get(&Rotation::default()).unwrap()).0; - for (commitment, eval) in self.h_commitments.iter().zip(self.h_evals.iter()) { - accumulate(current_index, *commitment, *eval); - } - - // Handle permutation arguments, if any exist - if !vk.cs.permutations.is_empty() { - // Open permutation product commitments at x_3 - for (commitment, eval) in self - .permutation_product_commitments - .iter() - .zip(self.permutation_product_evals.iter()) - { - accumulate(current_index, *commitment, *eval); - } - // Open permutation commitments for each permutation argument at x_3 - for (commitment, eval) in vk - .permutation_commitments - .iter() - .zip(self.permutation_evals.iter()) - .flat_map(|(commitments, evals)| commitments.iter().zip(evals.iter())) - { - accumulate(current_index, *commitment, *eval); - } - let current_index = (*vk.cs.rotations.get(&Rotation(-1)).unwrap()).0; - // Open permutation product commitments at \omega^{-1} x_3 - for (commitment, eval) in self - .permutation_product_commitments - .iter() - .zip(self.permutation_product_inv_evals.iter()) - { - accumulate(current_index, *commitment, *eval); + // Open permutation commitments for each permutation argument at x_3 + for outer_idx in 0..vk.permutation_commitments.len() { + let inner_len = vk.permutation_commitments[outer_idx].len(); + for inner_idx in 0..inner_len { + let commitment = &vk.permutation_commitments[outer_idx][inner_idx]; + let eval = self.permutation_evals[outer_idx][inner_idx]; + queries.push(VerifierQuery { + point: x_3, + commitment, + eval, + }); } } + + // Open permutation product commitments at \omega^{-1} x_3 + let x_3_inv = vk.domain.rotate_omega(x_3, Rotation(-1)); + for ((idx, _), &eval) in self + .permutation_product_commitments + .iter() + .enumerate() + .zip(self.permutation_product_inv_evals.iter()) + { + let commitment = &self.permutation_product_commitments[idx]; + queries.push(VerifierQuery { + point: x_3_inv, + commitment, + eval, + }); + } } - // Sample a challenge x_5 for keeping the multi-point quotient - // polynomial terms linearly independent. - let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - - // Obtain the commitment to the multi-point quotient polynomial f(X). - hash_point(&mut transcript, &self.f_commitment)?; - - // Sample a challenge x_6 for checking that f(X) was committed to - // correctly. - let x_6: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - - for eval in self.q_evals.iter() { - transcript_scalar.absorb(*eval); - } - - let transcript_scalar_point = - C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); - transcript.absorb(transcript_scalar_point); - - // We can compute the expected msm_eval at x_6 using the q_evals provided - // by the prover and from x_5 - let mut msm_eval = C::Scalar::zero(); - for (&row, point_index) in vk.cs.rotations.iter() { - let mut eval = self.q_evals[point_index.0]; - - let point = vk.domain.rotate_omega(x_3, row); - eval = eval - &q_evals[point_index.0]; - eval = eval * &(x_6 - &point).invert().unwrap(); - - msm_eval *= &x_5; - msm_eval += &eval; - } - - // Sample a challenge x_7 that we will use to collapse the openings of - // the various remaining polynomials at x_6 together. - let x_7: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - - // Compute the final commitment that has to be opened - let mut commitment_msm = params.empty_msm(); - commitment_msm.add_term(C::Scalar::one(), self.f_commitment); - for (_, &point_index) in vk.cs.rotations.iter() { - commitment_msm.scale(x_7); - commitment_msm.add_msm(&q_commitments[point_index.0]); - msm_eval *= &x_7; - msm_eval += &self.q_evals[point_index.0]; - } - - // Verify the opening proof - self.opening - .verify(params, msm, &mut transcript, x_6, commitment_msm, msm_eval) + // We are now convinced the circuit is satisfied so long as the + // polynomial commitments open to the correct values. + self.multiopening + .verify( + params, + &mut transcript, + &mut transcript_scalar, + queries, + msm, + ) .map_err(|_| Error::OpeningError) } @@ -225,7 +201,7 @@ impl<'a, C: CurveAffine> Proof { return Err(Error::IncompatibleParams); } - if self.q_evals.len() != vk.cs.rotations.len() { + if self.opening.q_evals.len() != vk.cs.rotations.len() { return Err(Error::IncompatibleParams); } diff --git a/src/poly.rs b/src/poly.rs index 2467599..fa2fc0f 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -10,6 +10,7 @@ use std::ops::{Add, Deref, DerefMut, Index, IndexMut, Mul, RangeFrom, RangeFull, pub mod commitment; mod domain; +pub mod multiopen; pub use domain::*; diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs new file mode 100644 index 0000000..5850fca --- /dev/null +++ b/src/poly/multiopen.rs @@ -0,0 +1,23 @@ +//! This module contains an implementation of the multipoint opening polynomial +//! commitment scheme described in the [Halo][halo] paper. +//! +//! [halo]: https://eprint.iacr.org/2019/1021 + +use super::*; +use crate::arithmetic::CurveAffine; + +mod prover; +mod verifier; + +/// This is a multi-point opening proof used in the polynomial commitment scheme opening. +#[derive(Debug, Clone)] +pub struct Proof { + /// A vector of evaluations at each set of query points + pub q_evals: Vec, + + /// Commitment to final polynomial + pub f_commitment: C, + + /// Commitment proof + pub opening: commitment::Proof, +} diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs new file mode 100644 index 0000000..48390b0 --- /dev/null +++ b/src/poly/multiopen/prover.rs @@ -0,0 +1,165 @@ +use std::marker::PhantomData; + +use super::super::{ + commitment::{self, Blind, Params}, + Coeff, Error, Polynomial, +}; +use super::Proof; + +use crate::arithmetic::{ + eval_polynomial, get_challenge_scalar, kate_division, parallelize, Challenge, Curve, + CurveAffine, Field, +}; +use crate::plonk::hash_point; +use crate::transcript::Hasher; + +impl Proof { + /// Create a multi-opening proof + pub fn create, HScalar: Hasher>( + params: &Params, + transcript: &mut HBase, + transcript_scalar: &mut HScalar, + points: Vec, + instances: I, + ) -> Result + where + I: IntoIterator< + Item = ( + usize, + Polynomial, + Blind, + C::Scalar, + ), + > + Clone, + { + let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + // Collapse openings at same points together into single openings using + // x_4 challenge. + let mut q_polys: Vec>> = vec![None; points.len()]; + let mut q_blinds = vec![Blind(C::Scalar::zero()); points.len()]; + let mut q_evals: Vec<_> = vec![C::Scalar::zero(); points.len()]; + { + let mut accumulate = + |point_index: usize, new_poly: Polynomial, blind, eval| { + q_polys[point_index] + .as_mut() + .map(|poly| { + parallelize(poly, |q, start| { + for (q, a) in q.iter_mut().zip(new_poly[start..].iter()) { + *q *= &x_4; + *q += a; + } + }); + }) + .or_else(|| { + q_polys[point_index] = Some(new_poly.clone()); + Some(()) + }); + q_blinds[point_index] *= x_4; + q_blinds[point_index] += blind; + q_evals[point_index] *= &x_4; + q_evals[point_index] += &eval; + }; + + for instance in instances.clone() { + accumulate( + instance.0, // point_index, + instance.1, // poly, + instance.2, // blind, + instance.3, // eval + ); + } + } + + let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + let mut f_poly: Option> = None; + for (point_index, &point) in points.iter().enumerate() { + let mut poly = q_polys[point_index].as_ref().unwrap().clone(); + poly[0] -= &q_evals[point_index]; + // TODO: change kate_division interface? + let mut poly = kate_division(&poly[..], point); + poly.push(C::Scalar::zero()); + let poly = Polynomial { + values: poly, + _marker: PhantomData, + }; + + f_poly = f_poly + .map(|mut f_poly| { + parallelize(&mut f_poly, |q, start| { + for (q, a) in q.iter_mut().zip(poly[start..].iter()) { + *q *= &x_5; + *q += a; + } + }); + f_poly + }) + .or_else(|| Some(poly)); + } + + let f_poly = f_poly.unwrap(); + let mut f_blind = Blind(C::Scalar::random()); + let mut f_commitment = params.commit(&f_poly, f_blind).to_affine(); + + let (opening, q_evals) = loop { + let mut transcript = transcript.clone(); + let mut transcript_scalar = transcript_scalar.clone(); + hash_point(&mut transcript, &f_commitment).unwrap(); + + let x_6: C::Scalar = + get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + let mut q_evals = vec![C::Scalar::zero(); points.len()]; + + for (point_index, _) in points.iter().enumerate() { + q_evals[point_index] = + eval_polynomial(&q_polys[point_index].as_ref().unwrap(), x_6); + } + + for eval in q_evals.iter() { + transcript_scalar.absorb(*eval); + } + + let transcript_scalar_point = + C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); + transcript.absorb(transcript_scalar_point); + + let x_7: C::Scalar = + get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + let mut f_blind_dup = f_blind; + let mut f_poly = f_poly.clone(); + for (point_index, _) in points.iter().enumerate() { + f_blind_dup *= x_7; + f_blind_dup += q_blinds[point_index]; + + parallelize(&mut f_poly, |f, start| { + for (f, a) in f + .iter_mut() + .zip(q_polys[point_index].as_ref().unwrap()[start..].iter()) + { + *f *= &x_7; + *f += a; + } + }); + } + + if let Ok(opening) = + commitment::Proof::create(¶ms, &mut transcript, &f_poly, f_blind_dup, x_6) + { + break (opening, q_evals); + } else { + f_blind += C::Scalar::one(); + f_commitment = (f_commitment + params.h).to_affine(); + } + }; + + Ok(Proof { + q_evals, + f_commitment, + opening, + }) + } +} diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs new file mode 100644 index 0000000..ea6f9a6 --- /dev/null +++ b/src/poly/multiopen/verifier.rs @@ -0,0 +1,92 @@ +use super::super::commitment::{Params, MSM}; +use super::Proof; +use crate::arithmetic::{get_challenge_scalar, Challenge, CurveAffine, Field}; +use crate::plonk::hash_point; +use crate::transcript::Hasher; + +impl<'a, C: CurveAffine> Proof { + /// Verify a multi-opening proof + pub fn verify, HScalar: Hasher>( + &self, + params: &'a Params, + transcript: &mut HBase, + transcript_scalar: &mut HScalar, + points: Vec, + instances: I, + ) -> (C::Scalar, MSM<'a, C>, C::Scalar) + where + I: IntoIterator + Clone, + { + // Sample x_4 for compressing openings at the same points together + let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + // Compress the commitments and expected evaluations at x_3 together + // using the challenge x_4 + let mut q_commitments: Vec<_> = vec![params.empty_msm(); points.len()]; + let mut q_evals: Vec<_> = vec![C::Scalar::zero(); points.len()]; + { + let mut accumulate = |point_index: usize, new_commitment, eval| { + q_commitments[point_index].scale(x_4); + q_commitments[point_index].add_term(C::Scalar::one(), new_commitment); + q_evals[point_index] *= &x_4; + q_evals[point_index] += &eval; + }; + + for instance in instances.clone() { + accumulate( + instance.0, // point_index, + instance.1, // commitment, + instance.2, // eval, + ); + } + } + + // Sample a challenge x_5 for keeping the multi-point quotient + // polynomial terms linearly independent. + let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + // Obtain the commitment to the multi-point quotient polynomial f(X). + hash_point(transcript, &self.f_commitment).unwrap(); + + // Sample a challenge x_6 for checking that f(X) was committed to + // correctly. + let x_6: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + for eval in self.q_evals.iter() { + transcript_scalar.absorb(*eval); + } + + let transcript_scalar_point = + C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); + transcript.absorb(transcript_scalar_point); + + // We can compute the expected msm_eval at x_6 using the q_evals provided + // by the prover and from x_5 + let mut msm_eval = C::Scalar::zero(); + for (point_index, point) in points.iter().enumerate() { + let mut eval = self.q_evals[point_index]; + + eval = eval - &q_evals[point_index]; + eval = eval * &(x_6 - &point).invert().unwrap(); + + msm_eval *= &x_5; + msm_eval += &eval; + } + + // Sample a challenge x_7 that we will use to collapse the openings of + // the various remaining polynomials at x_6 together. + let x_7: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + + // Compute the final commitment that has to be opened + let mut commitment_msm = params.empty_msm(); + commitment_msm.add_term(C::Scalar::one(), self.f_commitment); + for (point_index, _) in points.iter().enumerate() { + commitment_msm.scale(x_7); + commitment_msm.add_msm(&q_commitments[point_index]); + msm_eval *= &x_7; + msm_eval += &self.q_evals[point_index]; + } + + (x_6, commitment_msm, msm_eval) + } +} From 07e2d390a90bf4d639b4f6f56162e9adbd87d5ac Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 30 Sep 2020 08:45:24 +0800 Subject: [PATCH 02/30] Introduce Lagrange interpolation method in arithmetic.rs --- src/arithmetic.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index d14d4d3..58489e4 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -430,3 +430,40 @@ fn log2_floor(num: usize) -> u32 { pow } + +/// Returns coefficients of an n - 1 degree polynomial given a set of n points and their evaluations +pub fn interpolate(points: Vec, evals: Vec) -> Vec { + assert_eq!(points.len(), evals.len()); + if points.len() == 1 { + // Constant polynomial + return vec![evals[0]]; + } else { + let mut interpolation_polys = vec![]; + for (j, x_j) in points.iter().enumerate() { + let mut tmp = vec![F::one()]; + for (k, x_k) in points.iter().enumerate() { + if k != j { + // Compute (x_j - x_k)^(-1) + let denom = (*x_j - x_k).invert().unwrap(); + let factor = [-denom * x_k, denom]; + let mut product = vec![F::zero(); tmp.len() + factor.len() - 1]; + for (i, a) in tmp.iter().enumerate() { + for (j, b) in factor.iter().enumerate() { + product[i + j] += *a * b; + } + } + tmp = product; + } + } + interpolation_polys.push(tmp); + } + let mut final_poly = vec![F::zero(); points.len()]; + for (interpolation_poly, evaluation) in interpolation_polys.into_iter().zip(evals.iter()) { + for (final_coeff, interpolation_coeff) in final_poly.iter_mut().zip(interpolation_poly) + { + *final_coeff += interpolation_coeff * evaluation; + } + } + final_poly + } +} From 1e21c08acd6621cae2b54ed1bac85ead3884cf44 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 7 Oct 2020 21:54:00 +0800 Subject: [PATCH 03/30] Implement Ord and PartialOrd for Field --- src/arithmetic/fields.rs | 2 ++ src/arithmetic/fields/fp.rs | 22 ++++++++++++++++++++++ src/arithmetic/fields/fq.rs | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index 45b9f7b..67c3043 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -36,6 +36,8 @@ pub trait Field: + for<'a> SubAssign<&'a Self> + PartialEq + Eq + + PartialOrd + + Ord + ConditionallySelectable + ConstantTimeEq + Group diff --git a/src/arithmetic/fields/fp.rs b/src/arithmetic/fields/fp.rs index f85b75e..fd02695 100644 --- a/src/arithmetic/fields/fp.rs +++ b/src/arithmetic/fields/fp.rs @@ -62,6 +62,28 @@ impl PartialEq for Fp { } } +impl std::cmp::Ord for Fp { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let left = self.to_bytes(); + let right = other.to_bytes(); + for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() { + let tmp = left_byte.cmp(right_byte); + if let std::cmp::Ordering::Equal = tmp { + continue; + } else { + return tmp; + } + } + std::cmp::Ordering::Equal + } +} + +impl std::cmp::PartialOrd for Fp { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + impl ConditionallySelectable for Fp { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Fp([ diff --git a/src/arithmetic/fields/fq.rs b/src/arithmetic/fields/fq.rs index 1005263..0f99629 100644 --- a/src/arithmetic/fields/fq.rs +++ b/src/arithmetic/fields/fq.rs @@ -62,6 +62,28 @@ impl PartialEq for Fq { } } +impl std::cmp::Ord for Fq { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let left = self.to_bytes(); + let right = other.to_bytes(); + for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() { + let tmp = left_byte.cmp(right_byte); + if let std::cmp::Ordering::Equal = tmp { + continue; + } else { + return tmp; + } + } + std::cmp::Ordering::Equal + } +} + +impl std::cmp::PartialOrd for Fq { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + impl ConditionallySelectable for Fq { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Fq([ From cbe4415870679c2da0c8e97195fce7c0ca3d6019 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 7 Oct 2020 21:57:54 +0800 Subject: [PATCH 04/30] Introduce Query and CommitmentData structs for multiopen --- src/poly/multiopen.rs | 24 ++++++++++++++++++++++++ src/poly/multiopen/prover.rs | 8 ++++++++ src/poly/multiopen/verifier.rs | 7 +++++++ 3 files changed, 39 insertions(+) diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs index 5850fca..91a1a96 100644 --- a/src/poly/multiopen.rs +++ b/src/poly/multiopen.rs @@ -21,3 +21,27 @@ pub struct Proof { /// Commitment proof pub opening: commitment::Proof, } + +/// A polynomial query at a point +#[derive(Debug, Clone)] +pub struct ProverQuery<'a, C: CurveAffine> { + /// point at which polynomial is queried + pub point: C::Scalar, + /// coefficients of polynomial + pub poly: &'a Polynomial, + /// blinding factor of polynomial + pub blind: commitment::Blind, + /// evaluation of polynomial at query point + pub eval: C::Scalar, +} + +/// A polynomial query at a point +#[derive(Debug, Clone)] +pub struct VerifierQuery<'a, C: CurveAffine> { + /// point at which polynomial is queried + pub point: C::Scalar, + /// commitment to polynomial + pub commitment: &'a C, + /// evaluation of polynomial at query point + pub eval: C::Scalar, +} diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 48390b0..7a5b33d 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -13,6 +13,14 @@ use crate::arithmetic::{ use crate::plonk::hash_point; use crate::transcript::Hasher; +#[derive(Debug, Clone)] +struct CommitmentData { + set_index: usize, + blind: Blind, + point_indices: Vec, + evals: Vec, +} + impl Proof { /// Create a multi-opening proof pub fn create, HScalar: Hasher>( diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index ea6f9a6..5f78363 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -4,6 +4,13 @@ use crate::arithmetic::{get_challenge_scalar, Challenge, CurveAffine, Field}; use crate::plonk::hash_point; use crate::transcript::Hasher; +#[derive(Debug, Clone)] +struct CommitmentData { + set_index: usize, + point_indices: Vec, + evals: Vec, +} + impl<'a, C: CurveAffine> Proof { /// Verify a multi-opening proof pub fn verify, HScalar: Hasher>( From 9378d0cc706e27e438f45c6aeb7a82e01fad1782 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 7 Oct 2020 21:59:55 +0800 Subject: [PATCH 05/30] Define construct_intermediate_sets() helper method for multiopen prover and verifier --- src/poly/multiopen/prover.rs | 127 ++++++++++++++++++++++++++++++++- src/poly/multiopen/verifier.rs | 124 +++++++++++++++++++++++++++++++- 2 files changed, 249 insertions(+), 2 deletions(-) diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 7a5b33d..803a727 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -4,7 +4,7 @@ use super::super::{ commitment::{self, Blind, Params}, Coeff, Error, Polynomial, }; -use super::Proof; +use super::{Proof, ProverQuery}; use crate::arithmetic::{ eval_polynomial, get_challenge_scalar, kate_division, parallelize, Challenge, Curve, @@ -12,6 +12,7 @@ use crate::arithmetic::{ }; use crate::plonk::hash_point; use crate::transcript::Hasher; +use std::collections::{BTreeMap, BTreeSet}; #[derive(Debug, Clone)] struct CommitmentData { @@ -171,3 +172,127 @@ impl Proof { }) } } + +// For multiopen prover: Construct intermediate representations relating polynomials to sets of points by index +fn construct_intermediate_sets<'a, C: CurveAffine, I>( + queries: I, +) -> ( + Vec<(&'a Polynomial, CommitmentData)>, // poly_map + Vec>, // point_sets +) +where + I: IntoIterator> + Clone, +{ + // Construct vec of unique polynomials and corresponding information about their queries + let mut poly_map: Vec<(&'a Polynomial, CommitmentData)> = Vec::new(); + + // Also construct mapping from a unique point to a point_index + let mut point_index_map: BTreeMap = BTreeMap::new(); + + // Construct point_indices which each polynomial is queried at + for query in queries.clone() { + let num_points = point_index_map.len(); + let point_idx = point_index_map.entry(query.point).or_insert(num_points); + + let mut exists = false; + for (existing_poly, existing_commitment_data) in poly_map.iter_mut() { + // Add to CommitmentData for existing commitment in commitment_map + if std::ptr::eq(query.poly, *existing_poly) { + exists = true; + existing_commitment_data.point_indices.push(*point_idx); + } + } + + // Add new poly and CommitmentData to poly_map + if !exists { + let commitment_data = CommitmentData { + set_index: 0, + blind: query.blind, + point_indices: vec![*point_idx], + evals: vec![], + }; + poly_map.push((query.poly, commitment_data)); + } + } + + // Also construct inverse mapping from point_index to the point + let mut inverse_point_index_map: BTreeMap = BTreeMap::new(); + for (&point, &point_index) in point_index_map.iter() { + inverse_point_index_map.insert(point_index, point); + } + + // Construct map of unique ordered point_idx_sets to their set_idx + let mut point_idx_sets: BTreeMap, usize> = BTreeMap::new(); + // Also construct mapping from poly to point_idx_set + let mut poly_set_map: Vec<(&Polynomial, BTreeSet)> = Vec::new(); + + for (poly, commitment_data) in poly_map.iter_mut() { + let mut point_index_set = BTreeSet::new(); + // Note that point_index_set is ordered, unlike point_indices + for &point_index in commitment_data.point_indices.iter() { + point_index_set.insert(point_index); + } + + // Push point_index_set to CommitmentData for the relevant poly + poly_set_map.push((poly, point_index_set.clone())); + + let num_sets = point_idx_sets.len(); + point_idx_sets + .entry(point_index_set.clone()) + .or_insert(num_sets); + } + + // Initialise empty evals vec for each unique poly + for (_, commitment_data) in poly_map.iter_mut() { + let len = commitment_data.point_indices.len(); + commitment_data.evals = vec![C::Scalar::zero(); len]; + } + + // Populate set_index, evals and points for each poly using point_idx_sets + for query in queries.clone() { + // The index of the point at which the poly is queried + let point_index = point_index_map.get(&query.point).unwrap(); + + // The point_index_set at which the poly was queried + let mut point_index_set = BTreeSet::new(); + for (poly, point_idx_set) in poly_set_map.iter() { + if std::ptr::eq(query.poly, *poly) { + point_index_set = point_idx_set.clone(); + } + } + + // The set_index of the point_index_set + let set_index = point_idx_sets.get(&point_index_set).unwrap(); + for (poly, commitment_data) in poly_map.iter_mut() { + if std::ptr::eq(query.poly, *poly) { + commitment_data.set_index = *set_index; + } + } + + let point_index_set: Vec = point_index_set.iter().cloned().collect(); + + // The offset of the point_index in the point_index_set + let point_index_in_set = point_index_set + .iter() + .position(|i| i == point_index) + .unwrap(); + + for (poly, commitment_data) in poly_map.iter_mut() { + if std::ptr::eq(query.poly, *poly) { + // Insert the eval using the ordering of the point_index_set + commitment_data.evals[point_index_in_set] = query.eval; + } + } + } + + // Get actual points in each point set + let mut point_sets: Vec> = vec![Vec::new(); point_idx_sets.len()]; + for (point_idx_set, &set_idx) in point_idx_sets.iter() { + for &point_idx in point_idx_set.iter() { + let point = inverse_point_index_map.get(&point_idx).unwrap(); + point_sets[set_idx].push(*point); + } + } + + (poly_map, point_sets) +} diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 5f78363..17267cb 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -1,8 +1,9 @@ use super::super::commitment::{Params, MSM}; -use super::Proof; +use super::{Proof, VerifierQuery}; use crate::arithmetic::{get_challenge_scalar, Challenge, CurveAffine, Field}; use crate::plonk::hash_point; use crate::transcript::Hasher; +use std::collections::{BTreeMap, BTreeSet}; #[derive(Debug, Clone)] struct CommitmentData { @@ -97,3 +98,124 @@ impl<'a, C: CurveAffine> Proof { (x_6, commitment_msm, msm_eval) } } + +// For multiopen verifier: Construct intermediate representations relating commitments to sets of points by index +fn construct_intermediate_sets<'a, C: CurveAffine, I>( + queries: I, +) -> ( + Vec<(&'a C, CommitmentData)>, // commitment_map + Vec>, // point_sets +) +where + I: IntoIterator> + Clone, +{ + // Construct sets of unique commitments and corresponding information about their queries + let mut commitment_map: Vec<(&'a C, CommitmentData)> = Vec::new(); + + // Also construct mapping from a unique point to a point_index. This defines an ordering on the points. + let mut point_index_map: BTreeMap = BTreeMap::new(); + + // Construct point_indices which each commitment is queried at + for query in queries.clone() { + let num_points = point_index_map.len(); + let point_idx = point_index_map.entry(query.point).or_insert(num_points); + + let mut exists = false; + for (existing_commitment, existing_commitment_data) in commitment_map.iter_mut() { + // Add to CommitmentData for existing commitment in commitment_map + if std::ptr::eq(query.commitment, *existing_commitment) { + exists = true; + existing_commitment_data.point_indices.push(*point_idx); + } + } + + // Add new commitment and CommitmentData to commitment_map + if !exists { + let commitment_data = CommitmentData { + set_index: 0, + point_indices: vec![*point_idx], + evals: vec![], + }; + commitment_map.push((query.commitment, commitment_data)); + } + } + + // Also construct inverse mapping from point_index to the point + let mut inverse_point_index_map: BTreeMap = BTreeMap::new(); + for (&point, &point_index) in point_index_map.iter() { + inverse_point_index_map.insert(point_index, point); + } + + // Construct map of unique ordered point_idx_sets to their set_idx + let mut point_idx_sets: BTreeMap, usize> = BTreeMap::new(); + // Also construct mapping from commitment to point_idx_set + let mut commitment_set_map: Vec<(&'a C, BTreeSet)> = Vec::new(); + + for (commitment, commitment_data) in commitment_map.iter_mut() { + let mut point_index_set = BTreeSet::new(); + // Note that point_index_set is ordered, unlike point_indices + for &point_index in commitment_data.point_indices.iter() { + point_index_set.insert(point_index); + } + + // Push point_index_set to CommitmentData for the relevant commitment + commitment_set_map.push((commitment, point_index_set.clone())); + + let num_sets = point_idx_sets.len(); + point_idx_sets + .entry(point_index_set.clone()) + .or_insert(num_sets); + } + + // Initialise empty evals vec for each unique commitment + for (_, commitment_data) in commitment_map.iter_mut() { + let len = commitment_data.point_indices.len(); + commitment_data.evals = vec![C::Scalar::zero(); len]; + } + + // Populate set_index, evals and points for each commitment using point_idx_sets + for query in queries.clone() { + // The index of the point at which the commitment is queried + let point_index = point_index_map.get(&query.point).unwrap(); + + // The point_index_set at which the commitment was queried + let mut point_index_set = BTreeSet::new(); + for (commitment, point_idx_set) in commitment_set_map.iter() { + if std::ptr::eq(query.commitment, *commitment) { + point_index_set = point_idx_set.clone(); + } + } + // The set_index of the point_index_set + let set_index = point_idx_sets.get(&point_index_set).unwrap(); + for (commitment, commitment_data) in commitment_map.iter_mut() { + if std::ptr::eq(query.commitment, *commitment) { + commitment_data.set_index = *set_index; + } + } + let point_index_set: Vec = point_index_set.iter().cloned().collect(); + + // The offset of the point_index in the point_index_set + let point_index_in_set = point_index_set + .iter() + .position(|i| i == point_index) + .unwrap(); + + for (commitment, commitment_data) in commitment_map.iter_mut() { + if std::ptr::eq(query.commitment, *commitment) { + // Insert the eval using the ordering of the point_index_set + commitment_data.evals[point_index_in_set] = query.eval; + } + } + } + + // Get actual points in each point set + let mut point_sets: Vec> = vec![Vec::new(); point_idx_sets.len()]; + for (point_idx_set, &set_idx) in point_idx_sets.iter() { + for &point_idx in point_idx_set.iter() { + let point = inverse_point_index_map.get(&point_idx).unwrap(); + point_sets[set_idx].push(*point); + } + } + + (commitment_map, point_sets) +} From 6cd74999ffec403a74195ea1de684bef54a38ed1 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 8 Oct 2020 00:13:06 +0800 Subject: [PATCH 06/30] Use ProverQuery and construct_intermediate_sets() in prover --- src/plonk/prover.rs | 143 ++++++++++++++++++----------------- src/poly/multiopen/prover.rs | 141 ++++++++++++++++++---------------- 2 files changed, 150 insertions(+), 134 deletions(-) diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 90e2c22..3e862d2 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -8,7 +8,8 @@ use crate::arithmetic::{ }; use crate::poly::{ commitment::{Blind, Params}, - multiopen, Coeff, LagrangeCoeff, Polynomial, Rotation, + multiopen::{self, ProverQuery}, + LagrangeCoeff, Polynomial, Rotation, }; use crate::transcript::Hasher; @@ -457,53 +458,50 @@ impl Proof { C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); transcript.absorb(transcript_scalar_point); - let mut instances: Vec<( - usize, - Polynomial, - Blind, - C::Scalar, - )> = Vec::with_capacity( - meta.advice_queries.len() - + meta.aux_queries.len() - + meta.fixed_queries.len() - + h_pieces.len() - + permutation_product_polys.len() - + permutation_product_polys.len() - + pk.permutation_polys.len(), - ); + let mut instances: Vec> = Vec::new(); - for (query_index, &(wire, ref at)) in meta.advice_queries.iter().enumerate() { - let point_index = (*meta.rotations.get(at).unwrap()).0; - let poly = advice_polys[wire.0].clone(); - let blind = advice_blinds[wire.0]; - let eval = advice_evals[query_index]; - instances.push((point_index, poly, blind, eval)); + for (query_index, &(wire, at)) in pk.vk.cs.advice_queries.iter().enumerate() { + let point = domain.rotate_omega(x_3, at); + + instances.push(ProverQuery { + point, + poly: &advice_polys[wire.0], + blind: advice_blinds[wire.0], + eval: advice_evals[query_index], + }); } - for (query_index, &(wire, ref at)) in meta.aux_queries.iter().enumerate() { - let point_index = (*meta.rotations.get(at).unwrap()).0; - let poly = aux_polys[wire.0].clone(); - let blind = Blind::default(); - let eval = aux_evals[query_index]; - instances.push((point_index, poly, blind, eval)); + for (query_index, &(wire, at)) in pk.vk.cs.aux_queries.iter().enumerate() { + let point = domain.rotate_omega(x_3, at); + + instances.push(ProverQuery { + point, + poly: &aux_polys[wire.0], + blind: Blind::default(), + eval: aux_evals[query_index], + }); } - for (query_index, &(wire, ref at)) in meta.fixed_queries.iter().enumerate() { - let point_index = (*meta.rotations.get(at).unwrap()).0; - let poly = pk.fixed_polys[wire.0].clone(); - let blind = Blind::default(); - let eval = fixed_evals[query_index]; - instances.push((point_index, poly, blind, eval)); + for (query_index, &(wire, at)) in pk.vk.cs.fixed_queries.iter().enumerate() { + let point = domain.rotate_omega(x_3, at); + + instances.push(ProverQuery { + point, + poly: &pk.fixed_polys[wire.0], + blind: Blind::default(), + eval: fixed_evals[query_index], + }); } // We query the h(X) polynomial at x_3 - let current_index = (*meta.rotations.get(&Rotation::default()).unwrap()).0; - for ((h_poly, h_blind), h_eval) in h_pieces - .into_iter() - .zip(h_blinds.iter()) - .zip(h_evals.iter()) + for ((h_poly, h_blind), h_eval) in h_pieces.iter().zip(h_blinds.iter()).zip(h_evals.iter()) { - instances.push((current_index, h_poly.clone(), *h_blind, *h_eval)); + instances.push(ProverQuery { + point: x_3, + poly: h_poly, + blind: *h_blind, + eval: *h_eval, + }); } // Handle permutation arguments, if any exist @@ -514,7 +512,12 @@ impl Proof { .zip(permutation_product_blinds.iter()) .zip(permutation_product_evals.iter()) { - instances.push((current_index, poly.clone(), *blind, *eval)); + instances.push(ProverQuery { + point: x_3, + poly: poly, + blind: *blind, + eval: *eval, + }); } // Open permutation polynomial commitments at x_3 @@ -524,46 +527,48 @@ impl Proof { .zip(permutation_evals.iter()) .flat_map(|(polys, evals)| polys.iter().zip(evals.iter())) { - instances.push((current_index, poly.clone(), Blind::default(), *eval)); + instances.push(ProverQuery { + point: x_3, + poly: poly, + blind: Blind::default(), + eval: *eval, + }); } - let current_index = (*pk.vk.cs.rotations.get(&Rotation(-1)).unwrap()).0; + let x_3_inv = domain.rotate_omega(x_3, Rotation(-1)); // Open permutation product commitments at \omega^{-1} x_3 for ((poly, blind), eval) in permutation_product_polys .iter() .zip(permutation_product_blinds.iter()) .zip(permutation_product_inv_evals.iter()) { - instances.push((current_index, poly.clone(), *blind, *eval)); + instances.push(ProverQuery { + point: x_3_inv, + poly: poly, + blind: *blind, + eval: *eval, + }); } } - let mut points: Vec = vec![C::Scalar::zero(); meta.rotations.len()]; - for (&row, &point_index) in meta.rotations.iter() { - points[point_index.0] = domain.rotate_omega(x_3, row); + if let Ok(multiopening) = + multiopen::Proof::create(params, &mut transcript, &mut transcript_scalar, instances) + { + Ok(Proof { + advice_commitments, + h_commitments, + permutation_product_commitments, + permutation_product_evals, + permutation_product_inv_evals, + permutation_evals, + advice_evals, + fixed_evals, + aux_evals, + h_evals, + multiopening, + }) + } else { + Err(Error::OpeningError) } - - let multiopening = multiopen::Proof::create( - params, - &mut transcript, - &mut transcript_scalar, - points, - instances, - ) - .unwrap(); - - Ok(Proof { - advice_commitments, - h_commitments, - permutation_product_commitments, - permutation_product_evals, - permutation_product_inv_evals, - permutation_evals, - advice_evals, - fixed_evals, - aux_evals, - h_evals, - multiopening, - }) } } diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 803a727..42060dc 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -1,5 +1,3 @@ -use std::marker::PhantomData; - use super::super::{ commitment::{self, Blind, Params}, Coeff, Error, Polynomial, @@ -7,12 +5,13 @@ use super::super::{ use super::{Proof, ProverQuery}; use crate::arithmetic::{ - eval_polynomial, get_challenge_scalar, kate_division, parallelize, Challenge, Curve, - CurveAffine, Field, + eval_polynomial, get_challenge_scalar, interpolate, kate_division, parallelize, Challenge, + Curve, CurveAffine, Field, }; use crate::plonk::hash_point; use crate::transcript::Hasher; use std::collections::{BTreeMap, BTreeSet}; +use std::marker::PhantomData; #[derive(Debug, Clone)] struct CommitmentData { @@ -24,88 +23,101 @@ struct CommitmentData { impl Proof { /// Create a multi-opening proof - pub fn create, HScalar: Hasher>( + pub fn create<'a, I, HBase: Hasher, HScalar: Hasher>( params: &Params, transcript: &mut HBase, transcript_scalar: &mut HScalar, - points: Vec, - instances: I, + queries: I, ) -> Result where - I: IntoIterator< - Item = ( - usize, - Polynomial, - Blind, - C::Scalar, - ), - > + Clone, + I: IntoIterator> + Clone, { let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - // Collapse openings at same points together into single openings using - // x_4 challenge. - let mut q_polys: Vec>> = vec![None; points.len()]; - let mut q_blinds = vec![Blind(C::Scalar::zero()); points.len()]; - let mut q_evals: Vec<_> = vec![C::Scalar::zero(); points.len()]; - { - let mut accumulate = - |point_index: usize, new_poly: Polynomial, blind, eval| { - q_polys[point_index] - .as_mut() - .map(|poly| { - parallelize(poly, |q, start| { - for (q, a) in q.iter_mut().zip(new_poly[start..].iter()) { - *q *= &x_4; - *q += a; - } - }); - }) - .or_else(|| { - q_polys[point_index] = Some(new_poly.clone()); - Some(()) - }); - q_blinds[point_index] *= x_4; - q_blinds[point_index] += blind; - q_evals[point_index] *= &x_4; - q_evals[point_index] += &eval; - }; + let (poly_map, point_sets) = construct_intermediate_sets::<'a, C, I>(queries); - for instance in instances.clone() { + // Collapse openings at same point sets together into single openings using + // x_4 challenge. + let mut q_polys: Vec>> = vec![None; point_sets.len()]; + let mut q_blinds = vec![Blind(C::Scalar::zero()); point_sets.len()]; + let mut q_eval_sets: Vec> = vec![Vec::new(); point_sets.len()]; + for (set_idx, point_set) in point_sets.iter().enumerate() { + q_eval_sets[set_idx] = vec![C::Scalar::zero(); point_set.len()]; + } + + { + let mut accumulate = |set_idx: usize, + new_poly: &Polynomial, + blind: Blind, + evals: Vec| { + q_polys[set_idx] + .as_mut() + .map(|poly| { + parallelize(poly, |q, start| { + for (q, a) in q.iter_mut().zip(new_poly[start..].iter()) { + *q *= &x_4; + *q += a; + } + }); + }) + .or_else(|| { + q_polys[set_idx] = Some(new_poly.clone()); + Some(()) + }); + q_blinds[set_idx] *= x_4; + q_blinds[set_idx] += blind; + for (eval_idx, &eval) in evals.iter().enumerate() { + q_eval_sets[set_idx][eval_idx] *= &x_4; + q_eval_sets[set_idx][eval_idx] += &eval; + } + }; + + for (poly, commitment_data) in poly_map { accumulate( - instance.0, // point_index, - instance.1, // poly, - instance.2, // blind, - instance.3, // eval + commitment_data.set_index, // set_idx, + &poly, // poly, + commitment_data.blind, // blind, + commitment_data.evals.to_vec(), // evals ); } } let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + // Interpolate polynomial for evaluations at each set + let mut r_polys: Vec> = Vec::with_capacity(point_sets.len()); + for (points, evals) in point_sets.iter().zip(q_eval_sets.iter()) { + r_polys.push(interpolate(points.clone(), evals.clone())); + } + let mut f_poly: Option> = None; - for (point_index, &point) in points.iter().enumerate() { - let mut poly = q_polys[point_index].as_ref().unwrap().clone(); - poly[0] -= &q_evals[point_index]; + for (set_idx, point_set) in point_sets.clone().iter().enumerate() { + let mut poly = q_polys[set_idx].as_ref().unwrap().clone(); + for (coeff_idx, coeff) in r_polys[set_idx].iter().enumerate() { + poly[coeff_idx] -= coeff; + } // TODO: change kate_division interface? - let mut poly = kate_division(&poly[..], point); - poly.push(C::Scalar::zero()); - let poly = Polynomial { - values: poly, - _marker: PhantomData, - }; + let mut tmp_poly = poly.clone(); + for point in point_set { + let mut poly = kate_division(&tmp_poly[..], *point); + poly.push(C::Scalar::zero()); + tmp_poly = Polynomial { + values: poly, + _marker: PhantomData, + }; + } f_poly = f_poly .map(|mut f_poly| { parallelize(&mut f_poly, |q, start| { - for (q, a) in q.iter_mut().zip(poly[start..].iter()) { + for (q, a) in q.iter_mut().zip(tmp_poly[start..].iter()) { *q *= &x_5; *q += a; } }); f_poly }) - .or_else(|| Some(poly)); + .or_else(|| Some(tmp_poly)); } let f_poly = f_poly.unwrap(); @@ -120,11 +132,10 @@ impl Proof { let x_6: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - let mut q_evals = vec![C::Scalar::zero(); points.len()]; + let mut q_evals = vec![C::Scalar::zero(); point_sets.len()]; - for (point_index, _) in points.iter().enumerate() { - q_evals[point_index] = - eval_polynomial(&q_polys[point_index].as_ref().unwrap(), x_6); + for (set_idx, _) in point_sets.iter().enumerate() { + q_evals[set_idx] = eval_polynomial(&q_polys[set_idx].as_ref().unwrap(), x_6); } for eval in q_evals.iter() { @@ -140,14 +151,14 @@ impl Proof { let mut f_blind_dup = f_blind; let mut f_poly = f_poly.clone(); - for (point_index, _) in points.iter().enumerate() { + for (set_idx, _) in point_sets.iter().enumerate() { f_blind_dup *= x_7; - f_blind_dup += q_blinds[point_index]; + f_blind_dup += q_blinds[set_idx]; parallelize(&mut f_poly, |f, start| { for (f, a) in f .iter_mut() - .zip(q_polys[point_index].as_ref().unwrap()[start..].iter()) + .zip(q_polys[set_idx].as_ref().unwrap()[start..].iter()) { *f *= &x_7; *f += a; From 97873fa6ea48c359528f1249d337c1d180eb799b Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 8 Oct 2020 00:20:00 +0800 Subject: [PATCH 07/30] Use VerifierQuery and construct_intermediate_sets() in verifier --- src/poly/multiopen/verifier.rs | 79 ++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 17267cb..9cf8ea1 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -1,6 +1,11 @@ -use super::super::commitment::{Params, MSM}; +use super::super::{ + commitment::{Guard, Params, MSM}, + Error, +}; use super::{Proof, VerifierQuery}; -use crate::arithmetic::{get_challenge_scalar, Challenge, CurveAffine, Field}; +use crate::arithmetic::{ + eval_polynomial, get_challenge_scalar, interpolate, Challenge, CurveAffine, Field, +}; use crate::plonk::hash_point; use crate::transcript::Hasher; use std::collections::{BTreeMap, BTreeSet}; @@ -19,32 +24,39 @@ impl<'a, C: CurveAffine> Proof { params: &'a Params, transcript: &mut HBase, transcript_scalar: &mut HScalar, - points: Vec, - instances: I, - ) -> (C::Scalar, MSM<'a, C>, C::Scalar) + queries: I, + msm: MSM<'a, C>, + ) -> Result, Error> where - I: IntoIterator + Clone, + I: IntoIterator> + Clone, { // Sample x_4 for compressing openings at the same points together let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - // Compress the commitments and expected evaluations at x_3 together + let (commitment_map, point_sets) = construct_intermediate_sets::<'a, C, I>(queries.clone()); + + // Compress the commitments and expected evaluations at x_3 together. // using the challenge x_4 - let mut q_commitments: Vec<_> = vec![params.empty_msm(); points.len()]; - let mut q_evals: Vec<_> = vec![C::Scalar::zero(); points.len()]; + let mut q_commitments: Vec<_> = vec![params.empty_msm(); point_sets.len()]; + let mut q_eval_sets: Vec> = vec![Vec::new(); point_sets.len()]; + for (set_idx, point_set) in point_sets.iter().enumerate() { + q_eval_sets[set_idx] = vec![C::Scalar::zero(); point_set.len()]; + } { - let mut accumulate = |point_index: usize, new_commitment, eval| { - q_commitments[point_index].scale(x_4); - q_commitments[point_index].add_term(C::Scalar::one(), new_commitment); - q_evals[point_index] *= &x_4; - q_evals[point_index] += &eval; + let mut accumulate = |set_idx: usize, new_commitment, evals: Vec| { + q_commitments[set_idx].scale(x_4); + q_commitments[set_idx].add_term(C::Scalar::one(), new_commitment); + for (eval_idx, &eval) in evals.iter().enumerate() { + q_eval_sets[set_idx][eval_idx] *= &x_4; + q_eval_sets[set_idx][eval_idx] += &eval; + } }; - for instance in instances.clone() { + for (commitment, commitment_data) in commitment_map { accumulate( - instance.0, // point_index, - instance.1, // commitment, - instance.2, // eval, + commitment_data.set_index, // set_idx, + *commitment, // commitment, + commitment_data.evals.to_vec(), // evals ); } } @@ -68,14 +80,25 @@ impl<'a, C: CurveAffine> Proof { C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); transcript.absorb(transcript_scalar_point); + // Interpolate polynomial for evaluations at each set + let mut r_evals = vec![C::Scalar::zero(); point_sets.len()]; + let mut r_polys: Vec> = Vec::with_capacity(point_sets.len()); + for (points, evals) in point_sets.clone().iter().zip(q_eval_sets.clone().iter()) { + r_polys.push(interpolate(points.clone(), evals.clone())); + } + 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 // by the prover and from x_5 let mut msm_eval = C::Scalar::zero(); - for (point_index, point) in points.iter().enumerate() { - let mut eval = self.q_evals[point_index]; - - eval = eval - &q_evals[point_index]; - eval = eval * &(x_6 - &point).invert().unwrap(); + for (set_idx, points) in point_sets.iter().enumerate() { + let mut eval = self.q_evals[set_idx]; + eval -= &r_evals[set_idx]; + for point in points { + eval = eval * &(x_6 - &point).invert().unwrap(); + } msm_eval *= &x_5; msm_eval += &eval; @@ -88,14 +111,16 @@ impl<'a, C: CurveAffine> Proof { // Compute the final commitment that has to be opened let mut commitment_msm = params.empty_msm(); commitment_msm.add_term(C::Scalar::one(), self.f_commitment); - for (point_index, _) in points.iter().enumerate() { + for (set_idx, _) in point_sets.iter().enumerate() { commitment_msm.scale(x_7); - commitment_msm.add_msm(&q_commitments[point_index]); + commitment_msm.add_msm(&q_commitments[set_idx]); msm_eval *= &x_7; - msm_eval += &self.q_evals[point_index]; + msm_eval += &self.q_evals[set_idx]; } - (x_6, commitment_msm, msm_eval) + // Verify the opening proof + self.opening + .verify(params, msm, transcript, x_6, commitment_msm, msm_eval) } } From 5181ca56f196c3ea29309ac295e437edff8e1ab1 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 8 Oct 2020 00:21:32 +0800 Subject: [PATCH 08/30] Bind &[pubinput] in plonk test_proving() --- src/plonk.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/plonk.rs b/src/plonk.rs index 45238c3..4dfd777 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -408,9 +408,10 @@ fn test_proving() { ) .expect("proof generation should not fail"); + let pubinput_slice = &[pubinput]; let msm = params.empty_msm(); let guard = proof - .verify::, DummyHash>(¶ms, pk.get_vk(), msm, &[pubinput]) + .verify::, DummyHash>(¶ms, pk.get_vk(), msm, pubinput_slice) .unwrap(); { let msm = guard.clone().use_challenges(); @@ -424,7 +425,7 @@ fn test_proving() { let msm = guard.clone().use_challenges(); assert!(msm.clone().eval()); let guard = proof - .verify::, DummyHash>(¶ms, pk.get_vk(), msm, &[pubinput]) + .verify::, DummyHash>(¶ms, pk.get_vk(), msm, pubinput_slice) .unwrap(); { let msm = guard.clone().use_challenges(); From 89fd6e4d4476b1cca64d7394bcc4e63f7752fcc0 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 8 Oct 2020 12:15:46 +0800 Subject: [PATCH 09/30] Use map_err() when handling multiopen::Proof::create() Co-authored-by: Daira Hopwood --- src/plonk/prover.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 3e862d2..10c3fe4 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -551,24 +551,22 @@ impl Proof { } } - if let Ok(multiopening) = + let multiopening = multiopen::Proof::create(params, &mut transcript, &mut transcript_scalar, instances) - { - Ok(Proof { - advice_commitments, - h_commitments, - permutation_product_commitments, - permutation_product_evals, - permutation_product_inv_evals, - permutation_evals, - advice_evals, - fixed_evals, - aux_evals, - h_evals, - multiopening, - }) - } else { - Err(Error::OpeningError) - } + .map_err(|_| Error::OpeningError)?; + + Ok(Proof { + advice_commitments, + h_commitments, + permutation_product_commitments, + permutation_product_evals, + permutation_product_inv_evals, + permutation_evals, + advice_evals, + fixed_evals, + aux_evals, + h_evals, + multiopening, + }) } } From 6f6378b2eade830a0fded6bd51c3c636f2c7412a Mon Sep 17 00:00:00 2001 From: ying tong Date: Thu, 8 Oct 2020 12:23:16 +0800 Subject: [PATCH 10/30] More idiomatic implementation of Ord for Field Co-authored-by: str4d --- src/arithmetic/fields/fp.rs | 17 ++++++++--------- src/arithmetic/fields/fq.rs | 17 ++++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/arithmetic/fields/fp.rs b/src/arithmetic/fields/fp.rs index fd02695..1b99f04 100644 --- a/src/arithmetic/fields/fp.rs +++ b/src/arithmetic/fields/fp.rs @@ -66,15 +66,14 @@ impl std::cmp::Ord for Fp { fn cmp(&self, other: &Self) -> std::cmp::Ordering { let left = self.to_bytes(); let right = other.to_bytes(); - for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() { - let tmp = left_byte.cmp(right_byte); - if let std::cmp::Ordering::Equal = tmp { - continue; - } else { - return tmp; - } - } - std::cmp::Ordering::Equal + left.iter() + .zip(right.iter()) + .rev() + .find_map(|(left_byte, right_byte)| match left_byte.cmp(right_byte) { + std::cmp::Ordering::Equal => None, + res => Some(res), + }) + .unwrap_or(std::cmp::Ordering::Equal) } } diff --git a/src/arithmetic/fields/fq.rs b/src/arithmetic/fields/fq.rs index 0f99629..11026de 100644 --- a/src/arithmetic/fields/fq.rs +++ b/src/arithmetic/fields/fq.rs @@ -66,15 +66,14 @@ impl std::cmp::Ord for Fq { fn cmp(&self, other: &Self) -> std::cmp::Ordering { let left = self.to_bytes(); let right = other.to_bytes(); - for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() { - let tmp = left_byte.cmp(right_byte); - if let std::cmp::Ordering::Equal = tmp { - continue; - } else { - return tmp; - } - } - std::cmp::Ordering::Equal + left.iter() + .zip(right.iter()) + .rev() + .find_map(|(left_byte, right_byte)| match left_byte.cmp(right_byte) { + std::cmp::Ordering::Equal => None, + res => Some(res), + }) + .unwrap_or(std::cmp::Ordering::Equal) } } From 1441193de167df868f17678741273a7e30e86557 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 8 Oct 2020 12:48:40 +0800 Subject: [PATCH 11/30] Refactoring from initial code review Co-authored-by: ebfull --- src/arithmetic.rs | 2 +- src/poly/multiopen.rs | 12 ++--- src/poly/multiopen/prover.rs | 88 ++++++++++++++-------------------- src/poly/multiopen/verifier.rs | 2 +- 4 files changed, 43 insertions(+), 61 deletions(-) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 58489e4..100e962 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -432,7 +432,7 @@ fn log2_floor(num: usize) -> u32 { } /// Returns coefficients of an n - 1 degree polynomial given a set of n points and their evaluations -pub fn interpolate(points: Vec, evals: Vec) -> Vec { +pub fn interpolate(points: &[F], evals: &[F]) -> Vec { assert_eq!(points.len(), evals.len()); if points.len() == 1 { // Constant polynomial diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs index 91a1a96..ad702bd 100644 --- a/src/poly/multiopen.rs +++ b/src/poly/multiopen.rs @@ -12,14 +12,14 @@ mod verifier; /// This is a multi-point opening proof used in the polynomial commitment scheme opening. #[derive(Debug, Clone)] pub struct Proof { - /// A vector of evaluations at each set of query points - pub q_evals: Vec, + // A vector of evaluations at each set of query points + q_evals: Vec, - /// Commitment to final polynomial - pub f_commitment: C, + // Commitment to final polynomial + f_commitment: C, - /// Commitment proof - pub opening: commitment::Proof, + // Commitment proof + opening: commitment::Proof, } /// A polynomial query at a point diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 42060dc..f35376f 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -84,41 +84,31 @@ impl Proof { let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - // Interpolate polynomial for evaluations at each set - let mut r_polys: Vec> = Vec::with_capacity(point_sets.len()); - for (points, evals) in point_sets.iter().zip(q_eval_sets.iter()) { - r_polys.push(interpolate(points.clone(), evals.clone())); - } - - let mut f_poly: Option> = None; - for (set_idx, point_set) in point_sets.clone().iter().enumerate() { - let mut poly = q_polys[set_idx].as_ref().unwrap().clone(); - for (coeff_idx, coeff) in r_polys[set_idx].iter().enumerate() { - poly[coeff_idx] -= coeff; - } - // TODO: change kate_division interface? - let mut tmp_poly = poly.clone(); - for point in point_set { - let mut poly = kate_division(&tmp_poly[..], *point); - poly.push(C::Scalar::zero()); - tmp_poly = Polynomial { + let f_poly = point_sets + .iter() + .zip(q_eval_sets.iter()) + .zip(q_polys.iter()) + .fold(None, |f_poly, ((points, evals), poly)| { + let mut poly = poly.clone()?.values; + // TODO: makes implicit asssumption that poly degree is smaller than interpolation poly degree + for (p, r) in poly.iter_mut().zip(interpolate(points, evals)) { + *p -= &r; + } + let mut poly = points + .iter() + .fold(poly, |poly, point| kate_division(&poly, *point)); + poly.resize(params.n as usize, C::Scalar::zero()); + let poly = Polynomial { values: poly, _marker: PhantomData, }; - } - f_poly = f_poly - .map(|mut f_poly| { - parallelize(&mut f_poly, |q, start| { - for (q, a) in q.iter_mut().zip(tmp_poly[start..].iter()) { - *q *= &x_5; - *q += a; - } - }); - f_poly - }) - .or_else(|| Some(tmp_poly)); - } + if f_poly.is_none() { + Some(poly) + } else { + f_poly.map(|f_poly| f_poly * x_5 + &poly) + } + }); let f_poly = f_poly.unwrap(); let mut f_blind = Blind(C::Scalar::random()); @@ -132,11 +122,10 @@ impl Proof { let x_6: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - let mut q_evals = vec![C::Scalar::zero(); point_sets.len()]; - - for (set_idx, _) in point_sets.iter().enumerate() { - q_evals[set_idx] = eval_polynomial(&q_polys[set_idx].as_ref().unwrap(), x_6); - } + let q_evals: Vec = q_polys + .iter() + .map(|poly| eval_polynomial(poly.as_ref().unwrap(), x_6)) + .collect(); for eval in q_evals.iter() { transcript_scalar.absorb(*eval); @@ -149,25 +138,18 @@ impl Proof { let x_7: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - let mut f_blind_dup = f_blind; - let mut f_poly = f_poly.clone(); - for (set_idx, _) in point_sets.iter().enumerate() { - f_blind_dup *= x_7; - f_blind_dup += q_blinds[set_idx]; - - parallelize(&mut f_poly, |f, start| { - for (f, a) in f - .iter_mut() - .zip(q_polys[set_idx].as_ref().unwrap()[start..].iter()) - { - *f *= &x_7; - *f += a; - } - }); - } + let (f_poly, f_blind_try) = q_polys.iter().zip(q_blinds.iter()).fold( + (f_poly.clone(), f_blind), + |(f_poly, f_blind), (poly, blind)| { + ( + f_poly * x_7 + &poly.clone().unwrap(), + Blind((f_blind.0 * &x_7) + &blind.0), + ) + }, + ); if let Ok(opening) = - commitment::Proof::create(¶ms, &mut transcript, &f_poly, f_blind_dup, x_6) + commitment::Proof::create(¶ms, &mut transcript, &f_poly, f_blind_try, x_6) { break (opening, q_evals); } else { diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 9cf8ea1..6027609 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -84,7 +84,7 @@ impl<'a, C: CurveAffine> Proof { let mut r_evals = vec![C::Scalar::zero(); point_sets.len()]; let mut r_polys: Vec> = Vec::with_capacity(point_sets.len()); for (points, evals) in point_sets.clone().iter().zip(q_eval_sets.clone().iter()) { - r_polys.push(interpolate(points.clone(), evals.clone())); + r_polys.push(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); From 088118cc5de926b6ba46fbb6df2de96745a5deab Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 8 Oct 2020 22:59:55 +0800 Subject: [PATCH 12/30] Refactor lagrange_interpolate() method Co-authored-by: daira Co-authored-by: str4d --- src/arithmetic.rs | 22 +++++++++++++--------- src/poly/multiopen/prover.rs | 6 +++--- src/poly/multiopen/verifier.rs | 6 +++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 100e962..010cf47 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -432,7 +432,7 @@ fn log2_floor(num: usize) -> u32 { } /// Returns coefficients of an n - 1 degree polynomial given a set of n points and their evaluations -pub fn interpolate(points: &[F], evals: &[F]) -> Vec { +pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { assert_eq!(points.len(), evals.len()); if points.len() == 1 { // Constant polynomial @@ -440,19 +440,23 @@ pub fn interpolate(points: &[F], evals: &[F]) -> Vec { } else { let mut interpolation_polys = vec![]; for (j, x_j) in points.iter().enumerate() { - let mut tmp = vec![F::one()]; + let mut tmp: Vec = Vec::with_capacity(points.len() + 1); + let mut product = Vec::with_capacity(points.len() + 1); + tmp.push(F::one()); for (k, x_k) in points.iter().enumerate() { if k != j { // Compute (x_j - x_k)^(-1) let denom = (*x_j - x_k).invert().unwrap(); - let factor = [-denom * x_k, denom]; - let mut product = vec![F::zero(); tmp.len() + factor.len() - 1]; - for (i, a) in tmp.iter().enumerate() { - for (j, b) in factor.iter().enumerate() { - product[i + j] += *a * b; - } + product.resize(tmp.len() + 1, F::zero()); + for (i, (a, b)) in tmp + .iter() + .chain(std::iter::once(&F::zero())) + .zip(std::iter::once(&F::zero()).chain(tmp.iter())) + .enumerate() + { + product[i] = *a * (-denom * x_k) + *b * denom; } - tmp = product; + std::mem::swap(&mut tmp, &mut product); } } interpolation_polys.push(tmp); diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index f35376f..5e90805 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -5,8 +5,8 @@ use super::super::{ use super::{Proof, ProverQuery}; use crate::arithmetic::{ - eval_polynomial, get_challenge_scalar, interpolate, kate_division, parallelize, Challenge, - Curve, CurveAffine, Field, + eval_polynomial, get_challenge_scalar, kate_division, lagrange_interpolate, parallelize, + Challenge, Curve, CurveAffine, Field, }; use crate::plonk::hash_point; use crate::transcript::Hasher; @@ -91,7 +91,7 @@ impl Proof { .fold(None, |f_poly, ((points, evals), poly)| { let mut poly = poly.clone()?.values; // TODO: makes implicit asssumption that poly degree is smaller than interpolation poly degree - for (p, r) in poly.iter_mut().zip(interpolate(points, evals)) { + for (p, r) in poly.iter_mut().zip(lagrange_interpolate(points, evals)) { *p -= &r; } let mut poly = points diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 6027609..36ba1aa 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -4,7 +4,7 @@ use super::super::{ }; use super::{Proof, VerifierQuery}; use crate::arithmetic::{ - eval_polynomial, get_challenge_scalar, interpolate, Challenge, CurveAffine, Field, + eval_polynomial, get_challenge_scalar, lagrange_interpolate, Challenge, CurveAffine, Field, }; use crate::plonk::hash_point; use crate::transcript::Hasher; @@ -80,11 +80,11 @@ impl<'a, C: CurveAffine> Proof { C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); transcript.absorb(transcript_scalar_point); - // Interpolate polynomial for evaluations at each set + // 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::with_capacity(point_sets.len()); for (points, evals) in point_sets.clone().iter().zip(q_eval_sets.clone().iter()) { - r_polys.push(interpolate(&points, &evals)); + 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); From 79cabb3d8d7cb61a618046167a91bbb8fd5c2857 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Fri, 9 Oct 2020 12:21:06 +0800 Subject: [PATCH 13/30] Move random scaling of MSM into multiopen verifier Co-authored-by: Sean Bowe --- src/poly/multiopen/verifier.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 36ba1aa..4b1f193 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -25,11 +25,16 @@ impl<'a, C: CurveAffine> Proof { transcript: &mut HBase, transcript_scalar: &mut HScalar, queries: I, - msm: MSM<'a, C>, + mut msm: MSM<'a, C>, ) -> Result, Error> where I: IntoIterator> + Clone, { + // Scale the MSM by a random factor to ensure that if the existing MSM + // has is_zero() == false then this argument won't be able to interfere + // with it to make it true, with high probability. + msm.scale(C::Scalar::random()); + // Sample x_4 for compressing openings at the same points together let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); From b62d11303148add5b7eb070f8268ee9aa7e1e048 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Fri, 9 Oct 2020 21:34:53 +0800 Subject: [PATCH 14/30] Refactor to use fold() in multiple places --- src/poly/multiopen/verifier.rs | 50 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 4b1f193..aa5aa5e 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -85,29 +85,23 @@ impl<'a, C: CurveAffine> Proof { C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap(); 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::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 // by the prover and from x_5 - let mut msm_eval = C::Scalar::zero(); - for (set_idx, points) in point_sets.iter().enumerate() { - let mut eval = self.q_evals[set_idx]; - eval -= &r_evals[set_idx]; - for point in points { - eval = eval * &(x_6 - &point).invert().unwrap(); - } - - msm_eval *= &x_5; - msm_eval += &eval; - } + let msm_eval = point_sets + .iter() + .zip(q_eval_sets.iter()) + .zip(self.q_evals.iter()) + .fold( + C::Scalar::zero(), + |msm_eval, ((points, evals), proof_eval)| { + let r_poly = lagrange_interpolate(points, evals); + let r_eval = eval_polynomial(&r_poly, x_6); + 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 // the various remaining polynomials at x_6 together. @@ -116,12 +110,14 @@ impl<'a, C: CurveAffine> Proof { // Compute the final commitment that has to be opened let mut commitment_msm = params.empty_msm(); commitment_msm.add_term(C::Scalar::one(), self.f_commitment); - for (set_idx, _) in point_sets.iter().enumerate() { - commitment_msm.scale(x_7); - commitment_msm.add_msm(&q_commitments[set_idx]); - msm_eval *= &x_7; - msm_eval += &self.q_evals[set_idx]; - } + 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.add_msm(&q_commitment); + (commitment_msm, msm_eval * &x_7 + &q_eval) + }, + ); // Verify the opening proof self.opening From 24b85dec67c7d6ab549c2863d4da0debafc71657 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 14 Oct 2020 00:43:48 +0800 Subject: [PATCH 15/30] Remove q_evals.len() = rotations.len() check q_evals should now have the same length as point_sets, which is only constructed in the multiopen verifier. --- src/plonk/verifier.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index f7adbc0..2046140 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -201,10 +201,6 @@ impl<'a, C: CurveAffine> Proof { return Err(Error::IncompatibleParams); } - if self.opening.q_evals.len() != vk.cs.rotations.len() { - return Err(Error::IncompatibleParams); - } - // TODO: check h_evals if self.fixed_evals.len() != vk.cs.fixed_queries.len() { From 742c15bb515560f5753cd1db8236bc5c46d1d09e Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 14 Oct 2020 01:08:03 +0800 Subject: [PATCH 16/30] Minor changes and documentation --- src/arithmetic.rs | 6 ++++-- src/poly/multiopen/prover.rs | 7 ++++++- src/poly/multiopen/verifier.rs | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 010cf47..3c03aa4 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -440,8 +440,8 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { } else { let mut interpolation_polys = vec![]; for (j, x_j) in points.iter().enumerate() { - let mut tmp: Vec = Vec::with_capacity(points.len() + 1); - let mut product = Vec::with_capacity(points.len() + 1); + let mut tmp: Vec = Vec::with_capacity(points.len()); + let mut product = Vec::with_capacity(points.len() - 1); tmp.push(F::one()); for (k, x_k) in points.iter().enumerate() { if k != j { @@ -459,6 +459,8 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { std::mem::swap(&mut tmp, &mut product); } } + assert_eq!(tmp.len(), points.len()); + assert_eq!(product.len(), points.len() - 1); interpolation_polys.push(tmp); } let mut final_poly = vec![F::zero(); points.len()]; diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 5e90805..fbc6c0d 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -40,6 +40,9 @@ impl Proof { // x_4 challenge. let mut q_polys: Vec>> = vec![None; point_sets.len()]; let mut q_blinds = vec![Blind(C::Scalar::zero()); point_sets.len()]; + + // A vec of vecs of evals. The outer vec corresponds to the point set, + // while the inner vec corresponds to the points in a particular set. let mut q_eval_sets: Vec> = vec![Vec::new(); point_sets.len()]; for (set_idx, point_set) in point_sets.iter().enumerate() { q_eval_sets[set_idx] = vec![C::Scalar::zero(); point_set.len()]; @@ -66,6 +69,8 @@ impl Proof { }); q_blinds[set_idx] *= x_4; q_blinds[set_idx] += blind; + // Each polynomial is evaluated at a set of points. For each set, + // we collapse each polynomial's evals pointwise. for (eval_idx, &eval) in evals.iter().enumerate() { q_eval_sets[set_idx][eval_idx] *= &x_4; q_eval_sets[set_idx][eval_idx] += &eval; @@ -142,7 +147,7 @@ impl Proof { (f_poly.clone(), f_blind), |(f_poly, f_blind), (poly, blind)| { ( - f_poly * x_7 + &poly.clone().unwrap(), + f_poly * x_7 + poly.as_ref().unwrap(), Blind((f_blind.0 * &x_7) + &blind.0), ) }, diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index aa5aa5e..be47dbf 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -17,9 +17,9 @@ struct CommitmentData { evals: Vec, } -impl<'a, C: CurveAffine> Proof { +impl Proof { /// Verify a multi-opening proof - pub fn verify, HScalar: Hasher>( + pub fn verify<'a, I, HBase: Hasher, HScalar: Hasher>( &self, params: &'a Params, transcript: &mut HBase, @@ -43,6 +43,9 @@ impl<'a, C: CurveAffine> Proof { // Compress the commitments and expected evaluations at x_3 together. // using the challenge x_4 let mut q_commitments: Vec<_> = vec![params.empty_msm(); point_sets.len()]; + + // A vec of vecs of evals. The outer vec corresponds to the point set, + // while the inner vec corresponds to the points in a particular set. let mut q_eval_sets: Vec> = vec![Vec::new(); point_sets.len()]; for (set_idx, point_set) in point_sets.iter().enumerate() { q_eval_sets[set_idx] = vec![C::Scalar::zero(); point_set.len()]; @@ -57,6 +60,8 @@ impl<'a, C: CurveAffine> Proof { } }; + // Each commitment corresponds to evaluations at a set of points. + // For each set, we collapse each commitment's evals pointwise. for (commitment, commitment_data) in commitment_map { accumulate( commitment_data.set_index, // set_idx, From 2f7b46ffa1626b22fd0ea0ff2a90896a7843e16a Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 14 Oct 2020 08:16:14 +0800 Subject: [PATCH 17/30] Remove use of parallelize() from multiopen::prover --- src/poly/multiopen/prover.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index fbc6c0d..654dbd1 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -5,8 +5,8 @@ use super::super::{ use super::{Proof, ProverQuery}; use crate::arithmetic::{ - eval_polynomial, get_challenge_scalar, kate_division, lagrange_interpolate, parallelize, - Challenge, Curve, CurveAffine, Field, + eval_polynomial, get_challenge_scalar, kate_division, lagrange_interpolate, Challenge, Curve, + CurveAffine, Field, }; use crate::plonk::hash_point; use crate::transcript::Hasher; @@ -53,20 +53,11 @@ impl Proof { new_poly: &Polynomial, blind: Blind, evals: Vec| { - q_polys[set_idx] - .as_mut() - .map(|poly| { - parallelize(poly, |q, start| { - for (q, a) in q.iter_mut().zip(new_poly[start..].iter()) { - *q *= &x_4; - *q += a; - } - }); - }) - .or_else(|| { - q_polys[set_idx] = Some(new_poly.clone()); - Some(()) - }); + if let Some(poly) = &q_polys[set_idx] { + q_polys[set_idx] = Some(poly.clone() * x_4 + new_poly); + } else { + q_polys[set_idx] = Some(new_poly.clone()); + } q_blinds[set_idx] *= x_4; q_blinds[set_idx] += blind; // Each polynomial is evaluated at a set of points. For each set, From 5c563eca12a0111ee226e59894ebf5d695d81627 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 13:56:42 -0600 Subject: [PATCH 18/30] Do not enumerate product inside of lagrange_interpolate. --- src/arithmetic.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 3c03aa4..d5bcc5c 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -448,13 +448,13 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { // Compute (x_j - x_k)^(-1) let denom = (*x_j - x_k).invert().unwrap(); product.resize(tmp.len() + 1, F::zero()); - for (i, (a, b)) in tmp + for ((a, b), product) in tmp .iter() .chain(std::iter::once(&F::zero())) .zip(std::iter::once(&F::zero()).chain(tmp.iter())) - .enumerate() + .zip(product.iter_mut()) { - product[i] = *a * (-denom * x_k) + *b * denom; + *product = *a * (-denom * x_k) + *b * denom; } std::mem::swap(&mut tmp, &mut product); } From 63d7de3bc2371c6d9f78235e09dc0ead22084238 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 14:08:13 -0600 Subject: [PATCH 19/30] Perform inversions in lagrange_interpolate as part of a batch. --- src/arithmetic.rs | 50 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index d5bcc5c..840cbf4 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -431,7 +431,9 @@ fn log2_floor(num: usize) -> u32 { pow } -/// Returns coefficients of an n - 1 degree polynomial given a set of n points and their evaluations +/// Returns coefficients of an n - 1 degree polynomial given a set of n points +/// and their evaluations. This function will panic if two values in `points` +/// are the same. pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { assert_eq!(points.len(), evals.len()); if points.len() == 1 { @@ -439,25 +441,43 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { return vec![evals[0]]; } else { let mut interpolation_polys = vec![]; + let mut denoms = Vec::with_capacity(points.len()); for (j, x_j) in points.iter().enumerate() { + let mut denom = Vec::with_capacity(points.len() - 1); + for x_k in points + .iter() + .enumerate() + .filter(|&(k, _)| k != j) + .map(|a| a.1) + { + denom.push(*x_j - x_k); + } + denoms.push(denom); + } + // Compute (x_j - x_k)^(-1) for each j != i + denoms.iter_mut().flat_map(|v| v.iter_mut()).batch_invert(); + + for (j, denoms) in denoms.into_iter().enumerate() { let mut tmp: Vec = Vec::with_capacity(points.len()); let mut product = Vec::with_capacity(points.len() - 1); tmp.push(F::one()); - for (k, x_k) in points.iter().enumerate() { - if k != j { - // Compute (x_j - x_k)^(-1) - let denom = (*x_j - x_k).invert().unwrap(); - product.resize(tmp.len() + 1, F::zero()); - for ((a, b), product) in tmp - .iter() - .chain(std::iter::once(&F::zero())) - .zip(std::iter::once(&F::zero()).chain(tmp.iter())) - .zip(product.iter_mut()) - { - *product = *a * (-denom * x_k) + *b * denom; - } - std::mem::swap(&mut tmp, &mut product); + for (x_k, denom) in points + .iter() + .enumerate() + .filter(|&(k, _)| k != j) + .map(|a| a.1) + .zip(denoms.into_iter()) + { + product.resize(tmp.len() + 1, F::zero()); + for ((a, b), product) in tmp + .iter() + .chain(std::iter::once(&F::zero())) + .zip(std::iter::once(&F::zero()).chain(tmp.iter())) + .zip(product.iter_mut()) + { + *product = *a * (-denom * x_k) + *b * denom; } + std::mem::swap(&mut tmp, &mut product); } assert_eq!(tmp.len(), points.len()); assert_eq!(product.len(), points.len() - 1); From 588de0e024a5f03fb8940ba489d866b74e214018 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 14:15:03 -0600 Subject: [PATCH 20/30] Add test for lagrange interpolation. --- src/arithmetic.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 840cbf4..a9b780b 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -440,7 +440,6 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { // Constant polynomial return vec![evals[0]]; } else { - let mut interpolation_polys = vec![]; let mut denoms = Vec::with_capacity(points.len()); for (j, x_j) in points.iter().enumerate() { let mut denom = Vec::with_capacity(points.len() - 1); @@ -457,7 +456,8 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { // Compute (x_j - x_k)^(-1) for each j != i denoms.iter_mut().flat_map(|v| v.iter_mut()).batch_invert(); - for (j, denoms) in denoms.into_iter().enumerate() { + let mut final_poly = vec![F::zero(); points.len()]; + for (j, (denoms, eval)) in denoms.into_iter().zip(evals.iter()).enumerate() { let mut tmp: Vec = Vec::with_capacity(points.len()); let mut product = Vec::with_capacity(points.len() - 1); tmp.push(F::one()); @@ -481,15 +481,28 @@ pub fn lagrange_interpolate(points: &[F], evals: &[F]) -> Vec { } assert_eq!(tmp.len(), points.len()); assert_eq!(product.len(), points.len() - 1); - interpolation_polys.push(tmp); - } - let mut final_poly = vec![F::zero(); points.len()]; - for (interpolation_poly, evaluation) in interpolation_polys.into_iter().zip(evals.iter()) { - for (final_coeff, interpolation_coeff) in final_poly.iter_mut().zip(interpolation_poly) - { - *final_coeff += interpolation_coeff * evaluation; + for (final_coeff, interpolation_coeff) in final_poly.iter_mut().zip(tmp.into_iter()) { + *final_coeff += interpolation_coeff * eval; } } final_poly } } + +#[test] +fn test_lagrange_interpolate() { + let points = (0..5).map(|_| Fp::random()).collect::>(); + let evals = (0..5).map(|_| Fp::random()).collect::>(); + + for coeffs in 0..5 { + let points = &points[0..coeffs]; + let evals = &evals[0..coeffs]; + + let poly = lagrange_interpolate(points, evals); + assert_eq!(poly.len(), points.len()); + + for (point, eval) in points.iter().zip(evals) { + assert_eq!(eval_polynomial(&poly, *point), *eval); + } + } +} From 3955da435cbab12e3a6581ae8affb03f16243239 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 14:17:03 -0600 Subject: [PATCH 21/30] Avoid unnecessary vector cloning in multiopen. --- src/poly/multiopen/verifier.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index be47dbf..0d8f21f 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -62,11 +62,11 @@ impl Proof { // Each commitment corresponds to evaluations at a set of points. // For each set, we collapse each commitment's evals pointwise. - for (commitment, commitment_data) in commitment_map { + for (commitment, commitment_data) in commitment_map.into_iter() { accumulate( - commitment_data.set_index, // set_idx, - *commitment, // commitment, - commitment_data.evals.to_vec(), // evals + commitment_data.set_index, // set_idx, + *commitment, // commitment, + commitment_data.evals, // evals ); } } From d3f593e89c8441373c91833d2953a349dac83135 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 14:18:02 -0600 Subject: [PATCH 22/30] Avoid unnecessary enumeration. --- src/poly/multiopen/verifier.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 0d8f21f..998ceb0 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -54,9 +54,9 @@ impl Proof { let mut accumulate = |set_idx: usize, new_commitment, evals: Vec| { q_commitments[set_idx].scale(x_4); q_commitments[set_idx].add_term(C::Scalar::one(), new_commitment); - for (eval_idx, &eval) in evals.iter().enumerate() { - q_eval_sets[set_idx][eval_idx] *= &x_4; - q_eval_sets[set_idx][eval_idx] += &eval; + for (eval, set_eval) in evals.iter().zip(q_eval_sets[set_idx].iter_mut()) { + *set_eval *= &x_4; + *set_eval += eval; } }; From 55ef4ea1f77cb3d68a21c5427498dbffd8facfb8 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 17:01:30 -0600 Subject: [PATCH 23/30] Unify the construct_intermediate_sets function between multiopen prover and verifier. --- src/poly/multiopen.rs | 296 +++++++++++++++++++++++++++++++++ src/poly/multiopen/prover.rs | 165 ++++-------------- src/poly/multiopen/verifier.rs | 150 +++-------------- 3 files changed, 357 insertions(+), 254 deletions(-) diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs index ad702bd..77a2502 100644 --- a/src/poly/multiopen.rs +++ b/src/poly/multiopen.rs @@ -3,6 +3,8 @@ //! //! [halo]: https://eprint.iacr.org/2019/1021 +use std::collections::{BTreeMap, BTreeSet}; + use super::*; use crate::arithmetic::CurveAffine; @@ -45,3 +47,297 @@ pub struct VerifierQuery<'a, C: CurveAffine> { /// evaluation of polynomial at query point pub eval: C::Scalar, } + +struct CommitmentData { + commitment: T, + set_index: usize, + point_indices: Vec, + evals: Vec, +} + +impl CommitmentData { + fn new(commitment: T) -> Self { + CommitmentData { + commitment, + set_index: 0, + point_indices: vec![], + evals: vec![], + } + } +} + +trait Query: Sized { + type Commitment: PartialEq + Copy; + + fn get_point(&self) -> F; + fn get_eval(&self) -> F; + fn get_commitment(&self) -> Self::Commitment; +} + +fn construct_intermediate_sets>( + queries: I, +) -> (Vec>, Vec>) +where + I: IntoIterator + Clone, +{ + // Construct sets of unique commitments and corresponding information about + // their queries. + let mut commitment_map: Vec> = vec![]; + + // Also construct mapping from a unique point to a point_index. This defines + // an ordering on the points. + let mut point_index_map = BTreeMap::new(); + + // Iterate over all of the queries, computing the ordering of the points + // while also creating new commitment data. + for query in queries.clone() { + let num_points = point_index_map.len(); + let point_idx = point_index_map + .entry(query.get_point()) + .or_insert(num_points); + + if let Some(pos) = commitment_map + .iter() + .position(|comm| comm.commitment == query.get_commitment()) + { + commitment_map[pos].point_indices.push(*point_idx); + } else { + let mut tmp = CommitmentData::new(query.get_commitment()); + tmp.point_indices.push(*point_idx); + commitment_map.push(tmp); + } + } + + // Also construct inverse mapping from point_index to the point + let mut inverse_point_index_map = BTreeMap::new(); + for (&point, &point_index) in point_index_map.iter() { + inverse_point_index_map.insert(point_index, point); + } + + // Construct map of unique ordered point_idx_sets to their set_idx + let mut point_idx_sets = BTreeMap::new(); + // Also construct mapping from commitment to point_idx_set + let mut commitment_set_map = Vec::new(); + + for commitment_data in commitment_map.iter() { + let mut point_index_set = BTreeSet::new(); + // Note that point_index_set is ordered, unlike point_indices + for &point_index in commitment_data.point_indices.iter() { + point_index_set.insert(point_index); + } + + // Push point_index_set to CommitmentData for the relevant commitment + commitment_set_map.push((commitment_data.commitment, point_index_set.clone())); + + let num_sets = point_idx_sets.len(); + point_idx_sets.entry(point_index_set).or_insert(num_sets); + } + + // Initialise empty evals vec for each unique commitment + for commitment_data in commitment_map.iter_mut() { + let len = commitment_data.point_indices.len(); + commitment_data.evals = vec![F::zero(); len]; + } + + // Populate set_index, evals and points for each commitment using point_idx_sets + for query in queries { + // The index of the point at which the commitment is queried + let point_index = point_index_map.get(&query.get_point()).unwrap(); + + // The point_index_set at which the commitment was queried + let mut point_index_set = BTreeSet::new(); + for (commitment, point_idx_set) in commitment_set_map.iter() { + if query.get_commitment() == *commitment { + point_index_set = point_idx_set.clone(); + } + } + assert!(!point_index_set.is_empty()); + + // The set_index of the point_index_set + let set_index = point_idx_sets.get(&point_index_set).unwrap(); + for commitment_data in commitment_map.iter_mut() { + if query.get_commitment() == commitment_data.commitment { + commitment_data.set_index = *set_index; + } + } + let point_index_set: Vec = point_index_set.iter().cloned().collect(); + + // The offset of the point_index in the point_index_set + let point_index_in_set = point_index_set + .iter() + .position(|i| i == point_index) + .unwrap(); + + for commitment_data in commitment_map.iter_mut() { + if query.get_commitment() == commitment_data.commitment { + // Insert the eval using the ordering of the point_index_set + commitment_data.evals[point_index_in_set] = query.get_eval(); + } + } + } + + // Get actual points in each point set + let mut point_sets: Vec> = vec![Vec::new(); point_idx_sets.len()]; + for (point_idx_set, &set_idx) in point_idx_sets.iter() { + for &point_idx in point_idx_set.iter() { + let point = inverse_point_index_map.get(&point_idx).unwrap(); + point_sets[set_idx].push(*point); + } + } + + (commitment_map, point_sets) +} + +#[cfg(test)] +mod tests { + use super::{construct_intermediate_sets, Query}; + use crate::arithmetic::{Field, Fp}; + + #[derive(Clone)] + struct MyQuery { + commitment: usize, + point: F, + eval: F, + } + + impl Query for MyQuery { + type Commitment = usize; + + fn get_point(&self) -> F { + self.point + } + fn get_eval(&self) -> F { + self.eval + } + fn get_commitment(&self) -> Self::Commitment { + self.commitment + } + } + + #[test] + fn test_coherence() { + let points = &[ + Fp::random(), + Fp::random(), + Fp::random(), + Fp::random(), + Fp::random(), + ]; + + let queries = vec![ + MyQuery { + commitment: 0, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 0, + point: points[1], + eval: Fp::random(), + }, + MyQuery { + commitment: 1, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 1, + point: points[1], + eval: Fp::random(), + }, + MyQuery { + commitment: 2, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 2, + point: points[1], + eval: Fp::random(), + }, + MyQuery { + commitment: 2, + point: points[2], + eval: Fp::random(), + }, + MyQuery { + commitment: 3, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 3, + point: points[3], + eval: Fp::random(), + }, + MyQuery { + commitment: 4, + point: points[4], + eval: Fp::random(), + }, + ]; + + let (commitment_data, point_sets) = construct_intermediate_sets(queries); + + let mut a = false; + let mut a_set = 0; + let mut b = false; + let mut b_set = 0; + let mut c = false; + let mut c_set = 0; + let mut d = false; + let mut d_set = 0; + + for (i, mut point_set) in point_sets.into_iter().enumerate() { + point_set.sort(); + if point_set.len() == 1 { + assert_eq!(point_set[0], points[4]); + assert!(!a); + a = true; + a_set = i; + } else if point_set.len() == 2 { + let mut v0 = [points[0], points[1]]; + let mut v1 = [points[0], points[3]]; + v0.sort(); + v1.sort(); + + if &point_set[..] == &v0[..] { + assert!(!b); + b = true; + b_set = i; + } else if &point_set[..] == &v1[..] { + assert!(!c); + c = true; + c_set = i; + } else { + panic!("unexpected"); + } + } else if point_set.len() == 3 { + let mut v = [points[0], points[1], points[2]]; + v.sort(); + assert_eq!(&point_set[..], &v[..]); + assert!(!d); + d = true; + d_set = i; + } else { + panic!("unexpected"); + } + } + + assert!(a & b & c & d); + + for commitment_data in commitment_data { + assert_eq!( + commitment_data.set_index, + match commitment_data.commitment { + 0 => b_set, + 1 => b_set, + 2 => d_set, + 3 => c_set, + 4 => a_set, + _ => unreachable!(), + } + ); + } + } +} diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 654dbd1..5a293d3 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -2,7 +2,7 @@ use super::super::{ commitment::{self, Blind, Params}, Coeff, Error, Polynomial, }; -use super::{Proof, ProverQuery}; +use super::{construct_intermediate_sets, Proof, ProverQuery, Query}; use crate::arithmetic::{ eval_polynomial, get_challenge_scalar, kate_division, lagrange_interpolate, Challenge, Curve, @@ -10,7 +10,6 @@ use crate::arithmetic::{ }; use crate::plonk::hash_point; use crate::transcript::Hasher; -use std::collections::{BTreeMap, BTreeSet}; use std::marker::PhantomData; #[derive(Debug, Clone)] @@ -34,7 +33,7 @@ impl Proof { { let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - let (poly_map, point_sets) = construct_intermediate_sets::<'a, C, I>(queries); + let (poly_map, point_sets) = construct_intermediate_sets(queries); // Collapse openings at same point sets together into single openings using // x_4 challenge. @@ -68,12 +67,12 @@ impl Proof { } }; - for (poly, commitment_data) in poly_map { + for commitment_data in poly_map { accumulate( - commitment_data.set_index, // set_idx, - &poly, // poly, - commitment_data.blind, // blind, - commitment_data.evals.to_vec(), // evals + commitment_data.set_index, // set_idx, + commitment_data.commitment.poly, // poly, + commitment_data.commitment.blind, // blind, + commitment_data.evals.to_vec(), // evals ); } } @@ -162,126 +161,32 @@ impl Proof { } } -// For multiopen prover: Construct intermediate representations relating polynomials to sets of points by index -fn construct_intermediate_sets<'a, C: CurveAffine, I>( - queries: I, -) -> ( - Vec<(&'a Polynomial, CommitmentData)>, // poly_map - Vec>, // point_sets -) -where - I: IntoIterator> + Clone, -{ - // Construct vec of unique polynomials and corresponding information about their queries - let mut poly_map: Vec<(&'a Polynomial, CommitmentData)> = Vec::new(); - - // Also construct mapping from a unique point to a point_index - let mut point_index_map: BTreeMap = BTreeMap::new(); - - // Construct point_indices which each polynomial is queried at - for query in queries.clone() { - let num_points = point_index_map.len(); - let point_idx = point_index_map.entry(query.point).or_insert(num_points); - - let mut exists = false; - for (existing_poly, existing_commitment_data) in poly_map.iter_mut() { - // Add to CommitmentData for existing commitment in commitment_map - if std::ptr::eq(query.poly, *existing_poly) { - exists = true; - existing_commitment_data.point_indices.push(*point_idx); - } - } - - // Add new poly and CommitmentData to poly_map - if !exists { - let commitment_data = CommitmentData { - set_index: 0, - blind: query.blind, - point_indices: vec![*point_idx], - evals: vec![], - }; - poly_map.push((query.poly, commitment_data)); - } - } - - // Also construct inverse mapping from point_index to the point - let mut inverse_point_index_map: BTreeMap = BTreeMap::new(); - for (&point, &point_index) in point_index_map.iter() { - inverse_point_index_map.insert(point_index, point); - } - - // Construct map of unique ordered point_idx_sets to their set_idx - let mut point_idx_sets: BTreeMap, usize> = BTreeMap::new(); - // Also construct mapping from poly to point_idx_set - let mut poly_set_map: Vec<(&Polynomial, BTreeSet)> = Vec::new(); - - for (poly, commitment_data) in poly_map.iter_mut() { - let mut point_index_set = BTreeSet::new(); - // Note that point_index_set is ordered, unlike point_indices - for &point_index in commitment_data.point_indices.iter() { - point_index_set.insert(point_index); - } - - // Push point_index_set to CommitmentData for the relevant poly - poly_set_map.push((poly, point_index_set.clone())); - - let num_sets = point_idx_sets.len(); - point_idx_sets - .entry(point_index_set.clone()) - .or_insert(num_sets); - } - - // Initialise empty evals vec for each unique poly - for (_, commitment_data) in poly_map.iter_mut() { - let len = commitment_data.point_indices.len(); - commitment_data.evals = vec![C::Scalar::zero(); len]; - } - - // Populate set_index, evals and points for each poly using point_idx_sets - for query in queries.clone() { - // The index of the point at which the poly is queried - let point_index = point_index_map.get(&query.point).unwrap(); - - // The point_index_set at which the poly was queried - let mut point_index_set = BTreeSet::new(); - for (poly, point_idx_set) in poly_set_map.iter() { - if std::ptr::eq(query.poly, *poly) { - point_index_set = point_idx_set.clone(); - } - } - - // The set_index of the point_index_set - let set_index = point_idx_sets.get(&point_index_set).unwrap(); - for (poly, commitment_data) in poly_map.iter_mut() { - if std::ptr::eq(query.poly, *poly) { - commitment_data.set_index = *set_index; - } - } - - let point_index_set: Vec = point_index_set.iter().cloned().collect(); - - // The offset of the point_index in the point_index_set - let point_index_in_set = point_index_set - .iter() - .position(|i| i == point_index) - .unwrap(); - - for (poly, commitment_data) in poly_map.iter_mut() { - if std::ptr::eq(query.poly, *poly) { - // Insert the eval using the ordering of the point_index_set - commitment_data.evals[point_index_in_set] = query.eval; - } - } - } - - // Get actual points in each point set - let mut point_sets: Vec> = vec![Vec::new(); point_idx_sets.len()]; - for (point_idx_set, &set_idx) in point_idx_sets.iter() { - for &point_idx in point_idx_set.iter() { - let point = inverse_point_index_map.get(&point_idx).unwrap(); - point_sets[set_idx].push(*point); - } - } - - (poly_map, point_sets) +#[doc(hidden)] +#[derive(Copy, Clone)] +pub struct PolynomialPointer<'a, C: CurveAffine> { + poly: &'a Polynomial, + blind: commitment::Blind, +} + +impl<'a, C: CurveAffine> PartialEq for PolynomialPointer<'a, C> { + fn eq(&self, other: &Self) -> bool { + std::ptr::eq(self.poly, other.poly) + } +} + +impl<'a, C: CurveAffine> Query for ProverQuery<'a, C> { + type Commitment = PolynomialPointer<'a, C>; + + fn get_point(&self) -> C::Scalar { + self.point + } + fn get_eval(&self) -> C::Scalar { + self.eval + } + fn get_commitment(&self) -> Self::Commitment { + PolynomialPointer { + poly: self.poly, + blind: self.blind, + } + } } diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 998ceb0..b213260 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -2,13 +2,12 @@ use super::super::{ commitment::{Guard, Params, MSM}, Error, }; -use super::{Proof, VerifierQuery}; +use super::{construct_intermediate_sets, Proof, Query, VerifierQuery}; use crate::arithmetic::{ eval_polynomial, get_challenge_scalar, lagrange_interpolate, Challenge, CurveAffine, Field, }; use crate::plonk::hash_point; use crate::transcript::Hasher; -use std::collections::{BTreeMap, BTreeSet}; #[derive(Debug, Clone)] struct CommitmentData { @@ -38,7 +37,7 @@ impl Proof { // Sample x_4 for compressing openings at the same points together let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - let (commitment_map, point_sets) = construct_intermediate_sets::<'a, C, I>(queries.clone()); + let (commitment_map, point_sets) = construct_intermediate_sets(queries); // Compress the commitments and expected evaluations at x_3 together. // using the challenge x_4 @@ -62,11 +61,11 @@ impl Proof { // Each commitment corresponds to evaluations at a set of points. // For each set, we collapse each commitment's evals pointwise. - for (commitment, commitment_data) in commitment_map.into_iter() { + for commitment_data in commitment_map.into_iter() { accumulate( - commitment_data.set_index, // set_idx, - *commitment, // commitment, - commitment_data.evals, // evals + commitment_data.set_index, // set_idx, + *commitment_data.commitment.0, // commitment, + commitment_data.evals, // evals ); } } @@ -130,123 +129,26 @@ impl Proof { } } -// For multiopen verifier: Construct intermediate representations relating commitments to sets of points by index -fn construct_intermediate_sets<'a, C: CurveAffine, I>( - queries: I, -) -> ( - Vec<(&'a C, CommitmentData)>, // commitment_map - Vec>, // point_sets -) -where - I: IntoIterator> + Clone, -{ - // Construct sets of unique commitments and corresponding information about their queries - let mut commitment_map: Vec<(&'a C, CommitmentData)> = Vec::new(); +#[doc(hidden)] +#[derive(Copy, Clone)] +pub struct CommitmentPointer<'a, C>(&'a C); - // Also construct mapping from a unique point to a point_index. This defines an ordering on the points. - let mut point_index_map: BTreeMap = BTreeMap::new(); - - // Construct point_indices which each commitment is queried at - for query in queries.clone() { - let num_points = point_index_map.len(); - let point_idx = point_index_map.entry(query.point).or_insert(num_points); - - let mut exists = false; - for (existing_commitment, existing_commitment_data) in commitment_map.iter_mut() { - // Add to CommitmentData for existing commitment in commitment_map - if std::ptr::eq(query.commitment, *existing_commitment) { - exists = true; - existing_commitment_data.point_indices.push(*point_idx); - } - } - - // Add new commitment and CommitmentData to commitment_map - if !exists { - let commitment_data = CommitmentData { - set_index: 0, - point_indices: vec![*point_idx], - evals: vec![], - }; - commitment_map.push((query.commitment, commitment_data)); - } +impl<'a, C> PartialEq for CommitmentPointer<'a, C> { + fn eq(&self, other: &Self) -> bool { + std::ptr::eq(self.0, other.0) + } +} + +impl<'a, C: CurveAffine> Query for VerifierQuery<'a, C> { + type Commitment = CommitmentPointer<'a, C>; + + fn get_point(&self) -> C::Scalar { + self.point + } + fn get_eval(&self) -> C::Scalar { + self.eval + } + fn get_commitment(&self) -> Self::Commitment { + CommitmentPointer(self.commitment) } - - // Also construct inverse mapping from point_index to the point - let mut inverse_point_index_map: BTreeMap = BTreeMap::new(); - for (&point, &point_index) in point_index_map.iter() { - inverse_point_index_map.insert(point_index, point); - } - - // Construct map of unique ordered point_idx_sets to their set_idx - let mut point_idx_sets: BTreeMap, usize> = BTreeMap::new(); - // Also construct mapping from commitment to point_idx_set - let mut commitment_set_map: Vec<(&'a C, BTreeSet)> = Vec::new(); - - for (commitment, commitment_data) in commitment_map.iter_mut() { - let mut point_index_set = BTreeSet::new(); - // Note that point_index_set is ordered, unlike point_indices - for &point_index in commitment_data.point_indices.iter() { - point_index_set.insert(point_index); - } - - // Push point_index_set to CommitmentData for the relevant commitment - commitment_set_map.push((commitment, point_index_set.clone())); - - let num_sets = point_idx_sets.len(); - point_idx_sets - .entry(point_index_set.clone()) - .or_insert(num_sets); - } - - // Initialise empty evals vec for each unique commitment - for (_, commitment_data) in commitment_map.iter_mut() { - let len = commitment_data.point_indices.len(); - commitment_data.evals = vec![C::Scalar::zero(); len]; - } - - // Populate set_index, evals and points for each commitment using point_idx_sets - for query in queries.clone() { - // The index of the point at which the commitment is queried - let point_index = point_index_map.get(&query.point).unwrap(); - - // The point_index_set at which the commitment was queried - let mut point_index_set = BTreeSet::new(); - for (commitment, point_idx_set) in commitment_set_map.iter() { - if std::ptr::eq(query.commitment, *commitment) { - point_index_set = point_idx_set.clone(); - } - } - // The set_index of the point_index_set - let set_index = point_idx_sets.get(&point_index_set).unwrap(); - for (commitment, commitment_data) in commitment_map.iter_mut() { - if std::ptr::eq(query.commitment, *commitment) { - commitment_data.set_index = *set_index; - } - } - let point_index_set: Vec = point_index_set.iter().cloned().collect(); - - // The offset of the point_index in the point_index_set - let point_index_in_set = point_index_set - .iter() - .position(|i| i == point_index) - .unwrap(); - - for (commitment, commitment_data) in commitment_map.iter_mut() { - if std::ptr::eq(query.commitment, *commitment) { - // Insert the eval using the ordering of the point_index_set - commitment_data.evals[point_index_in_set] = query.eval; - } - } - } - - // Get actual points in each point set - let mut point_sets: Vec> = vec![Vec::new(); point_idx_sets.len()]; - for (point_idx_set, &set_idx) in point_idx_sets.iter() { - for &point_idx in point_idx_set.iter() { - let point = inverse_point_index_map.get(&point_idx).unwrap(); - point_sets[set_idx].push(*point); - } - } - - (commitment_map, point_sets) } From 123cacc7ccf24f1c4ae2cba1715e3938b0195ab0 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 17:06:35 -0600 Subject: [PATCH 24/30] Remove additional unnecessary vector clone from multiopen prover. --- src/poly/multiopen/prover.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 5a293d3..64e7ade 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -67,12 +67,12 @@ impl Proof { } }; - for commitment_data in poly_map { + for commitment_data in poly_map.into_iter() { accumulate( commitment_data.set_index, // set_idx, commitment_data.commitment.poly, // poly, commitment_data.commitment.blind, // blind, - commitment_data.evals.to_vec(), // evals + commitment_data.evals, // evals ); } } From 685bf7961384cd1d80007fa6559510efcf26f622 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 17:11:06 -0600 Subject: [PATCH 25/30] Relocate x_5 challenge sampling location to simplify logic. --- src/poly/multiopen/prover.rs | 3 +-- src/poly/multiopen/verifier.rs | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 64e7ade..0d14142 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -32,6 +32,7 @@ impl Proof { I: IntoIterator> + Clone, { let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); let (poly_map, point_sets) = construct_intermediate_sets(queries); @@ -77,8 +78,6 @@ impl Proof { } } - let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - let f_poly = point_sets .iter() .zip(q_eval_sets.iter()) diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index b213260..0366eac 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -34,9 +34,13 @@ impl Proof { // with it to make it true, with high probability. msm.scale(C::Scalar::random()); - // Sample x_4 for compressing openings at the same points together + // Sample x_4 for compressing openings at the same point sets together let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + // Sample a challenge x_5 for keeping the multi-point quotient + // polynomial terms linearly independent. + let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); + let (commitment_map, point_sets) = construct_intermediate_sets(queries); // Compress the commitments and expected evaluations at x_3 together. @@ -70,10 +74,6 @@ impl Proof { } } - // Sample a challenge x_5 for keeping the multi-point quotient - // polynomial terms linearly independent. - let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128())); - // Obtain the commitment to the multi-point quotient polynomial f(X). hash_point(transcript, &self.f_commitment).unwrap(); From 9d572c181ddeb1716d2eebfdea7a2cd9c435ad9c Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 17:12:17 -0600 Subject: [PATCH 26/30] Remove unnecessary enumeration in multiopen prover. --- src/poly/multiopen/prover.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 0d14142..124c630 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -62,9 +62,9 @@ impl Proof { q_blinds[set_idx] += blind; // Each polynomial is evaluated at a set of points. For each set, // we collapse each polynomial's evals pointwise. - for (eval_idx, &eval) in evals.iter().enumerate() { - q_eval_sets[set_idx][eval_idx] *= &x_4; - q_eval_sets[set_idx][eval_idx] += &eval; + for (eval, set_eval) in evals.iter().zip(q_eval_sets[set_idx].iter_mut()) { + *set_eval *= &x_4; + *set_eval += eval; } }; From 1e9f7f37c6984d4b75d5860b563ce5b1dbf5d3be Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 15 Oct 2020 17:16:44 -0600 Subject: [PATCH 27/30] Minor improvements to multiopen. --- src/poly/multiopen.rs | 117 +++++++++++++++++++---------------- src/poly/multiopen/prover.rs | 4 +- 2 files changed, 67 insertions(+), 54 deletions(-) diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs index 77a2502..c628017 100644 --- a/src/poly/multiopen.rs +++ b/src/poly/multiopen.rs @@ -224,61 +224,74 @@ mod tests { Fp::random(), ]; - let queries = vec![ - MyQuery { - commitment: 0, - point: points[0], - eval: Fp::random(), - }, - MyQuery { - commitment: 0, - point: points[1], - eval: Fp::random(), - }, - MyQuery { - commitment: 1, - point: points[0], - eval: Fp::random(), - }, - MyQuery { - commitment: 1, - point: points[1], - eval: Fp::random(), - }, - MyQuery { - commitment: 2, - point: points[0], - eval: Fp::random(), - }, - MyQuery { - commitment: 2, - point: points[1], - eval: Fp::random(), - }, - MyQuery { - commitment: 2, - point: points[2], - eval: Fp::random(), - }, - MyQuery { - commitment: 3, - point: points[0], - eval: Fp::random(), - }, - MyQuery { - commitment: 3, - point: points[3], - eval: Fp::random(), - }, - MyQuery { - commitment: 4, - point: points[4], - eval: Fp::random(), - }, - ]; + let build_queries = || { + vec![ + MyQuery { + commitment: 0, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 0, + point: points[1], + eval: Fp::random(), + }, + MyQuery { + commitment: 1, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 1, + point: points[1], + eval: Fp::random(), + }, + MyQuery { + commitment: 2, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 2, + point: points[1], + eval: Fp::random(), + }, + MyQuery { + commitment: 2, + point: points[2], + eval: Fp::random(), + }, + MyQuery { + commitment: 3, + point: points[0], + eval: Fp::random(), + }, + MyQuery { + commitment: 3, + point: points[3], + eval: Fp::random(), + }, + MyQuery { + commitment: 4, + point: points[4], + eval: Fp::random(), + }, + ] + }; + + let queries = build_queries(); let (commitment_data, point_sets) = construct_intermediate_sets(queries); + // It shouldn't matter what the point or eval values are; we should get + // the same exact point sets again. + { + let new_queries = build_queries(); + let (_, new_point_sets) = construct_intermediate_sets(new_queries); + + assert_eq!(point_sets, new_point_sets); + } + let mut a = false; let mut a_set = 0; let mut b = false; diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 124c630..2e5a967 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -102,9 +102,9 @@ impl Proof { } else { f_poly.map(|f_poly| f_poly * x_5 + &poly) } - }); + }) + .unwrap(); - let f_poly = f_poly.unwrap(); let mut f_blind = Blind(C::Scalar::random()); let mut f_commitment = params.commit(&f_poly, f_blind).to_affine(); From a30719c245989cc3c009b745bf5744b54e9a3bee Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Fri, 16 Oct 2020 08:43:41 +0800 Subject: [PATCH 28/30] Minor changes to multiopen Co-authored-by: ebfull --- src/poly/multiopen/prover.rs | 8 ++++---- src/poly/multiopen/verifier.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 2e5a967..fbe7dc9 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -43,9 +43,9 @@ impl Proof { // A vec of vecs of evals. The outer vec corresponds to the point set, // while the inner vec corresponds to the points in a particular set. - let mut q_eval_sets: Vec> = vec![Vec::new(); point_sets.len()]; - for (set_idx, point_set) in point_sets.iter().enumerate() { - q_eval_sets[set_idx] = vec![C::Scalar::zero(); point_set.len()]; + let mut q_eval_sets = Vec::with_capacity(point_sets.len()); + for point_set in point_sets.iter() { + q_eval_sets.push(vec![C::Scalar::zero(); point_set.len()]); } { @@ -83,7 +83,7 @@ impl Proof { .zip(q_eval_sets.iter()) .zip(q_polys.iter()) .fold(None, |f_poly, ((points, evals), poly)| { - let mut poly = poly.clone()?.values; + let mut poly = poly.clone().unwrap().values; // TODO: makes implicit asssumption that poly degree is smaller than interpolation poly degree for (p, r) in poly.iter_mut().zip(lagrange_interpolate(points, evals)) { *p -= &r; diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 0366eac..f674e31 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -49,9 +49,9 @@ impl Proof { // A vec of vecs of evals. The outer vec corresponds to the point set, // while the inner vec corresponds to the points in a particular set. - let mut q_eval_sets: Vec> = vec![Vec::new(); point_sets.len()]; - for (set_idx, point_set) in point_sets.iter().enumerate() { - q_eval_sets[set_idx] = vec![C::Scalar::zero(); point_set.len()]; + let mut q_eval_sets = Vec::with_capacity(point_sets.len()); + for point_set in point_sets.iter() { + q_eval_sets.push(vec![C::Scalar::zero(); point_set.len()]); } { let mut accumulate = |set_idx: usize, new_commitment, evals: Vec| { @@ -114,7 +114,7 @@ impl Proof { // Compute the final commitment that has to be opened let mut commitment_msm = params.empty_msm(); commitment_msm.add_term(C::Scalar::one(), self.f_commitment); - let (commitment_msm, msm_eval) = q_commitments.iter().zip(self.q_evals.iter()).fold( + let (commitment_msm, msm_eval) = q_commitments.into_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); From 3761419ebce84788e25bdbc93bd37ff3187e7e26 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Fri, 16 Oct 2020 11:28:38 +0800 Subject: [PATCH 29/30] Add documentation for multiopen.rs --- src/poly/multiopen.rs | 86 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs index c628017..2271701 100644 --- a/src/poly/multiopen.rs +++ b/src/poly/multiopen.rs @@ -1,7 +1,89 @@ -//! This module contains an implementation of the multipoint opening polynomial -//! commitment scheme described in the [Halo][halo] paper. +//! This module contains an optimisation of the polynomial commitment opening +//! scheme described in the [Halo][halo] paper. //! //! [halo]: https://eprint.iacr.org/2019/1021 +//! Consider the commitments $`A, B, C, D`$ to polynomials $`a(X), b(X), c(X) +//! , d(X)`$. Let's say that $`a`$ and $`b`$ were queried at the point $`x`$, +//! while $`c`$ and $`d`$ were queried at both points $`x`$ and $`\omega x`$. +//! (Here, $`\omega`$ is the primitive root of unity in the multiplicative +//! subgroup over which we constructed the polynomials). +//! +//! We can group the commitments in terms of the sets of points at which +//! they were queried: +//! ```math +//! {x} {x, \omega x} +//! A C +//! B D +//! ``` +//! The multipoint opening optimisation proceeds as such: +//! 1. Sample random $`x_3`$, at which we evaluate $`a(X), b(X), c(X), d(X)`$. +//! 2. The prover provides evaluations of each polynomial at each point of +//! interest: $`a(x_3), b(x_3), c(x_3), d(x_3), c(\omega x_3), d(\omega x_3)`$ +//! 3. Sample random $`x_4`$, to keep $`a, b, c, d`$ linearly independent. +//! 4. Accumulate polynomials and their corresponding evaluations according +//! to the point set at which they were queried: +//! +//! q_polys: +//! ```math +//! q_1(X) = a(X) + x_4 b(X) +//! q_2(X) = c(X) + x_4 d(X) +//! ``` +//! q_eval_sets: +//! ```math +//! [ +//! [a(x_3) + x_4 b(x_3)], +//! [ +//! c(x_3) + x_4 d(x_3), +//! c(\omega x_3) + x_4 d(\omega x_3) +//! ] +//! ] +//! ``` +//! NB: $`q_eval_sets`$ is a vector of sets of evaluations, +//! where the outer vector goes over the point sets, and +//! the inner vector goes over the points in each set. +//! +//! 5. Interpolate each set of values in $`q_eval_sets`$: +//! r_polys +//! ```math +//! r_1(X) s.t. +//! r_1(x_3) = a(x_3) + x_4 b(x_3) +//! +//! r_2(X) s.t. +//! r_2(x_3) = c(x_3) + x_4 d(x_3) +//! r_2(\omega x_3) = c(\omega x_3) + x_4 d(\omega x_3) +//! ``` +//! +//! 6. Construct $`f_polys`$ which check the correctness of $`q_polys`$: +//! f_polys +//! ```math +//! q_1(X) - r_1(X) +//! f_1(X) = ----------------- +//! X - x_3 +//! +//! q_2(X) - r_2(X) +//! f_2(X) = --------------------------- +//! (X - x_3)(X - \omega x_3) +//! ``` +//! If $`q_1(x_3)` = `r_1(x_3)`$, then $`f_1(X)`$ should be a polynomial. +//! If $`q_2(x_3)` = `r_2(x_3)`$ and $`q_2(\omega x_3) = r_2(\omega x_3)`$ +//! then $`f_2(X)`$ should be a polynomial. +//! +//! 6. Sample random $`x_5`$ to keep the $`f_polys`$ linearly independent. +//! 7. Construct $`f(X) = f_1(X) + x_5 f_2(X)`$. +//! 8. Sample random $`x_6`$, at which we evaluate $`f(X)`$. +//! ```math +//! f(x_6) = f_1(x_6) + x_5 f_2(x_6) +//! +//! q_1(x_6) - r_1(x_6) q_2(x_6) - r_2(x_6) +//! = --------------------- + x_5 ------------------------------- +//! x_6 - x_3 (x_6 - x_3)(x_6 - \omega x_3) +//! ``` +//! 9. Sample random $`x_7`$ to keep $`f(X)`$ and $`q_polys`$ linearly independent. +//! 10. Construct $`final_poly`$, +//! ```math +//! final_poly(X) = f(X) + x_7 q_1(X) + (x_7)^2 q_2(X), +//! ``` +//! which is the polynomial we commit to in the inner product argument. use std::collections::{BTreeMap, BTreeSet}; From 011132337f66a53c483862023760829c1c40dc56 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Tue, 20 Oct 2020 08:57:48 -0600 Subject: [PATCH 30/30] Remove notes on multiopen module --- src/poly/multiopen.rs | 82 ------------------------------------------- 1 file changed, 82 deletions(-) diff --git a/src/poly/multiopen.rs b/src/poly/multiopen.rs index 2271701..844157c 100644 --- a/src/poly/multiopen.rs +++ b/src/poly/multiopen.rs @@ -2,88 +2,6 @@ //! scheme described in the [Halo][halo] paper. //! //! [halo]: https://eprint.iacr.org/2019/1021 -//! Consider the commitments $`A, B, C, D`$ to polynomials $`a(X), b(X), c(X) -//! , d(X)`$. Let's say that $`a`$ and $`b`$ were queried at the point $`x`$, -//! while $`c`$ and $`d`$ were queried at both points $`x`$ and $`\omega x`$. -//! (Here, $`\omega`$ is the primitive root of unity in the multiplicative -//! subgroup over which we constructed the polynomials). -//! -//! We can group the commitments in terms of the sets of points at which -//! they were queried: -//! ```math -//! {x} {x, \omega x} -//! A C -//! B D -//! ``` -//! The multipoint opening optimisation proceeds as such: -//! 1. Sample random $`x_3`$, at which we evaluate $`a(X), b(X), c(X), d(X)`$. -//! 2. The prover provides evaluations of each polynomial at each point of -//! interest: $`a(x_3), b(x_3), c(x_3), d(x_3), c(\omega x_3), d(\omega x_3)`$ -//! 3. Sample random $`x_4`$, to keep $`a, b, c, d`$ linearly independent. -//! 4. Accumulate polynomials and their corresponding evaluations according -//! to the point set at which they were queried: -//! -//! q_polys: -//! ```math -//! q_1(X) = a(X) + x_4 b(X) -//! q_2(X) = c(X) + x_4 d(X) -//! ``` -//! q_eval_sets: -//! ```math -//! [ -//! [a(x_3) + x_4 b(x_3)], -//! [ -//! c(x_3) + x_4 d(x_3), -//! c(\omega x_3) + x_4 d(\omega x_3) -//! ] -//! ] -//! ``` -//! NB: $`q_eval_sets`$ is a vector of sets of evaluations, -//! where the outer vector goes over the point sets, and -//! the inner vector goes over the points in each set. -//! -//! 5. Interpolate each set of values in $`q_eval_sets`$: -//! r_polys -//! ```math -//! r_1(X) s.t. -//! r_1(x_3) = a(x_3) + x_4 b(x_3) -//! -//! r_2(X) s.t. -//! r_2(x_3) = c(x_3) + x_4 d(x_3) -//! r_2(\omega x_3) = c(\omega x_3) + x_4 d(\omega x_3) -//! ``` -//! -//! 6. Construct $`f_polys`$ which check the correctness of $`q_polys`$: -//! f_polys -//! ```math -//! q_1(X) - r_1(X) -//! f_1(X) = ----------------- -//! X - x_3 -//! -//! q_2(X) - r_2(X) -//! f_2(X) = --------------------------- -//! (X - x_3)(X - \omega x_3) -//! ``` -//! If $`q_1(x_3)` = `r_1(x_3)`$, then $`f_1(X)`$ should be a polynomial. -//! If $`q_2(x_3)` = `r_2(x_3)`$ and $`q_2(\omega x_3) = r_2(\omega x_3)`$ -//! then $`f_2(X)`$ should be a polynomial. -//! -//! 6. Sample random $`x_5`$ to keep the $`f_polys`$ linearly independent. -//! 7. Construct $`f(X) = f_1(X) + x_5 f_2(X)`$. -//! 8. Sample random $`x_6`$, at which we evaluate $`f(X)`$. -//! ```math -//! f(x_6) = f_1(x_6) + x_5 f_2(x_6) -//! -//! q_1(x_6) - r_1(x_6) q_2(x_6) - r_2(x_6) -//! = --------------------- + x_5 ------------------------------- -//! x_6 - x_3 (x_6 - x_3)(x_6 - \omega x_3) -//! ``` -//! 9. Sample random $`x_7`$ to keep $`f(X)`$ and $`q_polys`$ linearly independent. -//! 10. Construct $`final_poly`$, -//! ```math -//! final_poly(X) = f(X) + x_7 q_1(X) + (x_7)^2 q_2(X), -//! ``` -//! which is the polynomial we commit to in the inner product argument. use std::collections::{BTreeMap, BTreeSet};