Skip to content

Commit 5d507a0

Browse files
josibakel0rinc
andcommitted
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 <[email protected]>
1 parent f14900b commit 5d507a0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/test/key_tests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <key_io.h>
99
#include <span.h>
1010
#include <streams.h>
11+
#include <secp256k1_extrakeys.h>
1112
#include <test/util/random.h>
1213
#include <test/util/setup_common.h>
1314
#include <uint256.h>
@@ -345,4 +346,31 @@ BOOST_AUTO_TEST_CASE(bip341_test_h)
345346
BOOST_CHECK(XOnlyPubKey::NUMS_H == H);
346347
}
347348

349+
BOOST_AUTO_TEST_CASE(key_schnorr_tweak_smoke_test)
350+
{
351+
// Sanity check to ensure we get the same tweak using CPubKey vs secp256k1 functions
352+
secp256k1_context* secp256k1_context_sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
353+
354+
CKey key;
355+
key.MakeNewKey(true);
356+
uint256 merkle_root = InsecureRand256();
357+
358+
// secp256k1 functions
359+
secp256k1_keypair keypair;
360+
BOOST_CHECK(secp256k1_keypair_create(secp256k1_context_sign, &keypair, UCharCast(key.begin())));
361+
secp256k1_xonly_pubkey xonly_pubkey;
362+
BOOST_CHECK(secp256k1_keypair_xonly_pub(secp256k1_context_sign, &xonly_pubkey, nullptr, &keypair));
363+
unsigned char xonly_bytes[32];
364+
BOOST_CHECK(secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, xonly_bytes, &xonly_pubkey));
365+
uint256 tweak_old = XOnlyPubKey(xonly_bytes).ComputeTapTweakHash(&merkle_root);
366+
367+
// CPubKey
368+
CPubKey pubkey = key.GetPubKey();
369+
uint256 tweak_new = XOnlyPubKey(pubkey).ComputeTapTweakHash(&merkle_root);
370+
371+
BOOST_CHECK_EQUAL(tweak_old, tweak_new);
372+
373+
secp256k1_context_destroy(secp256k1_context_sign);
374+
}
375+
348376
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)