Merge #9911: Wshadow: various gcc fixes
[bitcoinplatinum.git] / src / test / key_tests.cpp
blob559b3caf1c58579a8039d3dd58e163d43aaf1aac
1 // Copyright (c) 2012-2015 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.
5 #include "key.h"
7 #include "base58.h"
8 #include "script/script.h"
9 #include "uint256.h"
10 #include "util.h"
11 #include "utilstrencodings.h"
12 #include "test/test_bitcoin.h"
14 #include <string>
15 #include <vector>
17 #include <boost/test/unit_test.hpp>
19 static const std::string strSecret1 ("5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj");
20 static const std::string strSecret2 ("5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3");
21 static const std::string strSecret1C ("Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw");
22 static const std::string strSecret2C ("L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g");
23 static const CBitcoinAddress addr1 ("1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ");
24 static const CBitcoinAddress addr2 ("1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ");
25 static const CBitcoinAddress addr1C("1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs");
26 static const CBitcoinAddress addr2C("1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs");
29 static const std::string strAddressBad("1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF");
32 BOOST_FIXTURE_TEST_SUITE(key_tests, BasicTestingSetup)
34 BOOST_AUTO_TEST_CASE(key_test1)
36 CBitcoinSecret bsecret1, bsecret2, bsecret1C, bsecret2C, baddress1;
37 BOOST_CHECK( bsecret1.SetString (strSecret1));
38 BOOST_CHECK( bsecret2.SetString (strSecret2));
39 BOOST_CHECK( bsecret1C.SetString(strSecret1C));
40 BOOST_CHECK( bsecret2C.SetString(strSecret2C));
41 BOOST_CHECK(!baddress1.SetString(strAddressBad));
43 CKey key1 = bsecret1.GetKey();
44 BOOST_CHECK(key1.IsCompressed() == false);
45 CKey key2 = bsecret2.GetKey();
46 BOOST_CHECK(key2.IsCompressed() == false);
47 CKey key1C = bsecret1C.GetKey();
48 BOOST_CHECK(key1C.IsCompressed() == true);
49 CKey key2C = bsecret2C.GetKey();
50 BOOST_CHECK(key2C.IsCompressed() == true);
52 CPubKey pubkey1 = key1. GetPubKey();
53 CPubKey pubkey2 = key2. GetPubKey();
54 CPubKey pubkey1C = key1C.GetPubKey();
55 CPubKey pubkey2C = key2C.GetPubKey();
57 BOOST_CHECK(key1.VerifyPubKey(pubkey1));
58 BOOST_CHECK(!key1.VerifyPubKey(pubkey1C));
59 BOOST_CHECK(!key1.VerifyPubKey(pubkey2));
60 BOOST_CHECK(!key1.VerifyPubKey(pubkey2C));
62 BOOST_CHECK(!key1C.VerifyPubKey(pubkey1));
63 BOOST_CHECK(key1C.VerifyPubKey(pubkey1C));
64 BOOST_CHECK(!key1C.VerifyPubKey(pubkey2));
65 BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C));
67 BOOST_CHECK(!key2.VerifyPubKey(pubkey1));
68 BOOST_CHECK(!key2.VerifyPubKey(pubkey1C));
69 BOOST_CHECK(key2.VerifyPubKey(pubkey2));
70 BOOST_CHECK(!key2.VerifyPubKey(pubkey2C));
72 BOOST_CHECK(!key2C.VerifyPubKey(pubkey1));
73 BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C));
74 BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
75 BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));
77 BOOST_CHECK(addr1.Get() == CTxDestination(pubkey1.GetID()));
78 BOOST_CHECK(addr2.Get() == CTxDestination(pubkey2.GetID()));
79 BOOST_CHECK(addr1C.Get() == CTxDestination(pubkey1C.GetID()));
80 BOOST_CHECK(addr2C.Get() == CTxDestination(pubkey2C.GetID()));
82 for (int n=0; n<16; n++)
84 std::string strMsg = strprintf("Very secret message %i: 11", n);
85 uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
87 // normal signatures
89 std::vector<unsigned char> sign1, sign2, sign1C, sign2C;
91 BOOST_CHECK(key1.Sign (hashMsg, sign1));
92 BOOST_CHECK(key2.Sign (hashMsg, sign2));
93 BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
94 BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
96 BOOST_CHECK( pubkey1.Verify(hashMsg, sign1));
97 BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2));
98 BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C));
99 BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C));
101 BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1));
102 BOOST_CHECK( pubkey2.Verify(hashMsg, sign2));
103 BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C));
104 BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C));
106 BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1));
107 BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2));
108 BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1C));
109 BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2C));
111 BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1));
112 BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2));
113 BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C));
114 BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C));
116 // compact signatures (with key recovery)
118 std::vector<unsigned char> csign1, csign2, csign1C, csign2C;
120 BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
121 BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
122 BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
123 BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
125 CPubKey rkey1, rkey2, rkey1C, rkey2C;
127 BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1));
128 BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2));
129 BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C));
130 BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C));
132 BOOST_CHECK(rkey1 == pubkey1);
133 BOOST_CHECK(rkey2 == pubkey2);
134 BOOST_CHECK(rkey1C == pubkey1C);
135 BOOST_CHECK(rkey2C == pubkey2C);
138 // test deterministic signing
140 std::vector<unsigned char> detsig, detsigc;
141 std::string strMsg = "Very deterministic message";
142 uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
143 BOOST_CHECK(key1.Sign(hashMsg, detsig));
144 BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
145 BOOST_CHECK(detsig == detsigc);
146 BOOST_CHECK(detsig == ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
147 BOOST_CHECK(key2.Sign(hashMsg, detsig));
148 BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
149 BOOST_CHECK(detsig == detsigc);
150 BOOST_CHECK(detsig == ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
151 BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
152 BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
153 BOOST_CHECK(detsig == ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
154 BOOST_CHECK(detsigc == ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
155 BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
156 BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
157 BOOST_CHECK(detsig == ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
158 BOOST_CHECK(detsigc == ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
161 BOOST_AUTO_TEST_SUITE_END()