Skip to content

Commit 80fc6f1

Browse files
Eliminate input_pos state field from ecmult_strauss_wnaf.
1 parent 486bd9c commit 80fc6f1

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
@@ -219,7 +219,6 @@ struct secp256k1_strauss_point_state {
219219
int wnaf_na_lam[129];
220220
int bits_na_1;
221221
int bits_na_lam;
222-
size_t input_pos;
223222
};
224223

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

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

@@ -263,37 +263,33 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
263263
if (state->ps[no].bits_na_lam > bits) {
264264
bits = state->ps[no].bits_na_lam;
265265
}
266-
++no;
267-
}
268266

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

290+
/* Bring them to the same Z denominator. */
291+
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
292+
297293
for (np = 0; np < no; ++np) {
298294
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
299295
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)