Path: blob/main/foundations/05-discrete-log-diffie-hellman/sage/05e-baby-step-giant-step.ipynb
483 views
Notebook 05e: Baby-Step Giant-Step
Module 05. The Discrete Logarithm and Diffie-Hellman
Motivating Question. Brute-force DLP takes time where . Can we do better without exploiting any special structure of the group? Shanks' baby-step giant-step (BSGS) algorithm answers yes: it solves any DLP in time and space. This is the classic time-space tradeoff in cryptanalysis.
Prerequisites. You should be comfortable with:
The discrete logarithm problem (notebook 05a)
Modular arithmetic (Module 01)
Learning objectives. By the end of this notebook you will be able to:
Explain the key idea behind BSGS: decompose .
Build the baby-step lookup table and perform giant steps.
Implement BSGS from scratch and verify correctness.
Analyse the time and space complexity.
Compare BSGS performance to brute force experimentally.
1. The Key Idea
We want to find such that in a group of order .
Set . Write where and .
Then:
Strategy:
Baby steps: Precompute and store for in a hash table.
Giant steps: For , compute and check if it appears in the table.
If , then .
| Phase | Operations | Storage |
|---|---|---|
| Baby steps | multiplications | table entries |
| Giant steps | up to multiplications + lookups | 0 extra |
| Total |
Checkpoint 1. If , what is ? How many baby steps and (at most) how many giant steps are needed? Compare with brute force's 10000 steps.
2. Worked Example:
Let us trace BSGS step by step for , (primitive root), and a target .
Notice: we found after only total group operations, not 42.
3. Complete Implementation
Let us package BSGS into a clean function.
Misconception alert. "BSGS needs to know the exact group order ." Technically, an upper bound suffices. If you use for any , the algorithm still works (the table might be slightly larger than necessary, but correctness is preserved).
4. Performance: BSGS vs Brute Force
Let us compare the actual running times.
Checkpoint 2. At 22 bits, how does the speedup compare to ? Why might the actual speedup differ from the theoretical ratio?
5. Visualising Baby and Giant Steps
Let us visualise how the two sets of steps "meet" to find the answer.
6. Memory vs Time
BSGS uses memory for the hash table. For a 128-bit group, that is entries, about 128 exabytes. This is infeasible!
| Group size | Memory (16 bytes/entry) | |
|---|---|---|
| = 65,536 | 1 MB | |
| = 4 billion | 64 GB | |
| 256 EB (infeasible) | ||
| absurdly large |
For cryptographic sizes, BSGS alone is not enough. More advanced methods like Pollard's rho algorithm achieve time with space, and index calculus methods achieve sub-exponential time for .
Bridge to Module 06. For elliptic curve groups, the best known generic algorithm is still (Pollard's rho). There is no index calculus for general elliptic curves! This is why ECC can use much smaller keys than -based crypto: 256-bit ECC 3072-bit DH.
Checkpoint 3. If you have 8 GB of RAM, roughly how many bits can your BSGS table handle? (Assume each entry takes about 100 bytes.)
7. Exercises
Exercise 1 (Worked): BSGS by Hand
Problem. Solve using BSGS.
Solution. , .
Baby steps ( for ):
| 0 | 1 |
| 1 | 5 |
| 2 | 2 |
| 3 | 10 |
| 4 | 4 |
Giant steps: . Since , so .
Let's compute with SageMath.
Exercise 2 (Guided): BSGS with a Larger Group
Problem. Use your baby_step_giant_step function to solve the DLP in for .
Pick a random secret and compute .
Solve using BSGS and verify.
Time it and compare with
discrete_log().
Hint: The step size should be .
Exercise 3 (Independent): Generalised BSGS
Problem.
Modify
baby_step_giant_stepso that it works even when the group order is not exactly known, instead, accept an upper bound and use .Test your modified version on where you pass (which is slightly larger than ).
What is the overhead of using an upper bound instead of the exact order?
Summary
| Concept | Key Fact |
|---|---|
| Key idea | Write with ; precompute baby steps, search with giant steps |
| Time | group operations |
| Space | hash table entries |
| Generic | Works for any group, no special structure needed |
| Limitation | Memory-bound: infeasible for or so |
| Crypto impact | Sets the baseline: DLP in a group of order can always be solved in |
BSGS is generic, it works for any group. But if the group order has special structure (many small prime factors), we can do even better. The next notebook introduces the Pohlig-Hellman algorithm, which exploits smooth-order groups.