Skip to content

Commit 4283857

Browse files
committed
Add x-only ecmult_const version for x=n/d
1 parent d70f0fe commit 4283857

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

src/ecmult_const.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,23 @@
1818
*/
1919
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q, int bits);
2020

21+
/**
22+
* Same as secp256k1_ecmult_const, but takes in an x coordinate of the base point
23+
* only, specified as fraction n/d. Only the x coordinate of the result is returned.
24+
*
25+
* If known_on_curve is 0, a verification is performed that n/d is a valid X
26+
* coordinate, and 0 is returned if not. Otherwise, 1 is returned.
27+
*
28+
* d being NULL is interpreted as d=1.
29+
*
30+
* Constant time in the value of q, but not any other inputs.
31+
*/
32+
static int secp256k1_ecmult_const_xonly(
33+
secp256k1_fe* r,
34+
const secp256k1_fe *n,
35+
const secp256k1_fe *d,
36+
const secp256k1_scalar *q,
37+
int bits,
38+
int known_on_curve);
39+
2140
#endif /* SECP256K1_ECMULT_CONST_H */

src/ecmult_const_impl.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,58 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
228228
secp256k1_fe_mul(&r->z, &r->z, &Z);
229229
}
230230

231+
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) {
232+
233+
/* This algorithm is a generalization of Peter Dettman's technique for
234+
* avoiding the square root in a random-basepoint x-only multiplication
235+
* on a Weierstrass curve:
236+
* https://mailarchive.ietf.org/arch/msg/cfrg/7DyYY6gg32wDgHAhgSb6XxMDlJA/
237+
*/
238+
secp256k1_fe g, i;
239+
secp256k1_ge p;
240+
secp256k1_gej rj;
241+
242+
/* Compute g = (n^3 + B*d^3). */
243+
secp256k1_fe_sqr(&g, n);
244+
secp256k1_fe_mul(&g, &g, n);
245+
if (d) {
246+
secp256k1_fe b;
247+
secp256k1_fe_sqr(&b, d);
248+
secp256k1_fe_mul(&b, &b, d);
249+
secp256k1_fe_mul(&b, &b, &secp256k1_fe_const_b);
250+
secp256k1_fe_add(&g, &b);
251+
if (!known_on_curve) {
252+
secp256k1_fe c;
253+
secp256k1_fe_mul(&c, &g, d);
254+
if (secp256k1_fe_jacobi_var(&c) < 0) return 0;
255+
}
256+
} else {
257+
secp256k1_fe_add(&g, &secp256k1_fe_const_b);
258+
if (!known_on_curve) {
259+
if (secp256k1_fe_jacobi_var(&g) < 0) return 0;
260+
}
261+
}
262+
263+
/* Compute base point P = (n*g, g^2), the effective affine version of
264+
* (n*g, g^2, sqrt(d*g)), which has corresponding affine X coordinate
265+
* n/d. */
266+
secp256k1_fe_mul(&p.x, &g, n);
267+
secp256k1_fe_sqr(&p.y, &g);
268+
p.infinity = 0;
269+
270+
/* Perform x-only EC multiplication of P with q. */
271+
secp256k1_ecmult_const(&rj, &p, q, bits);
272+
273+
/* The resulting (X, Y, Z) point on the effective-affine isomorphic curve
274+
* corresponds to (X, Y, Z*sqrt(d*g)) on the secp256k1 curve. The affine
275+
* version of that has X coordinate (X / (Z^2*d*g)). */
276+
secp256k1_fe_sqr(&i, &rj.z);
277+
secp256k1_fe_mul(&i, &i, &g);
278+
if (d) secp256k1_fe_mul(&i, &i, d);
279+
secp256k1_fe_inv(&i, &i);
280+
secp256k1_fe_mul(r, &rj.x, &i);
281+
282+
return 1;
283+
}
284+
231285
#endif /* SECP256K1_ECMULT_CONST_IMPL_H */

src/tests.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4028,6 +4028,68 @@ void ecmult_const_mult_zero_one(void) {
40284028
ge_equals_ge(&res2, &point);
40294029
}
40304030

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

40644127
typedef struct {

0 commit comments

Comments
 (0)