LCOV - code coverage report
Current view: top level - src/test - script_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 91.5 % 1077 985
Test Date: 2024-07-07 05:05:19 Functions: 93.8 % 64 60
Branches: 48.8 % 6532 3190

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

Generated by: LCOV version 2.0-1