@@ -7,10 +7,10 @@ use crate::edwards::affine::PointBytes;
7
7
use crate :: field:: FieldElement ;
8
8
use crate :: * ;
9
9
use elliptic_curve:: {
10
- CurveGroup , Error ,
10
+ BatchNormalize , CurveGroup , Error ,
11
11
array:: Array ,
12
12
group:: { Group , GroupEncoding , cofactor:: CofactorGroup , prime:: PrimeGroup } ,
13
- ops:: LinearCombination ,
13
+ ops:: { BatchInvert , LinearCombination } ,
14
14
point:: NonIdentity ,
15
15
} ;
16
16
use hash2curve:: ExpandMsgXof ;
@@ -290,6 +290,14 @@ impl CurveGroup for EdwardsPoint {
290
290
fn to_affine ( & self ) -> AffinePoint {
291
291
self . to_affine ( )
292
292
}
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
+ }
293
301
}
294
302
295
303
impl EdwardsPoint {
@@ -671,11 +679,76 @@ impl<'de> serdect::serde::Deserialize<'de> for EdwardsPoint {
671
679
672
680
impl elliptic_curve:: zeroize:: DefaultIsZeroes for EdwardsPoint { }
673
681
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
+
674
746
#[ cfg( test) ]
675
747
mod tests {
676
748
use super :: * ;
677
749
use elliptic_curve:: Field ;
678
750
use hex_literal:: hex;
751
+ use rand_core:: OsRng ;
679
752
680
753
fn hex_to_field ( hex : & ' static str ) -> FieldElement {
681
754
assert_eq ! ( hex. len( ) , 56 * 2 ) ;
@@ -970,4 +1043,33 @@ mod tests {
970
1043
971
1044
assert_eq ! ( computed_commitment, expected_commitment) ;
972
1045
}
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
+ }
973
1075
}
0 commit comments