Branch data Line data Source code
1 : : // Copyright (c) 2012-2022 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/random.h>
13 : : #include <test/util/setup_common.h>
14 : : #include <uint256.h>
15 : : #include <util/strencodings.h>
16 : : #include <util/string.h>
17 : :
18 : : #include <string>
19 : : #include <vector>
20 : :
21 : : #include <boost/test/unit_test.hpp>
22 : :
23 : : using namespace util::hex_literals;
24 : : using util::ToString;
25 : :
26 : : static const std::string strSecret1 = "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj";
27 : : static const std::string strSecret2 = "5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3";
28 : : static const std::string strSecret1C = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
29 : : static const std::string strSecret2C = "L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g";
30 : : static const std::string addr1 = "1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ";
31 : : static const std::string addr2 = "1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ";
32 : : static const std::string addr1C = "1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs";
33 : : static const std::string addr2C = "1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs";
34 : :
35 : : static const std::string strAddressBad = "1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF";
36 : :
37 : :
38 : : BOOST_FIXTURE_TEST_SUITE(key_tests, BasicTestingSetup)
39 : :
40 [ + - + - : 10 : BOOST_AUTO_TEST_CASE(key_test1)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
41 : : {
42 : 1 : CKey key1 = DecodeSecret(strSecret1);
43 [ + - + - : 2 : BOOST_CHECK(key1.IsValid() && !key1.IsCompressed());
- + + - +
- ]
44 [ + - ]: 1 : CKey key2 = DecodeSecret(strSecret2);
45 [ + - + - : 2 : BOOST_CHECK(key2.IsValid() && !key2.IsCompressed());
- + + - +
- ]
46 [ + - ]: 1 : CKey key1C = DecodeSecret(strSecret1C);
47 [ + - + - : 2 : BOOST_CHECK(key1C.IsValid() && key1C.IsCompressed());
- + + - +
- ]
48 [ + - ]: 1 : CKey key2C = DecodeSecret(strSecret2C);
49 [ + - + - : 2 : BOOST_CHECK(key2C.IsValid() && key2C.IsCompressed());
- + + - +
- ]
50 [ + - ]: 1 : CKey bad_key = DecodeSecret(strAddressBad);
51 [ + - + - : 2 : BOOST_CHECK(!bad_key.IsValid());
+ - ]
52 : :
53 [ + - ]: 1 : CPubKey pubkey1 = key1. GetPubKey();
54 [ + - ]: 1 : CPubKey pubkey2 = key2. GetPubKey();
55 [ + - ]: 1 : CPubKey pubkey1C = key1C.GetPubKey();
56 [ + - ]: 1 : CPubKey pubkey2C = key2C.GetPubKey();
57 : :
58 [ + - + - : 2 : BOOST_CHECK(key1.VerifyPubKey(pubkey1));
+ - + - ]
59 [ + - + - : 2 : BOOST_CHECK(!key1.VerifyPubKey(pubkey1C));
+ - + - ]
60 [ + - + - : 2 : BOOST_CHECK(!key1.VerifyPubKey(pubkey2));
+ - + - ]
61 [ + - + - : 2 : BOOST_CHECK(!key1.VerifyPubKey(pubkey2C));
+ - + - ]
62 : :
63 [ + - + - : 2 : BOOST_CHECK(!key1C.VerifyPubKey(pubkey1));
+ - + - ]
64 [ + - + - : 2 : BOOST_CHECK(key1C.VerifyPubKey(pubkey1C));
+ - + - ]
65 [ + - + - : 2 : BOOST_CHECK(!key1C.VerifyPubKey(pubkey2));
+ - + - ]
66 [ + - + - : 2 : BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C));
+ - + - ]
67 : :
68 [ + - + - : 2 : BOOST_CHECK(!key2.VerifyPubKey(pubkey1));
+ - + - ]
69 [ + - + - : 2 : BOOST_CHECK(!key2.VerifyPubKey(pubkey1C));
+ - + - ]
70 [ + - + - : 2 : BOOST_CHECK(key2.VerifyPubKey(pubkey2));
+ - + - ]
71 [ + - + - : 2 : BOOST_CHECK(!key2.VerifyPubKey(pubkey2C));
+ - + - ]
72 : :
73 [ + - + - : 2 : BOOST_CHECK(!key2C.VerifyPubKey(pubkey1));
+ - + - ]
74 [ + - + - : 2 : BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C));
+ - + - ]
75 [ + - + - : 2 : BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
+ - + - ]
76 [ + - + - : 2 : BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));
+ - + - ]
77 : :
78 [ + - + - : 3 : BOOST_CHECK(DecodeDestination(addr1) == CTxDestination(PKHash(pubkey1)));
+ - + - +
- ]
79 [ + - + - : 3 : BOOST_CHECK(DecodeDestination(addr2) == CTxDestination(PKHash(pubkey2)));
+ - + - +
- ]
80 [ + - + - : 3 : BOOST_CHECK(DecodeDestination(addr1C) == CTxDestination(PKHash(pubkey1C)));
+ - + - +
- ]
81 [ + - + - : 3 : BOOST_CHECK(DecodeDestination(addr2C) == CTxDestination(PKHash(pubkey2C)));
+ - + - ]
82 : :
83 [ + + ]: 17 : for (int n=0; n<16; n++)
84 : : {
85 [ + - ]: 16 : std::string strMsg = strprintf("Very secret message %i: 11", n);
86 [ + - ]: 16 : uint256 hashMsg = Hash(strMsg);
87 : :
88 : : // normal signatures
89 : :
90 : 16 : std::vector<unsigned char> sign1, sign2, sign1C, sign2C;
91 : :
92 [ + - + - : 32 : BOOST_CHECK(key1.Sign (hashMsg, sign1));
+ - + - ]
93 [ + - + - : 32 : BOOST_CHECK(key2.Sign (hashMsg, sign2));
+ - + - ]
94 [ + - + - : 32 : BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
+ - + - ]
95 [ + - + - : 32 : BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
+ - + - ]
96 : :
97 [ + - + - : 32 : BOOST_CHECK( pubkey1.Verify(hashMsg, sign1));
+ - + - ]
98 [ + - + - : 32 : BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2));
+ - + - ]
99 [ + - + - : 32 : BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C));
+ - + - ]
100 [ + - + - : 32 : BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C));
+ - + - ]
101 : :
102 [ + - + - : 32 : BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1));
+ - + - ]
103 [ + - + - : 32 : BOOST_CHECK( pubkey2.Verify(hashMsg, sign2));
+ - + - ]
104 [ + - + - : 32 : BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C));
+ - + - ]
105 [ + - + - : 32 : BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C));
+ - + - ]
106 : :
107 [ + - + - : 32 : BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1));
+ - + - ]
108 [ + - + - : 32 : BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2));
+ - + - ]
109 [ + - + - : 32 : BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1C));
+ - + - ]
110 [ + - + - : 32 : BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2C));
+ - + - ]
111 : :
112 [ + - + - : 32 : BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1));
+ - + - ]
113 [ + - + - : 32 : BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2));
+ - + - ]
114 [ + - + - : 32 : BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C));
+ - + - ]
115 [ + - + - : 32 : BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C));
+ - + - ]
116 : :
117 : : // compact signatures (with key recovery)
118 : :
119 : 16 : std::vector<unsigned char> csign1, csign2, csign1C, csign2C;
120 : :
121 [ + - + - : 32 : BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
+ - + - ]
122 [ + - + - : 32 : BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
+ - + - ]
123 [ + - + - : 32 : BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
+ - + - ]
124 [ + - + - : 32 : BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
+ - + - ]
125 : :
126 [ + - ]: 16 : CPubKey rkey1, rkey2, rkey1C, rkey2C;
127 : :
128 [ + - + - : 32 : BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1));
+ - + - ]
129 [ + - + - : 32 : BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2));
+ - + - ]
130 [ + - + - : 32 : BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C));
+ - + - ]
131 [ + - + - : 32 : BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C));
+ - + - ]
132 : :
133 [ + - + - : 32 : BOOST_CHECK(rkey1 == pubkey1);
+ - ]
134 [ + - + - : 32 : BOOST_CHECK(rkey2 == pubkey2);
+ - ]
135 [ + - + - : 32 : BOOST_CHECK(rkey1C == pubkey1C);
+ - ]
136 [ + - + - ]: 32 : BOOST_CHECK(rkey2C == pubkey2C);
137 : 16 : }
138 : :
139 : : // test deterministic signing
140 : :
141 : 1 : std::vector<unsigned char> detsig, detsigc;
142 [ + - ]: 1 : std::string strMsg = "Very deterministic message";
143 [ + - ]: 1 : uint256 hashMsg = Hash(strMsg);
144 [ + - + - : 2 : BOOST_CHECK(key1.Sign(hashMsg, detsig));
+ - + - ]
145 [ + - + - : 2 : BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
+ - + - ]
146 [ + - + - : 2 : BOOST_CHECK(detsig == detsigc);
+ - ]
147 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(detsig), "304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
+ - ]
148 : :
149 [ + - + - : 2 : BOOST_CHECK(key2.Sign(hashMsg, detsig));
+ - + - ]
150 [ + - + - : 2 : BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
+ - + - ]
151 [ + - + - : 2 : BOOST_CHECK(detsig == detsigc);
+ - ]
152 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(detsig), "3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
+ - ]
153 : :
154 [ + - + - : 2 : BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
+ - + - ]
155 [ + - + - : 2 : BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
+ - + - ]
156 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(detsig), "1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
+ - ]
157 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(detsigc), "205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
+ - ]
158 : :
159 [ + - + - : 2 : BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
+ - + - ]
160 [ + - + - : 2 : BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
+ - + - ]
161 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(detsig), "1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
+ - + - ]
162 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(detsigc), "2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
+ - ]
163 : 1 : }
164 : :
165 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(key_signature_tests)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
166 : : {
167 : : // When entropy is specified, we should see at least one high R signature within 20 signatures
168 : 1 : CKey key = DecodeSecret(strSecret1);
169 [ + - ]: 1 : std::string msg = "A message to be signed";
170 [ + - ]: 1 : uint256 msg_hash = Hash(msg);
171 : 1 : std::vector<unsigned char> sig;
172 : 1 : bool found = false;
173 : :
174 [ + - ]: 1 : for (int i = 1; i <=20; ++i) {
175 [ - + ]: 1 : sig.clear();
176 [ + - + - : 2 : BOOST_CHECK(key.Sign(msg_hash, sig, false, i));
+ - + - ]
177 [ + - - + ]: 1 : found = sig[3] == 0x21 && sig[4] == 0x00;
178 : 0 : if (found) {
179 : : break;
180 : : }
181 : : }
182 [ + - + - ]: 2 : BOOST_CHECK(found);
183 : :
184 : : // When entropy is not specified, we should always see low R signatures that are less than or equal to 70 bytes in 256 tries
185 : : // The low R signatures should always have the value of their "length of R" byte less than or equal to 32
186 : : // We should see at least one signature that is less than 70 bytes.
187 : 1 : bool found_small = false;
188 : 1 : bool found_big = false;
189 : 1 : bool bad_sign = false;
190 [ + + ]: 257 : for (int i = 0; i < 256; ++i) {
191 [ + - ]: 256 : sig.clear();
192 [ + - + - ]: 256 : std::string msg = "A message to be signed" + ToString(i);
193 [ + - ]: 256 : msg_hash = Hash(msg);
194 [ + - + - ]: 256 : if (!key.Sign(msg_hash, sig)) {
195 : : bad_sign = true;
196 : : break;
197 : : }
198 : : // sig.size() > 70 implies sig[3] > 32, because S is always low.
199 : : // But check both conditions anyway, just in case this implication is broken for some reason
200 [ + - + - ]: 256 : if (sig[3] > 32 || sig.size() > 70) {
201 : : found_big = true;
202 : : break;
203 : : }
204 : 256 : found_small |= sig.size() < 70;
205 : 256 : }
206 [ + - + - : 2 : BOOST_CHECK(!bad_sign);
+ - ]
207 [ + - + - : 2 : BOOST_CHECK(!found_big);
+ - ]
208 [ + - + - ]: 2 : BOOST_CHECK(found_small);
209 : 1 : }
210 : :
211 : 12 : static CPubKey UnserializePubkey(const std::vector<uint8_t>& data)
212 : : {
213 : 12 : DataStream stream{};
214 [ + - ]: 12 : stream << data;
215 [ + - ]: 12 : CPubKey pubkey;
216 [ + - ]: 12 : stream >> pubkey;
217 : 12 : return pubkey;
218 : 12 : }
219 : :
220 : 6 : static unsigned int GetLen(unsigned char chHeader)
221 : : {
222 [ + + ]: 6 : if (chHeader == 2 || chHeader == 3)
223 : : return CPubKey::COMPRESSED_SIZE;
224 [ + + + + ]: 4 : if (chHeader == 4 || chHeader == 6 || chHeader == 7)
225 : 3 : return CPubKey::SIZE;
226 : : return 0;
227 : : }
228 : :
229 : 12 : static void CmpSerializationPubkey(const CPubKey& pubkey)
230 : : {
231 : 12 : DataStream stream{};
232 [ + - ]: 12 : stream << pubkey;
233 [ + - ]: 12 : CPubKey pubkey2;
234 [ + - ]: 12 : stream >> pubkey2;
235 [ + - + - ]: 24 : BOOST_CHECK(pubkey == pubkey2);
236 : 12 : }
237 : :
238 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(pubkey_unserialize)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
239 : : {
240 [ + + ]: 7 : for (uint8_t i = 2; i <= 7; ++i) {
241 [ + - ]: 12 : CPubKey key = UnserializePubkey({0x02});
242 [ + - ]: 12 : BOOST_CHECK(!key.IsValid());
243 : 6 : CmpSerializationPubkey(key);
244 [ + - ]: 12 : key = UnserializePubkey(std::vector<uint8_t>(GetLen(i), i));
245 : 6 : CmpSerializationPubkey(key);
246 [ + + ]: 6 : if (i == 5) {
247 [ + - ]: 2 : BOOST_CHECK(!key.IsValid());
248 : : } else {
249 [ + - ]: 10 : BOOST_CHECK(key.IsValid());
250 : : }
251 : : }
252 : 1 : }
253 : :
254 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(bip340_test_vectors)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
255 : : {
256 : 1 : static const std::vector<std::pair<std::array<std::string, 3>, bool>> VECTORS = {
257 : : {{"F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", "0000000000000000000000000000000000000000000000000000000000000000", "E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0"}, true},
258 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A"}, true},
259 : : {{"DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8", "7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C", "5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7"}, true},
260 : : {{"25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3"}, true},
261 : : {{"D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9", "4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703", "00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6376AFB1548AF603B3EB45C9F8207DEE1060CB71C04E80F593060B07D28308D7F4"}, true},
262 : : {{"EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false},
263 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A14602975563CC27944640AC607CD107AE10923D9EF7A73C643E166BE5EBEAFA34B1AC553E2"}, false},
264 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "1FA62E331EDBC21C394792D2AB1100A7B432B013DF3F6FF4F99FCB33E0E1515F28890B3EDB6E7189B630448B515CE4F8622A954CFE545735AAEA5134FCCDB2BD"}, false},
265 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769961764B3AA9B2FFCB6EF947B6887A226E8D7C93E00C5ED0C1834FF0D0C2E6DA6"}, false},
266 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "0000000000000000000000000000000000000000000000000000000000000000123DDA8328AF9C23A94C1FEECFD123BA4FB73476F0D594DCB65C6425BD186051"}, false},
267 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "00000000000000000000000000000000000000000000000000000000000000017615FBAF5AE28864013C099742DEADB4DBA87F11AC6754F93780D5A1837CF197"}, false},
268 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false},
269 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false},
270 : : {{"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"}, false},
271 : : {{"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC30", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B"}, false}
272 [ + - + - : 18 : };
+ + - - ]
273 : :
274 [ + + ]: 16 : for (const auto& test : VECTORS) {
275 : 15 : auto pubkey = ParseHex(test.first[0]);
276 [ + - ]: 15 : auto msg = ParseHex(test.first[1]);
277 [ + - ]: 15 : auto sig = ParseHex(test.first[2]);
278 [ + - + - : 15 : BOOST_CHECK_EQUAL(XOnlyPubKey(pubkey).VerifySchnorr(uint256(msg), sig), test.second);
+ - ]
279 : 15 : }
280 : :
281 : 1 : static const std::vector<std::array<std::string, 5>> SIGN_VECTORS = {
282 : : {{"0000000000000000000000000000000000000000000000000000000000000003", "F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000", "E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0"}},
283 : : {{"B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF", "DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659", "0000000000000000000000000000000000000000000000000000000000000001", "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89", "6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A"}},
284 : : {{"C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9", "DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8", "C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906", "7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C", "5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7"}},
285 : : {{"0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710", "25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3"}},
286 [ + - + - : 7 : };
- + + + -
- ]
287 : :
288 [ + + ]: 5 : for (const auto& [sec_hex, pub_hex, aux_hex, msg_hex, sig_hex] : SIGN_VECTORS) {
289 : 4 : auto sec = ParseHex(sec_hex);
290 [ + - ]: 4 : auto pub = ParseHex(pub_hex);
291 [ + - ]: 4 : uint256 aux256(ParseHex(aux_hex));
292 [ + - ]: 4 : uint256 msg256(ParseHex(msg_hex));
293 [ + - ]: 4 : auto sig = ParseHex(sig_hex);
294 : 4 : unsigned char sig64[64];
295 : :
296 : : // Run the untweaked test vectors above, comparing with exact expected signature.
297 : 4 : CKey key;
298 [ + - ]: 4 : key.Set(sec.begin(), sec.end(), true);
299 [ + - ]: 4 : XOnlyPubKey pubkey(key.GetPubKey());
300 [ + - + - : 8 : BOOST_CHECK(std::equal(pubkey.begin(), pubkey.end(), pub.begin(), pub.end()));
+ - ]
301 [ + - ]: 4 : bool ok = key.SignSchnorr(msg256, sig64, nullptr, aux256);
302 [ + - + - : 8 : BOOST_CHECK(ok);
+ - ]
303 [ + - + - : 8 : BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
+ - + - ]
304 : : // Verify those signatures for good measure.
305 [ + - + - : 8 : BOOST_CHECK(pubkey.VerifySchnorr(msg256, sig64));
+ - + - ]
306 : :
307 : : // Repeat the same check, but use the KeyPair directly without any merkle tweak
308 [ + - ]: 4 : KeyPair keypair = key.ComputeKeyPair(/*merkle_root=*/nullptr);
309 [ + - ]: 4 : bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256);
310 [ + - + - : 8 : BOOST_CHECK(kp_ok);
+ - ]
311 [ + - + - : 8 : BOOST_CHECK(pubkey.VerifySchnorr(msg256, sig64));
+ - + - ]
312 [ + - + - : 8 : BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
+ - ]
313 : :
314 : : // Do 10 iterations where we sign with a random Merkle root to tweak,
315 : : // and compare against the resulting tweaked keys, with random aux.
316 : : // In iteration i=0 we tweak with empty Merkle tree.
317 [ + + ]: 44 : for (int i = 0; i < 10; ++i) {
318 : 40 : uint256 merkle_root;
319 [ + + ]: 40 : if (i) merkle_root = m_rng.rand256();
320 [ + - ]: 40 : auto tweaked = pubkey.CreateTapTweak(i ? &merkle_root : nullptr);
321 [ + - + - ]: 80 : BOOST_CHECK(tweaked);
322 : 40 : XOnlyPubKey tweaked_key = tweaked->first;
323 : 40 : aux256 = m_rng.rand256();
324 [ + - ]: 40 : bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, aux256);
325 [ + - + - : 80 : BOOST_CHECK(ok);
+ - ]
326 [ + - + - : 80 : BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64));
+ - + - ]
327 : :
328 : : // Repeat the same check, but use the KeyPair class directly
329 [ + - ]: 40 : KeyPair keypair = key.ComputeKeyPair(&merkle_root);
330 [ + - ]: 40 : bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256);
331 [ + - + - : 80 : BOOST_CHECK(kp_ok);
+ - ]
332 [ + - + - : 80 : BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64));
+ - ]
333 : 40 : }
334 : 4 : }
335 [ + - + - : 18 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - -
- ]
336 : :
337 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(key_ellswift)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
338 : : {
339 [ + - + - : 9 : for (const auto& secret : {strSecret1, strSecret2, strSecret1C, strSecret2C}) {
+ - + - +
+ - - ]
340 [ + - ]: 4 : CKey key = DecodeSecret(secret);
341 [ + - + - ]: 8 : BOOST_CHECK(key.IsValid());
342 : :
343 : 4 : uint256 ent32 = m_rng.rand256();
344 [ + - ]: 4 : auto ellswift = key.EllSwiftCreate(AsBytes(Span{ent32}));
345 : :
346 [ + - ]: 4 : CPubKey decoded_pubkey = ellswift.Decode();
347 [ + + ]: 4 : if (!key.IsCompressed()) {
348 : : // The decoding constructor returns a compressed pubkey. If the
349 : : // original was uncompressed, we must decompress the decoded one
350 : : // to compare.
351 [ + - ]: 2 : decoded_pubkey.Decompress();
352 : : }
353 [ + - + - : 8 : BOOST_CHECK(key.GetPubKey() == decoded_pubkey);
+ - ]
354 [ + + - - ]: 9 : }
355 : 1 : }
356 : :
357 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(bip341_test_h)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
358 : : {
359 : 1 : constexpr auto G_uncompressed{"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"_hex};
360 : 1 : HashWriter hw;
361 : 1 : hw.write(G_uncompressed);
362 : 1 : XOnlyPubKey H{hw.GetSHA256()};
363 [ + - ]: 2 : BOOST_CHECK(XOnlyPubKey::NUMS_H == H);
364 : 1 : }
365 : :
366 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(key_schnorr_tweak_smoke_test)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
367 : : {
368 : : // Sanity check to ensure we get the same tweak using CPubKey vs secp256k1 functions
369 : 1 : secp256k1_context* secp256k1_context_sign = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
370 : :
371 : 1 : CKey key;
372 [ + - ]: 1 : key.MakeNewKey(true);
373 : 1 : uint256 merkle_root = m_rng.rand256();
374 : :
375 : : // secp256k1 functions
376 : 1 : secp256k1_keypair keypair;
377 [ + - + - : 3 : BOOST_CHECK(secp256k1_keypair_create(secp256k1_context_sign, &keypair, UCharCast(key.begin())));
+ - + - +
- ]
378 : 1 : secp256k1_xonly_pubkey xonly_pubkey;
379 [ + - + - : 2 : BOOST_CHECK(secp256k1_keypair_xonly_pub(secp256k1_context_sign, &xonly_pubkey, nullptr, &keypair));
+ - + - ]
380 : 1 : unsigned char xonly_bytes[32];
381 [ + - + - : 2 : BOOST_CHECK(secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, xonly_bytes, &xonly_pubkey));
+ - ]
382 [ + - ]: 1 : uint256 tweak_old = XOnlyPubKey(xonly_bytes).ComputeTapTweakHash(&merkle_root);
383 : :
384 : : // CPubKey
385 [ + - ]: 1 : CPubKey pubkey = key.GetPubKey();
386 [ + - ]: 1 : uint256 tweak_new = XOnlyPubKey(pubkey).ComputeTapTweakHash(&merkle_root);
387 : :
388 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(tweak_old, tweak_new);
389 : :
390 [ + - ]: 1 : secp256k1_context_destroy(secp256k1_context_sign);
391 : 1 : }
392 : :
393 : : BOOST_AUTO_TEST_SUITE_END()
|