Script Size Limits and Consensus Rules
Script Size Limits and Consensus Rules
Bitcoin Script has multiple size limits enforced at the consensus level. These limits prevent resource exhaustion attacks and ensure that script execution is bounded in time and memory.
Script Size Limit: 10,000 Bytes
The maximum size for any single script (scriptSig or scriptPubKey) is 10,000 bytes at the consensus level. Bitcoin Core's policy rules are more restrictive — MAX_SCRIPT_ELEMENT_SIZE is 520 bytes for individual stack items, and the overall transaction weight limit constrains practical script sizes further.
// From src/script/script.h:
static const int MAX_SCRIPT_SIZE = 10000;
// Checked during script parsing:
if (script.size() > MAX_SCRIPT_SIZE) {
return set_error(serror, SCRIPT_ERR_SCRIPT_SIZE);
}
Opcode Count Limit: 201 Non-Push Opcodes
A script may contain at most 201 non-push opcodes (opcodes other than data pushes). This limits the computational complexity of scripts even when they don't take up many bytes.
// From interpreter.cpp:
int nOpCount = 0;
// In execution loop:
if (opcode > OP_16) { // Non-push opcodes start after OP_16
++nOpCount;
if (nOpCount > MAX_OPS_PER_SCRIPT) { // MAX_OPS_PER_SCRIPT = 201
return set_error(serror, SCRIPT_ERR_OP_COUNT);
}
}
Stack Item Size Limit: 520 Bytes
Individual items on the stack cannot exceed 520 bytes. This applies to both pushed data items and results of operations:
// From interpreter.cpp:
static const size_t MAX_SCRIPT_ELEMENT_SIZE = 520;
// Checked when pushing data:
if (stacktop(-1).size() > MAX_SCRIPT_ELEMENT_SIZE) {
return set_error(serror, SCRIPT_ERR_PUSH_SIZE);
}
Stack Depth Limit: 1,000 Items
The combined main stack and alt stack cannot exceed 1,000 items at any point during execution:
// From interpreter.cpp:
static const int MAX_STACK_SIZE = 1000;
// Checked after each operation:
if (stack.size() + altstack.size() > MAX_STACK_SIZE) {
return set_error(serror, SCRIPT_ERR_STACK_SIZE);
}
Numeric Value Limits
Script numbers (used in arithmetic opcodes) are limited to 4 bytes (values from -2,147,483,647 to 2,147,483,647). Numbers outside this range cannot be used with arithmetic opcodes.
// From script/script.h CScriptNum:
static const size_t nDefaultMaxNumSize = 4;
// 5-byte numbers are only allowed for CHECKLOCKTIMEVERIFY and
// CHECKSEQUENCEVERIFY (which use timestamps requiring larger values)
Tapscript Changes These Limits
Tapscript (BIP 342) modifies some of these limits:
Standard Script limits:
- MAX_SCRIPT_SIZE: 10,000 bytes
- MAX_OPS_PER_SCRIPT: 201
Tapscript limits:
- Script size: bounded by transaction weight, not a fixed byte limit
- Opcode count: NO LIMIT (the 201 limit is removed)
- Stack item size: still 520 bytes
- Stack depth: still 1,000 items
The removal of the opcode count limit in Tapscript is intentional — with Schnorr signatures and batch verification, the computational cost of scripts is better controlled through weight-based limits rather than opcode counting.
Summary Table of All Script Limits
Limit | Legacy Script | Tapscript
-------------------------|---------------|----------
Max script size | 10,000 bytes | weight-limited
Max non-push opcodes | 201 | unlimited
Max stack item size | 520 bytes | 520 bytes
Max stack depth | 1,000 items | 1,000 items
Max numeric value | 4 bytes | 4 bytes
Max P2SH redeem script | 520 bytes | N/A
Max P2WSH witness script | 10,000 bytes | N/A
Max sigops per block | 80,000 | weight-limited
These limits represent a carefully tuned balance between script expressiveness and network protection. They have changed slowly over Bitcoin's history, with each change requiring a soft fork and broad ecosystem consensus.
**
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: