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 reveals the private key.
The algorithm
Public: curve, base point of prime order , hash . Private key , public key .
Sign message :
- as an integer (truncated to the bit length of ).
- Pick a fresh secret , compute and (retry if ).
- (retry if ). The signature is .
Verify: compute , and ; accept iff .
Why it works: .
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 signs two different messages, both signatures share , and
This is how the Sony PlayStation 3 code-signing key was extracted in 2010 (Sony used a constant ), and how coins were stolen from Android Bitcoin wallets in 2013 after a random-number bug.
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 across many signatures are enough for lattice attacks (Lattices) to recover .
Deterministic nonces
RFC 6979 derives 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: . Both remove the most common real-world failure.
Malleability
If is valid, so is , because has the same -coordinate. Bitcoin enforces "low-" signatures () so a transaction's ID cannot be changed by flipping .
In software
- Python:
private_key.sign(data, ec.ECDSA(hashes.SHA256())); signatures are usually DER-encoded 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 for a valid signature.
Answer
.
Solution
, so . From the signing equation, , hence the sum is , whose -coordinate gives .
Exercise 2: Low-
On the toy curve (), a signature has . What is its low- form?
Answer
.
Solution
, so replace it with . Both verify; the low one is canonical.
Exercise 3: Malleability check (code)
Use the toy ECDSA code to check that verifies for the same message.
Answer
It verifies.
Solution
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 for two different messages?
- nothing
- the private key can be computed from the two signatures
- the signatures become invalid
- the curve changes
RFC 6979 makes ECDSA safer by…
- using larger curves
- deriving deterministically from the key and message
- removing the hash
- signing twice
Why is also a valid ECDSA signature?
- because is prime
- because and have the same -coordinate
- because hashes collide
- it is not valid