@@ -57,14 +57,13 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
57
57
optimization_type,
58
58
) ;
59
59
60
- let mut base_repr: <TargetField as PrimeField >:: BigInt = TargetField :: one ( ) . into_bigint ( ) ;
61
-
62
60
// Convert 2^{(params.bits_per_limb - 1)} into the TargetField and then double
63
61
// the base This is because 2^{(params.bits_per_limb)} might indeed be
64
62
// larger than the target field's prime.
65
- base_repr. muln ( ( params. bits_per_limb - 1 ) as u32 ) ;
66
- let mut base: TargetField = TargetField :: from_bigint ( base_repr) . unwrap ( ) ;
67
- base = base + & base;
63
+ let base_repr = TargetField :: ONE . into_bigint ( ) << ( params. bits_per_limb - 1 ) as u32 ;
64
+
65
+ let mut base = TargetField :: from_bigint ( base_repr) . unwrap ( ) ;
66
+ base. double_in_place ( ) ;
68
67
69
68
let mut result = TargetField :: zero ( ) ;
70
69
let mut power = TargetField :: one ( ) ;
@@ -206,25 +205,21 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
206
205
> BaseField :: MODULUS_BIT_SIZE as usize - 1 )
207
206
{
208
207
Reducer :: reduce ( & mut other) ?;
209
- surfeit = overhead ! ( other. num_of_additions_over_normal_form + BaseField :: one ( ) ) + 1 ;
208
+ surfeit = overhead ! ( other. num_of_additions_over_normal_form + BaseField :: ONE ) + 1 ;
210
209
}
211
210
212
211
// Step 2: construct the padding
213
- let mut pad_non_top_limb_repr: <BaseField as PrimeField >:: BigInt =
214
- BaseField :: one ( ) . into_bigint ( ) ;
215
- let mut pad_top_limb_repr: <BaseField as PrimeField >:: BigInt = pad_non_top_limb_repr;
212
+ let mut pad_non_top_limb = BaseField :: ONE . into_bigint ( ) ;
213
+ let mut pad_top_limb = pad_non_top_limb;
216
214
217
- pad_non_top_limb_repr . muln ( ( surfeit + params. bits_per_limb ) as u32 ) ;
218
- let pad_non_top_limb = BaseField :: from_bigint ( pad_non_top_limb_repr ) . unwrap ( ) ;
215
+ pad_non_top_limb <<= ( surfeit + params. bits_per_limb ) as u32 ;
216
+ let pad_non_top_limb = BaseField :: from_bigint ( pad_non_top_limb ) . unwrap ( ) ;
219
217
220
- pad_top_limb_repr. muln (
221
- ( surfeit
222
- + ( TargetField :: MODULUS_BIT_SIZE as usize
223
- - params. bits_per_limb * ( params. num_limbs - 1 ) ) ) as u32 ,
224
- ) ;
225
- let pad_top_limb = BaseField :: from_bigint ( pad_top_limb_repr) . unwrap ( ) ;
218
+ pad_top_limb <<= ( surfeit + TargetField :: MODULUS_BIT_SIZE as usize
219
+ - params. bits_per_limb * ( params. num_limbs - 1 ) ) as u32 ;
220
+ let pad_top_limb = BaseField :: from_bigint ( pad_top_limb) . unwrap ( ) ;
226
221
227
- let mut pad_limbs = Vec :: new ( ) ;
222
+ let mut pad_limbs = Vec :: with_capacity ( self . limbs . len ( ) ) ;
228
223
pad_limbs. push ( pad_top_limb) ;
229
224
for _ in 0 ..self . limbs . len ( ) - 1 {
230
225
pad_limbs. push ( pad_non_top_limb) ;
@@ -236,12 +231,12 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
236
231
Self :: get_limbs_representations ( & pad_to_kp_gap, self . get_optimization_type ( ) ) ?;
237
232
238
233
// Step 4: the result is self + pad + pad_to_kp - other
239
- let mut limbs = Vec :: new ( ) ;
234
+ let mut limbs = Vec :: with_capacity ( self . limbs . len ( ) ) ;
240
235
for ( i, ( ( this_limb, other_limb) , pad_to_kp_limb) ) in self
241
236
. limbs
242
237
. iter ( )
243
- . zip ( other. limbs . iter ( ) )
244
- . zip ( pad_to_kp_limbs. iter ( ) )
238
+ . zip ( & other. limbs )
239
+ . zip ( & pad_to_kp_limbs)
245
240
. enumerate ( )
246
241
{
247
242
if i != 0 {
@@ -341,7 +336,7 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
341
336
& cur_bits[ cur_bits. len ( ) - params. bits_per_limb ..] ,
342
337
) ; // therefore, the lowest `bits_per_non_top_limb` bits is what we want.
343
338
limbs. push ( BaseField :: from_bigint ( cur_mod_r) . unwrap ( ) ) ;
344
- cur. divn ( params. bits_per_limb as u32 ) ;
339
+ cur >>= params. bits_per_limb as u32 ;
345
340
}
346
341
347
342
// then we reserve, so that the limbs are ``big limb first''
0 commit comments