Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Script Execution

For each non-coinbase input $i$, spend authorization is the predicate:

$$ \operatorname{Authorize}_{R}(tx,i,coin,u,H) \in {\top,\bot}, $$

where $u=(scriptSig,witness)$ is the unlocking data, $coin$ is the spent UTXO, $R$ is the active rule context, and $H$ is the candidate height and median-time context. Authorization chooses a spend form, constructs a script machine state, and evaluates the committed program.

Spend forms

FormRequired relation
Legacy$Eval_R(scriptSig,BASE)$ then $Eval_R(scriptPubKey,BASE)$; witness empty; final stack top is true.
P2SH$R$ includes P2SH; scriptSig is push-only; final pushed item is redeemScript; $HASH160(redeemScript)$ matches; $Eval_R(redeemScript,BASE)$ succeeds on the pushed stack.
Witness v0 key hashNative witness program length 20; scriptSig empty unless P2SH-wrapped; witness stack is $(signature,pubkey)$; $HASH160(pubkey)$ matches; ECDSA verifies $M_wit0$.
Witness v0 script hashNative witness program length 32; final witness item is witnessScript; $SHA256(witnessScript)$ matches; $Eval_R(witnessScript,WITNESS_V0)$ succeeds on the remaining witness stack.
Taproot key path$R$ includes Taproot; witness stack, after optional annex removal, contains one Schnorr signature; BIP340 verifies $M_tr$ under the output key.
Taproot script pathControl block commits a tapleaf to the output key. Leaf version 0xc0 evaluates as Tapscript; unknown leaf versions succeed after commitment validation.
Unknown witness versionA syntactically valid witness program with no assigned meaning succeeds without execution.

Native witness spends require empty scriptSig; P2SH-wrapped witness spends require scriptSig to push exactly the witness program (sources: bip-0016,bip-0141,bip-0143,bip-0340,bip-0341,bip-0342).

Machine state

Script evaluation is a deterministic transition over:

$$ X=(tx,i,coin,script,pc,S,A,C,R,\sigma,k,\alpha,\beta). $$

FieldMeaning
script, pcProgram bytes and instruction cursor.
$S,A,C$Main stack, altstack, and conditional-execution stack.
$R,\sigma$Active rule context and signature version: $BASE$, $WITNESS_V0$, or $TAPSCRIPT$.
$k$Last executed code-separator position for signature-message construction.
$\alpha$Taproot annex, if present.
$\beta$Tapscript validation-weight budget.

Legacy and witness-v0 execution enforce the script limits named in Appendix Constants. Tapscript removes the script-size and per-script-opcode limits, keeps the stack-item and stack-size limits, and sets:

$$ \beta = \text{\code{TAPSCRIPT_SIGOPS_BUDGET_BASE}}+ \operatorname{serializedWitnessSize}(input). $$

Each executed signature opcode with a nonempty signature subtracts TAPSCRIPT_SIGOPS_COST; negative $\beta$ fails.

Evaluation

$$ \operatorname{Eval}_{R}(script,\sigma,S_0)= \begin{cases} \top, & \text{execution halts successfully with required final stack,}\ \bot, & \text{parsing, execution, resource, or final-stack validation fails.} \end{cases} $$

If $\sigma=TAPSCRIPT$, the script is scanned for any OP_SUCCESSx index before ordinary execution; encountering one succeeds. Otherwise, instructions are decoded left to right and $Step$ is applied while all entries of $C$ are true. Non-control opcodes in inactive branches do not mutate $S$, but parse-stage failures and Tapscript OP_SUCCESSx detection still apply.

Successful termination requires an empty $C$. Legacy and witness-v0 require a nonempty $S$ whose top element casts to true. Tapscript requires exactly one stack element, and that element must cast to true.

Primitive relations

RelationDefinition
$CastToBool(x)$False iff $x$ is empty, zero, or negative zero; true otherwise.
$Num(x,n,min)$Signed-magnitude little-endian integer from byte vector $x$, length at most $n$; if $min$, nonzero encodings must be minimal.
$MinimalPush(op,x)$True iff $op$ is the shortest permitted push form for $x$.
$LockTime(X,n)$Units match tx.lockTime, $n<= tx.lockTime$, and the input sequence is nonfinal.
$Sequence(X,n)$BIP68 is active in $R$, disable/type bits permit comparison, and the input sequence satisfies $n$.
$CheckSig(X,sig,key,h)$Empty signatures return false. Legacy and witness-v0 use ECDSA over secp256k1; Taproot and Tapscript use BIP340 Schnorr. Tapscript key size 0 fails, size 32 verifies, and other nonzero key sizes are unknown key types that pass current consensus.

Signature messages

Signature checks verify exactly one digest:

$$ \begin{aligned} M_{\mathrm{legacy}} &= \operatorname{SigMsg}{\mathrm{legacy}}(tx,i,k,h),\ M{\mathrm{wit0}} &= \operatorname{SigMsg}{\mathrm{wit0}}(tx,i,coin,k,h),\ M{\mathrm{tr}} &= \operatorname{SigMsg}_{\mathrm{tr}}(tx,i,coin,\alpha,k,h). \end{aligned} $$

Hash type bytes are 0x00 DEFAULT for Taproot, 0x01 ALL, 0x02 NONE, 0x03 SINGLE, and 0x80 ANYONECANPAY ORed with non-default forms. ALL, NONE, and SINGLE select the output commitment set; ANYONECANPAY restricts input commitment to the signed input. Legacy out-of-range SINGLE signs uint256::ONE; Taproot out-of-range SINGLE fails (sources: bip-0143,bip-0341,bip-0342).

Opcode classes

Effects use (before -- after) for stack transitions, with the rightmost item at the top of $S$. Parameterized direct byte pushes are represented as a single family; every named opcode is listed separately.

IndexOpcodeEffect
0OP_0 / OP_FALSEPush empty vector.
1-75OP_PUSHBYTES_nPush the next $n$ script bytes.
76OP_PUSHDATA1Next uint8 gives pushed byte length.
77OP_PUSHDATA2Next little-endian uint16 gives pushed byte length.
78OP_PUSHDATA4Next little-endian uint32 gives pushed byte length.
79OP_1NEGATEPush $-1$.
81OP_1Push integer $1$.
82OP_2Push integer $2$.
83OP_3Push integer $3$.
84OP_4Push integer $4$.
85OP_5Push integer $5$.
86OP_6Push integer $6$.
87OP_7Push integer $7$.
88OP_8Push integer $8$.
89OP_9Push integer $9$.
90OP_10Push integer $10$.
91OP_11Push integer $11$.
92OP_12Push integer $12$.
93OP_13Push integer $13$.
94OP_14Push integer $14$.
95OP_15Push integer $15$.
96OP_16Push integer $16$.
IndexOpcodeEffect
97OP_NOPNo effect.
99OP_IF(v --); push $CastToBool(v)$ to $C$.
100OP_NOTIF(v --); push negated condition to $C$.
101OP_VERIFFail.
102OP_VERNOTIFFail.
103OP_ELSEToggle top of $C$; unmatched ELSE fails.
104OP_ENDIFPop top of $C$; unmatched ENDIF fails.
105OP_VERIFY(v --) if true; otherwise fail.
106OP_RETURNFail.
IndexOpcodeEffect
107OP_TOALTSTACKMove top of $S$ to $A$.
108OP_FROMALTSTACKMove top of $A$ to $S$.
109OP_2DROP(x1 x2 --).
110OP_2DUP(x1 x2 -- x1 x2 x1 x2).
111OP_3DUP(x1 x2 x3 -- x1 x2 x3 x1 x2 x3).
112OP_2OVER(x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2).
113OP_2ROT(x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2).
114OP_2SWAP(x1 x2 x3 x4 -- x3 x4 x1 x2).
115OP_IFDUPDuplicate top item iff it casts true.
116OP_DEPTHPush stack depth.
117OP_DROP(x --).
118OP_DUP(x -- x x).
119OP_NIP(x1 x2 -- x2).
120OP_OVER(x1 x2 -- x1 x2 x1).
121OP_PICK(n -- x); copy stack item $n$ to the top.
122OP_ROLL(n -- x); move stack item $n$ to the top.
123OP_ROT(x1 x2 x3 -- x2 x3 x1).
124OP_SWAP(x1 x2 -- x2 x1).
125OP_TUCK(x1 x2 -- x2 x1 x2).
130OP_SIZE(x -- x len(x)).
IndexOpcodeEffect
135OP_EQUALByte-vector equality.
136OP_EQUALVERIFYEquality followed by VERIFY.
139OP_1ADD(x -- x+1) over $Num(x,4,min)$.
140OP_1SUB(x -- x-1) over $Num(x,4,min)$.
143OP_NEGATE(x -- -x) over $Num(x,4,min)$.
144OP_ABS`(x –
145OP_NOTPush $1$ iff numeric input is zero; otherwise push $0$.
146OP_0NOTEQUALPush $0$ iff numeric input is zero; otherwise push $1$.
147OP_ADD(x y -- x+y) over script numbers.
148OP_SUB(x y -- x-y) over script numbers.
154OP_BOOLANDPush $1$ iff both numeric inputs are nonzero.
155OP_BOOLORPush $1$ iff at least one numeric input is nonzero.
156OP_NUMEQUALNumeric equality.
157OP_NUMEQUALVERIFYNumeric equality followed by VERIFY.
158OP_NUMNOTEQUALNumeric inequality.
159OP_LESSTHANNumeric less-than comparison.
160OP_GREATERTHANNumeric greater-than comparison.
161OP_LESSTHANOREQUALNumeric less-than-or-equal comparison.
162OP_GREATERTHANOREQUALNumeric greater-than-or-equal comparison.
163OP_MINPush the smaller numeric input.
164OP_MAXPush the larger numeric input.
165OP_WITHIN(x min max -- ok) where $min <= x < max$.
IndexOpcodeEffect
166OP_RIPEMD160(x -- RIPEMD160(x)).
167OP_SHA1(x -- SHA1(x)).
168OP_SHA256(x -- SHA256(x)).
169OP_HASH160(x -- HASH160(x)).
170OP_HASH256(x -- H(x)).
171OP_CODESEPARATORSet $k$ to this opcode position.
172OP_CHECKSIG(sig key -- ok) using $CheckSig$.
173OP_CHECKSIGVERIFYCHECKSIG followed by VERIFY.
174OP_CHECKMULTISIG(dummy sig[m] m key[n] n -- ok); enforces historical dummy rule when active; disabled in Tapscript.
175OP_CHECKMULTISIGVERIFYCHECKMULTISIG followed by VERIFY; disabled in Tapscript.
186OP_CHECKSIGADDTapscript only: (sig n key -- n+ok).
IndexOpcodeEffect
176OP_NOP1No effect.
177OP_CHECKLOCKTIMEVERIFY / OP_NOP2If active in $R$, require $LockTime(X,n)$; otherwise no effect.
178OP_CHECKSEQUENCEVERIFY / OP_NOP3If active in $R$, require $Sequence(X,n)$; otherwise no effect.
179OP_NOP4No effect unless redefined by active soft-fork rules.
180OP_NOP5No effect unless redefined by active soft-fork rules.
181OP_NOP6No effect unless redefined by active soft-fork rules.
182OP_NOP7No effect unless redefined by active soft-fork rules.
183OP_NOP8No effect unless redefined by active soft-fork rules.
184OP_NOP9No effect unless redefined by active soft-fork rules.
185OP_NOP10No effect unless redefined by active soft-fork rules.
IndexOpcodeEffect
80OP_RESERVED / OP_SUCCESS80Reserved failure if executed outside Tapscript; Tapscript success.
98OP_VER / OP_SUCCESS98Reserved failure if executed outside Tapscript; Tapscript success.
126OP_CAT / OP_SUCCESS126Disabled failure outside Tapscript; Tapscript success.
127OP_SUBSTR / OP_SUCCESS127Disabled failure outside Tapscript; Tapscript success.
128OP_LEFT / OP_SUCCESS128Disabled failure outside Tapscript; Tapscript success.
129OP_RIGHT / OP_SUCCESS129Disabled failure outside Tapscript; Tapscript success.
131OP_INVERT / OP_SUCCESS131Disabled failure outside Tapscript; Tapscript success.
132OP_AND / OP_SUCCESS132Disabled failure outside Tapscript; Tapscript success.
133OP_OR / OP_SUCCESS133Disabled failure outside Tapscript; Tapscript success.
134OP_XOR / OP_SUCCESS134Disabled failure outside Tapscript; Tapscript success.
137OP_RESERVED1 / OP_SUCCESS137Reserved failure if executed outside Tapscript; Tapscript success.
138OP_RESERVED2 / OP_SUCCESS138Reserved failure if executed outside Tapscript; Tapscript success.
141OP_2MUL / OP_SUCCESS141Disabled failure outside Tapscript; Tapscript success.
142OP_2DIV / OP_SUCCESS142Disabled failure outside Tapscript; Tapscript success.
149OP_MUL / OP_SUCCESS149Disabled failure outside Tapscript; Tapscript success.
150OP_DIV / OP_SUCCESS150Disabled failure outside Tapscript; Tapscript success.
151OP_MOD / OP_SUCCESS151Disabled failure outside Tapscript; Tapscript success.
152OP_LSHIFT / OP_SUCCESS152Disabled failure outside Tapscript; Tapscript success.
153OP_RSHIFT / OP_SUCCESS153Disabled failure outside Tapscript; Tapscript success.
187OP_SUCCESS187Tapscript success; invalid outside Tapscript.
188OP_SUCCESS188Tapscript success; invalid outside Tapscript.
189OP_SUCCESS189Tapscript success; invalid outside Tapscript.
190OP_SUCCESS190Tapscript success; invalid outside Tapscript.
191OP_SUCCESS191Tapscript success; invalid outside Tapscript.
192OP_SUCCESS192Tapscript success; invalid outside Tapscript.
193OP_SUCCESS193Tapscript success; invalid outside Tapscript.
194OP_SUCCESS194Tapscript success; invalid outside Tapscript.
195OP_SUCCESS195Tapscript success; invalid outside Tapscript.
196OP_SUCCESS196Tapscript success; invalid outside Tapscript.
197OP_SUCCESS197Tapscript success; invalid outside Tapscript.
198OP_SUCCESS198Tapscript success; invalid outside Tapscript.
199OP_SUCCESS199Tapscript success; invalid outside Tapscript.
200OP_SUCCESS200Tapscript success; invalid outside Tapscript.
201OP_SUCCESS201Tapscript success; invalid outside Tapscript.
202OP_SUCCESS202Tapscript success; invalid outside Tapscript.
203OP_SUCCESS203Tapscript success; invalid outside Tapscript.
204OP_SUCCESS204Tapscript success; invalid outside Tapscript.
205OP_SUCCESS205Tapscript success; invalid outside Tapscript.
206OP_SUCCESS206Tapscript success; invalid outside Tapscript.
207OP_SUCCESS207Tapscript success; invalid outside Tapscript.
208OP_SUCCESS208Tapscript success; invalid outside Tapscript.
209OP_SUCCESS209Tapscript success; invalid outside Tapscript.
210OP_SUCCESS210Tapscript success; invalid outside Tapscript.
211OP_SUCCESS211Tapscript success; invalid outside Tapscript.
212OP_SUCCESS212Tapscript success; invalid outside Tapscript.
213OP_SUCCESS213Tapscript success; invalid outside Tapscript.
214OP_SUCCESS214Tapscript success; invalid outside Tapscript.
215OP_SUCCESS215Tapscript success; invalid outside Tapscript.
216OP_SUCCESS216Tapscript success; invalid outside Tapscript.
217OP_SUCCESS217Tapscript success; invalid outside Tapscript.
218OP_SUCCESS218Tapscript success; invalid outside Tapscript.
219OP_SUCCESS219Tapscript success; invalid outside Tapscript.
220OP_SUCCESS220Tapscript success; invalid outside Tapscript.
221OP_SUCCESS221Tapscript success; invalid outside Tapscript.
222OP_SUCCESS222Tapscript success; invalid outside Tapscript.
223OP_SUCCESS223Tapscript success; invalid outside Tapscript.
224OP_SUCCESS224Tapscript success; invalid outside Tapscript.
225OP_SUCCESS225Tapscript success; invalid outside Tapscript.
226OP_SUCCESS226Tapscript success; invalid outside Tapscript.
227OP_SUCCESS227Tapscript success; invalid outside Tapscript.
228OP_SUCCESS228Tapscript success; invalid outside Tapscript.
229OP_SUCCESS229Tapscript success; invalid outside Tapscript.
230OP_SUCCESS230Tapscript success; invalid outside Tapscript.
231OP_SUCCESS231Tapscript success; invalid outside Tapscript.
232OP_SUCCESS232Tapscript success; invalid outside Tapscript.
233OP_SUCCESS233Tapscript success; invalid outside Tapscript.
234OP_SUCCESS234Tapscript success; invalid outside Tapscript.
235OP_SUCCESS235Tapscript success; invalid outside Tapscript.
236OP_SUCCESS236Tapscript success; invalid outside Tapscript.
237OP_SUCCESS237Tapscript success; invalid outside Tapscript.
238OP_SUCCESS238Tapscript success; invalid outside Tapscript.
239OP_SUCCESS239Tapscript success; invalid outside Tapscript.
240OP_SUCCESS240Tapscript success; invalid outside Tapscript.
241OP_SUCCESS241Tapscript success; invalid outside Tapscript.
242OP_SUCCESS242Tapscript success; invalid outside Tapscript.
243OP_SUCCESS243Tapscript success; invalid outside Tapscript.
244OP_SUCCESS244Tapscript success; invalid outside Tapscript.
245OP_SUCCESS245Tapscript success; invalid outside Tapscript.
246OP_SUCCESS246Tapscript success; invalid outside Tapscript.
247OP_SUCCESS247Tapscript success; invalid outside Tapscript.
248OP_SUCCESS248Tapscript success; invalid outside Tapscript.
249OP_SUCCESS249Tapscript success; invalid outside Tapscript.
250OP_SUCCESS250Tapscript success; invalid outside Tapscript.
251OP_SUCCESS251Tapscript success; invalid outside Tapscript.
252OP_SUCCESS252Tapscript success; invalid outside Tapscript.
253OP_SUCCESS253Tapscript success; invalid outside Tapscript.
254OP_SUCCESS254Tapscript success; invalid outside Tapscript.
255OP_INVALIDOPCODEFail.

Consensus script behavior is defined by the active P2SH, DER, locktime, sequence, NULLDUMMY, SegWit, Taproot, and Tapscript rules (sources: bip-0065,bip-0066,bip-0068,bip-0112,bip-0113,bip-0147,bip-0341,bip-0342,bitcoin-core-v31).