Skip to content

Commit 15d0cca

Browse files
committed
Optimization: first table lookup needs no point addition
1 parent 7a33db3 commit 15d0cca

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
@@ -56,6 +56,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
5656
secp256k1_fe neg;
5757
secp256k1_ge_storage adds;
5858
secp256k1_scalar d;
59+
int first = 1;
5960

6061
memset(&adds, 0, sizeof(adds));
6162

@@ -177,7 +178,6 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
177178
* secp256k1_ecmult_gen_prec_table[b][index] stores the table(b, m) entries. Index
178179
* is the relevant mask(b) bits of m packed together without gaps. */
179180

180-
secp256k1_gej_set_infinity(r);
181181
/* Outer loop: iterate over comb_off from COMB_SPACING - 1 down to 0. */
182182
comb_off = COMB_SPACING - 1;
183183
while (1) {
@@ -221,7 +221,13 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
221221
secp256k1_fe_cmov(&add.y, &neg, sign);
222222

223223
/* Add the looked up and conditionally negated value to r. */
224-
secp256k1_gej_add_ge(r, r, &add);
224+
if (EXPECT(first, 0)) {
225+
/* If this is the first table lookup, we can skip addition. */
226+
secp256k1_gej_set_ge(r, &add);
227+
first = 0;
228+
} else {
229+
secp256k1_gej_add_ge(r, r, &add);
230+
}
225231
}
226232

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

0 commit comments

Comments
 (0)