Branch data Line data Source code
# 1 : : // Copyright (c) 2013-2021 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 <consensus/tx_check.h>
# 6 : : #include <consensus/validation.h>
# 7 : : #include <hash.h>
# 8 : : #include <script/interpreter.h>
# 9 : : #include <script/script.h>
# 10 : : #include <serialize.h>
# 11 : : #include <streams.h>
# 12 : : #include <test/data/sighash.json.h>
# 13 : : #include <test/util/setup_common.h>
# 14 : : #include <util/strencodings.h>
# 15 : : #include <util/system.h>
# 16 : : #include <version.h>
# 17 : :
# 18 : : #include <iostream>
# 19 : :
# 20 : : #include <boost/test/unit_test.hpp>
# 21 : :
# 22 : : #include <univalue.h>
# 23 : :
# 24 : : UniValue read_json(const std::string& jsondata);
# 25 : :
# 26 : : // Old script.cpp SignatureHash function
# 27 : : uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
# 28 : 100000 : {
# 29 [ - + ]: 100000 : if (nIn >= txTo.vin.size())
# 30 : 0 : {
# 31 : 0 : return uint256::ONE;
# 32 : 0 : }
# 33 : 100000 : CMutableTransaction txTmp(txTo);
# 34 : :
# 35 : : // In case concatenating two scripts ends up with two codeseparators,
# 36 : : // or an extra one at the end, this prevents all those possible incompatibilities.
# 37 : 100000 : FindAndDelete(scriptCode, CScript(OP_CODESEPARATOR));
# 38 : :
# 39 : : // Blank out other inputs' signatures
# 40 [ + + ]: 350062 : for (unsigned int i = 0; i < txTmp.vin.size(); i++)
# 41 : 250062 : txTmp.vin[i].scriptSig = CScript();
# 42 : 100000 : txTmp.vin[nIn].scriptSig = scriptCode;
# 43 : :
# 44 : : // Blank out some of the outputs
# 45 [ + + ]: 100000 : if ((nHashType & 0x1f) == SIGHASH_NONE)
# 46 : 3194 : {
# 47 : : // Wildcard payee
# 48 : 3194 : txTmp.vout.clear();
# 49 : :
# 50 : : // Let the others update at will
# 51 [ + + ]: 11250 : for (unsigned int i = 0; i < txTmp.vin.size(); i++)
# 52 [ + + ]: 8056 : if (i != nIn)
# 53 : 4862 : txTmp.vin[i].nSequence = 0;
# 54 : 3194 : }
# 55 [ + + ]: 96806 : else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
# 56 : 3046 : {
# 57 : : // Only lock-in the txout payee at same index as txin
# 58 : 3046 : unsigned int nOut = nIn;
# 59 [ - + ]: 3046 : if (nOut >= txTmp.vout.size())
# 60 : 0 : {
# 61 : 0 : return uint256::ONE;
# 62 : 0 : }
# 63 : 3046 : txTmp.vout.resize(nOut+1);
# 64 [ + + ]: 5358 : for (unsigned int i = 0; i < nOut; i++)
# 65 : 2312 : txTmp.vout[i].SetNull();
# 66 : :
# 67 : : // Let the others update at will
# 68 [ + + ]: 10632 : for (unsigned int i = 0; i < txTmp.vin.size(); i++)
# 69 [ + + ]: 7586 : if (i != nIn)
# 70 : 4540 : txTmp.vin[i].nSequence = 0;
# 71 : 3046 : }
# 72 : :
# 73 : : // Blank out other inputs completely, not recommended for open transactions
# 74 [ + + ]: 100000 : if (nHashType & SIGHASH_ANYONECANPAY)
# 75 : 49530 : {
# 76 : 49530 : txTmp.vin[0] = txTmp.vin[nIn];
# 77 : 49530 : txTmp.vin.resize(1);
# 78 : 49530 : }
# 79 : :
# 80 : : // Serialize and hash
# 81 : 100000 : CHashWriter ss(SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
# 82 : 100000 : ss << txTmp << nHashType;
# 83 : 100000 : return ss.GetHash();
# 84 : 100000 : }
# 85 : :
# 86 : 600186 : void static RandomScript(CScript &script) {
# 87 : 600186 : static const opcodetype oplist[] = {OP_FALSE, OP_1, OP_2, OP_3, OP_CHECKSIG, OP_IF, OP_VERIF, OP_RETURN, OP_CODESEPARATOR};
# 88 : 600186 : script = CScript();
# 89 : 600186 : int ops = (InsecureRandRange(10));
# 90 [ + + ]: 3302208 : for (int i=0; i<ops; i++)
# 91 : 2702022 : script << oplist[InsecureRandRange(std::size(oplist))];
# 92 : 600186 : }
# 93 : :
# 94 : : void static RandomTransaction(CMutableTransaction& tx, bool fSingle)
# 95 : 100000 : {
# 96 : 100000 : tx.nVersion = int(InsecureRand32());
# 97 : 100000 : tx.vin.clear();
# 98 : 100000 : tx.vout.clear();
# 99 [ + + ]: 100000 : tx.nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0;
# 100 : 100000 : int ins = (InsecureRandBits(2)) + 1;
# 101 [ + + ]: 100000 : int outs = fSingle ? ins : (InsecureRandBits(2)) + 1;
# 102 [ + + ]: 350062 : for (int in = 0; in < ins; in++) {
# 103 : 250062 : tx.vin.push_back(CTxIn());
# 104 : 250062 : CTxIn &txin = tx.vin.back();
# 105 : 250062 : txin.prevout.hash = InsecureRand256();
# 106 : 250062 : txin.prevout.n = InsecureRandBits(2);
# 107 : 250062 : RandomScript(txin.scriptSig);
# 108 [ + + ]: 250062 : txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : std::numeric_limits<uint32_t>::max();
# 109 : 250062 : }
# 110 [ + + ]: 350124 : for (int out = 0; out < outs; out++) {
# 111 : 250124 : tx.vout.push_back(CTxOut());
# 112 : 250124 : CTxOut &txout = tx.vout.back();
# 113 : 250124 : txout.nValue = InsecureRandRange(100000000);
# 114 : 250124 : RandomScript(txout.scriptPubKey);
# 115 : 250124 : }
# 116 : 100000 : }
# 117 : :
# 118 : : BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
# 119 : :
# 120 : : BOOST_AUTO_TEST_CASE(sighash_test)
# 121 : 2 : {
# 122 : : #if defined(PRINT_SIGHASH_JSON)
# 123 : : std::cout << "[\n";
# 124 : : std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
# 125 : : int nRandomTests = 500;
# 126 : : #else
# 127 : 2 : int nRandomTests = 50000;
# 128 : 2 : #endif
# 129 [ + + ]: 100002 : for (int i=0; i<nRandomTests; i++) {
# 130 : 100000 : int nHashType{int(InsecureRand32())};
# 131 : 100000 : CMutableTransaction txTo;
# 132 : 100000 : RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
# 133 : 100000 : CScript scriptCode;
# 134 : 100000 : RandomScript(scriptCode);
# 135 : 100000 : int nIn = InsecureRandRange(txTo.vin.size());
# 136 : :
# 137 : 100000 : uint256 sh, sho;
# 138 : 100000 : sho = SignatureHashOld(scriptCode, CTransaction(txTo), nIn, nHashType);
# 139 : 100000 : sh = SignatureHash(scriptCode, txTo, nIn, nHashType, 0, SigVersion::BASE);
# 140 : : #if defined(PRINT_SIGHASH_JSON)
# 141 : : CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
# 142 : : ss << txTo;
# 143 : :
# 144 : : std::cout << "\t[\"" ;
# 145 : : std::cout << HexStr(ss) << "\", \"";
# 146 : : std::cout << HexStr(scriptCode) << "\", ";
# 147 : : std::cout << nIn << ", ";
# 148 : : std::cout << nHashType << ", \"";
# 149 : : std::cout << sho.GetHex() << "\"]";
# 150 : : if (i+1 != nRandomTests) {
# 151 : : std::cout << ",";
# 152 : : }
# 153 : : std::cout << "\n";
# 154 : : #endif
# 155 : 100000 : BOOST_CHECK(sh == sho);
# 156 : 100000 : }
# 157 : : #if defined(PRINT_SIGHASH_JSON)
# 158 : : std::cout << "]\n";
# 159 : : #endif
# 160 : 2 : }
# 161 : :
# 162 : : // Goal: check that SignatureHash generates correct hash
# 163 : : BOOST_AUTO_TEST_CASE(sighash_from_data)
# 164 : 2 : {
# 165 : 2 : UniValue tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash)));
# 166 : :
# 167 [ + + ]: 1004 : for (unsigned int idx = 0; idx < tests.size(); idx++) {
# 168 : 1002 : UniValue test = tests[idx];
# 169 : 1002 : std::string strTest = test.write();
# 170 [ - + ]: 1002 : if (test.size() < 1) // Allow for extra stuff (useful for comments)
# 171 : 0 : {
# 172 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 173 : 0 : continue;
# 174 : 0 : }
# 175 [ + + ]: 1002 : if (test.size() == 1) continue; // comment
# 176 : :
# 177 : 1000 : std::string raw_tx, raw_script, sigHashHex;
# 178 : 1000 : int nIn, nHashType;
# 179 : 1000 : uint256 sh;
# 180 : 1000 : CTransactionRef tx;
# 181 : 1000 : CScript scriptCode = CScript();
# 182 : :
# 183 : 1000 : try {
# 184 : : // deserialize test data
# 185 : 1000 : raw_tx = test[0].get_str();
# 186 : 1000 : raw_script = test[1].get_str();
# 187 : 1000 : nIn = test[2].get_int();
# 188 : 1000 : nHashType = test[3].get_int();
# 189 : 1000 : sigHashHex = test[4].get_str();
# 190 : :
# 191 : 1000 : CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION);
# 192 : 1000 : stream >> tx;
# 193 : :
# 194 : 1000 : TxValidationState state;
# 195 : 1000 : BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);
# 196 : 1000 : BOOST_CHECK(state.IsValid());
# 197 : :
# 198 : 1000 : std::vector<unsigned char> raw = ParseHex(raw_script);
# 199 : 1000 : scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
# 200 : 1000 : } catch (...) {
# 201 : 0 : BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
# 202 : 0 : continue;
# 203 : 0 : }
# 204 : :
# 205 : 1000 : sh = SignatureHash(scriptCode, *tx, nIn, nHashType, 0, SigVersion::BASE);
# 206 : 1000 : BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
# 207 : 1000 : }
# 208 : 2 : }
# 209 : : BOOST_AUTO_TEST_SUITE_END()
|