Source

A from-scratch tour of Bitcoin in Python

Open the source

Karpathy walks through creating, signing, and broadcasting a Bitcoin testnet transaction in pure Python with zero dependencies, implementing elliptic curve key generation, SHA256, RIPEMD160, address encoding, transaction serialization, and ECDSA signing from scratch. He uses the build-it-yourself approach to demystify how Bitcoin represents value (UTXOs with locking scripts) and how cryptography secures transactions.

- He frames blockchain as extending open source software to 'open source + state', sharing a running computer permissionlessly. - A Bitcoin identity is a random secret integer, a public key equal to secret_key * G on secp256k1, and an address derived by hashing and base58check encoding. - He shows that SHA256 and the ECC math are 'not scary' underneath: mostly adds, multiplies, modulo operations and bit mixing. - UTXOs must be spent in full; you send part to the recipient, change back to yourself, and the leftover becomes the miner's fee. - The signed message is the transaction with the input's script_sig replaced by the referenced output's script_pubkey, and locking and unlocking scripts run together on a stack machine. - He warns that the educational code is not for production: reused signing nonces leak keys, and you should not roll your own crypto.

Based on Andrej Karpathy's public content. Not affiliated with or endorsed by Andrej Karpathy.

Supports these rules

Quotes from this video

“in the spirit of “what I cannot create I do not understand”, what better way to do this than implement it from scratch?”

“this is supposed to be a zero-dependency implementation, so import hashlib is cheating.”

“I want you to note that again there is nothing too scary going on inside.”

“everything is just a bunch of adds/multiplies over the tuples (x,y) with some modulo p sprinkled everywhere in between.”

“you’d never want to actually implement yourself (“don’t roll your own crypto”).”

“Please don’t use this anywhere close to anything that touches production.”

“we did above, we will only cover the (by far) most common use case of signing the entire transaction”

“we can verify that the generator point is indeed on the curve, i.e. y^2 x^3 7 (mod p)”

Topics