Skip to content

Commit

Permalink
Add x-only ecmult_const version for x=n/d
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Sep 16, 2022
1 parent 223ccb7 commit 855c8e6
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ecmult_const.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,23 @@
*/
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q, int bits);

/**
* Same as secp256k1_ecmult_const, but takes in an x coordinate of the base point
* only, specified as fraction n/d. Only the x coordinate of the result is returned.
*
* If known_on_curve is 0, a verification is performed that n/d is a valid X
* coordinate, and 0 is returned if not. Otherwise, 1 is returned.
*
* d being NULL is interpreted as d=1.
*
* Constant time in the value of q, but not any other inputs.
*/
static int secp256k1_ecmult_const_xonly(
secp256k1_fe* r,
const secp256k1_fe *n,
const secp256k1_fe *d,
const secp256k1_scalar *q,
int bits,
int known_on_curve);

#endif /* SECP256K1_ECMULT_CONST_H */
54 changes: 54 additions & 0 deletions src/ecmult_const_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,58 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
secp256k1_fe_mul(&r->z, &r->z, &Z);
}

static int secp256k1_ecmult_const_xonly(secp256k1_fe* r, const secp256k1_fe *n, const secp256k1_fe *d, const secp256k1_scalar *q, int bits, int known_on_curve) {

/* This algorithm is a generalization of Peter Dettman's technique for
* avoiding the square root in a random-basepoint x-only multiplication
* on a Weierstrass curve:
* https://mailarchive.ietf.org/arch/msg/cfrg/7DyYY6gg32wDgHAhgSb6XxMDlJA/
*/
secp256k1_fe g, i;
secp256k1_ge p;
secp256k1_gej rj;

/* Compute g = (n^3 + B*d^3). */
secp256k1_fe_sqr(&g, n);
secp256k1_fe_mul(&g, &g, n);
if (d) {
secp256k1_fe b;
secp256k1_fe_sqr(&b, d);
secp256k1_fe_mul(&b, &b, d);
secp256k1_fe_mul(&b, &b, &secp256k1_fe_const_b);
secp256k1_fe_add(&g, &b);
if (!known_on_curve) {
secp256k1_fe c;
secp256k1_fe_mul(&c, &g, d);
if (secp256k1_fe_jacobi_var(&c) < 0) return 0;
}
} else {
secp256k1_fe_add(&g, &secp256k1_fe_const_b);
if (!known_on_curve) {
if (secp256k1_fe_jacobi_var(&g) < 0) return 0;
}
}

/* Compute base point P = (n*g, g^2), the effective affine version of
* (n*g, g^2, sqrt(d*g)), which has corresponding affine X coordinate
* n/d. */
secp256k1_fe_mul(&p.x, &g, n);
secp256k1_fe_sqr(&p.y, &g);
p.infinity = 0;

/* Perform x-only EC multiplication of P with q. */
secp256k1_ecmult_const(&rj, &p, q, bits);

/* The resulting (X, Y, Z) point on the effective-affine isomorphic curve
* corresponds to (X, Y, Z*sqrt(d*g)) on the secp256k1 curve. The affine
* version of that has X coordinate (X / (Z^2*d*g)). */
secp256k1_fe_sqr(&i, &rj.z);
secp256k1_fe_mul(&i, &i, &g);
if (d) secp256k1_fe_mul(&i, &i, d);
secp256k1_fe_inv(&i, &i);
secp256k1_fe_mul(r, &rj.x, &i);

return 1;
}

#endif /* SECP256K1_ECMULT_CONST_IMPL_H */
63 changes: 63 additions & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -4028,6 +4028,68 @@ void ecmult_const_mult_zero_one(void) {
ge_equals_ge(&res2, &point);
}

void ecmult_const_mult_xonly(void) {
int i;

/* Test correspondence between secp256k1_ecmult_const and secp256k1_ecmult_const_xonly. */
for (i = 0; i < 2*count; ++i) {
secp256k1_ge base;
secp256k1_gej basej, resj;
secp256k1_fe n, d, resx, v;
secp256k1_scalar q;
int res;
/* Random base point. */
random_group_element_test(&base);
/* Random scalar to multiply it with. */
random_scalar_order_test(&q);
/* If i is odd, n=d*base.x for random non-zero d */
if (i & 1) {
do {
random_field_element_test(&d);
} while (secp256k1_fe_normalizes_to_zero_var(&d));
secp256k1_fe_mul(&n, &base.x, &d);
} else {
n = base.x;
}
/* Perform x-only multiplication. */
res = secp256k1_ecmult_const_xonly(&resx, &n, (i & 1) ? &d : NULL, &q, 256, i & 2);
CHECK(res);
/* Perform normal multiplication. */
secp256k1_gej_set_ge(&basej, &base);
secp256k1_ecmult(&resj, &basej, &q, NULL);
/* Check that resj's X coordinate corresponds with resx. */
secp256k1_fe_sqr(&v, &resj.z);
secp256k1_fe_mul(&v, &v, &resx);
CHECK(check_fe_equal(&v, &resj.x));
}

/* Test that secp256k1_ecmult_const_xonly correctly rejects X coordinates not on curve. */
for (i = 0; i < 2*count; ++i) {
secp256k1_fe x, n, d, c, r;
int res;
secp256k1_scalar q;
random_scalar_order_test(&q);
/* Generate random X coordinate not on the curve. */
do {
random_field_element_test(&x);
secp256k1_fe_sqr(&c, &x);
secp256k1_fe_mul(&c, &c, &x);
secp256k1_fe_add(&c, &secp256k1_fe_const_b);
} while (secp256k1_fe_jacobi_var(&c) >= 0);
/* If i is odd, n=d*x for random non-zero d. */
if (i & 1) {
do {
random_field_element_test(&d);
} while (secp256k1_fe_normalizes_to_zero_var(&d));
secp256k1_fe_mul(&n, &x, &d);
} else {
n = x;
}
res = secp256k1_ecmult_const_xonly(&r, &n, (i & 1) ? &d : NULL, &q, 256, 0);
CHECK(res == 0);
}
}

void ecmult_const_chain_multiply(void) {
/* Check known result (randomly generated test problem from sage) */
const secp256k1_scalar scalar = SECP256K1_SCALAR_CONST(
Expand Down Expand Up @@ -4059,6 +4121,7 @@ void run_ecmult_const_tests(void) {
ecmult_const_random_mult();
ecmult_const_commutativity();
ecmult_const_chain_multiply();
ecmult_const_mult_xonly();
}

typedef struct {
Expand Down

0 comments on commit 855c8e6

Please sign in to comment.