@@ -4023,6 +4023,68 @@ void ecmult_const_mult_zero_one(void) {
40234023 ge_equals_ge (& res2 , & point );
40244024}
40254025
4026+ void ecmult_const_mult_xonly (void ) {
4027+ int i ;
4028+
4029+ /* Test correspondence between secp256k1_ecmult_const and secp256k1_ecmult_const_xonly. */
4030+ for (i = 0 ; i < 2 * count ; ++ i ) {
4031+ secp256k1_ge base ;
4032+ secp256k1_gej basej , resj ;
4033+ secp256k1_fe n , d , resx , v ;
4034+ secp256k1_scalar q ;
4035+ int res ;
4036+ /* Random base point. */
4037+ random_group_element_test (& base );
4038+ /* Random scalar to multiply it with. */
4039+ random_scalar_order_test (& q );
4040+ /* If i is odd, n=d*base.x for random non-zero d */
4041+ if (i & 1 ) {
4042+ do {
4043+ random_field_element_test (& d );
4044+ } while (secp256k1_fe_normalizes_to_zero_var (& d ));
4045+ secp256k1_fe_mul (& n , & base .x , & d );
4046+ } else {
4047+ n = base .x ;
4048+ }
4049+ /* Perform x-only multiplication. */
4050+ res = secp256k1_ecmult_const_xonly (& resx , & n , (i & 1 ) ? & d : NULL , & q , 256 , i & 2 );
4051+ CHECK (res );
4052+ /* Perform normal multiplication. */
4053+ secp256k1_gej_set_ge (& basej , & base );
4054+ secp256k1_ecmult (& resj , & basej , & q , NULL );
4055+ /* Check that resj's X coordinate corresponds with resx. */
4056+ secp256k1_fe_sqr (& v , & resj .z );
4057+ secp256k1_fe_mul (& v , & v , & resx );
4058+ CHECK (check_fe_equal (& v , & resj .x ));
4059+ }
4060+
4061+ /* Test that secp256k1_ecmult_const_xonly correctly rejects X coordinates not on curve. */
4062+ for (i = 0 ; i < 2 * count ; ++ i ) {
4063+ secp256k1_fe x , n , d , c , r ;
4064+ int res ;
4065+ secp256k1_scalar q ;
4066+ random_scalar_order_test (& q );
4067+ /* Generate random X coordinate not on the curve. */
4068+ do {
4069+ random_field_element_test (& x );
4070+ secp256k1_fe_sqr (& c , & x );
4071+ secp256k1_fe_mul (& c , & c , & x );
4072+ secp256k1_fe_add (& c , & secp256k1_fe_const_b );
4073+ } while (secp256k1_fe_jacobi_var (& c ) >= 0 );
4074+ /* If i is odd, n=d*x for random non-zero d. */
4075+ if (i & 1 ) {
4076+ do {
4077+ random_field_element_test (& d );
4078+ } while (secp256k1_fe_normalizes_to_zero_var (& d ));
4079+ secp256k1_fe_mul (& n , & x , & d );
4080+ } else {
4081+ n = x ;
4082+ }
4083+ res = secp256k1_ecmult_const_xonly (& r , & n , (i & 1 ) ? & d : NULL , & q , 256 , 0 );
4084+ CHECK (res == 0 );
4085+ }
4086+ }
4087+
40264088void ecmult_const_chain_multiply (void ) {
40274089 /* Check known result (randomly generated test problem from sage) */
40284090 const secp256k1_scalar scalar = SECP256K1_SCALAR_CONST (
@@ -4054,6 +4116,7 @@ void run_ecmult_const_tests(void) {
40544116 ecmult_const_random_mult ();
40554117 ecmult_const_commutativity ();
40564118 ecmult_const_chain_multiply ();
4119+ ecmult_const_mult_xonly ();
40574120}
40584121
40594122typedef struct {
0 commit comments