Skip to content

Commit c5e9fa2

Browse files
committed
f use tagged hash for musig coefficients
1 parent ee26511 commit c5e9fa2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/modules/musig/main_impl.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,28 @@ static int secp256k1_musig_compute_ell(const secp256k1_context *ctx, unsigned ch
2929
return 1;
3030
}
3131

32+
/* Initializes SHA256 with fixed midstate. This midstate was computed by applying
33+
* SHA256 to SHA256("MuSig coefficient")||SHA256("MuSig coefficient"). */
34+
static void secp256k1_musig_sha256_init_tagged(secp256k1_sha256 *sha) {
35+
secp256k1_sha256_initialize(sha);
36+
37+
sha->s[0] = 0x0fd0690cul;
38+
sha->s[1] = 0xfefeae97ul;
39+
sha->s[2] = 0x996eac7ful;
40+
sha->s[3] = 0x5c30d864ul;
41+
sha->s[4] = 0x8c4a0573ul;
42+
sha->s[5] = 0xaca1a22ful;
43+
sha->s[6] = 0x6f43b801ul;
44+
sha->s[7] = 0x85ce27cdul;
45+
}
46+
3247
/* Compute r = SHA256(ell, idx). The four bytes of idx are serialized least significant byte first. */
3348
static void secp256k1_musig_coefficient(secp256k1_scalar *r, const unsigned char *ell, uint32_t idx) {
3449
secp256k1_sha256 sha;
3550
unsigned char buf[32];
3651
size_t i;
3752

38-
secp256k1_sha256_initialize(&sha);
53+
secp256k1_musig_sha256_init_tagged(&sha);
3954
secp256k1_sha256_write(&sha, ell, 32);
4055
/* We're hashing the index of the signer instead of its public key as specified
4156
* in the MuSig paper. This reduces the total amount of data that needs to be

src/modules/musig/tests_impl.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,33 @@ void scriptless_atomic_swap(secp256k1_scratch_space *scratch) {
706706
CHECK(secp256k1_schnorrsig_verify(ctx, &final_sig_a, msg32_a, &combined_pk_a) == 1);
707707
}
708708

709+
/* Checks that hash initialized by secp256k1_musig_sha256_init_tagged has the
710+
* expected state. */
711+
void sha256_tag_test(void) {
712+
char tag[17] = "MuSig coefficient";
713+
secp256k1_sha256 sha;
714+
secp256k1_sha256 sha_tagged;
715+
unsigned char buf[32];
716+
size_t i;
717+
718+
secp256k1_sha256_initialize(&sha);
719+
secp256k1_sha256_write(&sha, (unsigned char *) tag, 17);
720+
secp256k1_sha256_finalize(&sha, buf);
721+
/* buf = SHA256("MuSig coefficient") */
722+
723+
secp256k1_sha256_initialize(&sha);
724+
secp256k1_sha256_write(&sha, buf, 32);
725+
secp256k1_sha256_write(&sha, buf, 32);
726+
/* Is buffer fully consumed? */
727+
CHECK((sha.bytes & 0x3F) == 0);
728+
729+
/* Compare with tagged SHA */
730+
secp256k1_musig_sha256_init_tagged(&sha_tagged);
731+
for (i = 0; i < 8; i++) {
732+
CHECK(sha_tagged.s[i] == sha.s[i]);
733+
}
734+
}
735+
709736
void run_musig_tests(void) {
710737
int i;
711738
secp256k1_scratch_space *scratch = secp256k1_scratch_space_create(ctx, 1024 * 1024);
@@ -716,6 +743,7 @@ void run_musig_tests(void) {
716743
/* Run multiple times to ensure that the nonce is negated in some tests */
717744
scriptless_atomic_swap(scratch);
718745
}
746+
sha256_tag_test();
719747

720748
secp256k1_scratch_space_destroy(scratch);
721749
}

0 commit comments

Comments
 (0)