PIVX Core  5.6.99
P2P Digital Currency
key_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2013 The Bitcoin Core developers
2 // Copyright (c) 2017-2021 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "key.h"
7 
8 #include "base58.h"
9 #include "key_io.h"
10 #include "uint256.h"
11 #include "util/system.h"
12 #include "utilstrencodings.h"
13 #include "test_pivx.h"
14 
15 #include <string>
16 #include <vector>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 
21 static const std::string strSecret1 = "87vK7Vayi3QLsuiva5yWSuVwSMhMcRM9dBsaD6JXMD1P5vnjRFn";
22 static const std::string strSecret2 = "87FGYGFDg5SYfdD4XL593hr7do6f52czPecVsYSAXi8N4RGeS9i";
23 static const std::string strSecret1C = "YRYJwfAyJ9c2jhi3T2xQyLijGvM7yLTw4izDaNQLxBzgUYrQiPmJ";
24 static const std::string strSecret2C = "YNZyazHkwUbkmUpEYsBGWwHnHQTy2n9rJy1gS5k54YXVx3pE8n6N";
25 static const std::string addr1 = "DBFi8XAE1rcdCQfkv9w22n8Y9RxgaJnrDD";
26 static const std::string addr2 = "DPvKfv1FVp69yZMDzeuugvfZ9pzYiMv1bs";
27 static const std::string addr1C = "DNPrHK9ezAAUVExFDpZ7EE1xWpPskgp1gP";
28 static const std::string addr2C = "DNBVSAoc2whPFjZVAZ1pQbXPJk1LRrDC8Q";
29 
30 
31 static const std::string strAddressBad ="Xta1praZQjyELweyMByXyiREw1ZRsjXzVP";
32 
33 
35 
37 {
38  CKey key1 = KeyIO::DecodeSecret(strSecret1);
39  BOOST_CHECK(key1.IsValid() && !key1.IsCompressed());
40  CKey key2 = KeyIO::DecodeSecret(strSecret2);
41  BOOST_CHECK(key2.IsValid() && !key2.IsCompressed());
42  CKey key1C = KeyIO::DecodeSecret(strSecret1C);
43  BOOST_CHECK(key1C.IsValid() && key1C.IsCompressed());
44  CKey key2C = KeyIO::DecodeSecret(strSecret2C);
45  BOOST_CHECK(key2C.IsValid() && key2C.IsCompressed());
46  CKey bad_key = KeyIO::DecodeSecret(strAddressBad);
47  BOOST_CHECK(!bad_key.IsValid());
48 
49  CPubKey pubkey1 = key1. GetPubKey();
50  CPubKey pubkey2 = key2. GetPubKey();
51  CPubKey pubkey1C = key1C.GetPubKey();
52  CPubKey pubkey2C = key2C.GetPubKey();
53 
54  BOOST_CHECK(key1.VerifyPubKey(pubkey1));
55  BOOST_CHECK(!key1.VerifyPubKey(pubkey1C));
56  BOOST_CHECK(!key1.VerifyPubKey(pubkey2));
57  BOOST_CHECK(!key1.VerifyPubKey(pubkey2C));
58 
59  BOOST_CHECK(!key1C.VerifyPubKey(pubkey1));
60  BOOST_CHECK(key1C.VerifyPubKey(pubkey1C));
61  BOOST_CHECK(!key1C.VerifyPubKey(pubkey2));
62  BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C));
63 
64  BOOST_CHECK(!key2.VerifyPubKey(pubkey1));
65  BOOST_CHECK(!key2.VerifyPubKey(pubkey1C));
66  BOOST_CHECK(key2.VerifyPubKey(pubkey2));
67  BOOST_CHECK(!key2.VerifyPubKey(pubkey2C));
68 
69  BOOST_CHECK(!key2C.VerifyPubKey(pubkey1));
70  BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C));
71  BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
72  BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));
73 
74  BOOST_CHECK(DecodeDestination(addr1) == CTxDestination(pubkey1.GetID()));
75  BOOST_CHECK(DecodeDestination(addr2) == CTxDestination(pubkey2.GetID()));
76  BOOST_CHECK(DecodeDestination(addr1C) == CTxDestination(pubkey1C.GetID()));
77  BOOST_CHECK(DecodeDestination(addr2C) == CTxDestination(pubkey2C.GetID()));
78 
79  for (int n=0; n<16; n++)
80  {
81  std::string strMsg = strprintf("Very secret message %i: 11", n);
82  uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
83 
84  // normal signatures
85 
86  std::vector<unsigned char> sign1, sign2, sign1C, sign2C;
87 
88  BOOST_CHECK(key1.Sign (hashMsg, sign1));
89  BOOST_CHECK(key2.Sign (hashMsg, sign2));
90  BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
91  BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
92 
93  BOOST_CHECK( pubkey1.Verify(hashMsg, sign1));
94  BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2));
95  BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C));
96  BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C));
97 
98  BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1));
99  BOOST_CHECK( pubkey2.Verify(hashMsg, sign2));
100  BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C));
101  BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C));
102 
103  BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1));
104  BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2));
105  BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1C));
106  BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2C));
107 
108  BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1));
109  BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2));
110  BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C));
111  BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C));
112 
113  // compact signatures (with key recovery)
114 
115  std::vector<unsigned char> csign1, csign2, csign1C, csign2C;
116 
117  BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
118  BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
119  BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
120  BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
121 
122  CPubKey rkey1, rkey2, rkey1C, rkey2C;
123 
124  BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1));
125  BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2));
126  BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C));
127  BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C));
128 
129  BOOST_CHECK(rkey1 == pubkey1);
130  BOOST_CHECK(rkey2 == pubkey2);
131  BOOST_CHECK(rkey1C == pubkey1C);
132  BOOST_CHECK(rkey2C == pubkey2C);
133  }
134 
135  // test deterministic signing
136 
137  std::vector<unsigned char> detsig, detsigc;
138  std::string strMsg = "Very deterministic message";
139  uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
140  BOOST_CHECK(key1.Sign(hashMsg, detsig));
141  BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
142  BOOST_CHECK(detsig == detsigc);
143  BOOST_CHECK(detsig == ParseHex("30450221009071d4fead181ea197d6a23106c48ee5de25e023b38afaf71c170e3088e5238a02200dcbc7f1aad626a5ee812e08ef047114642538e423a94b4bd6a272731cf500d0"));
144  BOOST_CHECK(key2.Sign(hashMsg, detsig));
145  BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
146  BOOST_CHECK(detsig == detsigc);
147  BOOST_CHECK(detsig == ParseHex("304402204f304f1b05599f88bc517819f6d43c69503baea5f253c55ea2d791394f7ce0de02204f23c0d4c1f4d7a89bf130fed755201d22581911a8a44cf594014794231d325a"));
148  BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
149  BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
150  BOOST_CHECK(detsig == ParseHex("1b9071d4fead181ea197d6a23106c48ee5de25e023b38afaf71c170e3088e5238a0dcbc7f1aad626a5ee812e08ef047114642538e423a94b4bd6a272731cf500d0"));
151  BOOST_CHECK(detsigc == ParseHex("1f9071d4fead181ea197d6a23106c48ee5de25e023b38afaf71c170e3088e5238a0dcbc7f1aad626a5ee812e08ef047114642538e423a94b4bd6a272731cf500d0"));
152  BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
153  BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
154  BOOST_CHECK(detsig == ParseHex("1b4f304f1b05599f88bc517819f6d43c69503baea5f253c55ea2d791394f7ce0de4f23c0d4c1f4d7a89bf130fed755201d22581911a8a44cf594014794231d325a"));
155  BOOST_CHECK(detsigc == ParseHex("1f4f304f1b05599f88bc517819f6d43c69503baea5f253c55ea2d791394f7ce0de4f23c0d4c1f4d7a89bf130fed755201d22581911a8a44cf594014794231d325a"));
156 }
157 
An encapsulated private key.
Definition: key.h:30
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:95
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:98
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:186
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:200
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:216
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:231
An encapsulated public key.
Definition: pubkey.h:44
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:188
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:167
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:169
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:173
BOOST_AUTO_TEST_CASE(key_test1)
Definition: key_tests.cpp:36
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:127
CWDestination DecodeDestination(const std::string &strAddress)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:72
#define strprintf
Definition: tinyformat.h:1056
std::vector< unsigned char > ParseHex(const char *psz)