TExcelTAD
Home › Number Theory & Cryptography › ECDSA Signatures

ECDSA Signatures

ECDSA, the Elliptic Curve Digital Signature Algorithm, signs Bitcoin and Ethereum transactions, TLS certificates and passkeys. It is the elliptic-curve version of DSA, an ElGamal-style scheme, and it inherits ElGamal's fragility: reuse or bias in the per-signature random number kk reveals the private key.

The algorithm

Public: curve, base point GG of prime order nn, hash HH. Private key dd, public key Q=dGQ = dG.

Sign message mm:

  1. z=H(m)z = H(m) as an integer (truncated to the bit length of nn).
  2. Pick a fresh secret k∈[1,n−1]k \in [1, n - 1], compute R=kGR = kG and r=xR mod nr = x_R \bmod n (retry if r=0r = 0).
  3. s=k−1(z+rd) mod ns = k^{-1}(z + rd) \bmod n (retry if s=0s = 0). The signature is (r,s)(r, s).

Verify: compute u1=zs−1u_1 = zs^{-1}, u2=rs−1 mod nu_2 = rs^{-1} \bmod n and X=u1G+u2QX = u_1 G + u_2 Q; accept iff xX mod n=rx_X \bmod n = r.

Why it works: u1G+u2Q=s−1(z+rd)G=s−1⋅sk G=kG=Ru_1 G + u_2 Q = s^{-1}(z + rd)G = s^{-1} \cdot sk\, G = kG = R.

python · try it yourself
import hashlib

p, a_coef, n, G = 10007, -3, 10193, (2, 3)        # toy curve with prime order n

def add(P, Q):
    if P is None: return Q
    if Q is None: return P
    (x1, y1), (x2, y2) = P, Q
    if x1 == x2 and (y1 + y2) % p == 0: return None
    lam = ((3 * x1 * x1 + a_coef) * pow(2 * y1, -1, p) if P == Q else (y2 - y1) * pow(x2 - x1, -1, p)) % p
    x3 = (lam * lam - x1 - x2) % p
    return (x3, (lam * (x1 - x3) - y1) % p)

def mul(k, P):
    R = None
    for bit in bin(k)[2:]:
        R = add(R, R)
        if bit == "1": R = add(R, P)
    return R

def H(msg):                                       # hash reduced to the toy group size
    return int.from_bytes(hashlib.sha256(msg).digest(), "big") % n

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

def verify(msg, sig, Q):
    r, s = sig
    w = pow(s, -1, n)
    X = add(mul(H(msg) * w % n, G), mul(r * w % n, Q))
    return X is not None and X[0] % n == r

d = 4321; Q = mul(d, G)
sig = sign(b"send 1 coin to Bob", d, k=2468)
print("signature:", sig)
print(verify(b"send 1 coin to Bob", sig, Q), verify(b"send 9 coins to Bob", sig, Q))
signature: (8400, 3473)
True False

Nonce reuse leaks the key

If the same kk signs two different messages, both signatures share rr, and

s1−s2=k−1(z1−z2)  ⟹  k=z1−z2s1−s2,d=s1k−z1r(modn)s_1 - s_2 = k^{-1}(z_1 - z_2) \implies k = \frac{z_1 - z_2}{s_1 - s_2}, \qquad d = \frac{s_1 k - z_1}{r} \pmod{n}

This is how the Sony PlayStation 3 code-signing key was extracted in 2010 (Sony used a constant kk), and how coins were stolen from Android Bitcoin wallets in 2013 after a random-number bug.

python · try it yourself
import hashlib

p, a_coef, n, G = 10007, -3, 10193, (2, 3)
def add(P, Q):
    if P is None: return Q
    if Q is None: return P
    (x1, y1), (x2, y2) = P, Q
    if x1 == x2 and (y1 + y2) % p == 0: return None
    lam = ((3 * x1 * x1 + a_coef) * pow(2 * y1, -1, p) if P == Q else (y2 - y1) * pow(x2 - x1, -1, p)) % p
    x3 = (lam * lam - x1 - x2) % p
    return (x3, (lam * (x1 - x3) - y1) % p)
def mul(k, P):
    R = None
    for bit in bin(k)[2:]:
        R = add(R, R)
        if bit == "1": R = add(R, P)
    return R
H = lambda m: int.from_bytes(hashlib.sha256(m).digest(), "big") % n
def sign(msg, d, k):
    r = mul(k, G)[0] % n
    return r, pow(k, -1, n) * (H(msg) + r * d) % n

d_secret = 4321
m1, m2 = b"firmware v1.0", b"firmware v1.1"
(r1, s1), (r2, s2) = sign(m1, d_secret, 999), sign(m2, d_secret, 999)   # same k!
print("same r:", r1 == r2)
k = (H(m1) - H(m2)) * pow(s1 - s2, -1, n) % n
d = (s1 * k - H(m1)) * pow(r1, -1, n) % n
print("recovered k:", k, " recovered private key:", d)
same r: True
recovered k: 999  recovered private key: 4321

Even a few biased bits of kk across many signatures are enough for lattice attacks (Lattices) to recover dd.

Deterministic nonces

RFC 6979 derives kk from the private key and the message hash with HMAC, so it is unique per message and never depends on a random-number generator. EdDSA (Ed25519) does the same by design: k=H(prefix∥m)k = H(\text{prefix} \parallel m). Both remove the most common real-world failure.

Malleability

If (r,s)(r, s) is valid, so is (r,n−s)(r, n - s), because −R-R has the same xx-coordinate. Bitcoin enforces "low-ss" signatures (s≤n/2s \le n/2) so a transaction's ID cannot be changed by flipping ss.

In software

  • Python: private_key.sign(data, ec.ECDSA(hashes.SHA256())); signatures are usually DER-encoded (r,s)(r, s) pairs.
  • WebAuthn passkeys default to ECDSA on P-256 (COSE algorithm -7); Ethereum uses secp256k1 ECDSA with a recovery id so the public key can be recovered from the signature.

Exercises

Exercise 1: Verification equation

Show that u1G+u2Q=Ru_1 G + u_2 Q = R for a valid signature.

Answer

u1G+u2Q=s−1(z+rd)G=kGu_1 G + u_2 Q = s^{-1}(z + rd)G = kG.

Solution

Q=dGQ = dG, so u1G+u2Q=(u1+u2d)G=s−1(z+rd)Gu_1 G + u_2 Q = (u_1 + u_2 d)G = s^{-1}(z + rd)G. From the signing equation, s−1(z+rd)≡k(modn)s^{-1}(z + rd) \equiv k \pmod n, hence the sum is kG=RkG = R, whose xx-coordinate gives rr.

Exercise 2: Low-ss

On the toy curve (n=10193n = 10193), a signature has s=9000s = 9000. What is its low-ss form?

Answer

s′=1193s' = 1193.

Solution

9000>n/2=5096.59000 > n/2 = 5096.5, so replace it with n−s=10193−9000=1193n - s = 10193 - 9000 = 1193. Both verify; the low one is canonical.

Exercise 3: Malleability check (code)

Use the toy ECDSA code to check that (r,n−s)(r, n - s) verifies for the same message.

Answer

It verifies.

Solution
python · try it yourself
import hashlib
p, a_coef, n, G = 10007, -3, 10193, (2, 3)
def add(P, Q):
    if P is None: return Q
    if Q is None: return P
    (x1, y1), (x2, y2) = P, Q
    if x1 == x2 and (y1 + y2) % p == 0: return None
    lam = ((3 * x1 * x1 + a_coef) * pow(2 * y1, -1, p) if P == Q else (y2 - y1) * pow(x2 - x1, -1, p)) % p
    x3 = (lam * lam - x1 - x2) % p
    return (x3, (lam * (x1 - x3) - y1) % p)
def mul(k, P):
    R = None
    for bit in bin(k)[2:]:
        R = add(R, R)
        if bit == "1": R = add(R, P)
    return R
H = lambda m: int.from_bytes(hashlib.sha256(m).digest(), "big") % n
d = 4321; Q = mul(d, G)
r = mul(2468, G)[0] % n
s = pow(2468, -1, n) * (H(b"hi") + r * d) % n
def verify(sig):
    r, s = sig; w = pow(s, -1, n)
    X = add(mul(H(b"hi") * w % n, G), mul(r * w % n, Q))
    return X is not None and X[0] % n == r
print(verify((r, s)), verify((r, n - s)))
True True

Go further: Elliptic Curves, ECDSA and the Nonce-Reuse Failure recovers a private key from two signatures with the same nonce.

Test yourself

What happens if ECDSA uses the same nonce kk for two different messages?

  1. nothing
  2. the private key can be computed from the two signatures
  3. the signatures become invalid
  4. the curve changes

RFC 6979 makes ECDSA safer by…

  1. using larger curves
  2. deriving kk deterministically from the key and message
  3. removing the hash
  4. signing twice

Why is (r,n−s)(r, n - s) also a valid ECDSA signature?

  1. because nn is prime
  2. because RR and −R-R have the same xx-coordinate
  3. because hashes collide
  4. it is not valid