2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2012-01-08 16:59:34 +01:00
|
|
|
#include "key.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-01-08 16:59:34 +01:00
|
|
|
#include "base58.h"
|
2014-08-20 17:37:40 +02:00
|
|
|
#include "script/script.h"
|
2012-01-08 16:59:34 +01:00
|
|
|
#include "uint256.h"
|
|
|
|
#include "util.h"
|
2014-11-06 15:54:50 +01:00
|
|
|
#include "utilstrencodings.h"
|
2015-03-12 09:34:42 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2017-08-23 03:02:33 +02:00
|
|
|
static const std::string strSecret1 = "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj";
|
|
|
|
static const std::string strSecret2 = "5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3";
|
|
|
|
static const std::string strSecret1C = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
|
|
|
|
static const std::string strSecret2C = "L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g";
|
|
|
|
static const std::string addr1 = "1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ";
|
|
|
|
static const std::string addr2 = "1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ";
|
|
|
|
static const std::string addr1C = "1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs";
|
|
|
|
static const std::string addr2C = "1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs";
|
2012-05-14 23:44:52 +02:00
|
|
|
|
2017-08-23 03:02:33 +02:00
|
|
|
static const std::string strAddressBad = "1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF";
|
2012-05-14 23:44:52 +02:00
|
|
|
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(key_tests, BasicTestingSetup)
|
2012-01-08 16:59:34 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(key_test1)
|
|
|
|
{
|
2012-04-12 22:13:08 +02:00
|
|
|
CBitcoinSecret bsecret1, bsecret2, bsecret1C, bsecret2C, baddress1;
|
|
|
|
BOOST_CHECK( bsecret1.SetString (strSecret1));
|
|
|
|
BOOST_CHECK( bsecret2.SetString (strSecret2));
|
|
|
|
BOOST_CHECK( bsecret1C.SetString(strSecret1C));
|
|
|
|
BOOST_CHECK( bsecret2C.SetString(strSecret2C));
|
2012-05-14 23:44:52 +02:00
|
|
|
BOOST_CHECK(!baddress1.SetString(strAddressBad));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
CKey key1 = bsecret1.GetKey();
|
|
|
|
BOOST_CHECK(key1.IsCompressed() == false);
|
|
|
|
CKey key2 = bsecret2.GetKey();
|
|
|
|
BOOST_CHECK(key2.IsCompressed() == false);
|
|
|
|
CKey key1C = bsecret1C.GetKey();
|
|
|
|
BOOST_CHECK(key1C.IsCompressed() == true);
|
|
|
|
CKey key2C = bsecret2C.GetKey();
|
2014-10-13 17:57:09 +02:00
|
|
|
BOOST_CHECK(key2C.IsCompressed() == true);
|
2013-05-01 06:52:05 +02:00
|
|
|
|
|
|
|
CPubKey pubkey1 = key1. GetPubKey();
|
|
|
|
CPubKey pubkey2 = key2. GetPubKey();
|
|
|
|
CPubKey pubkey1C = key1C.GetPubKey();
|
|
|
|
CPubKey pubkey2C = key2C.GetPubKey();
|
|
|
|
|
2014-11-06 10:17:48 +01:00
|
|
|
BOOST_CHECK(key1.VerifyPubKey(pubkey1));
|
|
|
|
BOOST_CHECK(!key1.VerifyPubKey(pubkey1C));
|
|
|
|
BOOST_CHECK(!key1.VerifyPubKey(pubkey2));
|
|
|
|
BOOST_CHECK(!key1.VerifyPubKey(pubkey2C));
|
|
|
|
|
|
|
|
BOOST_CHECK(!key1C.VerifyPubKey(pubkey1));
|
|
|
|
BOOST_CHECK(key1C.VerifyPubKey(pubkey1C));
|
|
|
|
BOOST_CHECK(!key1C.VerifyPubKey(pubkey2));
|
|
|
|
BOOST_CHECK(!key1C.VerifyPubKey(pubkey2C));
|
|
|
|
|
|
|
|
BOOST_CHECK(!key2.VerifyPubKey(pubkey1));
|
|
|
|
BOOST_CHECK(!key2.VerifyPubKey(pubkey1C));
|
|
|
|
BOOST_CHECK(key2.VerifyPubKey(pubkey2));
|
|
|
|
BOOST_CHECK(!key2.VerifyPubKey(pubkey2C));
|
|
|
|
|
|
|
|
BOOST_CHECK(!key2C.VerifyPubKey(pubkey1));
|
|
|
|
BOOST_CHECK(!key2C.VerifyPubKey(pubkey1C));
|
|
|
|
BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
|
|
|
|
BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));
|
|
|
|
|
2017-08-23 03:02:33 +02:00
|
|
|
BOOST_CHECK(DecodeDestination(addr1) == CTxDestination(pubkey1.GetID()));
|
|
|
|
BOOST_CHECK(DecodeDestination(addr2) == CTxDestination(pubkey2.GetID()));
|
|
|
|
BOOST_CHECK(DecodeDestination(addr1C) == CTxDestination(pubkey1C.GetID()));
|
|
|
|
BOOST_CHECK(DecodeDestination(addr2C) == CTxDestination(pubkey2C.GetID()));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
|
|
|
for (int n=0; n<16; n++)
|
|
|
|
{
|
2016-12-05 08:03:53 +01:00
|
|
|
std::string strMsg = strprintf("Very secret message %i: 11", n);
|
2012-01-08 16:59:34 +01:00
|
|
|
uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
|
|
|
|
|
|
|
|
// normal signatures
|
|
|
|
|
2016-12-05 08:03:53 +01:00
|
|
|
std::vector<unsigned char> sign1, sign2, sign1C, sign2C;
|
2012-01-08 16:59:34 +01:00
|
|
|
|
|
|
|
BOOST_CHECK(key1.Sign (hashMsg, sign1));
|
|
|
|
BOOST_CHECK(key2.Sign (hashMsg, sign2));
|
|
|
|
BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
|
|
|
|
BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
|
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
BOOST_CHECK( pubkey1.Verify(hashMsg, sign1));
|
|
|
|
BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2));
|
|
|
|
BOOST_CHECK( pubkey1.Verify(hashMsg, sign1C));
|
|
|
|
BOOST_CHECK(!pubkey1.Verify(hashMsg, sign2C));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1));
|
|
|
|
BOOST_CHECK( pubkey2.Verify(hashMsg, sign2));
|
|
|
|
BOOST_CHECK(!pubkey2.Verify(hashMsg, sign1C));
|
|
|
|
BOOST_CHECK( pubkey2.Verify(hashMsg, sign2C));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1));
|
|
|
|
BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2));
|
|
|
|
BOOST_CHECK( pubkey1C.Verify(hashMsg, sign1C));
|
|
|
|
BOOST_CHECK(!pubkey1C.Verify(hashMsg, sign2C));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1));
|
|
|
|
BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2));
|
|
|
|
BOOST_CHECK(!pubkey2C.Verify(hashMsg, sign1C));
|
|
|
|
BOOST_CHECK( pubkey2C.Verify(hashMsg, sign2C));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
|
|
|
// compact signatures (with key recovery)
|
|
|
|
|
2016-12-05 08:03:53 +01:00
|
|
|
std::vector<unsigned char> csign1, csign2, csign1C, csign2C;
|
2012-01-08 16:59:34 +01:00
|
|
|
|
|
|
|
BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
|
|
|
|
BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
|
|
|
|
BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
|
|
|
|
BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
|
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
CPubKey rkey1, rkey2, rkey1C, rkey2C;
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
BOOST_CHECK(rkey1.RecoverCompact (hashMsg, csign1));
|
|
|
|
BOOST_CHECK(rkey2.RecoverCompact (hashMsg, csign2));
|
|
|
|
BOOST_CHECK(rkey1C.RecoverCompact(hashMsg, csign1C));
|
|
|
|
BOOST_CHECK(rkey2C.RecoverCompact(hashMsg, csign2C));
|
2012-01-08 16:59:34 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
BOOST_CHECK(rkey1 == pubkey1);
|
|
|
|
BOOST_CHECK(rkey2 == pubkey2);
|
|
|
|
BOOST_CHECK(rkey1C == pubkey1C);
|
|
|
|
BOOST_CHECK(rkey2C == pubkey2C);
|
2012-01-08 16:59:34 +01:00
|
|
|
}
|
2014-11-06 15:54:50 +01:00
|
|
|
|
|
|
|
// test deterministic signing
|
|
|
|
|
|
|
|
std::vector<unsigned char> detsig, detsigc;
|
2016-12-05 08:03:53 +01:00
|
|
|
std::string strMsg = "Very deterministic message";
|
2014-11-06 15:54:50 +01:00
|
|
|
uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
|
|
|
|
BOOST_CHECK(key1.Sign(hashMsg, detsig));
|
|
|
|
BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
|
|
|
|
BOOST_CHECK(detsig == detsigc);
|
|
|
|
BOOST_CHECK(detsig == ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
|
|
|
|
BOOST_CHECK(key2.Sign(hashMsg, detsig));
|
|
|
|
BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
|
|
|
|
BOOST_CHECK(detsig == detsigc);
|
|
|
|
BOOST_CHECK(detsig == ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
|
|
|
|
BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
|
|
|
|
BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
|
|
|
|
BOOST_CHECK(detsig == ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
|
|
|
|
BOOST_CHECK(detsigc == ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
|
|
|
|
BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
|
|
|
|
BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
|
|
|
|
BOOST_CHECK(detsig == ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
|
|
|
|
BOOST_CHECK(detsigc == ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
|
2012-01-08 16:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|