Bitcoin Script Opcodes Explained: Stack Rules and Examples
Bitcoin Script is a stack-based validation language. Its opcodes evaluate spending conditions attached to transaction outputs; they do not run arbitrary programs or maintain a persistent application state. For developers, the essential distinction is between the script bytes, the execution context, and the consensus rules governing that context.
How does the stack work?
Most operations consume and produce byte-vector stack elements. For example, OP_DUP copies the top item, OP_HASH160 hashes it with SHA-256 followed by RIPEMD-160, and OP_EQUALVERIFY checks equality and fails if the two items differ. A successful spend must satisfy all applicable validation rules, not merely reach an apparently true value in an isolated interpreter.
A familiar P2PKH locking script
A traditional pay-to-public-key-hash script is:
OP_DUP OP_HASH160 <20-byte-pubkey-hash> OP_EQUALVERIFY OP_CHECKSIG
The unlocking data supplies a signature and public key. The script checks that the public key matches the committed hash and that the signature is valid for the transaction under the applicable signature-hash rules. This is a teaching example, not a complete transaction builder.
Important opcode families
| Opcode | Typical purpose | Developer caution |
| --- | --- | --- |
| OP_DUP | Duplicate a stack item | Stack underflow causes failure |
| OP_HASH160 | Hash an item | Compare raw bytes, not displayed addresses |
| OP_EQUALVERIFY | Require equality | Consumes both compared items |
| OP_CHECKSIG | Verify a signature | Semantics depend on script version |
| OP_CHECKMULTISIG | Legacy multisignature verification | Has historical stack quirks; disabled in Tapscript |
| OP_CHECKLOCKTIMEVERIFY | Absolute locktime constraint | Requires compatible transaction nLockTime and input sequence |
| OP_CHECKSEQUENCEVERIFY | Relative locktime constraint | Depends on transaction version and sequence encoding |
Why script version matters
Legacy Script, SegWit v0 and Tapscript do not share identical signature-validation rules. In Tapscript, OP_CHECKSIG uses Schnorr signatures for recognized public-key types, and OP_CHECKSIGADD supports signature-counting constructions. OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY are disabled in Tapscript. Do not copy a legacy script into a Taproot leaf without reviewing BIP342.
How to test a script safely
Use regtest or testnet for experiments. Specify the exact output type and script version, construct a funding output, spend it with appropriate witness data, and validate with a consensus-compatible implementation. A script that looks correct in a simplified stack simulator may still fail transaction-level checks.
Primary references
- Bitcoin Developer Reference: Transactions: https://developer.bitcoin.org/reference/transactions.html
- BIP342: Validation of Taproot Scripts: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki
- Bitcoin Core source repository: https://github.com/bitcoin/bitcoin
