You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

key_tests.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (c) 2012-2013 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. #include "key.h"
  5. #include "base58.h"
  6. #include "script/script.h"
  7. #include "uint256.h"
  8. #include "util.h"
  9. #include "utilstrencodings.h"
  10. #include "test/test_bitcoin.h"
  11. #include <string>
  12. #include <vector>
  13. #include <boost/test/unit_test.hpp>
  14. using namespace std;
  15. static const string strSecret1 ("5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj");
  16. static const string strSecret2 ("5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3");
  17. static const string strSecret1C ("Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw");
  18. static const string strSecret2C ("L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g");
  19. static const CBitcoinAddress addr1 ("1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ");
  20. static const CBitcoinAddress addr2 ("1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ");
  21. static const CBitcoinAddress addr1C("1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs");
  22. static const CBitcoinAddress addr2C("1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs");
  23. static const string strAddressBad("1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF");
  24. #ifdef KEY_TESTS_DUMPINFO
  25. void dumpKeyInfo(uint256 privkey)
  26. {
  27. CKey key;
  28. key.resize(32);
  29. memcpy(&secret[0], &privkey, 32);
  30. vector<unsigned char> sec;
  31. sec.resize(32);
  32. memcpy(&sec[0], &secret[0], 32);
  33. printf(" * secret (hex): %s\n", HexStr(sec).c_str());
  34. for (int nCompressed=0; nCompressed<2; nCompressed++)
  35. {
  36. bool fCompressed = nCompressed == 1;
  37. printf(" * %s:\n", fCompressed ? "compressed" : "uncompressed");
  38. CBitcoinSecret bsecret;
  39. bsecret.SetSecret(secret, fCompressed);
  40. printf(" * secret (base58): %s\n", bsecret.ToString().c_str());
  41. CKey key;
  42. key.SetSecret(secret, fCompressed);
  43. vector<unsigned char> vchPubKey = key.GetPubKey();
  44. printf(" * pubkey (hex): %s\n", HexStr(vchPubKey).c_str());
  45. printf(" * address (base58): %s\n", CBitcoinAddress(vchPubKey).ToString().c_str());
  46. }
  47. }
  48. #endif
  49. BOOST_FIXTURE_TEST_SUITE(key_tests, BasicTestingSetup)
  50. BOOST_AUTO_TEST_CASE(key_test1)
  51. {
  52. CBitcoinSecret bsecret1, bsecret2, bsecret1C, bsecret2C, baddress1;
  53. BOOST_CHECK( bsecret1.SetString (strSecret1));
  54. BOOST_CHECK( bsecret2.SetString (strSecret2));
  55. BOOST_CHECK( bsecret1C.SetString(strSecret1C));
  56. BOOST_CHECK( bsecret2C.SetString(strSecret2C));
  57. BOOST_CHECK(!baddress1.SetString(strAddressBad));
  58. CKey key1 = bsecret1.GetKey();
  59. BOOST_CHECK(key1.IsCompressed() == false);
  60. CKey key2 = bsecret2.GetKey();
  61. BOOST_CHECK(key2.IsCompressed() == false);
  62. CKey key1C = bsecret1C.GetKey();
  63. BOOST_CHECK(key1C.IsCompressed() == true);
  64. CKey key2C = bsecret2C.GetKey();
  65. BOOST_CHECK(key2C.IsCompressed() == true);
  66. CPubKey pubkey1 = key1. GetPubKey();
  67. CPubKey pubkey2 = key2. GetPubKey();
  68. CPubKey pubkey1C = key1C.GetPubKey();
  69. CPubKey pubkey2C = key2C.GetPubKey();
  70. BOOST_CHECK(key1.VerifyPubKey(pubkey1));
  71. BOOST_CHECK(!key1.VerifyPubKey(pubkey1C));
  72. BOOST_CHECK(!key1.VerifyPubKey(pubkey2));
  73. BOOST_CHECK(!key1.VerifyPubKey(pubkey2C));
  74. BOOST_CHECK(!key1C.VerifyPubKey(pubkey1));
  75. BOOST_CHECK(key1C.VerifyPubKey(pubkey1C));
  76. BOOST_CHECK(!key1C.VerifyPubKey(pubkey2));
  77. BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C));
  78. BOOST_CHECK(!key2.VerifyPubKey(pubkey1));
  79. BOOST_CHECK(!key2.VerifyPubKey(pubkey1C));
  80. BOOST_CHECK(key2.VerifyPubKey(pubkey2));
  81. BOOST_CHECK(!key2.VerifyPubKey(pubkey2C));
  82. BOOST_CHECK(!key2C.VerifyPubKey(pubkey1));
  83. BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C));
  84. BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
  85. BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));
  86. BOOST_CHECK(addr1.Get() == CTxDestination(pubkey1.GetID()));
  87. BOOST_CHECK(addr2.Get() == CTxDestination(pubkey2.GetID()));
  88. BOOST_CHECK(addr1C.Get() == CTxDestination(pubkey1C.GetID()));
  89. BOOST_CHECK(addr2C.Get() == CTxDestination(pubkey2C.GetID()));
  90. for (int n=0; n<16; n++)
  91. {
  92. string strMsg = strprintf("Very secret message %i: 11", n);
  93. uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
  94. // normal signatures
  95. vector<unsigned char> sign1, sign2, sign1C, sign2C;
  96. BOOST_CHECK(key1.Sign (hashMsg, sign1));
  97. BOOST_CHECK(key2.Sign (hashMsg, sign2));
  98. BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
  99. BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
  100. BOOST_CHECK( pubkey1.Verify(hashMsg, sign1));
  101. BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2));
  102. BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C));
  103. BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C));
  104. BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1));
  105. BOOST_CHECK( pubkey2.Verify(hashMsg, sign2));
  106. BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C));
  107. BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C));
  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. BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1));
  113. BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2));
  114. BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C));
  115. BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C));
  116. // compact signatures (with key recovery)
  117. vector<unsigned char> csign1, csign2, csign1C, csign2C;
  118. BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
  119. BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
  120. BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
  121. BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
  122. CPubKey rkey1, rkey2, rkey1C, rkey2C;
  123. BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1));
  124. BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2));
  125. BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C));
  126. BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C));
  127. BOOST_CHECK(rkey1 == pubkey1);
  128. BOOST_CHECK(rkey2 == pubkey2);
  129. BOOST_CHECK(rkey1C == pubkey1C);
  130. BOOST_CHECK(rkey2C == pubkey2C);
  131. }
  132. // test deterministic signing
  133. std::vector<unsigned char> detsig, detsigc;
  134. string strMsg = "Very deterministic message";
  135. uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
  136. BOOST_CHECK(key1.Sign(hashMsg, detsig));
  137. BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
  138. BOOST_CHECK(detsig == detsigc);
  139. BOOST_CHECK(detsig == ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
  140. BOOST_CHECK(key2.Sign(hashMsg, detsig));
  141. BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
  142. BOOST_CHECK(detsig == detsigc);
  143. BOOST_CHECK(detsig == ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
  144. BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
  145. BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
  146. BOOST_CHECK(detsig == ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
  147. BOOST_CHECK(detsigc == ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
  148. BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
  149. BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
  150. BOOST_CHECK(detsig == ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
  151. BOOST_CHECK(detsigc == ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
  152. }
  153. BOOST_AUTO_TEST_SUITE_END()