20 for (
size_t i = 0; i < vch.size(); i++) {
23 if (i == vch.size() - 1 && vch[i] == 0x80) {
36#define stacktop(i) (stack.at(stack.size() + (i)))
37#define altstacktop(i) (altstack.at(altstack.size() + (i)))
38static inline void popstack(std::vector<valtype> &stack) {
40 throw std::runtime_error(
"popstack(): stack empty");
57 while (
static_cast<size_t>(end - pc) >= b.
size() &&
58 std::equal(b.
begin(), b.
end(), pc)) {
63 }
while (script.
GetOp(pc, opcode));
67 script = std::move(result);
74 const std::vector<uint8_t> &vchSig,
123 CScript scriptCode(pbegincodehash, pend);
128 fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode,
flags);
142 : stack(stackIn), script(scriptIn), checker(checkerIn), metrics(metricsIn) {
144 this->pend = scriptIn.
end();
145 this->pbegincodehash = scriptIn.
begin();
169 std::vector<uint8_t> &vchRet)
const {
218 static const valtype vchFalse(0);
219 static const valtype vchTrue(1, 1);
255 stack.push_back(vchPushValue);
256 }
else if (fExec || (
OP_IF <= opcode && opcode <=
OP_ENDIF)) {
297 if (
stack.size() < 1) {
298 return set_error(serror,
340 if (
stack.size() < 1) {
341 return set_error(serror,
381 return set_error(serror,
392 if (
stack.size() < 1) {
393 return set_error(serror,
398 if (vch.size() > 1) {
401 if (vch.size() == 1 && vch[0] != 1) {
416 return set_error(serror,
424 return set_error(serror,
433 if (
stack.size() < 1) {
434 return set_error(serror,
453 if (
stack.size() < 1) {
454 return set_error(serror,
463 return set_error(serror,
472 if (
stack.size() < 2) {
473 return set_error(serror,
482 if (
stack.size() < 2) {
483 return set_error(serror,
488 stack.push_back(vch1);
489 stack.push_back(vch2);
494 if (
stack.size() < 3) {
495 return set_error(serror,
501 stack.push_back(vch1);
502 stack.push_back(vch2);
503 stack.push_back(vch3);
508 if (
stack.size() < 4) {
509 return set_error(serror,
514 stack.push_back(vch1);
515 stack.push_back(vch2);
520 if (
stack.size() < 6) {
521 return set_error(serror,
527 stack.push_back(vch1);
528 stack.push_back(vch2);
533 if (
stack.size() < 4) {
534 return set_error(serror,
543 if (
stack.size() < 1) {
544 return set_error(serror,
549 stack.push_back(vch);
561 if (
stack.size() < 1) {
562 return set_error(serror,
570 if (
stack.size() < 1) {
571 return set_error(serror,
575 stack.push_back(vch);
580 if (
stack.size() < 2) {
581 return set_error(serror,
589 if (
stack.size() < 2) {
590 return set_error(serror,
594 stack.push_back(vch);
601 if (
stack.size() < 2) {
602 return set_error(serror,
609 if (n < 0 || uint64_t(n) >= uint64_t(
stack.size())) {
610 return set_error(serror,
617 stack.push_back(vch);
624 if (
stack.size() < 3) {
625 return set_error(serror,
634 if (
stack.size() < 2) {
635 return set_error(serror,
643 if (
stack.size() < 2) {
644 return set_error(serror,
653 if (
stack.size() < 1) {
654 return set_error(serror,
668 if (
stack.size() < 2) {
669 return set_error(serror,
676 if (vch1.size() != vch2.size()) {
683 for (
size_t i = 0; i < vch1.size(); ++i) {
688 for (
size_t i = 0; i < vch1.size(); ++i) {
693 for (
size_t i = 0; i < vch1.size(); ++i) {
710 if (
stack.size() < 2) {
711 return set_error(serror,
717 bool fEqual = (vch1 == vch2);
726 stack.push_back(fEqual ? vchTrue : vchFalse);
747 if (
stack.size() < 1) {
748 return set_error(serror,
774 assert(!
"invalid opcode");
797 if (
stack.size() < 2) {
798 return set_error(serror,
830 bn = (bn1 != bnZero && bn2 != bnZero);
833 bn = (bn1 != bnZero || bn2 != bnZero);
857 bn = (bn1 < bn2 ? bn1 : bn2);
860 bn = (bn1 > bn2 ? bn1 : bn2);
863 assert(!
"invalid opcode");
881 if (
stack.size() < 3) {
882 return set_error(serror,
888 bool fValue = (bn2 <= bn1 && bn1 < bn3);
892 stack.push_back(fValue ? vchTrue : vchFalse);
904 if (
stack.size() < 1) {
905 return set_error(serror,
915 .
Write(vch.data(), vch.size())
917 }
else if (opcode ==
OP_SHA1) {
919 .
Write(vch.data(), vch.size())
923 .
Write(vch.data(), vch.size())
931 stack.push_back(vchHash);
942 if (
stack.size() < 2) {
943 return set_error(serror,
949 bool fSuccess =
false;
956 stack.push_back(fSuccess ? vchTrue : vchFalse);
969 if (
stack.size() < 3) {
970 return set_error(serror,
984 bool fSuccess =
false;
988 .
Write(vchMessage.data(), vchMessage.size())
1002 stack.push_back(fSuccess ? vchTrue : vchFalse);
1007 return set_error(serror,
1017 const size_t idxKeyCount = 1;
1018 if (
stack.size() < idxKeyCount) {
1019 return set_error(serror,
1022 const int64_t nKeysCount =
1035 const size_t idxTopKey = idxKeyCount + 1;
1038 const size_t idxSigCount = idxTopKey + nKeysCount;
1039 if (
stack.size() < idxSigCount) {
1040 return set_error(serror,
1043 const int64_t nSigsCount =
1047 if (nSigsCount < 0 || nSigsCount > nKeysCount) {
1052 const size_t idxTopSig = idxSigCount + 1;
1055 const size_t idxDummy = idxTopSig + nSigsCount;
1056 if (
stack.size() < idxDummy) {
1057 return set_error(serror,
1067 bool fSuccess =
true;
1073 "Schnorr multisig checkbits implementation "
1074 "assumes < 32 pubkeys.");
1075 uint32_t checkBits = 0;
1088 if (
countBits(checkBits) != uint32_t(nSigsCount)) {
1089 return set_error(serror,
1093 const size_t idxBottomKey = idxTopKey + nKeysCount - 1;
1094 const size_t idxBottomSig = idxTopSig + nSigsCount - 1;
1097 for (
int iSig = 0; iSig < nSigsCount; iSig++, iKey++) {
1098 if ((checkBits >> iKey) == 0) {
1101 return set_error(serror,
1106 while (((checkBits >> iKey) & 0x01) == 0) {
1110 if (iKey >= nKeysCount) {
1123 vchSig,
flags, serror) ||
1144 if ((checkBits >> iKey) != 0) {
1147 return set_error(serror,
1154 for (
int k = 0; k < nSigsCount; k++) {
1159 int nSigsRemaining = nSigsCount;
1160 int nKeysRemaining = nKeysCount;
1161 while (fSuccess && nSigsRemaining > 0) {
1163 -idxTopSig - (nSigsCount - nSigsRemaining));
1165 -idxTopKey - (nKeysCount - nKeysRemaining));
1173 vchSig,
flags, serror) ||
1192 if (nSigsRemaining > nKeysRemaining) {
1197 bool areAllSignaturesNull =
true;
1198 for (
int i = 0; i < nSigsCount; i++) {
1199 if (
stacktop(-idxTopSig - i).size()) {
1200 areAllSignaturesNull =
false;
1208 !areAllSignaturesNull) {
1212 if (!areAllSignaturesNull) {
1222 for (
size_t i = 0; i < idxDummy; i++) {
1226 stack.push_back(fSuccess ? vchTrue : vchFalse);
1231 return set_error(serror,
1242 if (
stack.size() < 2) {
1243 return set_error(serror,
1251 vch1.insert(vch1.end(), vch2.begin(), vch2.end());
1257 if (
stack.size() < 2) {
1258 return set_error(serror,
1268 if (position > data.size()) {
1274 valtype n1(data.begin(), data.begin() + position);
1275 valtype n2(data.begin() + position, data.end());
1284 if (
stack.size() < 1) {
1285 return set_error(serror,
1290 std::reverse(data.begin(), data.end());
1298 if (
stack.size() < 2) {
1299 return set_error(serror,
1316 if (rawnum.size() > size) {
1323 if (rawnum.size() == size) {
1327 uint8_t signbit = 0x00;
1328 if (rawnum.size() > 0) {
1329 signbit = rawnum.back() & 0x80;
1330 rawnum[rawnum.size() - 1] &= 0x7f;
1333 rawnum.reserve(size);
1334 while (rawnum.size() < size - 1) {
1335 rawnum.push_back(0x00);
1338 rawnum.push_back(signbit);
1343 if (
stack.size() < 1) {
1344 return set_error(serror,
1387template <
class T>
class CTransactionSignatureSerializer {
1394 const unsigned int nIn;
1399 CTransactionSignatureSerializer(
const T &txToIn,
1403 : txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1404 sigHashType(sigHashTypeIn) {}
1407 template <
typename S>
void SerializeScriptCode(S &s)
const {
1411 unsigned int nCodeSeparators = 0;
1412 while (scriptCode.
GetOp(it, opcode)) {
1419 while (scriptCode.
GetOp(it, opcode)) {
1421 s.write(
AsBytes(
Span{&itBegin[0], size_t(it - itBegin - 1)}));
1425 if (itBegin != scriptCode.
end()) {
1426 s.write(
AsBytes(
Span{&itBegin[0], size_t(it - itBegin)}));
1431 template <
typename S>
void SerializeInput(S &s,
unsigned int nInput)
const {
1440 if (nInput != nIn) {
1444 SerializeScriptCode(s);
1447 if (nInput != nIn &&
1458 template <
typename S>
1459 void SerializeOutput(S &s,
unsigned int nOutput)
const {
1470 template <
typename S>
void Serialize(S &s)
const {
1474 unsigned int nInputs =
1477 for (
unsigned int nInput = 0; nInput < nInputs; nInput++) {
1478 SerializeInput(s, nInput);
1481 unsigned int nOutputs =
1486 : txTo.vout.size());
1488 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) {
1489 SerializeOutput(s, nOutput);
1496template <
class T>
uint256 GetPrevoutHash(
const T &txTo) {
1498 for (
const auto &txin : txTo.vin) {
1504template <
class T>
uint256 GetSequenceHash(
const T &txTo) {
1506 for (
const auto &txin : txTo.vin) {
1507 ss << txin.nSequence;
1512template <
class T>
uint256 GetOutputsHash(
const T &txTo) {
1514 for (
const auto &txout : txTo.vout) {
1536 return 6 * hash_type.hasForkId() + 3 * hash_type.hasAnyoneCanPay() +
1544 auto &entry = m_cache_entries[CacheIndex(hash_type)];
1545 if (entry.has_value() && (script_code == entry->first)) {
1555 auto &entry = m_cache_entries[CacheIndex(hash_type)];
1556 entry.emplace(script_code, writer);
1565 assert(nIn < txTo.vin.size());
1571 uint32_t newForkValue = sigHashType.
getForkValue() ^ 0xdead;
1572 sigHashType = sigHashType.
withForkValue(0xff0000 | newForkValue);
1575 const bool use_forkid_sighash =
1579 if (!use_forkid_sighash &&
1581 (nIn >= txTo.vout.size())) {
1589 if (sighash_cache && sighash_cache->
Load(sigHashType, scriptCode, ss)) {
1592 return ss.GetHash();
1595 if (use_forkid_sighash) {
1601 hashPrevouts = cache ? cache->
hashPrevouts : GetPrevoutHash(txTo);
1607 hashSequence = cache ? cache->
hashSequence : GetSequenceHash(txTo);
1611 hashOutputs = cache ? cache->
hashOutputs : GetOutputsHash(txTo);
1613 (nIn < txTo.vout.size())) {
1615 inner_ss << txTo.vout[nIn];
1616 hashOutputs = inner_ss.GetHash();
1620 ss << txTo.nVersion;
1627 ss << txTo.vin[nIn].prevout;
1630 ss << txTo.vin[nIn].nSequence;
1634 ss << txTo.nLockTime;
1638 CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn,
1645 if (sighash_cache !=
nullptr) {
1646 sighash_cache->
Store(sigHashType, scriptCode, ss);
1651 return ss.GetHash();
1656 const uint256 &sighash)
const {
1657 if (vchSig.size() == 64) {
1666 const std::vector<uint8_t> &vchSigIn,
const std::vector<uint8_t> &vchPubKey,
1674 std::vector<uint8_t> vchSig(vchSigIn);
1675 if (vchSig.empty()) {
1682 this->txdata,
flags, &m_sighash_cache);
1684 if (!VerifySignature(vchSig, pubkey, sighash)) {
1710 if (nLockTime > int64_t(txTo->nLockTime)) {
1735 const int64_t txToSequence = int64_t(txTo->vin[nIn].nSequence);
1739 if (
static_cast<uint32_t
>(txTo->nVersion) < 2) {
1753 const uint32_t nLockTimeMask =
1755 const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
1756 const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
1774 if (nSequenceMasked > txToSequenceMasked) {
1803 std::vector<valtype> stack, stackCopy;
1811 if (!
EvalScript(stack, scriptPubKey,
flags, checker, metrics, serror)) {
1815 if (stack.empty()) {
1830 swap(stack, stackCopy);
1837 const valtype &pubKeySerialized = stack.back();
1838 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1847 metricsOut = metrics;
1848 return set_success(serror);
1855 if (stack.empty()) {
1872 if (stack.size() != 1) {
1892 "overflow sanity check on max script size");
1894 "overflow sanity check on maximum possible sigchecks "
1895 "from sig+redeem+pub scripts");
1901 metricsOut = metrics;
1902 return set_success(serror);
bool DecodeBitfield(const std::vector< uint8_t > &vch, unsigned size, uint32_t &bitfield, ScriptError *serror)
uint32_t countBits(uint32_t v)
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
virtual bool CheckSequence(const CScriptNum &nSequence) const
virtual bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
CHash160 & Write(Span< const uint8_t > input)
void Finalize(Span< uint8_t > output)
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
CHash256 & Write(Span< const uint8_t > input)
void Finalize(Span< uint8_t > output)
A mutable version of CTransaction.
An encapsulated public key.
bool VerifySchnorr(const uint256 &hash, const std::array< uint8_t, SCHNORR_SIZE > &sig) const
Verify a Schnorr signature (=64 bytes).
bool VerifyECDSA(const uint256 &hash, const std::vector< uint8_t > &vchSig) const
Verify a DER-serialized ECDSA signature (~72 bytes).
A hasher class for RIPEMD-160.
CRIPEMD160 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
void Finalize(uint8_t hash[OUTPUT_SIZE])
CSHA1 & Write(const uint8_t *data, size_t len)
A hasher class for SHA-256.
CSHA256 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
Serialized script, used inside transaction inputs and outputs.
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
bool IsPayToScriptHash() const
bool IsWitnessProgram(int &version, std::vector< uint8_t > &program) const
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
std::vector< uint8_t > getvch() const
static bool IsMinimallyEncoded(const std::vector< uint8_t > &vch, const size_t nMaxNumSize)
static bool MinimallyEncode(std::vector< uint8_t > &data)
The basic transaction that is broadcasted on the network and contained in blocks.
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
If this flag set, CTxIn::nSequence is NOT interpreted as a relative lock-time.
static const uint32_t SEQUENCE_LOCKTIME_MASK
If CTxIn::nSequence encodes a relative lock-time, this mask is applied to extract that lock-time from...
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
If CTxIn::nSequence encodes a relative lock-time and this flag is set, the relative lock-time has uni...
An output of a transaction.
bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const final override
bool CheckSequence(const CScriptNum &nSequence) const final override
bool CheckLockTime(const CScriptNum &nLockTime) const final override
A writer stream (for serialization) that computes a 256-bit hash.
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
CScript::const_iterator pend
bool CheckPostConditions()
std::vector< std::vector< uint8_t > > altstack
CScript::const_iterator pbegincodehash
ScriptError GetScriptError()
bool RunNextOp() noexcept
const ConditionStack & GetConditionStack()
const BaseSignatureChecker & checker
bool CheckPreConditions()
CScript::const_iterator pc
ScriptInterpreter(std::vector< std::vector< uint8_t > > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics)
std::vector< std::vector< uint8_t > > & stack
bool GetNextOp(opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
ScriptExecutionMetrics & metrics
Data structure to cache SHA256 midstates for the ECDSA sighash calculations (bare,...
void Store(const SigHashType &hash_type, const CScript &script_code, const HashWriter &writer) noexcept
Store into this cache object the provided SHA256 midstate.
bool Load(const SigHashType &hash_type, const CScript &script_code, HashWriter &writer) const noexcept
Load into writer the SHA256 midstate if found in this cache.
int CacheIndex(const SigHashType &hash_type) const noexcept
Given a hash_type, find which of the 12 cache entries is to be used.
Signature hash type wrapper class.
BaseSigHashType getBaseType() const
bool hasAnyoneCanPay() const
SigHashType withForkValue(uint32_t forkId) const
uint32_t getForkValue() const
A Span is an object that can refer to a contiguous sequence of objects.
iterator insert(iterator pos, const T &value)
static bool IsOpcodeDisabled(opcodetype opcode, uint32_t flags)
static void CleanupScriptCode(CScript &scriptCode, const std::vector< uint8_t > &vchSig, uint32_t flags)
bool EvalScript(std::vector< valtype > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *serror)
static bool EvalChecksig(const valtype &vchSig, const valtype &vchPubKey, CScript::const_iterator pbegincodehash, CScript::const_iterator pend, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *serror, bool &fSuccess)
Helper for OP_CHECKSIG and OP_CHECKSIGVERIFY.
bool CastToBool(const valtype &vch)
int FindAndDelete(CScript &script, const CScript &b)
static void popstack(std::vector< valtype > &stack)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, SigHashType sigHashType, const Amount amount, const PrecomputedTransactionData *cache, uint32_t flags, SigHashCache *sighash_cache)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror)
Execute an unlocking and locking script together.
#define stacktop(i)
Script is a stack machine (like Forth) that evaluates a predicate returning a bool indicating valid o...
bool CheckMinimalPush(const std::vector< uint8_t > &data, opcodetype opcode)
Check whether the given stack element data would be minimally pushed using the given opcode.
static const unsigned int LOCKTIME_THRESHOLD
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
static const int MAX_SCRIPT_SIZE
opcodetype
Script opcodes.
static const int MAX_STACK_SIZE
static const int MAX_OPS_PER_SCRIPT
static const int MAX_PUBKEYS_PER_MULTISIG
constexpr size_t MAX_SCRIPTNUM_BYTE_SIZE
@ DISCOURAGE_UPGRADABLE_NOPS
@ INVALID_ALTSTACK_OPERATION
@ INVALID_STACK_OPERATION
@ SCRIPT_VERIFY_SIGPUSHONLY
@ SCRIPT_VERIFY_MINIMALIF
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_ENABLE_REPLAY_PROTECTION
@ SCRIPT_ENABLE_SCHNORR_MULTISIG
@ SCRIPT_VERIFY_STRICTENC
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_MINIMALDATA
@ SCRIPT_DISALLOW_SEGWIT_RECOVERY
@ SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
@ SCRIPT_ENABLE_SIGHASH_FORKID
@ SCRIPT_VERIFY_INPUT_SIGCHECKS
void Serialize(Stream &, V)=delete
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
bool CheckTransactionSchnorrSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded Schnorr signature (...
bool CheckDataSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided on some data is properly encoded.
bool CheckTransactionECDSASignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded ECDSA signature.
bool CheckTransactionSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded.
bool CheckPubKeyEncoding(const valtype &vchPubKey, uint32_t flags, ScriptError *serror)
Check that a public key is encoded properly.
std::vector< uint8_t > valtype
Span< const std::byte > AsBytes(Span< T > s) noexcept
Precompute sighash midstate to avoid quadratic hashing.
PrecomputedTransactionData()
Struct for holding cumulative results from executing a script or a sequence of scripts.