LCOV - code coverage report
Current view: top level - src/test - script_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 87.3 % 1224 1068
Test Date: 2026-10-11 07:59:32 Functions: 100.0 % 75 75
Branches: 50.5 % 6796 3429

             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 <common/system.h>
       6                 :             : #include <compressor.h>
       7                 :             : #include <core_io.h>
       8                 :             : #include <key.h>
       9                 :             : #include <rpc/util.h>
      10                 :             : #include <script/interpreter.h>
      11                 :             : #include <script/script.h>
      12                 :             : #include <script/script_error.h>
      13                 :             : #include <script/sigcache.h>
      14                 :             : #include <script/sign.h>
      15                 :             : #include <script/signingprovider.h>
      16                 :             : #include <script/solver.h>
      17                 :             : #include <secp256k1.h>
      18                 :             : #include <streams.h>
      19                 :             : #include <test/data/bip341_wallet_vectors.json.h>
      20                 :             : #include <test/data/script_tests.json.h>
      21                 :             : #include <test/util/common.h>
      22                 :             : #include <test/util/json.h>
      23                 :             : #include <test/util/random.h>
      24                 :             : #include <test/util/setup_common.h>
      25                 :             : #include <test/util/transaction_utils.h>
      26                 :             : #include <univalue.h>
      27                 :             : #include <util/check.h>
      28                 :             : #include <util/fs.h>
      29                 :             : #include <util/strencodings.h>
      30                 :             : #include <util/string.h>
      31                 :             : 
      32                 :             : #include <boost/test/unit_test.hpp>
      33                 :             : 
      34                 :             : #include <cstdint>
      35                 :             : #include <string>
      36                 :             : #include <vector>
      37                 :             : 
      38                 :             : // Uncomment if you want to output updated JSON tests.
      39                 :             : // #define UPDATE_JSON_TESTS
      40                 :             : 
      41                 :             : using namespace util::hex_literals;
      42                 :             : 
      43                 :             : static const script_verify_flags gFlags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
      44                 :             : 
      45                 :             : script_verify_flags ParseScriptFlags(std::string strFlags);
      46                 :             : 
      47                 :             : struct ScriptErrorDesc
      48                 :             : {
      49                 :             :     ScriptError_t err;
      50                 :             :     const char *name;
      51                 :             : };
      52                 :             : 
      53                 :             : static ScriptErrorDesc script_errors[]={
      54                 :             :     {SCRIPT_ERR_OK, "OK"},
      55                 :             :     {SCRIPT_ERR_EVAL_FALSE, "EVAL_FALSE"},
      56                 :             :     {SCRIPT_ERR_OP_RETURN, "OP_RETURN"},
      57                 :             :     {SCRIPT_ERR_SCRIPT_SIZE, "SCRIPT_SIZE"},
      58                 :             :     {SCRIPT_ERR_PUSH_SIZE, "PUSH_SIZE"},
      59                 :             :     {SCRIPT_ERR_OP_COUNT, "OP_COUNT"},
      60                 :             :     {SCRIPT_ERR_STACK_SIZE, "STACK_SIZE"},
      61                 :             :     {SCRIPT_ERR_SIG_COUNT, "SIG_COUNT"},
      62                 :             :     {SCRIPT_ERR_PUBKEY_COUNT, "PUBKEY_COUNT"},
      63                 :             :     {SCRIPT_ERR_VERIFY, "VERIFY"},
      64                 :             :     {SCRIPT_ERR_EQUALVERIFY, "EQUALVERIFY"},
      65                 :             :     {SCRIPT_ERR_CHECKMULTISIGVERIFY, "CHECKMULTISIGVERIFY"},
      66                 :             :     {SCRIPT_ERR_CHECKSIGVERIFY, "CHECKSIGVERIFY"},
      67                 :             :     {SCRIPT_ERR_NUMEQUALVERIFY, "NUMEQUALVERIFY"},
      68                 :             :     {SCRIPT_ERR_BAD_OPCODE, "BAD_OPCODE"},
      69                 :             :     {SCRIPT_ERR_DISABLED_OPCODE, "DISABLED_OPCODE"},
      70                 :             :     {SCRIPT_ERR_INVALID_STACK_OPERATION, "INVALID_STACK_OPERATION"},
      71                 :             :     {SCRIPT_ERR_INVALID_ALTSTACK_OPERATION, "INVALID_ALTSTACK_OPERATION"},
      72                 :             :     {SCRIPT_ERR_UNBALANCED_CONDITIONAL, "UNBALANCED_CONDITIONAL"},
      73                 :             :     {SCRIPT_ERR_NEGATIVE_LOCKTIME, "NEGATIVE_LOCKTIME"},
      74                 :             :     {SCRIPT_ERR_UNSATISFIED_LOCKTIME, "UNSATISFIED_LOCKTIME"},
      75                 :             :     {SCRIPT_ERR_SIG_HASHTYPE, "SIG_HASHTYPE"},
      76                 :             :     {SCRIPT_ERR_SIG_DER, "SIG_DER"},
      77                 :             :     {SCRIPT_ERR_MINIMALDATA, "MINIMALDATA"},
      78                 :             :     {SCRIPT_ERR_SIG_PUSHONLY, "SIG_PUSHONLY"},
      79                 :             :     {SCRIPT_ERR_SIG_HIGH_S, "SIG_HIGH_S"},
      80                 :             :     {SCRIPT_ERR_SIG_NULLDUMMY, "SIG_NULLDUMMY"},
      81                 :             :     {SCRIPT_ERR_PUBKEYTYPE, "PUBKEYTYPE"},
      82                 :             :     {SCRIPT_ERR_CLEANSTACK, "CLEANSTACK"},
      83                 :             :     {SCRIPT_ERR_MINIMALIF, "MINIMALIF"},
      84                 :             :     {SCRIPT_ERR_SIG_NULLFAIL, "NULLFAIL"},
      85                 :             :     {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, "DISCOURAGE_UPGRADABLE_NOPS"},
      86                 :             :     {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, "DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"},
      87                 :             :     {SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH, "WITNESS_PROGRAM_WRONG_LENGTH"},
      88                 :             :     {SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY, "WITNESS_PROGRAM_WITNESS_EMPTY"},
      89                 :             :     {SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH, "WITNESS_PROGRAM_MISMATCH"},
      90                 :             :     {SCRIPT_ERR_WITNESS_MALLEATED, "WITNESS_MALLEATED"},
      91                 :             :     {SCRIPT_ERR_WITNESS_MALLEATED_P2SH, "WITNESS_MALLEATED_P2SH"},
      92                 :             :     {SCRIPT_ERR_WITNESS_UNEXPECTED, "WITNESS_UNEXPECTED"},
      93                 :             :     {SCRIPT_ERR_WITNESS_PUBKEYTYPE, "WITNESS_PUBKEYTYPE"},
      94                 :             :     {SCRIPT_ERR_TAPSCRIPT_EMPTY_PUBKEY, "TAPSCRIPT_EMPTY_PUBKEY"},
      95                 :             :     {SCRIPT_ERR_TAPSCRIPT_MINIMALIF, "TAPSCRIPT_MINIMALIF"},
      96                 :             :     {SCRIPT_ERR_OP_CODESEPARATOR, "OP_CODESEPARATOR"},
      97                 :             :     {SCRIPT_ERR_SIG_FINDANDDELETE, "SIG_FINDANDDELETE"},
      98                 :             :     {SCRIPT_ERR_SCRIPTNUM, "SCRIPTNUM"}
      99                 :             : };
     100                 :             : 
     101                 :         140 : static std::string FormatScriptFlags(script_verify_flags flags)
     102                 :             : {
     103         [ +  - ]:         140 :     return util::Join(GetScriptFlagNames(flags), ",");
     104                 :             : }
     105                 :             : 
     106                 :        2918 : static std::string FormatScriptError(ScriptError_t err)
     107                 :             : {
     108         [ +  - ]:       29290 :     for (const auto& se : script_errors)
     109         [ +  + ]:       29290 :         if (se.err == err)
     110                 :        2918 :             return se.name;
     111         [ #  # ]:           0 :     BOOST_ERROR("Unknown scripterror enumeration value, update script_errors in script_tests.cpp.");
     112                 :           0 :     return "";
     113                 :             : }
     114                 :             : 
     115                 :        1258 : static ScriptError_t ParseScriptError(const std::string& name)
     116                 :             : {
     117         [ +  - ]:       12380 :     for (const auto& se : script_errors)
     118         [ +  + ]:       12380 :         if (se.name == name)
     119                 :        1258 :             return se.err;
     120         [ #  # ]:           0 :     BOOST_ERROR("Unknown scripterror \"" << name << "\" in test description");
     121                 :           0 :     return SCRIPT_ERR_UNKNOWN_ERROR;
     122                 :             : }
     123                 :             : 
     124                 :          42 : struct ScriptTest : BasicTestingSetup {
     125                 :        1392 : void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, script_verify_flags flags, const std::string& message, int scriptError, CAmount nValue = 0)
     126                 :             : {
     127                 :        1392 :     bool expect = (scriptError == SCRIPT_ERR_OK);
     128         [ +  + ]:        1392 :     if (flags & SCRIPT_VERIFY_CLEANSTACK) {
     129                 :           6 :         flags |= SCRIPT_VERIFY_P2SH;
     130                 :           6 :         flags |= SCRIPT_VERIFY_WITNESS;
     131                 :             :     }
     132                 :        1392 :     ScriptError err;
     133         [ +  - ]:        1392 :     const CTransaction txCredit{BuildCreditingTransaction(scriptPubKey, nValue)};
     134         [ +  - ]:        1392 :     CMutableTransaction tx = BuildSpendingTransaction(scriptSig, scriptWitness, txCredit);
     135   [ +  -  +  -  :        2784 :     BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message);
             +  -  +  - ]
     136   [ +  -  +  -  :        5568 :     BOOST_CHECK_MESSAGE(err == scriptError, FormatScriptError(err) + " where " + FormatScriptError((ScriptError_t)scriptError) + " expected: " + message);
          +  -  +  -  +  
                -  +  - ]
     137                 :             : 
     138                 :             :     // Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
     139         [ +  + ]:      357744 :     for (int i = 0; i < 256; ++i) {
     140         [ +  + ]:      356352 :         script_verify_flags extra_flags = script_verify_flags::from_int(m_rng.randbits(MAX_SCRIPT_VERIFY_FLAGS_BITS));
     141         [ +  + ]:      356352 :         script_verify_flags combined_flags{expect ? (flags & ~extra_flags) : (flags | extra_flags)};
     142                 :             :         // Weed out some invalid flag combinations.
     143   [ +  +  +  + ]:      356352 :         if (combined_flags & SCRIPT_VERIFY_CLEANSTACK && ~combined_flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) continue;
     144   [ +  +  +  + ]:      315924 :         if (combined_flags & SCRIPT_VERIFY_WITNESS && ~combined_flags & SCRIPT_VERIFY_P2SH) continue;
     145   [ +  -  +  -  :      661205 :         BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, combined_flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message + strprintf(" (with flags %x)", combined_flags.as_int()));
          +  -  +  -  +  
                      - ]
     146                 :             :     }
     147                 :        2784 : }
     148                 :             : }; // struct ScriptTest
     149                 :             : 
     150                 :           2 : void static NegateSignatureS(std::vector<unsigned char>& vchSig) {
     151                 :             :     // Parse the signature.
     152                 :           2 :     std::vector<unsigned char> r, s;
     153   [ +  -  +  - ]:           4 :     r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
     154         [ +  - ]:           4 :     s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
     155                 :             : 
     156   [ -  +  +  + ]:           4 :     while (s.size() < 33) {
     157         [ +  - ]:           2 :         s.insert(s.begin(), 0x00);
     158                 :             :     }
     159         [ -  + ]:           2 :     assert(s[0] == 0);
     160                 :             :     // Perform mod-n negation of s by (ab)using libsecp256k1
     161                 :             :     // (note that this function is meant to be used for negating secret keys,
     162                 :             :     //  but it works for any non-zero scalar modulo the group order, i.e. also for s)
     163         [ +  - ]:           2 :     int ret = secp256k1_ec_seckey_negate(secp256k1_context_static, s.data() + 1);
     164         [ -  + ]:           2 :     assert(ret);
     165                 :             : 
     166         [ -  + ]:           2 :     if (s[1] < 0x80) {
     167                 :           0 :         s.erase(s.begin());
     168                 :             :     }
     169                 :             : 
     170                 :             :     // Reconstruct the signature.
     171         [ +  - ]:           2 :     vchSig.clear();
     172         [ +  - ]:           2 :     vchSig.push_back(0x30);
     173   [ -  +  -  +  :           2 :     vchSig.push_back(4 + r.size() + s.size());
                   +  - ]
     174         [ +  - ]:           2 :     vchSig.push_back(0x02);
     175   [ -  +  +  - ]:           2 :     vchSig.push_back(r.size());
     176         [ +  - ]:           2 :     vchSig.insert(vchSig.end(), r.begin(), r.end());
     177         [ +  - ]:           2 :     vchSig.push_back(0x02);
     178   [ -  +  +  - ]:           2 :     vchSig.push_back(s.size());
     179         [ +  - ]:           2 :     vchSig.insert(vchSig.end(), s.begin(), s.end());
     180                 :           2 : }
     181                 :             : 
     182                 :             : namespace
     183                 :             : {
     184                 :             : const unsigned char vchKey0[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
     185                 :             : const unsigned char vchKey1[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
     186                 :             : const unsigned char vchKey2[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0};
     187                 :             : 
     188                 :             : struct KeyData
     189                 :             : {
     190                 :             :     CKey key0, key0C, key1, key1C, key2, key2C;
     191                 :             :     CPubKey pubkey0, pubkey0C, pubkey0H;
     192                 :             :     CPubKey pubkey1, pubkey1C;
     193                 :             :     CPubKey pubkey2, pubkey2C;
     194                 :             : 
     195                 :           2 :     KeyData()
     196         [ +  - ]:           2 :     {
     197         [ +  - ]:           2 :         key0.Set(vchKey0, vchKey0 + 32, false);
     198         [ +  - ]:           2 :         key0C.Set(vchKey0, vchKey0 + 32, true);
     199         [ +  - ]:           2 :         pubkey0 = key0.GetPubKey();
     200         [ +  - ]:           2 :         pubkey0H = key0.GetPubKey();
     201         [ +  - ]:           2 :         pubkey0C = key0C.GetPubKey();
     202         [ +  - ]:           2 :         *const_cast<unsigned char*>(pubkey0H.data()) = 0x06 | (pubkey0H[64] & 1);
     203                 :             : 
     204         [ +  - ]:           2 :         key1.Set(vchKey1, vchKey1 + 32, false);
     205         [ +  - ]:           2 :         key1C.Set(vchKey1, vchKey1 + 32, true);
     206         [ +  - ]:           2 :         pubkey1 = key1.GetPubKey();
     207         [ +  - ]:           2 :         pubkey1C = key1C.GetPubKey();
     208                 :             : 
     209         [ +  - ]:           2 :         key2.Set(vchKey2, vchKey2 + 32, false);
     210         [ +  - ]:           2 :         key2C.Set(vchKey2, vchKey2 + 32, true);
     211         [ +  - ]:           2 :         pubkey2 = key2.GetPubKey();
     212         [ +  - ]:           2 :         pubkey2C = key2C.GetPubKey();
     213                 :           2 :     }
     214                 :             : };
     215                 :             : 
     216                 :             : enum class WitnessMode {
     217                 :             :     NONE,
     218                 :             :     PKH,
     219                 :             :     SH
     220                 :             : };
     221                 :             : 
     222                 :             : class TestBuilder
     223                 :             : {
     224                 :             : private:
     225                 :             :     //! Actually executed script
     226                 :             :     CScript script;
     227                 :             :     //! The P2SH redeemscript
     228                 :             :     CScript redeemscript;
     229                 :             :     //! The Witness embedded script
     230                 :             :     CScript witscript;
     231                 :             :     CScriptWitness scriptWitness;
     232                 :             :     CTransactionRef creditTx;
     233                 :             :     CMutableTransaction spendTx;
     234                 :             :     bool havePush{false};
     235                 :             :     std::vector<unsigned char> push;
     236                 :             :     std::string comment;
     237                 :             :     script_verify_flags flags;
     238                 :             :     int scriptError{SCRIPT_ERR_OK};
     239                 :             :     CAmount nValue;
     240                 :             : 
     241                 :         577 :     void DoPush()
     242                 :             :     {
     243         [ +  + ]:         577 :         if (havePush) {
     244         [ -  + ]:         220 :             spendTx.vin[0].scriptSig << push;
     245                 :         220 :             havePush = false;
     246                 :             :         }
     247                 :         577 :     }
     248                 :             : 
     249                 :         253 :     void DoPush(const std::vector<unsigned char>& data)
     250                 :             :     {
     251                 :         253 :         DoPush();
     252                 :         253 :         push = data;
     253                 :         253 :         havePush = true;
     254                 :         253 :     }
     255                 :             : 
     256                 :             : public:
     257   [ +  -  -  + ]:         134 :     TestBuilder(const CScript& script_, const std::string& comment_, script_verify_flags flags_, bool P2SH = false, WitnessMode wm = WitnessMode::NONE, int witnessversion = 0, CAmount nValue_ = 0) : script(script_), comment(comment_), flags(flags_), nValue(nValue_)
     258                 :             :     {
     259                 :         134 :         CScript scriptPubKey = script;
     260         [ +  + ]:         134 :         if (wm == WitnessMode::PKH) {
     261                 :          16 :             uint160 hash;
     262   [ +  -  +  +  :          32 :             CHash160().Write(std::span{script}.subspan(1)).Finalize(hash);
             +  -  +  - ]
     263   [ +  -  +  -  :          32 :             script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG;
          +  -  +  -  +  
                      - ]
     264   [ +  -  +  - ]:          32 :             scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
     265         [ +  + ]:         118 :         } else if (wm == WitnessMode::SH) {
     266                 :          34 :             witscript = scriptPubKey;
     267                 :          34 :             uint256 hash;
     268   [ +  -  +  +  :         100 :             CSHA256().Write(witscript.data(), witscript.size()).Finalize(hash.begin());
          +  +  +  -  +  
                      - ]
     269   [ +  -  +  - ]:          68 :             scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
     270                 :             :         }
     271         [ +  + ]:         134 :         if (P2SH) {
     272                 :          36 :             redeemscript = scriptPubKey;
     273   [ +  -  +  -  :          72 :             scriptPubKey = CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemscript)) << OP_EQUAL;
             +  -  +  - ]
     274                 :             :         }
     275   [ +  -  -  + ]:         268 :         creditTx = MakeTransactionRef(BuildCreditingTransaction(scriptPubKey, nValue));
     276         [ +  - ]:         268 :         spendTx = BuildSpendingTransaction(CScript(), CScriptWitness(), *creditTx);
     277         [ -  - ]:         134 :     }
     278                 :             : 
     279                 :          71 :     TestBuilder& ScriptError(ScriptError_t err)
     280                 :             :     {
     281                 :          71 :         scriptError = err;
     282                 :          71 :         return *this;
     283                 :             :     }
     284                 :             : 
     285                 :           6 :     TestBuilder& Opcode(const opcodetype& _op)
     286                 :             :     {
     287                 :           6 :         DoPush();
     288   [ +  -  +  -  :           6 :         spendTx.vin[0].scriptSig << _op;
          +  -  +  -  +  
                -  +  - ]
     289                 :           6 :         return *this;
     290                 :             :     }
     291                 :             : 
     292                 :          50 :     TestBuilder& Num(int num)
     293                 :             :     {
     294                 :          38 :         DoPush();
     295   [ +  -  +  -  :          50 :         spendTx.vin[0].scriptSig << num;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     296                 :          50 :         return *this;
     297                 :             :     }
     298                 :             : 
     299                 :           2 :     TestBuilder& Push(const std::string& hex)
     300                 :             :     {
     301   [ -  +  +  - ]:           2 :         DoPush(ParseHex(hex));
     302                 :           2 :         return *this;
     303                 :             :     }
     304                 :             : 
     305                 :          21 :     TestBuilder& Push(const CScript& _script)
     306                 :             :     {
     307   [ +  +  +  - ]:          63 :         DoPush(std::vector<unsigned char>(_script.begin(), _script.end()));
     308                 :          21 :         return *this;
     309                 :             :     }
     310                 :             : 
     311                 :         142 :     TestBuilder& PushSig(const CKey& key, int nHashType = SIGHASH_ALL, unsigned int lenR = 32, unsigned int lenS = 32, SigVersion sigversion = SigVersion::BASE, CAmount amount = 0)
     312                 :             :     {
     313                 :         142 :         uint256 hash = SignatureHash(script, spendTx, 0, nHashType, amount, sigversion);
     314                 :         142 :         std::vector<unsigned char> vchSig, r, s;
     315                 :         142 :         uint32_t iter = 0;
     316                 :        1340 :         do {
     317         [ +  - ]:        1340 :             key.Sign(hash, vchSig, false, iter++);
     318         [ +  + ]:        1340 :             if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) {
     319         [ +  - ]:           2 :                 NegateSignatureS(vchSig);
     320                 :             :             }
     321   [ +  -  +  - ]:        2680 :             r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
     322   [ +  -  -  + ]:        2680 :             s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
     323   [ -  +  +  +  :        1340 :         } while (lenR != r.size() || lenS != s.size());
          -  +  -  +  +  
                      + ]
     324         [ +  - ]:         142 :         vchSig.push_back(static_cast<unsigned char>(nHashType));
     325         [ +  - ]:         142 :         DoPush(vchSig);
     326                 :         142 :         return *this;
     327                 :         142 :     }
     328                 :             : 
     329                 :          50 :     TestBuilder& PushWitSig(const CKey& key, CAmount amount = -1, int nHashType = SIGHASH_ALL, unsigned int lenR = 32, unsigned int lenS = 32, SigVersion sigversion = SigVersion::WITNESS_V0)
     330                 :             :     {
     331         [ +  + ]:          50 :         if (amount == -1)
     332                 :          46 :             amount = nValue;
     333                 :          50 :         return PushSig(key, nHashType, lenR, lenS, sigversion, amount).AsWit();
     334                 :             :     }
     335                 :             : 
     336                 :          20 :     TestBuilder& Push(const CPubKey& pubkey)
     337                 :             :     {
     338         [ +  - ]:          40 :         DoPush(std::vector<unsigned char>(pubkey.begin(), pubkey.end()));
     339                 :          20 :         return *this;
     340                 :             :     }
     341                 :             : 
     342                 :          36 :     TestBuilder& PushRedeem()
     343                 :             :     {
     344   [ +  +  +  - ]:         108 :         DoPush(std::vector<unsigned char>(redeemscript.begin(), redeemscript.end()));
     345                 :          36 :         return *this;
     346                 :             :     }
     347                 :             : 
     348                 :          32 :     TestBuilder& PushWitRedeem()
     349                 :             :     {
     350   [ +  +  +  - ]:          96 :         DoPush(std::vector<unsigned char>(witscript.begin(), witscript.end()));
     351                 :          32 :         return AsWit();
     352                 :             :     }
     353                 :             : 
     354                 :          31 :     TestBuilder& EditPush(unsigned int pos, const std::string& hexin, const std::string& hexout)
     355                 :             :     {
     356         [ -  + ]:          31 :         assert(havePush);
     357         [ -  + ]:          31 :         std::vector<unsigned char> datain = ParseHex(hexin);
     358   [ -  +  +  - ]:          31 :         std::vector<unsigned char> dataout = ParseHex(hexout);
     359   [ -  +  -  +  :          31 :         assert(pos + datain.size() <= push.size());
                   -  + ]
     360   [ +  -  -  +  :          62 :         BOOST_CHECK_MESSAGE(std::vector<unsigned char>(push.begin() + pos, push.begin() + pos + datain.size()) == datain, comment);
          +  -  +  -  -  
                      + ]
     361         [ -  + ]:          31 :         push.erase(push.begin() + pos, push.begin() + pos + datain.size());
     362         [ +  - ]:          31 :         push.insert(push.begin() + pos, dataout.begin(), dataout.end());
     363                 :          31 :         return *this;
     364                 :          31 :     }
     365                 :             : 
     366                 :          14 :     TestBuilder& DamagePush(unsigned int pos)
     367                 :             :     {
     368         [ -  + ]:          14 :         assert(havePush);
     369   [ -  +  -  + ]:          14 :         assert(pos < push.size());
     370                 :          14 :         push[pos] ^= 1;
     371                 :          14 :         return *this;
     372                 :             :     }
     373                 :             : 
     374                 :         134 :     TestBuilder& Test(ScriptTest& test)
     375                 :             :     {
     376                 :         134 :         TestBuilder copy = *this; // Make a copy so we can rollback the push.
     377                 :         134 :         DoPush();
     378         [ +  - ]:         134 :         test.DoTest(creditTx->vout[0].scriptPubKey, spendTx.vin[0].scriptSig, scriptWitness, flags, comment, scriptError, nValue);
     379         [ +  - ]:         134 :         *this = copy;
     380                 :         134 :         return *this;
     381                 :         134 :     }
     382                 :             : 
     383                 :         122 :     TestBuilder& AsWit()
     384                 :             :     {
     385         [ -  + ]:         122 :         assert(havePush);
     386                 :         122 :         scriptWitness.stack.push_back(push);
     387                 :         122 :         havePush = false;
     388                 :         122 :         return *this;
     389                 :             :     }
     390                 :             : 
     391                 :         134 :     UniValue GetJSON()
     392                 :             :     {
     393                 :         134 :         DoPush();
     394                 :         134 :         UniValue array(UniValue::VARR);
     395         [ +  + ]:         134 :         if (!scriptWitness.stack.empty()) {
     396                 :          51 :             UniValue wit(UniValue::VARR);
     397   [ -  +  +  + ]:         173 :             for (unsigned i = 0; i < scriptWitness.stack.size(); i++) {
     398   [ -  +  +  -  :         122 :                 wit.push_back(HexStr(scriptWitness.stack[i]));
             +  -  +  - ]
     399                 :             :             }
     400   [ +  -  +  - ]:          51 :             wit.push_back(ValueFromAmount(nValue));
     401         [ +  - ]:          51 :             array.push_back(std::move(wit));
     402                 :          51 :         }
     403   [ +  -  +  -  :         134 :         array.push_back(FormatScript(spendTx.vin[0].scriptSig));
                   +  - ]
     404   [ +  -  +  -  :         134 :         array.push_back(FormatScript(creditTx->vout[0].scriptPubKey));
                   +  - ]
     405   [ +  -  +  -  :         134 :         array.push_back(FormatScriptFlags(flags));
                   +  - ]
     406   [ +  -  +  -  :         134 :         array.push_back(FormatScriptError((ScriptError_t)scriptError));
                   +  - ]
     407   [ +  -  +  - ]:         134 :         array.push_back(comment);
     408                 :         134 :         return array;
     409                 :           0 :     }
     410                 :             : 
     411                 :           0 :     std::string GetComment() const
     412                 :             :     {
     413         [ #  # ]:           0 :         return comment;
     414                 :             :     }
     415                 :             : };
     416                 :             : 
     417                 :        1452 : std::string JSONPrettyPrint(const UniValue& univalue)
     418                 :             : {
     419                 :        1452 :     std::string ret = univalue.write(4);
     420                 :             :     // Workaround for libunivalue pretty printer, which puts a space between commas and newlines
     421                 :        1452 :     size_t pos = 0;
     422         [ -  + ]:        2904 :     while ((pos = ret.find(" \n", pos)) != std::string::npos) {
     423         [ #  # ]:           0 :         ret.replace(pos, 2, "\n");
     424                 :           0 :         pos++;
     425                 :             :     }
     426                 :        1452 :     return ret;
     427                 :           0 : }
     428                 :             : } // namespace
     429                 :             : 
     430                 :             : BOOST_FIXTURE_TEST_SUITE(script_tests, ScriptTest)
     431                 :             : 
     432   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_build)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     433                 :             : {
     434                 :           1 :     const KeyData keys;
     435                 :             : 
     436                 :           1 :     std::vector<TestBuilder> tests;
     437                 :             : 
     438   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
                   +  - ]
     439                 :           0 :                                 "P2PK", 0
     440   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0));
                   +  - ]
     441   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     442                 :           0 :                                 "P2PK, bad sig", 0
     443   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).DamagePush(10).ScriptError(SCRIPT_ERR_EVAL_FALSE));
             +  -  +  - ]
     444                 :             : 
     445   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1C.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     446                 :           0 :                                 "P2PKH", 0
     447   [ +  -  +  -  :           2 :                                ).PushSig(keys.key1).Push(keys.pubkey1C));
             +  -  +  - ]
     448   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey2C.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
          +  -  +  -  +  
                -  +  - ]
     449                 :           0 :                                 "P2PKH, bad pubkey", 0
     450   [ +  -  +  -  :           2 :                                ).PushSig(keys.key2).Push(keys.pubkey2C).DamagePush(5).ScriptError(SCRIPT_ERR_EQUALVERIFY));
          +  -  +  -  +  
                      - ]
     451                 :             : 
     452   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
                   +  - ]
     453                 :           0 :                                 "P2PK anyonecanpay", 0
     454   [ +  -  +  -  :           2 :                                ).PushSig(keys.key1, SIGHASH_ALL | SIGHASH_ANYONECANPAY));
                   +  - ]
     455   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
     456                 :           0 :                                 "P2PK anyonecanpay marked with normal hashtype", 0
     457   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL | SIGHASH_ANYONECANPAY).EditPush(70, "81", "01").ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     458                 :             : 
     459   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
                   +  - ]
     460                 :           0 :                                 "P2SH(P2PK)", SCRIPT_VERIFY_P2SH, true
     461   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).PushRedeem());
             +  -  +  - ]
     462   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
     463                 :           0 :                                 "P2SH(P2PK), bad redeemscript", SCRIPT_VERIFY_P2SH, true
     464   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).PushRedeem().DamagePush(10).ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                      - ]
     465                 :             : 
     466   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey0.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     467                 :           0 :                                 "P2SH(P2PKH)", SCRIPT_VERIFY_P2SH, true
     468   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).Push(keys.pubkey0).PushRedeem());
          +  -  +  -  +  
                      - ]
     469   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     470                 :           0 :                                 "P2SH(P2PKH), bad sig but no VERIFY_P2SH", 0, true
     471   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).DamagePush(10).PushRedeem());
             +  -  +  - ]
     472   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
          +  -  +  -  +  
                -  +  - ]
     473                 :           0 :                                 "P2SH(P2PKH), bad sig", SCRIPT_VERIFY_P2SH, true
     474   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).DamagePush(10).PushRedeem().ScriptError(SCRIPT_ERR_EQUALVERIFY));
          +  -  +  -  +  
                      - ]
     475                 :             : 
     476   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     477                 :           0 :                                 "3-of-3", 0
     478   [ +  -  +  -  :           3 :                                ).Num(0).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
          +  -  +  -  +  
                      - ]
     479   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     480                 :           0 :                                 "3-of-3, 2 sigs", 0
     481   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key0).PushSig(keys.key1).Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                      - ]
     482                 :             : 
     483   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     484                 :           0 :                                 "P2SH(2-of-3)", SCRIPT_VERIFY_P2SH, true
     485   [ +  -  +  -  :           3 :                                ).Num(0).PushSig(keys.key1).PushSig(keys.key2).PushRedeem());
          +  -  +  -  +  
                      - ]
     486   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     487                 :           0 :                                 "P2SH(2-of-3), 1 sig", SCRIPT_VERIFY_P2SH, true
     488   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1).Num(0).PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                      - ]
     489                 :             : 
     490   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
                   +  - ]
     491                 :           0 :                                 "P2PK with too much R padding but no DERSIG", 0
     492   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000"));
          +  -  +  -  +  
                -  +  - ]
     493   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     494                 :           0 :                                 "P2PK with too much R padding", SCRIPT_VERIFY_DERSIG
     495   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     496   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
                   +  - ]
     497                 :           0 :                                 "P2PK with too much S padding but no DERSIG", 0
     498   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL).EditPush(1, "44", "45").EditPush(37, "20", "2100"));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     499   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     500                 :           0 :                                 "P2PK with too much S padding", SCRIPT_VERIFY_DERSIG
     501   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL).EditPush(1, "44", "45").EditPush(37, "20", "2100").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     502   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
                   +  - ]
     503                 :           0 :                                 "P2PK with too little R padding but no DERSIG", 0
     504   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220"));
          +  -  +  -  +  
                -  +  - ]
     505   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     506                 :           0 :                                 "P2PK with too little R padding", SCRIPT_VERIFY_DERSIG
     507   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     508   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
             +  -  +  - ]
     509                 :           0 :                                 "P2PK NOT with bad sig with too much R padding but no DERSIG", 0
     510   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").DamagePush(10));
          +  -  +  -  +  
                -  +  - ]
     511   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     512                 :           0 :                                 "P2PK NOT with bad sig with too much R padding", SCRIPT_VERIFY_DERSIG
     513   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").DamagePush(10).ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     514   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     515                 :           0 :                                 "P2PK NOT with too much R padding but no DERSIG", 0
     516   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     517   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     518                 :           0 :                                 "P2PK NOT with too much R padding", SCRIPT_VERIFY_DERSIG
     519   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     520                 :             : 
     521   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
                   +  - ]
     522                 :           0 :                                 "BIP66 example 1, without DERSIG", 0
     523   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220"));
          +  -  +  -  +  
                -  +  - ]
     524   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     525                 :           0 :                                 "BIP66 example 1, with DERSIG", SCRIPT_VERIFY_DERSIG
     526   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     527   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     528                 :           0 :                                 "BIP66 example 2, without DERSIG", 0
     529   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     530   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     531                 :           0 :                                 "BIP66 example 2, with DERSIG", SCRIPT_VERIFY_DERSIG
     532   [ +  -  +  -  :           3 :                                ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     533   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     534                 :           0 :                                 "BIP66 example 3, without DERSIG", 0
     535   [ +  -  +  -  :           3 :                                ).Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
                   +  - ]
     536   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     537                 :           0 :                                 "BIP66 example 3, with DERSIG", SCRIPT_VERIFY_DERSIG
     538   [ +  -  +  -  :           3 :                                ).Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
                   +  - ]
     539   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     540                 :           0 :                                 "BIP66 example 4, without DERSIG", 0
     541   [ +  -  +  -  :           3 :                                ).Num(0));
                   +  - ]
     542   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     543                 :           0 :                                 "BIP66 example 4, with DERSIG", SCRIPT_VERIFY_DERSIG
     544   [ +  -  +  -  :           3 :                                ).Num(0));
                   +  - ]
     545   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     546                 :           0 :                                 "BIP66 example 5, without DERSIG", 0
     547   [ +  -  +  -  :           3 :                                ).Num(1).ScriptError(SCRIPT_ERR_EVAL_FALSE));
                   +  - ]
     548   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
     549                 :           0 :                                 "BIP66 example 5, with DERSIG", SCRIPT_VERIFY_DERSIG
     550   [ +  -  +  -  :           3 :                                ).Num(1).ScriptError(SCRIPT_ERR_SIG_DER));
                   +  - ]
     551   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     552                 :           0 :                                 "BIP66 example 6, without DERSIG", 0
     553   [ +  -  +  -  :           3 :                                ).Num(1));
                   +  - ]
     554   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     555                 :           0 :                                 "BIP66 example 6, with DERSIG", SCRIPT_VERIFY_DERSIG
     556   [ +  -  +  -  :           3 :                                ).Num(1).ScriptError(SCRIPT_ERR_SIG_DER));
                   +  - ]
     557   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     558                 :           0 :                                 "BIP66 example 7, without DERSIG", 0
     559   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     560   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     561                 :           0 :                                 "BIP66 example 7, with DERSIG", SCRIPT_VERIFY_DERSIG
     562   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2).ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     563   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
                -  +  - ]
     564                 :           0 :                                 "BIP66 example 8, without DERSIG", 0
     565   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2).ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     566   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
                -  +  - ]
     567                 :           0 :                                 "BIP66 example 8, with DERSIG", SCRIPT_VERIFY_DERSIG
     568   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2).ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     569   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     570                 :           0 :                                 "BIP66 example 9, without DERSIG", 0
     571   [ +  -  +  -  :           5 :                                ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     572   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     573                 :           0 :                                 "BIP66 example 9, with DERSIG", SCRIPT_VERIFY_DERSIG
     574   [ +  -  +  -  :           5 :                                ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     575   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     576                 :           0 :                                 "BIP66 example 10, without DERSIG", 0
     577   [ +  -  +  -  :           5 :                                ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220"));
          +  -  +  -  +  
                -  +  - ]
     578   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
                -  +  - ]
     579                 :           0 :                                 "BIP66 example 10, with DERSIG", SCRIPT_VERIFY_DERSIG
     580   [ +  -  +  -  :           5 :                                ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     581   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     582                 :           0 :                                 "BIP66 example 11, without DERSIG", 0
     583   [ +  -  +  -  :           5 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     584   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     585                 :           0 :                                 "BIP66 example 11, with DERSIG", SCRIPT_VERIFY_DERSIG
     586   [ +  -  +  -  :           5 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     587   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
                -  +  - ]
     588                 :           0 :                                 "BIP66 example 12, without DERSIG", 0
     589   [ +  -  +  -  :           5 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     590   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
                -  +  - ]
     591                 :           0 :                                 "BIP66 example 12, with DERSIG", SCRIPT_VERIFY_DERSIG
     592   [ +  -  +  -  :           5 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     593   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
                   +  - ]
     594                 :           0 :                                 "P2PK with multi-byte hashtype, without DERSIG", 0
     595   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2, SIGHASH_ALL).EditPush(70, "01", "0101"));
          +  -  +  -  +  
                -  +  - ]
     596   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
     597                 :           0 :                                 "P2PK with multi-byte hashtype, with DERSIG", SCRIPT_VERIFY_DERSIG
     598   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2, SIGHASH_ALL).EditPush(70, "01", "0101").ScriptError(SCRIPT_ERR_SIG_DER));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     599                 :             : 
     600   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
                   +  - ]
     601                 :           0 :                                 "P2PK with high S but no LOW_S", 0
     602   [ +  -  +  -  :           2 :                                ).PushSig(keys.key2, SIGHASH_ALL, 32, 33));
                   +  - ]
     603   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
     604                 :           0 :                                 "P2PK with high S", SCRIPT_VERIFY_LOW_S
     605   [ +  -  +  -  :           2 :                                ).PushSig(keys.key2, SIGHASH_ALL, 32, 33).ScriptError(SCRIPT_ERR_SIG_HIGH_S));
             +  -  +  - ]
     606                 :             : 
     607   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG,
                   +  - ]
     608                 :           0 :                                 "P2PK with hybrid pubkey but no STRICTENC", 0
     609   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0, SIGHASH_ALL));
                   +  - ]
     610   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG,
     611                 :           0 :                                 "P2PK with hybrid pubkey", SCRIPT_VERIFY_STRICTENC
     612   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0, SIGHASH_ALL).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
             +  -  +  - ]
     613   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     614                 :           0 :                                 "P2PK NOT with hybrid pubkey but no STRICTENC", 0
     615   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0, SIGHASH_ALL).ScriptError(SCRIPT_ERR_EVAL_FALSE));
             +  -  +  - ]
     616   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     617                 :           0 :                                 "P2PK NOT with hybrid pubkey", SCRIPT_VERIFY_STRICTENC
     618   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0, SIGHASH_ALL).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
             +  -  +  - ]
     619   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
             +  -  +  - ]
     620                 :           0 :                                 "P2PK NOT with invalid hybrid pubkey but no STRICTENC", 0
     621   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0, SIGHASH_ALL).DamagePush(10));
                   +  - ]
     622   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     623                 :           0 :                                 "P2PK NOT with invalid hybrid pubkey", SCRIPT_VERIFY_STRICTENC
     624   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0, SIGHASH_ALL).DamagePush(10).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
             +  -  +  - ]
     625   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey0H) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     626                 :           0 :                                 "1-of-2 with the second 1 hybrid pubkey and no STRICTENC", 0
     627   [ +  -  +  -  :           3 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL));
                   +  - ]
     628   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey0H) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     629                 :           0 :                                 "1-of-2 with the second 1 hybrid pubkey", SCRIPT_VERIFY_STRICTENC
     630   [ +  -  +  -  :           3 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL));
                   +  - ]
     631   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0H) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     632                 :           0 :                                 "1-of-2 with the first 1 hybrid pubkey", SCRIPT_VERIFY_STRICTENC
     633   [ +  -  +  -  :           3 :                                ).Num(0).PushSig(keys.key1, SIGHASH_ALL).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
             +  -  +  - ]
     634                 :             : 
     635   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
                   +  - ]
     636                 :           0 :                                 "P2PK with undefined hashtype but no STRICTENC", 0
     637   [ +  -  +  -  :           2 :                                ).PushSig(keys.key1, 5));
                   +  - ]
     638   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
     639                 :           0 :                                 "P2PK with undefined hashtype", SCRIPT_VERIFY_STRICTENC
     640   [ +  -  +  -  :           2 :                                ).PushSig(keys.key1, 5).ScriptError(SCRIPT_ERR_SIG_HASHTYPE));
             +  -  +  - ]
     641   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG << OP_NOT,
             +  -  +  - ]
     642                 :           0 :                                 "P2PK NOT with invalid sig and undefined hashtype but no STRICTENC", 0
     643   [ +  -  +  -  :           2 :                                ).PushSig(keys.key1, 5).DamagePush(10));
                   +  - ]
     644   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG << OP_NOT,
                   +  - ]
     645                 :           0 :                                 "P2PK NOT with invalid sig and undefined hashtype", SCRIPT_VERIFY_STRICTENC
     646   [ +  -  +  -  :           2 :                                ).PushSig(keys.key1, 5).DamagePush(10).ScriptError(SCRIPT_ERR_SIG_HASHTYPE));
             +  -  +  - ]
     647                 :             : 
     648   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     649                 :           0 :                                 "3-of-3 with nonzero dummy but no NULLDUMMY", 0
     650   [ +  -  +  -  :           3 :                                ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
          +  -  +  -  +  
                      - ]
     651   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     652                 :           0 :                                 "3-of-3 with nonzero dummy", SCRIPT_VERIFY_NULLDUMMY
     653   [ +  -  +  -  :           3 :                                ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).ScriptError(SCRIPT_ERR_SIG_NULLDUMMY));
          +  -  +  -  +  
                -  +  - ]
     654   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     655                 :           0 :                                 "3-of-3 NOT with invalid sig and nonzero dummy but no NULLDUMMY", 0
     656   [ +  -  +  -  :           3 :                                ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10));
          +  -  +  -  +  
                      - ]
     657   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG << OP_NOT,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     658                 :           0 :                                 "3-of-3 NOT with invalid sig with nonzero dummy", SCRIPT_VERIFY_NULLDUMMY
     659   [ +  -  +  -  :           3 :                                ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10).ScriptError(SCRIPT_ERR_SIG_NULLDUMMY));
          +  -  +  -  +  
                -  +  - ]
     660                 :             : 
     661   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     662                 :           0 :                                 "2-of-2 with two identical keys and sigs pushed using OP_DUP but no SIGPUSHONLY", 0
     663   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1).Opcode(OP_DUP));
             +  -  +  - ]
     664   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     665                 :           0 :                                 "2-of-2 with two identical keys and sigs pushed using OP_DUP", SCRIPT_VERIFY_SIGPUSHONLY
     666   [ +  -  +  -  :           4 :                                ).Num(0).PushSig(keys.key1).Opcode(OP_DUP).ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
             +  -  +  - ]
     667   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
                   +  - ]
     668                 :           0 :                                 "P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0, true
     669   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem());
             +  -  +  - ]
     670   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
     671                 :           0 :                                 "P2PK with non-push scriptSig but with P2SH validation", SCRIPT_VERIFY_P2SH
     672   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2).Opcode(OP_NOP8));
             +  -  +  - ]
     673   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
     674                 :           0 :                                 "P2SH(P2PK) with non-push scriptSig but no SIGPUSHONLY", SCRIPT_VERIFY_P2SH, true
     675   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
          +  -  +  -  +  
                      - ]
     676   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
     677                 :           0 :                                 "P2SH(P2PK) with non-push scriptSig but not P2SH", SCRIPT_VERIFY_SIGPUSHONLY, true
     678   [ +  -  +  -  :           3 :                                ).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
          +  -  +  -  +  
                      - ]
     679   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     680                 :           0 :                                 "2-of-2 with two identical keys and sigs pushed", SCRIPT_VERIFY_SIGPUSHONLY
     681   [ +  -  +  -  :           3 :                                ).Num(0).PushSig(keys.key1).PushSig(keys.key1));
             +  -  +  - ]
     682   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
                   +  - ]
     683                 :           0 :                                 "P2PK with unnecessary input but no CLEANSTACK", SCRIPT_VERIFY_P2SH
     684   [ +  -  +  -  :           3 :                                ).Num(11).PushSig(keys.key0));
                   +  - ]
     685   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     686                 :           0 :                                 "P2PK with unnecessary input", SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH
     687   [ +  -  +  -  :           3 :                                ).Num(11).PushSig(keys.key0).ScriptError(SCRIPT_ERR_CLEANSTACK));
             +  -  +  - ]
     688   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
                   +  - ]
     689                 :           0 :                                 "P2SH with unnecessary input but no CLEANSTACK", SCRIPT_VERIFY_P2SH, true
     690   [ +  -  +  -  :           3 :                                ).Num(11).PushSig(keys.key0).PushRedeem());
             +  -  +  - ]
     691   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     692                 :           0 :                                 "P2SH with unnecessary input", SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH, true
     693   [ +  -  +  -  :           3 :                                ).Num(11).PushSig(keys.key0).PushRedeem().ScriptError(SCRIPT_ERR_CLEANSTACK));
          +  -  +  -  +  
                      - ]
     694   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
                   +  - ]
     695                 :           0 :                                 "P2SH with CLEANSTACK", SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH, true
     696   [ +  -  +  -  :           2 :                                ).PushSig(keys.key0).PushRedeem());
             +  -  +  - ]
     697                 :             : 
     698   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
                   +  - ]
     699                 :           0 :                                 "Basic P2WSH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
     700   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).PushWitRedeem());
             +  -  +  - ]
     701   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     702                 :           0 :                                 "Basic P2WPKH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH,
     703   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit());
          +  -  +  -  +  
                      - ]
     704   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
                   +  - ]
     705                 :           0 :                                 "Basic P2SH(P2WSH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
     706   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
                      - ]
     707   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     708                 :           0 :                                 "Basic P2SH(P2WPKH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH,
     709   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().PushRedeem());
          +  -  +  -  +  
                -  +  - ]
     710   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
     711                 :           0 :                                 "Basic P2WSH with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
     712   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).PushWitRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                      - ]
     713         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
     714                 :           0 :                                 "Basic P2WPKH with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
     715   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                -  +  - ]
     716   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
     717                 :           0 :                                 "Basic P2SH(P2WSH) with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH
     718   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                -  +  - ]
     719         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
     720                 :           0 :                                 "Basic P2SH(P2WPKH) with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH
     721   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     722   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
                   +  - ]
     723                 :           0 :                                 "Basic P2WSH with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
     724   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).PushWitRedeem());
             +  -  +  - ]
     725   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
     726                 :           0 :                                 "Basic P2WPKH with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
     727   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit());
          +  -  +  -  +  
                      - ]
     728   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
                   +  - ]
     729                 :           0 :                                 "Basic P2SH(P2WSH) with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, true, WitnessMode::SH
     730   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
                      - ]
     731   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
     732                 :           0 :                                 "Basic P2SH(P2WPKH) with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH
     733   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().PushRedeem());
          +  -  +  -  +  
                -  +  - ]
     734   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     735                 :           0 :                                 "Basic P2WSH with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
     736   [ +  -  +  -  :           2 :                                 0, 0).PushWitSig(keys.key0, 1).PushWitRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                      - ]
     737         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     738                 :           0 :                                 "Basic P2WPKH with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH,
     739   [ +  -  +  -  :           2 :                                 0, 0).PushWitSig(keys.key0, 1).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                -  +  - ]
     740   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     741                 :           0 :                                 "Basic P2SH(P2WSH) with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
     742   [ +  -  +  -  :           2 :                                 0, 0).PushWitSig(keys.key0, 1).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
                -  +  - ]
     743         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     744                 :           0 :                                 "Basic P2SH(P2WPKH) with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH,
     745   [ +  -  +  -  :           2 :                                 0, 0).PushWitSig(keys.key0, 1).Push(keys.pubkey0).AsWit().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     746                 :             : 
     747         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     748                 :           0 :                                 "P2WPKH with future witness version", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH |
     749                 :             :                                 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, false, WitnessMode::PKH, 1
     750   [ +  -  +  -  :           2 :                                ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM));
          +  -  +  -  +  
                -  +  - ]
     751                 :           1 :     {
     752         [ +  - ]:           2 :         CScript witscript = CScript() << ToByteVector(keys.pubkey0);
     753                 :           1 :         uint256 hash;
     754   [ +  -  +  -  :           3 :         CSHA256().Write(witscript.data(), witscript.size()).Finalize(hash.begin());
             +  -  +  - ]
     755         [ +  - ]:           1 :         std::vector<unsigned char> hashBytes = ToByteVector(hash);
     756         [ +  - ]:           1 :         hashBytes.pop_back();
     757   [ +  -  -  + ]:           2 :         tests.push_back(TestBuilder(CScript() << OP_0 << hashBytes,
     758                 :           1 :                                     "P2WPKH with wrong witness program length", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false
     759   [ +  -  +  -  :           2 :                                    ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH));
          +  -  +  -  +  
                -  +  - ]
     760                 :           1 :     }
     761   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     762                 :           0 :                                 "P2WSH with empty witness", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
     763   [ +  -  +  -  :           2 :                                ).ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY));
                   +  - ]
     764                 :           1 :     {
     765   [ +  -  +  - ]:           2 :         CScript witscript = CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG;
     766                 :           1 :         tests.push_back(TestBuilder(witscript,
     767                 :           1 :                                     "P2WSH with witness program mismatch", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
     768   [ +  -  +  -  :           1 :                                    ).PushWitSig(keys.key0).Push(witscript).DamagePush(0).AsWit().ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH));
          +  -  +  -  +  
                -  +  - ]
     769                 :           0 :     }
     770         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     771                 :           0 :                                 "P2WPKH with witness program mismatch", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
     772   [ +  -  +  -  :           3 :                                ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().Push("0").AsWit().ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     773         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     774                 :           0 :                                 "P2WPKH with non-empty scriptSig", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
     775   [ +  -  +  -  :           3 :                                ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().Num(11).ScriptError(SCRIPT_ERR_WITNESS_MALLEATED));
          +  -  +  -  +  
                -  +  - ]
     776         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
     777                 :           0 :                                 "P2SH(P2WPKH) with superfluous push in scriptSig", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH
     778   [ +  -  +  -  :           3 :                                ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().Num(11).PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_MALLEATED_P2SH));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     779   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     780                 :           0 :                                 "P2PK with witness", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH
     781   [ +  -  +  -  :           3 :                                ).PushSig(keys.key0).Push("0").AsWit().ScriptError(SCRIPT_ERR_WITNESS_UNEXPECTED));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     782                 :             : 
     783                 :             :     // Compressed keys should pass SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
     784   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
                   +  - ]
     785                 :           0 :                                 "Basic P2WSH with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     786   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0C).PushWitRedeem());
             +  -  +  - ]
     787   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C),
     788                 :           0 :                                 "Basic P2WPKH with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::PKH,
     789   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit());
          +  -  +  -  +  
                      - ]
     790   [ +  -  +  -  :           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
                   +  - ]
     791                 :           0 :                                 "Basic P2SH(P2WSH) with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     792   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
                      - ]
     793   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C),
     794                 :           0 :                                 "Basic P2SH(P2WPKH) with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::PKH,
     795   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit().PushRedeem());
          +  -  +  -  +  
                -  +  - ]
     796                 :             : 
     797                 :             :     // Testing uncompressed key in witness with SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
     798   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     799                 :           0 :                                 "Basic P2WSH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     800   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
                      - ]
     801         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     802                 :           0 :                                 "Basic P2WPKH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::PKH,
     803   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
                -  +  - ]
     804   [ +  -  +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
     805                 :           0 :                                 "Basic P2SH(P2WSH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     806   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
                -  +  - ]
     807         [ +  - ]:           3 :     tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
     808                 :           0 :                                 "Basic P2SH(P2WPKH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::PKH,
     809   [ +  -  +  -  :           2 :                                 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     810                 :             : 
     811                 :             :     // P2WSH 1-of-2 multisig with compressed keys
     812   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     813                 :           0 :                                 "P2WSH CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     814   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     815   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     816                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     817   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     818   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     819                 :           0 :                                 "P2WSH CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     820   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     821   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     822                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     823   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     824                 :             : 
     825                 :             :     // P2WSH 1-of-2 multisig with first key uncompressed
     826   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     827                 :           0 :                                 "P2WSH CHECKMULTISIG with first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
     828   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     829   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     830                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
     831   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     832   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     833                 :           0 :                                 "P2WSH CHECKMULTISIG with first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     834   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     835   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     836                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     837   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     838   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     839                 :           0 :                                 "P2WSH CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
     840   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     841   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     842                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
     843   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     844   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     845                 :           0 :                                 "P2WSH CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     846   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     847   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     848                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     849   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     850                 :             :     // P2WSH 1-of-2 multisig with second key uncompressed
     851   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     852                 :           0 :                                 "P2WSH CHECKMULTISIG with second key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
     853   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     854   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     855                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG second key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
     856   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     857   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     858                 :           0 :                                 "P2WSH CHECKMULTISIG with second key uncompressed and signing with the first key should pass as the uncompressed key is not used", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     859   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     860   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     861                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with second key uncompressed and signing with the first key should pass as the uncompressed key is not used", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     862   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     863   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     864                 :           0 :                                 "P2WSH CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
     865   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem());
          +  -  +  -  +  
                -  +  - ]
     866   [ +  -  +  -  :           5 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                -  +  - ]
     867                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
     868   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().PushRedeem());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     869   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     870                 :           0 :                                 "P2WSH CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
     871   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     872   [ +  -  +  -  :           4 :     tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
          +  -  +  -  +  
                      - ]
     873                 :           0 :                                 "P2SH(P2WSH) CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
     874   [ +  -  +  -  :           3 :                                 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     875                 :             : 
     876         [ +  - ]:           1 :     std::set<std::string> tests_set;
     877                 :             : 
     878                 :           1 :     {
     879         [ +  - ]:           1 :         UniValue json_tests = read_json(json_tests::script_tests);
     880                 :             : 
     881   [ -  +  +  + ]:        1319 :         for (unsigned int idx = 0; idx < json_tests.size(); idx++) {
     882         [ +  - ]:        1318 :             const UniValue& tv = json_tests[idx];
     883   [ +  -  +  -  :        2636 :             tests_set.insert(JSONPrettyPrint(tv.get_array()));
                   +  - ]
     884                 :             :         }
     885                 :           1 :     }
     886                 :             : 
     887                 :             : #ifdef UPDATE_JSON_TESTS
     888                 :             :     std::string strGen;
     889                 :             : #endif
     890         [ +  + ]:         135 :     for (TestBuilder& test : tests) {
     891         [ +  - ]:         134 :         test.Test(*this);
     892   [ +  -  +  - ]:         134 :         std::string str = JSONPrettyPrint(test.GetJSON());
     893                 :             : #ifdef UPDATE_JSON_TESTS
     894                 :             :         strGen += str + ",\n";
     895                 :             : #else
     896         [ -  + ]:         134 :         if (!tests_set.contains(str)) {
     897   [ #  #  #  #  :           0 :             BOOST_CHECK_MESSAGE(false, "Missing auto script_valid test: " + test.GetComment());
             #  #  #  # ]
     898                 :             :         }
     899                 :             : #endif
     900                 :         134 :     }
     901                 :             : 
     902                 :             : #ifdef UPDATE_JSON_TESTS
     903                 :             :     FILE* file = fsbridge::fopen("script_tests.json.gen", "w");
     904                 :             :     fputs(strGen.c_str(), file);
     905                 :             :     fclose(file);
     906                 :             : #endif
     907                 :           1 : }
     908                 :             : 
     909   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_json_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     910                 :             : {
     911                 :             :     // Read tests from test/data/script_tests.json
     912                 :             :     // Format is an array of arrays
     913                 :             :     // Inner arrays are [ ["wit"..., nValue]?, "scriptSig", "scriptPubKey", "flags", "expected_scripterror" ]
     914                 :             :     // ... where scriptSig and scriptPubKey are stringified
     915                 :             :     // scripts.
     916                 :             :     // If a witness is given, then the last value in the array should be the
     917                 :             :     // amount (nValue) to use in the crediting tx
     918                 :           1 :     UniValue tests = read_json(json_tests::script_tests);
     919                 :             : 
     920         [ +  - ]:           1 :     const KeyData keys;
     921   [ -  +  +  + ]:        1319 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
     922         [ +  - ]:        1318 :         const UniValue& test = tests[idx];
     923         [ +  - ]:        1318 :         std::string strTest = test.write();
     924                 :        1318 :         CScriptWitness witness;
     925         [ -  + ]:        1318 :         TaprootBuilder taprootBuilder;
     926                 :        1318 :         CAmount nValue = 0;
     927                 :        1318 :         unsigned int pos = 0;
     928   [ -  +  +  -  :        1318 :         if (test.size() > 0 && test[pos].isArray()) {
             +  -  +  + ]
     929                 :             :             unsigned int i=0;
     930   [ +  -  -  +  :         363 :             for (i = 0; i < test[pos].size()-1; i++) {
                   +  + ]
     931   [ +  -  +  -  :         246 :                 auto element = test[pos][i].get_str();
             +  -  -  + ]
     932                 :             :                 // We use #SCRIPT# to flag a non-hex script that we can read using ParseScript
     933                 :             :                 // Taproot script must be third from the last element in witness stack
     934   [ +  +  +  - ]:         246 :                 static const std::string SCRIPT_FLAG{"#SCRIPT#"};
     935   [ -  +  -  +  :         246 :                 if (element.starts_with(SCRIPT_FLAG)) {
                   +  + ]
     936   [ +  -  +  - ]:           7 :                     CScript script = ParseScript(element.substr(SCRIPT_FLAG.size()));
     937         [ +  - ]:          14 :                     witness.stack.push_back(ToByteVector(script));
     938         [ +  + ]:         246 :                 } else if (element == "#CONTROLBLOCK#") {
     939                 :             :                     // Taproot script control block - second from the last element in witness stack
     940                 :             :                     // If #CONTROLBLOCK# we auto-generate the control block
     941   [ -  +  +  - ]:           7 :                     taprootBuilder.Add(/*depth=*/0, witness.stack.back(), TAPROOT_LEAF_TAPSCRIPT, /*track=*/true);
     942   [ +  -  +  - ]:           7 :                     taprootBuilder.Finalize(XOnlyPubKey(keys.key0.GetPubKey()));
     943   [ +  -  +  -  :           7 :                     auto controlblocks = taprootBuilder.GetSpendData().scripts[{witness.stack.back(), TAPROOT_LEAF_TAPSCRIPT}];
             +  -  +  - ]
     944         [ +  - ]:           7 :                     witness.stack.push_back(*(controlblocks.begin()));
     945                 :           7 :                 } else {
     946         [ +  - ]:         232 :                     const auto witness_value{TryParseHex<unsigned char>(element)};
     947         [ -  + ]:         232 :                     if (!witness_value.has_value()) {
     948   [ #  #  #  # ]:           0 :                         BOOST_ERROR("Bad witness in test: " << strTest << " witness is not hex: " << element);
     949                 :             :                     }
     950   [ +  -  +  - ]:         232 :                     witness.stack.push_back(witness_value.value());
     951                 :         232 :                 }
     952                 :         246 :             }
     953   [ +  -  +  -  :         117 :             nValue = AmountFromValue(test[pos][i]);
                   +  - ]
     954                 :             :             pos++;
     955                 :             :         }
     956   [ -  +  +  + ]:        1318 :         if (test.size() < 4 + pos) // Allow size > 3; extra stuff ignored (useful for comments)
     957                 :             :         {
     958         [ -  + ]:          60 :             if (test.size() != 1) {
     959   [ #  #  #  # ]:           0 :                 BOOST_ERROR("Bad test: " << strTest);
     960                 :             :             }
     961                 :          60 :             continue;
     962                 :             :         }
     963   [ +  -  +  -  :        1258 :         std::string scriptSigString = test[pos++].get_str();
                   -  + ]
     964         [ +  - ]:        1258 :         CScript scriptSig = ParseScript(scriptSigString);
     965   [ +  -  +  -  :        1258 :         std::string scriptPubKeyString = test[pos++].get_str();
                   -  + ]
     966                 :        1258 :         CScript scriptPubKey;
     967                 :             :         // If requested, auto-generate the taproot output
     968         [ +  + ]:        1258 :         if (scriptPubKeyString == "0x51 0x20 #TAPROOTOUTPUT#") {
     969   [ +  -  +  -  :          14 :             BOOST_CHECK_MESSAGE(taprootBuilder.IsComplete(), "Failed to autogenerate Tapscript output key");
                   +  - ]
     970   [ +  -  +  -  :          14 :             scriptPubKey = CScript() << OP_1 << ToByteVector(taprootBuilder.GetOutput());
                   +  - ]
     971                 :             :         } else {
     972         [ +  - ]:        2502 :             scriptPubKey = ParseScript(scriptPubKeyString);
     973                 :             :         }
     974   [ +  -  +  -  :        2516 :         script_verify_flags scriptflags = ParseScriptFlags(test[pos++].get_str());
             -  +  +  - ]
     975   [ +  -  +  -  :        1258 :         int scriptError = ParseScriptError(test[pos++].get_str());
                   +  - ]
     976                 :             : 
     977         [ +  - ]:        1258 :         DoTest(scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError, nValue);
     978                 :        1318 :     }
     979                 :           1 : }
     980                 :             : 
     981   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_PushData)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     982                 :             : {
     983                 :             :     // Check that PUSHDATA1, PUSHDATA2, and PUSHDATA4 create the same value on
     984                 :             :     // the stack as the 1-75 opcodes do.
     985                 :           1 :     static const unsigned char direct[] = { 1, 0x5a };
     986                 :           1 :     static const unsigned char pushdata1[] = { OP_PUSHDATA1, 1, 0x5a };
     987                 :           1 :     static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
     988                 :           1 :     static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
     989                 :             : 
     990                 :           1 :     ScriptError err;
     991                 :           1 :     std::vector<std::vector<unsigned char> > directStack;
     992   [ +  -  +  -  :           2 :     BOOST_CHECK(EvalScript(directStack, CScript(direct, direct + sizeof(direct)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
     993   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
     994                 :             : 
     995                 :           1 :     std::vector<std::vector<unsigned char> > pushdata1Stack;
     996   [ +  -  +  -  :           2 :     BOOST_CHECK(EvalScript(pushdata1Stack, CScript(pushdata1, pushdata1 + sizeof(pushdata1)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
     997   [ +  -  +  -  :           2 :     BOOST_CHECK(pushdata1Stack == directStack);
                   +  - ]
     998   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
     999                 :             : 
    1000                 :           1 :     std::vector<std::vector<unsigned char> > pushdata2Stack;
    1001   [ +  -  +  -  :           2 :     BOOST_CHECK(EvalScript(pushdata2Stack, CScript(pushdata2, pushdata2 + sizeof(pushdata2)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
    1002   [ +  -  +  -  :           2 :     BOOST_CHECK(pushdata2Stack == directStack);
                   +  - ]
    1003   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1004                 :             : 
    1005                 :           1 :     std::vector<std::vector<unsigned char> > pushdata4Stack;
    1006   [ +  -  +  -  :           2 :     BOOST_CHECK(EvalScript(pushdata4Stack, CScript(pushdata4, pushdata4 + sizeof(pushdata4)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
    1007   [ +  -  +  -  :           2 :     BOOST_CHECK(pushdata4Stack == directStack);
                   +  - ]
    1008   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1009                 :             : 
    1010         [ +  - ]:           1 :     const std::vector<unsigned char> pushdata1_trunc{OP_PUSHDATA1, 1};
    1011         [ +  - ]:           1 :     const std::vector<unsigned char> pushdata2_trunc{OP_PUSHDATA2, 1, 0};
    1012         [ +  - ]:           1 :     const std::vector<unsigned char> pushdata4_trunc{OP_PUSHDATA4, 1, 0, 0, 0};
    1013                 :             : 
    1014                 :           1 :     std::vector<std::vector<unsigned char>> stack_ignore;
    1015   [ +  -  +  -  :           2 :     BOOST_CHECK(!EvalScript(stack_ignore, CScript(pushdata1_trunc.begin(), pushdata1_trunc.end()), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
    1016   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(err, SCRIPT_ERR_BAD_OPCODE);
    1017   [ +  -  +  -  :           2 :     BOOST_CHECK(!EvalScript(stack_ignore, CScript(pushdata2_trunc.begin(), pushdata2_trunc.end()), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
    1018   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(err, SCRIPT_ERR_BAD_OPCODE);
    1019   [ +  -  +  -  :           2 :     BOOST_CHECK(!EvalScript(stack_ignore, CScript(pushdata4_trunc.begin(), pushdata4_trunc.end()), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
    1020   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(err, SCRIPT_ERR_BAD_OPCODE);
    1021                 :           1 : }
    1022                 :             : 
    1023   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_cltv_truncated)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1024                 :             : {
    1025         [ +  - ]:           1 :     const auto script_cltv_trunc = CScript() << OP_CHECKLOCKTIMEVERIFY;
    1026                 :             : 
    1027                 :           1 :     std::vector<std::vector<unsigned char>> stack_ignore;
    1028                 :           1 :     ScriptError err;
    1029   [ +  -  +  -  :           2 :     BOOST_CHECK(!EvalScript(stack_ignore, script_cltv_trunc, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, BaseSignatureChecker(), SigVersion::BASE, &err));
             +  -  +  - ]
    1030   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(err, SCRIPT_ERR_INVALID_STACK_OPERATION);
    1031                 :           1 : }
    1032                 :             : 
    1033                 :             : static CScript
    1034                 :          12 : sign_multisig(const CScript& scriptPubKey, const std::vector<CKey>& keys, const CTransaction& transaction)
    1035                 :             : {
    1036                 :          12 :     uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SIGHASH_ALL, 0, SigVersion::BASE);
    1037                 :             : 
    1038                 :          12 :     CScript result;
    1039                 :             :     //
    1040                 :             :     // NOTE: CHECKMULTISIG has an unfortunate bug; it requires
    1041                 :             :     // one extra item on the stack, before the signatures.
    1042                 :             :     // Putting OP_0 on the stack is the workaround;
    1043                 :             :     // fixing the bug would mean splitting the block chain (old
    1044                 :             :     // clients would not accept new CHECKMULTISIG transactions,
    1045                 :             :     // and vice-versa)
    1046                 :             :     //
    1047         [ +  - ]:          12 :     result << OP_0;
    1048         [ +  + ]:          31 :     for (const CKey &key : keys)
    1049                 :             :     {
    1050                 :          19 :         std::vector<unsigned char> vchSig;
    1051   [ +  -  +  -  :          38 :         BOOST_CHECK(key.Sign(hash, vchSig));
             +  -  +  - ]
    1052         [ +  - ]:          19 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
    1053         [ -  + ]:          19 :         result << vchSig;
    1054                 :          19 :     }
    1055                 :          12 :     return result;
    1056                 :           0 : }
    1057                 :             : static CScript
    1058                 :           3 : sign_multisig(const CScript& scriptPubKey, const CKey& key, const CTransaction& transaction)
    1059                 :             : {
    1060                 :           3 :     std::vector<CKey> keys;
    1061         [ +  - ]:           3 :     keys.push_back(key);
    1062         [ +  - ]:           6 :     return sign_multisig(scriptPubKey, keys, transaction);
    1063                 :           3 : }
    1064                 :             : 
    1065   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1066                 :             : {
    1067                 :           1 :     ScriptError err;
    1068                 :           1 :     CKey key1 = GenerateRandomKey();
    1069                 :           1 :     CKey key2 = GenerateRandomKey(/*compressed=*/false);
    1070                 :           1 :     CKey key3 = GenerateRandomKey();
    1071                 :             : 
    1072                 :           1 :     CScript scriptPubKey12;
    1073   [ +  -  +  -  :           4 :     scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1074                 :             : 
    1075   [ +  -  +  - ]:           1 :     const CTransaction txFrom12{BuildCreditingTransaction(scriptPubKey12)};
    1076         [ +  - ]:           2 :     CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom12);
    1077                 :             : 
    1078   [ +  -  +  - ]:           1 :     CScript goodsig1 = sign_multisig(scriptPubKey12, key1, CTransaction(txTo12));
    1079   [ +  -  +  -  :           2 :     BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1080   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1081         [ +  - ]:           1 :     txTo12.vout[0].nValue = 2;
    1082   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1083   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1084                 :             : 
    1085   [ +  -  +  - ]:           1 :     CScript goodsig2 = sign_multisig(scriptPubKey12, key2, CTransaction(txTo12));
    1086   [ +  -  +  -  :           2 :     BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1087   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1088                 :             : 
    1089   [ +  -  +  - ]:           1 :     CScript badsig1 = sign_multisig(scriptPubKey12, key3, CTransaction(txTo12));
    1090   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1091   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1092                 :           3 : }
    1093                 :             : 
    1094   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1095                 :             : {
    1096                 :           1 :     ScriptError err;
    1097                 :           1 :     CKey key1 = GenerateRandomKey();
    1098                 :           1 :     CKey key2 = GenerateRandomKey(/*compressed=*/false);
    1099                 :           1 :     CKey key3 = GenerateRandomKey();
    1100                 :           1 :     CKey key4 = GenerateRandomKey(/*compressed=*/false);
    1101                 :             : 
    1102                 :           1 :     CScript scriptPubKey23;
    1103   [ +  -  +  -  :           5 :     scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1104                 :             : 
    1105   [ +  -  +  - ]:           1 :     const CTransaction txFrom23{BuildCreditingTransaction(scriptPubKey23)};
    1106         [ +  - ]:           2 :     CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom23);
    1107                 :             : 
    1108                 :           1 :     std::vector<CKey> keys;
    1109   [ +  -  +  - ]:           1 :     keys.push_back(key1); keys.push_back(key2);
    1110   [ +  -  +  - ]:           1 :     CScript goodsig1 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1111   [ +  -  +  -  :           2 :     BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1112   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1113                 :             : 
    1114                 :           1 :     keys.clear();
    1115   [ +  -  +  - ]:           1 :     keys.push_back(key1); keys.push_back(key3);
    1116   [ +  -  +  - ]:           1 :     CScript goodsig2 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1117   [ +  -  +  -  :           2 :     BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1118   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1119                 :             : 
    1120                 :           1 :     keys.clear();
    1121   [ +  -  +  - ]:           1 :     keys.push_back(key2); keys.push_back(key3);
    1122   [ +  -  +  - ]:           1 :     CScript goodsig3 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1123   [ +  -  +  -  :           2 :     BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1124   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1125                 :             : 
    1126                 :           1 :     keys.clear();
    1127   [ +  -  +  - ]:           1 :     keys.push_back(key2); keys.push_back(key2); // Can't reuse sig
    1128   [ +  -  +  - ]:           1 :     CScript badsig1 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1129   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1130   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1131                 :             : 
    1132                 :           1 :     keys.clear();
    1133   [ +  -  +  - ]:           1 :     keys.push_back(key2); keys.push_back(key1); // sigs must be in correct order
    1134   [ +  -  +  - ]:           1 :     CScript badsig2 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1135   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1136   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1137                 :             : 
    1138                 :           1 :     keys.clear();
    1139   [ +  -  +  - ]:           1 :     keys.push_back(key3); keys.push_back(key2); // sigs must be in correct order
    1140   [ +  -  +  - ]:           1 :     CScript badsig3 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1141   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1142   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1143                 :             : 
    1144                 :           1 :     keys.clear();
    1145   [ +  -  +  - ]:           1 :     keys.push_back(key4); keys.push_back(key2); // sigs must match pubkeys
    1146   [ +  -  +  - ]:           1 :     CScript badsig4 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1147   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1148   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1149                 :             : 
    1150                 :           1 :     keys.clear();
    1151   [ +  -  +  - ]:           1 :     keys.push_back(key1); keys.push_back(key4); // sigs must match pubkeys
    1152   [ +  -  +  - ]:           1 :     CScript badsig5 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1153   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1154   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
                   +  - ]
    1155                 :             : 
    1156                 :           1 :     keys.clear(); // Must have signatures
    1157   [ +  -  +  - ]:           1 :     CScript badsig6 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
    1158   [ +  -  +  -  :           2 :     BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
             +  -  +  - ]
    1159   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_INVALID_STACK_OPERATION, ScriptErrorString(err));
                   +  - ]
    1160                 :           3 : }
    1161                 :             : 
    1162                 :             : /** Return the TxoutType of a script without exposing Solver details. */
    1163                 :           7 : static TxoutType GetTxoutType(const CScript& output_script)
    1164                 :             : {
    1165                 :           7 :     std::vector<std::vector<uint8_t>> unused;
    1166         [ +  - ]:          14 :     return Solver(output_script, unused);
    1167                 :           7 : }
    1168                 :             : 
    1169                 :             : #define CHECK_SCRIPT_STATIC_SIZE(script, expected_size)                   \
    1170                 :             :     do {                                                                  \
    1171                 :             :         BOOST_CHECK_EQUAL((script).size(), (expected_size));              \
    1172                 :             :         BOOST_CHECK_EQUAL((script).capacity(), CScriptBase::STATIC_SIZE); \
    1173                 :             :         BOOST_CHECK_EQUAL((script).allocated_memory(), 0);                \
    1174                 :             :     } while (0)
    1175                 :             : 
    1176                 :             : #define CHECK_SCRIPT_DYNAMIC_SIZE(script, expected_size, expected_extra)                 \
    1177                 :             :     do {                                                                 \
    1178                 :             :         BOOST_CHECK_EQUAL((script).size(), (expected_size));             \
    1179                 :             :         BOOST_CHECK_EQUAL((script).capacity(), (expected_extra));         \
    1180                 :             :         BOOST_CHECK_EQUAL((script).allocated_memory(), (expected_extra)); \
    1181                 :             :     } while (0)
    1182                 :             : 
    1183   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_size_and_capacity_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1184                 :             : {
    1185         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(CompressedScript), 40);
    1186         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(CScriptBase), 40);
    1187         [ +  - ]:           1 :     BOOST_CHECK_NE(sizeof(CScriptBase), sizeof(prevector<CScriptBase::STATIC_SIZE + 1, uint8_t>)); // CScriptBase size should be set to avoid wasting space in padding
    1188         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(CScript), 40);
    1189         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(CTxOut), 48);
    1190                 :             : 
    1191                 :           1 :     CKey dummy_key;
    1192         [ +  - ]:           1 :     dummy_key.MakeNewKey(/*fCompressed=*/true);
    1193         [ +  - ]:           1 :     const CPubKey dummy_pubkey{dummy_key.GetPubKey()};
    1194                 :             : 
    1195                 :             :     // Small OP_RETURN has direct allocation
    1196                 :           1 :     {
    1197   [ +  -  +  - ]:           2 :         const auto script{CScript() << OP_RETURN << std::vector<uint8_t>(10, 0xaa)};
    1198   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::NULL_DATA);
                   +  - ]
    1199   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 12);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1200                 :           0 :     }
    1201                 :             : 
    1202                 :             :     // P2WPKH has direct allocation
    1203                 :           1 :     {
    1204   [ +  -  +  -  :           1 :         const auto script{GetScriptForDestination(WitnessV0KeyHash{PKHash{dummy_pubkey}})};
                   +  - ]
    1205   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::WITNESS_V0_KEYHASH);
                   +  - ]
    1206   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 22);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1207                 :           0 :     }
    1208                 :             : 
    1209                 :             :     // P2SH has direct allocation
    1210                 :           1 :     {
    1211   [ +  -  +  -  :           2 :         const auto script{GetScriptForDestination(ScriptHash{CScript{} << OP_TRUE})};
                   +  - ]
    1212   [ +  -  +  -  :           2 :         BOOST_CHECK(script.IsPayToScriptHash());
             +  -  +  - ]
    1213   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 23);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1214                 :           0 :     }
    1215                 :             : 
    1216                 :             :     // P2PKH has direct allocation
    1217                 :           1 :     {
    1218   [ +  -  +  - ]:           1 :         const auto script{GetScriptForDestination(PKHash{dummy_pubkey})};
    1219   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::PUBKEYHASH);
                   +  - ]
    1220   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 25);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1221                 :           0 :     }
    1222                 :             : 
    1223                 :             :     // P2WSH has direct allocation
    1224                 :           1 :     {
    1225   [ +  -  +  -  :           2 :         const auto script{GetScriptForDestination(WitnessV0ScriptHash{CScript{} << OP_TRUE})};
                   +  - ]
    1226   [ +  -  +  -  :           2 :         BOOST_CHECK(script.IsPayToWitnessScriptHash());
             +  -  +  - ]
    1227   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 34);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1228                 :           0 :     }
    1229                 :             : 
    1230                 :             :     // P2TR has direct allocation
    1231                 :           1 :     {
    1232         [ +  - ]:           1 :         const auto script{GetScriptForDestination(WitnessV1Taproot{XOnlyPubKey{dummy_pubkey}})};
    1233   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::WITNESS_V1_TAPROOT);
                   +  - ]
    1234   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 34);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1235                 :           0 :     }
    1236                 :             : 
    1237                 :             :     // Compressed P2PK has direct allocation
    1238                 :           1 :     {
    1239         [ +  - ]:           1 :         const auto script{GetScriptForRawPubKey(dummy_pubkey)};
    1240   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::PUBKEY);
                   +  - ]
    1241   [ +  -  -  +  :           1 :         CHECK_SCRIPT_STATIC_SIZE(script, 35);
          +  -  +  -  -  
          +  +  -  +  -  
             -  +  +  - ]
    1242                 :           0 :     }
    1243                 :             : 
    1244                 :             :     // Uncompressed P2PK needs extra allocation
    1245                 :           1 :     {
    1246                 :           1 :         CKey uncompressed_key;
    1247         [ +  - ]:           1 :         uncompressed_key.MakeNewKey(/*fCompressed=*/false);
    1248         [ +  - ]:           1 :         const CPubKey uncompressed_pubkey{uncompressed_key.GetPubKey()};
    1249                 :             : 
    1250         [ +  - ]:           1 :         const auto script{GetScriptForRawPubKey(uncompressed_pubkey)};
    1251   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::PUBKEY);
                   +  - ]
    1252   [ +  -  +  -  :           4 :         CHECK_SCRIPT_DYNAMIC_SIZE(script, 67, 67);
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1253                 :           1 :     }
    1254                 :             : 
    1255                 :             :     // Bare multisig needs extra allocation
    1256                 :           1 :     {
    1257   [ +  -  +  -  :           2 :         const auto script{GetScriptForMultisig(1, std::vector{2, dummy_pubkey})};
                   +  - ]
    1258   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(GetTxoutType(script), TxoutType::MULTISIG);
                   +  - ]
    1259   [ +  -  +  -  :           4 :         CHECK_SCRIPT_DYNAMIC_SIZE(script, 71, 103);
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1260                 :           1 :     }
    1261                 :           1 : }
    1262                 :             : 
    1263                 :             : /* Wrapper around ProduceSignature to combine two scriptsigs */
    1264                 :          17 : SignatureData CombineSignatures(const CTxOut& txout, const CMutableTransaction& tx, const SignatureData& scriptSig1, const SignatureData& scriptSig2)
    1265                 :             : {
    1266                 :          17 :     SignatureData data;
    1267   [ +  -  +  - ]:          17 :     data.MergeSignatureData(scriptSig1);
    1268   [ +  -  +  - ]:          17 :     data.MergeSignatureData(scriptSig2);
    1269   [ +  -  +  - ]:          17 :     ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(tx, 0, txout.nValue, {.sighash_type = SIGHASH_DEFAULT}), txout.scriptPubKey, data);
    1270                 :          17 :     return data;
    1271                 :           0 : }
    1272                 :             : 
    1273   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_combineSigs)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1274                 :             : {
    1275                 :             :     // Test the ProduceSignature's ability to combine signatures function
    1276                 :           1 :     FillableSigningProvider keystore;
    1277                 :           1 :     std::vector<CKey> keys;
    1278                 :           1 :     std::vector<CPubKey> pubkeys;
    1279         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
    1280                 :             :     {
    1281                 :           3 :         CKey key = GenerateRandomKey(/*compressed=*/i%2 == 1);
    1282         [ +  - ]:           3 :         keys.push_back(key);
    1283         [ +  - ]:           3 :         pubkeys.push_back(key.GetPubKey());
    1284   [ +  -  +  -  :           6 :         BOOST_CHECK(keystore.AddKey(key));
                   +  - ]
    1285                 :           3 :     }
    1286                 :             : 
    1287   [ +  -  +  -  :           2 :     CMutableTransaction txFrom = BuildCreditingTransaction(GetScriptForDestination(PKHash(keys[0].GetPubKey())));
             +  -  +  - ]
    1288   [ +  -  +  - ]:           2 :     CMutableTransaction txTo = BuildSpendingTransaction(CScript(), CScriptWitness(), CTransaction(txFrom));
    1289                 :           1 :     CScript& scriptPubKey = txFrom.vout[0].scriptPubKey;
    1290                 :           1 :     SignatureData scriptSig;
    1291                 :             : 
    1292                 :           1 :     SignatureData empty;
    1293         [ +  - ]:           1 :     SignatureData combined = CombineSignatures(txFrom.vout[0], txTo, empty, empty);
    1294   [ +  -  -  +  :           2 :     BOOST_CHECK(combined.scriptSig.empty());
                   +  - ]
    1295                 :             : 
    1296                 :             :     // Single signature case:
    1297                 :           1 :     SignatureData dummy;
    1298   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL, dummy)); // changes scriptSig
          +  -  +  -  +  
                      - ]
    1299         [ +  - ]:           1 :     scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
    1300         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, scriptSig, empty);
    1301   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
                   +  - ]
    1302         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, empty, scriptSig);
    1303   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
                   +  - ]
    1304         [ +  - ]:           1 :     SignatureData scriptSigCopy = scriptSig;
    1305                 :             :     // Signing again will give a different, valid signature:
    1306                 :           1 :     SignatureData dummy_b;
    1307   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL, dummy_b));
          +  -  +  -  +  
                      - ]
    1308         [ +  - ]:           1 :     scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
    1309         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, scriptSigCopy, scriptSig);
    1310   [ +  -  -  +  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSigCopy.scriptSig || combined.scriptSig == scriptSig.scriptSig);
          -  -  +  -  +  
                      - ]
    1311                 :             : 
    1312                 :             :     // P2SH, single-signature case:
    1313   [ +  -  +  -  :           2 :     CScript pkSingle; pkSingle << ToByteVector(keys[0].GetPubKey()) << OP_CHECKSIG;
                   +  - ]
    1314   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(pkSingle));
             +  -  +  - ]
    1315   [ +  -  +  - ]:           2 :     scriptPubKey = GetScriptForDestination(ScriptHash(pkSingle));
    1316                 :           1 :     SignatureData dummy_c;
    1317   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL, dummy_c));
          +  -  +  -  +  
                      - ]
    1318         [ +  - ]:           1 :     scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
    1319         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, scriptSig, empty);
    1320   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
                   +  - ]
    1321         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, empty, scriptSig);
    1322   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
                   +  - ]
    1323         [ +  - ]:           1 :     scriptSigCopy = scriptSig;
    1324                 :           1 :     SignatureData dummy_d;
    1325   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL, dummy_d));
          +  -  +  -  +  
                      - ]
    1326         [ +  - ]:           1 :     scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
    1327         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, scriptSigCopy, scriptSig);
    1328   [ +  -  -  +  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSigCopy.scriptSig || combined.scriptSig == scriptSig.scriptSig);
          -  -  +  -  +  
                      - ]
    1329                 :             : 
    1330                 :             :     // Hardest case:  Multisig 2-of-3
    1331         [ +  - ]:           2 :     scriptPubKey = GetScriptForMultisig(2, pubkeys);
    1332   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(scriptPubKey));
                   +  - ]
    1333                 :           1 :     SignatureData dummy_e;
    1334   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL, dummy_e));
          +  -  +  -  +  
                      - ]
    1335         [ +  - ]:           1 :     scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
    1336         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, scriptSig, empty);
    1337   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
                   +  - ]
    1338         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, empty, scriptSig);
    1339   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
                   +  - ]
    1340                 :             : 
    1341                 :             :     // A couple of partially-signed versions:
    1342                 :           1 :     std::vector<unsigned char> sig1;
    1343         [ +  - ]:           1 :     uint256 hash1 = SignatureHash(scriptPubKey, txTo, 0, SIGHASH_ALL, 0, SigVersion::BASE);
    1344   [ +  -  +  -  :           2 :     BOOST_CHECK(keys[0].Sign(hash1, sig1));
             +  -  +  - ]
    1345         [ +  - ]:           1 :     sig1.push_back(SIGHASH_ALL);
    1346                 :           1 :     std::vector<unsigned char> sig2;
    1347         [ +  - ]:           1 :     uint256 hash2 = SignatureHash(scriptPubKey, txTo, 0, SIGHASH_NONE, 0, SigVersion::BASE);
    1348   [ +  -  +  -  :           2 :     BOOST_CHECK(keys[1].Sign(hash2, sig2));
             +  -  +  - ]
    1349         [ +  - ]:           1 :     sig2.push_back(SIGHASH_NONE);
    1350                 :           1 :     std::vector<unsigned char> sig3;
    1351         [ +  - ]:           1 :     uint256 hash3 = SignatureHash(scriptPubKey, txTo, 0, SIGHASH_SINGLE, 0, SigVersion::BASE);
    1352   [ +  -  +  -  :           2 :     BOOST_CHECK(keys[2].Sign(hash3, sig3));
             +  -  +  - ]
    1353         [ +  - ]:           1 :     sig3.push_back(SIGHASH_SINGLE);
    1354                 :             : 
    1355                 :             :     // Not fussy about order (or even existence) of placeholders or signatures:
    1356   [ +  -  -  +  :           1 :     CScript partial1a = CScript() << OP_0 << sig1 << OP_0;
                   +  - ]
    1357   [ +  -  +  -  :           1 :     CScript partial1b = CScript() << OP_0 << OP_0 << sig1;
                   -  + ]
    1358   [ +  -  -  + ]:           1 :     CScript partial2a = CScript() << OP_0 << sig2;
    1359   [ -  +  +  - ]:           1 :     CScript partial2b = CScript() << sig2 << OP_0;
    1360         [ -  + ]:           1 :     CScript partial3a = CScript() << sig3;
    1361   [ +  -  +  -  :           1 :     CScript partial3b = CScript() << OP_0 << OP_0 << sig3;
                   -  + ]
    1362   [ +  -  -  +  :           1 :     CScript partial3c = CScript() << OP_0 << sig3 << OP_0;
                   +  - ]
    1363   [ +  -  -  +  :           1 :     CScript complete12 = CScript() << OP_0 << sig1 << sig2;
                   -  + ]
    1364   [ +  -  -  +  :           1 :     CScript complete13 = CScript() << OP_0 << sig1 << sig3;
                   -  + ]
    1365   [ +  -  -  +  :           1 :     CScript complete23 = CScript() << OP_0 << sig2 << sig3;
                   -  + ]
    1366                 :           1 :     SignatureData partial1_sigs;
    1367   [ +  -  +  -  :           2 :     partial1_sigs.signatures.emplace(keys[0].GetPubKey().GetID(), SigPair(keys[0].GetPubKey(), sig1));
             +  -  +  - ]
    1368                 :           1 :     SignatureData partial2_sigs;
    1369   [ +  -  +  -  :           2 :     partial2_sigs.signatures.emplace(keys[1].GetPubKey().GetID(), SigPair(keys[1].GetPubKey(), sig2));
             +  -  +  - ]
    1370                 :           1 :     SignatureData partial3_sigs;
    1371   [ +  -  +  -  :           2 :     partial3_sigs.signatures.emplace(keys[2].GetPubKey().GetID(), SigPair(keys[2].GetPubKey(), sig3));
             +  -  +  - ]
    1372                 :             : 
    1373         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial1_sigs, partial1_sigs);
    1374   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == partial1a);
                   +  - ]
    1375         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial1_sigs, partial2_sigs);
    1376   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == complete12);
                   +  - ]
    1377         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial2_sigs, partial1_sigs);
    1378   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == complete12);
                   +  - ]
    1379         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial1_sigs, partial2_sigs);
    1380   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == complete12);
                   +  - ]
    1381         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial3_sigs, partial1_sigs);
    1382   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == complete13);
                   +  - ]
    1383         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial2_sigs, partial3_sigs);
    1384   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == complete23);
                   +  - ]
    1385         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial3_sigs, partial2_sigs);
    1386   [ +  -  +  -  :           2 :     BOOST_CHECK(combined.scriptSig == complete23);
                   +  - ]
    1387         [ +  - ]:           1 :     combined = CombineSignatures(txFrom.vout[0], txTo, partial3_sigs, partial3_sigs);
    1388   [ +  -  +  - ]:           2 :     BOOST_CHECK(combined.scriptSig == partial3c);
    1389                 :           3 : }
    1390                 :             : 
    1391                 :             : /**
    1392                 :             :  * Reproduction of an exception incorrectly raised when parsing a public key inside a TapMiniscript.
    1393                 :             :  */
    1394   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(sign_invalid_miniscript)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1395                 :             : {
    1396                 :           1 :     FillableSigningProvider keystore;
    1397                 :           1 :     SignatureData sig_data;
    1398   [ +  -  +  - ]:           1 :     CMutableTransaction prev, curr;
    1399                 :             : 
    1400                 :             :     // Create a Taproot output which contains a leaf in which a non-32 bytes push is used where a public key is expected
    1401                 :             :     // by the Miniscript parser. This offending Script was found by the RPC fuzzer.
    1402                 :           1 :     const auto invalid_pubkey{"173d36c8c9c9c9ffffffffffff0200000000021e1e37373721361818181818181e1e1e1e19000000000000000000b19292929292926b006c9b9b9292"_hex_u8};
    1403         [ +  - ]:           1 :     TaprootBuilder builder;
    1404         [ +  - ]:           1 :     builder.Add(0, {invalid_pubkey}, 0xc0);
    1405         [ +  - ]:           1 :     builder.Finalize(XOnlyPubKey::NUMS_H);
    1406   [ +  -  +  -  :           2 :     prev.vout.emplace_back(0, GetScriptForDestination(builder.GetOutput()));
                   +  - ]
    1407   [ +  -  +  - ]:           1 :     curr.vin.emplace_back(COutPoint{prev.GetHash(), 0});
    1408         [ +  - ]:           1 :     sig_data.tr_spenddata = builder.GetSpendData();
    1409                 :             : 
    1410                 :             :     // SignSignature can fail but it shouldn't raise an exception (nor crash).
    1411   [ +  -  +  -  :           3 :     BOOST_CHECK(!SignSignature(keystore, CTransaction(prev), curr, 0, SIGHASH_ALL, sig_data));
             +  -  +  - ]
    1412                 :           3 : }
    1413                 :             : 
    1414                 :             : /* P2A input should be considered signed. */
    1415   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(sign_paytoanchor)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1416                 :             : {
    1417                 :           1 :     FillableSigningProvider keystore;
    1418                 :           1 :     SignatureData sig_data;
    1419   [ +  -  +  - ]:           1 :     CMutableTransaction prev, curr;
    1420   [ +  -  +  -  :           2 :     prev.vout.emplace_back(0, GetScriptForDestination(PayToAnchor{}));
                   +  - ]
    1421                 :             : 
    1422   [ +  -  +  - ]:           1 :     curr.vin.emplace_back(COutPoint{prev.GetHash(), 0});
    1423                 :             : 
    1424   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(prev), curr, 0, SIGHASH_ALL, sig_data));
             +  -  +  - ]
    1425                 :           2 : }
    1426                 :             : 
    1427   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_standard_push)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1428                 :             : {
    1429                 :           1 :     ScriptError err;
    1430         [ +  + ]:       67001 :     for (int i=0; i<67000; i++) {
    1431                 :       67000 :         CScript script;
    1432         [ +  - ]:       67000 :         script << i;
    1433   [ +  -  +  -  :      134000 :         BOOST_CHECK_MESSAGE(script.IsPushOnly(), "Number " << i << " is not pure push.");
             +  -  +  - ]
    1434   [ +  -  +  -  :      134000 :         BOOST_CHECK_MESSAGE(VerifyScript(script, CScript() << OP_1, nullptr, SCRIPT_VERIFY_MINIMALDATA, BaseSignatureChecker(), &err), "Number " << i << " push is not minimal data.");
          +  -  +  -  +  
                      - ]
    1435   [ +  -  +  -  :      134000 :         BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1436                 :       67000 :     }
    1437                 :             : 
    1438         [ +  + ]:         522 :     for (unsigned int i=0; i<=MAX_SCRIPT_ELEMENT_SIZE; i++) {
    1439                 :         521 :         std::vector<unsigned char> data(i, '\111');
    1440                 :         521 :         CScript script;
    1441         [ -  + ]:         521 :         script << data;
    1442   [ +  -  +  -  :        1042 :         BOOST_CHECK_MESSAGE(script.IsPushOnly(), "Length " << i << " is not pure push.");
             +  -  +  - ]
    1443   [ +  -  +  -  :        1042 :         BOOST_CHECK_MESSAGE(VerifyScript(script, CScript() << OP_1, nullptr, SCRIPT_VERIFY_MINIMALDATA, BaseSignatureChecker(), &err), "Length " << i << " push is not minimal data.");
          +  -  +  -  +  
                      - ]
    1444   [ +  -  +  -  :        1042 :         BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
    1445                 :         521 :     }
    1446                 :           1 : }
    1447                 :             : 
    1448   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_IsPushOnly_on_invalid_scripts)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1449                 :             : {
    1450                 :             :     // IsPushOnly returns false when given a script containing only pushes that
    1451                 :             :     // are invalid due to truncation. IsPushOnly() is consensus critical
    1452                 :             :     // because P2SH evaluation uses it, although this specific behavior should
    1453                 :             :     // not be consensus critical as the P2SH evaluation would fail first due to
    1454                 :             :     // the invalid push. Still, it doesn't hurt to test it explicitly.
    1455                 :           1 :     static const unsigned char direct[] = { 1 };
    1456   [ +  -  +  - ]:           2 :     BOOST_CHECK(!CScript(direct, direct+sizeof(direct)).IsPushOnly());
    1457                 :           1 : }
    1458                 :             : 
    1459   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_CheckMinimalPush_boundary)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1460                 :             : {
    1461                 :             :     // Test the boundary at exactly 65535 bytes: must use OP_PUSHDATA2, not OP_PUSHDATA4.
    1462                 :           1 :     std::vector<unsigned char> data(65535, '\x42');
    1463   [ +  -  +  -  :           2 :     BOOST_CHECK(CheckMinimalPush(data, OP_PUSHDATA2));
             +  -  +  - ]
    1464   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckMinimalPush(data, OP_PUSHDATA4));
                   +  - ]
    1465                 :           1 : }
    1466                 :             : 
    1467   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_GetScriptAsm)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1468                 :             : {
    1469   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_NOP2, true));
                   +  - ]
    1470   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_CHECKLOCKTIMEVERIFY, true));
                   +  - ]
    1471   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_NOP2));
                   +  - ]
    1472   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_CHECKLOCKTIMEVERIFY));
                   +  - ]
    1473                 :             : 
    1474                 :           1 :     std::string derSig("304502207fa7a6d1e0ee81132a269ad84e68d695483745cde8b541e3bf630749894e342a022100c1f7ab20e13e22fb95281a870f3dcf38d782e53023ee313d741ad0cfbc0c5090");
    1475         [ +  - ]:           1 :     std::string pubKey("03b0da749730dc9b4b1f4a14d6902877a92541f5368778853d9c4a0cb7802dcfb2");
    1476   [ -  +  +  -  :           1 :     std::vector<unsigned char> vchPubKey = ToByteVector(ParseHex(pubKey));
                   +  - ]
    1477                 :             : 
    1478   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "00 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "00")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1479   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "80 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "80")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1480   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "[ALL] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "01")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1481   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "[NONE] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "02")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1482   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "[SINGLE] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "03")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1483   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "[ALL|ANYONECANPAY] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "81")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1484   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "[NONE|ANYONECANPAY] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "82")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1485   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "[SINGLE|ANYONECANPAY] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey, true));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1486                 :             : 
    1487   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "00 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "00")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1488   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "80 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "80")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1489   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "01 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "01")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1490   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "02 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "02")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1491   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "03 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "03")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1492   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "81 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "81")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1493   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "82 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "82")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1494   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(derSig + "83 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey));
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1495                 :           1 : }
    1496                 :             : 
    1497                 :             : template <typename T>
    1498                 :          30 : CScript ToScript(const T& byte_container)
    1499                 :             : {
    1500                 :          30 :     auto span{MakeUCharSpan(byte_container)};
    1501                 :          30 :     return {span.begin(), span.end()};
    1502                 :             : }
    1503                 :             : 
    1504   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_byte_array_u8_vector_equivalence)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1505                 :             : {
    1506   [ +  -  +  - ]:           2 :     const CScript scriptPubKey1 = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex_v_u8 << OP_CHECKSIG;
    1507         [ +  - ]:           1 :     const CScript scriptPubKey2 = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG;
    1508   [ +  -  +  - ]:           2 :     BOOST_CHECK(scriptPubKey1 == scriptPubKey2);
    1509                 :           1 : }
    1510                 :             : 
    1511   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_FindAndDelete)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1512                 :             : {
    1513                 :             :     // Exercise the FindAndDelete functionality
    1514                 :           1 :     CScript s;
    1515                 :           1 :     CScript d;
    1516                 :           1 :     CScript expect;
    1517                 :             : 
    1518   [ +  -  +  - ]:           1 :     s = CScript() << OP_1 << OP_2;
    1519                 :           1 :     d = CScript(); // delete nothing should be a no-op
    1520                 :           1 :     expect = s;
    1521   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
                   +  - ]
    1522   [ +  -  +  -  :           2 :     BOOST_CHECK(s == expect);
                   +  - ]
    1523                 :             : 
    1524   [ +  -  +  -  :           1 :     s = CScript() << OP_1 << OP_2 << OP_3;
                   +  - ]
    1525         [ +  - ]:           1 :     d = CScript() << OP_2;
    1526   [ +  -  +  - ]:           1 :     expect = CScript() << OP_1 << OP_3;
    1527   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1528   [ +  -  +  -  :           2 :     BOOST_CHECK(s == expect);
                   +  - ]
    1529                 :             : 
    1530   [ +  -  +  -  :           1 :     s = CScript() << OP_3 << OP_1 << OP_3 << OP_3 << OP_4 << OP_3;
          +  -  +  -  +  
                -  +  - ]
    1531         [ +  - ]:           1 :     d = CScript() << OP_3;
    1532   [ +  -  +  - ]:           1 :     expect = CScript() << OP_1 << OP_4;
    1533   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 4);
                   +  - ]
    1534   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1535                 :             : 
    1536                 :           1 :     s = ToScript("0302ff03"_hex); // PUSH 0x02ff03 onto stack
    1537                 :           1 :     d = ToScript("0302ff03"_hex);
    1538                 :           1 :     expect = CScript();
    1539   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1540   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1541                 :             : 
    1542                 :           1 :     s = ToScript("0302ff030302ff03"_hex); // PUSH 0x02ff03 PUSH 0x02ff03
    1543                 :           1 :     d = ToScript("0302ff03"_hex);
    1544                 :           1 :     expect = CScript();
    1545   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
                   +  - ]
    1546   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1547                 :             : 
    1548                 :           1 :     s = ToScript("0302ff030302ff03"_hex);
    1549                 :           1 :     d = ToScript("02"_hex);
    1550                 :           1 :     expect = s; // FindAndDelete matches entire opcodes
    1551   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
                   +  - ]
    1552   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1553                 :             : 
    1554                 :           1 :     s = ToScript("0302ff030302ff03"_hex);
    1555                 :           1 :     d = ToScript("ff"_hex);
    1556                 :           1 :     expect = s;
    1557   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
                   +  - ]
    1558   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1559                 :             : 
    1560                 :             :     // This is an odd edge case: strip of the push-three-bytes
    1561                 :             :     // prefix, leaving 02ff03 which is push-two-bytes:
    1562                 :           1 :     s = ToScript("0302ff030302ff03"_hex);
    1563                 :           1 :     d = ToScript("03"_hex);
    1564                 :           1 :     expect = CScript() << "ff03"_hex << "ff03"_hex;
    1565   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
                   +  - ]
    1566   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1567                 :             : 
    1568                 :             :     // Byte sequence that spans multiple opcodes:
    1569                 :           1 :     s = ToScript("02feed5169"_hex); // PUSH(0xfeed) OP_1 OP_VERIFY
    1570                 :           1 :     d = ToScript("feed51"_hex);
    1571                 :           1 :     expect = s;
    1572   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0); // doesn't match 'inside' opcodes
                   +  - ]
    1573   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1574                 :             : 
    1575                 :           1 :     s = ToScript("02feed5169"_hex); // PUSH(0xfeed) OP_1 OP_VERIFY
    1576                 :           1 :     d = ToScript("02feed51"_hex);
    1577                 :           1 :     expect = ToScript("69"_hex);
    1578   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1579   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1580                 :             : 
    1581                 :           1 :     s = ToScript("516902feed5169"_hex);
    1582                 :           1 :     d = ToScript("feed51"_hex);
    1583                 :           1 :     expect = s;
    1584   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
                   +  - ]
    1585   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1586                 :             : 
    1587                 :           1 :     s = ToScript("516902feed5169"_hex);
    1588                 :           1 :     d = ToScript("02feed51"_hex);
    1589                 :           1 :     expect = ToScript("516969"_hex);
    1590   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1591   [ +  -  +  -  :           2 :     BOOST_CHECK(s == expect);
                   +  - ]
    1592                 :             : 
    1593   [ +  -  +  -  :           1 :     s = CScript() << OP_0 << OP_0 << OP_1 << OP_1;
             +  -  +  - ]
    1594   [ +  -  +  - ]:           1 :     d = CScript() << OP_0 << OP_1;
    1595   [ +  -  +  - ]:           1 :     expect = CScript() << OP_0 << OP_1; // FindAndDelete is single-pass
    1596   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1597   [ +  -  +  -  :           2 :     BOOST_CHECK(s == expect);
                   +  - ]
    1598                 :             : 
    1599   [ +  -  +  -  :           1 :     s = CScript() << OP_0 << OP_0 << OP_1 << OP_0 << OP_1 << OP_1;
          +  -  +  -  +  
                -  +  - ]
    1600   [ +  -  +  - ]:           1 :     d = CScript() << OP_0 << OP_1;
    1601   [ +  -  +  - ]:           1 :     expect = CScript() << OP_0 << OP_1; // FindAndDelete is single-pass
    1602   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
                   +  - ]
    1603   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1604                 :             : 
    1605                 :             :     // Another weird edge case:
    1606                 :             :     // End with invalid push (not enough data)...
    1607                 :           1 :     s = ToScript("0003feed"_hex);
    1608                 :           1 :     d = ToScript("03feed"_hex); // ... can remove the invalid push
    1609                 :           1 :     expect = ToScript("00"_hex);
    1610   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1611   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1612                 :             : 
    1613                 :           1 :     s = ToScript("0003feed"_hex);
    1614                 :           1 :     d = ToScript("00"_hex);
    1615                 :           1 :     expect = ToScript("03feed"_hex);
    1616   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
                   +  - ]
    1617   [ +  -  +  - ]:           2 :     BOOST_CHECK(s == expect);
    1618                 :           1 : }
    1619                 :             : 
    1620   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(script_HasValidOps)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1621                 :             : {
    1622                 :             :     // Exercise the HasValidOps functionality
    1623                 :           1 :     CScript script;
    1624                 :           1 :     script = ToScript("76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"_hex); // Normal script
    1625   [ +  -  +  -  :           2 :     BOOST_CHECK(script.HasValidOps());
                   +  - ]
    1626                 :           1 :     script = ToScript("76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"_hex);
    1627   [ +  -  +  -  :           2 :     BOOST_CHECK(script.HasValidOps());
                   +  - ]
    1628                 :           1 :     script = ToScript("ff88ac"_hex); // Script with OP_INVALIDOPCODE explicit
    1629   [ +  -  +  -  :           2 :     BOOST_CHECK(!script.HasValidOps());
                   +  - ]
    1630                 :           1 :     script = ToScript("88acc0"_hex); // Script with undefined opcode
    1631   [ +  -  +  -  :           2 :     BOOST_CHECK(!script.HasValidOps());
                   +  - ]
    1632                 :           1 : }
    1633                 :             : 
    1634   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1635                 :             : {
    1636         [ +  - ]:           1 :     UniValue tests;
    1637   [ +  -  -  + ]:           1 :     Assert(tests.read(json_tests::bip341_wallet_vectors));
    1638                 :             : 
    1639   [ +  -  +  - ]:           1 :     const auto& vectors = tests["keyPathSpending"];
    1640                 :             : 
    1641   [ +  -  +  + ]:           2 :     for (const auto& vec : vectors.getValues()) {
    1642   [ +  -  +  -  :           2 :         auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
          +  -  +  -  +  
             -  -  +  +  
                      - ]
    1643         [ +  - ]:           1 :         CMutableTransaction tx;
    1644   [ -  +  +  - ]:           1 :         SpanReader{txhex} >> TX_WITH_WITNESS(tx);
    1645                 :           1 :         std::vector<CTxOut> utxos;
    1646   [ +  -  +  -  :          10 :         for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
          +  -  +  -  +  
                -  +  + ]
    1647   [ +  -  +  -  :           9 :             auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());
          +  -  -  +  +  
                      - ]
    1648                 :           9 :             CScript script{script_bytes.begin(), script_bytes.end()};
    1649   [ +  -  +  -  :           9 :             CAmount amount{utxo_spent["amountSats"].getInt<int>()};
                   +  - ]
    1650         [ +  - ]:           9 :             utxos.emplace_back(amount, script);
    1651                 :           9 :         }
    1652                 :             : 
    1653                 :           1 :         PrecomputedTransactionData txdata;
    1654   [ +  -  +  - ]:           1 :         txdata.Init(tx, std::vector<CTxOut>{utxos}, true);
    1655                 :             : 
    1656   [ +  -  +  -  :           2 :         BOOST_CHECK(txdata.m_bip341_taproot_ready);
                   +  - ]
    1657   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(txdata.m_spent_amounts_single_hash), vec["intermediary"]["hashAmounts"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1658   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(txdata.m_outputs_single_hash), vec["intermediary"]["hashOutputs"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1659   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(txdata.m_prevouts_single_hash), vec["intermediary"]["hashPrevouts"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1660   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(txdata.m_spent_scripts_single_hash), vec["intermediary"]["hashScriptPubkeys"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1661   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(txdata.m_sequences_single_hash), vec["intermediary"]["hashSequences"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1662                 :             : 
    1663   [ +  -  +  -  :           8 :         for (const auto& input : vec["inputSpending"].getValues()) {
             +  -  +  + ]
    1664   [ +  -  +  -  :           7 :             int txinpos = input["given"]["txinIndex"].getInt<int>();
          +  -  +  -  +  
                      - ]
    1665   [ +  -  +  -  :           7 :             int hashtype = input["given"]["hashType"].getInt<int>();
          +  -  +  -  +  
                      - ]
    1666                 :             : 
    1667                 :             :             // Load key.
    1668   [ +  -  +  -  :          14 :             auto privkey = ParseHex(input["given"]["internalPrivkey"].get_str());
          +  -  +  -  +  
             -  -  +  +  
                      - ]
    1669                 :           7 :             CKey key;
    1670         [ +  - ]:           7 :             key.Set(privkey.begin(), privkey.end(), true);
    1671                 :             : 
    1672                 :             :             // Load Merkle root.
    1673                 :           7 :             uint256 merkle_root;
    1674   [ +  -  +  -  :           7 :             if (!input["given"]["merkleRoot"].isNull()) {
          +  -  +  -  +  
                      + ]
    1675   [ +  -  +  -  :          12 :                 merkle_root = uint256{ParseHex(input["given"]["merkleRoot"].get_str())};
          +  -  +  -  +  
             -  -  +  +  
                      - ]
    1676                 :             :             }
    1677                 :             : 
    1678                 :             :             // Compute and verify (internal) public key.
    1679         [ +  - ]:           7 :             XOnlyPubKey pubkey{key.GetPubKey()};
    1680   [ +  -  +  -  :           7 :             BOOST_CHECK_EQUAL(HexStr(pubkey), input["intermediary"]["internalPubkey"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1681                 :             : 
    1682                 :             :             // Sign and verify signature.
    1683                 :           7 :             FlatSigningProvider provider;
    1684   [ +  -  +  -  :           7 :             provider.keys[key.GetPubKey().GetID()] = key;
             +  -  +  - ]
    1685         [ +  - ]:           7 :             MutableTransactionSignatureCreator creator(tx, txinpos, utxos[txinpos].nValue, &txdata, {.sighash_type = hashtype});
    1686                 :           7 :             std::vector<unsigned char> signature;
    1687   [ +  -  +  -  :          14 :             BOOST_CHECK(creator.CreateSchnorrSig(provider, signature, pubkey, nullptr, &merkle_root, SigVersion::TAPROOT));
             +  -  +  - ]
    1688   [ +  -  +  -  :           7 :             BOOST_CHECK_EQUAL(HexStr(signature), input["expected"]["witness"][0].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
                      - ]
    1689                 :             : 
    1690                 :             :             // We can't observe the tweak used inside the signing logic, so verify by recomputing it.
    1691   [ +  -  +  -  :          20 :             BOOST_CHECK_EQUAL(HexStr(pubkey.ComputeTapTweakHash(merkle_root.IsNull() ? nullptr : &merkle_root)), input["intermediary"]["tweak"].get_str());
          +  -  +  -  +  
          -  +  -  +  +  
          +  -  +  -  +  
                      - ]
    1692                 :             : 
    1693                 :             :             // We can't observe the sighash used inside the signing logic, so verify by recomputing it.
    1694         [ +  - ]:           7 :             ScriptExecutionData sed;
    1695                 :           7 :             sed.m_annex_init = true;
    1696                 :           7 :             sed.m_annex_present = false;
    1697                 :           7 :             uint256 sighash;
    1698   [ +  -  +  -  :          14 :             BOOST_CHECK(SignatureHashSchnorr(sighash, sed, tx, txinpos, hashtype, SigVersion::TAPROOT, txdata, MissingDataBehavior::FAIL));
             +  -  +  - ]
    1699   [ +  -  +  -  :           7 :             BOOST_CHECK_EQUAL(HexStr(sighash), input["intermediary"]["sigHash"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1700                 :             : 
    1701                 :             :             // To verify the sigmsg, hash the expected sigmsg, and compare it with the (expected) sighash.
    1702   [ +  -  +  -  :          21 :             BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << std::span<const uint8_t>{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1703                 :           7 :         }
    1704                 :           2 :     }
    1705                 :           1 : }
    1706                 :             : 
    1707   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(compute_tapbranch)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1708                 :             : {
    1709                 :           1 :     constexpr uint256 hash1{"8ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7"};
    1710                 :           1 :     constexpr uint256 hash2{"f224a923cd0021ab202ab139cc56802ddb92dcfc172b9212261a539df79a112a"};
    1711                 :           1 :     constexpr uint256 result{"a64c5b7b943315f9b805d7a7296bedfcfd08919270a1f7a1466e98f8693d8cd9"};
    1712         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(ComputeTapbranchHash(hash1, hash2), result);
    1713                 :           1 : }
    1714                 :             : 
    1715   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(compute_tapleaf)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1716                 :             : {
    1717                 :           1 :     constexpr uint8_t script[6] = {'f','o','o','b','a','r'};
    1718                 :           1 :     constexpr uint256 tlc0{"edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06"};
    1719                 :           1 :     constexpr uint256 tlc2{"8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a"};
    1720                 :             : 
    1721         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, std::span(script)), tlc0);
    1722         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, std::span(script)), tlc2);
    1723                 :           1 : }
    1724                 :             : 
    1725   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(formatscriptflags)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1726                 :             : {
    1727                 :             :     // quick check that FormatScriptFlags reports any unknown/unexpected bits
    1728         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(FormatScriptFlags(SCRIPT_VERIFY_P2SH), "P2SH");
    1729         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(FormatScriptFlags(SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_TAPROOT), "P2SH,TAPROOT");
    1730         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(FormatScriptFlags(SCRIPT_VERIFY_P2SH | script_verify_flags::from_int(1u<<31)), "P2SH,0x80000000");
    1731         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(FormatScriptFlags(SCRIPT_VERIFY_TAPROOT | script_verify_flags::from_int(1u<<27)), "TAPROOT,0x08000000");
    1732         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(FormatScriptFlags(SCRIPT_VERIFY_TAPROOT | script_verify_flags::from_int((1u<<28) | (1ull<<58))), "TAPROOT,0x400000010000000");
    1733         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(FormatScriptFlags(script_verify_flags::from_int(1u<<26)), "0x04000000");
    1734                 :           1 : }
    1735                 :             : 
    1736                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1