@@ -809,7 +809,7 @@ impl WiresArity for DecompressedG2Wires {
809809
810810#[ cfg( test) ]
811811mod tests {
812- use ark_ec:: { CurveGroup , PrimeGroup , VariableBaseMSM } ;
812+ use ark_ec:: { AffineRepr , CurveGroup , PrimeGroup , VariableBaseMSM } ;
813813 use ark_ff:: { Field , UniformRand } ;
814814 use ark_serialize:: CanonicalSerialize ;
815815 use rand:: { Rng , SeedableRng } ;
@@ -1285,4 +1285,37 @@ mod tests {
12851285 } ) ;
12861286 assert_eq ! ( out. output_value[ 0 ] , ref_is_on_curve) ;
12871287 }
1288+
1289+ #[ test]
1290+ fn test_cofactor_clearing ( ) {
1291+ let mut rng = ChaCha20Rng :: seed_from_u64 ( 112 ) ;
1292+ for _ in 0 ..5 {
1293+ // sufficient sample size to sample both valid and invalid points
1294+ let x = ark_bn254:: Fq2 :: rand ( & mut rng) ;
1295+ let a1 = ark_bn254:: Fq2 :: sqrt ( & ( ( x * x * x) + ark_bn254:: g2:: Config :: COEFF_B ) ) ;
1296+ let ( y, ref_is_valid) = if let Some ( a1) = a1 {
1297+ // if it is possible to take square root, you have found correct y,
1298+ ( a1, true )
1299+ } else {
1300+ // else generate some random value
1301+ ( ark_bn254:: Fq2 :: rand ( & mut rng) , false )
1302+ } ;
1303+ let pt = ark_bn254:: G2Affine :: new_unchecked ( x, y) ;
1304+
1305+ let pt = pt. into_group ( ) ;
1306+ const COFACTOR : & [ u64 ] = & [
1307+ 0x345f2299c0f9fa8d ,
1308+ 0x06ceecda572a2489 ,
1309+ 0xb85045b68181585e ,
1310+ 0x30644e72e131a029 ,
1311+ ] ;
1312+ let pt = pt. mul_bigint ( COFACTOR ) ;
1313+ let pt = pt. into_affine ( ) ;
1314+ // if it's a valid point, it should be on curve and subgroup (after cofactor clearing)
1315+ assert_eq ! (
1316+ ref_is_valid,
1317+ pt. is_on_curve( ) && pt. is_in_correct_subgroup_assuming_on_curve( )
1318+ ) ;
1319+ }
1320+ }
12881321}
0 commit comments