Skip to content

Commit fdd2299

Browse files
committed
Optimization: move (2^COMB_BITS-1)/2 term into ctx->scalar_offset
It is unnecessary to recompute this term needed by the SDMC algorithm for every multiplication; move it into the context scalar_offset value instead.
1 parent 467f8f3 commit fdd2299

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/ecmult_gen_impl.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
6868
* (gn - b + (2^COMB_BITS - 1)/2). In other words, the bits of that scalar select
6969
* whether to add normal or negated versions of 2^(i-1)*G together.
7070
*
71-
* We have precomputed -b = ctx->scalar_offset, and b*G = ctx->final_point_add, so our
72-
* overall result becomes R = sum((2*d_i-1)*2^(i-1)*G) + ctx->final_point_add, with
73-
* d_i the bits of scalar (gn + ctx->scalar_offset + (2^COMB_BITS - 1)/2).
71+
* We have precomputed -b + (2^COMB_BITS - 1)/2) = ctx->scalar_offset, and b*G =
72+
* ctx->final_point_add, so our overall result becomes R = sum((2*d_i-1)*2^(i-1)*G)
73+
* + ctx->final_point_add, with d_i the bits of scalar (gn + ctx->scalar_offset).
7474
*/
7575

76-
/* Compute the scalar (gn + ctx->scalar_offset + (2^COMB_BITS - 1)/2). */
77-
secp256k1_ecmult_gen_scalar_diff(&recoded);
78-
secp256k1_scalar_add(&recoded, &recoded, &ctx->scalar_offset);
79-
secp256k1_scalar_add(&recoded, &recoded, gn);
76+
/* Compute the scalar (gn + ctx->scalar_offset). */
77+
secp256k1_scalar_add(&recoded, &ctx->scalar_offset, gn);
8078

8179
/* In secp256k1_ecmult_gen_prec_table we have precomputed sums of the
8280
* (2*d_i-1) * 2^(i-1) * G points, for various combinations of i positions.
@@ -200,14 +198,19 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
200198
/* Setup blinding values for secp256k1_ecmult_gen. */
201199
static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32) {
202200
secp256k1_scalar b;
201+
secp256k1_scalar diff;
203202
secp256k1_gej gb;
204203
unsigned char nonce32[32];
205204
secp256k1_rfc6979_hmac_sha256 rng;
206205
unsigned char keydata[64] = {0};
206+
207+
/* Compute the (2^COMB_BITS - 1)/2 term once. */
208+
secp256k1_ecmult_gen_scalar_diff(&diff);
209+
207210
if (seed32 == NULL) {
208211
/* When seed is NULL, reset the final point and blinding value. */
209212
secp256k1_ge_neg(&ctx->final_point_add, &secp256k1_ge_const_g);
210-
ctx->scalar_offset = secp256k1_scalar_one;
213+
secp256k1_scalar_add(&ctx->scalar_offset, &secp256k1_scalar_one, &diff);
211214
}
212215
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
213216
secp256k1_scalar_get_b32(nonce32, &ctx->scalar_offset);
@@ -224,7 +227,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
224227

225228
/* TODO: reintroduce projective blinding. */
226229

227-
/* For a random blinding value b, set ctx->scalar_offset=-b, ctx->final_point_add=bG. */
230+
/* For a random blinding value b, set scalar_offset=diff-n, final_point_add=bG */
228231
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
229232
secp256k1_scalar_set_b32(&b, nonce32, NULL);
230233
/* The blinding value cannot be zero, as that would mean final_point_add = infinity,
@@ -234,7 +237,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
234237
memset(nonce32, 0, 32);
235238
secp256k1_ecmult_gen(ctx, &gb, &b);
236239
secp256k1_scalar_negate(&b, &b);
237-
ctx->scalar_offset = b;
240+
secp256k1_scalar_add(&ctx->scalar_offset, &b, &diff);
238241
secp256k1_ge_set_gej(&ctx->final_point_add, &gb);
239242

240243
/* Clean up. */

0 commit comments

Comments
 (0)