Skip to content

Commit a91d532

Browse files
committed
Add CKey::SignSchnorr function for BIP 340/341 signing
1 parent e77a283 commit a91d532

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

src/key.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
#include <crypto/common.h>
99
#include <crypto/hmac_sha512.h>
10+
#include <hash.h>
1011
#include <random.h>
1112

1213
#include <secp256k1.h>
14+
#include <secp256k1_extrakeys.h>
1315
#include <secp256k1_recovery.h>
16+
#include <secp256k1_schnorrsig.h>
1417

1518
static secp256k1_context* secp256k1_context_sign = nullptr;
1619

@@ -258,6 +261,24 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
258261
return true;
259262
}
260263

264+
bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256* aux) const
265+
{
266+
assert(sig.size() == 64);
267+
secp256k1_keypair keypair;
268+
if (!secp256k1_keypair_create(secp256k1_context_sign, &keypair, begin())) return false;
269+
if (merkle_root) {
270+
secp256k1_xonly_pubkey pubkey;
271+
if (!secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, &keypair)) return false;
272+
unsigned char pubkey_bytes[32];
273+
if (!secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, pubkey_bytes, &pubkey)) return false;
274+
uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root);
275+
if (!secp256k1_keypair_xonly_tweak_add(GetVerifyContext(), &keypair, tweak.data())) return false;
276+
}
277+
bool ret = secp256k1_schnorrsig_sign(secp256k1_context_sign, sig.data(), hash.data(), &keypair, secp256k1_nonce_function_bip340, aux ? (void*)aux->data() : nullptr);
278+
memory_cleanse(&keypair, sizeof(keypair));
279+
return ret;
280+
}
281+
261282
bool CKey::Load(const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck=false) {
262283
if (!ec_seckey_import_der(secp256k1_context_sign, (unsigned char*)begin(), seckey.data(), seckey.size()))
263284
return false;

src/key.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ class CKey
128128
*/
129129
bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
130130

131+
/**
132+
* Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this,
133+
* optionally tweaked by *merkle_root. Additional nonce entropy can be provided through
134+
* aux.
135+
*
136+
* When merkle_root is not nullptr, this results in a signature with a modified key as
137+
* specified in BIP341:
138+
* - If merkle_root->IsNull(): key + H_TapTweak(pubkey)*G
139+
* - Otherwise: key + H_TapTweak(pubkey || *merkle_root)
140+
*/
141+
bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root = nullptr, const uint256* aux = nullptr) const;
142+
131143
//! Derive BIP32 child key.
132144
bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
133145

src/pubkey.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,7 @@ ECCVerifyHandle::~ECCVerifyHandle()
373373
secp256k1_context_verify = nullptr;
374374
}
375375
}
376+
377+
const secp256k1_context* GetVerifyContext() {
378+
return secp256k1_context_verify;
379+
}

src/pubkey.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,10 @@ class ECCVerifyHandle
316316
~ECCVerifyHandle();
317317
};
318318

319+
typedef struct secp256k1_context_struct secp256k1_context;
320+
321+
/** Access to the internal secp256k1 context used for verification. Only intended to be used
322+
* by key.cpp. */
323+
const secp256k1_context* GetVerifyContext();
324+
319325
#endif // BITCOIN_PUBKEY_H

src/test/key_tests.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,48 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors)
300300
auto sig = ParseHex(test.first[2]);
301301
BOOST_CHECK_EQUAL(XOnlyPubKey(pubkey).VerifySchnorr(uint256(msg), sig), test.second);
302302
}
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+
}
303345
}
304346

305347
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)