@@ -56,6 +56,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
56
56
secp256k1_fe neg ;
57
57
secp256k1_ge_storage adds ;
58
58
secp256k1_scalar d ;
59
+ int first = 1 ;
59
60
60
61
memset (& adds , 0 , sizeof (adds ));
61
62
@@ -177,7 +178,6 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
177
178
* secp256k1_ecmult_gen_prec_table[b][index] stores the table(b, m) entries. Index
178
179
* is the relevant mask(b) bits of m packed together without gaps. */
179
180
180
- secp256k1_gej_set_infinity (r );
181
181
/* Outer loop: iterate over comb_off from COMB_SPACING - 1 down to 0. */
182
182
comb_off = COMB_SPACING - 1 ;
183
183
while (1 ) {
@@ -221,7 +221,13 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
221
221
secp256k1_fe_cmov (& add .y , & neg , sign );
222
222
223
223
/* 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
+ }
225
231
}
226
232
227
233
/* Double the result, except in the last iteration. */
0 commit comments