LCOV - code coverage report
Current view: top level - src/test - bip32_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 81 82 98.8 %
Date: 2020-09-26 01:30:44 Functions: 42 42 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2013-2020 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             : 
       5             : #include <boost/test/unit_test.hpp>
       6             : 
       7             : #include <clientversion.h>
       8             : #include <key.h>
       9             : #include <key_io.h>
      10             : #include <streams.h>
      11             : #include <test/util/setup_common.h>
      12             : #include <util/strencodings.h>
      13             : 
      14             : #include <string>
      15             : #include <vector>
      16             : 
      17       20292 : struct TestDerivation {
      18             :     std::string pub;
      19             :     std::string prv;
      20             :     unsigned int nChild;
      21             : };
      22             : 
      23        1602 : struct TestVector {
      24             :     std::string strHexMaster;
      25             :     std::vector<TestDerivation> vDerive;
      26             : 
      27         534 :     explicit TestVector(std::string strHexMasterIn) : strHexMaster(strHexMasterIn) {}
      28             : 
      29        1246 :     TestVector& operator()(std::string pub, std::string prv, unsigned int nChild) {
      30        1246 :         vDerive.push_back(TestDerivation());
      31        1246 :         TestDerivation &der = vDerive.back();
      32        1246 :         der.pub = pub;
      33        1246 :         der.prv = prv;
      34        1246 :         der.nChild = nChild;
      35        1246 :         return *this;
      36           0 :     }
      37             : };
      38             : 
      39         178 : TestVector test1 =
      40         623 :   TestVector("000102030405060708090a0b0c0d0e0f")
      41          89 :     ("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8",
      42          89 :      "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
      43             :      0x80000000)
      44          89 :     ("xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw",
      45          89 :      "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
      46             :      1)
      47          89 :     ("xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ",
      48          89 :      "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs",
      49             :      0x80000002)
      50          89 :     ("xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5",
      51          89 :      "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM",
      52             :      2)
      53          89 :     ("xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV",
      54          89 :      "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334",
      55             :      1000000000)
      56          89 :     ("xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy",
      57          89 :      "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
      58             :      0);
      59             : 
      60         178 : TestVector test2 =
      61         623 :   TestVector("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")
      62          89 :     ("xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB",
      63          89 :      "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
      64             :      0)
      65          89 :     ("xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH",
      66          89 :      "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
      67             :      0xFFFFFFFF)
      68          89 :     ("xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a",
      69          89 :      "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9",
      70             :      1)
      71          89 :     ("xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon",
      72          89 :      "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef",
      73             :      0xFFFFFFFE)
      74          89 :     ("xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL",
      75          89 :      "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc",
      76             :      2)
      77          89 :     ("xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt",
      78          89 :      "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
      79             :      0);
      80             : 
      81         178 : TestVector test3 =
      82         267 :   TestVector("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be")
      83          89 :     ("xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13",
      84          89 :      "xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6",
      85             :       0x80000000)
      86          89 :     ("xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y",
      87          89 :      "xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L",
      88             :       0);
      89             : 
      90           3 : static void RunTest(const TestVector &test) {
      91           3 :     std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
      92           3 :     CExtKey key;
      93           3 :     CExtPubKey pubkey;
      94           3 :     key.SetSeed(seed.data(), seed.size());
      95           3 :     pubkey = key.Neuter();
      96          17 :     for (const TestDerivation &derive : test.vDerive) {
      97          14 :         unsigned char data[74];
      98          14 :         key.Encode(data);
      99          14 :         pubkey.Encode(data);
     100             : 
     101             :         // Test private key
     102          14 :         BOOST_CHECK(EncodeExtKey(key) == derive.prv);
     103          14 :         BOOST_CHECK(DecodeExtKey(derive.prv) == key); //ensure a base58 decoded key also matches
     104             : 
     105             :         // Test public key
     106          14 :         BOOST_CHECK(EncodeExtPubKey(pubkey) == derive.pub);
     107          14 :         BOOST_CHECK(DecodeExtPubKey(derive.pub) == pubkey); //ensure a base58 decoded pubkey also matches
     108             : 
     109             :         // Derive new keys
     110          14 :         CExtKey keyNew;
     111          14 :         BOOST_CHECK(key.Derive(keyNew, derive.nChild));
     112          14 :         CExtPubKey pubkeyNew = keyNew.Neuter();
     113          14 :         if (!(derive.nChild & 0x80000000)) {
     114             :             // Compare with public derivation
     115           9 :             CExtPubKey pubkeyNew2;
     116           9 :             BOOST_CHECK(pubkey.Derive(pubkeyNew2, derive.nChild));
     117           9 :             BOOST_CHECK(pubkeyNew == pubkeyNew2);
     118           9 :         }
     119          14 :         key = keyNew;
     120          14 :         pubkey = pubkeyNew;
     121          14 :     }
     122           3 : }
     123             : 
     124          89 : BOOST_FIXTURE_TEST_SUITE(bip32_tests, BasicTestingSetup)
     125             : 
     126          95 : BOOST_AUTO_TEST_CASE(bip32_test1) {
     127           1 :     RunTest(test1);
     128           1 : }
     129             : 
     130          95 : BOOST_AUTO_TEST_CASE(bip32_test2) {
     131           1 :     RunTest(test2);
     132           1 : }
     133             : 
     134          95 : BOOST_AUTO_TEST_CASE(bip32_test3) {
     135           1 :     RunTest(test3);
     136           1 : }
     137             : 
     138          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15