21#include <boost/test/unit_test.hpp>
26static const std::string
strSecret1 =
"5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj";
27static const std::string
strSecret2 =
"5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3";
28static const std::string
strSecret1C =
"Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
29static const std::string
strSecret2C =
"L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g";
30static const std::string
addr1 =
"1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ";
31static const std::string
addr2 =
"1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ";
32static const std::string
addr1C =
"1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs";
33static const std::string
addr2C =
"1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs";
35static const std::string
strAddressBad =
"1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF";
83 for (
int n=0; n<16; n++)
85 std::string strMsg =
strprintf(
"Very secret message %i: 11", n);
90 std::vector<unsigned char> sign1, sign2, sign1C, sign2C;
119 std::vector<unsigned char> csign1, csign2, csign1C, csign2C;
126 CPubKey rkey1, rkey2, rkey1C, rkey2C;
141 std::vector<unsigned char> detsig, detsigc;
142 std::string strMsg =
"Very deterministic message";
147 BOOST_CHECK_EQUAL(
HexStr(detsig),
"304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
152 BOOST_CHECK_EQUAL(
HexStr(detsig),
"3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
156 BOOST_CHECK_EQUAL(
HexStr(detsig),
"1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
157 BOOST_CHECK_EQUAL(
HexStr(detsigc),
"205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
161 BOOST_CHECK_EQUAL(
HexStr(detsig),
"1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
162 BOOST_CHECK_EQUAL(
HexStr(detsigc),
"2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
169 std::string
msg =
"A message to be signed";
171 std::vector<unsigned char> sig;
174 for (
int i = 1; i <=20; ++i) {
177 found = sig[3] == 0x21 && sig[4] == 0x00;
187 bool found_small =
false;
188 bool found_big =
false;
189 bool bad_sign =
false;
190 for (
int i = 0; i < 256; ++i) {
192 std::string
msg =
"A message to be signed" +
ToString(i);
194 if (!key.
Sign(msg_hash, sig)) {
200 if (sig[3] > 32 || sig.size() > 70) {
204 found_small |= sig.size() < 70;
220static unsigned int GetLen(
unsigned char chHeader)
222 if (chHeader == 2 || chHeader == 3)
224 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
240 for (uint8_t i = 2; i <= 7; ++i) {
256 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}
274 for (
const auto& test : VECTORS) {
275 auto pubkey =
ParseHex(test.first[0]);
281 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"}},
288 for (
const auto& [sec_hex, pub_hex, aux_hex, msg_hex, sig_hex] : SIGN_VECTORS) {
294 unsigned char sig64[64];
298 key.
Set(sec.begin(), sec.end(),
true);
301 bool ok = key.
SignSchnorr(msg256, sig64,
nullptr, aux256);
303 BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
309 bool kp_ok = keypair.
SignSchnorr(msg256, sig64, aux256);
312 BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
317 for (
int i = 0; i < 10; ++i) {
319 if (i) merkle_root = m_rng.rand256();
323 aux256 = m_rng.rand256();
324 bool ok = key.
SignSchnorr(msg256, sig64, &merkle_root, aux256);
330 bool kp_ok = keypair.
SignSchnorr(msg256, sig64, aux256);
343 uint256 ent32 = m_rng.rand256();
346 CPubKey decoded_pubkey = ellswift.Decode();
359 constexpr auto G_uncompressed{
"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"_hex};
361 hw.
write(G_uncompressed);
373 uint256 merkle_root = m_rng.rand256();
380 unsigned char xonly_bytes[32];
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
An encapsulated private key.
bool SignSchnorr(const uint256 &hash, 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...
KeyPair ComputeKeyPair(const uint256 *merkle_root) const
Compute a KeyPair.
bool IsValid() const
Check whether this private key is valid.
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
const std::byte * begin() const
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
CPubKey GetPubKey() const
Compute the public key from a private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
EllSwiftPubKey EllSwiftCreate(Span< const std::byte > entropy) const
Create an ellswift-encoded public key for this key, with specified entropy.
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
An encapsulated public key.
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
static constexpr unsigned int COMPRESSED_SIZE
bool Decompress()
Turn this public key into an uncompressed public key.
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
static constexpr unsigned int SIZE
secp256k1:
Double ended buffer combining vector and stream-like interfaces.
A writer stream (for serialization) that computes a 256-bit hash.
void write(Span< const std::byte > src)
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
bool SignSchnorr(const uint256 &hash, Span< unsigned char > sig, const uint256 &aux) const
A Span is an object that can refer to a contiguous sequence of objects.
const unsigned char * end() const
const unsigned char * begin() const
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
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...
bool VerifySchnorr(const uint256 &msg, Span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
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.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static secp256k1_context * secp256k1_context_sign
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
CKey DecodeSecret(const std::string &str)
static const std::string strSecret2
static unsigned int GetLen(unsigned char chHeader)
static const std::string addr1
static const std::string strSecret2C
static void CmpSerializationPubkey(const CPubKey &pubkey)
static const std::string addr2C
static CPubKey UnserializePubkey(const std::vector< uint8_t > &data)
static const std::string addr1C
static const std::string strSecret1
static const std::string strSecret1C
static const std::string addr2
BOOST_AUTO_TEST_CASE(key_test1)
static const std::string strAddressBad
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
std::string ToString(const T &t)
Locale-independent version of std::to_string.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Span< const std::byte > AsBytes(Span< T > s) noexcept
unsigned char * UCharCast(char *c)
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
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.