Skip to content

Commit 6034a04

Browse files
Merge #778: secp256k1_gej_double_nonzero supports infinity
18d3632 secp256k1_gej_double_nonzero supports infinity (Pieter Wuille) Pull request description: Our existing function `secp256k1_gej_double_nonzero` actually supports infinity if only it wouldn't check that the input isn't infinity. Drop the check, rename it to `secp256k1_gej_double`, and adapt the tests. ACKs for top commit: real-or-random: ACK 18d3632 I looked at the diff and ran tests locally gmaxwell: ACK 18d3632 Tree-SHA512: 79dc42099c318f0bdfe7961495ab3fbbe87551c3cc373557a371914bb65638b129ddfd360e694959349f184e2d71a540abdbef04211e7eb70ee17b691632b915
2 parents f609159 + 18d3632 commit 6034a04

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

src/ecmult_const_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
208208
int n;
209209
int j;
210210
for (j = 0; j < WINDOW_A - 1; ++j) {
211-
secp256k1_gej_double_nonzero(r, r);
211+
secp256k1_gej_double(r, r);
212212
}
213213

214214
n = wnaf_1[i];

src/group.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ static int secp256k1_gej_is_infinity(const secp256k1_gej *a);
9595
/** Check whether a group element's y coordinate is a quadratic residue. */
9696
static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a);
9797

98-
/** Set r equal to the double of a, a cannot be infinity. Constant time. */
99-
static void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a);
98+
/** Set r equal to the double of a. Constant time. */
99+
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a);
100100

101101
/** Set r equal to the double of a. If rzr is not-NULL this sets *rzr such that r->z == a->z * *rzr (where infinity means an implicit z = 0). */
102102
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr);

src/group_impl.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static int secp256k1_ge_is_valid_var(const secp256k1_ge *a) {
303303
return secp256k1_fe_equal_var(&y2, &x3);
304304
}
305305

306-
static SECP256K1_INLINE void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a) {
306+
static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a) {
307307
/* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
308308
*
309309
* Note that there is an implementation described at
@@ -313,8 +313,7 @@ static SECP256K1_INLINE void secp256k1_gej_double_nonzero(secp256k1_gej *r, cons
313313
*/
314314
secp256k1_fe t1,t2,t3,t4;
315315

316-
VERIFY_CHECK(!secp256k1_gej_is_infinity(a));
317-
r->infinity = 0;
316+
r->infinity = a->infinity;
318317

319318
secp256k1_fe_mul(&r->z, &a->z, &a->y);
320319
secp256k1_fe_mul_int(&r->z, 2); /* Z' = 2*Y*Z (2) */
@@ -363,7 +362,7 @@ static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, s
363362
secp256k1_fe_mul_int(rzr, 2);
364363
}
365364

366-
secp256k1_gej_double_nonzero(r, a);
365+
secp256k1_gej_double(r, a);
367366
}
368367

369368
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr) {

src/tests.c

+3
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,9 @@ void test_ge(void) {
22182218
/* Normal doubling. */
22192219
secp256k1_gej_double_var(&resj, &gej[i2], NULL);
22202220
ge_equals_gej(&ref, &resj);
2221+
/* Constant-time doubling. */
2222+
secp256k1_gej_double(&resj, &gej[i2]);
2223+
ge_equals_gej(&ref, &resj);
22212224
}
22222225

22232226
/* Test adding opposites. */

src/tests_exhaustive.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,8 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
141141
/* Check doubling */
142142
for (i = 0; i < order; i++) {
143143
secp256k1_gej tmp;
144-
if (i > 0) {
145-
secp256k1_gej_double_nonzero(&tmp, &groupj[i]);
146-
ge_equals_gej(&group[(2 * i) % order], &tmp);
147-
}
144+
secp256k1_gej_double(&tmp, &groupj[i]);
145+
ge_equals_gej(&group[(2 * i) % order], &tmp);
148146
secp256k1_gej_double_var(&tmp, &groupj[i], NULL);
149147
ge_equals_gej(&group[(2 * i) % order], &tmp);
150148
}

0 commit comments

Comments
 (0)