Path: blob/main/foundations/06-elliptic-curves/sage/06f-ecdh-and-ecdsa.ipynb
483 views
Notebook 06f: ECDH and ECDSA
Module 06. Elliptic Curves
Motivating Question. Diffie-Hellman key exchange and digital signatures were invented for multiplicative groups (Module 05). Can we transplant these protocols onto elliptic curves, and if so, why bother? The answer: elliptic curve groups resist index-calculus attacks, so the same security level needs dramatically shorter keys (256-bit EC 3072-bit RSA). In this notebook, we implement ECDH (key exchange) and ECDSA (signatures) from scratch.
Prerequisites. You should be comfortable with:
Scalar multiplication and double-and-add (Notebook 06e)
The ECDLP: given and , finding is hard (06e)
Diffie-Hellman key exchange in (Module 05)
Learning objectives. By the end of this notebook you will be able to:
Implement ECDH key exchange and verify that both parties derive the same shared secret.
Implement ECDSA signing and verification from scratch.
Understand why the ECDSA verification equation works.
Demonstrate the catastrophic nonce-reuse attack on ECDSA.
Identify standard curves used in practice (secp256k1, P-256, Curve25519).
1. From Diffie-Hellman to ECDH
Bridge from Module 05. In Module 05, Alice and Bob performed Diffie-Hellman in : they shared a generator , each picked a secret exponent, and computed as their shared secret. ECDH is the same protocol with a different group: instead of exponentiation , we use scalar multiplication .
Translation table:
| Classical DH | ECDH |
|---|---|
| Public prime , generator | Public curve , base point of order |
| Alice picks secret , publishes | Alice picks secret , publishes |
| Bob picks secret , publishes | Bob picks secret , publishes |
| Shared secret: | Shared secret: |
| Security: DLP in | Security: ECDLP on |
Setting up a curve
We need:
A prime (the field )
Curve parameters (defining )
A base point of prime order
For a teaching example, we use a small curve. In practice, would be .
ECDH Protocol
Step 1. Alice and Bob agree on public parameters: .
Step 2. Key generation:
Alice picks a random secret and publishes .
Bob picks a random secret and publishes .
Step 3. Shared secret:
Alice computes .
Bob computes .
Both arrive at the same point . An eavesdropper sees but needs to solve the ECDLP to find or .
Checkpoint 1. Why does ? Because EC scalar multiplication distributes: . This is just commutativity of integer multiplication, combined with the group law. The same trick that made classical DH work!
2. Digital Signatures: Motivation
Key exchange lets Alice and Bob establish a shared secret. But how does Bob know the message he received was truly from Alice? He needs a digital signature.
A digital signature scheme has three algorithms:
KeyGen: Generate a private/public key pair where .
Sign: Given private key and message , produce a signature .
Verify: Given public key , message , and signature , output "accept" or "reject."
The Elliptic Curve Digital Signature Algorithm (ECDSA) is the most widely deployed EC signature scheme. It is used in Bitcoin, Ethereum, TLS, and many other systems.
3. ECDSA Key Generation
Key generation is identical to the first step of ECDH:
Choose a random private key .
Compute the public key .
The private key is kept secret; the public key is published.
4. ECDSA Signing
To sign a message with private key :
Hash the message: . (We simulate this with a simple hash.)
Pick a random nonce . (This MUST be random and secret!)
Compute the nonce point and set . If , pick a new .
Compute . If , pick a new .
The signature is .
Intuitively: the nonce "randomizes" the signature, the private key binds it to the signer, and the hash binds it to the message.
Let's trace through the signing step by step so nothing is hidden:
Checkpoint 2. The nonce must be:
Random: predictable leaks the private key.
Unique per signature: reusing for two different messages leaks the private key.
Secret: if an attacker learns , they can compute .
We will demonstrate the nonce-reuse attack later in this notebook.
5. ECDSA Verification
Given a message , signature , and public key , the verifier checks:
Check .
Compute .
Compute .
Compute and .
Compute .
Accept if ; reject otherwise.
Note that the verifier never needs the private key or the nonce !
6. Why Verification Works
Let us prove that a correctly generated signature always passes verification.
Signing produced: , where and .
Verification computes:
Proof. Since , we have .
Therefore:
So . The verification succeeds.
Misconception alert. "ECDSA signatures are deterministic." No, standard ECDSA uses a random nonce each time, so signing the same message twice produces different signatures (both valid). RFC 6979 defines a deterministic variant that derives from the message and private key, which avoids the catastrophic nonce-reuse vulnerability. But the basic protocol is randomized.
7. The Nonce-Reuse Catastrophe
This is one of the most famous real-world crypto failures. In 2010, the Sony PlayStation 3 was hacked because Sony used the same nonce for every ECDSA signature on game updates. This allowed hackers to recover Sony's private signing key.
The attack: Suppose Alice signs two different messages using the same nonce :
Note that is the same in both (since is the same). Subtracting:
Therefore:
And once is known, the private key is:
Checkpoint 3. The attack requires only two signatures with the same value (which reveals the reused nonce). In practice, this was exploited against:
PlayStation 3 (2010): Sony used for every signature.
Android Bitcoin wallets (2013): A buggy random number generator repeated nonces.
Various smart contracts (ongoing): Poor entropy in nonce generation.
Lesson: never, ever reuse a nonce in ECDSA. Or better, use deterministic nonces (RFC 6979).
8. Standard Curves
In practice, you don't choose your own curve parameters, you use a standardized curve. Here are the most important ones:
| Curve | Field size | Use | Security level |
|---|---|---|---|
| secp256k1 | 256-bit prime | Bitcoin, Ethereum | 128-bit |
| P-256 (secp256r1) | 256-bit prime | TLS, government, smart cards | 128-bit |
| Curve25519 | 255-bit prime () | X25519 key exchange (TLS 1.3, Signal) | 128-bit |
| Ed25519 | 255-bit prime () | EdDSA signatures (SSH, Signal) | 128-bit |
| P-384 | 384-bit prime | High-security TLS, government | 192-bit |
All of these provide -bit security with only -bit keys. Compare:
| Security level | EC key size | RSA key size | DH key size |
|---|---|---|---|
| 128-bit | 256 bits | 3072 bits | 3072 bits |
| 192-bit | 384 bits | 7680 bits | 7680 bits |
| 256-bit | 512 bits | 15360 bits | 15360 bits |
This is why EC crypto has largely replaced RSA and classical DH for new protocols.
Crypto foreshadowing. Elliptic curves are the foundation for much of frontier cryptography:
Pairings (Module 07): bilinear maps on special EC groups enable identity-based encryption, BLS signatures, and SNARKs.
Commitments (Module 09): Pedersen commitments on elliptic curves are additively homomorphic.
SNARKs/STARKs (Module 10): elliptic curve pairings power the trusted setup in Groth16 and the KZG polynomial commitment scheme.
However, elliptic curve crypto is not quantum-safe, Shor's algorithm breaks ECDLP. Module 08 (Lattices) covers post-quantum alternatives.
9. Exercises
Exercise 1 (Worked): ECDH with a Twist
Problem. On over , Alice picks and Bob picks . Using the generator from SageMath, compute the shared secret step by step.
Solution:
Exercise 2 (Guided): ECDSA Round Trip
Problem. Using the curve over where is the next prime after :
Generate an ECDSA key pair.
Sign the message
"Crypto is beautiful".Verify the signature.
Modify the message slightly and show that verification fails.
Fill in the TODOs:
Exercise 3 (Independent): Nonce-Reuse Key Recovery
Problem.
On a curve of your choice, generate an ECDSA key pair.
Sign two different messages using the same nonce . (You'll need to modify the signing code to accept a fixed .)
From the two signatures and and the two message hashes , recover the nonce and then the private key .
Verify that your recovered private key is correct by signing a third message and checking that it verifies against the original public key.
Bonus: If an attacker observes 100 signatures, but only two share the same value, can they still break the scheme? How would they detect the reuse?
Summary
| Concept | Key Fact |
|---|---|
| ECDH | Key exchange: shared secret , where are private scalars |
| ECDSA KeyGen | Private key , public key |
| ECDSA Sign | Nonce , compute , |
| ECDSA Verify | Check where , |
| Nonce reuse | Reusing in two signatures reveals the private key, catastrophic! |
| Standard curves | secp256k1 (Bitcoin), P-256 (TLS), Curve25519 (modern protocols) |
| Key advantage | 256-bit EC key 3072-bit RSA key in security |
This completes Module 06. You now understand elliptic curve cryptography from the ground up: the geometry of curves, the group law, scalar multiplication, and the two fundamental protocols (ECDH and ECDSA). In the Break section, you will exploit ECDSA nonce reuse, invalid curves, and small subgroups. In Connect, you will see how ECDH and ECDSA appear in TLS, Bitcoin, and SSH.
Next module: Module 07: Pairings