Skip to content

Commit 1cd7ab2

Browse files
Eliminate input_pos state field from ecmult_strauss_wnaf.
1 parent 701e83c commit 1cd7ab2

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/ecmult_impl.h

+21-27
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ struct secp256k1_strauss_point_state {
220220
int wnaf_na_lam[129];
221221
int bits_na_1;
222222
int bits_na_lam;
223-
size_t input_pos;
224223
};
225224

226225
struct secp256k1_strauss_state {
@@ -244,12 +243,13 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
244243
size_t np;
245244
size_t no = 0;
246245

246+
secp256k1_fe_set_int(&Z, 1);
247247
for (np = 0; np < num; ++np) {
248+
secp256k1_gej tmp;
248249
secp256k1_scalar na_1, na_lam;
249250
if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
250251
continue;
251252
}
252-
state->ps[no].input_pos = np;
253253
/* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
254254
secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
255255

@@ -264,40 +264,34 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
264264
if (state->ps[no].bits_na_lam > bits) {
265265
bits = state->ps[no].bits_na_lam;
266266
}
267-
++no;
268-
}
269267

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

292+
/* Bring them to the same Z denominator. */
293+
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
294+
301295
for (np = 0; np < no; ++np) {
302296
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
303297
secp256k1_ge pre_a_lam;

0 commit comments

Comments
 (0)