Skip to content
KALANBLON
Show contents

Chapter 2 of 6 · 20 min

ECDSA and the nonce that betrays the key

By the end of this chapter, you'll be able to

  • Sign and verify a message with ECDSA on secp256k1
  • Explain the role of the nonce k and why it must be secret and unique
  • Recover a private key from two signatures that share the same nonce
  • Name the protections: deterministic nonces (RFC 6979) and Ed25519

ECDSA in four lines

Notation: G the base point, n the order of the group, d the private key (a number), Q = d·G the public key (a point), z the SHA-256 fingerprint of the message, seen as a number.

  • Sign: draw a secret nonce k, compute R = k·G, then r = R.x mod n and s = k⁻¹ (z + r·d) mod n. The signature is the pair (r, s).
  • Verify: compute w = s⁻¹, u1 = z·w, u2 = r·w, then the point X = u1·G + u2·Q. The signature is valid if X.x mod n == r.

With the public key Q alone, we get the point R back if and only if the signature is correct.

Step 5 of 5
Text description of the animation
  1. We sign the message's fingerprint, a 256-bit number.
  2. That is the weak point: k must stay secret and change with every signature.
  3. r does not reveal k: finding k from k·G is a discrete logarithm.
  4. s mixes the fingerprint, the private key d and the nonce k.
  5. With the public key Q alone, we get the point R back if and only if the signature is correct.
import hashlib

p = 2**256 - 2**32 - 977
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
     0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)

def add(P, Q):
    if P is None: return Q
    if Q is None: return P
    if P[0] == Q[0] and (P[1] + Q[1]) % p == 0: return None
    l = (3 * P[0] ** 2 * pow(2 * P[1], -1, p) if P == Q else (Q[1] - P[1]) * pow(Q[0] - P[0], -1, p)) % p
    x = (l * l - P[0] - Q[0]) % p
    return (x, (l * (P[0] - x) - P[1]) % p)

def mul(k, P):
    R = None
    while k:
        if k & 1: R = add(R, P)
        P, k = add(P, P), k >> 1
    return R

def z_of(message):
    return int.from_bytes(hashlib.sha256(message).digest(), "big")

def sign(d, message, k):
    r = mul(k, G)[0] % n
    s = pow(k, -1, n) * (z_of(message) + r * d) % n
    return r, s

def verify(Q, message, signature):
    r, s = signature
    if not (1 <= r < n and 1 <= s < n):
        return False
    w = pow(s, -1, n)
    X = add(mul(z_of(message) * w % n, G), mul(r * w % n, Q))
    return X is not None and X[0] % n == r

d = 0x2B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFE   # private key
Q = mul(d, G)                                                            # public key
sig = sign(d, b"Pay 12,000 F CFA to the cooperative", k=0x6C1A4D3B2E5F7081)  # for real: a random 256-bit k
print("valid signature   :", verify(Q, b"Pay 12,000 F CFA to the cooperative", sig))
print("modified message  :", verify(Q, b"Pay 92,000 F CFA to the cooperative", sig))
print("other public key  :", verify(mul(12345, G), b"Pay 12,000 F CFA to the cooperative", sig))
valid signature   : True
modified message  : False
other public key  : False

Why the nonce is so sensitive

Look at the equation for s: s·k = z + r·d (mod n). One equation, two unknowns (k and d): impossible to solve. But if two signatures use the same k, they have the same r and we get two equations with the same unknowns:

s1·k = z1 + r·d
s2·k = z2 + r·d
→ k = (z1 − z2) / (s1 − s2)    then    d = (s1·k − z1) / r     (mod n)

And the problem does not stop at exact reuse: if k is predictable (clock, counter) or even just biased (a few bits always zero), more advanced lattice attacks recover d from a few hundred signatures.

Mini-lab: one signature, one messagePython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: auditing a register of signatures

A public register contains ECDSA signatures from the same signer. Write a function find_reused_nonce(register) that spots two signatures with the same r, deduces the private key, and checks the result by recomputing the public key. Build the register with three normal signatures and two that share a nonce.

Hint 1
Store the signatures in a dictionary r → (message, s): as soon as an r is already present, you have your pair.
Hint 2
With the chapter's formulas, k = (z1 − z2)·(s1 − s2)⁻¹ then d = (s1·k − z1)·r⁻¹, all modulo n. Compare mul(d, G) with the published public key.
Show the solution and common mistakes
import hashlib

p = 2**256 - 2**32 - 977
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
     0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)

def add(P, Q):
    if P is None: return Q
    if Q is None: return P
    if P[0] == Q[0] and (P[1] + Q[1]) % p == 0: return None
    l = (3 * P[0] ** 2 * pow(2 * P[1], -1, p) if P == Q else (Q[1] - P[1]) * pow(Q[0] - P[0], -1, p)) % p
    x = (l * l - P[0] - Q[0]) % p
    return (x, (l * (P[0] - x) - P[1]) % p)

def mul(k, P):
    R = None
    while k:
        if k & 1: R = add(R, P)
        P, k = add(P, P), k >> 1
    return R

z = lambda m: int.from_bytes(hashlib.sha256(m).digest(), "big")
def sign(d, m, k):
    r = mul(k, G)[0] % n
    return r, pow(k, -1, n) * (z(m) + r * d) % n

secret_d = 0x1F4E2D3C5B6A79880716253443526170F8E9DAC1B2A3948576A5B4C3D2E1F0A9
public_key = mul(secret_d, G)
register = [(m, *sign(secret_d, m, k)) for m, k in [
    (b"payment 1", 0x51A3), (b"payment 2", 0x77B9), (b"payment 3", 0x51A3),
    (b"payment 4", 0x9E04), (b"payment 5", 0x13C8)]]

def find_reused_nonce(register, public_key):
    seen = {}
    for m, r, s in register:
        if r in seen:
            m1, s1 = seen[r]
            k = (z(m1) - z(m)) * pow(s1 - s, -1, n) % n
            d = (s1 * k - z(m1)) * pow(r, -1, n) % n
            return m1, m, mul(d, G) == public_key
        seen[r] = (m, s)
    return None

print(find_reused_nonce(register, public_key))
(b'payment 1', b'payment 3', True)

The dictionary spots the collision in a single pass, even over millions of signatures: that is exactly what researchers did on the Bitcoin blockchain, where thousands of public signatures shared an r.

Common mistakes:

  • Forgetting the % n: numbers grow and inverses are computed modulo the wrong number.
  • Using p (the curve's field) instead of n (the group's order) for computations on k, r, s and d.
  • Concluding without checking: recomputing d·G and comparing it with the public key confirms that the recovered key is the right one.

Summary

  1. 1ECDSA signs with s = k⁻¹(z + r·d) mod n; it verifies with u1·G + u2·Q.
  2. 2The nonce k must be secret, unique and unpredictable: reusing it hands over the private key.
  3. 3Even a merely biased or predictable k lets an attacker recover the key with enough signatures.
  4. 4Never pick k yourself: deterministic nonces (RFC 6979) or Ed25519.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.