@@ -17,6 +17,7 @@ use ff::Field;
1717use group:: Curve ;
1818use halo2curves:: pairing:: Engine ;
1919use rand_core:: RngCore ;
20+ use rayon:: prelude:: * ;
2021use std:: fmt:: Debug ;
2122use std:: io:: { self , Write } ;
2223use std:: marker:: PhantomData ;
@@ -36,8 +37,8 @@ struct CommitmentExtension<'a, C: CurveAffine> {
3637}
3738
3839impl < ' a , C : CurveAffine > Commitment < C :: Scalar , PolynomialPointer < ' a , C > > {
39- fn extend ( & self , points : Vec < C :: Scalar > ) -> CommitmentExtension < ' a , C > {
40- let poly = lagrange_interpolate ( & points[ .. ] , & self . evals ( ) [ ..] ) ;
40+ fn extend ( & self , points : & [ C :: Scalar ] ) -> CommitmentExtension < ' a , C > {
41+ let poly = lagrange_interpolate ( points, & self . evals ( ) [ ..] ) ;
4142
4243 let low_degree_equivalent = Polynomial {
4344 values : poly,
@@ -79,10 +80,10 @@ struct RotationSetExtension<'a, C: CurveAffine> {
7980}
8081
8182impl < ' a , C : CurveAffine > RotationSet < C :: Scalar , PolynomialPointer < ' a , C > > {
82- fn extend ( & self , commitments : Vec < CommitmentExtension < ' a , C > > ) -> RotationSetExtension < ' a , C > {
83+ fn extend ( self , commitments : Vec < CommitmentExtension < ' a , C > > ) -> RotationSetExtension < ' a , C > {
8384 RotationSetExtension {
8485 commitments,
85- points : self . points . clone ( ) ,
86+ points : self . points ,
8687 }
8788 }
8889}
@@ -136,15 +137,17 @@ impl<'params, E: Engine + Debug> Prover<'params, KZGCommitmentScheme<E>>
136137 // [P_i_0(X) - R_i_0(X), P_i_1(X) - R_i_1(X), ... ]
137138 let numerators = rotation_set
138139 . commitments
139- . iter ( )
140- . map ( |commitment| commitment. quotient_contribution ( ) ) ;
140+ . par_iter ( )
141+ . map ( |commitment| commitment. quotient_contribution ( ) )
142+ . collect :: < Vec < _ > > ( ) ;
141143
142144 // define numerator polynomial as
143145 // N_i_j(X) = (P_i_j(X) - R_i_j(X))
144146 // and combine polynomials with same evaluation point set
145147 // N_i(X) = linear_combinination(y, N_i_j(X))
146148 // where y is random scalar to combine numerator polynomials
147149 let n_x = numerators
150+ . into_iter ( )
148151 . zip ( powers ( * y) )
149152 . map ( |( numerator, power_of_y) | numerator * power_of_y)
150153 . reduce ( |acc, numerator| acc + & numerator)
@@ -171,22 +174,26 @@ impl<'params, E: Engine + Debug> Prover<'params, KZGCommitmentScheme<E>>
171174 ) ;
172175
173176 let rotation_sets: Vec < RotationSetExtension < E :: G1Affine > > = rotation_sets
174- . iter ( )
177+ . into_par_iter ( )
175178 . map ( |rotation_set| {
176179 let commitments: Vec < CommitmentExtension < E :: G1Affine > > = rotation_set
177180 . commitments
178- . iter ( )
179- . map ( |commitment_data| commitment_data. extend ( rotation_set. points . clone ( ) ) )
181+ . par_iter ( )
182+ . map ( |commitment_data| commitment_data. extend ( & rotation_set. points ) )
180183 . collect ( ) ;
181184 rotation_set. extend ( commitments)
182185 } )
183186 . collect ( ) ;
184187
185188 let v: ChallengeV < _ > = transcript. squeeze_challenge_scalar ( ) ;
186189
187- let quotient_polynomials = rotation_sets. iter ( ) . map ( quotient_contribution) ;
190+ let quotient_polynomials = rotation_sets
191+ . par_iter ( )
192+ . map ( quotient_contribution)
193+ . collect :: < Vec < _ > > ( ) ;
188194
189195 let h_x: Polynomial < E :: Scalar , Coeff > = quotient_polynomials
196+ . into_iter ( )
190197 . zip ( powers ( * v) )
191198 . map ( |( poly, power_of_v) | poly * power_of_v)
192199 . reduce ( |acc, poly| acc + & poly)
@@ -196,18 +203,15 @@ impl<'params, E: Engine + Debug> Prover<'params, KZGCommitmentScheme<E>>
196203 transcript. write_point ( h) ?;
197204 let u: ChallengeU < _ > = transcript. squeeze_challenge_scalar ( ) ;
198205
199- let zt_eval = evaluate_vanishing_polynomial ( & super_point_set[ ..] , * u) ;
200-
201206 let linearisation_contribution =
202207 |rotation_set : RotationSetExtension < E :: G1Affine > | -> ( Polynomial < E :: Scalar , Coeff > , E :: Scalar ) {
203- let diffs: Vec < E :: Scalar > = super_point_set
204- . iter ( )
205- . filter ( | point| !rotation_set . points . contains ( point ) )
206- . copied ( )
207- . collect ( ) ;
208+ let mut diffs = super_point_set. clone ( ) ;
209+ for point in rotation_set . points . iter ( ) {
210+ diffs . remove ( point) ;
211+ }
212+ let diffs = diffs . into_iter ( ) . collect :: < Vec < _ > > ( ) ;
208213
209214 // calculate difference vanishing polynomial evaluation
210-
211215 let z_i = evaluate_vanishing_polynomial ( & diffs[ ..] , * u) ;
212216
213217 // inner linearisation contibutions are
@@ -216,15 +220,15 @@ impl<'params, E: Engine + Debug> Prover<'params, KZGCommitmentScheme<E>>
216220 // where u is random evaluation point
217221 let inner_contributions = rotation_set
218222 . commitments
219- . iter ( )
220- . map ( |commitment| commitment. linearisation_contribution ( * u) ) ;
223+ . par_iter ( )
224+ . map ( |commitment| commitment. linearisation_contribution ( * u) ) . collect :: < Vec < _ > > ( ) ;
221225
222226 // define inner contributor polynomial as
223227 // L_i_j(X) = (P_i_j(X) - r_i_j)
224228 // and combine polynomials with same evaluation point set
225229 // L_i(X) = linear_combinination(y, L_i_j(X))
226230 // where y is random scalar to combine inner contibutors
227- let l_x: Polynomial < E :: Scalar , Coeff > = inner_contributions. zip ( powers ( * y) ) . map ( |( poly, power_of_y) | poly * power_of_y) . reduce ( |acc, poly| acc + & poly) . unwrap ( ) ;
231+ let l_x: Polynomial < E :: Scalar , Coeff > = inner_contributions. into_iter ( ) . zip ( powers ( * y) ) . map ( |( poly, power_of_y) | poly * power_of_y) . reduce ( |acc, poly| acc + & poly) . unwrap ( ) ;
228232
229233 // finally scale l_x by difference vanishing polynomial evaluation z_i
230234 ( l_x * z_i, z_i)
@@ -235,7 +239,7 @@ impl<'params, E: Engine + Debug> Prover<'params, KZGCommitmentScheme<E>>
235239 Vec < Polynomial < E :: Scalar , Coeff > > ,
236240 Vec < E :: Scalar > ,
237241 ) = rotation_sets
238- . into_iter ( )
242+ . into_par_iter ( )
239243 . map ( linearisation_contribution)
240244 . unzip ( ) ;
241245
@@ -246,9 +250,12 @@ impl<'params, E: Engine + Debug> Prover<'params, KZGCommitmentScheme<E>>
246250 . reduce ( |acc, poly| acc + & poly)
247251 . unwrap ( ) ;
248252
253+ let super_point_set = super_point_set. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
254+ let zt_eval = evaluate_vanishing_polynomial ( & super_point_set[ ..] , * u) ;
249255 let l_x = l_x - & ( h_x * zt_eval) ;
250256
251257 // sanity check
258+ #[ cfg( debug_assertions) ]
252259 {
253260 let must_be_zero = eval_polynomial ( & l_x. values [ ..] , * u) ;
254261 assert_eq ! ( must_be_zero, E :: Scalar :: zero( ) ) ;
0 commit comments