Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_SCRIPT_SCRIPT_H7#define BITCOIN_SCRIPT_SCRIPT_H9#include <attributes.h>10#include <crypto/common.h>11#include <prevector.h>12#include <serialize.h>13#include <uint256.h>14#include <util/hash_type.h>16#include <cassert>17#include <cstddef>18#include <cstdint>19#include <iterator>20#include <limits>21#include <span>22#include <stdexcept>23#include <string>24#include <type_traits>25#include <utility>26#include <vector>28// Maximum number of bytes pushable to the stack29inline constexpr unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;31// Maximum number of non-push operations per script32inline constexpr int MAX_OPS_PER_SCRIPT = 201;34// Maximum number of public keys per multisig35inline constexpr int MAX_PUBKEYS_PER_MULTISIG = 20;37/** The limit of keys in OP_CHECKSIGADD-based scripts. It is due to the stack limit in BIP342. */38inline constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A = 999;40// Maximum script length in bytes41inline constexpr int MAX_SCRIPT_SIZE{10'000};43// Maximum number of values on script interpreter stack44inline constexpr int MAX_STACK_SIZE = 1000;46// Threshold for nLockTime: below this value it is interpreted as block number,47// otherwise as UNIX timestamp.48inline constexpr unsigned int LOCKTIME_THRESHOLD{500'000'000}; // Tue Nov 5 00:53:20 1985 UTC50// Maximum nLockTime. Since a lock time indicates the last invalid timestamp, a51// transaction with this lock time will never be valid unless lock time52// checking is disabled (by setting all input sequence numbers to53// SEQUENCE_FINAL).54inline constexpr uint32_t LOCKTIME_MAX = 0xFFFFFFFFU;56// Tag for input annex. If there are at least two witness elements for a transaction input,57// and the first byte of the last element is 0x50, this last element is called annex, and58// has meanings independent of the script59inline constexpr unsigned int ANNEX_TAG = 0x50;61// Validation weight per passing signature (Tapscript only, see BIP 342).62inline constexpr int64_t VALIDATION_WEIGHT_PER_SIGOP_PASSED{50};64// How much weight budget is added to the witness size (Tapscript only, see BIP 342).65inline constexpr int64_t VALIDATION_WEIGHT_OFFSET{50};67template <typename T>68std::vector<unsigned char> ToByteVector(const T& in)69{70return std::vector<unsigned char>(in.begin(), in.end());71}73/** Script opcodes */74enum opcodetype75{76// push value77OP_0 = 0x00,78OP_FALSE = OP_0,79OP_PUSHDATA1 = 0x4c,80OP_PUSHDATA2 = 0x4d,81OP_PUSHDATA4 = 0x4e,82OP_1NEGATE = 0x4f,83OP_RESERVED = 0x50,84OP_1 = 0x51,85OP_TRUE=OP_1,86OP_2 = 0x52,87OP_3 = 0x53,88OP_4 = 0x54,89OP_5 = 0x55,90OP_6 = 0x56,91OP_7 = 0x57,92OP_8 = 0x58,93OP_9 = 0x59,94OP_10 = 0x5a,95OP_11 = 0x5b,96OP_12 = 0x5c,97OP_13 = 0x5d,98OP_14 = 0x5e,99OP_15 = 0x5f,100OP_16 = 0x60,102// control103OP_NOP = 0x61,104OP_VER = 0x62,105OP_IF = 0x63,106OP_NOTIF = 0x64,107OP_VERIF = 0x65,108OP_VERNOTIF = 0x66,109OP_ELSE = 0x67,110OP_ENDIF = 0x68,111OP_VERIFY = 0x69,112OP_RETURN = 0x6a,114// stack ops115OP_TOALTSTACK = 0x6b,116OP_FROMALTSTACK = 0x6c,117OP_2DROP = 0x6d,118OP_2DUP = 0x6e,119OP_3DUP = 0x6f,120OP_2OVER = 0x70,121OP_2ROT = 0x71,122OP_2SWAP = 0x72,123OP_IFDUP = 0x73,124OP_DEPTH = 0x74,125OP_DROP = 0x75,126OP_DUP = 0x76,127OP_NIP = 0x77,128OP_OVER = 0x78,129OP_PICK = 0x79,130OP_ROLL = 0x7a,131OP_ROT = 0x7b,132OP_SWAP = 0x7c,133OP_TUCK = 0x7d,135// splice ops136OP_CAT = 0x7e,137OP_SUBSTR = 0x7f,138OP_LEFT = 0x80,139OP_RIGHT = 0x81,140OP_SIZE = 0x82,142// bit logic143OP_INVERT = 0x83,144OP_AND = 0x84,145OP_OR = 0x85,146OP_XOR = 0x86,147OP_EQUAL = 0x87,148OP_EQUALVERIFY = 0x88,149OP_RESERVED1 = 0x89,150OP_RESERVED2 = 0x8a,152// numeric153OP_1ADD = 0x8b,154OP_1SUB = 0x8c,155OP_2MUL = 0x8d,156OP_2DIV = 0x8e,157OP_NEGATE = 0x8f,158OP_ABS = 0x90,159OP_NOT = 0x91,160OP_0NOTEQUAL = 0x92,162OP_ADD = 0x93,163OP_SUB = 0x94,164OP_MUL = 0x95,165OP_DIV = 0x96,166OP_MOD = 0x97,167OP_LSHIFT = 0x98,168OP_RSHIFT = 0x99,170OP_BOOLAND = 0x9a,171OP_BOOLOR = 0x9b,172OP_NUMEQUAL = 0x9c,173OP_NUMEQUALVERIFY = 0x9d,174OP_NUMNOTEQUAL = 0x9e,175OP_LESSTHAN = 0x9f,176OP_GREATERTHAN = 0xa0,177OP_LESSTHANOREQUAL = 0xa1,178OP_GREATERTHANOREQUAL = 0xa2,179OP_MIN = 0xa3,180OP_MAX = 0xa4,182OP_WITHIN = 0xa5,184// crypto185OP_RIPEMD160 = 0xa6,186OP_SHA1 = 0xa7,187OP_SHA256 = 0xa8,188OP_HASH160 = 0xa9,189OP_HASH256 = 0xaa,190OP_CODESEPARATOR = 0xab,191OP_CHECKSIG = 0xac,192OP_CHECKSIGVERIFY = 0xad,193OP_CHECKMULTISIG = 0xae,194OP_CHECKMULTISIGVERIFY = 0xaf,196// expansion197OP_NOP1 = 0xb0,198OP_CHECKLOCKTIMEVERIFY = 0xb1,199OP_NOP2 = OP_CHECKLOCKTIMEVERIFY,200OP_CHECKSEQUENCEVERIFY = 0xb2,201OP_NOP3 = OP_CHECKSEQUENCEVERIFY,202OP_NOP4 = 0xb3,203OP_NOP5 = 0xb4,204OP_NOP6 = 0xb5,205OP_NOP7 = 0xb6,206OP_NOP8 = 0xb7,207OP_NOP9 = 0xb8,208OP_NOP10 = 0xb9,210// Opcode added by BIP 342 (Tapscript)211OP_CHECKSIGADD = 0xba,213OP_INVALIDOPCODE = 0xff,214};216// Maximum value that an opcode can be217inline constexpr unsigned int MAX_OPCODE = OP_NOP10;219std::string GetOpName(opcodetype opcode);221class scriptnum_error : public std::runtime_error222{223public:224explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}225};227class CScriptNum228{229/**230* Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.231* The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],232* but results may overflow (and are valid as long as they are not used in a subsequent233* numeric operation). CScriptNum enforces those semantics by storing results as234* an int64 and allowing out-of-range values to be returned as a vector of bytes but235* throwing an exception if arithmetic is done or the result is interpreted as an integer.236*/237public:239explicit CScriptNum(const int64_t& n)240{241m_value = n;242}244static constexpr size_t nDefaultMaxNumSize{4};246explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,247const size_t nMaxNumSize = nDefaultMaxNumSize)248{249if (vch.size() > nMaxNumSize) {250throw scriptnum_error("script number overflow");