Elliptic Curves over Finite Fields
Replace the real numbers by 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
. For each , the right side is a square (two values of ), zero (one) or a non-square (none), which Euler's criterion decides.
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
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 values give a square, each with two values, so we expect about points. Hasse's theorem makes this precise:
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 in polynomial time, which is how standard curves were vetted.
Group structure
is always cyclic or a product of two cyclic groups. For cryptography we want with a large prime and a small cofactor (1 for P-256 and secp256k1, 8 for Curve25519). Then a point of order generates a subgroup where the discrete log is hard, and Pohlig-Hellman cannot split it.
The curve used in the next lessons, over , has a prime number of points, 10193, so every point other than generates the whole group.
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 points ("anomalous" curves) have discrete logs solvable in polynomial time (Smart's attack). Other bad cases are curves where divides for small (the MOV attack). Standard curves are chosen to avoid all of them. The table above contains such a weak case: has exactly points over (trace 0) because . It is supersingular there and the MOV attack applies; secp256k1 uses the same equation over a prime , where it is safe.
In software
- P-256 uses , , and has prime order . secp256k1 uses , , .
- 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 over .
Answer
, , , , , , , , : nine points.
Solution
Squares modulo 5: . Right side for : . gives 3 (not a square). The others give square roots for value 1 and for value 4.
Exercise 2: Hasse interval
Between which bounds must lie?
Answer
From 82 to 122.
Solution
, so , that is .
Exercise 3: Order of a point (code)
On over (100 points), find the order of . Why must it divide 100?
Answer
The order is 50, which divides 100 by Lagrange's theorem.
Solution
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 is…
- exactly
- within of
- always prime
- about
In the addition formulas over , division means…
- integer division
- multiplying by a modular inverse
- floating-point division
- a right shift
Why should a cryptographic curve's group order be a prime (or a prime times a small cofactor)?
- for faster addition
- so Pohlig-Hellman cannot split the discrete log
- so points are integers
- to satisfy Hasse