Skip to content

Commit 9d2c777

Browse files
Eliminate input_pos state field from ecmult_strauss_wnaf.
1 parent b7599c9 commit 9d2c777

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
@@ -207,7 +207,6 @@ struct secp256k1_strauss_point_state {
207207
int wnaf_na_lam[129];
208208
int bits_na_1;
209209
int bits_na_lam;
210-
size_t input_pos;
211210
};
212211

213212
struct secp256k1_strauss_state {
@@ -231,12 +230,13 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
231230
size_t np;
232231
size_t no = 0;
233232

233+
secp256k1_fe_set_int(&Z, 1);
234234
for (np = 0; np < num; ++np) {
235+
secp256k1_gej tmp;
235236
secp256k1_scalar na_1, na_lam;
236237
if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
237238
continue;
238239
}
239-
state->ps[no].input_pos = np;
240240
/* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
241241
secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
242242

@@ -251,37 +251,33 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
251251
if (state->ps[no].bits_na_lam > bits) {
252252
bits = state->ps[no].bits_na_lam;
253253
}
254-
++no;
255-
}
256254

257-
/* Calculate odd multiples of a.
258-
* All multiples are brought to the same Z 'denominator', which is stored
259-
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
260-
* that the Z coordinate was 1, use affine addition formulae, and correct
261-
* the Z coordinate of the result once at the end.
262-
* The exception is the precomputed G table points, which are actually
263-
* affine. Compared to the base used for other points, they have a Z ratio
264-
* of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
265-
* isomorphism to efficiently add with a known Z inverse.
266-
*/
267-
if (no > 0) {
268-
/* Compute the odd multiples in Jacobian form. */
269-
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a, state->aux, &Z, &a[state->ps[0].input_pos]);
270-
for (np = 1; np < no; ++np) {
271-
secp256k1_gej tmp = a[state->ps[np].input_pos];
255+
/* Calculate odd multiples of a.
256+
* All multiples are brought to the same Z 'denominator', which is stored
257+
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
258+
* that the Z coordinate was 1, use affine addition formulae, and correct
259+
* the Z coordinate of the result once at the end.
260+
* The exception is the precomputed G table points, which are actually
261+
* affine. Compared to the base used for other points, they have a Z ratio
262+
* of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
263+
* isomorphism to efficiently add with a known Z inverse.
264+
*/
265+
tmp = a[np];
266+
if (no) {
272267
#ifdef VERIFY
273268
secp256k1_fe_normalize_var(&Z);
274269
#endif
275270
secp256k1_gej_rescale(&tmp, &Z);
276-
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);
277-
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));
278271
}
279-
/* Bring them to the same Z denominator. */
280-
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
281-
} else {
282-
secp256k1_fe_set_int(&Z, 1);
272+
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);
273+
if (no) secp256k1_fe_mul(state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), &(a[np].z));
274+
275+
++no;
283276
}
284277

278+
/* Bring them to the same Z denominator. */
279+
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
280+
285281
for (np = 0; np < no; ++np) {
286282
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
287283
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)