MASTERING-BITCOIN

The Elegant Math Behind Bitcoin's Most Compact Signature

IKey Formulas

Formula Name Description
s=k+eβ‹…xs = k + e \cdot x Response equation Combines nonce kk, challenge ee, and secret xx
sG=?kG+eβ‹…xGsG \stackrel{?}{=} kG + e \cdot xG Verification check Verifier confirms signature using only public values
e=H(kG βˆ₯ xG βˆ₯ m)e = H(kG \,\Vert\, xG \,\Vert\, m) BIP340 challenge hash Fiat-Shamir non-interactive challenge
x=s1βˆ’s2e1βˆ’e2x = \dfrac{s_1 - s_2}{e_1 - e_2} Nonce-reuse recovery If kk is reused, secret xx is exposed

IIVisualization 1: The Sigma Protocol Verification

The heart of Schnorr is the equation sG=kG+eβ‹…xGsG = kG + e \cdot xG. To make this concrete, we'll work with scalar coefficients of GG β€” Alice's public key is xGxG, her commitment is kGkG, and the verifier checks that the scalar coefficients balance.

Adjust the secret xx, nonce kk, and challenge ee below to see how the response s=k+eβ‹…xs = k + e \cdot x is constructed and verified.

βš™ interactive visualization β€” coming to this page format soon

Notice: Both bars always have the same length, regardless of how you set xx, kk, or ee. This is the algebraic identity at the heart of Schnorr β€” multiplying s=k+eβ‹…xs = k + e \cdot x by GG gives exactly kG+eβ‹…xGkG + e \cdot xG. The verifier checks this equation using only public values (kGkG, ee, xGxG), never seeing the secret xx or the nonce kk.

IIIVisualization 2: The Nonce-Reuse Attack

The nonce kk is single-use. If Alice ever signs two different messages with the same kk, the secret xx is no longer secret β€” anyone can recover it with simple algebra.

Below, set Alice's secret xx, the reused nonce kk, and two different challenges e1,e2e_1, e_2. The plot shows how the attacker recovers xx from the two leaked signatures.

βš™ interactive visualization β€” coming to this page format soon

Reflect: Real Bitcoin wallets have been emptied this way β€” most famously, the Sony PlayStation 3 used a constant nonce in its ECDSA signing routine. BIP340 mandates deterministic nonce generation: k=H(privateΒ key βˆ₯ m)k = H(\text{private key} \,\Vert\, m). This way the nonce is unpredictable to outsiders, but the same key + same message produces the same nonce β€” so two different messages produce two different nonces, and the attack is impossible.

IVVisualization 3: BIP340 Signature Layout

A BIP340 Schnorr signature is just two scalars worth of bytes. Compare its compactness to the older ECDSA format used in Bitcoin before Taproot.

βš™ interactive visualization β€” coming to this page format soon

Notice: BIP340 fixes the y-coordinate of kGkG to be even, so only the x-coordinate needs to be transmitted. ECDSA must DER-encode two scalars of variable length (because leading zero bytes can shift), bloating the format. Multiply that ~7-byte savings by every input on every transaction in every block, and the scaling impact is real.

VQuiz Questions

Question 1

Alice's secret is x=4x = 4. She picks nonce k=9k = 9, and the challenge comes out to e=5e = 5. What is her response ss?

βœ… Correct! s=9+5β‹…4=29s = 9 + 5 \cdot 4 = 29.

❌ Not quite. That's k+e=9+5k + e = 9 + 5. The formula is s=k+eβ‹…xs = k + e \cdot x β€” you need to multiply ee by xx first.

❌ Not quite. That's (k+e)β‹…x=14β‹…4(k+e) \cdot x = 14 \cdot 4 (wrong order of operations). Compute eβ‹…xe \cdot x first, then add kk.

❌ Not quite. Apply s=k+eβ‹…xs = k + e \cdot x carefully.

Show solution

Solution:

The Schnorr response equation is: s=k+eβ‹…xs = k + e \cdot x

Substituting: s=9+5β‹…4=9+20=29s = 9 + 5 \cdot 4 = 9 + 20 = 29

Verification check (in scalar coefficients of GG): sG=29G=?kG+eβ‹…xG=9G+5β‹…4G=9G+20G=29GsG = 29G \stackrel{?}{=} kG + e \cdot xG = 9G + 5 \cdot 4G = 9G + 20G = 29G

The signature (kG,s)=(9G,29)(kG, s) = (9G, 29) verifies correctly.

Question 2

True or False: If Alice signs two different messages using the same nonce kk, the resulting signatures still keep her private key xx secret, because xx never appears in the signature directly.

βœ… Correct! Reusing kk across two messages exposes xx via x=(s1βˆ’s2)/(e1βˆ’e2)x = (s_1 - s_2)/(e_1 - e_2).

❌ Not quite. Although xx is masked by kk in a single signature, two signatures sharing the same kk let an attacker subtract them to cancel kk and solve for xx.

❌ Not quite. Try again β€” the hints above can help.

Show solution

Solution: False.

Reusing the nonce is catastrophic. Suppose Alice produces two signatures with the same kk:

s1=k+e1β‹…xs2=k+e2β‹…xs_1 = k + e_1 \cdot x \qquad s_2 = k + e_2 \cdot x

Subtracting eliminates kk entirely:

s1βˆ’s2=(e1βˆ’e2)β‹…xs_1 - s_2 = (e_1 - e_2) \cdot x

So anyone who sees both signatures can recover the secret directly:

x=s1βˆ’s2e1βˆ’e2x = \frac{s_1 - s_2}{e_1 - e_2}

This is exactly why BIP340 mandates deterministic nonce generation, k=H(privateΒ key βˆ₯ m)k = H(\text{private key} \,\Vert\, m), which guarantees a fresh kk for every distinct message.

Question 3

BIP340 uses the challenge hash e=H(kG βˆ₯ xG βˆ₯ m)e = H(kG \,\Vert\, xG \,\Vert\, m), which includes the public key xGxG as an input. What attack does including xGxG in the hash specifically prevent?

βœ… Correct! Including xGxG in the hash makes the parent's challenge differ from any related child's, so the algebraic shift used in the forgery cannot succeed.

❌ Not quite. The specific role of xGxG in the hash is to defeat forgeries that exploit related-key algebra (e.g., BIP32 child keys xG+tGxG + tG).

Show solution

Solution: Key-shift forgeries.

Without xGxG in the hash, the challenge is e=H(kG βˆ₯ m)e = H(kG \,\Vert\, m) β€” independent of which public key the verifier checks against. An attacker who knows a publicly derivable offset tt (as in BIP32 unhardened derivation, where the child key is xG+tGxG + tG) can take Alice's signature (kG,s)(kG, s) on the parent key and forge:

sβ€²=s+eβ‹…ts' = s + e \cdot t

Then sβ€²G=sG+eβ‹…tG=kG+eβ‹…xG+eβ‹…tG=kG+eβ‹…(xG+tG)s'G = sG + e \cdot tG = kG + e \cdot xG + e \cdot tG = kG + e \cdot (xG + tG), which verifies under the child key for free.

Putting xGxG inside the hash makes eparent≠echilde_{\text{parent}} \neq e_{\text{child}}, so the algebraic shift no longer lines up and the forgery dies.

The other options are unrelated:

  • Discrete log is prevented by the curve choice, not the hash inputs.
  • Message binding (preventing replay on different messages) comes from including mm in the hash.
  • Nonce brute-force is prevented by kk's 256-bit entropy.

Question 4

In Schnorr's identity protocol, why must Alice send the commitment kGkG before seeing Bob's challenge ee?

βœ… Correct! Without committing first, an attacker can pick ss freely and back-compute kG=sGβˆ’eβ‹…xGkG = sG - e \cdot xG, producing a signature that verifies without knowing xx.

❌ Not quite. The ordering is load-bearing for security, not for performance or convenience. Think about what an attacker could do if they saw ee before committing to kGkG.

Show solution

Solution: Reversing the order enables a trivial forgery.

If a (cheating) prover sees ee first, they can:

  1. Pick any response ss they like (e.g., s=7s = 7).
  2. Take ee and the victim's public key xGxG.
  3. Compute kG:=sGβˆ’eβ‹…xGkG := sG - e \cdot xG.
  4. Output the pair (kG,s)(kG, s).

Plug into the verifier: sG=?kG+eβ‹…xG=(sGβˆ’eβ‹…xG)+eβ‹…xG=sGβœ“sG \stackrel{?}{=} kG + e \cdot xG = (sG - e \cdot xG) + e \cdot xG = sG \quad \checkmark

The signature verifies β€” yet the attacker never knew xx.

Committing to kGkG first locks in a specific kk before ee is known, so s=k+eβ‹…xs = k + e \cdot x must be computed honestly using the secret xx. This commit-challenge-respond ordering is the heart of every sigma protocol, and it's exactly what the Fiat-Shamir transform must preserve (by hashing kGkG together with mm to derive ee).

Solved: 0 / 4