LCOV - code coverage report
Current view: top level - src/test - multisig_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.3 % 142 141
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 8 8
Branches: 51.7 % 810 419

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-present 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 <key.h>
       6                 :             : #include <policy/policy.h>
       7                 :             : #include <script/interpreter.h>
       8                 :             : #include <script/script.h>
       9                 :             : #include <script/script_error.h>
      10                 :             : #include <script/sign.h>
      11                 :             : #include <script/signingprovider.h>
      12                 :             : #include <test/util/common.h>
      13                 :             : #include <test/util/setup_common.h>
      14                 :             : #include <test/util/transaction_utils.h>
      15                 :             : #include <tinyformat.h>
      16                 :             : #include <uint256.h>
      17                 :             : 
      18                 :             : 
      19                 :             : #include <boost/test/unit_test.hpp>
      20                 :             : 
      21                 :             : BOOST_FIXTURE_TEST_SUITE(multisig_tests, BasicTestingSetup)
      22                 :             : 
      23                 :             : static CScript
      24                 :          29 : sign_multisig(const CScript& scriptPubKey, const std::vector<CKey>& keys, const CTransaction& transaction, int whichIn)
      25                 :             : {
      26                 :          29 :     uint256 hash = SignatureHash(scriptPubKey, transaction, whichIn, SIGHASH_ALL, 0, SigVersion::BASE);
      27                 :             : 
      28                 :          29 :     CScript result;
      29         [ +  - ]:          29 :     result << OP_0; // CHECKMULTISIG bug workaround
      30         [ +  + ]:          79 :     for (const CKey &key : keys)
      31                 :             :     {
      32                 :          50 :         std::vector<unsigned char> vchSig;
      33   [ +  -  +  -  :         100 :         BOOST_CHECK(key.Sign(hash, vchSig));
             +  -  +  - ]
      34                 :          50 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
      35         [ -  + ]:          50 :         result << vchSig;
      36                 :          50 :     }
      37                 :          29 :     return result;
      38                 :           0 : }
      39                 :             : 
      40   [ +  -  +  -  :          10 : BOOST_AUTO_TEST_CASE(multisig_verify)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  +  - ]
      41                 :             : {
      42                 :           1 :     script_verify_flags flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
      43                 :             : 
      44                 :           1 :     ScriptError err;
      45                 :           4 :     CKey key[4];
      46                 :           1 :     CAmount amount = 0;
      47         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
      48         [ +  - ]:           4 :         key[i].MakeNewKey(true);
      49                 :             : 
      50                 :           1 :     CScript a_and_b;
      51   [ +  -  +  -  :           4 :     a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
      52                 :             : 
      53                 :           1 :     CScript a_or_b;
      54   [ +  -  +  -  :           4 :     a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
      55                 :             : 
      56                 :           1 :     CScript escrow;
      57   [ +  -  +  -  :           5 :     escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      58                 :             : 
      59         [ +  - ]:           1 :     CMutableTransaction txFrom;  // Funding transaction
      60                 :           1 :     txFrom.vout = {
      61                 :           1 :         CTxOut{-1, a_and_b},
      62                 :           2 :         CTxOut{-1, a_or_b},
      63                 :           2 :         CTxOut{-1, escrow},
      64   [ -  +  +  +  :           4 :     };
                   -  - ]
      65                 :             : 
      66   [ +  -  +  +  :          10 :     CMutableTransaction txTo[3]; // Spending transaction
                   -  - ]
      67         [ +  + ]:           4 :     for (uint32_t i = 0; i < 3; i++)
      68                 :             :     {
      69                 :           3 :         txTo[i].vin = {
      70         [ +  - ]:           3 :             CTxIn{COutPoint{txFrom.GetHash(), i}},
      71   [ -  +  +  +  :           6 :         };
                   -  - ]
      72                 :           3 :         txTo[i].vout = {
      73                 :           3 :             CTxOut{1, CScript{}},
      74   [ -  +  +  +  :           6 :         };
                   -  - ]
      75                 :             :     }
      76                 :             : 
      77                 :           1 :     std::vector<CKey> keys;
      78                 :           1 :     CScript s;
      79                 :             : 
      80                 :             :     // Test a AND b:
      81         [ +  - ]:           1 :     keys.assign(1,key[0]);
      82         [ +  - ]:           1 :     keys.push_back(key[1]);
      83   [ +  -  +  - ]:           2 :     s = sign_multisig(a_and_b, keys, CTransaction(txTo[0]), 0);
      84   [ +  -  +  -  :           2 :     BOOST_CHECK(VerifyScript(s, a_and_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[0], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
      85   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
      86                 :             : 
      87         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
      88                 :             :     {
      89         [ +  - ]:           4 :         keys.assign(1,key[i]);
      90   [ +  -  +  - ]:           8 :         s = sign_multisig(a_and_b, keys, CTransaction(txTo[0]), 0);
      91   [ +  -  +  -  :           8 :         BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[0], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err), strprintf("a&b 1: %d", i));
             +  -  +  - ]
      92   [ +  -  +  -  :           8 :         BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_INVALID_STACK_OPERATION, ScriptErrorString(err));
                   +  - ]
      93                 :             : 
      94         [ +  - ]:           4 :         keys.assign(1,key[1]);
      95         [ +  - ]:           4 :         keys.push_back(key[i]);
      96   [ +  -  +  - ]:           8 :         s = sign_multisig(a_and_b, keys, CTransaction(txTo[0]), 0);
      97   [ +  -  +  -  :           8 :         BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[0], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err), strprintf("a&b 2: %d", i));
             +  -  +  - ]
      98   [ +  -  +  -  :           8 :         BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
      99                 :             :     }
     100                 :             : 
     101                 :             :     // Test a OR b:
     102         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     103                 :             :     {
     104         [ +  - ]:           4 :         keys.assign(1,key[i]);
     105   [ +  -  +  - ]:           8 :         s = sign_multisig(a_or_b, keys, CTransaction(txTo[1]), 0);
     106         [ +  + ]:           4 :         if (i == 0 || i == 1)
     107                 :             :         {
     108   [ +  -  +  -  :           4 :             BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[1], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err), strprintf("a|b: %d", i));
             +  -  +  - ]
     109   [ +  -  +  -  :           4 :             BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
     110                 :             :         }
     111                 :             :         else
     112                 :             :         {
     113   [ +  -  +  -  :           4 :             BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[1], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err), strprintf("a|b: %d", i));
             +  -  +  - ]
     114   [ +  -  +  -  :           6 :             BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
     115                 :             :         }
     116                 :             :     }
     117                 :           1 :     s.clear();
     118   [ +  -  +  - ]:           1 :     s << OP_0 << OP_1;
     119   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(s, a_or_b, nullptr, flags, MutableTransactionSignatureChecker(&txTo[1], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
     120   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_SIG_DER, ScriptErrorString(err));
                   +  - ]
     121                 :             : 
     122                 :             : 
     123         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     124         [ +  + ]:          20 :         for (int j = 0; j < 4; j++)
     125                 :             :         {
     126         [ +  - ]:          16 :             keys.assign(1,key[i]);
     127         [ +  - ]:          16 :             keys.push_back(key[j]);
     128   [ +  -  +  - ]:          32 :             s = sign_multisig(escrow, keys, CTransaction(txTo[2]), 0);
     129   [ +  +  +  -  :          16 :             if (i < j && i < 3 && j < 3)
                   +  + ]
     130                 :             :             {
     131   [ +  -  +  -  :           6 :                 BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, nullptr, flags, MutableTransactionSignatureChecker(&txTo[2], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err), strprintf("escrow 1: %d %d", i, j));
             +  -  +  - ]
     132   [ +  -  +  -  :           6 :                 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
     133                 :             :             }
     134                 :             :             else
     135                 :             :             {
     136   [ +  -  +  -  :          26 :                 BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, nullptr, flags, MutableTransactionSignatureChecker(&txTo[2], 0, amount, MissingDataBehavior::ASSERT_FAIL), &err), strprintf("escrow 2: %d %d", i, j));
             +  -  +  - ]
     137   [ +  -  +  -  :          29 :                 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
     138                 :             :             }
     139                 :             :         }
     140   [ +  -  +  -  :          19 : }
          +  -  +  -  +  
          -  +  +  +  +  
          -  -  -  -  -  
                      - ]
     141                 :             : 
     142   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(multisig_IsStandard)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     143                 :             : {
     144                 :           4 :     CKey key[4];
     145         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     146         [ +  - ]:           4 :         key[i].MakeNewKey(true);
     147                 :             : 
     148                 :          11 :     const auto is_standard{[](const CScript& spk) {
     149                 :          10 :         TxoutType type;
     150                 :          10 :         bool res{::IsStandard(spk, type)};
     151         [ +  + ]:          10 :         if (res) {
     152         [ +  - ]:           3 :             BOOST_CHECK_EQUAL(type, TxoutType::MULTISIG);
     153                 :             :         }
     154                 :          10 :         return res;
     155                 :             :     }};
     156                 :             : 
     157                 :           1 :     CScript a_and_b;
     158   [ +  -  +  -  :           4 :     a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     159   [ +  -  +  -  :           2 :     BOOST_CHECK(is_standard(a_and_b));
             +  -  +  - ]
     160                 :             : 
     161                 :           1 :     CScript a_or_b;
     162   [ +  -  +  -  :           4 :     a_or_b  << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     163   [ +  -  +  -  :           2 :     BOOST_CHECK(is_standard(a_or_b));
             +  -  +  - ]
     164                 :             : 
     165                 :           1 :     CScript escrow;
     166   [ +  -  +  -  :           5 :     escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     167   [ +  -  +  -  :           2 :     BOOST_CHECK(is_standard(escrow));
             +  -  +  - ]
     168                 :             : 
     169                 :           1 :     CScript one_of_four;
     170   [ +  -  +  -  :           6 :     one_of_four << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << ToByteVector(key[3].GetPubKey()) << OP_4 << OP_CHECKMULTISIG;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     171   [ +  -  +  -  :           2 :     BOOST_CHECK(!is_standard(one_of_four));
             +  -  +  - ]
     172                 :             : 
     173                 :           6 :     CScript malformed[6];
     174   [ +  -  +  -  :           4 :     malformed[0] << OP_3 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     175   [ +  -  +  -  :           4 :     malformed[1] << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     176   [ +  -  +  -  :           4 :     malformed[2] << OP_0 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     177   [ +  -  +  -  :           4 :     malformed[3] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_0 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     178   [ +  -  +  -  :           4 :     malformed[4] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_CHECKMULTISIG;
          +  -  +  -  +  
                -  +  - ]
     179   [ +  -  +  -  :           3 :     malformed[5] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey());
          +  -  +  -  +  
                      - ]
     180                 :             : 
     181         [ +  + ]:           7 :     for (int i = 0; i < 6; i++) {
     182   [ +  -  +  -  :          12 :         BOOST_CHECK(!is_standard(malformed[i]));
                   +  - ]
     183                 :             :     }
     184   [ +  +  +  +  :          13 : }
             -  -  -  - ]
     185                 :             : 
     186   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(multisig_Sign)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     187                 :             : {
     188                 :             :     // Test SignSignature() (and therefore the version of Solver() that signs transactions)
     189                 :           1 :     FillableSigningProvider keystore;
     190                 :           4 :     CKey key[4];
     191         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     192                 :             :     {
     193         [ +  - ]:           4 :         key[i].MakeNewKey(true);
     194   [ +  -  +  -  :           8 :         BOOST_CHECK(keystore.AddKey(key[i]));
                   +  - ]
     195                 :             :     }
     196                 :             : 
     197                 :           1 :     CScript a_and_b;
     198   [ +  -  +  -  :           4 :     a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     199                 :             : 
     200                 :           1 :     CScript a_or_b;
     201   [ +  -  +  -  :           4 :     a_or_b  << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     202                 :             : 
     203                 :           1 :     CScript escrow;
     204   [ +  -  +  -  :           5 :     escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     205                 :             : 
     206         [ +  - ]:           1 :     CMutableTransaction txFrom;  // Funding transaction
     207                 :           1 :     txFrom.vout = {
     208                 :           1 :         CTxOut{-1, a_and_b},
     209                 :           2 :         CTxOut{-1, a_or_b},
     210                 :           2 :         CTxOut{-1, escrow},
     211   [ -  +  +  +  :           4 :     };
                   -  - ]
     212                 :             : 
     213   [ +  -  +  +  :          10 :     CMutableTransaction txTo[3]; // Spending transaction
                   -  - ]
     214         [ +  + ]:           4 :     for (uint32_t i = 0; i < 3; i++)
     215                 :             :     {
     216                 :           3 :         txTo[i].vin = {
     217         [ +  - ]:           3 :             CTxIn{COutPoint{txFrom.GetHash(), i}},
     218   [ -  +  +  +  :           6 :         };
                   -  - ]
     219                 :           3 :         txTo[i].vout = {
     220                 :           3 :             CTxOut{1, CScript{}},
     221   [ -  +  +  +  :           6 :         };
                   -  - ]
     222                 :             :     }
     223                 :             : 
     224         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
     225                 :             :     {
     226                 :           3 :         SignatureData empty;
     227   [ +  -  +  -  :           9 :         BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), txTo[i], 0, SIGHASH_ALL, empty), strprintf("SignSignature %d", i));
          +  -  +  -  +  
                      - ]
     228                 :           3 :     }
     229   [ +  -  +  -  :          19 : }
          +  -  +  -  +  
          -  +  +  +  +  
          -  -  -  -  -  
                      - ]
     230                 :             : 
     231                 :             : 
     232                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1