Hash Functions

10 min readarticleIncludes quiz · 5 questions

Hash functions are one-way mathematical blenders. Put data in, get a unique fingerprint out. Change even one bit of input, and the output changes completely. Bitcoin uses hash functions everywhere: addresses, mining, transaction IDs, and Merkle trees.

What is a Hash Function?

  • Definition: A mathematical function that takes any input and produces a fixed-size output (the hash or digest).
  • One-way: Easy to compute the hash from input, impossible to reverse (find input from hash).
  • Deterministic: Same input always produces the same output.
  • Avalanche effect: Changing one bit in the input completely changes the output.
  • Collision resistance: Nearly impossible to find two different inputs that produce the same hash.

Key Properties of Cryptographic Hash Functions:

1. Pre-image resistance: Given a hash, you can't find the original input. 2. Second pre-image resistance: Given an input and its hash, you can't find a different input with the same hash. 3. Collision resistance: You can't find any two inputs that produce the same hash. 4. Deterministic: Same input = same output, always. 5. Fast to compute: Efficient to calculate. 6. Fixed output size: No matter the input size, output is always the same length (256 bits for SHA-256).

Hash Function Visualization
Hash Function Visualization

SHA-256 (Bitcoin's Main Hash Function):

  • Full name: Secure Hash Algorithm 256-bit
  • Output: Always 256 bits (64 hexadecimal characters)
  • Example: SHA256("Hello") = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
  • Security: No known attacks; considered secure for decades to come.
  • Speed: Fast enough to compute billions per second, slow enough to make brute force impractical.
  • Uses in Bitcoin: Addresses, mining, transaction IDs, Merkle roots, block hashing.

Key Definitions:

  • SHA-256: Bitcoin's primary hash function (256-bit output).
  • RIPEMD-160: A 160-bit hash function used in creating Bitcoin addresses.
  • Double SHA-256: Hashing the output of SHA-256 again (SHA256(SHA256(x))). Bitcoin uses this in many places.
  • Hash rate: In mining context, the number of hashes per second miners compute.
  • Merkle root: A single hash representing all transactions in a block (created by hashing pairs of hashes).
  • Collision: When two different inputs produce the same hash (should be nearly impossible).

How Bitcoin Uses Hash Functions:

1. Mining (Proof of Work)

2. Addresses

3. Transaction IDs

  • Each transaction's data is hashed to create a unique transaction ID (txid).

4. Merkle Trees

  • All transactions in a block are hashed into a single Merkle root.

5. Block Hashes

  • Each block's header is hashed to create a unique block ID.

6. Script verification

  • Hash locks (like in Lightning) use hashes to lock funds until a secret (preimage) is revealed.
SHA-256 in Bitcoin
SHA-256 in Bitcoin

Example: Hash Avalanche Effect

Input: "Bitcoin" SHA-256: b4056df6691f8dc72e56302ddad345d65fead3ead9299609a826e2344eb63aa4

Input: "bitcoin" (lowercase b) SHA-256: 6b88c087247aa2f07ee1c5956b8e1a9f4c7f892a70e324f1bb3d161e05ca107b

Changing one letter completely changed the hash! This makes hashes unpredictable and secure.

Why Hashes Are Secure:

  • Huge output space: 2²⁵⁶ possible hashes (more than atoms in the universe).
  • No shortcuts: Only way to find a specific hash is trial and error (brute force).
  • Avalanche effect: Can't predict output by tweaking input.
  • No known breaks: SHA-256 has been thoroughly analyzed for decades with no practical attacks.
  • Quantum resistance: SHA-256 is believed to be resistant to quantum computers (unlike ECDSA).

Hash-Based Security in Bitcoin:

  • Address security: Even if someone knows your public key, they only see the hashed version (your address). This adds an extra layer of security.
  • Transaction immutability: Once a transaction is hashed into a block, changing it would change the block hash, breaking the chain.
  • Chain integrity: Each block includes the previous block's hash, creating an immutable chain.
  • Simplified Payment Verification (SPV): Light clients can verify transactions using Merkle proofs without downloading the full blockchain.

Other Hash Functions:

  • RIPEMD-160: Used alongside SHA-256 in address generation (smaller 160-bit output).
  • SHA-1: Older, broken hash function (collisions found). Not used in Bitcoin.
  • MD5: Even older, completely broken. Never use for security.
  • SHA-3: Newer NIST standard. Not used in Bitcoin, but secure.
  • BLAKE3: Modern, fast hash function. Not used in Bitcoin, but interesting for future systems.

Test Your Knowledge

This lesson includes a 5-question quiz (passing score: 75%).

Quiz functionality available in the mobile app.