@@ -7,6 +7,7 @@ This source file implements prover's zk-proof primitive.
77pub  use  super :: { index:: Index ,  range} ; 
88use  crate :: plonk_sponge:: FrSponge ; 
99use  ark_ec:: AffineCurve ; 
10+ use  ark_ff:: UniformRand ; 
1011use  ark_ff:: { FftField ,  Field ,  One ,  Zero } ; 
1112use  ark_poly:: { 
1213    univariate:: DensePolynomial ,  Evaluations ,  Polynomial ,  Radix2EvaluationDomain  as  D ,  UVPolynomial , 
@@ -18,14 +19,14 @@ use commitment_dlog::commitment::{
1819use  lookup:: CombinedEntry ; 
1920use  o1_utils:: ExtendedDensePolynomial ; 
2021use  oracle:: { rndoracle:: ProofError ,  sponge:: ScalarChallenge ,  FqSponge } ; 
22+ use  plonk_15_wires_circuits:: nolookup:: constraints:: ZK_ROWS ; 
2123use  plonk_15_wires_circuits:: { 
2224    expr:: { l0_1,  Constants ,  Environment ,  LookupEnvironment } , 
2325    gate:: { combine_table_entry,  GateType ,  LookupInfo ,  LookupsUsed } , 
2426    nolookup:: scalars:: { LookupEvaluations ,  ProofEvaluations } , 
2527    polynomials:: { chacha,  complete_add,  endomul_scalar,  endosclmul,  lookup,  poseidon,  varbasemul} , 
2628    wires:: { COLUMNS ,  PERMUTS } , 
2729} ; 
28- use  rand:: thread_rng; 
2930use  std:: collections:: HashMap ; 
3031
3132type  Fr < G >  = <G  as  AffineCurve >:: ScalarField ; 
@@ -105,18 +106,42 @@ where
105106    //     RETURN: prover's zk-proof 
106107    pub  fn  create < EFqSponge :  Clone  + FqSponge < Fq < G > ,  G ,  Fr < G > > ,  EFrSponge :  FrSponge < Fr < G > > > ( 
107108        group_map :  & G :: Map , 
108-         witness :  & [ Vec < Fr < G > > ;  COLUMNS ] , 
109+         mut   witness :  [ Vec < Fr < G > > ;  COLUMNS ] , 
109110        index :  & Index < G > , 
110111        prev_challenges :  Vec < ( Vec < Fr < G > > ,  PolyComm < G > ) > , 
111112    )  -> 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 :: WitnessCsInconsistent ) ?; 
127+         if  length_padding < ZK_ROWS  as  usize  { 
128+             return  Err ( ProofError :: WitnessCsInconsistent ) ; 
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 { 
116134                return  Err ( ProofError :: WitnessCsInconsistent ) ; 
117135            } 
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+             } 
118144        } 
119-         //if index.cs.verify(witness) != true {return Err(ProofError::WitnessCsInconsistent)}; 
120145
121146        // the transcript of the random oracle non-interactive argument 
122147        let  mut  fq_sponge = EFqSponge :: new ( index. fq_sponge_params . clone ( ) ) ; 
@@ -129,15 +154,15 @@ where
129154        ) 
130155        . interpolate ( ) ; 
131156
132-         let  rng = & mut  thread_rng ( ) ; 
133- 
134157        // commit to the wire values 
135158        let  w_comm:  [ ( PolyComm < G > ,  PolyComm < Fr < G > > ) ;  COLUMNS ]  = array_init ( |i| { 
136159            let  e = Evaluations :: < Fr < G > ,  D < Fr < G > > > :: from_vec_and_domain ( 
137160                witness[ i] . clone ( ) , 
138161                index. cs . domain . d1 , 
139162            ) ; 
140-             index. srs . commit_evaluations ( d1,  & e,  None ,  rng) 
163+             index
164+                 . srs 
165+                 . commit_evaluations ( index. cs . domain . d1 ,  & e,  None ,  rng) 
141166        } ) ; 
142167
143168        // compute witness polynomials 
@@ -224,7 +249,7 @@ where
224249                None  => ( None ,  None ,  None ,  None ) , 
225250                Some ( _)  => { 
226251                    let  iter_lookup_table = || { 
227-                         ( 0 ..n ) . map ( |i| { 
252+                         ( 0 ..d1_size ) . map ( |i| { 
228253                            let  row = index. cs . lookup_tables8 [ 0 ] . iter ( ) . map ( |e| & e. evals [ 8  *  i] ) ; 
229254                            CombinedEntry ( combine_table_entry ( joint_combiner,  row) ) 
230255                        } ) 
@@ -237,7 +262,7 @@ where
237262                        dummy_lookup_value. clone ( ) , 
238263                        iter_lookup_table, 
239264                        index. cs . lookup_table_lengths [ 0 ] , 
240-                         d1, 
265+                         index . cs . domain . d1 , 
241266                        & index. cs . gates , 
242267                        & witness, 
243268                        joint_combiner, 
@@ -247,13 +272,17 @@ where
247272                        . into_iter ( ) 
248273                        . map ( |chunk| { 
249274                            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) 
251276                        } ) 
252277                        . collect ( ) ; 
253278
254279                    let  comm:  Vec < _ >  = lookup_sorted
255280                        . 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+                         } ) 
257286                        . collect ( ) ; 
258287                    let  coeffs :  Vec < _ >  =
259288                        // TODO: We can avoid storing these coefficients. 
@@ -279,7 +308,7 @@ where
279308            match  lookup_sorted { 
280309                None  => ( None ,  None ,  None ) , 
281310                Some ( lookup_sorted)  => { 
282-                     let  iter_lookup_table = || ( 0 ..n ) . map ( |i| { 
311+                     let  iter_lookup_table = || ( 0 ..d1_size ) . map ( |i| { 
283312                        let  row = index. cs . lookup_tables8 [ 0 ] . iter ( ) . map ( |e| &  e. evals [ 8  *  i] ) ; 
284313                        combine_table_entry ( joint_combiner,  row) 
285314                    } ) ; 
@@ -288,7 +317,7 @@ where
288317                        lookup:: aggregation :: < _ ,  Fr < G > ,  _ > ( 
289318                            dummy_lookup_value. 0 , 
290319                            iter_lookup_table ( ) , 
291-                             d1, 
320+                             index . cs . domain . d1 , 
292321                            & index. cs . gates , 
293322                            & witness, 
294323                            joint_combiner, 
@@ -297,11 +326,11 @@ where
297326                            rng) ?; 
298327
299328                    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  - 4 ]  != Fr :: < G > :: one ( )  { 
330+                         panic ! ( "aggregation incorrect: {}" ,  aggreg. evals[ d1_size -3 ] ) ; 
302331                    } 
303332
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) ; 
305334                    fq_sponge. absorb_g ( & comm. 0 . unshifted ) ; 
306335
307336                    let  coeffs = aggreg. interpolate ( ) ; 
@@ -314,7 +343,7 @@ where
314343            } ; 
315344
316345        // 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) ?; 
318347        // commit to z 
319348        let  z_comm = index. srs . commit ( & z,  None ,  rng) ; 
320349
@@ -381,7 +410,7 @@ where
381410                coefficient :  & index. cs . coefficients8 , 
382411                vanishes_on_last_4_rows :  & index. cs . vanishes_on_last_4_rows , 
383412                z :  & lagrange. d8 . this . z , 
384-                 l0_1 :  l0_1 ( d1) , 
413+                 l0_1 :  l0_1 ( index . cs . domain . d1 ) , 
385414                domain :  index. cs . domain , 
386415                index :  index_evals, 
387416                lookup :  lookup_env, 
@@ -440,7 +469,7 @@ where
440469                ( t4,  t8) , 
441470                alpha, 
442471                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 ) 
444473                    . iter ( ) 
445474                    . map ( |e| e. evaluations ( & env) ) 
446475                    . collect ( ) , 
@@ -606,8 +635,8 @@ where
606635                ) 
607636            } ) 
608637            . 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 ] , 
611640            shifted :  None , 
612641        } ; 
613642
@@ -627,7 +656,7 @@ where
627656        // construct evaluation proof 
628657        let  mut  polynomials = polys
629658            . iter ( ) 
630-             . map ( |( p,  n ) | ( p,  None ,  non_hiding ( * n ) ) ) 
659+             . map ( |( p,  d1_size ) | ( p,  None ,  non_hiding ( * d1_size ) ) ) 
631660            . collect :: < Vec < _ > > ( ) ; 
632661        polynomials. extend ( vec ! [ ( & p,  None ,  non_hiding( 1 ) ) ] ) ; 
633662        polynomials. extend ( vec ! [ ( & ft,  None ,  blinding_ft) ] ) ; 
0 commit comments