Bitcoin Core  27.99.0
P2P Digital Currency
sighash_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-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 <common/system.h>
6 #include <consensus/tx_check.h>
7 #include <consensus/validation.h>
8 #include <hash.h>
9 #include <script/interpreter.h>
10 #include <script/script.h>
11 #include <serialize.h>
12 #include <streams.h>
13 #include <test/data/sighash.json.h>
14 #include <test/util/json.h>
15 #include <test/util/random.h>
16 #include <test/util/setup_common.h>
17 #include <util/strencodings.h>
18 
19 #include <iostream>
20 
21 #include <boost/test/unit_test.hpp>
22 
23 #include <univalue.h>
24 
25 // Old script.cpp SignatureHash function
26 uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
27 {
28  if (nIn >= txTo.vin.size())
29  {
30  return uint256::ONE;
31  }
32  CMutableTransaction txTmp(txTo);
33 
34  // In case concatenating two scripts ends up with two codeseparators,
35  // or an extra one at the end, this prevents all those possible incompatibilities.
37 
38  // Blank out other inputs' signatures
39  for (unsigned int i = 0; i < txTmp.vin.size(); i++)
40  txTmp.vin[i].scriptSig = CScript();
41  txTmp.vin[nIn].scriptSig = scriptCode;
42 
43  // Blank out some of the outputs
44  if ((nHashType & 0x1f) == SIGHASH_NONE)
45  {
46  // Wildcard payee
47  txTmp.vout.clear();
48 
49  // Let the others update at will
50  for (unsigned int i = 0; i < txTmp.vin.size(); i++)
51  if (i != nIn)
52  txTmp.vin[i].nSequence = 0;
53  }
54  else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
55  {
56  // Only lock-in the txout payee at same index as txin
57  unsigned int nOut = nIn;
58  if (nOut >= txTmp.vout.size())
59  {
60  return uint256::ONE;
61  }
62  txTmp.vout.resize(nOut+1);
63  for (unsigned int i = 0; i < nOut; i++)
64  txTmp.vout[i].SetNull();
65 
66  // Let the others update at will
67  for (unsigned int i = 0; i < txTmp.vin.size(); i++)
68  if (i != nIn)
69  txTmp.vin[i].nSequence = 0;
70  }
71 
72  // Blank out other inputs completely, not recommended for open transactions
73  if (nHashType & SIGHASH_ANYONECANPAY)
74  {
75  txTmp.vin[0] = txTmp.vin[nIn];
76  txTmp.vin.resize(1);
77  }
78 
79  // Serialize and hash
80  HashWriter ss{};
81  ss << TX_NO_WITNESS(txTmp) << nHashType;
82  return ss.GetHash();
83 }
84 
85 void static RandomScript(CScript &script) {
87  script = CScript();
88  int ops = (InsecureRandRange(10));
89  for (int i=0; i<ops; i++)
90  script << oplist[InsecureRandRange(std::size(oplist))];
91 }
92 
93 void static RandomTransaction(CMutableTransaction& tx, bool fSingle)
94 {
95  tx.nVersion = int(InsecureRand32());
96  tx.vin.clear();
97  tx.vout.clear();
98  tx.nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0;
99  int ins = (InsecureRandBits(2)) + 1;
100  int outs = fSingle ? ins : (InsecureRandBits(2)) + 1;
101  for (int in = 0; in < ins; in++) {
102  tx.vin.emplace_back();
103  CTxIn &txin = tx.vin.back();
105  txin.prevout.n = InsecureRandBits(2);
106  RandomScript(txin.scriptSig);
107  txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : std::numeric_limits<uint32_t>::max();
108  }
109  for (int out = 0; out < outs; out++) {
110  tx.vout.emplace_back();
111  CTxOut &txout = tx.vout.back();
113  RandomScript(txout.scriptPubKey);
114  }
115 }
116 
117 BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
118 
119 BOOST_AUTO_TEST_CASE(sighash_test)
120 {
121  #if defined(PRINT_SIGHASH_JSON)
122  std::cout << "[\n";
123  std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
124  int nRandomTests = 500;
125  #else
126  int nRandomTests = 50000;
127  #endif
128  for (int i=0; i<nRandomTests; i++) {
129  int nHashType{int(InsecureRand32())};
130  CMutableTransaction txTo;
131  RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
132  CScript scriptCode;
133  RandomScript(scriptCode);
134  int nIn = InsecureRandRange(txTo.vin.size());
135 
136  uint256 sh, sho;
137  sho = SignatureHashOld(scriptCode, CTransaction(txTo), nIn, nHashType);
138  sh = SignatureHash(scriptCode, txTo, nIn, nHashType, 0, SigVersion::BASE);
139  #if defined(PRINT_SIGHASH_JSON)
140  DataStream ss;
141  ss << TX_WITH_WITNESS(txTo);
142 
143  std::cout << "\t[\"" ;
144  std::cout << HexStr(ss) << "\", \"";
145  std::cout << HexStr(scriptCode) << "\", ";
146  std::cout << nIn << ", ";
147  std::cout << nHashType << ", \"";
148  std::cout << sho.GetHex() << "\"]";
149  if (i+1 != nRandomTests) {
150  std::cout << ",";
151  }
152  std::cout << "\n";
153  #endif
154  BOOST_CHECK(sh == sho);
155  }
156  #if defined(PRINT_SIGHASH_JSON)
157  std::cout << "]\n";
158  #endif
159 }
160 
161 // Goal: check that SignatureHash generates correct hash
162 BOOST_AUTO_TEST_CASE(sighash_from_data)
163 {
164  UniValue tests = read_json(json_tests::sighash);
165 
166  for (unsigned int idx = 0; idx < tests.size(); idx++) {
167  const UniValue& test = tests[idx];
168  std::string strTest = test.write();
169  if (test.size() < 1) // Allow for extra stuff (useful for comments)
170  {
171  BOOST_ERROR("Bad test: " << strTest);
172  continue;
173  }
174  if (test.size() == 1) continue; // comment
175 
176  std::string raw_tx, raw_script, sigHashHex;
177  int nIn, nHashType;
178  uint256 sh;
179  CTransactionRef tx;
180  CScript scriptCode = CScript();
181 
182  try {
183  // deserialize test data
184  raw_tx = test[0].get_str();
185  raw_script = test[1].get_str();
186  nIn = test[2].getInt<int>();
187  nHashType = test[3].getInt<int>();
188  sigHashHex = test[4].get_str();
189 
190  DataStream stream(ParseHex(raw_tx));
191  stream >> TX_WITH_WITNESS(tx);
192 
193  TxValidationState state;
194  BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);
195  BOOST_CHECK(state.IsValid());
196 
197  std::vector<unsigned char> raw = ParseHex(raw_script);
198  scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
199  } catch (...) {
200  BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
201  continue;
202  }
203 
204  sh = SignatureHash(scriptCode, *tx, nIn, nHashType, 0, SigVersion::BASE);
205  BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
206  }
207 }
uint32_t n
Definition: transaction.h:32
Txid hash
Definition: transaction.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
const std::vector< CTxIn > vin
Definition: transaction.h:306
An input of a transaction.
Definition: transaction.h:67
uint32_t nSequence
Definition: transaction.h:71
CScript scriptSig
Definition: transaction.h:70
COutPoint prevout
Definition: transaction.h:69
An output of a transaction.
Definition: transaction.h:150
CScript scriptPubKey
Definition: transaction.h:153
CAmount nValue
Definition: transaction.h:152
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
size_t size() const
Definition: univalue.h:70
Int getInt() const
Definition: univalue.h:137
bool IsValid() const
Definition: validation.h:122
std::string GetHex() const
Definition: uint256.cpp:11
iterator end()
Definition: prevector.h:306
iterator insert(iterator pos, const T &value)
Definition: prevector.h:361
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:106
static const uint256 ONE
Definition: uint256.h:112
BOOST_AUTO_TEST_SUITE_END()
int FindAndDelete(CScript &script, const CScript &b)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ SIGHASH_ANYONECANPAY
Definition: interpreter.h:33
@ SIGHASH_NONE
Definition: interpreter.h:31
@ SIGHASH_SINGLE
Definition: interpreter.h:32
UniValue read_json(const std::string &jsondata)
Definition: json.cpp:12
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static constexpr TransactionSerParams TX_NO_WITNESS
Definition: transaction.h:196
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
opcodetype
Script opcodes.
Definition: script.h:73
@ OP_2
Definition: script.h:84
@ OP_IF
Definition: script.h:103
@ OP_CHECKSIG
Definition: script.h:189
@ OP_VERIF
Definition: script.h:105
@ OP_CODESEPARATOR
Definition: script.h:188
@ OP_FALSE
Definition: script.h:76
@ OP_1
Definition: script.h:82
@ OP_3
Definition: script.h:85
@ OP_RETURN
Definition: script.h:110
static uint256 SignatureHashOld(CScript scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
static void RandomTransaction(CMutableTransaction &tx, bool fSingle)
BOOST_AUTO_TEST_CASE(sighash_test)
static void RandomScript(CScript &script)
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:65
Basic testing setup.
Definition: setup_common.h:52
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
static CAmount InsecureRandMoneyAmount()
Definition: random.h:70
static uint64_t InsecureRandRange(uint64_t range)
Definition: random.h:60
static uint256 InsecureRand256()
Definition: random.h:50
static uint64_t InsecureRandBits(int bits)
Definition: random.h:55
static uint32_t InsecureRand32()
Definition: random.h:45
static bool InsecureRandBool()
Definition: random.h:65
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:11
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.