1use ff::Field;
2use rand_core::RngCore;
34use super::super::{Coeff, Polynomial};
5use super::{Blind, Params};
6use crate::arithmetic::{
7 best_multiexp, compute_inner_product, eval_polynomial, parallelize, CurveAffine, FieldExt,
8};
9use crate::transcript::{EncodedChallenge, TranscriptWrite};
1011use group::Curve;
12use std::io;
1314/// Create a polynomial commitment opening proof for the polynomial defined
15/// by the coefficients `px`, the blinding factor `blind` used for the
16/// polynomial commitment, and the point `x` that the polynomial is
17/// evaluated at.
18///
19/// This function will panic if the provided polynomial is too large with
20/// respect to the polynomial commitment parameters.
21///
22/// **Important:** This function assumes that the provided `transcript` has
23/// already seen the common inputs: the polynomial commitment P, the claimed
24/// opening v, and the point x. It's probably also nice for the transcript
25/// to have seen the elliptic curve description and the URS, if you want to
26/// be rigorous.
27pub fn create_proof<
28 C: CurveAffine,
29 E: EncodedChallenge<C>,
30 R: RngCore,
31 T: TranscriptWrite<C, E>,
32>(
33 params: &Params<C>,
34mut rng: R,
35 transcript: &mut T,
36 p_poly: &Polynomial<C::Scalar, Coeff>,
37 p_blind: Blind<C::Scalar>,
38 x_3: C::Scalar,
39) -> io::Result<()> {
40// We're limited to polynomials of degree n - 1.
41assert_eq!(p_poly.len(), params.n as usize);
4243// Sample a random polynomial (of same degree) that has a root at x_3, first
44 // by setting all coefficients to random values.
45let mut s_poly = (*p_poly).clone();
46for coeff in s_poly.iter_mut() {
47*coeff = C::Scalar::random(&mut rng);
48 }
49// Evaluate the random polynomial at x_3
50let s_at_x3 = eval_polynomial(&s_poly[..], x_3);
51// Subtract constant coefficient to get a random polynomial with a root at x_3
52s_poly[0] = s_poly[0] - &s_at_x3;
53// And sample a random blind
54let s_poly_blind = Blind(C::Scalar::random(&mut rng));
5556// Write a commitment to the random polynomial to the transcript
57let s_poly_commitment = params.commit(&s_poly, s_poly_blind).to_affine();
58 transcript.write_point(s_poly_commitment)?;
5960// Challenge that will ensure that the prover cannot change P but can only
61 // witness a random polynomial commitment that agrees with P at x_3, with high
62 // probability.
63let xi = *transcript.squeeze_challenge_scalar::<()>();
6465// Challenge that ensures that the prover did not interfere with the U term
66 // in their commitments.
67let z = *transcript.squeeze_challenge_scalar::<()>();
6869// We'll be opening `P' = P - [v] G_0 + [ξ] S` to ensure it has a root at
70 // zero.
71let mut p_prime_poly = s_poly * xi + p_poly;
72let v = eval_polynomial(&p_prime_poly, x_3);
73 p_prime_poly[0] = p_prime_poly[0] - &v;
74let p_prime_blind = s_poly_blind * Blind(xi) + p_blind;
7576// This accumulates the synthetic blinding factor `f` starting
77 // with the blinding factor for `P'`.
78let mut f = p_prime_blind.0;
7980// Initialize the vector `p_prime` as the coefficients of the polynomial.
81let mut p_prime = p_prime_poly.values;
82assert_eq!(p_prime.len(), params.n as usize);
8384// Initialize the vector `b` as the powers of `x_3`. The inner product of
85 // `p_prime` and `b` is the evaluation of the polynomial at `x_3`.
86let mut b = Vec::with_capacity(1 << params.k);
87 {
88let mut cur = C::Scalar::one();
89for _ in 0..(1 << params.k) {
90 b.push(cur);
91 cur *= &x_3;
92 }
93 }
9495// Initialize the vector `G'` from the URS. We'll be progressively collapsing
96 // this vector into smaller and smaller vectors until it is of length 1.
97let mut g_prime = params.g.clone();
9899// Perform the inner product argument, round by round.
100for j in 0..params.k {
101let half = 1 << (params.k - j - 1); // half the length of `p_prime`, `b`, `G'`
102103 // Compute L, R
104 //
105 // TODO: If we modify multiexp to take "extra" bases, we could speed
106 // this piece up a bit by combining the multiexps.
107let l_j = best_multiexp(&p_prime[half..], &g_prime[0..half]);
108let r_j = best_multiexp(&p_prime[0..half], &g_prime[half..]);
109let value_l_j = compute_inner_product(&p_prime[half..], &b[0..half]);
110let value_r_j = compute_inner_product(&p_prime[0..half], &b[half..]);
111let l_j_randomness = C::Scalar::random(&mut rng);
112let r_j_randomness = C::Scalar::random(&mut rng);
113let l_j = l_j + &best_multiexp(&[value_l_j * &z, l_j_randomness], &[params.u, params.w]);
114let r_j = r_j + &best_multiexp(&[value_r_j * &z, r_j_randomness], &[params.u, params.w]);
115let l_j = l_j.to_affine();
116let r_j = r_j.to_affine();
117118// Feed L and R into the real transcript
119transcript.write_point(l_j)?;
120 transcript.write_point(r_j)?;
121122let u_j = *transcript.squeeze_challenge_scalar::<()>();
123let u_j_inv = u_j.invert().unwrap(); // TODO, bubble this up
124125 // Collapse `p_prime` and `b`.
126 // TODO: parallelize
127for i in 0..half {
128 p_prime[i] = p_prime[i] + &(p_prime[i + half] * &u_j_inv);
129 b[i] = b[i] + &(b[i + half] * &u_j);
130 }
131 p_prime.truncate(half);
132 b.truncate(half);
133134// Collapse `G'`
135parallel_generator_collapse(&mut g_prime, u_j);
136 g_prime.truncate(half);
137138// Update randomness (the synthetic blinding factor at the end)
139f += &(l_j_randomness * &u_j_inv);
140 f += &(r_j_randomness * &u_j);
141 }
142143// We have fully collapsed `p_prime`, `b`, `G'`
144assert_eq!(p_prime.len(), 1);
145let c = p_prime[0];
146147 transcript.write_scalar(c)?;
148 transcript.write_scalar(f)?;
149150Ok(())
151}
152153fn parallel_generator_collapse<C: CurveAffine>(g: &mut [C], challenge: C::Scalar) {
154let len = g.len() / 2;
155let (g_lo, g_hi) = g.split_at_mut(len);
156157 parallelize(g_lo, |g_lo, start| {
158let g_hi = &g_hi[start..];
159let mut tmp = Vec::with_capacity(g_lo.len());
160for (g_lo, g_hi) in g_lo.iter().zip(g_hi.iter()) {
161 tmp.push(g_lo.to_curve() + &(*g_hi * challenge));
162 }
163 C::Curve::batch_normalize(&tmp, g_lo);
164 });
165}