How baby-step giant-step solves a discrete log

Short answer: Baby-step giant-step solves g^x ≡ h (mod p) for a small x by writing x = i·m − j with m = ceil(sqrt(N)) and x in a range of length N. It stores the baby steps g^j for j = 0..m−1 in a hash table, then walks giant steps h·(g^{−m})^i and looks each one up. A hit gives x = i·m − j. Time and memory are O(√N) multiplications and table entries. The algorithm is deterministic and exponential in the bit length of the group order.


What the problem actually asks

A discrete logarithm has no known polynomial-time classical algorithm in a general prime field. Brute force tries every exponent. Baby-step giant-step is the meet-in-the-middle improvement: the unknown exponent is a high part times m minus a low part, and one side of that equation can be tabulated. Any collision between a stored baby step and a computed giant step solves for x.

The group does not have to be the multiplicative group modulo a prime. Any cyclic group in which elements can be multiplied and hashed works. The range N must be known. Usually N is the group order.


What the algorithm guarantees

If a solution exists in the searched range, the algorithm finds the smallest nonnegative one, or any one, depending on the order of the search. Time is Θ(√N) group operations and memory is Θ(√N) group elements. That memory is the limit long before the time is. Pollard’s rho for discrete logs uses negligible memory and has a similar expected operation count. It is the practical replacement when √N elements will not fit.

The algorithm is not an index-calculus method and does not exploit a special prime. On a cryptographic group whose order is a large prime, √N is still infeasible. The existence of this algorithm is why a discrete-log system needs a group large enough that √N operations are impossible.


The working steps

  1. Let m = ceil(sqrt(N)). Compute baby steps b[j] = g^j for j = 0, 1, …, m−1, and store a map from the group element to j.
  2. Compute factor = g^{−m}, the inverse of g^m. Set gamma = h.
  3. For i = 0, 1, …, m−1, look up gamma in the map. If gamma = g^j is found, return x = i·m − j, adjusted into range if the sign is negative.
  4. Replace gamma by gamma·factor and continue.
  5. If the loop ends with no hit, no exponent in the range solves the equation.

The square-root bound is time and memory. Pollard’s rho for logarithms drops the memory and keeps a similar expected time. Neither algorithm is a reason to use a small group. Index calculus and number-field sieve variants attack discrete logs in some groups faster and do not apply unchanged to a generic elliptic-curve group, where baby-step giant-step and rho remain the generic attacks.


Approaches that look simpler and fail

Approach Why it fails when the table stores only the exponent
Store j and not the group element g^j The lookup is by the element the giant step just computed. The exponent is the value retrieved after the element hits.
Use m much smaller than sqrt(N) and stop at i < m The pairs (i, j) then cover fewer than N exponents. A solution outside the covered rectangle is missed.
Ignore the memory and quote only the time Θ(√N) group elements is the reason a large instance cannot run this algorithm even when the operation count looks acceptable on paper.
Apply the result as a polynomial-time break of a standard-size discrete-log key Square root of a 256-bit group order is 128 bits of work. The algorithm is exponential in the security parameter.

A low-memory generic discrete log is Pollard’s rho in the group. A prime-field log at cryptographic size, when it is attacked at all, uses index calculus, not this table. Baby-step giant-step is the deterministic meet-in-the-middle log for a moderate range.


A working example

This Python fragment stores baby steps g^j for j < m, with m = ceil(sqrt(order)), then takes giant steps of g^{−m}. In the group modulo 23, base 5, target 8, the discrete log is 6 because 5^6 ≡ 8.

p, g, target = 23, 5, 8
order = p - 1
m = int(order ** 0.5 + 0.999)
baby = {}
value = 1
for j in range(m):
    baby[value] = j
    value = value * g % p
factor = pow(pow(g, m, p), -1, p)
gamma = target
for i in range(m):
    if gamma in baby:
        answer = i * m + baby[gamma]
        break
    gamma = gamma * factor % p
assert pow(g, answer, p) == target
print(answer)

Where it is used

Baby-step giant-step computes a discrete logarithm in a group small enough to store the baby steps. It is a cryptanalytic and a parameter-checking tool, not a handshake. Pollard’s rho is the method used once the group no longer fits in memory.


When you can drop this algorithm

Drop the table when √N elements do not fit. Use a rho-style random walk. Keep baby-step giant-step when the range is small enough to store and a deterministic answer is wanted.


Why this stays published

We publish this so a search for baby-step giant-step returns the √N table of powers and the giant-step lookup that recovers the exponent. Baby-step giant-step solves a discrete logarithm in a known range of length N with O(√N) time and O(√N) memory by matching tabulated powers against giant steps.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *