Skip to content

Commit d45fbdc

Browse files
Merge #180: musig: add test vectors for applying multiple tweaks
510b61a musig: add test vectors for applying multiple tweaks (Jonas Nick) Pull request description: ACKs for top commit: real-or-random: utACK 510b61a robot-dreams: utACK 510b61a Tree-SHA512: 5fed7e01f23c0c7d1526bd9f89c5f385ad95ab1f0331df6e5bc7710e4d9f4f3860a5fd63adb7adda0a57e5fcf6204ccb941232ceb26eae44cb74f0916963d674
2 parents 9a814be + 510b61a commit d45fbdc

File tree

1 file changed

+88
-15
lines changed

1 file changed

+88
-15
lines changed

src/modules/musig/tests_impl.h

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ void musig_test_vectors_noncegen(void) {
11431143
}
11441144
}
11451145

1146-
void musig_test_vectors_sign_helper(secp256k1_musig_keyagg_cache *keyagg_cache, int *fin_nonce_parity, unsigned char *sig, const unsigned char *secnonce_bytes, const unsigned char *agg_pubnonce_ser, const unsigned char *sk, const unsigned char *msg, const unsigned char *tweak, int xonly_tweak, const secp256k1_pubkey *adaptor, const unsigned char **pk_ser, int signer_pos) {
1146+
void musig_test_vectors_sign_helper(secp256k1_musig_keyagg_cache *keyagg_cache, int *fin_nonce_parity, unsigned char *sig, const unsigned char *secnonce_bytes, const unsigned char *agg_pubnonce_ser, const unsigned char *sk, const unsigned char *msg, const unsigned char tweak[][32], const int *is_xonly_t, int n_tweaks, const secp256k1_pubkey *adaptor, const unsigned char **pk_ser, int signer_pos) {
11471147
secp256k1_keypair signer_keypair;
11481148
secp256k1_musig_secnonce secnonce;
11491149
secp256k1_xonly_pubkey pk[3];
@@ -1163,11 +1163,11 @@ void musig_test_vectors_sign_helper(secp256k1_musig_keyagg_cache *keyagg_cache,
11631163
pk_ptr[i] = &pk[i];
11641164
}
11651165
CHECK(secp256k1_musig_pubkey_agg(ctx, NULL, &agg_pk, keyagg_cache, pk_ptr, 3) == 1);
1166-
if (tweak != NULL) {
1167-
if (xonly_tweak) {
1168-
CHECK(secp256k1_musig_pubkey_xonly_tweak_add(ctx, NULL, keyagg_cache, tweak) == 1);
1166+
for (i = 0; i < n_tweaks; i++) {
1167+
if (is_xonly_t[i]) {
1168+
CHECK(secp256k1_musig_pubkey_xonly_tweak_add(ctx, NULL, keyagg_cache, tweak[i]) == 1);
11691169
} else {
1170-
CHECK(secp256k1_musig_pubkey_ec_tweak_add(ctx, NULL, keyagg_cache, tweak) == 1);
1170+
CHECK(secp256k1_musig_pubkey_ec_tweak_add(ctx, NULL, keyagg_cache, tweak[i]) == 1);
11711171
}
11721172
}
11731173
memcpy(&secnonce.data[0], secp256k1_musig_secnonce_magic, 4);
@@ -1247,7 +1247,7 @@ void musig_test_vectors_sign(void) {
12471247
0x20, 0xA1, 0x81, 0x85, 0x5F, 0xD8, 0xBD, 0xB7,
12481248
0xF1, 0x27, 0xBB, 0x12, 0x40, 0x3B, 0x4D, 0x3B,
12491249
};
1250-
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, 0, NULL, pk, 0);
1250+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, NULL, 0, NULL, pk, 0);
12511251
/* TODO: remove when test vectors are not expected to change anymore */
12521252
/* int k, l; */
12531253
/* printf("const unsigned char sig_expected[32] = {\n"); */
@@ -1276,7 +1276,7 @@ void musig_test_vectors_sign(void) {
12761276
0x81, 0x38, 0xDA, 0xEC, 0x5C, 0xB2, 0x0A, 0x35,
12771277
0x7C, 0xEC, 0xA7, 0xC8, 0x42, 0x42, 0x95, 0xEA,
12781278
};
1279-
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, 0, NULL, pk, 1);
1279+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, NULL, 0, NULL, pk, 1);
12801280
/* Check that the description of the test vector is correct */
12811281
CHECK(musig_test_pk_parity(&keyagg_cache) == 0);
12821282
CHECK(musig_test_is_second_pk(&keyagg_cache, sk));
@@ -1292,7 +1292,7 @@ void musig_test_vectors_sign(void) {
12921292
0xE6, 0xA7, 0xF7, 0xFB, 0xE1, 0x5C, 0xDC, 0xAF,
12931293
0xA4, 0xA3, 0xD1, 0xBC, 0xAA, 0xBC, 0x75, 0x17,
12941294
};
1295-
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, 0, NULL, pk, 2);
1295+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, NULL, 0, NULL, pk, 2);
12961296
/* Check that the description of the test vector is correct */
12971297
CHECK(musig_test_pk_parity(&keyagg_cache) == 1);
12981298
CHECK(fin_nonce_parity == 0);
@@ -1307,13 +1307,14 @@ void musig_test_vectors_sign(void) {
13071307
0x15, 0x97, 0xF9, 0x60, 0x3D, 0x3A, 0xB0, 0x5B,
13081308
0x49, 0x13, 0x64, 0x17, 0x75, 0xE1, 0x37, 0x5B,
13091309
};
1310-
const unsigned char tweak[32] = {
1310+
const unsigned char tweak[1][32] = {{
13111311
0xE8, 0xF7, 0x91, 0xFF, 0x92, 0x25, 0xA2, 0xAF,
13121312
0x01, 0x02, 0xAF, 0xFF, 0x4A, 0x9A, 0x72, 0x3D,
13131313
0x96, 0x12, 0xA6, 0x82, 0xA2, 0x5E, 0xBE, 0x79,
13141314
0x80, 0x2B, 0x26, 0x3C, 0xDF, 0xCD, 0x83, 0xBB,
1315-
};
1316-
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, tweak, 1, NULL, pk, 2);
1315+
}};
1316+
int is_xonly_t[1] = { 1 };
1317+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, tweak, is_xonly_t, 1, NULL, pk, 2);
13171318

13181319
CHECK(musig_test_pk_parity(&keyagg_cache) == 1);
13191320
CHECK(!musig_test_is_second_pk(&keyagg_cache, sk));
@@ -1328,19 +1329,91 @@ void musig_test_vectors_sign(void) {
13281329
0x19, 0x5C, 0x1D, 0x4B, 0x52, 0xE6, 0x3E, 0xCD,
13291330
0x7B, 0xC5, 0x99, 0x16, 0x44, 0xE4, 0x4D, 0xDD,
13301331
};
1331-
const unsigned char tweak[32] = {
1332+
const unsigned char tweak[1][32] = {{
13321333
0xE8, 0xF7, 0x91, 0xFF, 0x92, 0x25, 0xA2, 0xAF,
13331334
0x01, 0x02, 0xAF, 0xFF, 0x4A, 0x9A, 0x72, 0x3D,
13341335
0x96, 0x12, 0xA6, 0x82, 0xA2, 0x5E, 0xBE, 0x79,
13351336
0x80, 0x2B, 0x26, 0x3C, 0xDF, 0xCD, 0x83, 0xBB,
1336-
};
1337-
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, tweak, 0, NULL, pk, 2);
1337+
}};
1338+
int is_xonly_t[1] = { 0 };
1339+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, tweak, is_xonly_t, 1, NULL, pk, 2);
13381340

13391341
CHECK(musig_test_pk_parity(&keyagg_cache) == 1);
13401342
CHECK(!musig_test_is_second_pk(&keyagg_cache, sk));
13411343
CHECK(fin_nonce_parity == 0);
13421344
CHECK(memcmp(sig, sig_expected, 32) == 0);
13431345
}
1346+
{
1347+
/* This is a test that includes an ordinary and an x-only public key tweak. */
1348+
const unsigned char sig_expected[32] = {
1349+
0xC3, 0xA8, 0x29, 0xA8, 0x14, 0x80, 0xE3, 0x6E,
1350+
0xC3, 0xAB, 0x05, 0x29, 0x64, 0x50, 0x9A, 0x94,
1351+
0xEB, 0xF3, 0x42, 0x10, 0x40, 0x3D, 0x16, 0xB2,
1352+
0x26, 0xA6, 0xF1, 0x6E, 0xC8, 0x5B, 0x73, 0x57,
1353+
};
1354+
1355+
const unsigned char tweak[2][32] = {
1356+
{
1357+
0xE8, 0xF7, 0x91, 0xFF, 0x92, 0x25, 0xA2, 0xAF,
1358+
0x01, 0x02, 0xAF, 0xFF, 0x4A, 0x9A, 0x72, 0x3D,
1359+
0x96, 0x12, 0xA6, 0x82, 0xA2, 0x5E, 0xBE, 0x79,
1360+
0x80, 0x2B, 0x26, 0x3C, 0xDF, 0xCD, 0x83, 0xBB,
1361+
},
1362+
{
1363+
0xAE, 0x2E, 0xA7, 0x97, 0xCC, 0x0F, 0xE7, 0x2A,
1364+
0xC5, 0xB9, 0x7B, 0x97, 0xF3, 0xC6, 0x95, 0x7D,
1365+
0x7E, 0x41, 0x99, 0xA1, 0x67, 0xA5, 0x8E, 0xB0,
1366+
0x8B, 0xCA, 0xFF, 0xDA, 0x70, 0xAC, 0x04, 0x55,
1367+
},
1368+
};
1369+
int is_xonly_t[2] = { 0, 1 };
1370+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, tweak, is_xonly_t, 2, NULL, pk, 2);
1371+
CHECK(musig_test_pk_parity(&keyagg_cache) == 0);
1372+
CHECK(!musig_test_is_second_pk(&keyagg_cache, sk));
1373+
CHECK(fin_nonce_parity == 0);
1374+
CHECK(memcmp(sig, sig_expected, 32) == 0);
1375+
}
1376+
{
1377+
/* This is a test with four tweaks: x-only, ordinary, x-only, ordinary. */
1378+
const unsigned char sig_expected[32] = {
1379+
0x8C, 0x44, 0x73, 0xC6, 0xA3, 0x82, 0xBD, 0x3C,
1380+
0x4A, 0xD7, 0xBE, 0x59, 0x81, 0x8D, 0xA5, 0xED,
1381+
0x7C, 0xF8, 0xCE, 0xC4, 0xBC, 0x21, 0x99, 0x6C,
1382+
0xFD, 0xA0, 0x8B, 0xB4, 0x31, 0x6B, 0x8B, 0xC7,
1383+
};
1384+
const unsigned char tweak[4][32] = {
1385+
{
1386+
0xE8, 0xF7, 0x91, 0xFF, 0x92, 0x25, 0xA2, 0xAF,
1387+
0x01, 0x02, 0xAF, 0xFF, 0x4A, 0x9A, 0x72, 0x3D,
1388+
0x96, 0x12, 0xA6, 0x82, 0xA2, 0x5E, 0xBE, 0x79,
1389+
0x80, 0x2B, 0x26, 0x3C, 0xDF, 0xCD, 0x83, 0xBB,
1390+
},
1391+
{
1392+
0xAE, 0x2E, 0xA7, 0x97, 0xCC, 0x0F, 0xE7, 0x2A,
1393+
0xC5, 0xB9, 0x7B, 0x97, 0xF3, 0xC6, 0x95, 0x7D,
1394+
0x7E, 0x41, 0x99, 0xA1, 0x67, 0xA5, 0x8E, 0xB0,
1395+
0x8B, 0xCA, 0xFF, 0xDA, 0x70, 0xAC, 0x04, 0x55,
1396+
},
1397+
{
1398+
0xF5, 0x2E, 0xCB, 0xC5, 0x65, 0xB3, 0xD8, 0xBE,
1399+
0xA2, 0xDF, 0xD5, 0xB7, 0x5A, 0x4F, 0x45, 0x7E,
1400+
0x54, 0x36, 0x98, 0x09, 0x32, 0x2E, 0x41, 0x20,
1401+
0x83, 0x16, 0x26, 0xF2, 0x90, 0xFA, 0x87, 0xE0,
1402+
},
1403+
{
1404+
0x19, 0x69, 0xAD, 0x73, 0xCC, 0x17, 0x7F, 0xA0,
1405+
0xB4, 0xFC, 0xED, 0x6D, 0xF1, 0xF7, 0xBF, 0x99,
1406+
0x07, 0xE6, 0x65, 0xFD, 0xE9, 0xBA, 0x19, 0x6A,
1407+
0x74, 0xFE, 0xD0, 0xA3, 0xCF, 0x5A, 0xEF, 0x9D,
1408+
},
1409+
};
1410+
int is_xonly_t[4] = { 1, 0, 1, 0 };
1411+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, tweak, is_xonly_t, 4, NULL, pk, 2);
1412+
CHECK(musig_test_pk_parity(&keyagg_cache) == 0);
1413+
CHECK(!musig_test_is_second_pk(&keyagg_cache, sk));
1414+
CHECK(fin_nonce_parity == 1);
1415+
CHECK(memcmp(sig, sig_expected, 32) == 0);
1416+
}
13441417
{
13451418
/* This is a test that includes an adaptor. */
13461419
const unsigned char sig_expected[32] = {
@@ -1357,7 +1430,7 @@ void musig_test_vectors_sign(void) {
13571430
};
13581431
secp256k1_pubkey pub_adaptor;
13591432
CHECK(secp256k1_ec_pubkey_create(ctx, &pub_adaptor, sec_adaptor) == 1);
1360-
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, 0, &pub_adaptor, pk, 2);
1433+
musig_test_vectors_sign_helper(&keyagg_cache, &fin_nonce_parity, sig, secnonce, agg_pubnonce, sk, msg, NULL, NULL, 0, &pub_adaptor, pk, 2);
13611434

13621435
CHECK(musig_test_pk_parity(&keyagg_cache) == 1);
13631436
CHECK(!musig_test_is_second_pk(&keyagg_cache, sk));

0 commit comments

Comments
 (0)