← back to the interactive explainer
White-hat deep-dive · run on self-generated keys only

The reused number that steals Bitcoin

The elliptic curve is unbreakable. The hash is unbreakable. Yet real coins have been stolen — because of one random number used twice. Here's exactly how, with the ~20 lines of arithmetic that recover the key, demonstrated on keys we generate ourselves.

Every signature hides a secret dice-roll

To spend Bitcoin you sign a transaction with ECDSA. Each signature needs a fresh, secret, random number called the nonce k. A signature is a pair (r, s):

r = (k·G)x mod n     s = k⁻¹ · (z + r·d) mod n

where d is your private key, z is the message hash, and G, n are public curve constants. The k never leaves your wallet — it's thrown away after signing. As long as every k is unique and unpredictable, the signature reveals nothing about d.

The whole security rests on one word: unique. Break that, and the arithmetic below does the rest.

Sign twice with the same k, and d falls out

Suppose a buggy wallet reuses the same k for two different transactions. Then both signatures share the same r (because r depends only on k·G). An attacker who sees both signatures on the blockchain now has:

1. Two equations, same k and d:
s₁ = k⁻¹(z₁ + r·d)    s₂ = k⁻¹(z₂ + r·d)
2. Subtract them — the r·d term cancels, so k pops out:
k = (z₁ − z₂) · (s₁ − s₂)⁻¹ mod n
3. Plug k back into either equation and solve for the private key d:
d = (s₁·k − z₁) · r⁻¹ mod n

That's it. Two signatures, four subtractions and two modular inverses, and the "unbreakable" key is in the attacker's hands. The curve was never touched.

Proof you can run yourself (on a key we make here)

This demo generates its own victim key, signs two messages with a deliberately reused nonce, then recovers the key from only the public signatures — nothing real is touched:

# pip install ecdsa ; secp256k1 order n, generator G
import secrets, hashlib
from ecdsa import SECP256k1
from ecdsa.numbertheory import inverse_mod
n = SECP256k1.order; G = SECP256k1.generator
H = lambda m: int.from_bytes(hashlib.sha256(m).digest(),"big") % n

d = secrets.randbelow(n-1)+1          # the victim's secret (we made it)
k = secrets.randbelow(n-1)+1          # a nonce the buggy RNG will REUSE
def sign(msg):
    z = H(msg); r = (k*G).x() % n
    s = (inverse_mod(k,n)*(z + r*d)) % n
    return r, s, z

r1,s1,z1 = sign(b"Pay Alice 1 BTC")
r2,s2,z2 = sign(b"Pay Bob 2 BTC")     # different message, SAME k — the bug

k_rec = ((z1-z2) * inverse_mod((s1-s2) % n, n)) % n
d_rec = ((s1*k_rec - z1) * inverse_mod(r1, n)) % n
print("recovered == victim?", d_rec == d)   # → True
Output: recovered == victim? True. The tell-tale sign in the wild is simply two signatures sharing the same r — scannable in public blockchain data without touching a single key.

This isn't hypothetical

It has happened, repeatedly, with real losses:

• 2010 — Sony PlayStation 3. Sony signed firmware with a constant nonce. The fail0verflow team recovered the master signing key and the console was permanently jailbroken.

• 2013 — Android SecureRandom. A flaw made some Android wallets generate repeated nonces. Attackers scanned the chain for reused r values and swept the affected wallets. Real Bitcoin was stolen.

• It still surfaces today whenever a hardware wallet, library, or hastily-written signer botches its RNG.

The lesson (and the fix)

A private key is only as strong as the randomness around it. secp256k1 and SHA-256 are fine — the failure is always broken randomness. The modern fix is deterministic nonces (RFC 6979): derive k from the private key and the message hash, so it's unique per message and never depends on a flaky RNG. Every serious wallet uses it now.

White-hat rule. Detecting reused nonces in public signatures is legitimate research. But a recovered key belongs to its owner: if you ever find a live weakness, disclose it to the owner — never sweep the funds. Possessing a stranger's key is the crime, published or not.