Module 05
Crypto Primitives
Bitcoin Script has five built-in hash functions. Two of them — HASH160 and HASH256 — are fundamental to how Bitcoin addresses and transactions work. Every P2PKH address encodes a HASH160.
What you'll learn
- →Apply OP_SHA256, OP_HASH256, OP_RIPEMD160, OP_HASH160 correctly
- →Explain the difference between single and double SHA-256
- →Derive a Bitcoin pubkey hash (HASH160) step by step
- →Build a hash commitment script that proves knowledge of a preimage
01
SHA256 and HASH256
OP_SHA256 applies a single SHA-256 and pushes the 32-byte result. OP_HASH256 applies it twice — SHA256(SHA256(x)) — which is used for transaction IDs and block headers.
Notice how the raw bytes on the stack grow to exactly 32 bytes regardless of the input size. That's the defining property of a hash function.
OP_SHA256SHA256 of the byte 0x01. Result is always 32 bytes.
↑ top of stack
nothing here yet
press Step or Run to push an item
OP_HASH256SHA256(SHA256(0x01)). Used for txids. Different result from single SHA256.
↑ top of stack
nothing here yet
press Step or Run to push an item
02
RIPEMD160 and HASH160
OP_RIPEMD160 produces a 20-byte digest. OP_HASH160 is RIPEMD160(SHA256(x)) — also 20 bytes — and this is the hash function behind every P2PKH and P2SH address. The 20-byte output is what gets base58check-encoded into the address you see in a wallet.
OP_RIPEMD160RIPEMD-160 of 0x01. Result is 20 bytes.
↑ top of stack
nothing here yet
press Step or Run to push an item
OP_HASH160RIPEMD160(SHA256(0x01)). This is the pubkey hash in every P2PKH address.
↑ top of stack
nothing here yet
press Step or Run to push an item
03
Hash commitments
A hash commitment is a pattern where a script locks funds to a hash of a secret value. The spender proves knowledge of the preimage by revealing it — the script re-hashes and checks for equality.
This is how Hash Time-Locked Contracts (HTLCs) in the Lightning Network work at the Script level.
HASH160 and Bitcoin addresses
Hash commitmentHashes the same value twice independently and checks equality — both sides of the comparison must match.
↑ top of stack
nothing here yet
press Step or Run to push an item
Ctrl+Enter to run
↑ top of stack
nothing here yet
press Step or Run to push an item
04
Your turn
SHA256 of anything
Ctrl+Enter to check