Skip to content

Commit cf8749e

Browse files
committed
fixup! Signed-digit based ecmult_const algorithm
1 parent ab875e8 commit cf8749e

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/ecmult_const_impl.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -225,33 +225,24 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
225225
* This means that all we need to do is add these looked up values together, multiplied
226226
* by 2^(ECMULT_GROUP_SIZE * group).
227227
*/
228-
229-
/* Run the first iteration (separated out so we can directly set r). */
230-
{
228+
for (group = ECMULT_CONST_GROUPS - 1; group >= 0; --group) {
231229
/* Using the _var get_bits function is ok here, since it's only variable in the offset/count, not in the scalar. */
232-
unsigned bits1 = secp256k1_scalar_get_bits_var(&v1, (ECMULT_CONST_GROUPS - 1U) * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE);
233-
unsigned bits2 = secp256k1_scalar_get_bits_var(&v2, (ECMULT_CONST_GROUPS - 1U) * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE);
234-
secp256k1_ge t;
235-
ECMULT_CONST_TABLE_GET_GE(&t, pre_a, bits1);
236-
secp256k1_gej_set_ge(&res, &t);
237-
ECMULT_CONST_TABLE_GET_GE(&t, pre_a_lam, bits2);
238-
secp256k1_gej_add_ge(&res, &res, &t);
239-
}
240-
241-
/* Run the next iterations. */
242-
for (group = ECMULT_CONST_GROUPS - 2; group >= 0; --group) {
243230
unsigned bits1 = secp256k1_scalar_get_bits_var(&v1, group * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE);
244231
unsigned bits2 = secp256k1_scalar_get_bits_var(&v2, group * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE);
245232
secp256k1_ge t;
246233
int j;
247234

248-
/* Shift the result so far up. */
249-
for (j = 0; j < ECMULT_CONST_GROUP_SIZE; ++j) {
250-
secp256k1_gej_double(&res, &res);
251-
}
252-
253235
ECMULT_CONST_TABLE_GET_GE(&t, pre_a, bits1);
254-
secp256k1_gej_add_ge(&res, &res, &t);
236+
if (group == ECMULT_CONST_GROUPS - 1) {
237+
/* Directly set res in the first iteration. */
238+
secp256k1_gej_set_ge(&res, &t);
239+
} else {
240+
/* Shift the result so far up. */
241+
for (j = 0; j < ECMULT_CONST_GROUP_SIZE; ++j) {
242+
secp256k1_gej_double(&res, &res);
243+
}
244+
secp256k1_gej_add_ge(&res, &res, &t);
245+
}
255246
ECMULT_CONST_TABLE_GET_GE(&t, pre_a_lam, bits2);
256247
secp256k1_gej_add_ge(&res, &res, &t);
257248
}

0 commit comments

Comments
 (0)