← LibraryBlog · AI

A from-scratch tour of Bitcoin in Python

Andrej Karpathy
✓ Source-grounded

A deep-dive technical guide that teaches Bitcoin by forcing the reader to implement its core cryptographic and transactional logic from scratch in pure Python.

Read the original

In one sentence

To truly understand Bitcoin, you must move beyond high-level abstractions and implement its core primitives—from elliptic curve math to script execution—from the ground up.

Why read this (and who it's for)

This book solves the "black box" problem of cryptocurrency. Many people can use a wallet, but few understand the underlying machinery that makes a decentralized ledger possible. It is designed for engineers and developers who want to move from "user" to "architect." After reading this, you will be able to write the code that generates keys, hashes blocks, and validates transactions without relying on external libraries.

The big idea

The central thesis is that Blockchain is "Open Source + State." While traditional open source allows for the sharing of logic, blockchain allows for the sharing of a "running computer"—a persistent, permissionless state that anyone can interact with.

Karpathy argues that the only way to internalize this complexity is through the principle: "What I cannot create, I do not understand." By stripping away dependencies and implementing Bitcoin in pure Python, the reader is forced to confront the mathematical reality of the protocol. The logic follows a bottom-up path: first, you build the identity (Cryptography); then, you build the proof (Hashing/PoW); finally, you build the economy (Transactions/Scripting).

Key ideas

Blockchain as Open Source + State

Elliptic Curve Cryptography (ECC) Identity

The Properties of SHA-256

The "Change" Model of Transactions

Bitcoin Script (Pkscript)

Frameworks & tools

The Public Key Generation Framework

To generate a Bitcoin identity, follow these mathematical steps:

  1. Select the Curve: Use secp256k1 (where $a=0$ and $b=7$).
  2. Select a Private Key: Choose a random integer $k$ such that $1 \le k < n$ (where $n$ is the order of the curve).
  3. Perform Scalar Multiplication: Calculate the public key by adding the Generator Point ($G$) to itself $k$ times.
  4. Formula: $\text{public_key} = \text{secret_key} \cdot G$.

The Transaction Validation Framework (The "Bitcoin VM")

To validate a transaction, the system executes a stack-based script:

  1. Concatenate: Join the script_pubkey (locking script) and script_sig (unlocking script).
  2. Push to Stack: Push the signature and the public key onto the stack.
  3. Verify Hash: Use OP_DUP and OP_HASH160 to ensure the public key matches the expected hash.
  4. Verify Signature: Execute OP_CHECKSIG to ensure the signature is valid for that public key.

Memorable examples & stories

The "Random Walk" of the Generator Point

The author describes the public key as a "random walk" on the curve's cycle. This helps visualize how a simple starting point ($G$) can reach a seemingly random point on the curve when multiplied by a secret integer. Lesson: Cryptography is built on the difficulty of predicting where a walk ends.

The "Change" as a Fee

The author explains how the difference between input and output values isn't "lost" money but is the mechanism for miner incentives. Lesson: Economic incentives are hard-coded into the transaction data structure.

The Privacy of Hash160

The author explains how using a hash of a public key (p2pkh) instead of the raw public key hides the owner's identity until they decide to spend the funds. Lesson: Privacy is a default property of the data structure, not just an added feature.

Key quotes

"What I cannot create, I do not understand." Why it matters: This is the core pedagogical philosophy of the book—learning through implementation.

"Blockchain extends open source software development to include 'open source + state'." Why it matters: It defines the unique value proposition of blockchain technology.

"The sum of all outputs must be lower than the sum of all inputs (prevents minting money)." (Paraphrased) Why it matters: This is the fundamental rule of the Bitcoin "economy."

How to apply it

This week

Ongoing

Common pitfalls

Connects to

If you remember only three things

Internalize it

Fidelity note

This summary is grounded in the provided notes covering sections 1 through 4. It accurately reflects Karpathy's "build-to-understand" methodology and the technical specifics of ECC, SHA-256, and the UTXO model. The summary does not include information on mining difficulty adjustments, Layer 2 solutions, or specific privacy coins, as those were not present in the provided material.

More from Andrej Karpathy