Skip to content

Commit a0d32b5

Browse files
peterdettmansipa
authored andcommitted
Optimization: use Nx32 representation for recoded bits
The existing code needs to deal with the edge case that bit_pos >= 256, which would lead to an out-of-bounds read from secp256k1_scalar. Instead, recode the scalar into an array of uint32_t with enough zero padding at the end to alleviate the issue. This also simplifies the code, and is necessary for a security improvement in a follow-up commit. Original code by Peter Dettman, with modifications by Pieter Wuille.
1 parent e03dcc4 commit a0d32b5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/ecmult_gen_impl.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
5656
secp256k1_fe neg;
5757
secp256k1_ge_storage adds;
5858
secp256k1_scalar d;
59-
int first = 1;
59+
/* Array of uint32_t values large enough to store COMB_BITS bits. Only the bottom
60+
* 8 are ever nonzero, but having the zero padding at the end if COMB_BITS>256
61+
* avoids the need to deal with out-of-bounds reads from a scalar. */
62+
uint32_t recoded[(COMB_BITS + 31) >> 5] = {0};
63+
int first = 1, i;
6064

6165
memset(&adds, 0, sizeof(adds));
6266

@@ -103,6 +107,11 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
103107

104108
/* Compute the scalar d = (gn + ctx->scalar_offset). */
105109
secp256k1_scalar_add(&d, &ctx->scalar_offset, gn);
110+
/* Convert to recoded array. */
111+
for (i = 0; i < 8; ++i) {
112+
recoded[i] = secp256k1_scalar_get_bits_limb32(&d, 32 * i, 32);
113+
}
114+
secp256k1_scalar_clear(&d);
106115

107116
/* In secp256k1_ecmult_gen_prec_table we have precomputed sums of the
108117
* (2*d[i]-1) * 2^(i-1) * G points, for various combinations of i positions.
@@ -188,8 +197,8 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
188197
/* Gather the mask(block)-selected bits of d into bits. They're packed:
189198
* bits[tooth] = d[(block*COMB_TEETH + tooth)*COMB_SPACING + comb_off]. */
190199
uint32_t bits = 0, sign, abs, index, tooth;
191-
for (tooth = 0; tooth < COMB_TEETH && bit_pos < 256; ++tooth) {
192-
uint32_t bit = secp256k1_scalar_get_bits_limb32(&d, bit_pos, 1);
200+
for (tooth = 0; tooth < COMB_TEETH; ++tooth) {
201+
uint32_t bit = (recoded[bit_pos >> 5] >> (bit_pos & 0x1f)) & 1;
193202
bits |= bit << tooth;
194203
bit_pos += COMB_SPACING;
195204
}
@@ -243,7 +252,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
243252
secp256k1_fe_clear(&neg);
244253
secp256k1_ge_clear(&add);
245254
memset(&adds, 0, sizeof(adds));
246-
secp256k1_scalar_clear(&d);
255+
memset(&recoded, 0, sizeof(recoded));
247256
}
248257

249258
/* Setup blinding values for secp256k1_ecmult_gen. */

0 commit comments

Comments
 (0)