@@ -341,21 +341,21 @@ static void secp256k1_nonce_function_musig_sha256_tagged(secp256k1_sha256 *sha)
341
341
sha -> bytes = 64 ;
342
342
}
343
343
344
- static void secp256k1_nonce_function_musig (secp256k1_scalar * k , const unsigned char * session_id , const unsigned char * msg32 , const unsigned char * seckey32 , const unsigned char * pk33 , const unsigned char * agg_pk32 , const unsigned char * extra_input32 ) {
344
+ static void secp256k1_nonce_function_musig (secp256k1_scalar * k , const unsigned char * session_secrand , const unsigned char * msg32 , const unsigned char * seckey32 , const unsigned char * pk33 , const unsigned char * agg_pk32 , const unsigned char * extra_input32 ) {
345
345
secp256k1_sha256 sha ;
346
346
unsigned char rand [32 ];
347
347
unsigned char i ;
348
348
unsigned char msg_present ;
349
349
350
350
if (seckey32 != NULL ) {
351
351
secp256k1_nonce_function_musig_sha256_tagged_aux (& sha );
352
- secp256k1_sha256_write (& sha , session_id , 32 );
352
+ secp256k1_sha256_write (& sha , session_secrand , 32 );
353
353
secp256k1_sha256_finalize (& sha , rand );
354
354
for (i = 0 ; i < 32 ; i ++ ) {
355
355
rand [i ] ^= seckey32 [i ];
356
356
}
357
357
} else {
358
- memcpy (rand , session_id , sizeof (rand ));
358
+ memcpy (rand , session_secrand , sizeof (rand ));
359
359
}
360
360
361
361
/* Subtract one from `sizeof` to avoid hashing the implicit null byte */
@@ -379,7 +379,7 @@ static void secp256k1_nonce_function_musig(secp256k1_scalar *k, const unsigned c
379
379
}
380
380
}
381
381
382
- int secp256k1_musig_nonce_gen (const secp256k1_context * ctx , secp256k1_musig_secnonce * secnonce , secp256k1_musig_pubnonce * pubnonce , const unsigned char * session_id32 , const unsigned char * seckey , const secp256k1_pubkey * pubkey , const unsigned char * msg32 , const secp256k1_musig_keyagg_cache * keyagg_cache , const unsigned char * extra_input32 ) {
382
+ int secp256k1_musig_nonce_gen_internal (const secp256k1_context * ctx , secp256k1_musig_secnonce * secnonce , secp256k1_musig_pubnonce * pubnonce , const unsigned char * input_nonce , const unsigned char * seckey , const secp256k1_pubkey * pubkey , const unsigned char * msg32 , const secp256k1_musig_keyagg_cache * keyagg_cache , const unsigned char * extra_input32 ) {
383
383
secp256k1_keyagg_cache_internal cache_i ;
384
384
secp256k1_scalar k [2 ];
385
385
secp256k1_ge nonce_pt [2 ];
@@ -392,24 +392,12 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
392
392
int pk_serialize_success ;
393
393
int ret = 1 ;
394
394
395
- VERIFY_CHECK (ctx != NULL );
396
395
ARG_CHECK (secnonce != NULL );
397
396
memset (secnonce , 0 , sizeof (* secnonce ));
398
397
ARG_CHECK (pubnonce != NULL );
399
398
memset (pubnonce , 0 , sizeof (* pubnonce ));
400
- ARG_CHECK (session_id32 != NULL );
401
399
ARG_CHECK (pubkey != NULL );
402
400
ARG_CHECK (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx ));
403
- if (seckey == NULL ) {
404
- /* Check in constant time that the session_id is not 0 as a
405
- * defense-in-depth measure that may protect against a faulty RNG. */
406
- unsigned char acc = 0 ;
407
- for (i = 0 ; i < 32 ; i ++ ) {
408
- acc |= session_id32 [i ];
409
- }
410
- ret &= !!acc ;
411
- memset (& acc , 0 , sizeof (acc ));
412
- }
413
401
414
402
/* Check that the seckey is valid to be able to sign for it later. */
415
403
if (seckey != NULL ) {
@@ -439,7 +427,7 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
439
427
(void ) pk_serialize_success ;
440
428
#endif
441
429
442
- secp256k1_nonce_function_musig (k , session_id32 , msg32 , seckey , pk_ser , aggpk_ser_ptr , extra_input32 );
430
+ secp256k1_nonce_function_musig (k , input_nonce , msg32 , seckey , pk_ser , aggpk_ser_ptr , extra_input32 );
443
431
VERIFY_CHECK (!secp256k1_scalar_is_zero (& k [0 ]));
444
432
VERIFY_CHECK (!secp256k1_scalar_is_zero (& k [1 ]));
445
433
VERIFY_CHECK (!secp256k1_scalar_eq (& k [0 ], & k [1 ]));
@@ -458,6 +446,47 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
458
446
return ret ;
459
447
}
460
448
449
+ int secp256k1_musig_nonce_gen (const secp256k1_context * ctx , secp256k1_musig_secnonce * secnonce , secp256k1_musig_pubnonce * pubnonce , const unsigned char * session_secrand32 , const unsigned char * seckey , const secp256k1_pubkey * pubkey , const unsigned char * msg32 , const secp256k1_musig_keyagg_cache * keyagg_cache , const unsigned char * extra_input32 ) {
450
+ int ret = 1 ;
451
+ unsigned char acc = 0 ;
452
+ int i ;
453
+
454
+ VERIFY_CHECK (ctx != NULL );
455
+ ARG_CHECK (session_secrand32 != NULL );
456
+
457
+ /* Check in constant time that the session_secrand32 is not 0 as a
458
+ * defense-in-depth measure that may protect against a faulty RNG. */
459
+ for (i = 0 ; i < 32 ; i ++ ) {
460
+ acc |= session_secrand32 [i ];
461
+ }
462
+ ret &= !!acc ;
463
+ memset (& acc , 0 , sizeof (acc ));
464
+
465
+ /* We can declassify because branching on ret is only relevant when this
466
+ * function called with an invalid session_secrand32 argument */
467
+ secp256k1_declassify (ctx , & ret , sizeof (ret ));
468
+ if (ret == 0 ) {
469
+ secp256k1_musig_secnonce_invalidate (ctx , secnonce , 1 );
470
+ return 0 ;
471
+ }
472
+
473
+ return secp256k1_musig_nonce_gen_internal (ctx , secnonce , pubnonce , session_secrand32 , seckey , pubkey , msg32 , keyagg_cache , extra_input32 );
474
+ }
475
+
476
+ int secp256k1_musig_nonce_gen_counter (const secp256k1_context * ctx , secp256k1_musig_secnonce * secnonce , secp256k1_musig_pubnonce * pubnonce , uint64_t nonrepeating_cnt , const unsigned char * seckey , const secp256k1_pubkey * pubkey , const unsigned char * msg32 , const secp256k1_musig_keyagg_cache * keyagg_cache , const unsigned char * extra_input32 ) {
477
+ unsigned char buf [32 ] = { 0 };
478
+ int i ;
479
+
480
+ VERIFY_CHECK (ctx != NULL );
481
+ ARG_CHECK ((seckey != NULL ));
482
+
483
+ for (i = 0 ; i < 8 ; ++ i ) {
484
+ buf [7 - i ] = (nonrepeating_cnt >> (i * 8 )) & 0xFF ;
485
+ }
486
+
487
+ return secp256k1_musig_nonce_gen_internal (ctx , secnonce , pubnonce , buf , seckey , pubkey , msg32 , keyagg_cache , extra_input32 );
488
+ }
489
+
461
490
static int secp256k1_musig_sum_nonces (const secp256k1_context * ctx , secp256k1_gej * summed_nonces , const secp256k1_musig_pubnonce * const * pubnonces , size_t n_pubnonces ) {
462
491
size_t i ;
463
492
int j ;
0 commit comments