P2MS ScriptPubKey Structure
P2MS ScriptPubKey Structure
The P2MS locking script is a "Template" script. It lists every authorized public key and the threshold required to unlock them. It is one of the longest script types in the Bitcoin protocol.
1. The Locking Script (ScriptPubKey)
A P2MS script follows this format:
OP_M [PubKey1] [PubKey2] ... [PubKeyN] OP_N OP_CHECKMULTISIG
-
OP_M: The number of signatures required (e.g.,
OP_2). -
[PubKeys]: The raw bytes of the N authorized public keys.
-
OP_N: The total number of public keys listed (e.g.,
OP_3). -
OP_CHECKMULTISIG: The opcode that evaluates the stack and verifies the signatures against the list of keys.
2. Threshold Values
The values M and N are represented by small integers (OP_1 through OP_16).
-
Consensus rules allow
Nto be as high as 20. -
Standardness rules (what nodes will actually relay) usually limit
Nto 3 if it is a "Bare" P2MS script.
3. The Execution Logic
When the Bitcoin Virtual Machine hits OP_CHECKMULTISIG, it does the following:
-
It pops the value
Nfrom the stack. -
It pops
Npublic keys. -
It pops the value
M. -
It pops
Msignatures. -
It iterates through the keys to see if the signatures are valid.
4. Raw Hex Example (2-of-2)
A 2-of-2 multisig script would look like this:
52 [33-byte Key1] [33-byte Key2] 52 ae
-
52: Hex forOP_2. -
ae: Hex forOP_CHECKMULTISIG.
| Component | Hex Code | Role |
|---|---|---|
| M (Threshold) | 0x51 to 0x60 |
Required signatures |
| Public Keys | Variable | Authorized participants |
| N (Total Keys) | 0x51 to 0x60 |
Total list size |
| Opcode | 0xae |
The verification logic |
In the next section, we will discuss the famous Off-by-One Bug in multisig.
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: