2020-08-27 19:27:24 +00:00
|
|
|
use super::{domain::Rotation, hash_point, Proof, SRS};
|
2020-08-22 20:15:39 +00:00
|
|
|
use crate::arithmetic::{get_challenge_scalar, Challenge, Curve, CurveAffine, Field};
|
|
|
|
|
use crate::polycommit::Params;
|
|
|
|
|
use crate::transcript::Hasher;
|
|
|
|
|
|
|
|
|
|
impl<C: CurveAffine> Proof<C> {
|
|
|
|
|
/// Returns
|
|
|
|
|
pub fn verify<HBase: Hasher<C::Base>, HScalar: Hasher<C::Scalar>>(
|
|
|
|
|
&self,
|
|
|
|
|
params: &Params<C>,
|
|
|
|
|
srs: &SRS<C>,
|
|
|
|
|
) -> bool {
|
|
|
|
|
// Create a transcript for obtaining Fiat-Shamir challenges.
|
|
|
|
|
let mut transcript = HBase::init(C::Base::one());
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Hash the prover's advice commitments into the transcript
|
2020-08-22 22:10:27 +00:00
|
|
|
for commitment in &self.advice_commitments {
|
|
|
|
|
hash_point(&mut transcript, commitment)
|
|
|
|
|
.expect("proof cannot contain points at infinity");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Sample x_2 challenge, which keeps the gates linearly independent.
|
2020-08-27 16:10:55 +00:00
|
|
|
let x_2: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128()));
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Obtain a commitment to h(X) in the form of multiple pieces of degree n - 1
|
2020-08-22 20:15:39 +00:00
|
|
|
for c in &self.h_commitments {
|
|
|
|
|
hash_point(&mut transcript, c).expect("proof cannot contain points at infinity");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Sample x_3 challenge, which is used to ensure the circuit is
|
|
|
|
|
// satisfied with high probability.
|
2020-08-27 16:10:55 +00:00
|
|
|
let x_3: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128()));
|
2020-08-22 20:15:39 +00:00
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Hash together all the openings provided by the prover into a new
|
|
|
|
|
// transcript on the scalar field.
|
2020-08-22 20:15:39 +00:00
|
|
|
let mut transcript_scalar = HScalar::init(C::Scalar::one());
|
2020-08-27 16:10:55 +00:00
|
|
|
|
2020-08-27 19:52:55 +00:00
|
|
|
for eval in self.advice_evals.iter().chain(self.fixed_evals.iter()).chain(self.h_evals.iter()) {
|
2020-08-22 20:15:39 +00:00
|
|
|
transcript_scalar.absorb(*eval);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
let transcript_scalar_point =
|
|
|
|
|
C::Base::from_bytes(&(transcript_scalar.squeeze()).to_bytes()).unwrap();
|
|
|
|
|
transcript.absorb(transcript_scalar_point);
|
|
|
|
|
|
2020-08-27 16:10:55 +00:00
|
|
|
// Evaluate the circuit using the custom gates provided
|
|
|
|
|
let mut h_eval = C::Scalar::zero();
|
|
|
|
|
for poly in srs.meta.gates.iter() {
|
|
|
|
|
h_eval *= &x_2;
|
|
|
|
|
|
|
|
|
|
let evaluation: C::Scalar = poly.evaluate(
|
2020-08-27 16:25:36 +00:00
|
|
|
&|index| self.fixed_evals[index],
|
|
|
|
|
&|index| self.advice_evals[index],
|
2020-08-27 16:10:55 +00:00
|
|
|
&|a, b| a + &b,
|
|
|
|
|
&|a, b| a * &b,
|
|
|
|
|
&|a, scalar| a * &scalar,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
h_eval += &evaluation;
|
|
|
|
|
}
|
|
|
|
|
let xn = x_3.pow(&[params.n as u64, 0, 0, 0]);
|
|
|
|
|
|
|
|
|
|
// Compute the expected h(x) value
|
|
|
|
|
let mut expected_h_eval = C::Scalar::zero();
|
|
|
|
|
let mut cur = C::Scalar::one();
|
2020-08-27 16:25:36 +00:00
|
|
|
for eval in &self.h_evals {
|
2020-08-27 16:10:55 +00:00
|
|
|
expected_h_eval += &(cur * eval);
|
|
|
|
|
cur *= &xn;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 19:52:55 +00:00
|
|
|
if h_eval != (expected_h_eval * &(xn - &C::Scalar::one())) {
|
2020-08-27 16:10:55 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// We are now convinced the circuit is satisfied so long as the
|
|
|
|
|
// polynomial commitments open to the correct values.
|
2020-08-22 20:15:39 +00:00
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Sample x_4 for compressing openings at the same points together
|
2020-08-27 16:10:55 +00:00
|
|
|
let x_4: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128()));
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Compress the commitments and expected evaluations at x_3 together
|
|
|
|
|
// using the challenge x_4
|
2020-08-27 19:52:55 +00:00
|
|
|
let mut q_commitments: Vec<Option<C::Projective>> = vec![None; srs.meta.rotations.len()];
|
2020-08-27 19:27:24 +00:00
|
|
|
let mut q_evals: Vec<_> = vec![C::Scalar::zero(); srs.meta.rotations.len()];
|
2020-08-27 16:10:55 +00:00
|
|
|
{
|
2020-08-27 19:52:55 +00:00
|
|
|
let mut accumulate = |point_index: usize, new_commitment, eval| {
|
|
|
|
|
q_commitments[point_index] = q_commitments[point_index].map(|mut commitment| {
|
|
|
|
|
commitment *= x_4;
|
|
|
|
|
commitment += new_commitment;
|
|
|
|
|
commitment
|
|
|
|
|
}).or_else(|| Some(new_commitment.to_projective()));
|
|
|
|
|
q_evals[point_index] *= &x_4;
|
|
|
|
|
q_evals[point_index] += &eval;
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-27 19:27:24 +00:00
|
|
|
for (query_index, &(wire, ref at)) in srs.meta.advice_queries.iter().enumerate() {
|
|
|
|
|
let point_index = (*srs.meta.rotations.get(at).unwrap()).0;
|
2020-08-27 19:52:55 +00:00
|
|
|
accumulate(point_index, self.advice_commitments[wire.0], self.advice_evals[query_index]);
|
2020-08-27 16:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-27 19:27:24 +00:00
|
|
|
for (query_index, &(wire, ref at)) in srs.meta.fixed_queries.iter().enumerate() {
|
|
|
|
|
let point_index = (*srs.meta.rotations.get(at).unwrap()).0;
|
2020-08-27 19:52:55 +00:00
|
|
|
accumulate(point_index, srs.fixed_commitments[wire.0], self.fixed_evals[query_index]);
|
2020-08-27 16:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-27 19:52:55 +00:00
|
|
|
let current_index = (*srs.meta.rotations.get(&Rotation::default()).unwrap()).0;
|
2020-08-27 16:25:36 +00:00
|
|
|
for (h_commitment, h_eval) in self.h_commitments.iter().zip(self.h_evals.iter()) {
|
2020-08-27 19:52:55 +00:00
|
|
|
accumulate(current_index, *h_commitment, *h_eval);
|
2020-08-27 16:10:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Sample a challenge x_5 for keeping the multi-point quotient
|
|
|
|
|
// polynomial terms linearly independent.
|
2020-08-27 16:10:55 +00:00
|
|
|
let x_5: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128()));
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Obtain the commitment to the multi-point quotient polynomial f(X).
|
2020-08-27 16:10:55 +00:00
|
|
|
hash_point(&mut transcript, &self.f_commitment)
|
|
|
|
|
.expect("proof cannot contain points at infinity");
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Sample a challenge x_6 for checking that f(X) was committed to
|
|
|
|
|
// correctly.
|
2020-08-27 16:10:55 +00:00
|
|
|
let x_6: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128()));
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
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 f_eval at x_6 using the q_evals provided
|
|
|
|
|
// by the prover and from x_5
|
2020-08-27 16:10:55 +00:00
|
|
|
let mut f_eval = C::Scalar::zero();
|
2020-08-27 19:27:24 +00:00
|
|
|
for (&row, &point_index) in srs.meta.rotations.iter() {
|
2020-08-27 19:52:55 +00:00
|
|
|
let mut eval = self.q_evals[point_index.0];
|
2020-08-27 19:27:24 +00:00
|
|
|
|
|
|
|
|
let point = srs.domain.rotate_omega(x_3, row);
|
|
|
|
|
eval = eval - &q_evals[point_index.0];
|
2020-08-27 16:10:55 +00:00
|
|
|
eval = eval * &(x_6 - &point).invert().unwrap();
|
|
|
|
|
|
|
|
|
|
f_eval *= &x_5;
|
|
|
|
|
f_eval += &eval;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Sample a challenge x_7 that we will use to collapse the openings of
|
|
|
|
|
// the various remaining polynomials at x_6 together.
|
2020-08-27 16:10:55 +00:00
|
|
|
let x_7: C::Scalar = get_challenge_scalar(Challenge(transcript.squeeze().get_lower_128()));
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Compute the final commitment that has to be opened
|
2020-08-27 16:10:55 +00:00
|
|
|
let mut f_commitment: C::Projective = self.f_commitment.to_projective();
|
2020-08-27 19:27:24 +00:00
|
|
|
for (_, &point_index) in srs.meta.rotations.iter() {
|
2020-08-27 16:10:55 +00:00
|
|
|
f_commitment *= x_7;
|
2020-08-27 19:27:24 +00:00
|
|
|
f_commitment = f_commitment + &q_commitments[point_index.0].as_ref().unwrap();
|
2020-08-27 16:10:55 +00:00
|
|
|
f_eval *= &x_7;
|
2020-08-27 19:27:24 +00:00
|
|
|
f_eval += &self.q_evals[point_index.0];
|
2020-08-27 16:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-27 16:46:54 +00:00
|
|
|
// Verify the opening proof
|
2020-08-27 16:10:55 +00:00
|
|
|
params.verify_proof(
|
|
|
|
|
&self.opening,
|
|
|
|
|
&mut transcript,
|
|
|
|
|
x_6,
|
|
|
|
|
&f_commitment.to_affine(),
|
|
|
|
|
f_eval,
|
|
|
|
|
)
|
2020-08-22 20:15:39 +00:00
|
|
|
}
|
|
|
|
|
}
|