LCOV - code coverage report
Current view: top level - src/test - sigopcount_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.2 % 128 127
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 7 7
Branches: 49.8 % 562 280

             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 = {
      91         [ +  - ]:           8 :         CTxIn{COutPoint{}},
      92   [ +  -  +  +  :          24 :     };
                   -  - ]
      93                 :           8 :     creationTx.vout = {
      94                 :           8 :         CTxOut{1, scriptPubKey},
      95   [ +  -  +  +  :          24 :     };
                   -  - ]
      96                 :             : 
      97                 :           8 :     spendingTx.version = 1;
      98                 :           8 :     spendingTx.vin = {
      99                 :           8 :         CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig},
     100   [ +  -  +  +  :          24 :     };
                   -  - ]
     101                 :           8 :     spendingTx.vin[0].scriptWitness = witness;
     102                 :           8 :     spendingTx.vout = {
     103                 :           0 :         CTxOut{1, CScript{}},
     104   [ +  -  +  +  :          24 :     };
                   -  - ]
     105                 :             : 
     106         [ +  - ]:           8 :     AddCoins(coins, CTransaction(creationTx), 0);
     107   [ +  -  +  -  :          40 : }
             +  -  +  - ]
     108                 :             : 
     109   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     110                 :             : {
     111                 :             :     // Transaction creates outputs
     112                 :           1 :     CMutableTransaction creationTx;
     113                 :             :     // Transaction that spends outputs and whose
     114                 :             :     // sig op cost is going to be tested
     115         [ +  - ]:           1 :     CMutableTransaction spendingTx;
     116                 :             : 
     117                 :             :     // Create utxo set
     118   [ +  -  +  - ]:           1 :     CCoinsViewCache coins{&CoinsViewEmpty::Get()};
     119                 :             :     // Create key
     120                 :           1 :     CKey key = GenerateRandomKey();
     121         [ +  - ]:           1 :     CPubKey pubkey = key.GetPubKey();
     122                 :             :     // Default flags
     123                 :           1 :     const script_verify_flags flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
     124                 :             : 
     125                 :             :     // Multisig script (legacy counting)
     126                 :           1 :     {
     127   [ +  -  +  -  :           3 :         CScript scriptPubKey = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     128                 :             :         // Do not use a valid signature to avoid using wallet operations.
     129   [ +  -  +  - ]:           1 :         CScript scriptSig = CScript() << OP_0 << OP_0;
     130                 :             : 
     131         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
     132                 :             :         // Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
     133                 :             :         // of a transaction and does not take the actual executed sig operations into account.
     134                 :             :         // spendingTx in itself does not contain a signature operation.
     135   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
                   -  + ]
     136                 :             :         // creationTx contains two signature operations in its scriptPubKey, but legacy counting
     137                 :             :         // is not accurate.
     138   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(creationTx), coins, flags) == MAX_PUBKEYS_PER_MULTISIG * WITNESS_SCALE_FACTOR);
                   -  + ]
     139                 :             :         // Sanity check: script verification fails because of an invalid signature.
     140   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
                   -  + ]
     141                 :           1 :     }
     142                 :             : 
     143                 :             :     // Multisig nested in P2SH
     144                 :           1 :     {
     145   [ +  -  +  -  :           3 :         CScript redeemScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     146   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     147   [ +  -  +  -  :           2 :         CScript scriptSig = CScript() << OP_0 << OP_0 << ToByteVector(redeemScript);
                   +  - ]
     148                 :             : 
     149         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
     150   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2 * WITNESS_SCALE_FACTOR);
                   -  + ]
     151   [ +  -  +  -  :           1 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
                   -  + ]
     152                 :             : 
     153                 :             :         // P2SH sigops are not counted if we don't set the SCRIPT_VERIFY_P2SH flag
     154   [ +  -  +  -  :           2 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, /*flags=*/0) == 0);
                   -  + ]
     155                 :           1 :     }
     156                 :             : 
     157                 :             :     // P2WPKH witness program
     158                 :           1 :     {
     159   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkey));
     160                 :           1 :         CScript scriptSig = CScript();
     161   [ +  -  +  -  :           3 :         CScriptWitness scriptWitness{{{}, {}}};
             +  +  -  - ]
     162                 :             : 
     163                 :             : 
     164         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     165   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
                   -  + ]
     166                 :             :         // No signature operations if we don't verify the witness.
     167   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
                   -  + ]
     168   [ +  -  +  -  :           1 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
                   -  + ]
     169                 :             : 
     170                 :             :         // The sig op cost for witness version != 0 is zero.
     171   [ -  +  -  + ]:           1 :         assert(scriptPubKey[0] == 0x00);
     172         [ +  - ]:           1 :         scriptPubKey[0] = 0x51;
     173         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     174   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
                   -  + ]
     175         [ -  + ]:           1 :         scriptPubKey[0] = 0x00;
     176         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     177                 :             : 
     178                 :             :         // The witness of a coinbase transaction is not taken into account.
     179                 :           1 :         spendingTx.vin[0].prevout.SetNull();
     180   [ +  -  +  -  :           2 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
             -  +  +  - ]
     181                 :           1 :     }
     182                 :             : 
     183                 :             :     // P2WPKH nested in P2SH
     184                 :           1 :     {
     185   [ +  -  +  - ]:           1 :         CScript scriptSig = GetScriptForDestination(WitnessV0KeyHash(pubkey));
     186   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(ScriptHash(scriptSig));
     187         [ +  - ]:           2 :         scriptSig = CScript() << ToByteVector(scriptSig);
     188   [ +  -  +  -  :           3 :         CScriptWitness scriptWitness{{{}, {}}};
             +  +  -  - ]
     189                 :             : 
     190         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     191   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
                   -  + ]
     192   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
             -  +  +  - ]
     193                 :           1 :     }
     194                 :             : 
     195                 :             :     // P2WSH witness program
     196                 :           1 :     {
     197   [ +  -  +  -  :           3 :         CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     198   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     199                 :           1 :         CScript scriptSig = CScript();
     200   [ +  -  +  -  :           4 :         CScriptWitness scriptWitness{{{}, {}, ToByteVector(witnessScript)}};
             +  +  -  - ]
     201                 :             : 
     202         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     203   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
                   -  + ]
     204   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
                   -  + ]
     205   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
             -  +  +  - ]
     206                 :           1 :     }
     207                 :             : 
     208                 :             :     // P2WSH nested in P2SH
     209                 :           1 :     {
     210   [ +  -  +  -  :           3 :         CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
          +  -  +  -  +  
                      - ]
     211   [ +  -  +  - ]:           1 :         CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
     212   [ +  -  +  - ]:           1 :         CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
     213         [ +  - ]:           2 :         CScript scriptSig = CScript() << ToByteVector(redeemScript);
     214   [ +  -  +  -  :           4 :         CScriptWitness scriptWitness{{{}, {}, ToByteVector(witnessScript)}};
             +  +  -  - ]
     215                 :             : 
     216         [ +  - ]:           1 :         BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
     217   [ +  -  +  -  :           1 :         assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
                   -  + ]
     218   [ +  -  +  -  :           2 :         assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
             -  +  +  - ]
     219                 :           1 :     }
     220   [ +  -  +  -  :           5 : }
             -  -  -  - ]
     221                 :             : 
     222                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1