Bitcoin Core 31.99.0
P2P Digital Currency
key_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <key.h>
6
7#include <common/system.h>
8#include <key_io.h>
9#include <span.h>
10#include <streams.h>
11#include <secp256k1_extrakeys.h>
12#include <test/util/common.h>
13#include <test/util/random.h>
15#include <uint256.h>
16#include <util/strencodings.h>
17#include <util/string.h>
18
19#include <string>
20#include <vector>
21
22#include <boost/test/unit_test.hpp>
23
24using namespace util::hex_literals;
25using util::ToString;
26
27static const std::string strSecret1 = "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj";
28static const std::string strSecret2 = "5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3";
29static const std::string strSecret1C = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
30static const std::string strSecret2C = "L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g";
31static const std::string addr1 = "1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ";
32static const std::string addr2 = "1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ";
33static const std::string addr1C = "1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs";
34static const std::string addr2C = "1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs";
35
36static const std::string strAddressBad = "1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF";
37
38
40
42{
44 BOOST_CHECK(key1.IsValid() && !key1.IsCompressed());
46 BOOST_CHECK(key2.IsValid() && !key2.IsCompressed());
48 BOOST_CHECK(key1C.IsValid() && key1C.IsCompressed());
50 BOOST_CHECK(key2C.IsValid() && key2C.IsCompressed());
52 BOOST_CHECK(!bad_key.IsValid());
53
54 CPubKey pubkey1 = key1. GetPubKey();
55 CPubKey pubkey2 = key2. GetPubKey();
56 CPubKey pubkey1C = key1C.GetPubKey();
57 CPubKey pubkey2C = key2C.GetPubKey();
58
59 BOOST_CHECK(key1.VerifyPubKey(pubkey1));
60 BOOST_CHECK(!key1.VerifyPubKey(pubkey1C));
61 BOOST_CHECK(!key1.VerifyPubKey(pubkey2));
62 BOOST_CHECK(!key1.VerifyPubKey(pubkey2C));
63
64 BOOST_CHECK(!key1C.VerifyPubKey(pubkey1));
65 BOOST_CHECK(key1C.VerifyPubKey(pubkey1C));
66 BOOST_CHECK(!key1C.VerifyPubKey(pubkey2));
67 BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C));
68
69 BOOST_CHECK(!key2.VerifyPubKey(pubkey1));
70 BOOST_CHECK(!key2.VerifyPubKey(pubkey1C));
71 BOOST_CHECK(key2.VerifyPubKey(pubkey2));
72 BOOST_CHECK(!key2.VerifyPubKey(pubkey2C));
73
74 BOOST_CHECK(!key2C.VerifyPubKey(pubkey1));
75 BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C));
76 BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
77 BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));
78
83
84 for (int n=0; n<16; n++)
85 {
86 std::string strMsg = strprintf("Very secret message %i: 11", n);
87 uint256 hashMsg = Hash(strMsg);
88
89 // normal signatures
90
91 std::vector<unsigned char> sign1, sign2, sign1C, sign2C;
92
93 BOOST_CHECK(key1.Sign (hashMsg, sign1));
94 BOOST_CHECK(key2.Sign (hashMsg, sign2));
95 BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
96 BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
97
98 BOOST_CHECK( pubkey1.Verify(hashMsg, sign1));
99 BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2));
100 BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C));
101 BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C));
102
103 BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1));
104 BOOST_CHECK( pubkey2.Verify(hashMsg, sign2));
105 BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C));
106 BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C));
107
108 BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1));
109 BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2));
110 BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1C));
111 BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2C));
112
113 BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1));
114 BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2));
115 BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C));
116 BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C));
117
118 // compact signatures (with key recovery)
119
120 std::vector<unsigned char> csign1, csign2, csign1C, csign2C;
121
122 BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
123 BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
124 BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
125 BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
126
127 CPubKey rkey1, rkey2, rkey1C, rkey2C;
128
129 BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1));
130 BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2));
131 BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C));
132 BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C));
133
134 BOOST_CHECK(rkey1 == pubkey1);
135 BOOST_CHECK(rkey2 == pubkey2);
136 BOOST_CHECK(rkey1C == pubkey1C);
137 BOOST_CHECK(rkey2C == pubkey2C);
138 }
139
140 // test deterministic signing
141
142 std::vector<unsigned char> detsig, detsigc;
143 std::string strMsg = "Very deterministic message";
144 uint256 hashMsg = Hash(strMsg);
145 BOOST_CHECK(key1.Sign(hashMsg, detsig));
146 BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
147 BOOST_CHECK(detsig == detsigc);
148 BOOST_CHECK_EQUAL(HexStr(detsig), "304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
149
150 BOOST_CHECK(key2.Sign(hashMsg, detsig));
151 BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
152 BOOST_CHECK(detsig == detsigc);
153 BOOST_CHECK_EQUAL(HexStr(detsig), "3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
154
155 BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
156 BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
157 BOOST_CHECK_EQUAL(HexStr(detsig), "1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
158 BOOST_CHECK_EQUAL(HexStr(detsigc), "205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
159
160 BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
161 BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
162 BOOST_CHECK_EQUAL(HexStr(detsig), "1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
163 BOOST_CHECK_EQUAL(HexStr(detsigc), "2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
164}
165
166BOOST_AUTO_TEST_CASE(key_signature_tests)
167{
168 // When entropy is specified, we should see at least one high R signature within 20 signatures
170 std::string msg = "A message to be signed";
171 uint256 msg_hash = Hash(msg);
172 std::vector<unsigned char> sig;
173 bool found = false;
174
175 for (int i = 1; i <=20; ++i) {
176 sig.clear();
177 BOOST_CHECK(key.Sign(msg_hash, sig, false, i));
178 found = sig[3] == 0x21 && sig[4] == 0x00;
179 if (found) {
180 break;
181 }
182 }
183 BOOST_CHECK(found);
184
185 // When entropy is not specified, we should always see low R signatures that are less than or equal to 70 bytes in 256 tries
186 // The low R signatures should always have the value of their "length of R" byte less than or equal to 32
187 // We should see at least one signature that is less than 70 bytes.
188 bool found_small = false;
189 bool found_big = false;
190 bool bad_sign = false;
191 for (int i = 0; i < 256; ++i) {
192 sig.clear();
193 std::string msg = "A message to be signed" + ToString(i);
194 msg_hash = Hash(msg);
195 if (!key.Sign(msg_hash, sig)) {
196 bad_sign = true;
197 break;
198 }
199 // sig.size() > 70 implies sig[3] > 32, because S is always low.
200 // But check both conditions anyway, just in case this implication is broken for some reason
201 if (sig[3] > 32 || sig.size() > 70) {
202 found_big = true;
203 break;
204 }
205 found_small |= sig.size() < 70;
206 }
207 BOOST_CHECK(!bad_sign);
208 BOOST_CHECK(!found_big);
209 BOOST_CHECK(found_small);
210}
211
212static CPubKey UnserializePubkey(const std::vector<uint8_t>& data)
213{
214 DataStream stream{};
215 stream << data;
216 CPubKey pubkey;
217 stream >> pubkey;
218 return pubkey;
219}
220
221static unsigned int GetLen(unsigned char chHeader)
222{
223 if (chHeader == 2 || chHeader == 3)
225 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
226 return CPubKey::SIZE;
227 return 0;
228}
229
230static void CmpSerializationPubkey(const CPubKey& pubkey)
231{
232 DataStream stream{};
233 stream << pubkey;
234 CPubKey pubkey2;
235 stream >> pubkey2;
236 BOOST_CHECK(pubkey == pubkey2);
237}
238
239BOOST_AUTO_TEST_CASE(pubkey_unserialize)
240{
241 for (uint8_t i = 2; i <= 7; ++i) {
242 CPubKey key = UnserializePubkey({0x02});
243 BOOST_CHECK(!key.IsValid());
245 key = UnserializePubkey(std::vector<uint8_t>(GetLen(i), i));
247 if (i == 5) {
248 BOOST_CHECK(!key.IsValid());
249 } else {
250 BOOST_CHECK(key.IsValid());
251 }
252 }
253}
254
255BOOST_AUTO_TEST_CASE(bip340_test_vectors)
256{
257 static const std::vector<std::pair<std::array<std::string, 3>, bool>> VECTORS = {
258 {{"F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", "0000000000000000000000000000000000000000000000000000000000000000", "E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0"}, true},
259 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A"}, true},
260 {{"DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8", "7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C", "5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7"}, true},
261 {{"25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3"}, true},
262 {{"D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9", "4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703", "00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6376AFB1548AF603B3EB45C9F8207DEE1060CB71C04E80F593060B07D28308D7F4"}, true},
263 {{"EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false},
264 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A14602975563CC27944640AC607CD107AE10923D9EF7A73C643E166BE5EBEAFA34B1AC553E2"}, false},
265 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "1FA62E331EDBC21C394792D2AB1100A7B432B013DF3F6FF4F99FCB33E0E1515F28890B3EDB6E7189B630448B515CE4F8622A954CFE545735AAEA5134FCCDB2BD"}, false},
266 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769961764B3AA9B2FFCB6EF947B6887A226E8D7C93E00C5ED0C1834FF0D0C2E6DA6"}, false},
267 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "0000000000000000000000000000000000000000000000000000000000000000123DDA8328AF9C23A94C1FEECFD123BA4FB73476F0D594DCB65C6425BD186051"}, false},
268 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "00000000000000000000000000000000000000000000000000000000000000017615FBAF5AE28864013C099742DEADB4DBA87F11AC6754F93780D5A1837CF197"}, false},
269 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false},
270 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false},
271 {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"}, false},
272 {{"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC30", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false}
273 };
274
275 for (const auto& test : VECTORS) {
276 auto pubkey = ParseHex(test.first[0]);
277 auto msg = ParseHex(test.first[1]);
278 auto sig = ParseHex(test.first[2]);
279 BOOST_CHECK_EQUAL(XOnlyPubKey(pubkey).VerifySchnorr(uint256(msg), sig), test.second);
280 }
281
282 static const std::vector<std::array<std::string, 5>> SIGN_VECTORS = {
283 {{"0000000000000000000000000000000000000000000000000000000000000003", "F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000", "E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0"}},
284 {{"B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF", "DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "0000000000000000000000000000000000000000000000000000000000000001", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A"}},
285 {{"C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9", "DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8", "C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906", "7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C", "5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7"}},
286 {{"0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710", "25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3"}},
287 };
288
289 for (const auto& [sec_hex, pub_hex, aux_hex, msg_hex, sig_hex] : SIGN_VECTORS) {
290 auto sec = ParseHex(sec_hex);
291 auto pub = ParseHex(pub_hex);
292 uint256 aux256(ParseHex(aux_hex));
293 uint256 msg256(ParseHex(msg_hex));
294 auto sig = ParseHex(sig_hex);
295 unsigned char sig64[64];
296
297 // Run the untweaked test vectors above, comparing with exact expected signature.
298 CKey key;
299 key.Set(sec.begin(), sec.end(), true);
300 XOnlyPubKey pubkey(key.GetPubKey());
301 BOOST_CHECK(std::equal(pubkey.begin(), pubkey.end(), pub.begin(), pub.end()));
302 bool ok = key.SignSchnorr(msg256, sig64, nullptr, aux256);
303 BOOST_CHECK(ok);
304 BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
305 // Verify those signatures for good measure.
306 BOOST_CHECK(pubkey.VerifySchnorr(msg256, sig64));
307
308 // Repeat the same check, but use the KeyPair directly without any merkle tweak
309 KeyPair keypair = key.ComputeKeyPair(/*merkle_root=*/nullptr);
310 bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256);
311 BOOST_CHECK(kp_ok);
312 BOOST_CHECK(pubkey.VerifySchnorr(msg256, sig64));
313 BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
314
315 // Do 10 iterations where we sign with a random Merkle root to tweak,
316 // and compare against the resulting tweaked keys, with random aux.
317 // In iteration i=0 we tweak with empty Merkle tree.
318 for (int i = 0; i < 10; ++i) {
319 uint256 merkle_root;
320 if (i) merkle_root = m_rng.rand256();
321 auto tweaked = pubkey.CreateTapTweak(i ? &merkle_root : nullptr);
322 BOOST_CHECK(tweaked);
323 XOnlyPubKey tweaked_key = tweaked->first;
324 aux256 = m_rng.rand256();
325 bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, aux256);
326 BOOST_CHECK(ok);
327 BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64));
328
329 // Repeat the same check, but use the KeyPair class directly
330 KeyPair keypair = key.ComputeKeyPair(&merkle_root);
331 bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256);
332 BOOST_CHECK(kp_ok);
333 BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64));
334 }
335 }
336}
337
339{
340 for (const auto& secret : {strSecret1, strSecret2, strSecret1C, strSecret2C}) {
341 CKey key = DecodeSecret(secret);
342 BOOST_CHECK(key.IsValid());
343
344 uint256 ent32 = m_rng.rand256();
345 auto ellswift = key.EllSwiftCreate(std::as_bytes(std::span{ent32}));
346
347 CPubKey decoded_pubkey = ellswift.Decode();
348 if (!key.IsCompressed()) {
349 // The decoding constructor returns a compressed pubkey. If the
350 // original was uncompressed, we must decompress the decoded one
351 // to compare.
352 decoded_pubkey.Decompress();
353 }
354 BOOST_CHECK(key.GetPubKey() == decoded_pubkey);
355 }
356}
357
359{
360 constexpr auto G_uncompressed{"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"_hex};
361 HashWriter hw;
362 hw.write(G_uncompressed);
363 XOnlyPubKey H{hw.GetSHA256()};
365}
366
367BOOST_AUTO_TEST_CASE(key_schnorr_tweak_smoke_test)
368{
369 // Sanity check to ensure we get the same tweak using CPubKey vs secp256k1 functions
371
372 CKey key;
373 key.MakeNewKey(true);
374 uint256 merkle_root = m_rng.rand256();
375
376 // secp256k1 functions
377 secp256k1_keypair keypair;
379 secp256k1_xonly_pubkey xonly_pubkey;
380 BOOST_CHECK(secp256k1_keypair_xonly_pub(secp256k1_context_static, &xonly_pubkey, nullptr, &keypair));
381 unsigned char xonly_bytes[32];
383 uint256 tweak_old = XOnlyPubKey(xonly_bytes).ComputeTapTweakHash(&merkle_root);
384
385 // CPubKey
386 CPubKey pubkey = key.GetPubKey();
387 uint256 tweak_new = XOnlyPubKey(pubkey).ComputeTapTweakHash(&merkle_root);
388
389 BOOST_CHECK_EQUAL(tweak_old, tweak_new);
390
392}
393
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
An encapsulated private key.
Definition: key.h:36
KeyPair ComputeKeyPair(const uint256 *merkle_root) const
Compute a KeyPair.
Definition: key.cpp:348
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 *merkle_root, const uint256 &aux) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
Definition: key.cpp:273
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:124
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:209
const std::byte * begin() const
Definition: key.h:120
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:127
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:162
EllSwiftPubKey EllSwiftCreate(std::span< const std::byte > entropy) const
Create an ellswift-encoded public key for this key, with specified entropy.
Definition: key.cpp:312
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:183
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:104
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:237
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
Definition: key.cpp:250
An encapsulated public key.
Definition: pubkey.h:34
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:300
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:40
bool IsValid() const
Definition: pubkey.h:185
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:327
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:283
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:39
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:133
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:126
void write(std::span< const std::byte > src)
Definition: hash.h:106
KeyPair.
Definition: key.h:272
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 &aux) const
Definition: key.cpp:549
const unsigned char * end() const
Definition: pubkey.h:296
const unsigned char * begin() const
Definition: pubkey.h:295
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
Definition: pubkey.cpp:265
static const XOnlyPubKey NUMS_H
Nothing Up My Sleeve point H Used as an internal key for provably disabling the key path spend see BI...
Definition: pubkey.h:235
bool VerifySchnorr(const uint256 &msg, std::span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
Definition: pubkey.cpp:236
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
Definition: pubkey.cpp:246
256-bit opaque blob.
Definition: uint256.h:195
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
static secp256k1_context * secp256k1_context_sign
Definition: key.cpp:20
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:300
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:214
static const std::string strSecret2
Definition: key_tests.cpp:28
static unsigned int GetLen(unsigned char chHeader)
Definition: key_tests.cpp:221
static const std::string addr1
Definition: key_tests.cpp:31
static const std::string strSecret2C
Definition: key_tests.cpp:30
static void CmpSerializationPubkey(const CPubKey &pubkey)
Definition: key_tests.cpp:230
static const std::string addr2C
Definition: key_tests.cpp:34
static CPubKey UnserializePubkey(const std::vector< uint8_t > &data)
Definition: key_tests.cpp:212
static const std::string addr1C
Definition: key_tests.cpp:33
static const std::string strSecret1
Definition: key_tests.cpp:27
static const std::string strSecret1C
Definition: key_tests.cpp:29
static const std::string addr2
Definition: key_tests.cpp:32
BOOST_AUTO_TEST_CASE(key_test1)
Definition: key_tests.cpp:41
static const std::string strAddressBad
Definition: key_tests.cpp:36
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
Definition: strencodings.h:400
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:246
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:187
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:141
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:214
SECP256K1_API const secp256k1_context *const secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition: secp256k1.h:245
SECP256K1_API int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an xonly_pubkey object into a 32-byte sequence.
Definition: main_impl.h:44
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a valid secret key.
Definition: main_impl.h:196
SECP256K1_API int secp256k1_keypair_xonly_pub(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Get the x-only public key from a keypair.
Definition: main_impl.h:234
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:221
unsigned char * UCharCast(char *c)
Definition: span.h:95
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:69
Basic testing setup.
Definition: setup_common.h:64
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid "x-only" public key.
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172