@@ -310,14 +310,45 @@ static void secp256k1_nonce_function_musig_helper(secp256k1_sha256 *sha, unsigne
310310 }
311311}
312312
313+ /* Initializes SHA256 with fixed midstate. This midstate was computed by applying
314+ * SHA256 to SHA256("MuSig/aux")||SHA256("MuSig/aux"). */
315+ static void secp256k1_nonce_function_musig_sha256_tagged_aux (secp256k1_sha256 * sha ) {
316+ secp256k1_sha256_initialize (sha );
317+ sha -> s [0 ] = 0xa19e884bul ;
318+ sha -> s [1 ] = 0xf463fe7eul ;
319+ sha -> s [2 ] = 0x2f18f9a2ul ;
320+ sha -> s [3 ] = 0xbeb0f9fful ;
321+ sha -> s [4 ] = 0x0f37e8b0ul ;
322+ sha -> s [5 ] = 0x06ebd26ful ;
323+ sha -> s [6 ] = 0xe3b243d2ul ;
324+ sha -> s [7 ] = 0x522fb150ul ;
325+ sha -> bytes = 64 ;
326+
327+ }
328+
329+ /* Initializes SHA256 with fixed midstate. This midstate was computed by applying
330+ * SHA256 to SHA256("MuSig/nonce")||SHA256("MuSig/nonce"). */
331+ static void secp256k1_nonce_function_musig_sha256_tagged (secp256k1_sha256 * sha ) {
332+ secp256k1_sha256_initialize (sha );
333+ sha -> s [0 ] = 0x07101b64ul ;
334+ sha -> s [1 ] = 0x18003414ul ;
335+ sha -> s [2 ] = 0x0391bc43ul ;
336+ sha -> s [3 ] = 0x0e6258eeul ;
337+ sha -> s [4 ] = 0x29d26b72ul ;
338+ sha -> s [5 ] = 0x8343937eul ;
339+ sha -> s [6 ] = 0xb7a0a4fbul ;
340+ sha -> s [7 ] = 0xff568a30ul ;
341+ sha -> bytes = 64 ;
342+ }
343+
313344static 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 ) {
314345 secp256k1_sha256 sha ;
315346 unsigned char rand [32 ];
316347 unsigned char i ;
317348 unsigned char msg_present ;
318349
319350 if (seckey32 != NULL ) {
320- secp256k1_sha256_initialize_tagged (& sha , ( unsigned char * ) "MuSig/aux" , sizeof ( "MuSig/aux" ) - 1 );
351+ secp256k1_nonce_function_musig_sha256_tagged_aux (& sha );
321352 secp256k1_sha256_write (& sha , session_id , 32 );
322353 secp256k1_sha256_finalize (& sha , rand );
323354 for (i = 0 ; i < 32 ; i ++ ) {
@@ -328,7 +359,7 @@ static void secp256k1_nonce_function_musig(secp256k1_scalar *k, const unsigned c
328359 }
329360
330361 /* Subtract one from `sizeof` to avoid hashing the implicit null byte */
331- secp256k1_sha256_initialize_tagged (& sha , ( unsigned char * ) "MuSig/nonce" , sizeof ( "MuSig/nonce" ) - 1 );
362+ secp256k1_nonce_function_musig_sha256_tagged (& sha );
332363 secp256k1_sha256_write (& sha , rand , sizeof (rand ));
333364 secp256k1_nonce_function_musig_helper (& sha , 1 , pk33 , 33 );
334365 secp256k1_nonce_function_musig_helper (& sha , 1 , agg_pk32 , 32 );
@@ -465,13 +496,28 @@ int secp256k1_musig_nonce_agg(const secp256k1_context* ctx, secp256k1_musig_aggn
465496 return 1 ;
466497}
467498
499+ /* Initializes SHA256 with fixed midstate. This midstate was computed by applying
500+ * SHA256 to SHA256("MuSig/noncecoef")||SHA256("MuSig/noncecoef"). */
501+ static void secp256k1_musig_compute_noncehash_sha256_tagged (secp256k1_sha256 * sha ) {
502+ secp256k1_sha256_initialize (sha );
503+ sha -> s [0 ] = 0x2c7d5a45ul ;
504+ sha -> s [1 ] = 0x06bf7e53ul ;
505+ sha -> s [2 ] = 0x89be68a6ul ;
506+ sha -> s [3 ] = 0x971254c0ul ;
507+ sha -> s [4 ] = 0x60ac12d2ul ;
508+ sha -> s [5 ] = 0x72846dcdul ;
509+ sha -> s [6 ] = 0x6c81212ful ;
510+ sha -> s [7 ] = 0xde7a2500ul ;
511+ sha -> bytes = 64 ;
512+ }
513+
468514/* tagged_hash(aggnonce[0], aggnonce[1], agg_pk, msg) */
469515static int secp256k1_musig_compute_noncehash (unsigned char * noncehash , secp256k1_ge * aggnonce , const unsigned char * agg_pk32 , const unsigned char * msg ) {
470516 unsigned char buf [33 ];
471517 secp256k1_sha256 sha ;
472518 int i ;
473519
474- secp256k1_sha256_initialize_tagged (& sha , ( unsigned char * ) "MuSig/noncecoef" , sizeof ( "MuSig/noncecoef" ) - 1 );
520+ secp256k1_musig_compute_noncehash_sha256_tagged (& sha );
475521 for (i = 0 ; i < 2 ; i ++ ) {
476522 secp256k1_musig_ge_serialize_ext (buf , & aggnonce [i ]);
477523 secp256k1_sha256_write (& sha , buf , sizeof (buf ));
0 commit comments