@@ -27,7 +27,6 @@ use std::{
27
27
ops:: { Add , AddAssign , Deref , DerefMut , Div , Mul , MulAssign , Neg , Sub , SubAssign } ,
28
28
} ;
29
29
30
- #[ cfg( feature = "serial" ) ]
31
30
use itertools:: Itertools ;
32
31
33
32
#[ cfg( not( feature = "serial" ) ) ]
@@ -75,7 +74,7 @@ impl<F: Field> DensePolynomial<F> {
75
74
/// Constructs a new polynomial from a list of coefficients.
76
75
pub fn from_coefficients_vec ( mut coeffs : Vec < F > ) -> Self {
77
76
// While there are zeros at the end of the coefficient vector, pop them off.
78
- while coeffs. last ( ) . map_or ( false , |c| c. is_zero ( ) ) {
77
+ while let Some ( true ) = coeffs. last ( ) . map ( |c| c. is_zero ( ) ) {
79
78
coeffs. pop ( ) ;
80
79
}
81
80
// Check that either the coefficients vec is empty or that the last coeff is non-zero.
@@ -113,10 +112,16 @@ impl<F: Field> DensePolynomial<F> {
113
112
crate :: cfg_reduce!( mapping, || zero, |a, b| a + b)
114
113
}
115
114
116
- /// Outputs a polynomial of degree `d` where each coefficient is sampled uniformly at random
117
- /// from the field `F`.
115
+ /// Outputs a univariate polynomial of degree `d` where each non-leading
116
+ /// coefficient is sampled uniformly at random from R and the leading
117
+ /// coefficient is sampled uniformly at random from among the non-zero
118
+ /// elements of R.
118
119
pub fn rand < R : Rng > ( d : usize , rng : & mut R ) -> Self {
119
- let random_coeffs = ( 0 ..( d + 1 ) ) . map ( |_| F :: rand ( rng) ) . collect ( ) ;
120
+ let mut random_coeffs = ( 0 ..( d + 1 ) ) . map ( |_| F :: rand ( rng) ) . collect_vec ( ) ;
121
+ while random_coeffs[ d] . is_zero ( ) {
122
+ // In the extremely unlikely event, sample again.
123
+ random_coeffs[ d] = F :: rand ( rng) ;
124
+ }
120
125
Self :: from_coefficients_vec ( random_coeffs)
121
126
}
122
127
@@ -189,7 +194,7 @@ impl<'a, 'b, F: Field> Add<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
189
194
type Output = DensePolynomial < F > ;
190
195
191
196
fn add ( self , other : & ' a DensePolynomial < F > ) -> DensePolynomial < F > {
192
- if self . is_zero ( ) {
197
+ let mut result = if self . is_zero ( ) {
193
198
other. clone ( )
194
199
} else if other. is_zero ( ) {
195
200
self . clone ( )
@@ -202,12 +207,13 @@ impl<'a, 'b, F: Field> Add<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
202
207
let mut result = other. clone ( ) ;
203
208
// Zip safety: `result` and `other` could have different lengths.
204
209
cfg_iter_mut ! ( result. coeffs) . zip ( & self . coeffs ) . for_each ( |( a, b) | * a += b) ;
205
- // If the leading coefficient ends up being zero, pop it off.
206
- while let Some ( true ) = self . coeffs . last ( ) . map ( |c| c. is_zero ( ) ) {
207
- result. coeffs . pop ( ) ;
208
- }
209
210
result
211
+ } ;
212
+ // If the leading coefficient ends up being zero, pop it off.
213
+ while let Some ( true ) = result. coeffs . last ( ) . map ( |c| c. is_zero ( ) ) {
214
+ result. coeffs . pop ( ) ;
210
215
}
216
+ result
211
217
}
212
218
}
213
219
@@ -273,10 +279,10 @@ impl<'a, F: Field> AddAssign<(F, &'a DensePolynomial<F>)> for DensePolynomial<F>
273
279
cfg_iter_mut ! ( self . coeffs) . zip ( & other. coeffs ) . for_each ( |( a, b) | {
274
280
* a += f * b;
275
281
} ) ;
276
- // If the leading coefficient ends up being zero, pop it off.
277
- while let Some ( true ) = self . coeffs . last ( ) . map ( |c| c . is_zero ( ) ) {
278
- self . coeffs . pop ( ) ;
279
- }
282
+ }
283
+ // If the leading coefficient ends up being zero, pop it off.
284
+ while let Some ( true ) = self . coeffs . last ( ) . map ( |c| c . is_zero ( ) ) {
285
+ self . coeffs . pop ( ) ;
280
286
}
281
287
}
282
288
}
@@ -298,7 +304,7 @@ impl<'a, 'b, F: Field> Sub<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
298
304
299
305
#[ inline]
300
306
fn sub ( self , other : & ' a DensePolynomial < F > ) -> DensePolynomial < F > {
301
- if self . is_zero ( ) {
307
+ let mut result = if self . is_zero ( ) {
302
308
let mut result = other. clone ( ) ;
303
309
for coeff in & mut result. coeffs {
304
310
* coeff = -( * coeff) ;
@@ -318,15 +324,13 @@ impl<'a, 'b, F: Field> Sub<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
318
324
cfg_iter_mut ! ( result. coeffs) . zip ( & other. coeffs ) . for_each ( |( a, b) | {
319
325
* a -= b;
320
326
} ) ;
321
- if !result. is_zero ( ) {
322
- // If the leading coefficient ends up being zero, pop it off.
323
- while result. coeffs . last ( ) . map ( |c| c. is_zero ( ) ) == Some ( true ) {
324
- result. coeffs . pop ( ) ;
325
- }
326
- }
327
-
328
327
result
328
+ } ;
329
+ // If the leading coefficient ends up being zero, pop it off.
330
+ while let Some ( true ) = result. coeffs . last ( ) . map ( |c| c. is_zero ( ) ) {
331
+ result. coeffs . pop ( ) ;
329
332
}
333
+ result
330
334
}
331
335
}
332
336
@@ -348,10 +352,10 @@ impl<'a, F: Field> SubAssign<&'a DensePolynomial<F>> for DensePolynomial<F> {
348
352
self . coeffs . resize ( other. coeffs . len ( ) , F :: zero ( ) ) ;
349
353
// Zip safety: self and other have the same length after the resize.
350
354
cfg_iter_mut ! ( self . coeffs) . zip ( & other. coeffs ) . for_each ( |( a, b) | * a -= b) ;
351
- // If the leading coefficient ends up being zero, pop it off.
352
- while let Some ( true ) = self . coeffs . last ( ) . map ( |c| c . is_zero ( ) ) {
353
- self . coeffs . pop ( ) ;
354
- }
355
+ }
356
+ // If the leading coefficient ends up being zero, pop it off.
357
+ while let Some ( true ) = self . coeffs . last ( ) . map ( |c| c . is_zero ( ) ) {
358
+ self . coeffs . pop ( ) ;
355
359
}
356
360
}
357
361
}
0 commit comments