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
| Form | Required 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 hash | Native witness program length 20; scriptSig empty unless P2SH-wrapped; witness stack is $(signature,pubkey)$; $HASH160(pubkey)$ matches; ECDSA verifies $M_wit0$. |
| Witness v0 script hash | Native 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 path | Control block commits a tapleaf to the output key. Leaf version 0xc0 evaluates as Tapscript; unknown leaf versions succeed after commitment validation. |
| Unknown witness version | A 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). $$
| Field | Meaning |
|---|---|
script, pc | Program 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
| Relation | Definition |
|---|---|
| $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.
| Index | Opcode | Effect |
|---|---|---|
0 | OP_0 / OP_FALSE | Push empty vector. |
1-75 | OP_PUSHBYTES_n | Push the next $n$ script bytes. |
76 | OP_PUSHDATA1 | Next uint8 gives pushed byte length. |
77 | OP_PUSHDATA2 | Next little-endian uint16 gives pushed byte length. |
78 | OP_PUSHDATA4 | Next little-endian uint32 gives pushed byte length. |
79 | OP_1NEGATE | Push $-1$. |
81 | OP_1 | Push integer $1$. |
82 | OP_2 | Push integer $2$. |
83 | OP_3 | Push integer $3$. |
84 | OP_4 | Push integer $4$. |
85 | OP_5 | Push integer $5$. |
86 | OP_6 | Push integer $6$. |
87 | OP_7 | Push integer $7$. |
88 | OP_8 | Push integer $8$. |
89 | OP_9 | Push integer $9$. |
90 | OP_10 | Push integer $10$. |
91 | OP_11 | Push integer $11$. |
92 | OP_12 | Push integer $12$. |
93 | OP_13 | Push integer $13$. |
94 | OP_14 | Push integer $14$. |
95 | OP_15 | Push integer $15$. |
96 | OP_16 | Push integer $16$. |
| Index | Opcode | Effect |
|---|---|---|
97 | OP_NOP | No effect. |
99 | OP_IF | (v --); push $CastToBool(v)$ to $C$. |
100 | OP_NOTIF | (v --); push negated condition to $C$. |
101 | OP_VERIF | Fail. |
102 | OP_VERNOTIF | Fail. |
103 | OP_ELSE | Toggle top of $C$; unmatched ELSE fails. |
104 | OP_ENDIF | Pop top of $C$; unmatched ENDIF fails. |
105 | OP_VERIFY | (v --) if true; otherwise fail. |
106 | OP_RETURN | Fail. |
| Index | Opcode | Effect |
|---|---|---|
107 | OP_TOALTSTACK | Move top of $S$ to $A$. |
108 | OP_FROMALTSTACK | Move top of $A$ to $S$. |
109 | OP_2DROP | (x1 x2 --). |
110 | OP_2DUP | (x1 x2 -- x1 x2 x1 x2). |
111 | OP_3DUP | (x1 x2 x3 -- x1 x2 x3 x1 x2 x3). |
112 | OP_2OVER | (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2). |
113 | OP_2ROT | (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2). |
114 | OP_2SWAP | (x1 x2 x3 x4 -- x3 x4 x1 x2). |
115 | OP_IFDUP | Duplicate top item iff it casts true. |
116 | OP_DEPTH | Push stack depth. |
117 | OP_DROP | (x --). |
118 | OP_DUP | (x -- x x). |
119 | OP_NIP | (x1 x2 -- x2). |
120 | OP_OVER | (x1 x2 -- x1 x2 x1). |
121 | OP_PICK | (n -- x); copy stack item $n$ to the top. |
122 | OP_ROLL | (n -- x); move stack item $n$ to the top. |
123 | OP_ROT | (x1 x2 x3 -- x2 x3 x1). |
124 | OP_SWAP | (x1 x2 -- x2 x1). |
125 | OP_TUCK | (x1 x2 -- x2 x1 x2). |
130 | OP_SIZE | (x -- x len(x)). |
| Index | Opcode | Effect |
|---|---|---|
135 | OP_EQUAL | Byte-vector equality. |
136 | OP_EQUALVERIFY | Equality followed by VERIFY. |
139 | OP_1ADD | (x -- x+1) over $Num(x,4,min)$. |
140 | OP_1SUB | (x -- x-1) over $Num(x,4,min)$. |
143 | OP_NEGATE | (x -- -x) over $Num(x,4,min)$. |
144 | OP_ABS | `(x – |
145 | OP_NOT | Push $1$ iff numeric input is zero; otherwise push $0$. |
146 | OP_0NOTEQUAL | Push $0$ iff numeric input is zero; otherwise push $1$. |
147 | OP_ADD | (x y -- x+y) over script numbers. |
148 | OP_SUB | (x y -- x-y) over script numbers. |
154 | OP_BOOLAND | Push $1$ iff both numeric inputs are nonzero. |
155 | OP_BOOLOR | Push $1$ iff at least one numeric input is nonzero. |
156 | OP_NUMEQUAL | Numeric equality. |
157 | OP_NUMEQUALVERIFY | Numeric equality followed by VERIFY. |
158 | OP_NUMNOTEQUAL | Numeric inequality. |
159 | OP_LESSTHAN | Numeric less-than comparison. |
160 | OP_GREATERTHAN | Numeric greater-than comparison. |
161 | OP_LESSTHANOREQUAL | Numeric less-than-or-equal comparison. |
162 | OP_GREATERTHANOREQUAL | Numeric greater-than-or-equal comparison. |
163 | OP_MIN | Push the smaller numeric input. |
164 | OP_MAX | Push the larger numeric input. |
165 | OP_WITHIN | (x min max -- ok) where $min <= x < max$. |
| Index | Opcode | Effect |
|---|---|---|
166 | OP_RIPEMD160 | (x -- RIPEMD160(x)). |
167 | OP_SHA1 | (x -- SHA1(x)). |
168 | OP_SHA256 | (x -- SHA256(x)). |
169 | OP_HASH160 | (x -- HASH160(x)). |
170 | OP_HASH256 | (x -- H(x)). |
171 | OP_CODESEPARATOR | Set $k$ to this opcode position. |
172 | OP_CHECKSIG | (sig key -- ok) using $CheckSig$. |
173 | OP_CHECKSIGVERIFY | CHECKSIG followed by VERIFY. |
174 | OP_CHECKMULTISIG | (dummy sig[m] m key[n] n -- ok); enforces historical dummy rule when active; disabled in Tapscript. |
175 | OP_CHECKMULTISIGVERIFY | CHECKMULTISIG followed by VERIFY; disabled in Tapscript. |
186 | OP_CHECKSIGADD | Tapscript only: (sig n key -- n+ok). |
| Index | Opcode | Effect |
|---|---|---|
176 | OP_NOP1 | No effect. |
177 | OP_CHECKLOCKTIMEVERIFY / OP_NOP2 | If active in $R$, require $LockTime(X,n)$; otherwise no effect. |
178 | OP_CHECKSEQUENCEVERIFY / OP_NOP3 | If active in $R$, require $Sequence(X,n)$; otherwise no effect. |
179 | OP_NOP4 | No effect unless redefined by active soft-fork rules. |
180 | OP_NOP5 | No effect unless redefined by active soft-fork rules. |
181 | OP_NOP6 | No effect unless redefined by active soft-fork rules. |
182 | OP_NOP7 | No effect unless redefined by active soft-fork rules. |
183 | OP_NOP8 | No effect unless redefined by active soft-fork rules. |
184 | OP_NOP9 | No effect unless redefined by active soft-fork rules. |
185 | OP_NOP10 | No effect unless redefined by active soft-fork rules. |
| Index | Opcode | Effect |
|---|---|---|
80 | OP_RESERVED / OP_SUCCESS80 | Reserved failure if executed outside Tapscript; Tapscript success. |
98 | OP_VER / OP_SUCCESS98 | Reserved failure if executed outside Tapscript; Tapscript success. |
126 | OP_CAT / OP_SUCCESS126 | Disabled failure outside Tapscript; Tapscript success. |
127 | OP_SUBSTR / OP_SUCCESS127 | Disabled failure outside Tapscript; Tapscript success. |
128 | OP_LEFT / OP_SUCCESS128 | Disabled failure outside Tapscript; Tapscript success. |
129 | OP_RIGHT / OP_SUCCESS129 | Disabled failure outside Tapscript; Tapscript success. |
131 | OP_INVERT / OP_SUCCESS131 | Disabled failure outside Tapscript; Tapscript success. |
132 | OP_AND / OP_SUCCESS132 | Disabled failure outside Tapscript; Tapscript success. |
133 | OP_OR / OP_SUCCESS133 | Disabled failure outside Tapscript; Tapscript success. |
134 | OP_XOR / OP_SUCCESS134 | Disabled failure outside Tapscript; Tapscript success. |
137 | OP_RESERVED1 / OP_SUCCESS137 | Reserved failure if executed outside Tapscript; Tapscript success. |
138 | OP_RESERVED2 / OP_SUCCESS138 | Reserved failure if executed outside Tapscript; Tapscript success. |
141 | OP_2MUL / OP_SUCCESS141 | Disabled failure outside Tapscript; Tapscript success. |
142 | OP_2DIV / OP_SUCCESS142 | Disabled failure outside Tapscript; Tapscript success. |
149 | OP_MUL / OP_SUCCESS149 | Disabled failure outside Tapscript; Tapscript success. |
150 | OP_DIV / OP_SUCCESS150 | Disabled failure outside Tapscript; Tapscript success. |
151 | OP_MOD / OP_SUCCESS151 | Disabled failure outside Tapscript; Tapscript success. |
152 | OP_LSHIFT / OP_SUCCESS152 | Disabled failure outside Tapscript; Tapscript success. |
153 | OP_RSHIFT / OP_SUCCESS153 | Disabled failure outside Tapscript; Tapscript success. |
187 | OP_SUCCESS187 | Tapscript success; invalid outside Tapscript. |
188 | OP_SUCCESS188 | Tapscript success; invalid outside Tapscript. |
189 | OP_SUCCESS189 | Tapscript success; invalid outside Tapscript. |
190 | OP_SUCCESS190 | Tapscript success; invalid outside Tapscript. |
191 | OP_SUCCESS191 | Tapscript success; invalid outside Tapscript. |
192 | OP_SUCCESS192 | Tapscript success; invalid outside Tapscript. |
193 | OP_SUCCESS193 | Tapscript success; invalid outside Tapscript. |
194 | OP_SUCCESS194 | Tapscript success; invalid outside Tapscript. |
195 | OP_SUCCESS195 | Tapscript success; invalid outside Tapscript. |
196 | OP_SUCCESS196 | Tapscript success; invalid outside Tapscript. |
197 | OP_SUCCESS197 | Tapscript success; invalid outside Tapscript. |
198 | OP_SUCCESS198 | Tapscript success; invalid outside Tapscript. |
199 | OP_SUCCESS199 | Tapscript success; invalid outside Tapscript. |
200 | OP_SUCCESS200 | Tapscript success; invalid outside Tapscript. |
201 | OP_SUCCESS201 | Tapscript success; invalid outside Tapscript. |
202 | OP_SUCCESS202 | Tapscript success; invalid outside Tapscript. |
203 | OP_SUCCESS203 | Tapscript success; invalid outside Tapscript. |
204 | OP_SUCCESS204 | Tapscript success; invalid outside Tapscript. |
205 | OP_SUCCESS205 | Tapscript success; invalid outside Tapscript. |
206 | OP_SUCCESS206 | Tapscript success; invalid outside Tapscript. |
207 | OP_SUCCESS207 | Tapscript success; invalid outside Tapscript. |
208 | OP_SUCCESS208 | Tapscript success; invalid outside Tapscript. |
209 | OP_SUCCESS209 | Tapscript success; invalid outside Tapscript. |
210 | OP_SUCCESS210 | Tapscript success; invalid outside Tapscript. |
211 | OP_SUCCESS211 | Tapscript success; invalid outside Tapscript. |
212 | OP_SUCCESS212 | Tapscript success; invalid outside Tapscript. |
213 | OP_SUCCESS213 | Tapscript success; invalid outside Tapscript. |
214 | OP_SUCCESS214 | Tapscript success; invalid outside Tapscript. |
215 | OP_SUCCESS215 | Tapscript success; invalid outside Tapscript. |
216 | OP_SUCCESS216 | Tapscript success; invalid outside Tapscript. |
217 | OP_SUCCESS217 | Tapscript success; invalid outside Tapscript. |
218 | OP_SUCCESS218 | Tapscript success; invalid outside Tapscript. |
219 | OP_SUCCESS219 | Tapscript success; invalid outside Tapscript. |
220 | OP_SUCCESS220 | Tapscript success; invalid outside Tapscript. |
221 | OP_SUCCESS221 | Tapscript success; invalid outside Tapscript. |
222 | OP_SUCCESS222 | Tapscript success; invalid outside Tapscript. |
223 | OP_SUCCESS223 | Tapscript success; invalid outside Tapscript. |
224 | OP_SUCCESS224 | Tapscript success; invalid outside Tapscript. |
225 | OP_SUCCESS225 | Tapscript success; invalid outside Tapscript. |
226 | OP_SUCCESS226 | Tapscript success; invalid outside Tapscript. |
227 | OP_SUCCESS227 | Tapscript success; invalid outside Tapscript. |
228 | OP_SUCCESS228 | Tapscript success; invalid outside Tapscript. |
229 | OP_SUCCESS229 | Tapscript success; invalid outside Tapscript. |
230 | OP_SUCCESS230 | Tapscript success; invalid outside Tapscript. |
231 | OP_SUCCESS231 | Tapscript success; invalid outside Tapscript. |
232 | OP_SUCCESS232 | Tapscript success; invalid outside Tapscript. |
233 | OP_SUCCESS233 | Tapscript success; invalid outside Tapscript. |
234 | OP_SUCCESS234 | Tapscript success; invalid outside Tapscript. |
235 | OP_SUCCESS235 | Tapscript success; invalid outside Tapscript. |
236 | OP_SUCCESS236 | Tapscript success; invalid outside Tapscript. |
237 | OP_SUCCESS237 | Tapscript success; invalid outside Tapscript. |
238 | OP_SUCCESS238 | Tapscript success; invalid outside Tapscript. |
239 | OP_SUCCESS239 | Tapscript success; invalid outside Tapscript. |
240 | OP_SUCCESS240 | Tapscript success; invalid outside Tapscript. |
241 | OP_SUCCESS241 | Tapscript success; invalid outside Tapscript. |
242 | OP_SUCCESS242 | Tapscript success; invalid outside Tapscript. |
243 | OP_SUCCESS243 | Tapscript success; invalid outside Tapscript. |
244 | OP_SUCCESS244 | Tapscript success; invalid outside Tapscript. |
245 | OP_SUCCESS245 | Tapscript success; invalid outside Tapscript. |
246 | OP_SUCCESS246 | Tapscript success; invalid outside Tapscript. |
247 | OP_SUCCESS247 | Tapscript success; invalid outside Tapscript. |
248 | OP_SUCCESS248 | Tapscript success; invalid outside Tapscript. |
249 | OP_SUCCESS249 | Tapscript success; invalid outside Tapscript. |
250 | OP_SUCCESS250 | Tapscript success; invalid outside Tapscript. |
251 | OP_SUCCESS251 | Tapscript success; invalid outside Tapscript. |
252 | OP_SUCCESS252 | Tapscript success; invalid outside Tapscript. |
253 | OP_SUCCESS253 | Tapscript success; invalid outside Tapscript. |
254 | OP_SUCCESS254 | Tapscript success; invalid outside Tapscript. |
255 | OP_INVALIDOPCODE | Fail. |
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).