← LibraryThe Baby Step/Giant Step MethodEngineering · MathematicsLesson 126/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Discrete Logarithms and Factoring

The Baby Step/Giant Step Method

Shanks' baby step giant step algorithm, its square-root running time, and the time-memory trade-off it embodies.

Page KV-MATH-0398Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Baby step giant step computes discrete logarithms in time proportional to the square root of the group order, by writing the unknown exponent in a two-digit representation and matching halves.

It is deterministic and achieves the generic lower bound, at the cost of memory proportional to the square root.

Learning objectives

  1. Derive the two-digit decomposition.
  2. State the algorithm and its cost.
  3. Analyse the time-memory trade-off.

01The decomposition

Write the unknown exponent x in base m = ⌈√q⌉ as x = im + j with 0 ≤ i, j < m. Then the defining equation rearranges into a matching condition.

γ^{im + j} = α  ⇒  α · γ^{−j} = (γ^m)^i

The left side depends only on j and the right only on i. Tabulating one side and scanning the other finds the match, and the birthday-style meet in the middle converts a product of ranges into a sum.

02The algorithm

Algorithm

Baby step giant step

Inputgenerator γ, target α, order q
Outputx with γ^x = α
  1. Set m = ⌈√q⌉.
  2. Baby steps: for j from 0 to m−1, store the pair (α · γ^{−j}, j) in a lookup table.
  3. Compute δ = γ^m.
  4. Giant steps: for i from 0 to m−1:
  5.   If δ^i appears in the table with value j, return x = im + j.
  6. Report no solution.
Cost  O(√q) group operations and O(√q) memory

A hash table gives constant expected lookup, so the total is dominated by the 2√q group operations. The method is deterministic — it always finds the answer if one exists, with no probabilistic element.

03The trade-off

Generic discrete logarithm methods
MethodTimeMemoryDeterministic?
Brute forceO(q)O(1)Yes
Baby step giant stepO(√q)O(√q)Yes
Pollard's rhoO(√q)O(1)No
Parallel rho with distinguished pointsO(√q / P)ModestNo

Pollard's rho achieves the same time with constant memory by detecting a cycle in a pseudorandom walk rather than storing a table. It is randomised rather than deterministic, which is a small price for eliminating the storage requirement.

04Frequently asked questions

Can the table be made smaller by unbalancing the split?

Yes. Choosing m smaller reduces memory and increases time proportionally, so any point on the time-memory curve is reachable. The balanced choice minimises the total operation count.

Does the method need the group order?

It needs an upper bound on the order to size the table. An exact order gives the tightest choice; a bound merely costs a constant factor.

Why is this the generic optimum?

Because a matching lower bound is known: any algorithm treating the group as a black box needs on the order of √q operations. Beating it requires exploiting structure the black-box model forbids.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 271-272.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

Brute-Force Discrete Logarithm SearchArticle · MathematicsNEXT LESSON →Discrete Logarithms in Groups of Prime Power OrderArticle · MathematicsFinding a Generator of the Group of Units Modulo pArticle · MathematicsDiscrete Logarithms in the Full Group Modulo pArticle · Mathematics