Skip to content

Commit

Permalink
tests: add test for KeyPair
Browse files Browse the repository at this point in the history
Reuse existing bip340 test vectors.
  • Loading branch information
josibake committed Jul 16, 2024
1 parent 9b78e25 commit f22894b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors)
// Verify those signatures for good measure.
BOOST_CHECK(pubkey.VerifySchnorr(msg256, sig64));

// Repeat the same check, but use the KeyPair directly without any merkle tweak
KeyPair keypair = key.ComputeKeyPair(/*merkle_root=*/nullptr);
CKey keypair_seckey;
BOOST_CHECK(keypair.GetKey(keypair_seckey));
BOOST_CHECK(key == keypair_seckey);
bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256);
BOOST_CHECK(kp_ok);
XOnlyPubKey keypair_xonly{keypair_seckey.GetPubKey()};
BOOST_CHECK(keypair_xonly.VerifySchnorr(msg256, sig64));

// Do 10 iterations where we sign with a random Merkle root to tweak,
// and compare against the resulting tweaked keys, with random aux.
// In iteration i=0 we tweak with empty Merkle tree.
Expand All @@ -312,6 +322,16 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors)
bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, aux256);
BOOST_CHECK(ok);
BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64));

// Repeat the same check, but use the KeyPair class directly
KeyPair keypair = key.ComputeKeyPair(&merkle_root);
CKey keypair_seckey;
BOOST_CHECK(keypair.GetKey(keypair_seckey));
XOnlyPubKey keypair_xonly{keypair_seckey.GetPubKey()};
BOOST_CHECK(tweaked_key == keypair_xonly);
bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256);
BOOST_CHECK(kp_ok);
BOOST_CHECK(keypair_xonly.VerifySchnorr(msg256, sig64));
}
}
}
Expand Down

0 comments on commit f22894b

Please sign in to comment.