Baby-Step Giant-Step
Brute force finds a discrete logarithm in a group of order in up to steps. Shanks's baby-step giant-step algorithm trades memory for time and needs only about steps. It is the reason a group of order offers at most bits of security.
The idea
Let . Any exponent can be written with . Then
- Baby steps: store for in a hash table.
- Giant steps: compute and look each up in the table. A hit at step gives .
Both phases take group operations, and the table holds entries.
from math import isqrt
def bsgs(g, h, p, n=None):
n = n or p - 1
m = isqrt(n) + 1
table = {}
e = 1
for j in range(m):
table.setdefault(e, j)
e = e * g % p
factor = pow(g, -m, p) # g^(-m)
y = h
for i in range(m):
if y in table:
return i * m + table[y], i + m
y = y * factor % p
return None
p, g = 1019, 2
x, ops = bsgs(g, 777, p)
print(x, pow(g, x, p), "operations:", ops)
p = 1_000_003
x, ops = bsgs(2, 123456, p)
print(x, pow(2, x, p) == 123456, "operations:", ops, " sqrt(p) =", isqrt(p))1004 777 operations: 63
864664 True operations: 1864 sqrt(p) = 1000
Scaling
from math import isqrt
from sympy import nextprime, primitive_root
def bsgs(g, h, p):
m = isqrt(p - 1) + 1
table, e = {}, 1
for j in range(m):
table.setdefault(e, j)
e = e * g % p
f, y = pow(g, -m, p), h
for i in range(m):
if y in table:
return i * m + table[y]
y = y * f % p
for bits in (20, 28, 36):
p = nextprime(2**bits)
g = primitive_root(p)
secret = 3**bits % (p - 1)
x = bsgs(g, pow(g, secret, p), p)
print(f"{bits}-bit p: found x == secret: {x == secret}, table size about {isqrt(p)}")20-bit p: found x == secret: True, table size about 1024
28-bit p: found x == secret: True, table size about 16384
36-bit p: found x == secret: True, table size about 262144
Each 2 extra bits in double the work and the memory. A 128-bit group order would need table entries, more memory than exists. That is why groups for 128-bit security have order about .
Pollard's rho for logarithms
Pollard's rho method reaches the same time with constant memory: it walks a pseudo-random sequence of elements until a collision , which gives . Parallel versions with "distinguished points" are what attackers use against elliptic curves in practice. Shoup proved that no generic algorithm (one that only uses group operations) can beat , so for well-chosen groups this is optimal.
In software
- The ECC challenges set by Certicom were attacked with parallel rho: ECCp-131 (a 131-bit curve) needed thousands of GPU-years-scale effort; nothing near 256 bits is reachable.
- The same meet-in-the-middle idea breaks double encryption: two 56-bit DES keys give only about 57 bits of security against a time-memory tradeoff, which is why Triple DES was used instead.
Exercises
Exercise 1: Parameters
For a group of order , what are , the table size and the number of giant steps in the worst case?
Answer
; a million table entries and at most a million giant steps.
Solution
. The table needs entries; the giant steps need at most multiplications plus lookups. Brute force would need up to .
Exercise 2: Work a tiny case
Use baby-step giant-step with to solve .
Answer
.
Solution
Baby steps: , , , . (since ). Giant step 0: , not in the table. Giant step 1: , found with . So .
Exercise 3: Security level (code)
For group orders of 64, 128, 160 and 256 bits, print the generic security level .
Answer
32, 64, 80 and 128 bits.
Solution
for bits in (64, 128, 160, 256):
print(bits, "-> about", bits // 2, "bits of security against generic attacks")64 -> about 32 bits of security against generic attacks
128 -> about 64 bits of security against generic attacks
160 -> about 80 bits of security against generic attacks
256 -> about 128 bits of security against generic attacks
Test yourself
Baby-step giant-step solves a discrete log in a group of order in time about…
What does baby-step giant-step store?
- nothing
- the baby steps for in a lookup table
- all of
- the factorization of
Why do 128-bit-secure elliptic-curve groups have order about ?
- for faster arithmetic
- generic attacks need about operations
- 256 is a power of two
- to fit in SHA-256