Skip to content

Commit 2b45d49

Browse files
committed
Optimization: first table lookup needs no point addition
1 parent fdd2299 commit 2b45d49

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/ecmult_gen_impl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
5454
secp256k1_fe neg;
5555
secp256k1_ge_storage adds;
5656
secp256k1_scalar recoded;
57+
int first = 1;
5758

5859
memset(&adds, 0, sizeof(adds));
59-
secp256k1_gej_set_infinity(r);
6060

6161
/* We want to compute R = gn*G.
6262
*
@@ -176,7 +176,13 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
176176
secp256k1_fe_cmov(&add.y, &neg, sign);
177177

178178
/* Add the looked up and conditionally negated value to r. */
179-
secp256k1_gej_add_ge(r, r, &add);
179+
if (EXPECT(first, 0)) {
180+
/* If this is the first table lookup, we can skip addition. */
181+
secp256k1_gej_set_ge(r, &add);
182+
first = 0;
183+
} else {
184+
secp256k1_gej_add_ge(r, r, &add);
185+
}
180186
}
181187

182188
/* Double the result, except in the last iteration. */

0 commit comments

Comments
 (0)