LCOV - code coverage report
Current view: top level - src/script - solver.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 96.0 % 126 121
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 9 9
Branches: 70.2 % 238 167

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <pubkey.h>
       7                 :             : #include <script/interpreter.h>
       8                 :             : #include <script/script.h>
       9                 :             : #include <script/solver.h>
      10                 :             : #include <span.h>
      11                 :             : 
      12                 :             : #include <algorithm>
      13                 :             : #include <cassert>
      14                 :             : #include <string>
      15                 :             : 
      16                 :             : typedef std::vector<unsigned char> valtype;
      17                 :             : 
      18                 :        4980 : std::string GetTxnOutputType(TxoutType t)
      19                 :             : {
      20   [ +  +  +  +  :        4980 :     switch (t) {
          +  +  +  +  +  
                +  +  - ]
      21                 :         453 :     case TxoutType::NONSTANDARD: return "nonstandard";
      22                 :         451 :     case TxoutType::PUBKEY: return "pubkey";
      23                 :         464 :     case TxoutType::PUBKEYHASH: return "pubkeyhash";
      24                 :         456 :     case TxoutType::SCRIPTHASH: return "scripthash";
      25                 :         451 :     case TxoutType::MULTISIG: return "multisig";
      26                 :         452 :     case TxoutType::NULL_DATA: return "nulldata";
      27                 :         450 :     case TxoutType::ANCHOR: return "anchor";
      28                 :         451 :     case TxoutType::WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
      29                 :         452 :     case TxoutType::WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
      30                 :         450 :     case TxoutType::WITNESS_V1_TAPROOT: return "witness_v1_taproot";
      31                 :         450 :     case TxoutType::WITNESS_UNKNOWN: return "witness_unknown";
      32                 :             :     } // no default case, so the compiler can warn about missing cases
      33                 :           0 :     assert(false);
      34                 :             : }
      35                 :             : 
      36                 :        6474 : static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
      37                 :             : {
      38   [ +  +  +  +  :        6474 :     if (script.size() == CPubKey::SIZE + 2 && script[0] == CPubKey::SIZE && script.back() == OP_CHECKSIG) {
             +  -  -  + ]
      39                 :         110 :         pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::SIZE + 1);
      40                 :          55 :         return CPubKey::ValidSize(pubkey);
      41                 :             :     }
      42   [ +  +  +  +  :        6419 :     if (script.size() == CPubKey::COMPRESSED_SIZE + 2 && script[0] == CPubKey::COMPRESSED_SIZE && script.back() == OP_CHECKSIG) {
             +  -  +  - ]
      43                 :        1306 :         pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_SIZE + 1);
      44                 :         653 :         return CPubKey::ValidSize(pubkey);
      45                 :             :     }
      46                 :             :     return false;
      47                 :             : }
      48                 :             : 
      49                 :        5766 : static bool MatchPayToPubkeyHash(const CScript& script, valtype& pubkeyhash)
      50                 :             : {
      51   [ +  +  +  +  :       31863 :     if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 && script[2] == 20 && script[23] == OP_EQUALVERIFY && script[24] == OP_CHECKSIG) {
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
      52                 :       10176 :         pubkeyhash = valtype(script.begin () + 3, script.begin() + 23);
      53                 :        5088 :         return true;
      54                 :             :     }
      55                 :             :     return false;
      56                 :             : }
      57                 :             : 
      58                 :             : /** Test for "small positive integer" script opcodes - OP_1 through OP_16. */
      59                 :         750 : static constexpr bool IsSmallInteger(opcodetype opcode)
      60                 :             : {
      61                 :         750 :     return opcode >= OP_1 && opcode <= OP_16;
      62                 :             : }
      63                 :             : 
      64                 :             : /** Retrieve a minimally-encoded number in range [min,max] from an (opcode, data) pair,
      65                 :             :  *  whether it's OP_n or through a push. */
      66                 :         750 : static std::optional<int> GetScriptNumber(opcodetype opcode, valtype data, int min, int max)
      67                 :             : {
      68                 :         750 :     int count;
      69         [ +  + ]:         750 :     if (IsSmallInteger(opcode)) {
      70                 :         658 :         count = CScript::DecodeOP_N(opcode);
      71         [ +  + ]:          92 :     } else if (IsPushdataOp(opcode)) {
      72         [ +  + ]:          76 :         if (!CheckMinimalPush(data, opcode)) return {};
      73                 :          72 :         try {
      74         [ +  - ]:          72 :             count = CScriptNum(data, /* fRequireMinimal = */ true).getint();
      75         [ -  - ]:           0 :         } catch (const scriptnum_error&) {
      76                 :           0 :             return {};
      77                 :           0 :         }
      78                 :             :     } else {
      79                 :          16 :         return {};
      80                 :             :     }
      81   [ +  +  -  + ]:         730 :     if (count < min || count > max) return {};
      82                 :         728 :     return count;
      83                 :             : }
      84                 :             : 
      85                 :         678 : static bool MatchMultisig(const CScript& script, int& required_sigs, std::vector<valtype>& pubkeys)
      86                 :             : {
      87                 :         678 :     opcodetype opcode;
      88                 :         678 :     valtype data;
      89                 :             : 
      90         [ +  + ]:         678 :     CScript::const_iterator it = script.begin();
      91   [ +  +  +  +  :        2005 :     if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
             +  +  +  + ]
      92                 :             : 
      93   [ +  -  +  - ]:         365 :     if (!script.GetOp(it, opcode, data)) return false;
      94   [ +  -  +  - ]:         365 :     auto req_sigs = GetScriptNumber(opcode, data, 1, MAX_PUBKEYS_PER_MULTISIG);
      95         [ +  + ]:         365 :     if (!req_sigs) return false;
      96                 :         349 :     required_sigs = *req_sigs;
      97   [ +  -  +  -  :        1990 :     while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
                   +  + ]
      98         [ +  - ]:        1641 :         pubkeys.emplace_back(std::move(data));
      99                 :             :     }
     100   [ +  -  +  - ]:         349 :     auto num_keys = GetScriptNumber(opcode, data, required_sigs, MAX_PUBKEYS_PER_MULTISIG);
     101         [ +  + ]:         349 :     if (!num_keys) return false;
     102         [ +  + ]:         343 :     if (pubkeys.size() != static_cast<unsigned long>(*num_keys)) return false;
     103                 :             : 
     104                 :         340 :     return (it + 1 == script.end());
     105                 :         678 : }
     106                 :             : 
     107                 :         116 : std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script)
     108                 :             : {
     109                 :         116 :     std::vector<Span<const unsigned char>> keyspans;
     110                 :             : 
     111                 :             :     // Redundant, but very fast and selective test.
     112   [ +  +  +  -  :         392 :     if (script.size() == 0 || script[0] != 32 || script.back() != OP_NUMEQUAL) return {};
          +  +  +  +  -  
                +  +  + ]
     113                 :             : 
     114                 :             :     // Parse keys
     115                 :          36 :     auto it = script.begin();
     116         [ +  + ]:          96 :     while (script.end() - it >= 34) {
     117         [ -  + ]:          60 :         if (*it != 32) return {};
     118         [ +  - ]:          60 :         ++it;
     119         [ +  - ]:          60 :         keyspans.emplace_back(&*it, 32);
     120         [ +  + ]:          60 :         it += 32;
     121         [ -  + ]:          60 :         if (*it != (keyspans.size() == 1 ? OP_CHECKSIG : OP_CHECKSIGADD)) return {};
     122                 :          60 :         ++it;
     123                 :             :     }
     124   [ +  -  +  - ]:          36 :     if (keyspans.size() == 0 || keyspans.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
     125                 :             : 
     126                 :             :     // Parse threshold.
     127                 :          36 :     opcodetype opcode;
     128                 :          36 :     std::vector<unsigned char> data;
     129   [ +  -  -  + ]:          36 :     if (!script.GetOp(it, opcode, data)) return {};
     130         [ -  + ]:          36 :     if (it == script.end()) return {};
     131         [ -  + ]:          36 :     if (*it != OP_NUMEQUAL) return {};
     132         [ -  + ]:          36 :     ++it;
     133         [ -  + ]:          36 :     if (it != script.end()) return {};
     134   [ +  -  +  - ]:          36 :     auto threshold = GetScriptNumber(opcode, data, 1, (int)keyspans.size());
     135         [ -  + ]:          36 :     if (!threshold) return {};
     136                 :             : 
     137                 :             :     // Construct result.
     138                 :          36 :     return std::pair{*threshold, std::move(keyspans)};
     139                 :         152 : }
     140                 :             : 
     141                 :       24681 : TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
     142                 :             : {
     143                 :       24681 :     vSolutionsRet.clear();
     144                 :             : 
     145                 :             :     // Shortcut for pay-to-script-hash, which are more constrained than the other types:
     146                 :             :     // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
     147         [ +  + ]:       24681 :     if (scriptPubKey.IsPayToScriptHash())
     148                 :             :     {
     149         [ +  - ]:        1010 :         std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
     150         [ +  - ]:         505 :         vSolutionsRet.push_back(hashBytes);
     151                 :         505 :         return TxoutType::SCRIPTHASH;
     152                 :         505 :     }
     153                 :             : 
     154                 :       24176 :     int witnessversion;
     155                 :       24176 :     std::vector<unsigned char> witnessprogram;
     156   [ +  -  +  + ]:       24176 :     if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
     157   [ +  +  +  + ]:       17669 :         if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
     158         [ +  - ]:       16763 :             vSolutionsRet.push_back(std::move(witnessprogram));
     159                 :             :             return TxoutType::WITNESS_V0_KEYHASH;
     160                 :             :         }
     161   [ +  +  +  + ]:         906 :         if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
     162         [ +  - ]:         610 :             vSolutionsRet.push_back(std::move(witnessprogram));
     163                 :             :             return TxoutType::WITNESS_V0_SCRIPTHASH;
     164                 :             :         }
     165   [ +  +  +  + ]:         296 :         if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE) {
     166         [ +  - ]:         245 :             vSolutionsRet.push_back(std::move(witnessprogram));
     167                 :             :             return TxoutType::WITNESS_V1_TAPROOT;
     168                 :             :         }
     169   [ +  -  +  + ]:          51 :         if (scriptPubKey.IsPayToAnchor()) {
     170                 :             :             return TxoutType::ANCHOR;
     171                 :             :         }
     172         [ +  + ]:          47 :         if (witnessversion != 0) {
     173   [ +  -  +  - ]:          90 :             vSolutionsRet.push_back(std::vector<unsigned char>{(unsigned char)witnessversion});
     174         [ +  - ]:       24176 :             vSolutionsRet.push_back(std::move(witnessprogram));
     175                 :             :             return TxoutType::WITNESS_UNKNOWN;
     176                 :             :         }
     177                 :             :         return TxoutType::NONSTANDARD;
     178                 :             :     }
     179                 :             : 
     180                 :             :     // Provably prunable, data-carrying output
     181                 :             :     //
     182                 :             :     // So long as script passes the IsUnspendable() test and all but the first
     183                 :             :     // byte passes the IsPushOnly() test we don't care what exactly is in the
     184                 :             :     // script.
     185   [ +  +  +  +  :       14394 :     if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
          +  +  +  +  +  
                -  +  + ]
     186                 :             :         return TxoutType::NULL_DATA;
     187                 :             :     }
     188                 :             : 
     189                 :        6474 :     std::vector<unsigned char> data;
     190   [ +  -  +  + ]:        6474 :     if (MatchPayToPubkey(scriptPubKey, data)) {
     191         [ +  - ]:         708 :         vSolutionsRet.push_back(std::move(data));
     192                 :             :         return TxoutType::PUBKEY;
     193                 :             :     }
     194                 :             : 
     195   [ +  -  +  + ]:        5766 :     if (MatchPayToPubkeyHash(scriptPubKey, data)) {
     196         [ +  - ]:        6474 :         vSolutionsRet.push_back(std::move(data));
     197                 :             :         return TxoutType::PUBKEYHASH;
     198                 :             :     }
     199                 :             : 
     200                 :         678 :     int required;
     201                 :         678 :     std::vector<std::vector<unsigned char>> keys;
     202   [ +  -  +  + ]:         678 :     if (MatchMultisig(scriptPubKey, required, keys)) {
     203   [ +  -  +  - ]:         680 :         vSolutionsRet.push_back({static_cast<unsigned char>(required)}); // safe as required is in range 1..20
     204         [ +  - ]:         340 :         vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
     205         [ +  - ]:         680 :         vSolutionsRet.push_back({static_cast<unsigned char>(keys.size())}); // safe as size is in range 1..20
     206                 :         340 :         return TxoutType::MULTISIG;
     207                 :             :     }
     208                 :             : 
     209                 :         338 :     vSolutionsRet.clear();
     210                 :         338 :     return TxoutType::NONSTANDARD;
     211                 :       31328 : }
     212                 :             : 
     213                 :        2462 : CScript GetScriptForRawPubKey(const CPubKey& pubKey)
     214                 :             : {
     215   [ +  -  +  - ]:        2462 :     return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
     216                 :             : }
     217                 :             : 
     218                 :         523 : CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
     219                 :             : {
     220                 :         523 :     CScript script;
     221                 :             : 
     222         [ +  - ]:         523 :     script << nRequired;
     223         [ +  + ]:        3119 :     for (const CPubKey& key : keys)
     224         [ +  - ]:        2596 :         script << ToByteVector(key);
     225   [ +  -  +  - ]:         523 :     script << keys.size() << OP_CHECKMULTISIG;
     226                 :             : 
     227                 :         523 :     return script;
     228                 :           0 : }
        

Generated by: LCOV version 2.0-1