MASTERING-BITCOIN Β· Unit 4 Β· Video 2
| Concept | Form | Notes |
|---|---|---|
| P2PK output script | <pubkey> OP_CHECKSIG |
65-byte key sits openly in the output |
| P2PKH output script | OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG |
20-byte commitment only |
| P2PKH input script | <sig> <pubkey> |
Spender reveals key at spend time |
| HASH160 recipe | 65 B β 32 B β 20 B | |
| Success condition | Stack top equals 1 after evaluation |
LIFO stack model |
Conceptual question: How does the Bitcoin stack machine evaluate a payment? Choose a script type and advance the step slider to watch every push, duplicate, hash, and verification β one opcode at a time.
β interactive visualization β coming to this page format soon
Reflect: Notice how P2PK finishes in only 3 pushes and a CHECKSIG, while
P2PKH takes 7 steps. The extra work is the commitment check: before we verify
the signature, we prove the revealed pubkey hashes to the value baked into the
output. That's why OP_DUP appears β the pubkey is consumed twice, once by
HASH160 and once by CHECKSIG.
Conceptual question: A hash function is deterministic (same input β same output) and collision-resistant (finding two matching inputs is infeasible). Type an "input" below β think of it as a public key β and watch the SHA-256 digest. Change a single character; the output changes completely. This avalanche behavior is why a hash makes a reliable commitment.
β interactive visualization β coming to this page format soon
Notice: A 65-byte public key collapses to a 20-byte commitment β roughly a 3.25Γ reduction β while still uniquely binding to the original key.
Conceptual question: Each P2PK output carries a 65-byte public key; each P2PKH output carries only a 20-byte hash. Across millions of transactions, how much blockchain real estate does the hash-commitment trick save?
β interactive visualization β coming to this page format soon
Reflect: The byte savings are nice, but the decisive win is usability: the hash is small enough to share through any medium, and the receiver never has to be online at payment time.
Question 1
Bitcoin's HASH160 function is defined as RIPEMD-160(SHA-256(K)), where
is a 65-byte public key. What is the size of the final output?
β Correct! RIPEMD-160 produces a 20-byte hash, regardless of input size.
β Not quite.
Solution:
HASH160 is the composition of two hash functions applied in order:
The inner SHA-256 produces 32 bytes. That digest is then fed into RIPEMD-160, whose output length is 20 bytes (hence the "160" β 160 bits = 20 bytes).
So the final HASH160 output is 20 bytes. This is the value that sits inside a
P2PKH output script as the commitment.
Question 2
The P2PKH output script is:
OP_DUP OP_HASH160 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG
Why does the script begin with OP_DUP?
β
Correct! Without a duplicate, the hash check would destroy the key before CHECKSIG could use it.
β Not quite. Think about what happens to the public key on the stack after OP_HASH160.
Solution:
After the input script pushes <sig> and <pubkey>, the stack is [sig, pubkey].
The script now needs to do two things with the pubkey:
OP_HASH160 and compare against the 20-byte commitment
(OP_EQUALVERIFY). This proves the spender revealed the right key.OP_CHECKSIG.Both OP_HASH160 and OP_CHECKSIG consume the public key β they pop it off the
stack. So without OP_DUP, the first operation would destroy the key before the
second operation could use it.
OP_DUP duplicates the top of the stack, leaving [sig, pubkey, pubkey]. The top
copy is then consumed by the hash check, and the remaining copy is consumed by the
signature check. Every opcode earns its place.
Question 3
True or False. In the original pay-to-IP / P2PK scheme, a receiver had to be online and reachable at the moment of payment β because Alice's node needed to connect to theirs and request a fresh public key before building the transaction.
β Correct! Pay-to-IP required a live request/response with the receiver; if they were offline or behind a firewall, payment failed.
β Not quite. Re-read the mechanics of the pay-to-IP flow β Alice's node had to contact the receiver to obtain a fresh public key.
β Not quite. Try again β the hints above can help.
Solution: True.
In the early Bitcoin design, you didn't share your public key directly β you shared your IP address. The payment flow was:
If the receiver was offline, behind a NAT router, or blocked by a firewall, step 2 failed and the payment never happened. That's why you couldn't put an IP address on a business card, email an invoice, or accept donations while asleep.
P2PKH fixed this by letting you publish a 20-byte hash once, through any medium, without needing an active network connection. The full public key is only revealed later, at spend time β by you.
Question 4
During P2PKH execution, after OP_HASH160 runs, the stack is
[<sig>, <pubkey>, H(pubkey)]. The next two opcodes are the push of the
committed 20-byte hash from the output script, then OP_EQUALVERIFY. What
is the stack immediately after OP_EQUALVERIFY completes successfully?
β
Correct! OP_EQUALVERIFY consumes both hashes and leaves nothing β CHECKSIG then pops sig and pubkey.
β Not quite.
Solution:
Let's trace the stack step by step, starting from [sig, pubkey, H(pubkey)]:
| Step | Opcode / push | Stack after |
|---|---|---|
| 1 | Push <commit> from output script |
[sig, pubkey, H(pubkey), commit] |
| 2 | OP_EQUALVERIFY |
[sig, pubkey] |
OP_EQUALVERIFY is equivalent to OP_EQUAL followed by OP_VERIFY:
OP_EQUAL pops the top two items, compares them, and pushes 1 if equal or 0
if not.OP_VERIFY pops that result and aborts the script if it is 0. If it is 1,
execution continues β and OP_VERIFY leaves nothing on the stack.So the combined effect of OP_EQUALVERIFY is: pop two items, abort if unequal,
otherwise continue with both popped and nothing added. The final stack is
[<sig>, <pubkey>] β exactly the two items OP_CHECKSIG needs next.
β [<sig>, <pubkey>, 1] would be the result of a plain OP_EQUAL, not
OP_EQUALVERIFY.
Wrapping up. P2PKH replaced P2PK by committing to a public key with a 20-byte hash instead of publishing the full 65-byte key. That single change decoupled receiving an address from being online β addresses could now live on paper, in email, on a sign β and set the template for the rest of Bitcoin's evolution: Base58Check, Bech32, Merkle roots, SegWit, Taproot. Every one of them is the same idea, applied somewhere new: keep data compact, reveal detail only when spent.
Solved: 0 / 4