1732 Words

✏️ Digital signatures: exercises and in depth

Overview · Belongs to 1. The mathematics, section 6 · Previous: ← Elliptic curves · Back to 1. The mathematics

Everything here uses part 1’s small curve $y^2 = x^3 + 2x + 2 \pmod{17}$ with $G = (5, 1)$ and group order $n = 19$. All “signature numbers” ($k$, $s$, $e$, $x$) count modulo 19; point coordinates count modulo 17. The table of multiples $kG$ is in part 1; Elliptic curves in depth explains it. Answers are hidden under Answer.

$k$12345678910
$kG$(5,1)(6,3)(10,6)(3,1)(9,16)(16,13)(0,6)(13,7)(7,6)(7,11)
$k$111213141516171819
$kG$(13,10)(0,11)(16,4)(9,1)(3,16)(10,11)(6,14)(5,16)$\mathcal{O}$

1. Where Schnorr comes from: proving you know a secret

Before signatures, Schnorr (1989) designed an identification protocol: Peggy proves to Victor that she knows $x$ with $P = xG$, without revealing $x$.

  1. Commit. Peggy picks a random $k$ and sends $R = kG$.
  2. Challenge. Victor picks a random $e$ and sends it.
  3. Response. Peggy sends $s = k + e x \bmod n$.
  4. Check. Victor checks $sG = R + eP$.

Why can’t a cheater fake it? If Peggy could answer two different challenges $e_1, e_2$ for the same $R$, then $s_1 - s_2 = (e_1 - e_2)x$, so she could calculate $x$. Somebody who does not know $x$ can therefore answer at most one challenge: with a random 256-bit $e$, a chance of $2^{-256}$.

Why doesn’t it leak $x$? $s = k + ex$ with a fresh random $k$ is just a random-looking number; $k$ works like a one-time pad over $ex$.

Exercise 1.1. Show that if Peggy knew the challenge $e$ in advance, she could pass without knowing $x$. Do it with $P = (0, 6)$, $e = 3$, and the response $s = 5$ that she picks herself. Which $R$ must she send?

Answer

She needs $sG = R + eP$, so $R = sG - eP = 5G - 3 \cdot 7G = 5G - 21G = -16G = 3G = $ (10, 6). Victor checks $5G = (9, 16)$ and $R + 3P = 3G + 21G = 24G = 5G = (9, 16)$ ✓. She passed, without ever using $x$. So the order matters: $R$ must be fixed before the challenge is known.

Fiat–Shamir: from conversation to signature

A signature cannot wait for a Victor. The Fiat–Shamir trick (1986) replaces his random challenge by a hash of the commitment and the message:

$$e = H(R, P, m)$$

Because $R$ goes into the hash, Peggy cannot pick $e$ first and $R$ afterwards (exercise 1.1); she would have to find an $R$ that hashes to her chosen $e$, which a good hash prevents. And because $m$ goes into the hash, the signature is tied to this one message.


2. Schnorr signatures: exercises

Sign $m$ with private key $x$: pick $k$, $R = kG$, $e = H(R, P, m)$, $s = k + ex \bmod n$. Signature: $(R, s)$.

Verify: $sG \overset{?}{=} R + eP$.

(We cannot run a real hash on these tiny numbers, so each exercise just gives you $e$.)

Exercise 2.1. Private key $x = 4$, nonce $k = 6$, hash $e = 11$. Calculate $P$, $R$ and $s$, and verify.

Answer

$P = 4G = (3, 1)$, $R = 6G = (16, 13)$, $s = 6 + 11 \cdot 4 = 50 \equiv$ 12.

Verify: $sG = 12G = (0, 11)$ and $R + eP = 6G + 44G = 50G = 12G = (0, 11)$ ✓

Exercise 2.2. $x = 3$, $k = 8$, $e = 2$. Calculate the signature. Then a forger changes the message, so the hash becomes $e = 3$, but keeps $(R, s)$. Does it still verify?

Answer

$P = 3G = (10, 6)$, $R = 8G = (13, 7)$, $s = 8 + 2 \cdot 3 = $ 14, and $14G = (9, 1) = R + 2P = 8G + 6G$ ✓.

With $e = 3$: $R + 3P = 8G + 9G = 17G = (6, 14) \ne (9, 1)$: rejected.


3. Attack: the reused nonce (Schnorr)

Exercise 3.1. You see two signatures from the same public key $P = (0, 6)$, both with the same $R = (7, 11)$:

  • message 1: $e_1 = 5$, $s_1 = 7$
  • message 2: $e_2 = 12$, $s_2 = 18$

Find the private key $x$, and the nonce $k$.

Answer

Same $R$ means same $k$. Subtract the equations $s_1 = k + e_1 x$ and $s_2 = k + e_2 x$:

$$x = \frac{s_1 - s_2}{e_1 - e_2} = \frac{7 - 18}{5 - 12} = \frac{-11}{-7} \equiv \frac{8}{12} \pmod{19}$$

$12^{-1} \equiv 8$ (since $96 = 5 \cdot 19 + 1$), so $x \equiv 8 \cdot 8 = 64 \equiv$ 7. Check: $7G = (0, 6) = P$ ✓.

Then $k = s_1 - e_1 x = 7 - 35 = -28 \equiv$ 10, and $10G = (7, 11) = R$ ✓.

Two signatures, one subtraction, one division: the key is gone. This is exactly the attack from the “Check” step of section 1, carried out by an outsider.


4. ECDSA: exercises

ECDSA (1992, standardised for NIST curves) avoids Schnorr’s patent, which expired in 2008, at the cost of a messier formula.

Sign hash $h$ with private key $x$:

  1. Pick $k$, calculate $R = kG$, and let $r = x_R \bmod n$ (the x-coordinate of $R$).
  2. $s = k^{-1}(h + r x) \bmod n$. Signature: $(r, s)$.

Verify with $P$:

  1. $w = s^{-1}$, $u_1 = h w$, $u_2 = r w$ (all modulo $n$).
  2. Calculate $V = u_1 G + u_2 P$ and accept if $x_V \bmod n = r$.

Why it works: $u_1 G + u_2 P = (h w + r w x)G = w(h + rx)G = \dfrac{h + rx}{s}G = kG = R$.

Exercise 4.1. $x = 7$ (so $P = (0, 6)$), $k = 10$, $h = 5$. Sign, then verify.

Answer

Sign: $R = 10G = (7, 11)$, $r = 7$. $k^{-1} = 10^{-1} \bmod 19 = 2$ ($20 \equiv 1$). $h + rx = 5 + 49 = 54 \equiv 16$. $s = 2 \cdot 16 = 32 \equiv$ 13. Signature $(7, 13)$.

Verify: $w = 13^{-1} \bmod 19 = 3$ ($39 = 2 \cdot 19 + 1$). $u_1 = 5 \cdot 3 = 15$, $u_2 = 7 \cdot 3 = 21 \equiv 2$.

$V = 15G + 2P = 15G + 14G = 29G = 10G = (7, 11)$. $x_V = 7 = r$ ✓

Exercise 4.2 (the PlayStation 3 attack). The same key signs two hashes with the same nonce: $h_1 = 5$ gives $(r, s_1) = (7, 13)$, and $h_2 = 3$ gives $(r, s_2) = (7, 9)$. Recover $k$ and then $x$.

Answer

From $s_i k = h_i + rx$, subtract: $(s_1 - s_2)k = h_1 - h_2$, so

$$k = \frac{h_1 - h_2}{s_1 - s_2} = \frac{2}{4} \equiv 2 \cdot 5 = 10 \pmod{19} \qquad (4^{-1} \equiv 5)$$

Then $x = \dfrac{s_1 k - h_1}{r} = \dfrac{130 - 5}{7} \equiv \dfrac{11}{7} \equiv 11 \cdot 11 = 121 \equiv$ 7 $\pmod{19}$ (because $7^{-1} \equiv 11$).

The same $r$ in two signatures is the giveaway: anybody can spot it. Sony used a fixed $k$ for every PS3 firmware signature, and in 2010 the fail0verflow group computed the master signing key this way. In 2013, Android Bitcoin wallets leaked keys because a broken random generator sometimes produced the same $k$ twice.

Even a slightly biased nonce is fatal. If only a few bits of every $k$ are predictable, many signatures together give a lattice problem that reveals $x$. In 2019 the “Minerva” and “TPM-Fail” attacks recovered keys from smart cards and TPM chips this way, using nothing but the time each signature took.


5. Ed25519: no random nonce at all

Ed25519 (Bernstein et al., 2011) is Schnorr on the Edwards form of Curve25519, with one important change: the nonce is a hash, not a random number.

  1. The private key is a 32-byte seed. $\text{SHA-512}(\text{seed})$ gives 64 bytes: the first half (clamped) is the secret scalar $a$, the second half is a secret prefix.
  2. Public key: $A = aB$ (B is the base point).
  3. Nonce: $r = \text{SHA-512}(\text{prefix}, m) \bmod \ell$. Then $R = rB$.
  4. $S = r + \text{SHA-512}(R, A, m) \cdot a \bmod \ell$. Signature: $(R, S)$, 64 bytes.

Same message → same $r$ → the same signature (harmless, it is the same message). Different message → a different, unpredictable $r$. Reusing a nonce for two different messages is impossible without breaking SHA-512, and no random generator is needed at signing time.

Exercise 5.1. Why does it not help an attacker that $r$ is “predictable” in the sense that the same message always gives the same $r$?

Answer

To predict $r$ you need the secret prefix, which only the key owner has. Seeing the same signature twice for the same message gives no new equation (exercise 3.1 needs two different $e$ with the same $k$). An attacker who cannot compute $r$ is in the same position as with a perfect random $k$.


6. Overview

RSA (PSS)ECDSASchnorr / Ed25519
sign$s = \text{pad}(h)^d \bmod n$$s = k^{-1}(h + rx)$$s = k + ex$
verify$s^e \overset{?}{=} \text{pad}(h)$$x(u_1G + u_2P) \overset{?}{=} r$$sG \overset{?}{=} R + eP$
noncerandom salt (harmless if bad)random $k$, fatal if reused or biasedEd25519: derived by hashing
signature size (128-bit security)384 bytes64 bytes64 bytes
used inTLS certificates, old SSH keysTLS (P-256), passkeys, BitcoinSSH ed25519 keys, Signal, Tor, Bitcoin Taproot

Check yourself in Python

Using add and mul from Elliptic curves in depth:

G, n = (5, 1), 19
x, k, e = 4, 6, 11
P, R = mul(x, G), mul(k, G)
s = (k + e * x) % n                                   # 12
mul(s, G) == add(R, mul(e, P))                         # True

# ECDSA
h, x, k = 5, 7, 10
r = mul(k, G)[0] % n                                   # 7
s = pow(k, -1, n) * (h + r * x) % n                   # 13
w = pow(s, -1, n)
add(mul(h * w % n, G), mul(r * w % n, mul(x, G)))[0] % n == r   # True

Back to 1. The mathematics · On to 2. SSH →