Skip to content

Commit 4ba9971

Browse files
Eliminate input_pos state field from ecmult_strauss_wnaf.
1 parent 072f1ff commit 4ba9971

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
@@ -446,7 +446,6 @@ struct secp256k1_strauss_point_state {
446446
int wnaf_na_lam[129];
447447
int bits_na_1;
448448
int bits_na_lam;
449-
size_t input_pos;
450449
};
451450

452451
struct secp256k1_strauss_state {
@@ -470,12 +469,13 @@ static void secp256k1_ecmult_strauss_wnaf(const secp256k1_ecmult_context *ctx, c
470469
size_t np;
471470
size_t no = 0;
472471

472+
secp256k1_fe_set_int(&Z, 1);
473473
for (np = 0; np < num; ++np) {
474+
secp256k1_gej tmp;
474475
secp256k1_scalar na_1, na_lam;
475476
if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
476477
continue;
477478
}
478-
state->ps[no].input_pos = np;
479479
/* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
480480
secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
481481

@@ -490,40 +490,34 @@ static void secp256k1_ecmult_strauss_wnaf(const secp256k1_ecmult_context *ctx, c
490490
if (state->ps[no].bits_na_lam > bits) {
491491
bits = state->ps[no].bits_na_lam;
492492
}
493-
++no;
494-
}
495493

496-
/* Calculate odd multiples of a.
497-
* All multiples are brought to the same Z 'denominator', which is stored
498-
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
499-
* that the Z coordinate was 1, use affine addition formulae, and correct
500-
* the Z coordinate of the result once at the end.
501-
* The exception is the precomputed G table points, which are actually
502-
* affine. Compared to the base used for other points, they have a Z ratio
503-
* of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
504-
* isomorphism to efficiently add with a known Z inverse.
505-
*/
506-
if (no > 0) {
507-
/* Compute the odd multiples in Jacobian form. */
508-
secp256k1_gej tmp = a[state->ps[0].input_pos];
509-
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a, state->aux, &tmp);
510-
for (np = 1; np < no; ++np) {
511-
Z = tmp.z;
512-
tmp = a[state->ps[np].input_pos];
494+
/* Calculate odd multiples of a.
495+
* All multiples are brought to the same Z 'denominator', which is stored
496+
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
497+
* that the Z coordinate was 1, use affine addition formulae, and correct
498+
* the Z coordinate of the result once at the end.
499+
* The exception is the precomputed G table points, which are actually
500+
* affine. Compared to the base used for other points, they have a Z ratio
501+
* of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
502+
* isomorphism to efficiently add with a known Z inverse.
503+
*/
504+
tmp = a[np];
505+
if (no) {
513506
#ifdef VERIFY
514507
secp256k1_fe_normalize_var(&Z);
515508
#endif
516509
secp256k1_gej_rescale(&tmp, &Z);
517-
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);
518-
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));
519510
}
520-
/* Bring them to the same Z denominator. */
511+
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);
512+
if (no) secp256k1_fe_mul(state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), state->aux + no * ECMULT_TABLE_SIZE(WINDOW_A), &(a[np].z));
521513
Z = tmp.z;
522-
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
523-
} else {
524-
secp256k1_fe_set_int(&Z, 1);
514+
515+
++no;
525516
}
526517

518+
/* Bring them to the same Z denominator. */
519+
secp256k1_ge_globalz_fixup_table(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
520+
527521
for (np = 0; np < no; ++np) {
528522
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
529523
secp256k1_ge pre_a_lam;

0 commit comments

Comments
 (0)