@@ -7,6 +7,7 @@ This source file implements prover's zk-proof primitive.
7
7
pub use super :: { index:: Index , range} ;
8
8
use crate :: plonk_sponge:: FrSponge ;
9
9
use ark_ec:: AffineCurve ;
10
+ use ark_ff:: UniformRand ;
10
11
use ark_ff:: { FftField , Field , One , Zero } ;
11
12
use ark_poly:: {
12
13
univariate:: DensePolynomial , Evaluations , Polynomial , Radix2EvaluationDomain as D , UVPolynomial ,
@@ -18,14 +19,14 @@ use commitment_dlog::commitment::{
18
19
use lookup:: CombinedEntry ;
19
20
use o1_utils:: ExtendedDensePolynomial ;
20
21
use oracle:: { rndoracle:: ProofError , sponge:: ScalarChallenge , FqSponge } ;
22
+ use plonk_15_wires_circuits:: nolookup:: constraints:: ZK_ROWS ;
21
23
use plonk_15_wires_circuits:: {
22
24
expr:: { l0_1, Constants , Environment , LookupEnvironment } ,
23
25
gate:: { combine_table_entry, GateType , LookupInfo , LookupsUsed } ,
24
26
nolookup:: scalars:: { LookupEvaluations , ProofEvaluations } ,
25
27
polynomials:: { chacha, complete_add, endomul_scalar, endosclmul, lookup, poseidon, varbasemul} ,
26
28
wires:: { COLUMNS , PERMUTS } ,
27
29
} ;
28
- use rand:: thread_rng;
29
30
use std:: collections:: HashMap ;
30
31
31
32
type Fr < G > = <G as AffineCurve >:: ScalarField ;
@@ -105,18 +106,42 @@ where
105
106
// RETURN: prover's zk-proof
106
107
pub fn create < EFqSponge : Clone + FqSponge < Fq < G > , G , Fr < G > > , EFrSponge : FrSponge < Fr < G > > > (
107
108
group_map : & G :: Map ,
108
- witness : & [ Vec < Fr < G > > ; COLUMNS ] ,
109
+ mut witness : [ Vec < Fr < G > > ; COLUMNS ] ,
109
110
index : & Index < G > ,
110
111
prev_challenges : Vec < ( Vec < Fr < G > > , PolyComm < G > ) > ,
111
112
) -> Result < Self , ProofError > {
112
- let d1 = index. cs . domain . d1 ;
113
- let n = index. cs . domain . d1 . size as usize ;
114
- for w in witness. iter ( ) {
115
- if w. len ( ) != n {
113
+ let d1_size = index. cs . domain . d1 . size as usize ;
114
+ // TODO: rng should be passed as arg
115
+ let rng = & mut rand:: rngs:: OsRng ;
116
+
117
+ // double-check the witness
118
+ if cfg ! ( test) {
119
+ index. cs . verify ( & witness) . expect ( "incorrect witness" ) ;
120
+ }
121
+
122
+ // ensure we have room for the zero-knowledge rows
123
+ let length_witness = witness[ 0 ] . len ( ) ;
124
+ let length_padding = d1_size
125
+ . checked_sub ( length_witness)
126
+ . ok_or_else ( || ProofError :: NoRoomForZkInWitness ) ?;
127
+ if length_padding < ZK_ROWS as usize {
128
+ return Err ( ProofError :: NoRoomForZkInWitness ) ;
129
+ }
130
+
131
+ // pad and add zero-knowledge rows to the witness columns
132
+ for w in & mut witness {
133
+ if w. len ( ) != length_witness {
116
134
return Err ( ProofError :: WitnessCsInconsistent ) ;
117
135
}
136
+
137
+ // padding
138
+ w. extend ( std:: iter:: repeat ( Fr :: < G > :: zero ( ) ) . take ( length_padding) ) ;
139
+
140
+ // zk-rows
141
+ for row in w. iter_mut ( ) . rev ( ) . take ( ZK_ROWS as usize ) {
142
+ * row = Fr :: < G > :: rand ( rng) ;
143
+ }
118
144
}
119
- //if index.cs.verify(witness) != true {return Err(ProofError::WitnessCsInconsistent)};
120
145
121
146
// the transcript of the random oracle non-interactive argument
122
147
let mut fq_sponge = EFqSponge :: new ( index. fq_sponge_params . clone ( ) ) ;
@@ -129,15 +154,15 @@ where
129
154
)
130
155
. interpolate ( ) ;
131
156
132
- let rng = & mut thread_rng ( ) ;
133
-
134
157
// commit to the wire values
135
158
let w_comm: [ ( PolyComm < G > , PolyComm < Fr < G > > ) ; COLUMNS ] = array_init ( |i| {
136
159
let e = Evaluations :: < Fr < G > , D < Fr < G > > > :: from_vec_and_domain (
137
160
witness[ i] . clone ( ) ,
138
161
index. cs . domain . d1 ,
139
162
) ;
140
- index. srs . commit_evaluations ( d1, & e, None , rng)
163
+ index
164
+ . srs
165
+ . commit_evaluations ( index. cs . domain . d1 , & e, None , rng)
141
166
} ) ;
142
167
143
168
// compute witness polynomials
@@ -224,7 +249,7 @@ where
224
249
None => ( None , None , None , None ) ,
225
250
Some ( _) => {
226
251
let iter_lookup_table = || {
227
- ( 0 ..n ) . map ( |i| {
252
+ ( 0 ..d1_size ) . map ( |i| {
228
253
let row = index. cs . lookup_tables8 [ 0 ] . iter ( ) . map ( |e| & e. evals [ 8 * i] ) ;
229
254
CombinedEntry ( combine_table_entry ( joint_combiner, row) )
230
255
} )
@@ -237,7 +262,7 @@ where
237
262
dummy_lookup_value. clone ( ) ,
238
263
iter_lookup_table,
239
264
index. cs . lookup_table_lengths [ 0 ] ,
240
- d1,
265
+ index . cs . domain . d1 ,
241
266
& index. cs . gates ,
242
267
& witness,
243
268
joint_combiner,
@@ -247,13 +272,17 @@ where
247
272
. into_iter ( )
248
273
. map ( |chunk| {
249
274
let v: Vec < _ > = chunk. into_iter ( ) . map ( |x| x. 0 ) . collect ( ) ;
250
- lookup:: zk_patch ( v, d1, rng)
275
+ lookup:: zk_patch ( v, index . cs . domain . d1 , rng)
251
276
} )
252
277
. collect ( ) ;
253
278
254
279
let comm: Vec < _ > = lookup_sorted
255
280
. iter ( )
256
- . map ( |v| index. srs . commit_evaluations ( d1, v, None , rng) )
281
+ . map ( |v| {
282
+ index
283
+ . srs
284
+ . commit_evaluations ( index. cs . domain . d1 , v, None , rng)
285
+ } )
257
286
. collect ( ) ;
258
287
let coeffs : Vec < _ > =
259
288
// TODO: We can avoid storing these coefficients.
@@ -279,7 +308,7 @@ where
279
308
match lookup_sorted {
280
309
None => ( None , None , None ) ,
281
310
Some ( lookup_sorted) => {
282
- let iter_lookup_table = || ( 0 ..n ) . map ( |i| {
311
+ let iter_lookup_table = || ( 0 ..d1_size ) . map ( |i| {
283
312
let row = index. cs . lookup_tables8 [ 0 ] . iter ( ) . map ( |e| & e. evals [ 8 * i] ) ;
284
313
combine_table_entry ( joint_combiner, row)
285
314
} ) ;
@@ -288,7 +317,7 @@ where
288
317
lookup:: aggregation :: < _ , Fr < G > , _ > (
289
318
dummy_lookup_value. 0 ,
290
319
iter_lookup_table ( ) ,
291
- d1,
320
+ index . cs . domain . d1 ,
292
321
& index. cs . gates ,
293
322
& witness,
294
323
joint_combiner,
@@ -297,11 +326,11 @@ where
297
326
rng) ?;
298
327
299
328
drop ( lookup_sorted) ;
300
- if aggreg. evals [ n - 4 ] != Fr :: < G > :: one ( ) {
301
- panic ! ( "aggregation incorrect: {}" , aggreg. evals[ n- 3 ] ) ;
329
+ if aggreg. evals [ d1_size - ( ZK_ROWS as usize + 1 ) ] != Fr :: < G > :: one ( ) {
330
+ panic ! ( "aggregation incorrect: {}" , aggreg. evals[ d1_size- ( ZK_ROWS as usize + 1 ) ] ) ;
302
331
}
303
332
304
- let comm = index. srs . commit_evaluations ( d1, & aggreg, None , rng) ;
333
+ let comm = index. srs . commit_evaluations ( index . cs . domain . d1 , & aggreg, None , rng) ;
305
334
fq_sponge. absorb_g ( & comm. 0 . unshifted ) ;
306
335
307
336
let coeffs = aggreg. interpolate ( ) ;
@@ -314,7 +343,7 @@ where
314
343
} ;
315
344
316
345
// compute permutation aggregation polynomial
317
- let z = index. cs . perm_aggreg ( witness, & beta, & gamma, rng) ?;
346
+ let z = index. cs . perm_aggreg ( & witness, & beta, & gamma, rng) ?;
318
347
// commit to z
319
348
let z_comm = index. srs . commit ( & z, None , rng) ;
320
349
@@ -381,7 +410,7 @@ where
381
410
coefficient : & index. cs . coefficients8 ,
382
411
vanishes_on_last_4_rows : & index. cs . vanishes_on_last_4_rows ,
383
412
z : & lagrange. d8 . this . z ,
384
- l0_1 : l0_1 ( d1) ,
413
+ l0_1 : l0_1 ( index . cs . domain . d1 ) ,
385
414
domain : index. cs . domain ,
386
415
index : index_evals,
387
416
lookup : lookup_env,
@@ -440,7 +469,7 @@ where
440
469
( t4, t8) ,
441
470
alpha,
442
471
alphas[ alphas. len ( ) - 1 ] ,
443
- lookup:: constraints ( & index. cs . dummy_lookup_values [ 0 ] , d1)
472
+ lookup:: constraints ( & index. cs . dummy_lookup_values [ 0 ] , index . cs . domain . d1 )
444
473
. iter ( )
445
474
. map ( |e| e. evaluations ( & env) )
446
475
. collect ( ) ,
@@ -606,8 +635,8 @@ where
606
635
)
607
636
} )
608
637
. collect :: < Vec < _ > > ( ) ;
609
- let non_hiding = |n : usize | PolyComm {
610
- unshifted : vec ! [ Fr :: <G >:: zero( ) ; n ] ,
638
+ let non_hiding = |d1_size : usize | PolyComm {
639
+ unshifted : vec ! [ Fr :: <G >:: zero( ) ; d1_size ] ,
611
640
shifted : None ,
612
641
} ;
613
642
@@ -627,7 +656,7 @@ where
627
656
// construct evaluation proof
628
657
let mut polynomials = polys
629
658
. iter ( )
630
- . map ( |( p, n ) | ( p, None , non_hiding ( * n ) ) )
659
+ . map ( |( p, d1_size ) | ( p, None , non_hiding ( * d1_size ) ) )
631
660
. collect :: < Vec < _ > > ( ) ;
632
661
polynomials. extend ( vec ! [ ( & p, None , non_hiding( 1 ) ) ] ) ;
633
662
polynomials. extend ( vec ! [ ( & ft, None , blinding_ft) ] ) ;
0 commit comments