Scalar Multiplication and the EC Discrete Log
Every elliptic-curve protocol rests on one operation, , and on one hard problem, recovering from and . This lesson implements fast scalar multiplication, shows why it must be constant-time, and measures the elliptic-curve discrete logarithm problem on a toy curve.
All examples use the toy curve over , which has 10193 points (a prime), with generator .
Double-and-add
Write in binary and process bits from the top: double for every bit, add for every 1-bit. That is point operations, the additive version of square-and-multiply.
p, a, b = 10007, -3, 7
N = 10193 # number of points (prime)
G = (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)
def mul(k, P):
R = None
for bit in bin(k)[2:]:
R = add(R, R)
if bit == "1":
R = add(R, P)
return R
print("on curve:", (G[1]**2 - G[0]**3 - a * G[0] - b) % p == 0)
print("1234 G =", mul(1234, G))
print("N G =", mul(N, G), " (the point at infinity)")
print("(N + 5) G == 5 G:", mul(N + 5, G) == mul(5, G))
print("distributive: 300G + 900G == 1200G:", add(mul(300, G), mul(900, G)) == mul(1200, G))on curve: True
1234 G = (5453, 4435)
N G = None (the point at infinity)
(N + 5) G == 5 G: True
distributive: 300G + 900G == 1200G: True
Since , scalars live modulo , just as exponents modulo the group order in .
The ECDLP
Given and , find . On a well-chosen curve the best known attack is a generic one, baby-step giant-step or Pollard rho, costing about operations. For our 14-bit group that is about 100 steps:
from math import isqrt
p, a = 10007, -3
N, G = 10193, (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
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)
def mul(k, P):
R = None
for bit in bin(k)[2:]:
R = add(R, R)
if bit == "1": R = add(R, P)
return R
def neg(P):
return None if P is None else (P[0], -P[1] % p)
def ec_bsgs(Q):
m = isqrt(N) + 1
table, R = {}, None
for j in range(m): # baby steps: jG
table[R] = j
R = add(R, G)
step, R = neg(mul(m, G)), Q # giant steps: Q - i*m*G
for i in range(m):
if R in table:
return i * m + table[R]
R = add(R, step)
secret = 7777
Q = mul(secret, G)
print("found:", ec_bsgs(Q), " table size:", isqrt(N) + 1)found: 7777 table size: 101
A 256-bit curve has , so the same attack needs about steps: that is what "128-bit security" means for P-256 and Curve25519.
Constant time and the Montgomery ladder
Double-and-add does an extra addition only for 1-bits, so its timing and power trace reveal . The Montgomery ladder keeps and and does exactly one addition and one doubling per bit. X25519 is specified with the ladder on -coordinates only, with a constant-time swap instead of a branch:
p, a = 10007, -3
N, G = 10193, (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
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)
def ladder(k, P, bits=14):
R0, R1 = None, P
ops = 0
for i in reversed(range(bits)): # fixed number of iterations
bit = (k >> i) & 1
if bit:
R0, R1 = add(R0, R1), add(R1, R1)
else:
R1, R0 = add(R0, R1), add(R0, R0)
ops += 2
return R0, ops
def mul(k, P):
R = None
for bit in bin(k)[2:]:
R = add(R, R)
if bit == "1": R = add(R, P)
return R
for k in (1, 4096, 8191, 7777):
R, ops = ladder(k, G)
print(k, R == mul(k, G), "operations:", ops)1 True operations: 28
4096 True operations: 28
8191 True operations: 28
7777 True operations: 28
Every scalar costs the same 28 operations. (Python still branches on bit; real code uses a masked conditional swap so even the memory access pattern is independent of .)
Why curves beat
In , index calculus factors numbers over a base of small primes and solves discrete logs in sub-exponential time. Elliptic-curve points have no notion of "small" or "factoring", and no comparable attack is known for general curves. So a 256-bit curve matches a 3072-bit finite-field group, with 12 times smaller keys and much faster operations.
In software
- Libraries use projective or Jacobian coordinates to avoid one inversion per addition, and precomputed tables of multiples of the fixed base point to make key generation and signing faster.
- Bitcoin's secp256k1 library and Ed25519 implementations are carefully constant-time; side-channel attacks on non-constant-time ECDSA (for example "Minerva", 2019) recovered keys from timing.
Exercises
Exercise 1: Count operations
About how many point doublings and additions does double-and-add need for a random 256-bit scalar?
Answer
About 256 doublings and 128 additions.
Solution
One doubling per bit and one addition per 1-bit; a random scalar has about half its bits set.
Exercise 2: Scalars mod
On our toy curve, why is ?
Answer
and .
Solution
The group has elements, so (Lagrange), and .
Exercise 3: Negation is free (code)
Show that for on the toy curve. Why does this let Pollard rho run about times faster?
Answer
Same -coordinate, opposite ; the attack can work with classes , halving the search space.
Solution
p, a, N, G = 10007, -3, 10193, (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
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)
def mul(k, P):
R = None
for bit in bin(k)[2:]:
R = add(R, R)
if bit == "1": R = add(R, P)
return R
P1, P2 = mul(1234, G), mul(N - 1234, G)
print(P1, P2, P1[0] == P2[0] and (P1[1] + P2[1]) % p == 0)(5453, 4435) (5453, 5572) True
Negating a point is free, so a random walk can identify with , which halves the effective group size and saves a factor .
Test yourself
The elliptic-curve discrete logarithm problem asks to find…
- from
- from and
- the number of points
- the curve equation
Double-and-add is to elliptic curves what … is to modular exponentiation.
- the extended Euclidean algorithm
- square-and-multiply
- the sieve
- the CRT
Why do implementations use the Montgomery ladder?
- it needs fewer bits
- it performs the same operations for every scalar bit, resisting timing attacks
- it avoids the point at infinity
- it works only on Curve25519