Skip to content

Commit 2a0d934

Browse files
committed
fixup! use optimized tagged hashes
1 parent dd4932b commit 2a0d934

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

src/modules/musig/session_impl.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
313344
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) {
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) */
469515
static 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));

src/modules/musig/tests_impl.h

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../../util.h"
2424

2525
#include "vectors.h"
26+
#include <inttypes.h>
2627

2728
static int create_keypair_and_pk(secp256k1_keypair *keypair, secp256k1_pubkey *pk, const unsigned char *sk) {
2829
int ret;
@@ -490,45 +491,38 @@ static void musig_nonce_test(void) {
490491

491492
static void sha256_tag_test_internal(secp256k1_sha256 *sha_tagged, unsigned char *tag, size_t taglen) {
492493
secp256k1_sha256 sha;
493-
unsigned char buf[32];
494-
unsigned char buf2[32];
495-
size_t i;
496-
497-
secp256k1_sha256_initialize(&sha);
498-
secp256k1_sha256_write(&sha, tag, taglen);
499-
secp256k1_sha256_finalize(&sha, buf);
500-
/* buf = SHA256(tag) */
501-
502-
secp256k1_sha256_initialize(&sha);
503-
secp256k1_sha256_write(&sha, buf, 32);
504-
secp256k1_sha256_write(&sha, buf, 32);
505-
/* Is buffer fully consumed? */
506-
CHECK((sha.bytes & 0x3F) == 0);
507-
508-
/* Compare with tagged SHA */
509-
for (i = 0; i < 8; i++) {
510-
CHECK(sha_tagged->s[i] == sha.s[i]);
511-
}
512-
secp256k1_sha256_write(&sha, buf, 32);
513-
secp256k1_sha256_write(sha_tagged, buf, 32);
514-
secp256k1_sha256_finalize(&sha, buf);
515-
secp256k1_sha256_finalize(sha_tagged, buf2);
516-
CHECK(secp256k1_memcmp_var(buf, buf2, 32) == 0);
494+
secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
495+
test_sha256_eq(&sha, sha_tagged);
517496
}
518497

519498
/* Checks that the initialized tagged hashes initialized have the expected
520499
* state. */
521500
static void sha256_tag_test(void) {
522-
secp256k1_sha256 sha_tagged;
501+
secp256k1_sha256 sha;
523502
{
524503
char tag[11] = "KeyAgg list";
525-
secp256k1_musig_keyagglist_sha256(&sha_tagged);
526-
sha256_tag_test_internal(&sha_tagged, (unsigned char*)tag, sizeof(tag));
504+
secp256k1_musig_keyagglist_sha256(&sha);
505+
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag));
527506
}
528507
{
529508
char tag[18] = "KeyAgg coefficient";
530-
secp256k1_musig_keyaggcoef_sha256(&sha_tagged);
531-
sha256_tag_test_internal(&sha_tagged, (unsigned char*)tag, sizeof(tag));
509+
secp256k1_musig_keyaggcoef_sha256(&sha);
510+
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag));
511+
}
512+
{
513+
unsigned char tag[9] = "MuSig/aux";
514+
secp256k1_nonce_function_musig_sha256_tagged_aux(&sha);
515+
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag));
516+
}
517+
{
518+
unsigned char tag[11] = "MuSig/nonce";
519+
secp256k1_nonce_function_musig_sha256_tagged(&sha);
520+
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag));
521+
}
522+
{
523+
unsigned char tag[15] = "MuSig/noncecoef";
524+
secp256k1_musig_compute_noncehash_sha256_tagged(&sha);
525+
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag));
532526
}
533527
}
534528

0 commit comments

Comments
 (0)