TExcelTAD
Home › Number Theory & Cryptography › Baby-Step Giant-Step

Baby-Step Giant-Step

Brute force finds a discrete logarithm in a group of order nn in up to nn steps. Shanks's baby-step giant-step algorithm trades memory for time and needs only about 2n2\sqrt{n} steps. It is the reason a group of order nn offers at most 12log⁡2n\frac{1}{2}\log_2 n bits of security.

The idea

Let m=⌈n⌉m = \lceil \sqrt{n} \rceil. Any exponent x<nx < n can be written x=im+jx = im + j with 0≤i,j<m0 \le i, j < m. Then

gx=h  ⟺  gj=h⋅(g−m)ig^x = h \iff g^j = h \cdot (g^{-m})^i
  1. Baby steps: store gj↦jg^j \mapsto j for j=0,…,m−1j = 0, \ldots, m - 1 in a hash table.
  2. Giant steps: compute h,hg−m,hg−2m,…h, hg^{-m}, hg^{-2m}, \ldots and look each up in the table. A hit at step ii gives x=im+jx = im + j.

Both phases take m≈nm \approx \sqrt{n} group operations, and the table holds mm entries.

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

python · try it yourself
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 pp double the work and the memory. A 128-bit group order would need 2642^{64} table entries, more memory than exists. That is why groups for 128-bit security have order about 22562^{256}.

Pollard's rho for logarithms

Pollard's rho method reaches the same n\sqrt{n} time with constant memory: it walks a pseudo-random sequence of elements gaihbig^{a_i} h^{b_i} until a collision gaihbi=gajhbjg^{a_i}h^{b_i} = g^{a_j}h^{b_j}, which gives x≡(ai−aj)/(bj−bi)(modn)x \equiv (a_i - a_j)/(b_j - b_i) \pmod{n}. 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 n\sqrt{n}, 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 n=1012n = 10^{12}, what are mm, the table size and the number of giant steps in the worst case?

Answer

m=106m = 10^6; a million table entries and at most a million giant steps.

Solution

m=⌈1012⌉=106m = \lceil \sqrt{10^{12}} \rceil = 10^6. The table needs 10610^6 entries; the giant steps need at most 10610^6 multiplications plus lookups. Brute force would need up to 101210^{12}.

Exercise 2: Work a tiny case

Use baby-step giant-step with m=4m = 4 to solve 3x≡13(mod17)3^x \equiv 13 \pmod{17}.

Answer

x=4x = 4.

Solution

Baby steps: 30=13^0 = 1, 31=33^1 = 3, 32=93^2 = 9, 33=103^3 = 10. g−4=13−1 mod 17=4g^{-4} = 13^{-1} \bmod 17 = 4 (since 13⋅4=52=51+113 \cdot 4 = 52 = 51 + 1). Giant step 0: 1313, not in the table. Giant step 1: 13⋅4=52≡113 \cdot 4 = 52 \equiv 1, found with j=0j = 0. So x=1⋅4+0=4x = 1 \cdot 4 + 0 = 4.

Exercise 3: Security level (code)

For group orders of 64, 128, 160 and 256 bits, print the generic security level 12log⁡2n\frac{1}{2}\log_2 n.

Answer

32, 64, 80 and 128 bits.

Solution
python · try it yourself
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 nn in time about…

  1. nn
  2. n\sqrt{n}
  3. log⁡n\log n
  4. n2n^2

What does baby-step giant-step store?

  1. nothing
  2. the baby steps gjg^j for j<mj < m in a lookup table
  3. all of g0,…,gn−1g^0, \ldots, g^{n-1}
  4. the factorization of nn

Why do 128-bit-secure elliptic-curve groups have order about 22562^{256}?

  1. for faster arithmetic
  2. generic attacks need about n=2128\sqrt{n} = 2^{128} operations
  3. 256 is a power of two
  4. to fit in SHA-256