@@ -341,21 +341,21 @@ static void secp256k1_nonce_function_musig_sha256_tagged(secp256k1_sha256 *sha)
341341 sha -> bytes = 64 ;
342342}
343343
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 ) {
345345 secp256k1_sha256 sha ;
346346 unsigned char rand [32 ];
347347 unsigned char i ;
348348 unsigned char msg_present ;
349349
350350 if (seckey32 != NULL ) {
351351 secp256k1_nonce_function_musig_sha256_tagged_aux (& sha );
352- secp256k1_sha256_write (& sha , session_id , 32 );
352+ secp256k1_sha256_write (& sha , session_secrand , 32 );
353353 secp256k1_sha256_finalize (& sha , rand );
354354 for (i = 0 ; i < 32 ; i ++ ) {
355355 rand [i ] ^= seckey32 [i ];
356356 }
357357 } else {
358- memcpy (rand , session_id , sizeof (rand ));
358+ memcpy (rand , session_secrand , sizeof (rand ));
359359 }
360360
361361 /* 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
379379 }
380380}
381381
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 ) {
383383 secp256k1_keyagg_cache_internal cache_i ;
384384 secp256k1_scalar k [2 ];
385385 secp256k1_ge nonce_pt [2 ];
@@ -392,24 +392,12 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
392392 int pk_serialize_success ;
393393 int ret = 1 ;
394394
395- VERIFY_CHECK (ctx != NULL );
396395 ARG_CHECK (secnonce != NULL );
397396 memset (secnonce , 0 , sizeof (* secnonce ));
398397 ARG_CHECK (pubnonce != NULL );
399398 memset (pubnonce , 0 , sizeof (* pubnonce ));
400- ARG_CHECK (session_id32 != NULL );
401399 ARG_CHECK (pubkey != NULL );
402400 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- }
413401
414402 /* Check that the seckey is valid to be able to sign for it later. */
415403 if (seckey != NULL ) {
@@ -439,7 +427,7 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
439427 (void ) pk_serialize_success ;
440428#endif
441429
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 );
443431 VERIFY_CHECK (!secp256k1_scalar_is_zero (& k [0 ]));
444432 VERIFY_CHECK (!secp256k1_scalar_is_zero (& k [1 ]));
445433 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
458446 return ret ;
459447}
460448
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+
461490static int secp256k1_musig_sum_nonces (const secp256k1_context * ctx , secp256k1_gej * summed_nonces , const secp256k1_musig_pubnonce * const * pubnonces , size_t n_pubnonces ) {
462491 size_t i ;
463492 int j ;
0 commit comments