2020-12-23 12:03:31 -08:00
|
|
|
use super::Argument;
|
2020-12-02 07:16:37 -08:00
|
|
|
use crate::{
|
|
|
|
arithmetic::{eval_polynomial, Curve, CurveAffine, FieldExt},
|
|
|
|
plonk::{ChallengeX, ChallengeY, Error},
|
|
|
|
poly::{
|
|
|
|
commitment::{Blind, Params},
|
|
|
|
multiopen::ProverQuery,
|
|
|
|
Coeff, EvaluationDomain, ExtendedLagrangeCoeff, Polynomial,
|
|
|
|
},
|
2020-12-23 12:03:31 -08:00
|
|
|
transcript::TranscriptWrite,
|
2020-12-02 07:16:37 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
pub(in crate::plonk) struct Constructed<C: CurveAffine> {
|
|
|
|
h_pieces: Vec<Polynomial<C::Scalar, Coeff>>,
|
|
|
|
h_blinds: Vec<Blind<C::Scalar>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::plonk) struct Evaluated<C: CurveAffine> {
|
|
|
|
constructed: Constructed<C>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: CurveAffine> Argument<C> {
|
2020-12-23 15:20:27 -08:00
|
|
|
pub(in crate::plonk) fn construct<T: TranscriptWrite<C>>(
|
2020-12-02 07:16:37 -08:00
|
|
|
params: &Params<C>,
|
|
|
|
domain: &EvaluationDomain<C::Scalar>,
|
|
|
|
expressions: impl Iterator<Item = Polynomial<C::Scalar, ExtendedLagrangeCoeff>>,
|
2020-12-23 12:03:31 -08:00
|
|
|
y: ChallengeY<C>,
|
|
|
|
transcript: &mut T,
|
2020-12-02 07:16:37 -08:00
|
|
|
) -> Result<Constructed<C>, Error> {
|
|
|
|
// Evaluate the h(X) polynomial's constraint system expressions for the constraints provided
|
|
|
|
let h_poly = expressions.fold(domain.empty_extended(), |h_poly, v| h_poly * *y + &v);
|
|
|
|
|
|
|
|
// Divide by t(X) = X^{params.n} - 1.
|
|
|
|
let h_poly = domain.divide_by_vanishing_poly(h_poly);
|
|
|
|
|
|
|
|
// Obtain final h(X) polynomial
|
|
|
|
let h_poly = domain.extended_to_coeff(h_poly);
|
|
|
|
|
|
|
|
// Split h(X) up into pieces
|
|
|
|
let h_pieces = h_poly
|
|
|
|
.chunks_exact(params.n as usize)
|
|
|
|
.map(|v| domain.coeff_from_vec(v.to_vec()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
drop(h_poly);
|
|
|
|
let h_blinds: Vec<_> = h_pieces.iter().map(|_| Blind(C::Scalar::rand())).collect();
|
|
|
|
|
|
|
|
// Compute commitments to each h(X) piece
|
|
|
|
let h_commitments_projective: Vec<_> = h_pieces
|
|
|
|
.iter()
|
|
|
|
.zip(h_blinds.iter())
|
|
|
|
.map(|(h_piece, blind)| params.commit(&h_piece, *blind))
|
|
|
|
.collect();
|
|
|
|
let mut h_commitments = vec![C::zero(); h_commitments_projective.len()];
|
|
|
|
C::Projective::batch_to_affine(&h_commitments_projective, &mut h_commitments);
|
|
|
|
let h_commitments = h_commitments;
|
|
|
|
|
|
|
|
// Hash each h(X) piece
|
|
|
|
for c in h_commitments.iter() {
|
|
|
|
transcript
|
2020-12-23 12:03:31 -08:00
|
|
|
.write_point(*c)
|
2020-12-02 07:16:37 -08:00
|
|
|
.map_err(|_| Error::TranscriptError)?;
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:03:31 -08:00
|
|
|
Ok(Constructed { h_pieces, h_blinds })
|
2020-12-02 07:16:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: CurveAffine> Constructed<C> {
|
2020-12-23 15:20:27 -08:00
|
|
|
pub(in crate::plonk) fn evaluate<T: TranscriptWrite<C>>(
|
2020-12-02 07:16:37 -08:00
|
|
|
self,
|
2020-12-23 12:03:31 -08:00
|
|
|
x: ChallengeX<C>,
|
|
|
|
transcript: &mut T,
|
|
|
|
) -> Result<Evaluated<C>, Error> {
|
2020-12-02 07:16:37 -08:00
|
|
|
let h_evals: Vec<_> = self
|
|
|
|
.h_pieces
|
|
|
|
.iter()
|
|
|
|
.map(|poly| eval_polynomial(poly, *x))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Hash each advice evaluation
|
|
|
|
for eval in &h_evals {
|
2020-12-23 12:03:31 -08:00
|
|
|
transcript
|
|
|
|
.write_scalar(*eval)
|
|
|
|
.map_err(|_| Error::TranscriptError)?;
|
2020-12-02 07:16:37 -08:00
|
|
|
}
|
|
|
|
|
2021-01-13 16:22:32 -08:00
|
|
|
Ok(Evaluated { constructed: self })
|
2020-12-02 07:16:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: CurveAffine> Evaluated<C> {
|
|
|
|
pub(in crate::plonk) fn open<'a>(
|
|
|
|
&'a self,
|
2020-12-23 12:03:31 -08:00
|
|
|
x: ChallengeX<C>,
|
2020-12-02 07:16:37 -08:00
|
|
|
) -> impl Iterator<Item = ProverQuery<'a, C>> + Clone {
|
|
|
|
self.constructed
|
|
|
|
.h_pieces
|
|
|
|
.iter()
|
|
|
|
.zip(self.constructed.h_blinds.iter())
|
2021-01-13 16:22:32 -08:00
|
|
|
.map(move |(h_poly, h_blind)| ProverQuery {
|
2020-12-02 07:16:37 -08:00
|
|
|
point: *x,
|
|
|
|
poly: h_poly,
|
|
|
|
blind: *h_blind,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|