diff options
author | josibake <josibake@protonmail.com> | 2024-07-21 18:43:00 +0200 |
---|---|---|
committer | josibake <josibake@protonmail.com> | 2024-08-03 15:16:03 +0200 |
commit | 5d507a0091da1b6c013b00b6c76e19dd4d3b93a7 (patch) | |
tree | d97fdb1a09be82111c1fef4562268de11551f272 /src | |
parent | f14900b6e4eac26ae5f1c0badfa176d895851c97 (diff) |
tests: add key tweak smoke test
Sanity check that using CKey/CPubKey directly vs using secp256k1_keypair objects
returns the same results for BIP341 key tweaking.
Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/test/key_tests.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp index b897a0a153..6714d8445b 100644 --- a/src/test/key_tests.cpp +++ b/src/test/key_tests.cpp @@ -8,6 +8,7 @@ #include <key_io.h> #include <span.h> #include <streams.h> +#include <secp256k1_extrakeys.h> #include <test/util/random.h> #include <test/util/setup_common.h> #include <uint256.h> @@ -345,4 +346,31 @@ BOOST_AUTO_TEST_CASE(bip341_test_h) BOOST_CHECK(XOnlyPubKey::NUMS_H == H); } +BOOST_AUTO_TEST_CASE(key_schnorr_tweak_smoke_test) +{ + // Sanity check to ensure we get the same tweak using CPubKey vs secp256k1 functions + secp256k1_context* secp256k1_context_sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); + + CKey key; + key.MakeNewKey(true); + uint256 merkle_root = InsecureRand256(); + + // secp256k1 functions + secp256k1_keypair keypair; + BOOST_CHECK(secp256k1_keypair_create(secp256k1_context_sign, &keypair, UCharCast(key.begin()))); + secp256k1_xonly_pubkey xonly_pubkey; + BOOST_CHECK(secp256k1_keypair_xonly_pub(secp256k1_context_sign, &xonly_pubkey, nullptr, &keypair)); + unsigned char xonly_bytes[32]; + BOOST_CHECK(secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, xonly_bytes, &xonly_pubkey)); + uint256 tweak_old = XOnlyPubKey(xonly_bytes).ComputeTapTweakHash(&merkle_root); + + // CPubKey + CPubKey pubkey = key.GetPubKey(); + uint256 tweak_new = XOnlyPubKey(pubkey).ComputeTapTweakHash(&merkle_root); + + BOOST_CHECK_EQUAL(tweak_old, tweak_new); + + secp256k1_context_destroy(secp256k1_context_sign); +} + BOOST_AUTO_TEST_SUITE_END() |