LCOV - code coverage report
Current view: top level - src/test - sigopcount_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 139 139
Test Date: 2026-04-27 06:44:50 Functions: 100.0 % 7 7
Branches: 50.2 % 504 253

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-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 <addresstype.h>
       6                 :             : #include <coins.h>
       7                 :             : #include <consensus/consensus.h>
       8                 :             : #include <consensus/tx_verify.h>
       9                 :             : #include <key.h>
      10                 :             : #include <pubkey.h>
      11                 :             : #include <script/interpreter.h>
      12                 :             : #include <script/script.h>
      13                 :             : #include <script/solver.h>
      14                 :             : #include <test/util/setup_common.h>
      15                 :             : #include <uint256.h>
      16                 :             : 
      17                 :             : #include <vector>
      18                 :             : 
      19                 :             : #include <boost/test/unit_test.hpp>
      20                 :             : 
      21                 :             : // Helpers:
      22                 :             : static std::vector<unsigned char>
      23                 :           2 : Serialize(const CScript& s)
      24                 :             : {
      25         [ -  + ]:           4 :     std::vector<unsigned char> sSerialized(s.begin(), s.end());
      26                 :           2 :     return sSerialized;
      27                 :             : }
      28                 :             : 
      29                 :             : BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup)
      30                 :             : 
      31   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(GetSigOpCount)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      32                 :             : {
      33                 :             :     // Test CScript::GetSigOpCount()
      34                 :           1 :     CScript s1;
      35   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U);
                   +  - ]
      36   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U);
                   +  - ]
      37                 :             : 
      38                 :           1 :     uint160 dummy;
      39   [ +  -  +  -  :           4 :     s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
                      - ]
      40   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U);
                   +  - ]
      41   [ +  -  +  -  :           1 :     s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
                   +  - ]
      42   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U);
                   +  - ]
      43   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U);
                   +  - ]
      44                 :             : 
      45   [ +  -  +  - ]:           1 :     CScript p2sh = GetScriptForDestination(ScriptHash(s1));
      46                 :           1 :     CScript scriptSig;
      47   [ +  -  +  - ]:           2 :     scriptSig << OP_0 << Serialize(s1);
      48   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U);
                   +  - ]
      49                 :             : 
      50                 :           1 :     std::vector<CPubKey> keys;
      51         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
      52                 :             :     {
      53                 :           3 :         CKey k = GenerateRandomKey();
      54         [ +  - ]:           6 :         keys.push_back(k.GetPubKey());
      55                 :           3 :     }
      56         [ +  - ]:           1 :     CScript s2 = GetScriptForMultisig(1, keys);
      57   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U);
                   +  - ]
      58   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U);
                   +  - ]
      59                 :             : 
      60   [ +  -  +  - ]:           2 :     p2sh = GetScriptForDestination(ScriptHash(s2));
      61   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U);
                   +  - ]
      62   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U);
                   +  - ]
      63                 :           1 :     CScript scriptSig2;
      64   [ +  -  +  -  :           4 :     scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2);
             +  -  +  - ]
      65   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U);
                   +  - ]
      66                 :           1 : }
      67                 :             : 
      68                 :             : /**
      69                 :             :  * Verifies script execution of the zeroth scriptPubKey of tx output and
      70                 :             :  * zeroth scriptSig and witness of tx input.
      71                 :             :  */
      72                 :           6 : static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, script_verify_flags flags)
      73                 :             : {
      74                 :           6 :     ScriptError error;
      75                 :           6 :     CTransaction inputi(input);
      76         [ +  - ]:           6 :     bool ret = VerifyScript(inputi.vin[0].scriptSig, output.vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &error);
      77   [ +  -  +  - ]:          12 :     BOOST_CHECK((ret == true) == (error == SCRIPT_ERR_OK));
      78                 :             : 
      79                 :           6 :     return error;
      80                 :           6 : }
      81                 :             : 
      82                 :             : /**
      83                 :             :  * Builds a creationTx from scriptPubKey and a spendingTx from scriptSig
      84                 :             :  * and witness such that spendingTx spends output zero of creationTx.
      85                 :             :  * Also inserts creationTx's output into the coins view.
      86                 :             :  */
      87                 :           8 : static void BuildTxs(CMutableTransaction& spendingTx, CCoinsViewCache& coins, CMutableTransaction& creationTx, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
      88                 :             : {
      89                 :           8 :     creationTx.version = 1;
      90                 :           8 :     creationTx.vin.resize(1);
      91                 :           8 :     creationTx.vin[0].prevout.SetNull();
      92                 :           8 :     creationTx.vin[0].scriptSig = CScript();
      93                 :           8 :     creationTx.vout.resize(1);
      94                 :           8 :     creationTx.vout[0].nValue = 1;
      95                 :           8 :     creationTx.vout[0].scriptPubKey = scriptPubKey;
      96                 :             : 
      97                 :           8 :     spendingTx.version = 1;
      98                 :           8 :     spendingTx.vin.resize(1);
      99                 :           8 :     spendingTx.vin[0].prevout.hash = creationTx.GetHash();
     100                 :           8 :     spendingTx.vin[0].prevout.n = 0;
     101                 :           8 :     spendingTx.vin[0].scriptSig = scriptSig;
     102                 :           8 :     spendingTx.vin[0].scriptWitness = witness;
     103                 :           8 :     spendingTx.vout.resize(1);
     104                 :           8 :     spendingTx.vout[0].nValue = 1;
     105                 :           8 :     spendingTx.vout[0].scriptPubKey = CScript();
     106                 :             : 
     107         [ +  - ]:           8 :     AddCoins(coins, CTransaction(creationTx), 0);
     108                 :           8 : }
     109                 :             : 
     110   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     111                 :             : {
     112                 :             :     // Transaction creates outputs
     113                 :           1 :     CMutableTransaction creationTx;
     114                 :             :     // Transaction that spends outputs and whose
     115                 :             :     // sig op cost is going to be tested
     116         [ +  - ]:           1 :     CMutableTransaction spendingTx;
     117                 :             : 
     118                 :             :     // Create utxo set
     119   [ +  -  +  - ]:           1 :     CCoinsViewCache coins{&CoinsViewEmpty::Get()};
     120                 :             :     // Create key
     121                 :           1 :     CKey key = GenerateRandomKey();
     122         [ +  - ]:           1 :     CPubKey pubkey = key.GetPubKey();
     123                 :             :     // Default flags
     124                 :           1 :     const script_verify_flags flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
     125                 :             : 
     126                 :             :     // Multisig script (legacy counting)
     127                 :           1 :     {
     128   [ +  -  +  -  :           3 :         CScript scriptPubKey = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     129                 :             :         // Do not use a valid signature to avoid using wallet operations.
     130   [ +  -  +  - ]:           1 :         CScript scriptSig = CScript() << OP_0 << OP_0;
     131                 :             : 
     132         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
     133                 :             :         // Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
     134                 :             :         // of a transaction and does not take the actual executed sig operations into account.
     135                 :             :         // spendingTx in itself does not contain a signature operation.
     136   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
                   -  + ]
     137                 :             :         // creationTx contains two signature operations in its scriptPubKey, but legacy counting
     138                 :             :         // is not accurate.
     139   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(creationTx), coins, flags) == MAX_PUBKEYS_PER_MULTISIG * WITNESS_SCALE_FACTOR);
                   -  + ]
     140                 :             :         // Sanity check: script verification fails because of an invalid signature.
     141   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
                   -  + ]
     142                 :           1 :     }
     143                 :             : 
     144                 :             :     // Multisig nested in P2SH
     145                 :           1 :     {
     146   [ +  -  +  -  :           3 :         CScript redeemScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     147   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     148   [ +  -  +  -  :           2 :         CScript scriptSig = CScript() << OP_0 << OP_0 << ToByteVector(redeemScript);
                   +  - ]
     149                 :             : 
     150         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
     151   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2 * WITNESS_SCALE_FACTOR);
                   -  + ]
     152   [ +  -  +  -  :           1 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
                   -  + ]
     153                 :             : 
     154                 :             :         // P2SH sigops are not counted if we don't set the SCRIPT_VERIFY_P2SH flag
     155   [ +  -  +  -  :           2 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, /*flags=*/0) == 0);
                   -  + ]
     156                 :           1 :     }
     157                 :             : 
     158                 :             :     // P2WPKH witness program
     159                 :           1 :     {
     160   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkey));
     161                 :           1 :         CScript scriptSig = CScript();
     162                 :           1 :         CScriptWitness scriptWitness;
     163         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     164         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     165                 :             : 
     166                 :             : 
     167         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     168   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
                   -  + ]
     169                 :             :         // No signature operations if we don't verify the witness.
     170   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
                   -  + ]
     171   [ +  -  +  -  :           1 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
                   -  + ]
     172                 :             : 
     173                 :             :         // The sig op cost for witness version != 0 is zero.
     174   [ -  +  -  + ]:           1 :         assert(scriptPubKey[0] == 0x00);
     175         [ +  - ]:           1 :         scriptPubKey[0] = 0x51;
     176         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     177   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
                   -  + ]
     178         [ -  + ]:           1 :         scriptPubKey[0] = 0x00;
     179         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     180                 :             : 
     181                 :             :         // The witness of a coinbase transaction is not taken into account.
     182                 :           1 :         spendingTx.vin[0].prevout.SetNull();
     183   [ +  -  +  -  :           2 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
                   -  + ]
     184                 :           1 :     }
     185                 :             : 
     186                 :             :     // P2WPKH nested in P2SH
     187                 :           1 :     {
     188   [ +  -  +  - ]:           1 :         CScript scriptSig = GetScriptForDestination(WitnessV0KeyHash(pubkey));
     189   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(ScriptHash(scriptSig));
     190         [ +  - ]:           2 :         scriptSig = CScript() << ToByteVector(scriptSig);
     191                 :           1 :         CScriptWitness scriptWitness;
     192         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     193         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     194                 :             : 
     195         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     196   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
                   -  + ]
     197   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
                   -  + ]
     198                 :           1 :     }
     199                 :             : 
     200                 :             :     // P2WSH witness program
     201                 :           1 :     {
     202   [ +  -  +  -  :           3 :         CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     203   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     204                 :           1 :         CScript scriptSig = CScript();
     205                 :           1 :         CScriptWitness scriptWitness;
     206         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     207         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     208   [ +  -  +  - ]:           2 :         scriptWitness.stack.emplace_back(witnessScript.begin(), witnessScript.end());
     209                 :             : 
     210         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     211   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
                   -  + ]
     212   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
                   -  + ]
     213   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
                   -  + ]
     214                 :           1 :     }
     215                 :             : 
     216                 :             :     // P2WSH nested in P2SH
     217                 :           1 :     {
     218   [ +  -  +  -  :           3 :         CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     219   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     220   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     221         [ +  - ]:           2 :         CScript scriptSig = CScript() << ToByteVector(redeemScript);
     222                 :           1 :         CScriptWitness scriptWitness;
     223         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     224         [ +  - ]:           1 :         scriptWitness.stack.emplace_back(0);
     225   [ +  -  +  - ]:           2 :         scriptWitness.stack.emplace_back(witnessScript.begin(), witnessScript.end());
     226                 :             : 
     227         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     228   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
                   -  + ]
     229   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
                   -  + ]
     230                 :           1 :     }
     231                 :           3 : }
     232                 :             : 
     233                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1