halo2_proofs/poly/commitment/
prover.rs

1use ff::Field;
2use rand_core::RngCore;
3
4use 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};
10
11use group::Curve;
12use std::io;
13
14/// 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>,
34    mut 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.
41    assert_eq!(p_poly.len(), params.n as usize);
42
43    // Sample a random polynomial (of same degree) that has a root at x_3, first
44    // by setting all coefficients to random values.
45    let mut s_poly = (*p_poly).clone();
46    for coeff in s_poly.iter_mut() {
47        *coeff = C::Scalar::random(&mut rng);
48    }
49    // Evaluate the random polynomial at x_3
50    let s_at_x3 = eval_polynomial(&s_poly[..], x_3);
51    // Subtract constant coefficient to get a random polynomial with a root at x_3
52    s_poly[0] = s_poly[0] - &s_at_x3;
53    // And sample a random blind
54    let s_poly_blind = Blind(C::Scalar::random(&mut rng));
55
56    // Write a commitment to the random polynomial to the transcript
57    let s_poly_commitment = params.commit(&s_poly, s_poly_blind).to_affine();
58    transcript.write_point(s_poly_commitment)?;
59
60    // 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.
63    let xi = *transcript.squeeze_challenge_scalar::<()>();
64
65    // Challenge that ensures that the prover did not interfere with the U term
66    // in their commitments.
67    let z = *transcript.squeeze_challenge_scalar::<()>();
68
69    // We'll be opening `P' = P - [v] G_0 + [ξ] S` to ensure it has a root at
70    // zero.
71    let mut p_prime_poly = s_poly * xi + p_poly;
72    let v = eval_polynomial(&p_prime_poly, x_3);
73    p_prime_poly[0] = p_prime_poly[0] - &v;
74    let p_prime_blind = s_poly_blind * Blind(xi) + p_blind;
75
76    // This accumulates the synthetic blinding factor `f` starting
77    // with the blinding factor for `P'`.
78    let mut f = p_prime_blind.0;
79
80    // Initialize the vector `p_prime` as the coefficients of the polynomial.
81    let mut p_prime = p_prime_poly.values;
82    assert_eq!(p_prime.len(), params.n as usize);
83
84    // 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`.
86    let mut b = Vec::with_capacity(1 << params.k);
87    {
88        let mut cur = C::Scalar::one();
89        for _ in 0..(1 << params.k) {
90            b.push(cur);
91            cur *= &x_3;
92        }
93    }
94
95    // 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.
97    let mut g_prime = params.g.clone();
98
99    // Perform the inner product argument, round by round.
100    for j in 0..params.k {
101        let half = 1 << (params.k - j - 1); // half the length of `p_prime`, `b`, `G'`
102
103        // 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.
107        let l_j = best_multiexp(&p_prime[half..], &g_prime[0..half]);
108        let r_j = best_multiexp(&p_prime[0..half], &g_prime[half..]);
109        let value_l_j = compute_inner_product(&p_prime[half..], &b[0..half]);
110        let value_r_j = compute_inner_product(&p_prime[0..half], &b[half..]);
111        let l_j_randomness = C::Scalar::random(&mut rng);
112        let r_j_randomness = C::Scalar::random(&mut rng);
113        let l_j = l_j + &best_multiexp(&[value_l_j * &z, l_j_randomness], &[params.u, params.w]);
114        let r_j = r_j + &best_multiexp(&[value_r_j * &z, r_j_randomness], &[params.u, params.w]);
115        let l_j = l_j.to_affine();
116        let r_j = r_j.to_affine();
117
118        // Feed L and R into the real transcript
119        transcript.write_point(l_j)?;
120        transcript.write_point(r_j)?;
121
122        let u_j = *transcript.squeeze_challenge_scalar::<()>();
123        let u_j_inv = u_j.invert().unwrap(); // TODO, bubble this up
124
125        // Collapse `p_prime` and `b`.
126        // TODO: parallelize
127        for 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);
133
134        // Collapse `G'`
135        parallel_generator_collapse(&mut g_prime, u_j);
136        g_prime.truncate(half);
137
138        // Update randomness (the synthetic blinding factor at the end)
139        f += &(l_j_randomness * &u_j_inv);
140        f += &(r_j_randomness * &u_j);
141    }
142
143    // We have fully collapsed `p_prime`, `b`, `G'`
144    assert_eq!(p_prime.len(), 1);
145    let c = p_prime[0];
146
147    transcript.write_scalar(c)?;
148    transcript.write_scalar(f)?;
149
150    Ok(())
151}
152
153fn parallel_generator_collapse<C: CurveAffine>(g: &mut [C], challenge: C::Scalar) {
154    let len = g.len() / 2;
155    let (g_lo, g_hi) = g.split_at_mut(len);
156
157    parallelize(g_lo, |g_lo, start| {
158        let g_hi = &g_hi[start..];
159        let mut tmp = Vec::with_capacity(g_lo.len());
160        for (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}