@@ -91,19 +91,17 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
91
91
*
92
92
* Adding precomputation, our final equations become:
93
93
*
94
- * ctx->scalar_offset = -b
94
+ * ctx->scalar_offset = (2^COMB_BITS - 1)/2 - b (mod order)
95
95
* 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)
97
97
* R = comb(d, G/2) + ctx->ge_offset
98
98
*
99
99
* comb(d, G/2) function is then computed by summing + or - 2^(i-1)*G, for i=0..COMB_BITS-1,
100
100
* depending on the value of the bits d[i] of the binary representation of scalar d.
101
101
*/
102
102
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 );
107
105
108
106
/* In secp256k1_ecmult_gen_prec_table we have precomputed sums of the
109
107
* (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
245
243
/* Setup blinding values for secp256k1_ecmult_gen. */
246
244
static void secp256k1_ecmult_gen_blind (secp256k1_ecmult_gen_context * ctx , const unsigned char * seed32 ) {
247
245
secp256k1_scalar b ;
246
+ secp256k1_scalar diff ;
248
247
secp256k1_gej gb ;
249
248
unsigned char nonce32 [32 ];
250
249
secp256k1_rfc6979_hmac_sha256 rng ;
251
250
unsigned char keydata [64 ];
251
+
252
+ /* Compute the (2^COMB_BITS - 1)/2 term once. */
253
+ secp256k1_ecmult_gen_scalar_diff (& diff );
254
+
252
255
if (seed32 == NULL ) {
253
256
/* When seed is NULL, reset the final point and blinding value. */
254
257
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 ) ;
256
259
return ;
257
260
}
258
261
/* 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
268
271
269
272
/* TODO: reintroduce projective blinding. */
270
273
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 */
272
275
secp256k1_rfc6979_hmac_sha256_generate (& rng , nonce32 , 32 );
273
276
secp256k1_scalar_set_b32 (& b , nonce32 , NULL );
274
277
/* 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
278
281
memset (nonce32 , 0 , 32 );
279
282
secp256k1_ecmult_gen (ctx , & gb , & b );
280
283
secp256k1_scalar_negate (& b , & b );
281
- ctx -> scalar_offset = b ;
284
+ secp256k1_scalar_add ( & ctx -> scalar_offset , & b , & diff ) ;
282
285
secp256k1_ge_set_gej (& ctx -> ge_offset , & gb );
283
286
284
287
/* Clean up. */
0 commit comments