15 #include "validation.h"
20 #include <boost/test/unit_test.hpp>
29 if (nIn >= txTo.
vin.size())
31 printf(
"ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
41 for (
unsigned int i = 0; i < txTmp.vin.size(); i++)
42 txTmp.vin[i].scriptSig =
CScript();
43 txTmp.vin[nIn].scriptSig = scriptCode;
52 for (
unsigned int i = 0; i < txTmp.vin.size(); i++)
54 txTmp.vin[i].nSequence = 0;
59 unsigned int nOut = nIn;
60 if (nOut >= txTmp.vout.size())
62 printf(
"ERROR: SignatureHash() : nOut=%d out of range\n", nOut);
65 txTmp.vout.resize(nOut+1);
66 for (
unsigned int i = 0; i < nOut; i++)
70 for (
unsigned int i = 0; i < txTmp.vin.size(); i++)
72 txTmp.vin[i].nSequence = 0;
78 txTmp.vin[0] = txTmp.vin[nIn];
84 ss << txTmp << nHashType;
88 void static RandomScript(
CScript &script) {
91 int ops = (InsecureRandRange(10));
92 for (
int i=0; i<ops; i++)
93 script << oplist[InsecureRandRange(
sizeof(oplist)/
sizeof(oplist[0]))];
97 bool isSapling = !(InsecureRand32() % 7);
101 tx.
sapData->vShieldedSpend.clear();
102 tx.
sapData->vShieldedOutput.clear();
103 tx.
nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0;
104 int ins = (InsecureRandBits(2)) + 1;
105 int outs = fSingle ? ins : (InsecureRandBits(2)) + 1;
106 for (
int in = 0; in < ins; in++) {
107 tx.
vin.emplace_back();
110 txin.
prevout.
n = InsecureRandBits(2);
112 txin.
nSequence = (InsecureRandBool()) ? InsecureRand32() : (
unsigned int)-1;
114 for (
int out = 0; out < outs; out++) {
115 tx.
vout.emplace_back();
117 txout.
nValue = InsecureRandRange(100000000);
122 int shielded_spends = (InsecureRandBits(2)) + 1;
123 int shielded_outs = (InsecureRandBits(2)) + 1;
124 tx.
sapData->valueBalance = InsecureRandRange(100000000);;
125 for (
int spend = 0; spend < shielded_spends; spend++) {
132 tx.
sapData->vShieldedSpend.push_back(sdesc);
134 for (
int out = 0; out < shielded_outs; out++) {
142 tx.
sapData->vShieldedOutput.push_back(odesc);
151 #if defined(PRINT_SIGHASH_JSON)
153 std::cout <<
"\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
155 int nRandomTests = 50000;
157 #if defined(PRINT_SIGHASH_JSON)
160 for (
int i=0; i<nRandomTests; i++) {
161 int nHashType = InsecureRand32();
165 RandomScript(scriptCode);
166 int nIn = InsecureRandRange(txTo.
vin.size());
169 sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
171 #if defined(PRINT_SIGHASH_JSON)
174 std::cout <<
"\t[\"" ;
175 std::cout <<
HexStr(ss) <<
"\", \"";
176 std::cout <<
HexStr(scriptCode) <<
"\", ";
177 std::cout << nIn <<
", ";
178 std::cout << nHashType <<
", \"";
179 std::cout << sho.
GetHex() <<
"\"]";
180 if (i+1 != nRandomTests) {
189 #if defined(PRINT_SIGHASH_JSON)
198 UniValue tests =
read_json(std::string(json_tests::sighash, json_tests::sighash +
sizeof(json_tests::sighash)));
200 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
202 std::string strTest = test.
write();
205 BOOST_ERROR(
"Bad test: " << strTest);
208 if (test.
size() == 1)
continue;
210 std::string raw_tx, raw_script, sigHashHex;
219 raw_script = test[1].
get_str();
222 sigHashHex = test[4].
get_str();
232 std::vector<unsigned char> raw =
ParseHex(raw_script);
233 scriptCode.
insert(scriptCode.
end(), raw.begin(), raw.end());
235 BOOST_ERROR(
"Bad test, couldn't deserialize data: " << strTest);
239 sh =
SignatureHash(scriptCode, *tx, nIn, nHashType, 0, tx->GetRequiredSigVersion());
240 BOOST_CHECK_MESSAGE(sh.
GetHex() == sigHashHex, strTest);
247 int nRandomTests = 5000;
248 std::vector<uint256> vsh;
249 std::vector<CScript> vScriptCode;
250 for (
int t = 0; t < nRandomTests; t++) {
255 RandomTransaction(_tx,
false);
257 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
258 vScriptCode.emplace_back();
259 RandomScript(vScriptCode.back());
265 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
270 if (mtx.
vin.size() > 0) {
273 for (
int nIn = 0; nIn < (int) tx.
vin.size() - 1; nIn++) {
280 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
281 while (mtx.
vin[nIn].prevout.hash == tx.
vin[nIn].prevout.hash)
282 mtx.
vin[nIn].prevout.hash = InsecureRand256();
290 CTxIn in(InsecureRand256(), InsecureRandBits(2));
292 in.
nSequence = (InsecureRandBool()) ? InsecureRand32() : (
unsigned int)-1;
293 mtx.
vin.emplace_back(in);
295 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
304 mtx.
sapData->vShieldedSpend.pop_back();
305 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
310 int i = InsecureRandRange(tx.
sapData->vShieldedSpend.size());
313 while (mtx.
sapData->vShieldedSpend[i].cv == tx.
sapData->vShieldedSpend[i].cv)
315 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
320 while (mtx.
sapData->vShieldedSpend[i].anchor == tx.
sapData->vShieldedSpend[i].anchor)
322 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
327 while (mtx.
sapData->vShieldedSpend[i].nullifier == tx.
sapData->vShieldedSpend[i].nullifier)
329 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
334 while (mtx.
sapData->vShieldedSpend[i].rk == tx.
sapData->vShieldedSpend[i].rk)
336 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
341 auto zkproof = &mtx.
sapData->vShieldedSpend[i].zkproof;
342 randombytes_buf(zkproof->begin(), zkproof->size());
343 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
348 auto spendAuthSig = &mtx.
sapData->vShieldedSpend[i].spendAuthSig;
349 randombytes_buf(spendAuthSig->begin(), spendAuthSig->size());
350 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
363 mtx.
sapData->vShieldedSpend.push_back(sdesc);
364 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
369 if (mtx.
vout.size() > 0) {
374 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
379 int i = InsecureRandRange(tx.
vout.size());
381 while (mtx.
vout[i].nValue == tx.
vout[i].nValue)
382 mtx.
vout[i].nValue = InsecureRandRange(100000000);
384 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
389 while (mtx.
vout[i].scriptPubKey == tx.
vout[i].scriptPubKey)
390 RandomScript(mtx.
vout[i].scriptPubKey);
392 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
401 out.
nValue = InsecureRandRange(100000000);
403 mtx.
vout.emplace_back(out);
405 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
414 mtx.
sapData->vShieldedOutput.pop_back();
415 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
420 int i = InsecureRandRange(tx.
sapData->vShieldedOutput.size());
423 while (mtx.
sapData->vShieldedOutput[i].cv == tx.
sapData->vShieldedOutput[i].cv)
425 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
430 while (mtx.
sapData->vShieldedOutput[i].cmu == tx.
sapData->vShieldedOutput[i].cmu)
432 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
437 while (mtx.
sapData->vShieldedOutput[i].ephemeralKey == tx.
sapData->vShieldedOutput[i].ephemeralKey)
439 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
444 auto encCiphertext = &mtx.
sapData->vShieldedOutput[i].encCiphertext;
445 randombytes_buf(encCiphertext->begin(), encCiphertext->size());
446 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
451 auto outCiphertext = &mtx.
sapData->vShieldedOutput[i].outCiphertext;
452 randombytes_buf(outCiphertext->begin(), outCiphertext->size());
453 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
458 auto zkproof = &mtx.
sapData->vShieldedOutput[i].zkproof;
459 randombytes_buf(zkproof->begin(), zkproof->size());
460 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
474 mtx.
sapData->vShieldedOutput.push_back(odesc);
475 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
481 (!tx.
sapData->vShieldedOutput.empty() || !tx.
sapData->vShieldedSpend.empty())) {
484 mtx.
sapData->valueBalance = InsecureRandRange(100000000);
485 for (
int nIn = 0; nIn < (int) tx.
vin.size(); nIn++) {
A writer stream (for serialization) that computes a 256-bit hash.
Serialized script, used inside transaction inputs and outputs.
int FindAndDelete(const CScript &b)
The basic transaction that is broadcasted on the network and contained in blocks.
SigVersion GetRequiredSigVersion() const
Optional< SaplingTxData > sapData
std::vector< CTxOut > vout
An input of a transaction.
An output of a transaction.
Capture information about block/transaction validation.
A shielded output to a transaction.
uint256 cmu
The u-coordinate of the note commitment for the output note.
libzcash::SaplingOutCiphertext outCiphertext
A ciphertext component for the encrypted output note.
libzcash::GrothProof zkproof
A zero-knowledge proof using the output circuit.
libzcash::SaplingEncCiphertext encCiphertext
A ciphertext component for the encrypted output note.
uint256 cv
A value commitment to the value of the output note.
uint256 ephemeralKey
A Jubjub public key.
A shielded input to a transaction.
uint256 cv
A value commitment to the value of the input note.
libzcash::GrothProof zkproof
A zero-knowledge proof using the spend circuit.
uint256 anchor
A Merkle root of the Sapling note commitment tree at some block height in the past.
uint256 rk
The randomized public key for spendAuthSig.
uint256 nullifier
The nullifier of the input note.
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string GetHex() const
iterator insert(iterator pos, const T &value)
BOOST_AUTO_TEST_SUITE_END()
uint256 SignatureHash(const CScript &scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK(expr)
uint256 GetRandHash() noexcept
opcodetype
Script opcodes.
UniValue read_json(const std::string &jsondata)
BOOST_AUTO_TEST_CASE(sighash_test)
A mutable version of CTransaction.
Optional< SaplingTxData > sapData
SigVersion GetRequiredSigVersion() const
std::vector< CTxOut > vout
std::shared_ptr< const CTransaction > CTransactionRef
bool CheckTransaction(const CTransaction &tx, CValidationState &state, bool fColdStakingActive)
Transaction validation functions.
const uint256 UINT256_ONE
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< unsigned char > ParseHex(const char *psz)