@@ -214,7 +214,6 @@ struct secp256k1_strauss_point_state {
214214 int wnaf_na_lam [129 ];
215215 int bits_na_1 ;
216216 int bits_na_lam ;
217- size_t input_pos ;
218217};
219218
220219struct secp256k1_strauss_state {
@@ -238,12 +237,13 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
238237 size_t np ;
239238 size_t no = 0 ;
240239
240+ secp256k1_fe_set_int (& Z , 1 );
241241 for (np = 0 ; np < num ; ++ np ) {
242+ secp256k1_gej tmp ;
242243 secp256k1_scalar na_1 , na_lam ;
243244 if (secp256k1_scalar_is_zero (& na [np ]) || secp256k1_gej_is_infinity (& a [np ])) {
244245 continue ;
245246 }
246- state -> ps [no ].input_pos = np ;
247247 /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
248248 secp256k1_scalar_split_lambda (& na_1 , & na_lam , & na [np ]);
249249
@@ -258,37 +258,33 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
258258 if (state -> ps [no ].bits_na_lam > bits ) {
259259 bits = state -> ps [no ].bits_na_lam ;
260260 }
261- ++ no ;
262- }
263261
264- /* Calculate odd multiples of a.
265- * All multiples are brought to the same Z 'denominator', which is stored
266- * in Z. Due to secp256k1' isomorphism we can do all operations pretending
267- * that the Z coordinate was 1, use affine addition formulae, and correct
268- * the Z coordinate of the result once at the end.
269- * The exception is the precomputed G table points, which are actually
270- * affine. Compared to the base used for other points, they have a Z ratio
271- * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
272- * isomorphism to efficiently add with a known Z inverse.
273- */
274- if (no > 0 ) {
275- /* Compute the odd multiples in Jacobian form. */
276- secp256k1_ecmult_odd_multiples_table (ECMULT_TABLE_SIZE (WINDOW_A ), state -> pre_a , state -> aux , & Z , & a [state -> ps [0 ].input_pos ]);
277- for (np = 1 ; np < no ; ++ np ) {
278- secp256k1_gej tmp = a [state -> ps [np ].input_pos ];
262+ /* Calculate odd multiples of a.
263+ * All multiples are brought to the same Z 'denominator', which is stored
264+ * in Z. Due to secp256k1' isomorphism we can do all operations pretending
265+ * that the Z coordinate was 1, use affine addition formulae, and correct
266+ * the Z coordinate of the result once at the end.
267+ * The exception is the precomputed G table points, which are actually
268+ * affine. Compared to the base used for other points, they have a Z ratio
269+ * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
270+ * isomorphism to efficiently add with a known Z inverse.
271+ */
272+ tmp = a [np ];
273+ if (no ) {
279274#ifdef VERIFY
280275 secp256k1_fe_normalize_var (& Z );
281276#endif
282277 secp256k1_gej_rescale (& tmp , & Z );
283- 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 );
284- 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 ));
285278 }
286- /* Bring them to the same Z denominator. */
287- secp256k1_ge_table_set_globalz ( ECMULT_TABLE_SIZE ( WINDOW_A ) * no , state -> pre_a , state -> aux );
288- } else {
289- secp256k1_fe_set_int ( & Z , 1 ) ;
279+ 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 );
280+ if ( no ) secp256k1_fe_mul ( state -> aux + no * ECMULT_TABLE_SIZE ( WINDOW_A ) , state -> aux + no * ECMULT_TABLE_SIZE ( WINDOW_A ), & ( a [ np ]. z ) );
281+
282+ ++ no ;
290283 }
291284
285+ /* Bring them to the same Z denominator. */
286+ secp256k1_ge_table_set_globalz (ECMULT_TABLE_SIZE (WINDOW_A ) * no , state -> pre_a , state -> aux );
287+
292288 for (np = 0 ; np < no ; ++ np ) {
293289 for (i = 0 ; i < ECMULT_TABLE_SIZE (WINDOW_A ); i ++ ) {
294290 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