BitcoinMachine

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.

0 / 4 sections

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_SHA256

SHA256 of the byte 0x01. Result is always 32 bytes.

Script
OP_1
OP_SHA256
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
OP_HASH256

SHA256(SHA256(0x01)). Used for txids. Different result from single SHA256.

Script
OP_1
OP_HASH256
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
The two hashes of the same input are completely different — that's by design. Double-SHA256 was chosen by Satoshi to defend against length-extension attacks.

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_RIPEMD160

RIPEMD-160 of 0x01. Result is 20 bytes.

Script
OP_1
OP_RIPEMD160
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
OP_HASH160

RIPEMD160(SHA256(0x01)). This is the pubkey hash in every P2PKH address.

Script
OP_1
OP_HASH160
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady

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.

Real Bitcoin

HASH160 and Bitcoin addresses

When you share a Bitcoin address starting with '1', you're sharing a Base58Check-encoded HASH160 of your public key. The node re-derives this hash during validation to confirm you own the key.
Hash commitment

Hashes the same value twice independently and checks equality — both sides of the comparison must match.

Script
OP_1
OP_SHA256
OP_1
OP_SHA256
OP_EQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_1Pushes the number 1 onto the stack.
0 stepsReady
Try building a hash lock yourself. Push a preimage, hash it, push the expected hash, and use OP_EQUAL to verify.

Ctrl+Enter to run

Script
0xdeadbeefdeadbeef
OP_SHA256
<hash256>32B
OP_EQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
0xdeadbeefPush 4 bytes onto the stack.
0 stepsReady

04

Your turn

Challenge

SHA256 of anything

Write a script that leaves a single 32-byte SHA256 hash on the stack. Push any value and hash it — the validator just checks for a 32-byte result.

Ctrl+Enter to check

← Home