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))) 38 static 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,
118 class ConditionStack {
121 static constexpr uint32_t NO_FALSE = std::numeric_limits<uint32_t>::max();
124 uint32_t m_stack_size = 0;
127 uint32_t m_first_false_pos = NO_FALSE;
130 bool empty() {
return m_stack_size == 0; }
131 bool all_true() {
return m_first_false_pos == NO_FALSE; }
132 void push_back(
bool f) {
133 if (m_first_false_pos == NO_FALSE && !f) {
136 m_first_false_pos = m_stack_size;
141 assert(m_stack_size > 0);
143 if (m_first_false_pos == m_stack_size) {
145 m_first_false_pos = NO_FALSE;
149 assert(m_stack_size > 0);
150 if (m_first_false_pos == NO_FALSE) {
153 m_first_false_pos = m_stack_size - 1;
154 }
else if (m_first_false_pos == m_stack_size - 1) {
157 m_first_false_pos = NO_FALSE;
188 CScript scriptCode(pbegincodehash, pend);
193 fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode, flags);
208 static const valtype vchFalse(0);
209 static const valtype vchTrue(1, 1);
216 ConditionStack vfExec;
217 std::vector<valtype> altstack;
227 bool fExec = vfExec.all_true();
232 if (!script.
GetOp(pc, opcode, vchPushValue)) {
250 if (fRequireMinimal &&
254 stack.push_back(vchPushValue);
255 }
else if (fExec || (
OP_IF <= opcode && opcode <=
OP_ENDIF)) {
279 stack.push_back(bn.
getvch());
296 if (stack.size() < 1) {
323 return set_error(serror,
330 return set_error(serror,
342 if (stack.size() < 1) {
357 return set_error(serror,
371 return set_error(serror,
399 if (stack.size() < 1) {
406 if (vch.size() > 1) {
407 return set_error(serror,
410 if (vch.size() == 1 && vch[0] != 1) {
411 return set_error(serror,
421 vfExec.push_back(fValue);
425 if (vfExec.empty()) {
433 if (vfExec.empty()) {
443 if (stack.size() < 1) {
463 if (stack.size() < 1) {
472 if (altstack.size() < 1) {
483 if (stack.size() < 2) {
493 if (stack.size() < 2) {
499 stack.push_back(vch1);
500 stack.push_back(vch2);
505 if (stack.size() < 3) {
512 stack.push_back(vch1);
513 stack.push_back(vch2);
514 stack.push_back(vch3);
519 if (stack.size() < 4) {
525 stack.push_back(vch1);
526 stack.push_back(vch2);
531 if (stack.size() < 6) {
537 stack.erase(stack.end() - 6, stack.end() - 4);
538 stack.push_back(vch1);
539 stack.push_back(vch2);
544 if (stack.size() < 4) {
554 if (stack.size() < 1) {
560 stack.push_back(vch);
567 stack.push_back(bn.getvch());
572 if (stack.size() < 1) {
581 if (stack.size() < 1) {
586 stack.push_back(vch);
591 if (stack.size() < 2) {
595 stack.erase(stack.end() - 2);
600 if (stack.size() < 2) {
605 stack.push_back(vch);
612 if (stack.size() < 2) {
619 if (n < 0 || n >= (
int)stack.size()) {
625 stack.erase(stack.end() - n - 1);
627 stack.push_back(vch);
634 if (stack.size() < 3) {
644 if (stack.size() < 2) {
653 if (stack.size() < 2) {
658 stack.insert(stack.end() - 2, vch);
663 if (stack.size() < 1) {
668 stack.push_back(bn.
getvch());
678 if (stack.size() < 2) {
686 if (vch1.size() != vch2.size()) {
687 return set_error(serror,
694 for (
size_t i = 0; i < vch1.size(); ++i) {
699 for (
size_t i = 0; i < vch1.size(); ++i) {
704 for (
size_t i = 0; i < vch1.size(); ++i) {
721 if (stack.size() < 2) {
729 bool fEqual = (vch1 == vch2);
738 stack.push_back(fEqual ? vchTrue : vchFalse);
743 return set_error(serror,
760 if (stack.size() < 1) {
787 assert(!
"invalid opcode");
791 stack.push_back(bn.
getvch());
810 if (stack.size() < 2) {
829 return set_error(serror,
838 return set_error(serror,
845 bn = (bn1 != bnZero && bn2 != bnZero);
848 bn = (bn1 != bnZero || bn2 != bnZero);
872 bn = (bn1 < bn2 ? bn1 : bn2);
875 bn = (bn1 > bn2 ? bn1 : bn2);
878 assert(!
"invalid opcode");
883 stack.push_back(bn.
getvch());
889 return set_error(serror,
897 if (stack.size() < 3) {
904 bool fValue = (bn2 <= bn1 && bn1 < bn3);
908 stack.push_back(fValue ? vchTrue : vchFalse);
920 if (stack.size() < 1) {
932 .
Write(vch.data(), vch.size())
933 .Finalize(vchHash.data());
934 }
else if (opcode ==
OP_SHA1) {
936 .
Write(vch.data(), vch.size())
937 .Finalize(vchHash.data());
940 .
Write(vch.data(), vch.size())
941 .Finalize(vchHash.data());
944 .
Write(vch.data(), vch.size())
945 .Finalize(vchHash.data());
948 .
Write(vch.data(), vch.size())
949 .Finalize(vchHash.data());
952 stack.push_back(vchHash);
963 if (stack.size() < 2) {
970 bool fSuccess =
false;
972 pend, flags, checker, metrics, serror,
978 stack.push_back(fSuccess ? vchTrue : vchFalse);
983 return set_error(serror,
992 if (stack.size() < 3) {
1008 bool fSuccess =
false;
1009 if (vchSig.size()) {
1012 .
Write(vchMessage.data(), vchMessage.size())
1013 .Finalize(vchHash.data());
1019 return set_error(serror,
1027 stack.push_back(fSuccess ? vchTrue : vchFalse);
1042 const size_t idxKeyCount = 1;
1043 if (stack.size() < idxKeyCount) {
1047 const int nKeysCount =
1050 if (nKeysCount < 0 ||
1054 nOpCount += nKeysCount;
1060 const size_t idxTopKey = idxKeyCount + 1;
1063 const size_t idxSigCount = idxTopKey + nKeysCount;
1064 if (stack.size() < idxSigCount) {
1068 const int nSigsCount =
1071 if (nSigsCount < 0 || nSigsCount > nKeysCount) {
1076 const size_t idxTopSig = idxSigCount + 1;
1079 const size_t idxDummy = idxTopSig + nSigsCount;
1080 if (stack.size() < idxDummy) {
1087 CScript scriptCode(pbegincodehash, pend);
1091 bool fSuccess =
true;
1098 "Schnorr multisig checkbits implementation " 1099 "assumes < 32 pubkeys.");
1100 uint32_t checkBits = 0;
1113 if (
countBits(checkBits) != uint32_t(nSigsCount)) {
1118 const size_t idxBottomKey =
1119 idxTopKey + nKeysCount - 1;
1120 const size_t idxBottomSig =
1121 idxTopSig + nSigsCount - 1;
1124 for (
int iSig = 0; iSig < nSigsCount;
1126 if ((checkBits >> iKey) == 0) {
1134 while (((checkBits >> iKey) & 0x01) == 0) {
1138 if (iKey >= nKeysCount) {
1141 return set_error(serror,
1154 vchSig, flags, serror) ||
1162 if (!checker.
CheckSig(vchSig, vchPubKey,
1163 scriptCode, flags)) {
1168 return set_error(serror,
1177 if ((checkBits >> iKey) != 0) {
1187 for (
int k = 0; k < nSigsCount; k++) {
1192 int nSigsRemaining = nSigsCount;
1193 int nKeysRemaining = nKeysCount;
1194 while (fSuccess && nSigsRemaining > 0) {
1196 -idxTopSig - (nSigsCount - nSigsRemaining));
1198 -idxTopKey - (nKeysCount - nKeysRemaining));
1206 vchSig, flags, serror) ||
1214 bool fOk = checker.
CheckSig(vchSig, vchPubKey,
1226 if (nSigsRemaining > nKeysRemaining) {
1231 bool areAllSignaturesNull =
true;
1232 for (
int i = 0; i < nSigsCount; i++) {
1233 if (
stacktop(-idxTopSig - i).size()) {
1234 areAllSignaturesNull =
false;
1242 !areAllSignaturesNull) {
1243 return set_error(serror,
1247 if (!areAllSignaturesNull) {
1257 for (
size_t i = 0; i < idxDummy; i++) {
1261 stack.push_back(fSuccess ? vchTrue : vchFalse);
1277 if (stack.size() < 2) {
1283 if (vch1.size() + vch2.size() >
1287 vch1.insert(vch1.end(), vch2.begin(), vch2.end());
1293 if (stack.size() < 2) {
1303 if (position > data.size()) {
1304 return set_error(serror,
1310 valtype n1(data.begin(), data.begin() + position);
1311 valtype n2(data.begin() + position, data.end());
1320 if (stack.size() < 1) {
1326 std::reverse(data.begin(), data.end());
1334 if (stack.size() < 2) {
1351 if (rawnum.size() > size) {
1353 return set_error(serror,
1359 if (rawnum.size() == size) {
1363 uint8_t signbit = 0x00;
1364 if (rawnum.size() > 0) {
1365 signbit = rawnum.back() & 0x80;
1366 rawnum[rawnum.size() - 1] &= 0x7f;
1369 rawnum.reserve(size);
1370 while (rawnum.size() < size - 1) {
1371 rawnum.push_back(0x00);
1374 rawnum.push_back(signbit);
1379 if (stack.size() < 1) {
1389 return set_error(serror,
1408 if (!vfExec.empty()) {
1412 return set_success(serror);
1421 template <
class T>
class CTransactionSignatureSerializer {
1428 const unsigned int nIn;
1433 CTransactionSignatureSerializer(
const T &txToIn,
1437 : txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1438 sigHashType(sigHashTypeIn) {}
1441 template <
typename S>
void SerializeScriptCode(S &s)
const {
1445 unsigned int nCodeSeparators = 0;
1446 while (scriptCode.
GetOp(it, opcode)) {
1453 while (scriptCode.
GetOp(it, opcode)) {
1455 s.write((
char *)&itBegin[0], it - itBegin - 1);
1459 if (itBegin != scriptCode.
end()) {
1460 s.write((
char *)&itBegin[0], it - itBegin);
1465 template <
typename S>
void SerializeInput(S &s,
unsigned int nInput)
const {
1474 if (nInput != nIn) {
1478 SerializeScriptCode(s);
1481 if (nInput != nIn &&
1492 template <
typename S>
1493 void SerializeOutput(S &s,
unsigned int nOutput)
const {
1504 template <
typename S>
void Serialize(S &s)
const {
1508 unsigned int nInputs =
1511 for (
unsigned int nInput = 0; nInput < nInputs; nInput++) {
1512 SerializeInput(s, nInput);
1515 unsigned int nOutputs =
1520 : txTo.vout.size());
1522 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) {
1523 SerializeOutput(s, nOutput);
1530 template <
class T>
uint256 GetPrevoutHash(
const T &txTo) {
1532 for (
const auto &txin : txTo.vin) {
1538 template <
class T>
uint256 GetSequenceHash(
const T &txTo) {
1540 for (
const auto &txin : txTo.vin) {
1541 ss << txin.nSequence;
1546 template <
class T>
uint256 GetOutputsHash(
const T &txTo) {
1548 for (
const auto &txout : txTo.vout) {
1558 hashPrevouts = GetPrevoutHash(txTo);
1559 hashSequence = GetSequenceHash(txTo);
1560 hashOutputs = GetOutputsHash(txTo);
1574 assert(nIn < txTo.vin.size());
1580 uint32_t newForkValue = sigHashType.
getForkValue() ^ 0xdead;
1581 sigHashType = sigHashType.
withForkValue(0xff0000 | newForkValue);
1590 hashPrevouts = cache ? cache->
hashPrevouts : GetPrevoutHash(txTo);
1596 hashSequence = cache ? cache->
hashSequence : GetSequenceHash(txTo);
1601 hashOutputs = cache ? cache->
hashOutputs : GetOutputsHash(txTo);
1603 (nIn < txTo.vout.size())) {
1605 ss << txTo.vout[nIn];
1618 ss << txTo.vin[nIn].prevout;
1621 ss << txTo.vin[nIn].nSequence;
1625 ss << txTo.nLockTime;
1634 (nIn >= txTo.vout.size())) {
1641 CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn,
1646 ss << txTmp << sigHashType;
1652 const uint256 &sighash)
const {
1653 if (vchSig.size() == 64) {
1662 const std::vector<uint8_t> &vchSigIn,
const std::vector<uint8_t> &vchPubKey,
1670 std::vector<uint8_t> vchSig(vchSigIn);
1671 if (vchSig.empty()) {
1678 this->txdata, flags);
1680 if (!VerifySignature(vchSig, pubkey, sighash)) {
1706 if (nLockTime > int64_t(txTo->
nLockTime)) {
1731 const int64_t txToSequence = int64_t(txTo->
vin[nIn].nSequence);
1735 if (static_cast<uint32_t>(txTo->
nVersion) < 2) {
1749 const uint32_t nLockTimeMask =
1751 const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
1752 const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
1770 if (nSequenceMasked > txToSequenceMasked) {
1799 std::vector<valtype> stack, stackCopy;
1800 if (!
EvalScript(stack, scriptSig, flags, checker, metrics, serror)) {
1807 if (!
EvalScript(stack, scriptPubKey, flags, checker, metrics, serror)) {
1811 if (stack.empty()) {
1826 swap(stack, stackCopy);
1831 assert(!stack.empty());
1833 const valtype &pubKeySerialized = stack.back();
1834 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1841 pubKey2.IsWitnessProgram()) {
1843 metricsOut = metrics;
1844 return set_success(serror);
1847 if (!
EvalScript(stack, pubKey2, flags, checker, metrics, serror)) {
1851 if (stack.empty()) {
1867 assert((flags & SCRIPT_VERIFY_P2SH) != 0);
1868 if (stack.size() != 1) {
1888 "overflow sanity check on max script size");
1890 "overflow sanity check on maximum possible sigchecks " 1891 "from sig+redeem+pub scripts");
1897 metricsOut = metrics;
1898 return set_success(serror);
bool CheckSequence(const CScriptNum &nSequence) const final override
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
static const int MAX_PUBKEYS_PER_MULTISIG
bool EvalScript(std::vector< valtype > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *serror)
bool hasAnyoneCanPay() const
iterator insert(iterator pos, const T &value)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
bool IsPayToScriptHash() const
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Precompute sighash midstate to avoid quadratic hashing.
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
CHash160 & Write(const uint8_t *data, size_t len)
BaseSigHashType getBaseType() const
static void popstack(std::vector< valtype > &stack)
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
If this flag set, CTxIn::nSequence is NOT interpreted as a relative lock-time.
virtual bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const
bool VerifyECDSA(const uint256 &hash, const std::vector< uint8_t > &vchSig) const
Verify a DER-serialized ECDSA signature (~72 bytes).
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.
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, SigHashType sigHashType, const Amount amount, const PrecomputedTransactionData *cache, uint32_t flags)
PrecomputedTransactionData()
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
bool VerifySchnorr(const uint256 &hash, const std::array< uint8_t, SCHNORR_SIZE > &sig) const
Verify a Schnorr signature (=64 bytes).
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
static const int MAX_OPS_PER_SCRIPT
void Serialize(Stream &s, char a)
static bool IsMinimallyEncoded(const std::vector< uint8_t > &vch, const size_t nMaxNumSize=CScriptNum::MAXIMUM_ELEMENT_SIZE)
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...
CRIPEMD160 & Write(const uint8_t *data, size_t len)
bool CheckLockTime(const CScriptNum &nLockTime) const final override
static bool IsOpcodeDisabled(opcodetype opcode, uint32_t flags)
bool CheckTransactionECDSASignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded ECDSA signature...
Struct for holding cumulative results from executing a script or a sequence of scripts.
opcodetype
Script opcodes.
bool CheckDataSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided on some data is properly encoded.
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
An encapsulated public key.
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.
SigHashType withForkValue(uint32_t forkId) const
#define stacktop(i)
Script is a stack machine (like Forth) that evaluates a predicate returning a bool indicating valid o...
uint32_t getForkValue() const
An output of a transaction.
static const int MAX_SCRIPT_SIZE
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...
static const int MAX_STACK_SIZE
CHash256 & Write(const uint8_t *data, size_t len)
bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const final override
std::vector< uint8_t > getvch() const
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...
Serialized script, used inside transaction inputs and outputs.
bool CheckTransactionSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded.
static void CleanupScriptCode(CScript &scriptCode, const std::vector< uint8_t > &vchSig, uint32_t flags)
A mutable version of CTransaction.
bool DecodeBitfield(const std::vector< uint8_t > &vch, unsigned size, uint32_t &bitfield, ScriptError *serror)
CSHA256 & Write(const uint8_t *data, size_t len)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
virtual bool CheckSequence(const CScriptNum &nSequence) const
A writer stream (for serialization) that computes a 256-bit hash.
CSHA1 & Write(const uint8_t *data, size_t len)
static const unsigned int LOCKTIME_THRESHOLD
The basic transaction that is broadcasted on the network and contained in blocks. ...
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
A hasher class for SHA-256.
int FindAndDelete(CScript &script, const CScript &b)
bool CastToBool(const valtype &vch)
uint32_t countBits(uint32_t v)
static bool MinimallyEncode(std::vector< uint8_t > &data)
A hasher class for RIPEMD-160.
Signature hash type wrapper class.
bool CheckTransactionSchnorrSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded Schnorr signature (...
std::vector< uint8_t > valtype
bool CheckPubKeyEncoding(const valtype &vchPubKey, uint32_t flags, ScriptError *serror)
Check that a public key is encoded properly.