@@ -300,6 +300,48 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors)
300
300
auto sig = ParseHex (test.first [2 ]);
301
301
BOOST_CHECK_EQUAL (XOnlyPubKey (pubkey).VerifySchnorr (uint256 (msg), sig), test.second );
302
302
}
303
+
304
+ static const std::vector<std::array<std::string, 5 >> SIGN_VECTORS = {
305
+ {{" 0000000000000000000000000000000000000000000000000000000000000003" , " F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9" , " 0000000000000000000000000000000000000000000000000000000000000000" , " 0000000000000000000000000000000000000000000000000000000000000000" , " E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0" }},
306
+ {{" B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF" , " DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659" , " 0000000000000000000000000000000000000000000000000000000000000001" , " 243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89" , " 6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A" }},
307
+ {{" C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9" , " DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8" , " C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906" , " 7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C" , " 5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7" }},
308
+ {{" 0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710" , " 25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517" , " FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" , " FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" , " 7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3" }},
309
+ };
310
+
311
+ for (const auto & [sec_hex, pub_hex, aux_hex, msg_hex, sig_hex] : SIGN_VECTORS) {
312
+ auto sec = ParseHex (sec_hex);
313
+ auto pub = ParseHex (pub_hex);
314
+ uint256 aux256 (ParseHex (aux_hex));
315
+ uint256 msg256 (ParseHex (msg_hex));
316
+ auto sig = ParseHex (sig_hex);
317
+ unsigned char sig64[64 ];
318
+
319
+ // Run the untweaked test vectors above, comparing with exact expected signature.
320
+ CKey key;
321
+ key.Set (sec.begin (), sec.end (), true );
322
+ XOnlyPubKey pubkey (key.GetPubKey ());
323
+ BOOST_CHECK (std::equal (pubkey.begin (), pubkey.end (), pub.begin (), pub.end ()));
324
+ bool ok = key.SignSchnorr (msg256, sig64, nullptr , &aux256);
325
+ BOOST_CHECK (ok);
326
+ BOOST_CHECK (std::vector<unsigned char >(sig64, sig64 + 64 ) == sig);
327
+ // Verify those signatures for good measure.
328
+ BOOST_CHECK (pubkey.VerifySchnorr (msg256, sig64));
329
+
330
+ // Do 10 iterations where we sign with a random Merkle root to tweak,
331
+ // and compare against the resulting tweaked keys, with random aux.
332
+ // In iteration i=0 we tweak with empty Merkle tree.
333
+ for (int i = 0 ; i < 10 ; ++i) {
334
+ uint256 merkle_root;
335
+ if (i) merkle_root = InsecureRand256 ();
336
+ auto tweaked = pubkey.CreateTapTweak (i ? &merkle_root : nullptr );
337
+ BOOST_CHECK (tweaked);
338
+ XOnlyPubKey tweaked_key = tweaked->first ;
339
+ aux256 = InsecureRand256 ();
340
+ bool ok = key.SignSchnorr (msg256, sig64, &merkle_root, &aux256);
341
+ BOOST_CHECK (ok);
342
+ BOOST_CHECK (tweaked_key.VerifySchnorr (msg256, sig64));
343
+ }
344
+ }
303
345
}
304
346
305
347
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments