@@ -334,28 +334,31 @@ macro_rules! curve_impl {
334334 self . is_zero( ) || self . z == $basefield:: one( )
335335 }
336336
337- fn batch_normalization( v: & mut [ Self ] )
337+ fn batch_normalization<II : ?Sized >( v: & mut II )
338+ where for <' a> & ' a mut II : IntoIterator <Item = & ' a mut Self >,
339+ for <' a> <& ' a mut II as IntoIterator >:: IntoIter : DoubleEndedIterator +ExactSizeIterator
338340 {
339341 // Montgomery’s Trick and Fast Implementation of Masked AES
340342 // Genelle, Prouff and Quisquater
341343 // Section 3.2
342344
343345 // First pass: compute [a, ab, abc, ...]
344- let mut prod = Vec :: with_capacity( v. len( ) ) ;
346+ let mut prod = Vec :: with_capacity( v. into_iter ( ) . len( ) ) ;
345347 let mut tmp = $basefield:: one( ) ;
346- for g in v. iter_mut ( )
348+ for g in v. into_iter ( )
347349 // Ignore normalized elements
348350 . filter( |g| !g. is_normalized( ) )
349351 {
350352 tmp. mul_assign( & g. z) ;
351353 prod. push( tmp) ;
352354 }
355+ if prod. is_empty( ) { return ; }
353356
354357 // Invert `tmp`.
355358 tmp = tmp. inverse( ) . unwrap( ) ; // Guaranteed to be nonzero.
356359
357360 // Second pass: iterate backwards to compute inverses
358- for ( g, s) in v. iter_mut ( )
361+ for ( g, s) in v. into_iter ( )
359362 // Backwards
360363 . rev( )
361364 // Ignore normalized elements
@@ -372,7 +375,7 @@ macro_rules! curve_impl {
372375 }
373376
374377 // Perform affine transformations
375- for g in v. iter_mut ( )
378+ for g in v. into_iter ( )
376379 . filter( |g| !g. is_normalized( ) )
377380 {
378381 let mut z = g. z; // 1/z
0 commit comments