1use group::{
2 ff::{BatchInvert, Field},
3 Curve,
4};
5use rand_core::RngCore;
6use std::iter::{self, ExactSizeIterator};
7
8use super::super::{circuit::Any, ChallengeBeta, ChallengeGamma, ChallengeX};
9use super::{Argument, ProvingKey};
10use crate::{
11 arithmetic::{eval_polynomial, parallelize, CurveAffine, FieldExt},
12 plonk::{self, Error},
13 poly::{
14 self,
15 commitment::{Blind, Params},
16 multiopen::ProverQuery,
17 Coeff, ExtendedLagrangeCoeff, LagrangeCoeff, Polynomial, Rotation,
18 },
19 transcript::{EncodedChallenge, TranscriptWrite},
20};
21
22pub struct CommittedSet<C: CurveAffine, Ev> {
23 permutation_product_poly: Polynomial<C::Scalar, Coeff>,
24 permutation_product_coset: poly::AstLeaf<Ev, ExtendedLagrangeCoeff>,
25 permutation_product_blind: Blind<C::Scalar>,
26}
27
28pub(crate) struct Committed<C: CurveAffine, Ev> {
29 sets: Vec<CommittedSet<C, Ev>>,
30}
31
32pub struct ConstructedSet<C: CurveAffine> {
33 permutation_product_poly: Polynomial<C::Scalar, Coeff>,
34 permutation_product_blind: Blind<C::Scalar>,
35}
36
37pub(crate) struct Constructed<C: CurveAffine> {
38 sets: Vec<ConstructedSet<C>>,
39}
40
41pub(crate) struct Evaluated<C: CurveAffine> {
42 constructed: Constructed<C>,
43}
44
45impl Argument {
46 pub(in crate::plonk) fn commit<
47 C: CurveAffine,
48 E: EncodedChallenge<C>,
49 Ev: Copy + Send + Sync,
50 R: RngCore,
51 T: TranscriptWrite<C, E>,
52 >(
53 &self,
54 params: &Params<C>,
55 pk: &plonk::ProvingKey<C>,
56 pkey: &ProvingKey<C>,
57 advice: &[Polynomial<C::Scalar, LagrangeCoeff>],
58 fixed: &[Polynomial<C::Scalar, LagrangeCoeff>],
59 instance: &[Polynomial<C::Scalar, LagrangeCoeff>],
60 beta: ChallengeBeta<C>,
61 gamma: ChallengeGamma<C>,
62 evaluator: &mut poly::Evaluator<Ev, C::Scalar, ExtendedLagrangeCoeff>,
63 mut rng: R,
64 transcript: &mut T,
65 ) -> Result<Committed<C, Ev>, Error> {
66 let domain = &pk.vk.domain;
67
68 assert!(pk.vk.cs.degree() >= 3);
73 let chunk_len = pk.vk.cs.degree() - 2;
74 let blinding_factors = pk.vk.cs.blinding_factors();
75
76 let mut deltaomega = C::Scalar::one();
78
79 let mut last_z = C::Scalar::one();
81
82 let mut sets = vec![];
83
84 for (columns, permutations) in self
85 .columns
86 .chunks(chunk_len)
87 .zip(pkey.permutations.chunks(chunk_len))
88 {
89 let mut modified_values = vec![C::Scalar::one(); params.n as usize];
98
99 for (&column, permuted_column_values) in columns.iter().zip(permutations.iter()) {
101 let values = match column.column_type() {
102 Any::Advice => advice,
103 Any::Fixed => fixed,
104 Any::Instance => instance,
105 };
106 parallelize(&mut modified_values, |modified_values, start| {
107 for ((modified_values, value), permuted_value) in modified_values
108 .iter_mut()
109 .zip(values[column.index()][start..].iter())
110 .zip(permuted_column_values[start..].iter())
111 {
112 *modified_values *= &(*beta * permuted_value + &*gamma + value);
113 }
114 });
115 }
116
117 modified_values.batch_invert();
119
120 for &column in columns.iter() {
123 let omega = domain.get_omega();
124 let values = match column.column_type() {
125 Any::Advice => advice,
126 Any::Fixed => fixed,
127 Any::Instance => instance,
128 };
129 parallelize(&mut modified_values, |modified_values, start| {
130 let mut deltaomega = deltaomega * &omega.pow_vartime(&[start as u64, 0, 0, 0]);
131 for (modified_values, value) in modified_values
132 .iter_mut()
133 .zip(values[column.index()][start..].iter())
134 {
135 *modified_values *= &(deltaomega * &*beta + &*gamma + value);
137 deltaomega *= ω
138 }
139 });
140 deltaomega *= &C::Scalar::DELTA;
141 }
142
143 let mut z = vec![last_z];
155 for row in 1..(params.n as usize) {
156 let mut tmp = z[row - 1];
157
158 tmp *= &modified_values[row - 1];
159 z.push(tmp);
160 }
161 let mut z = domain.lagrange_from_vec(z);
162 for z in &mut z[params.n as usize - blinding_factors..] {
164 *z = C::Scalar::random(&mut rng);
165 }
166 last_z = z[params.n as usize - (blinding_factors + 1)];
168
169 let blind = Blind(C::Scalar::random(&mut rng));
170
171 let permutation_product_commitment_projective = params.commit_lagrange(&z, blind);
172 let permutation_product_blind = blind;
173 let z = domain.lagrange_to_coeff(z);
174 let permutation_product_poly = z.clone();
175
176 let permutation_product_coset =
177 evaluator.register_poly(domain.coeff_to_extended(z.clone()));
178
179 let permutation_product_commitment =
180 permutation_product_commitment_projective.to_affine();
181
182 transcript.write_point(permutation_product_commitment)?;
184
185 sets.push(CommittedSet {
186 permutation_product_poly,
187 permutation_product_coset,
188 permutation_product_blind,
189 });
190 }
191
192 Ok(Committed { sets })
193 }
194}
195
196impl<C: CurveAffine, Ev: Copy + Send + Sync> Committed<C, Ev> {
197 pub(in crate::plonk) fn construct<'a>(
198 self,
199 pk: &'a plonk::ProvingKey<C>,
200 p: &'a Argument,
201 advice_cosets: &'a [poly::AstLeaf<Ev, ExtendedLagrangeCoeff>],
202 fixed_cosets: &'a [poly::AstLeaf<Ev, ExtendedLagrangeCoeff>],
203 instance_cosets: &'a [poly::AstLeaf<Ev, ExtendedLagrangeCoeff>],
204 permutation_cosets: &'a [poly::AstLeaf<Ev, ExtendedLagrangeCoeff>],
205 l0: poly::AstLeaf<Ev, ExtendedLagrangeCoeff>,
206 l_blind: poly::AstLeaf<Ev, ExtendedLagrangeCoeff>,
207 l_last: poly::AstLeaf<Ev, ExtendedLagrangeCoeff>,
208 beta: ChallengeBeta<C>,
209 gamma: ChallengeGamma<C>,
210 ) -> (
211 Constructed<C>,
212 impl Iterator<Item = poly::Ast<Ev, C::Scalar, ExtendedLagrangeCoeff>> + 'a,
213 ) {
214 let chunk_len = pk.vk.cs.degree() - 2;
215 let blinding_factors = pk.vk.cs.blinding_factors();
216 let last_rotation = Rotation(-((blinding_factors + 1) as i32));
217
218 let constructed = Constructed {
219 sets: self
220 .sets
221 .iter()
222 .map(|set| ConstructedSet {
223 permutation_product_poly: set.permutation_product_poly.clone(),
224 permutation_product_blind: set.permutation_product_blind,
225 })
226 .collect(),
227 };
228
229 let expressions = iter::empty()
230 .chain(
233 self.sets
234 .first()
235 .map(|first_set| (poly::Ast::one() - first_set.permutation_product_coset) * l0),
236 )
237 .chain(self.sets.last().map(|last_set| {
240 ((poly::Ast::from(last_set.permutation_product_coset)
241 * last_set.permutation_product_coset)
242 - last_set.permutation_product_coset)
243 * l_last
244 }))
245 .chain(
248 self.sets
249 .iter()
250 .skip(1)
251 .zip(self.sets.iter())
252 .map(|(set, last_set)| {
253 (poly::Ast::from(set.permutation_product_coset)
254 - last_set
255 .permutation_product_coset
256 .with_rotation(last_rotation))
257 * l0
258 })
259 .collect::<Vec<_>>(),
260 )
261 .chain(
267 self.sets
268 .into_iter()
269 .zip(p.columns.chunks(chunk_len))
270 .zip(permutation_cosets.chunks(chunk_len))
271 .enumerate()
272 .map(move |(chunk_index, ((set, columns), cosets))| {
273 let mut left = poly::Ast::<_, C::Scalar, _>::from(
274 set.permutation_product_coset
275 .with_rotation(Rotation::next()),
276 );
277 for (values, permutation) in columns
278 .iter()
279 .map(|&column| match column.column_type() {
280 Any::Advice => &advice_cosets[column.index()],
281 Any::Fixed => &fixed_cosets[column.index()],
282 Any::Instance => &instance_cosets[column.index()],
283 })
284 .zip(cosets.iter())
285 {
286 left *= poly::Ast::<_, C::Scalar, _>::from(*values)
287 + (poly::Ast::ConstantTerm(*beta) * poly::Ast::from(*permutation))
288 + poly::Ast::ConstantTerm(*gamma);
289 }
290
291 let mut right = poly::Ast::from(set.permutation_product_coset);
292 let mut current_delta = *beta
293 * &(C::Scalar::DELTA.pow_vartime(&[(chunk_index * chunk_len) as u64]));
294 for values in columns.iter().map(|&column| match column.column_type() {
295 Any::Advice => &advice_cosets[column.index()],
296 Any::Fixed => &fixed_cosets[column.index()],
297 Any::Instance => &instance_cosets[column.index()],
298 }) {
299 right *= poly::Ast::from(*values)
300 + poly::Ast::LinearTerm(current_delta)
301 + poly::Ast::ConstantTerm(*gamma);
302 current_delta *= &C::Scalar::DELTA;
303 }
304
305 (left - right) * (poly::Ast::one() - (poly::Ast::from(l_last) + l_blind))
306 }),
307 );
308
309 (constructed, expressions)
310 }
311}
312
313impl<C: CurveAffine> super::ProvingKey<C> {
314 pub(in crate::plonk) fn open(
315 &self,
316 x: ChallengeX<C>,
317 ) -> impl Iterator<Item = ProverQuery<'_, C>> + Clone {
318 self.polys.iter().map(move |poly| ProverQuery {
319 point: *x,
320 poly,
321 blind: Blind::default(),
322 })
323 }
324
325 pub(in crate::plonk) fn evaluate<E: EncodedChallenge<C>, T: TranscriptWrite<C, E>>(
326 &self,
327 x: ChallengeX<C>,
328 transcript: &mut T,
329 ) -> Result<(), Error> {
330 for eval in self.polys.iter().map(|poly| eval_polynomial(poly, *x)) {
332 transcript.write_scalar(eval)?;
333 }
334
335 Ok(())
336 }
337}
338
339impl<C: CurveAffine> Constructed<C> {
340 pub(in crate::plonk) fn evaluate<E: EncodedChallenge<C>, T: TranscriptWrite<C, E>>(
341 self,
342 pk: &plonk::ProvingKey<C>,
343 x: ChallengeX<C>,
344 transcript: &mut T,
345 ) -> Result<Evaluated<C>, Error> {
346 let domain = &pk.vk.domain;
347 let blinding_factors = pk.vk.cs.blinding_factors();
348
349 {
350 let mut sets = self.sets.iter();
351
352 while let Some(set) = sets.next() {
353 let permutation_product_eval = eval_polynomial(&set.permutation_product_poly, *x);
354
355 let permutation_product_next_eval = eval_polynomial(
356 &set.permutation_product_poly,
357 domain.rotate_omega(*x, Rotation::next()),
358 );
359
360 for eval in iter::empty()
362 .chain(Some(&permutation_product_eval))
363 .chain(Some(&permutation_product_next_eval))
364 {
365 transcript.write_scalar(*eval)?;
366 }
367
368 if sets.len() > 0 {
372 let permutation_product_last_eval = eval_polynomial(
373 &set.permutation_product_poly,
374 domain.rotate_omega(*x, Rotation(-((blinding_factors + 1) as i32))),
375 );
376
377 transcript.write_scalar(permutation_product_last_eval)?;
378 }
379 }
380 }
381
382 Ok(Evaluated { constructed: self })
383 }
384}
385
386impl<C: CurveAffine> Evaluated<C> {
387 pub(in crate::plonk) fn open<'a>(
388 &'a self,
389 pk: &'a plonk::ProvingKey<C>,
390 x: ChallengeX<C>,
391 ) -> impl Iterator<Item = ProverQuery<'a, C>> + Clone {
392 let blinding_factors = pk.vk.cs.blinding_factors();
393 let x_next = pk.vk.domain.rotate_omega(*x, Rotation::next());
394 let x_last = pk
395 .vk
396 .domain
397 .rotate_omega(*x, Rotation(-((blinding_factors + 1) as i32)));
398
399 iter::empty()
400 .chain(self.constructed.sets.iter().flat_map(move |set| {
401 iter::empty()
402 .chain(Some(ProverQuery {
404 point: *x,
405 poly: &set.permutation_product_poly,
406 blind: set.permutation_product_blind,
407 }))
408 .chain(Some(ProverQuery {
409 point: x_next,
410 poly: &set.permutation_product_poly,
411 blind: set.permutation_product_blind,
412 }))
413 }))
414 .chain(
418 self.constructed
419 .sets
420 .iter()
421 .rev()
422 .skip(1)
423 .flat_map(move |set| {
424 Some(ProverQuery {
425 point: x_last,
426 poly: &set.permutation_product_poly,
427 blind: set.permutation_product_blind,
428 })
429 }),
430 )
431 }
432}