Skip to content

Commit 5aea2e8

Browse files
Eliminate input_pos state field from ecmult_strauss_wnaf.
1 parent 80e9193 commit 5aea2e8

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/ecmult_impl.h

+21-25
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ struct secp256k1_strauss_point_state {
224224
int wnaf_na_lam[129];
225225
int bits_na_1;
226226
int bits_na_lam;
227-
size_t input_pos;
228227
};
229228

230229
struct secp256k1_strauss_state {
@@ -248,12 +247,13 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
248247
size_t np;
249248
size_t no = 0;
250249

250+
secp256k1_fe_set_int(&Z, 1);
251251
for (np = 0; np < num; ++np) {
252+
secp256k1_gej tmp;
252253
secp256k1_scalar na_1, na_lam;
253254
if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
254255
continue;
255256
}
256-
state->ps[no].input_pos = np;
257257
/* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
258258
secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
259259

@@ -268,37 +268,33 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
268268
if (state->ps[no].bits_na_lam > bits) {
269269
bits = state->ps[no].bits_na_lam;
270270
}
271-
++no;
272-
}
273271

274-
/* Calculate odd multiples of a.
275-
* All multiples are brought to the same Z 'denominator', which is stored
276-
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
277-
* that the Z coordinate was 1, use affine addition formulae, and correct
278-
* the Z coordinate of the result once at the end.
279-
* The exception is the precomputed G table points, which are actually
280-
* affine. Compared to the base used for other points, they have a Z ratio
281-
* of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
282-
* isomorphism to efficiently add with a known Z inverse.
283-
*/
284-
if (no > 0) {
285-
/* Compute the odd multiples in Jacobian form. */
286-
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a, state->aux, &Z, &a[state->ps[0].input_pos]);
287-
for (np = 1; np < no; ++np) {
288-
secp256k1_gej tmp = a[state->ps[np].input_pos];
272+
/* Calculate odd multiples of a.
273+
* All multiples are brought to the same Z 'denominator', which is stored
274+
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
275+
* that the Z coordinate was 1, use affine addition formulae, and correct
276+
* the Z coordinate of the result once at the end.
277+
* The exception is the precomputed G table points, which are actually
278+
* affine. Compared to the base used for other points, they have a Z ratio
279+
* of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
280+
* isomorphism to efficiently add with a known Z inverse.
281+
*/
282+
tmp = a[np];
283+
if (no) {
289284
#ifdef VERIFY
290285
secp256k1_fe_normalize_var(&Z);
291286
#endif
292287
secp256k1_gej_rescale(&tmp, &Z);
293-
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a + np * ECMULT_TABLE_SIZE(WINDOW_A), state->aux + np * ECMULT_TABLE_SIZE(WINDOW_A), &Z, &tmp);
294-
secp256k1_fe_mul(state->aux + np * ECMULT_TABLE_SIZE(WINDOW_A), state->aux + np * ECMULT_TABLE_SIZE(WINDOW_A), &(a[state->ps[np].input_pos].z));
295288
}
296-
/* Bring them to the same Z denominator. */
297-
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
298-
} else {
299-
secp256k1_fe_set_int(&Z, 1);
289+
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a + no * ECMULT_TABLE_SIZE(WINDOW_A), state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), &Z, &tmp);
290+
if (no) secp256k1_fe_mul(state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), &(a[np].z));
291+
292+
++no;
300293
}
301294

295+
/* Bring them to the same Z denominator. */
296+
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
297+
302298
for (np = 0; np < no; ++np) {
303299
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
304300
secp256k1_fe_mul(&state->aux[np * ECMULT_TABLE_SIZE(WINDOW_A) + i], &state->pre_a[np * ECMULT_TABLE_SIZE(WINDOW_A) + i].x, &secp256k1_const_beta);

0 commit comments

Comments
 (0)