@@ -68,15 +68,13 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
68
68
* (gn - b + (2^COMB_BITS - 1)/2). In other words, the bits of that scalar select
69
69
* whether to add normal or negated versions of 2^(i-1)*G together.
70
70
*
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).
74
74
*/
75
75
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 );
80
78
81
79
/* In secp256k1_ecmult_gen_prec_table we have precomputed sums of the
82
80
* (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
200
198
/* Setup blinding values for secp256k1_ecmult_gen. */
201
199
static void secp256k1_ecmult_gen_blind (secp256k1_ecmult_gen_context * ctx , const unsigned char * seed32 ) {
202
200
secp256k1_scalar b ;
201
+ secp256k1_scalar diff ;
203
202
secp256k1_gej gb ;
204
203
unsigned char nonce32 [32 ];
205
204
secp256k1_rfc6979_hmac_sha256 rng ;
206
205
unsigned char keydata [64 ] = {0 };
206
+
207
+ /* Compute the (2^COMB_BITS - 1)/2 term once. */
208
+ secp256k1_ecmult_gen_scalar_diff (& diff );
209
+
207
210
if (seed32 == NULL ) {
208
211
/* When seed is NULL, reset the final point and blinding value. */
209
212
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 ) ;
211
214
}
212
215
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
213
216
secp256k1_scalar_get_b32 (nonce32 , & ctx -> scalar_offset );
@@ -224,7 +227,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
224
227
225
228
/* TODO: reintroduce projective blinding. */
226
229
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 */
228
231
secp256k1_rfc6979_hmac_sha256_generate (& rng , nonce32 , 32 );
229
232
secp256k1_scalar_set_b32 (& b , nonce32 , NULL );
230
233
/* 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
234
237
memset (nonce32 , 0 , 32 );
235
238
secp256k1_ecmult_gen (ctx , & gb , & b );
236
239
secp256k1_scalar_negate (& b , & b );
237
- ctx -> scalar_offset = b ;
240
+ secp256k1_scalar_add ( & ctx -> scalar_offset , & b , & diff ) ;
238
241
secp256k1_ge_set_gej (& ctx -> final_point_add , & gb );
239
242
240
243
/* Clean up. */
0 commit comments