Skip to content

Commit 7a33db3

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 ed2a056 commit 7a33db3

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/ecmult_gen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ typedef struct {
103103

104104
/* Values chosen such that
105105
*
106-
* n*G == comb(n + (2^COMB_BITS-1)/2 + scalar_offset, G/2) + ge_offset.
106+
* n*G == comb(n + scalar_offset, G/2) + ge_offset.
107107
*
108108
* This expression lets us use scalar blinding and optimize the comb precomputation. See
109109
* ecmult_gen_impl.h for more details. */

src/ecmult_gen_impl.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,17 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
9191
*
9292
* Adding precomputation, our final equations become:
9393
*
94-
* ctx->scalar_offset = -b
94+
* ctx->scalar_offset = (2^COMB_BITS - 1)/2 - b (mod order)
9595
* ctx->ge_offset = b*G
96-
* d = gn + ctx->scalar_offset + (2^COMB_BITS - 1)/2 (mod order)
96+
* d = gn + ctx->scalar_offset (mod order)
9797
* R = comb(d, G/2) + ctx->ge_offset
9898
*
9999
* comb(d, G/2) function is then computed by summing + or - 2^(i-1)*G, for i=0..COMB_BITS-1,
100100
* depending on the value of the bits d[i] of the binary representation of scalar d.
101101
*/
102102

103-
/* Compute the scalar d = (gn + ctx->scalar_offset + (2^COMB_BITS - 1)/2). */
104-
secp256k1_ecmult_gen_scalar_diff(&d);
105-
secp256k1_scalar_add(&d, &d, &ctx->scalar_offset);
106-
secp256k1_scalar_add(&d, &d, gn);
103+
/* Compute the scalar d = (gn + ctx->scalar_offset). */
104+
secp256k1_scalar_add(&d, &ctx->scalar_offset, gn);
107105

108106
/* In secp256k1_ecmult_gen_prec_table we have precomputed sums of the
109107
* (2*d[i]-1) * 2^(i-1) * G points, for various combinations of i positions.
@@ -245,14 +243,19 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
245243
/* Setup blinding values for secp256k1_ecmult_gen. */
246244
static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32) {
247245
secp256k1_scalar b;
246+
secp256k1_scalar diff;
248247
secp256k1_gej gb;
249248
unsigned char nonce32[32];
250249
secp256k1_rfc6979_hmac_sha256 rng;
251250
unsigned char keydata[64];
251+
252+
/* Compute the (2^COMB_BITS - 1)/2 term once. */
253+
secp256k1_ecmult_gen_scalar_diff(&diff);
254+
252255
if (seed32 == NULL) {
253256
/* When seed is NULL, reset the final point and blinding value. */
254257
secp256k1_ge_neg(&ctx->ge_offset, &secp256k1_ge_const_g);
255-
ctx->scalar_offset = secp256k1_scalar_one;
258+
secp256k1_scalar_add(&ctx->scalar_offset, &secp256k1_scalar_one, &diff);
256259
return;
257260
}
258261
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
@@ -268,7 +271,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
268271

269272
/* TODO: reintroduce projective blinding. */
270273

271-
/* For a random blinding value b, set ctx->scalar_offset=-b, ctx->ge_offset=bG. */
274+
/* For a random blinding value b, set scalar_offset=diff-b, ge_offset=bG */
272275
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
273276
secp256k1_scalar_set_b32(&b, nonce32, NULL);
274277
/* The blinding value cannot be zero, as that would mean ge_offset = infinity,
@@ -278,7 +281,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
278281
memset(nonce32, 0, 32);
279282
secp256k1_ecmult_gen(ctx, &gb, &b);
280283
secp256k1_scalar_negate(&b, &b);
281-
ctx->scalar_offset = b;
284+
secp256k1_scalar_add(&ctx->scalar_offset, &b, &diff);
282285
secp256k1_ge_set_gej(&ctx->ge_offset, &gb);
283286

284287
/* Clean up. */

0 commit comments

Comments
 (0)