Cryptographic Primitives

πŸ€” The Foundation of Digital Trust

Before we dive into Bitcoin's cryptographic building blocks, consider these fundamental questions:

How can you prove you own something digital without revealing your secret?

If you share your password to prove it's yours, anyone can steal it. What's the alternative?

🎲
How do you generate a truly random secret that no one else will ever create?

With 7.8 billion people on Earth, how do you ensure your secret is unique?

πŸ“œ
How can thousands of nodes verify a transaction without storing every signature?

Bitcoin blocks contain thousands of transactions. Is there a compact way to prove inclusion?

What makes a function "one-way" in cryptography?

Easy to compute forward, impossible to reverse. How does mathematics achieve this?

Bitcoin answers all of these with three cryptographic primitives.

Part 1: Cryptographic Hash Functions (SHA-256)

A cryptographic hash function takes any input (text, file, transaction data) and produces a fixed-size output called a hash. Bitcoin uses SHA-256 (Secure Hash Algorithm, 256-bit), which produces a 64-character hexadecimal string.

Properties of Cryptographic Hash Functions

  1. Deterministic: Same input always produces same output
  2. Quick Computation: Fast to calculate hash from input
  3. Avalanche Effect: Tiny input change completely changes output
  4. One-Way Function: Impossible to reverse (find input from hash)
  5. Collision Resistant: Nearly impossible to find two different inputs with same hash

Example: SHA-256 in Action

Input: "Hello World"
SHA-256: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

Input: "Hello World!"  ← Added one character (!)
SHA-256: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069

Input: "hello world"  ← Changed capitalization
SHA-256: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Notice how completely different the outputs are despite minimal input changes. This is the avalanche effectβ€” a critical property for Bitcoin's security.

SHA-256 in Bitcoin

Bitcoin uses SHA-256 for:

  • Mining: Finding hashes that meet difficulty target
  • Transaction IDs: Unique identifier for each transaction
  • Block IDs: Hash of block header creates block ID
  • Merkle Trees: Compressing thousands of transactions into one hash
  • Address Generation: Part of converting public keys to addresses (with RIPEMD-160)

Interactive SHA-256 Hasher

Experience the avalanche effect in real-time. See how tiny input changes completely transform the hash.

Characters: 11 Hash length: 64 hex chars (256 bits)
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
10100101 10010001 10100110 11010100 00001011 11110100 00100000 01000000
01001010 00000001 00010111 00110011 11001111 10110111 10110001 10010000

Avalanche Effect Demonstration

Original: "Hello World"
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
Changed: "Hello World!" (+1 char)
7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
100% different hash from just 1 character change!

Part 2: Digital Signatures & ECDSA

Digital signatures solve the proof-of-ownership problem. Bitcoin uses Elliptic Curve Digital Signature Algorithm (ECDSA) with the secp256k1 curveβ€”the same curve used by Ethereum and many other cryptocurrencies.

The Magic of Asymmetric Cryptography

Unlike passwords (symmetric), asymmetric cryptography uses two related keys:

  • Private Key: A secret number (256 bits of randomness) that NEVER leaves your wallet
  • Public Key: Mathematically derived from private key, safe to share publicly

The mathematical relationship is one-way: Easy to derive public key from private key, but computationally impossible to reverse (would take longer than the universe's age with current computers).

How ECDSA Signatures Work

// Signing a transaction
1. Transaction data: "Send 0.5 BTC to Alice"
2. Hash transaction: SHA-256(transaction) β†’ message hash
3. Sign with private key: ECDSA_sign(message_hash, private_key) β†’ signature (r, s)
4. Broadcast: transaction + signature + public_key

// Verification (by nodes)
5. Hash transaction: SHA-256(transaction) β†’ message hash
6. Verify signature: ECDSA_verify(message_hash, signature, public_key) β†’ true/false

Why This Is Secure

The Elliptic Curve Discrete Logarithm Problem (ECDLP): Given a public key (point on curve), finding the private key (scalar) requires solving an equation with no known efficient algorithm. Bitcoin's security relies on this mathematical hard problem.

Key Lifecycle in Bitcoin

  1. Entropy: Generate 256 bits of random data (private key)
  2. Private Key: Usually displayed as 64 hexadecimal characters
  3. Public Key: ECDSA point multiplication on secp256k1 curve
  4. Address: Hash160(public_key) + Base58Check encoding

πŸ”‘ Interactive Key Generation Tool

Visualize the complete Bitcoin key lifecycle: Entropy β†’ Private Key β†’ Public Key β†’ Address

⚠️ EDUCATIONAL ONLY - NEVER USE THESE KEYS FOR REAL FUNDS

Keys generated in your browser are for learning purposes. Real wallets use secure entropy sources.

1

Entropy (Random Data)

256 bits of cryptographically secure randomness

Click "Generate New Key Pair" to start
2

Private Key

Your secret - never share this!

Waiting for entropy...
πŸ“ This is a 256-bit number, typically shown as 64 hexadecimal characters
3

Public Key

Derived via ECDSA secp256k1 curve

Waiting for private key...
Formula: Public Key = Private Key Γ— G where G is the generator point on secp256k1
4

Bitcoin Address

Hash160 + Base58Check encoding

Waiting for public key...
🏦 Process: SHA-256(Public Key) β†’ RIPEMD-160 β†’ Add version byte β†’ Add checksum β†’ Base58 encode

πŸ”¬ What Just Happened?

  • Entropy: Browser's crypto.getRandomValues() generated 256 random bits
  • Private Key: Random number converted to hex (your secret)
  • Public Key: Elliptic curve point multiplication (one-way function)
  • Address: Double hash + encoding for user-friendly format

Part 3: Merkle Trees

A Merkle tree (or hash tree) is a data structure that allows efficient verification of large datasets. Bitcoin uses Merkle trees to compress thousands of transactions into a single 32-byte hash (the Merkle root).

How Merkle Trees Work

Imagine a block with 4 transactions (in reality, blocks can have 2,000+ transactions):

Level 3 (Root):              ABCDEFGH
                                /          \
Level 2:                  ABCD            EFGH
                         /    \          /    \
Level 1:              AB      CD      EF      GH
                     / \     / \     / \     / \
Level 0 (Leaves):   A   B   C   D   E   F   G   H

Where:
- A, B, C, D, E, F, G, H = Transaction hashes (TxID)
- AB = SHA-256(SHA-256(A + B))  ← Double SHA-256
- ABCD = SHA-256(SHA-256(AB + CD))
- Merkle Root = ABCDEFGH

Why Merkle Trees Matter

  1. Efficient Verification: Prove a transaction is in a block without downloading the whole block
  2. SPV (Simplified Payment Verification): Mobile wallets can verify payments with only block headers
  3. Compact Proofs: Proof size is O(log n) instead of O(n)

Merkle Proof Example

To prove transaction E is in the block, you only need:

  • Transaction E (your transaction)
  • F (E's sibling)
  • GH (parent's sibling)
  • ABCD (grandparent's sibling)
  • Merkle Root from block header

That's only 4 hashes instead of all 8 transactions! For a block with 2,048 transactions, you'd need only 11 hashes instead of 2,048 (99.5% reduction).

🌳 Interactive Merkle Tree Builder

Build a Merkle tree from transactions and create proof-of-inclusion paths

No transactions added yet. Add some transactions to build a Merkle tree.

Key Takeaways

  • βœ“ SHA-256 creates unique fingerprints for data (deterministic, one-way, collision-resistant)
  • βœ“ Bitcoin uses SHA-256 for mining, transaction IDs, addresses, and Merkle trees
  • βœ“ ECDSA enables digital signatures with public/private key pairs
  • βœ“ Private keys are secret, public keys can be shared, addresses are derived from public keys
  • βœ“ Security relies on ECDLP (elliptic curve discrete logarithm problem) being computationally hard
  • βœ“ Merkle trees compress thousands of transactions into one 32-byte root hash
  • βœ“ Merkle proofs enable SPV: prove inclusion with O(log n) data instead of O(n)

βœ“ Knowledge Check

Test your understanding of cryptographic primitives with these practical questions.

1. What is the primary characteristic that makes SHA-256 "one-way"?

2. In Bitcoin's ECDSA system, what can you safely share publicly without compromising your bitcoin?

3. What property of hash functions makes tiny input changes produce completely different outputs?

4. How does a Merkle tree help with blockchain efficiency?

5. For a block with 2,048 transactions, how many hashes would you need for a Merkle proof of inclusion?