Block Chain - Bitcoin Script and Script Execution
Bitcoin Script is a small, purpose-built programming language used by the Bitcoin network to define the conditions under which bitcoins can be spent. Unlike general-purpose programming languages such as Python, Java, or C, Bitcoin Script is intentionally limited and does not support unrestricted program execution. Its primary purpose is to verify whether a transaction is authorized to spend specific bitcoins.
What Is Bitcoin Script?
Bitcoin Script is a stack-based, instruction-based scripting language. It is used mainly in Bitcoin transaction inputs and outputs.
Every Bitcoin transaction specifies conditions for spending its outputs. These conditions are expressed through scripts. When someone later tries to spend those outputs, the Bitcoin network evaluates the relevant scripts to determine whether the spending conditions have been satisfied.
The basic idea can be represented as:
Transaction Output → Spending Conditions → Unlocking Data → Script Evaluation → Valid or Invalid
For example, a traditional Bitcoin payment can require the spender to provide a valid digital signature corresponding to a particular public key. If the signature is valid, the transaction satisfies the spending condition.
Two Important Parts of Bitcoin Transaction Scripts
In traditional Bitcoin transactions, two scripts work together:
1. ScriptPubKey
The ScriptPubKey is contained in the transaction output. It defines the conditions that must be fulfilled before the output can be spent.
It is sometimes called the locking script because it effectively locks the bitcoins behind certain conditions.
A simplified example might require:
-
A valid public key
-
A valid digital signature corresponding to that public key
2. ScriptSig
The ScriptSig is traditionally included in the transaction input. It provides the information required to satisfy the conditions established by the previous transaction's ScriptPubKey.
It is commonly called the unlocking script, although this terminology is somewhat simplified because modern Bitcoin output types can use different mechanisms.
For a traditional pay-to-public-key-hash transaction, the ScriptSig supplies a signature and public key. The Bitcoin Script interpreter then evaluates these elements against the previous output's conditions.
How Script Execution Works
Bitcoin Script uses a stack to process data and instructions.
A stack follows the last-in, first-out principle. This means the most recently added item is normally the first item removed.
Consider a simplified sequence:
Data A
Data B
Operation
The data items are placed onto the stack. When an operation is encountered, the operation takes the required values from the stack, performs its function, and places the result back onto the stack.
Bitcoin Script contains different types of operations, commonly called opcodes.
Some opcodes manipulate data, while others perform cryptographic or logical operations.
For example:
<signature>
<public key>
OP_DUP
OP_HASH160
<public key hash>
OP_EQUALVERIFY
OP_CHECKSIG
This resembles the logic used by a traditional Pay-to-Public-Key-Hash transaction.
The script does not execute like a conventional application. Instead, the Bitcoin node processes the instructions sequentially and determines whether the required conditions are satisfied.
Example of Script Execution
Suppose Alice receives bitcoins using a traditional public-key-hash output.
The output establishes a condition that effectively says:
"To spend these bitcoins, provide a public key whose hash matches this specified hash, and provide a valid signature made with the corresponding private key."
Later, Alice creates a transaction spending those bitcoins.
Her input provides:
-
Her digital signature
-
Her public key
The Bitcoin node combines the spending information with the conditions from the previous output and evaluates the relevant script operations.
The process conceptually looks like this:
Signature + Public Key
|
v
Check Public Key Hash
|
v
Compare with Required Hash
|
v
Verify Digital Signature
|
v
Transaction Valid
If the public key does not correspond to the required hash, or if the signature is invalid, the script evaluation fails and the transaction is rejected as invalid.
Why Bitcoin Script Is Stack-Based
The stack-based design keeps Bitcoin's transaction-validation logic relatively simple and predictable.
Instead of allowing arbitrary programs to run, Bitcoin Script provides a restricted set of operations. This reduces the possibilities for uncontrolled computation during transaction validation.
For a decentralized network such as Bitcoin, this is important because thousands of independent nodes need to reach the same conclusion about whether a transaction is valid.
If transaction validation depended on unpredictable or unrestricted computation, nodes could potentially obtain different results or consume excessive resources.
Bitcoin Script Is Not Turing Complete
Bitcoin Script is deliberately designed with restrictions that prevent it from functioning like a fully general-purpose programming language.
One important characteristic is that Bitcoin Script does not provide unrestricted looping constructs. This helps make script execution more predictable.
A Bitcoin node should be able to evaluate a transaction script within reasonable resource limits. Since transaction validation is performed repeatedly by nodes, limiting computational complexity is important for network security.
This design differs from programmable blockchain platforms where smart contracts can support much more complex computations.
Common Bitcoin Script Opcodes
Bitcoin Script contains numerous opcodes with different purposes.
Some important categories include:
Stack operations
These move or manipulate items on the stack.
Examples include:
OP_DUP
OP_DROP
OP_SWAP
Cryptographic operations
These perform operations involving hashing or signature verification.
Examples include:
OP_HASH160
OP_SHA256
OP_CHECKSIG
Comparison operations
These compare values and determine whether conditions are satisfied.
Examples include:
OP_EQUAL
OP_EQUALVERIFY
Conditional operations
Bitcoin Script also provides mechanisms for conditional spending conditions.
Examples include:
OP_IF
OP_ELSE
OP_ENDIF
These allow transaction outputs to specify alternative conditions under which an output can be spent.
Bitcoin Script and Multisignature Transactions
Bitcoin Script can also enforce multisignature conditions.
For example, an output can require signatures from multiple authorized parties before the bitcoins can be spent.
A simplified requirement could be:
2 of 3 signatures required
This means three authorized public keys may exist, but at least two corresponding valid signatures are required to spend the funds.
Multisignature scripts can therefore be used when control over funds needs to be distributed among multiple parties.
Bitcoin Script and Pay-to-Script-Hash
Bitcoin Script also supports more complex spending conditions through Pay-to-Script-Hash (P2SH).
Instead of putting the complete spending script directly into the output, a P2SH output contains a hash of the redeem script.
The spender later provides the redeem script along with the information required to satisfy it.
Conceptually:
Output
|
v
Hash of Redeem Script
|
v
Spender provides Redeem Script
|
v
Hash is checked
|
v
Redeem Script is executed
This makes it possible to use more complicated scripts without requiring the sender to know all the details of the eventual spending conditions.
Bitcoin Script and Segregated Witness
Bitcoin's Segregated Witness (SegWit) introduced changes to how transaction data and scripts are handled.
For example, Pay-to-Witness-Public-Key-Hash (P2WPKH) moves signature-related witness data into the transaction's witness structure rather than using the traditional ScriptSig arrangement.
This improves transaction structure and contributes to Bitcoin's transaction-capacity and malleability improvements.
Bitcoin Script therefore continues to be fundamental even though the exact way scripts are represented can differ between transaction output types.
Script Validation and Consensus
Bitcoin Script is not merely a programming feature. It is part of Bitcoin's consensus rules.
Every full node independently validates transactions. If a transaction violates the applicable script conditions, the node considers that transaction invalid.
This decentralized validation is important because Bitcoin does not depend on a central organization to approve transactions.
For example, if a transaction attempts to spend an output without providing the required valid signature, nodes can reject it regardless of who created the transaction.
Script Execution and Security
Bitcoin Script's restricted nature contributes to the security of transaction validation.
A node must protect itself against scripts that could consume excessive computational resources or exploit unexpected execution behavior. Bitcoin therefore places restrictions on what scripts can do and how they are evaluated.
Additionally, Bitcoin has limits concerning transaction size, script size, operation counts, and other resource-related characteristics. These restrictions help prevent transaction validation from becoming an easy avenue for denial-of-service attacks.
Bitcoin Script vs. Smart Contract Languages
Bitcoin Script and smart-contract languages such as Solidity serve different purposes.
Bitcoin Script primarily establishes conditions for spending Bitcoin. Its operations are intentionally limited.
Smart-contract platforms can provide significantly more expressive programming environments. Developers can create applications involving tokens, financial protocols, marketplaces, games, and other programmable systems.
The distinction can be summarized as:
| Feature | Bitcoin Script | General Smart-Contract Language |
|---|---|---|
| Primary purpose | Define spending conditions | Build programmable applications |
| Execution model | Stack-based | Usually more general programming model |
| Complexity | Intentionally limited | Generally much more expressive |
| Looping | Restricted | Often supported |
| Main use | Transaction validation | Application logic |
| Security focus | Predictable transaction validation | Safe execution of complex programs |
Importance of Bitcoin Script
Bitcoin Script is a fundamental component of Bitcoin because it determines who can spend an output and under what conditions.
It enables more than simple signature verification. Through different script constructions, Bitcoin can support multisignature authorization, time-based spending conditions, conditional transactions, and other specialized mechanisms.
Its restricted design is intentional. Bitcoin prioritizes predictable and independently verifiable transaction validation rather than providing an unrestricted programming environment.
In simple terms, Bitcoin Script acts as the rule system attached to Bitcoin transactions. When a transaction attempts to spend previously locked bitcoins, the Bitcoin network evaluates those rules. If the supplied data satisfies the required conditions, the transaction can be considered valid; otherwise, it is rejected.