@@ -7,10 +7,10 @@ use crate::edwards::affine::PointBytes;
77use crate :: field:: FieldElement ;
88use crate :: * ;
99use elliptic_curve:: {
10- CurveGroup , Error ,
10+ BatchNormalize , CurveGroup , Error ,
1111 array:: Array ,
1212 group:: { Group , GroupEncoding , cofactor:: CofactorGroup , prime:: PrimeGroup } ,
13- ops:: LinearCombination ,
13+ ops:: { BatchInvert , LinearCombination } ,
1414 point:: NonIdentity ,
1515} ;
1616use hash2curve:: ExpandMsgXof ;
@@ -290,6 +290,14 @@ impl CurveGroup for EdwardsPoint {
290290 fn to_affine ( & self ) -> AffinePoint {
291291 self . to_affine ( )
292292 }
293+
294+ #[ cfg( feature = "alloc" ) ]
295+ #[ inline]
296+ fn batch_normalize ( projective : & [ Self ] , affine : & mut [ Self :: AffineRepr ] ) {
297+ assert_eq ! ( projective. len( ) , affine. len( ) ) ;
298+ let mut zs = alloc:: vec![ FieldElement :: ONE ; projective. len( ) ] ;
299+ batch_normalize_generic ( projective, zs. as_mut_slice ( ) , affine) ;
300+ }
293301}
294302
295303impl EdwardsPoint {
@@ -671,11 +679,76 @@ impl<'de> serdect::serde::Deserialize<'de> for EdwardsPoint {
671679
672680impl elliptic_curve:: zeroize:: DefaultIsZeroes for EdwardsPoint { }
673681
682+ impl < const N : usize > BatchNormalize < [ EdwardsPoint ; N ] > for EdwardsPoint {
683+ type Output = [ <Self as CurveGroup >:: AffineRepr ; N ] ;
684+
685+ #[ inline]
686+ fn batch_normalize ( points : & [ Self ; N ] ) -> [ <Self as CurveGroup >:: AffineRepr ; N ] {
687+ let zs = [ FieldElement :: ONE ; N ] ;
688+ let mut affine_points = [ AffinePoint :: IDENTITY ; N ] ;
689+ batch_normalize_generic ( points, zs, & mut affine_points) ;
690+ affine_points
691+ }
692+ }
693+
694+ #[ cfg( feature = "alloc" ) ]
695+ impl BatchNormalize < [ EdwardsPoint ] > for EdwardsPoint {
696+ type Output = Vec < <Self as CurveGroup >:: AffineRepr > ;
697+
698+ #[ inline]
699+ fn batch_normalize ( points : & [ Self ] ) -> Vec < <Self as CurveGroup >:: AffineRepr > {
700+ use alloc:: vec;
701+
702+ let mut zs = vec ! [ FieldElement :: ONE ; points. len( ) ] ;
703+ let mut affine_points = vec ! [ AffinePoint :: IDENTITY ; points. len( ) ] ;
704+ batch_normalize_generic ( points, zs. as_mut_slice ( ) , & mut affine_points) ;
705+ affine_points
706+ }
707+ }
708+
709+ /// Generic implementation of batch normalization.
710+ fn batch_normalize_generic < P , Z , I , O > ( points : & P , mut zs : Z , out : & mut O )
711+ where
712+ FieldElement : BatchInvert < Z , Output = CtOption < I > > ,
713+ P : AsRef < [ EdwardsPoint ] > + ?Sized ,
714+ Z : AsMut < [ FieldElement ] > ,
715+ I : AsRef < [ FieldElement ] > ,
716+ O : AsMut < [ AffinePoint ] > + ?Sized ,
717+ {
718+ let points = points. as_ref ( ) ;
719+ let out = out. as_mut ( ) ;
720+
721+ for ( i, point) in points. iter ( ) . enumerate ( ) {
722+ // Even a single zero value will fail inversion for the entire batch.
723+ // Put a dummy value (above `FieldElement::ONE`) so inversion succeeds
724+ // and treat that case specially later-on.
725+ zs. as_mut ( ) [ i] . conditional_assign ( & point. Z , !point. Z . ct_eq ( & FieldElement :: ZERO ) ) ;
726+ }
727+
728+ // This is safe to unwrap since we assured that all elements are non-zero
729+ let zs_inverses = <FieldElement as BatchInvert < Z > >:: batch_invert ( zs)
730+ . expect ( "all elements should be non-zero" ) ;
731+
732+ for i in 0 ..out. len ( ) {
733+ // If the `z` coordinate is non-zero, we can use it to invert;
734+ // otherwise it defaults to the `IDENTITY` value.
735+ out[ i] = AffinePoint :: conditional_select (
736+ & AffinePoint {
737+ x : points[ i] . X * zs_inverses. as_ref ( ) [ i] ,
738+ y : points[ i] . Y * zs_inverses. as_ref ( ) [ i] ,
739+ } ,
740+ & AffinePoint :: IDENTITY ,
741+ points[ i] . Z . ct_eq ( & FieldElement :: ZERO ) ,
742+ ) ;
743+ }
744+ }
745+
674746#[ cfg( test) ]
675747mod tests {
676748 use super :: * ;
677749 use elliptic_curve:: Field ;
678750 use hex_literal:: hex;
751+ use rand_core:: OsRng ;
679752
680753 fn hex_to_field ( hex : & ' static str ) -> FieldElement {
681754 assert_eq ! ( hex. len( ) , 56 * 2 ) ;
@@ -970,4 +1043,33 @@ mod tests {
9701043
9711044 assert_eq ! ( computed_commitment, expected_commitment) ;
9721045 }
1046+
1047+ #[ test]
1048+ fn batch_normalize ( ) {
1049+ let points: [ EdwardsPoint ; 2 ] = [
1050+ EdwardsPoint :: try_from_rng ( & mut OsRng ) . unwrap ( ) ,
1051+ EdwardsPoint :: try_from_rng ( & mut OsRng ) . unwrap ( ) ,
1052+ ] ;
1053+
1054+ let affine_points = <EdwardsPoint as BatchNormalize < _ > >:: batch_normalize ( & points) ;
1055+
1056+ for ( point, affine_point) in points. into_iter ( ) . zip ( affine_points) {
1057+ assert_eq ! ( affine_point, point. to_affine( ) ) ;
1058+ }
1059+ }
1060+
1061+ #[ test]
1062+ #[ cfg( feature = "alloc" ) ]
1063+ fn batch_normalize_alloc ( ) {
1064+ let points = alloc:: vec![
1065+ EdwardsPoint :: try_from_rng( & mut OsRng ) . unwrap( ) ,
1066+ EdwardsPoint :: try_from_rng( & mut OsRng ) . unwrap( ) ,
1067+ ] ;
1068+
1069+ let affine_points = <EdwardsPoint as BatchNormalize < _ > >:: batch_normalize ( points. as_slice ( ) ) ;
1070+
1071+ for ( point, affine_point) in points. into_iter ( ) . zip ( affine_points) {
1072+ assert_eq ! ( affine_point, point. to_affine( ) ) ;
1073+ }
1074+ }
9731075}
0 commit comments