@@ -40,22 +40,21 @@ static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx
4040 * precomputed (call it prec(i, n_i)). The formula now becomes sum(prec(i, n_i), i=0 ... PREC_N-1).
4141 * None of the resulting prec group elements have a known scalar, and neither do any of
4242 * the intermediate sums while computing a*G.
43- * The prec values are stored in secp256k1_ecmult_gen_prec_table[j][i ] = (PREC_G)^j * i * G + U_i.
43+ * The prec values are stored in secp256k1_ecmult_gen_prec_table[i][n_i ] = n_i * (PREC_G)^i * G + U_i.
4444 */
4545static void secp256k1_ecmult_gen (const secp256k1_ecmult_gen_context * ctx , secp256k1_gej * r , const secp256k1_scalar * gn ) {
4646 secp256k1_ge add ;
4747 secp256k1_ge_storage adds ;
4848 secp256k1_scalar gnb ;
49- int bits ;
50- int i , j ;
49+ int i , j , n_i ;
5150 memset (& adds , 0 , sizeof (adds ));
5251 * r = ctx -> initial ;
5352 /* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
5453 secp256k1_scalar_add (& gnb , gn , & ctx -> blind );
5554 add .infinity = 0 ;
56- for (j = 0 ; j < ECMULT_GEN_PREC_N ; j ++ ) {
57- bits = secp256k1_scalar_get_bits (& gnb , j * ECMULT_GEN_PREC_B , ECMULT_GEN_PREC_B );
58- for (i = 0 ; i < ECMULT_GEN_PREC_G ; i ++ ) {
55+ for (i = 0 ; i < ECMULT_GEN_PREC_N ; i ++ ) {
56+ n_i = secp256k1_scalar_get_bits (& gnb , i * ECMULT_GEN_PREC_B , ECMULT_GEN_PREC_B );
57+ for (j = 0 ; j < ECMULT_GEN_PREC_G ; j ++ ) {
5958 /** This uses a conditional move to avoid any secret data in array indexes.
6059 * _Any_ use of secret indexes has been demonstrated to result in timing
6160 * sidechannels, even when the cache-line access patterns are uniform.
@@ -66,12 +65,12 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
6665 * by Dag Arne Osvik, Adi Shamir, and Eran Tromer
6766 * (https://www.tau.ac.il/~tromer/papers/cache.pdf)
6867 */
69- secp256k1_ge_storage_cmov (& adds , & secp256k1_ecmult_gen_prec_table [j ][ i ], i == bits );
68+ secp256k1_ge_storage_cmov (& adds , & secp256k1_ecmult_gen_prec_table [i ][ j ], j == n_i );
7069 }
7170 secp256k1_ge_from_storage (& add , & adds );
7271 secp256k1_gej_add_ge (r , r , & add );
7372 }
74- bits = 0 ;
73+ n_i = 0 ;
7574 secp256k1_ge_clear (& add );
7675 secp256k1_scalar_clear (& gnb );
7776}
0 commit comments