TExcelTAD
Home › Number Theory & Cryptography › Scalar Multiplication and the EC Discrete Log

Scalar Multiplication and the EC Discrete Log

Every elliptic-curve protocol rests on one operation, Q=kGQ = kG, and on one hard problem, recovering kk from QQ and GG. 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 y2=x3−3x+7y^2 = x^3 - 3x + 7 over F10007\mathbb{F}_{10007}, which has 10193 points (a prime), with generator G=(2,3)G = (2, 3).

Double-and-add

Write kk in binary and process bits from the top: double for every bit, add GG for every 1-bit. That is O(log⁡k)O(\log k) point operations, the additive version of square-and-multiply.

python · try it yourself
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 NG=ONG = \mathcal{O}, scalars live modulo NN, just as exponents modulo the group order in Zp×\mathbb{Z}_p^\times.

The ECDLP

Given GG and Q=kGQ = kG, find kk. On a well-chosen curve the best known attack is a generic one, baby-step giant-step or Pollard rho, costing about N\sqrt{N} operations. For our 14-bit group that is about 100 steps:

python · try it yourself
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 N≈2256N \approx 2^{256}, so the same attack needs about 21282^{128} 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 kk. The Montgomery ladder keeps R0=jGR_0 = jG and R1=(j+1)GR_1 = (j + 1)G and does exactly one addition and one doubling per bit. X25519 is specified with the ladder on xx-coordinates only, with a constant-time swap instead of a branch:

python · try it yourself
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 kk.)

Why curves beat Zp×\mathbb{Z}_p^\times

In Zp×\mathbb{Z}_p^\times, 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 GG 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 NN

On our toy curve, why is 10194G=G10194G = G?

Answer

10194=N+110194 = N + 1 and NG=ONG = \mathcal{O}.

Solution

The group has N=10193N = 10193 elements, so NG=ONG = \mathcal{O} (Lagrange), and (N+1)G=NG+G=G(N + 1)G = NG + G = G.

Exercise 3: Negation is free (code)

Show that (N−k)G=−(kG)(N - k)G = -(kG) for k=1234k = 1234 on the toy curve. Why does this let Pollard rho run about 2\sqrt{2} times faster?

Answer

Same xx-coordinate, opposite yy; the attack can work with classes {P,−P}\{P, -P\}, halving the search space.

Solution
python · try it yourself
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 PP with −P-P, which halves the effective group size and saves a factor 2\sqrt{2}.

Test yourself

The elliptic-curve discrete logarithm problem asks to find…

  1. GG from QQ
  2. kk from Q=kGQ = kG and GG
  3. the number of points
  4. the curve equation

Double-and-add is to elliptic curves what … is to modular exponentiation.

  1. the extended Euclidean algorithm
  2. square-and-multiply
  3. the sieve
  4. the CRT

Why do implementations use the Montgomery ladder?

  1. it needs fewer bits
  2. it performs the same operations for every scalar bit, resisting timing attacks
  3. it avoids the point at infinity
  4. it works only on Curve25519