Security

Elliptic Curve Cryptography Explained, With a Toy Curve Example

Elliptic curve cryptography (ECC) is public-key cryptography built on arithmetic with points on a curve. It gives you key pairs, key agreement (ECDH) and digital signatures (ECDSA, EdDSA) with much shorter keys than RSA, for example 32-byte keys for X25519 against a 256-byte RSA-2048 modulus.

If you have used an SSH key of type ed25519, loaded an HTTPS page over TLS 1.3, or signed in with a passkey, you have almost certainly used it. This article shows the idea on a curve small enough to check with pencil and paper, then maps it onto the real curves in standards.

How elliptic curve cryptography works

An elliptic curve here is the set of points (x, y) that satisfy an equation such as y^2 = x^3 + a*x + b, with all arithmetic done modulo a prime p. That makes the "curve" a scattered grid of whole-number points rather than a smooth line, but the algebra works the same way.

The curve comes with an operation called point addition: take two points and a fixed rule gives you a third point on the curve. Adding a point to itself is called doubling. Repeating this, you can compute k × G for a starting point G (the generator) and a whole number k, which means "add G to itself k times".

  • Private key: a number k.
  • Public key: the point k × G.

Going from k to k × G is fast, because doubling and adding lets a computer do it in about as many steps as k has bits. Going back, finding k when you only know G and k × G, is the elliptic curve discrete logarithm problem. For a well-chosen curve with a large prime, no practical classical method is known. That one-way gap is what lets you publish the public key.

A toy curve you can check by hand

This is a toy. The numbers are tiny so the arithmetic fits on paper, and anyone can break them instantly. Real curves use primes around 2255 or larger.

Use the curve y^2 = x^3 + 2x + 2 (mod 17) and the generator G = (5, 1). Check that G is on the curve: the left side is 1, and the right side is 125 + 10 + 2 = 137, which leaves remainder 1 when divided by 17.

Point doubling

To double a point (x, y), first compute the slope λ = (3x^2 + a) / (2y) mod p. Division means multiplying by the modular inverse. Then x3 = λ^2 − 2x and y3 = λ(x − x3) − y, both mod p.

2G: λ = (3·25 + 2) / 2 = 77 / 2 → 9 × 9 = 81 → 13   (the inverse of 2 mod 17 is 9)
    x3 = 13² − 2·5 = 159 → 6
    y3 = 13·(5 − 6) − 1 = −14 → 3
    2G = (6, 3)

Point addition

To add two different points, the slope is λ = (y2 − y1) / (x2 − x1), with x3 = λ^2 − x1 − x2 and y3 = λ(x1 − x3) − y1.

3G = 2G + G: λ = (1 − 3) / (5 − 6) = (−2) / (−1) = 2
    x3 = 4 − 6 − 5 = −7 → 10
    y3 = 2·(6 − 10) − 3 = −11 → 6
    3G = (10, 6)

You can confirm each result lies on the curve. For (10, 6): 6² = 36, which is 2 mod 17, and 10³ + 20 + 2 = 1022, which is also 2 mod 17.

Key agreement (ECDH) on the toy curve

Say Alice picks private key 3 and Bob picks private key 2. Their public keys are the points 3G = (10, 6) and 2G = (6, 3), which we just computed. They swap public keys over an open channel.

  • Alice multiplies Bob's public point by her number: 3 × (2G) = 6G.
  • Bob multiplies Alice's public point by his number: 2 × (3G) = 6G.

Both reach the same point. Here is Bob's side, doubling (10, 6) to get 6G:

λ = (3·100 + 2) / 12 = 302 / 12 → 13 × 10 = 130 → 11   (the inverse of 12 mod 17 is 10)
x3 = 11² − 2·10 = 101 → 16
y3 = 11·(10 − 16) − 6 = −72 → 13
6G = (16, 13)

The shared secret is the point (16, 13), usually reduced to its x-coordinate. An eavesdropper sees only G, (10, 6) and (6, 3) and would need to recover a private number from a public point. On this toy curve that is a few tries. On a real curve it is not feasible. Real protocols then pass the shared secret through a key derivation function and use the result as a symmetric key.

All of the arithmetic above is plain modular arithmetic that you can repeat with any calculator or a few lines of code in a language with big integers.

ECDSA and EdDSA: signing with a curve

Key agreement is one use. The other is signing. You sign a hash of the message with your private number, and anyone holding your public point can verify the signature. The difference from RSA is only the math underneath. See public-key cryptography explained for the encrypt-versus-sign distinction, and larely's hash generator if you want to see what the hash of a message looks like.

  • ECDSA is part of NIST's Digital Signature Standard, FIPS 186-5, which "specifies a suite of algorithms that can be used to generate a digital signature". Each ECDSA signature needs a fresh secret number. RFC 6979 warns that "even slight biases in that process may be turned into attacks on the signature schemes", and defines a deterministic way to derive it.
  • EdDSA is specified in RFC 8032. It states that "EdDSA signatures are deterministic", so there is no per-signature random number to get wrong. For Ed25519, the keys are 32 bytes and signatures are 64 bytes.

Which curves are used in practice

CurveUsed forNotes from the standards
secp256r1 (NIST P-256), secp384r1, secp521r1ECDH and ECDSA, including TLSListed as TLS 1.3 named groups in RFC 8446; NIST curves defined in the FIPS 186 and SP 800-186 documents
Curve25519 (X25519)Key agreementRFC 7748: prime 2255 − 19, about 128-bit security, 32-byte inputs and outputs
Curve448 (X448)Key agreementRFC 7748: about 224-bit security, 56-byte inputs and outputs
Edwards25519 (Ed25519)SignaturesRFC 8032: about 128-bit security, 32-byte public keys, 64-byte signatures
Edwards448 (Ed448)SignaturesRFC 8032: about 224-bit security

RFC 7748 gives Curve25519 as v^2 = u^3 + A*u^2 + u, a different shape from the toy equation above. It is a Montgomery curve, chosen so the key agreement function can be computed quickly and with fewer ways to make an implementation mistake. The RFC's constant a24 is (486662 − 2) / 4 = 121665. NIST SP 800-186 now lists two Edwards curves alongside the older Weierstrass ones, citing "increased performance, side-channel resistance, and simpler implementation".

Where you will meet it

  • HTTPS. TLS 1.3 lists secp256r1, secp384r1, secp521r1, x25519 and x448 as elliptic curve groups for the key exchange, and says peers "MUST validate each other's public value Q". Walk through where that fits in the TLS handshake.
  • SSH. Ed25519 is the usual modern key type. Generating an Ed25519 SSH key takes one command.
  • Passkeys, code signing and certificates. These use ECDSA or EdDSA signatures, with the same private key and public point pattern as above.

What can go wrong

  • Bad randomness in ECDSA. Reusing or biasing the per-signature secret can leak the private key. RFC 6979 exists for this reason.
  • Unvalidated public points. A received point must actually be on the curve and valid for the protocol. This is why RFC 8446 requires peers to validate each other's public value.
  • Homemade curves and code. Use a curve from a standard and a maintained library. Do not pick your own parameters or write the arithmetic yourself: small mistakes can be invisible in testing and fatal in production.

Frequently asked questions

Is elliptic curve cryptography encryption or signing?
It is a family of public-key techniques, not one operation. The two common uses are key agreement (ECDH, X25519), where two parties derive a shared secret, and digital signatures (ECDSA, EdDSA). Data itself is then encrypted with a symmetric cipher using the derived key.
What is the difference between ECDSA and Ed25519?
Both sign with an elliptic curve private key. ECDSA is specified in NIST FIPS 186-5 and is usually used with NIST curves such as P-256; it needs a fresh secret number for every signature. Ed25519 is an EdDSA variant on a different curve. RFC 8032 says EdDSA signatures are deterministic, and Ed25519 keys are 32 bytes and signatures 64 bytes.
Why are elliptic curve keys smaller than RSA keys?
The best known attacks on a well-chosen curve are slower per key bit than attacks on RSA's factoring problem, so a shorter key reaches a similar security level. RFC 7748 puts Curve25519 at about 128 bits of security with 32-byte keys, whereas an RSA modulus needs 2048 bits (256 bytes) to meet NIST's minimum.
Is a shared ECDH secret used directly as an encryption key?
Normally not. The shared point, usually reduced to its x-coordinate, is passed through a key derivation function, and the output becomes the symmetric key that encrypts the actual data. Protocols such as TLS 1.3 do this inside the handshake.

Sources