LCOV - code coverage report
Current view: top level - src/test - sigopcount_tests.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 138 0.0 %
Date: 2023-10-05 12:38:51 Functions: 0 17 0.0 %
Branches: 0 0 -

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

Generated by: LCOV version 1.14