TExcelTAD
Home › Number Theory & Cryptography › Elliptic Curves over Finite Fields

Elliptic Curves over Finite Fields

Replace the real numbers by Fp\mathbb{F}_p and an elliptic curve becomes a finite set of points with the same addition law. The pictures are scattered dots instead of smooth curves, but the formulas are unchanged, only now division means multiplying by a modular inverse. These finite groups are where elliptic-curve cryptography lives.

Points modulo pp

E(Fp)={(x,y)∈Fp2:y2≡x3+ax+b}∪{O}E(\mathbb{F}_p) = \{(x, y) \in \mathbb{F}_p^2 : y^2 \equiv x^3 + ax + b\} \cup \{\mathcal{O}\}. For each xx, the right side is a square (two values of yy), zero (one) or a non-square (none), which Euler's criterion decides.

python · try it yourself
p, a, b = 97, 2, 3
points = [(x, y) for x in range(p) for y in range(p) if (y * y - (x**3 + a * x + b)) % p == 0]
print("number of points (with O):", len(points) + 1)
print(points[:10])
number of points (with O): 100
[(0, 10), (0, 87), (1, 43), (1, 54), (3, 6), (3, 91), (4, 47), (4, 50), (10, 21), (10, 76)]

The same addition formulas

python · try it yourself
p, a, b = 97, 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
    if P == Q:
        lam = (3 * x1 * x1 + a) * pow(2 * y1, -1, p) % p
    else:
        lam = (y2 - y1) * pow(x2 - x1, -1, p) % p
    x3 = (lam * lam - x1 - x2) % p
    return (x3, (lam * (x1 - x3) - y1) % p)

on = lambda P: (P[1] ** 2 - (P[0] ** 3 + a * P[0] + b)) % p == 0
P, Q = (0, 10), (3, 6)
print(on(P), on(Q), add(P, Q), on(add(P, Q)), add(P, P))
multiples, R = [], None
for k in range(1, 6):
    R = add(R, P)
    multiples.append(R)
print("P, 2P, ..., 5P:", multiples)
True True (85, 71) True (65, 32)
P, 2P, ..., 5P: [(0, 10), (65, 32), (23, 24), (52, 68), (88, 56)]

How many points? Hasse's theorem

About half the xx values give a square, each with two yy values, so we expect about pp points. Hasse's theorem makes this precise:

∣#E(Fp)−(p+1)∣≤2p\lvert \#E(\mathbb{F}_p) - (p + 1) \rvert \le 2\sqrt{p}
python · try it yourself
from math import isqrt

def count_points(p, a, b):
    squares = {}
    for y in range(p):
        squares[y * y % p] = squares.get(y * y % p, 0) + 1
    return 1 + sum(squares.get((x**3 + a * x + b) % p, 0) for x in range(p))

p = 10007
for a, b in ((-3, 7), (2, 3), (0, 7), (1, 1)):
    N = count_points(p, a % p, b)
    print(f"a={a:2} b={b}  #E={N}  trace t = p+1-#E = {p + 1 - N:5}  bound 2*sqrt(p) = {2 * isqrt(p)}")
a=-3 b=7  #E=10193  trace t = p+1-#E =  -185  bound 2*sqrt(p) = 200
a= 2 b=3  #E=9846  trace t = p+1-#E =   162  bound 2*sqrt(p) = 200
a= 0 b=7  #E=10008  trace t = p+1-#E =     0  bound 2*sqrt(p) = 200
a= 1 b=1  #E=10065  trace t = p+1-#E =   -57  bound 2*sqrt(p) = 200

For 256-bit primes nobody counts point by point; Schoof's algorithm (and its SEA improvements) computes #E\#E in polynomial time, which is how standard curves were vetted.

Group structure

E(Fp)E(\mathbb{F}_p) is always cyclic or a product of two cyclic groups. For cryptography we want #E=h⋅n\#E = h \cdot n with nn a large prime and a small cofactor hh (1 for P-256 and secp256k1, 8 for Curve25519). Then a point GG of order nn generates a subgroup where the discrete log is hard, and Pohlig-Hellman cannot split it.

The curve used in the next lessons, y2=x3−3x+7y^2 = x^3 - 3x + 7 over F10007\mathbb{F}_{10007}, has a prime number of points, 10193, so every point other than O\mathcal{O} generates the whole group.

python · try it yourself
from sympy import isprime

p, a, b = 10007, -3, 7
squares = {}
for y in range(p):
    squares[y * y % p] = squares.get(y * y % p, 0) + 1
N = 1 + sum(squares.get((x**3 + a * x + b) % p, 0) for x in range(p))
print(N, isprime(N), N != p)
10193 True True

The check N != p matters: curves with exactly pp points ("anomalous" curves) have discrete logs solvable in polynomial time (Smart's attack). Other bad cases are curves where nn divides pk−1p^k - 1 for small kk (the MOV attack). Standard curves are chosen to avoid all of them. The table above contains such a weak case: y2=x3+7y^2 = x^3 + 7 has exactly p+1=10008p + 1 = 10008 points over F10007\mathbb{F}_{10007} (trace 0) because 10007≡2(mod3)10007 \equiv 2 \pmod 3. It is supersingular there and the MOV attack applies; secp256k1 uses the same equation over a prime p≡1(mod3)p \equiv 1 \pmod 3, where it is safe.

In software

  • P-256 uses p=2256−2224+2192+296−1p = 2^{256} - 2^{224} + 2^{192} + 2^{96} - 1, a=−3a = -3, and has prime order n≈2256n \approx 2^{256}. secp256k1 uses p=2256−232−977p = 2^{256} - 2^{32} - 977, a=0a = 0, b=7b = 7.
  • SafeCurves (Bernstein and Lange) lists criteria for choosing curves: rigid parameters, twist security, complete formulas.

Exercises

Exercise 1: Points by hand

List all points of y2=x3+x+1y^2 = x^3 + x + 1 over F5\mathbb{F}_5.

Answer

O\mathcal{O}, (0,1)(0, 1), (0,4)(0, 4), (2,1)(2, 1), (2,4)(2, 4), (3,1)(3, 1), (3,4)(3, 4), (4,2)(4, 2), (4,3)(4, 3): nine points.

Solution

Squares modulo 5: 0,1,40, 1, 4. Right side for x=0..4x = 0..4: 1,3,11≡1,31≡1,69≡41, 3, 11 \equiv 1, 31 \equiv 1, 69 \equiv 4. x=1x = 1 gives 3 (not a square). The others give square roots ±1\pm 1 for value 1 and ±2\pm 2 for value 4.

Exercise 2: Hasse interval

Between which bounds must #E(F101)\#E(\mathbb{F}_{101}) lie?

Answer

From 82 to 122.

Solution

2101≈20.12\sqrt{101} \approx 20.1, so #E∈[102−20.1,102+20.1]\#E \in [102 - 20.1, 102 + 20.1], that is 82≤#E≤12282 \le \#E \le 122.

Exercise 3: Order of a point (code)

On y2=x3+2x+3y^2 = x^3 + 2x + 3 over F97\mathbb{F}_{97} (100 points), find the order of P=(0,10)P = (0, 10). Why must it divide 100?

Answer

The order is 50, which divides 100 by Lagrange's theorem.

Solution
python · try it yourself
p, a = 97, 2
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) * 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)
P, R, k = (0, 10), (0, 10), 1
while R is not None:
    R, k = add(R, P), k + 1
print("order:", k, " divides 100:", 100 % k == 0)
order: 50  divides 100: True

Test yourself

Hasse's theorem says #E(Fp)\#E(\mathbb{F}_p) is…

  1. exactly pp
  2. within 2p2\sqrt{p} of p+1p + 1
  3. always prime
  4. about p2p^2

In the addition formulas over Fp\mathbb{F}_p, division means…

  1. integer division
  2. multiplying by a modular inverse
  3. floating-point division
  4. a right shift

Why should a cryptographic curve's group order be a prime (or a prime times a small cofactor)?

  1. for faster addition
  2. so Pohlig-Hellman cannot split the discrete log
  3. so points are integers
  4. to satisfy Hasse