@@ -6,7 +6,7 @@ use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
6
6
use crate :: curve:: scalar_mul:: variable_base;
7
7
use crate :: curve:: twedwards:: IsogenyMap ;
8
8
use crate :: curve:: twedwards:: extended:: ExtendedPoint as TwistedExtendedPoint ;
9
- use crate :: field:: FieldElement ;
9
+ use crate :: field:: { ConstMontyType , FieldElement } ;
10
10
use crate :: * ;
11
11
use elliptic_curve:: {
12
12
CurveGroup , Error ,
@@ -711,8 +711,57 @@ impl EdwardsPoint {
711
711
/// prime-order subgroup;
712
712
/// * `false` if `self` has a nonzero torsion component and is not
713
713
/// in the prime-order subgroup.
714
+ // See https://eprint.iacr.org/2022/1164.
714
715
pub fn is_torsion_free ( & self ) -> Choice {
715
- ( self * EdwardsScalar :: new ( * ORDER ) ) . ct_eq ( & Self :: IDENTITY )
716
+ const A : FieldElement = FieldElement ( ConstMontyType :: new ( & U448 :: from_be_hex (
717
+ "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffffffffffffffffffffffffffffffffffffffffffffffffeceaf" ,
718
+ ) ) ) ;
719
+ const A1 : FieldElement = FieldElement ( ConstMontyType :: new ( & U448 :: from_u64 ( 156320 ) ) ) ;
720
+ const MINUS_SQRT_B1 : FieldElement = FieldElement ( ConstMontyType :: new ( & U448 :: from_be_hex (
721
+ "749a7410536c225f1025ca374176557d7839611d691caad26d74a1fca5cfad15f196642c0a4484b67f321025577cc6b5a6f443c2eaa36327" ,
722
+ ) ) ) ;
723
+
724
+ let mut e = self . X * ( self . Z - self . Y ) ;
725
+ let ee = e. square ( ) ;
726
+ let mut u = FieldElement :: A_PLUS_TWO_OVER_FOUR * ( self . Z + self . Y ) * e * self . X ;
727
+ let w = self . Z . double ( ) * ( self . Z - self . Y ) ;
728
+
729
+ let u2 = u. double ( ) . double ( ) ;
730
+ let w2 = w. double ( ) ;
731
+
732
+ let mut w1 = u2. sqrt ( ) ;
733
+ let mut ok = w1. square ( ) . ct_eq ( & u2) ;
734
+ let u1 = ( u2 - A1 * ee - w1 * w2) . half ( ) ;
735
+
736
+ // If `u1` happens not to be a square, then `sqrt(u1)` returns `sqrt(-u1)`
737
+ // in that case (since we are in a finite field GF(q) with q = 3 mod 4,
738
+ // if `u1` is not a square then `-u1` must be a square). In such a case, we
739
+ // should replace `(u1,w1)` with `((B1*e^4)/u1, -w1)`. To avoid the division,
740
+ // we instead switch to an isomorphic curve; namely:
741
+ // u2 = B1*(e^4)*u1
742
+ // w2 = -w1*u1
743
+ // e2 = e*u1
744
+ // Then:
745
+ // w = sqrt(u2) = sqrt(-B1)*(e^2)*sqrt(-u1)
746
+ // u = (w^2 - A*e^2 - w*w1)/2
747
+ let mut w = u1. sqrt ( ) ;
748
+ let u1_is_square = w. square ( ) . ct_eq ( & u1) ;
749
+ w1. conditional_assign ( & -( w1 * u1) , !u1_is_square) ;
750
+ e. conditional_assign ( & ( e * u1) , !u1_is_square) ;
751
+ w. conditional_assign ( & ( MINUS_SQRT_B1 * ee * w) , !u1_is_square) ;
752
+ u = ( w. square ( ) - A * e. square ( ) - w * w1) . half ( ) ;
753
+
754
+ ok &= u. is_square ( ) ;
755
+
756
+ // If the source point was a low-order point, then the computations
757
+ // above are incorrect. We handle this case here; among the
758
+ // low-order points, only the neutral point is in the prime-order
759
+ // subgroup.
760
+ let is_low_order = self . X . is_zero ( ) | self . Y . is_zero ( ) ;
761
+ let is_neutral = self . Y . ct_eq ( & self . Z ) ;
762
+ ok ^= is_low_order & ( ok ^ is_neutral) ;
763
+
764
+ ok
716
765
}
717
766
718
767
/// Hash a message to a point on the curve
@@ -961,6 +1010,8 @@ mod tests {
961
1010
use super :: * ;
962
1011
use elliptic_curve:: Field ;
963
1012
use hex_literal:: hex;
1013
+ use proptest:: prelude:: any;
1014
+ use proptest:: proptest;
964
1015
use rand_core:: TryRngCore ;
965
1016
966
1017
fn hex_to_field ( hex : & ' static str ) -> FieldElement {
@@ -1256,4 +1307,15 @@ mod tests {
1256
1307
1257
1308
assert_eq ! ( computed_commitment, expected_commitment) ;
1258
1309
}
1310
+
1311
+ proptest ! {
1312
+ #[ test]
1313
+ fn fuzz_is_torsion_free(
1314
+ bytes in any:: <[ u8 ; 57 ] >( )
1315
+ ) {
1316
+ let scalar = EdwardsScalar :: from_bytes_mod_order( & bytes. into( ) ) ;
1317
+ let point = EdwardsPoint :: mul_by_generator( & scalar) ;
1318
+ assert_eq!( point. is_torsion_free( ) . unwrap_u8( ) , 1 ) ;
1319
+ }
1320
+ }
1259
1321
}
0 commit comments