OP_PUSHDATA2 - Pushing Up to 65535 Bytes
5. OP_PUSHDATA2 — Pushing Up to 65535 Bytes
Overview
OP_PUSHDATA2 extends the data push capability of Bitcoin Script to allow pushing up to 65,535 bytes (64 KB minus 1 byte) of data in a single push operation. It occupies opcode byte 0x4d and uses a 2-byte little-endian length field following the opcode byte.
Opcode Format
Format: 0x4d <length:2 bytes little-endian> <data:length bytes>
Example: Push 300 bytes (0x012C in little-endian = 0x2C 0x01):
0x4d 0x2C 0x01 <300 bytes of data>
Example: Push 1000 bytes (0x03E8 = 0xE8 0x03 in little-endian):
0x4d 0xE8 0x03 <1000 bytes of data>
The 2-byte little-endian length field means values are stored least-significant byte first:
Length 256 = 0x0100 --> stored as 0x00 0x01
Length 1000 = 0x03E8 --> stored as 0xE8 0x03
Length 65535 = 0xFFFF --> stored as 0xFF 0xFF
Practical Script Limits
While OP_PUSHDATA2 can theoretically push up to 65,535 bytes, practical Bitcoin consensus and standardness rules impose much tighter limits:
Maximum script element size (consensus): 520 bytes
Maximum script size (consensus): 10,000 bytes
Maximum standard output script size: 10,000 bytes
Maximum standard scriptSig size: 1,650 bytes
This means that in practice, OP_PUSHDATA2 is useful only for data in the range of 520+ bytes — but even pushing 521 bytes is a consensus violation for stack elements. The opcode is therefore largely vestigial in current Bitcoin usage.
Historical Context
OP_PUSHDATA2 was more relevant in early Bitcoin when the protocol had fewer restrictions. Before element size limits were enforced as consensus rules, large data blobs could be pushed and used in complex scripts. After BIP 16 (P2SH) and subsequent consensus hardening, the 520-byte element limit made OP_PUSHDATA2 usable only in very narrow circumstances.
Interpreter Implementation
// From src/script/interpreter.cpp
case OP_PUSHDATA2: {
if (pc + 2 > pend)
return set_error(serror, SCRIPT_ERR_BAD_OPCODE);
nSize = ReadLE16(&pc[0]); // Read 2-byte little-endian length
pc += 2;
break;
}
ReadLE16 reads two bytes in little-endian order and assembles them into a 16-bit unsigned integer representing the data length.
BIP 62 Minimal Push Context
Under BIP 62, OP_PUSHDATA2 must only be used when the data length is between 256 and 65,535 bytes. Using it for shorter data when a smaller opcode would suffice is a violation:
Non-minimal (BIP 62 violation) for 100-byte push:
0x4d 0x64 0x00 <100 bytes> (OP_PUSHDATA2 when OP_PUSHDATA1 would work)
Minimal (BIP 62 compliant) for 100-byte push:
0x4c 0x64 <100 bytes> (OP_PUSHDATA1)
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: