Skip to content

Add baby-step giant-step discrete logarithm algorithm - #14997

Open
felipeofdev-ai wants to merge 4 commits into
TheAlgorithms:masterfrom
felipeofdev-ai:feat/baby-step-giant-step
Open

Add baby-step giant-step discrete logarithm algorithm#14997
felipeofdev-ai wants to merge 4 commits into
TheAlgorithms:masterfrom
felipeofdev-ai:feat/baby-step-giant-step

Conversation

@felipeofdev-ai

@felipeofdev-ai felipeofdev-ai commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword.

Summary

Adds maths/baby_step_giant_step.py implementing the classic baby-step giant-step method for discrete logarithms modulo a prime (or any modulus where base is invertible).

  • Builds a baby-step table of size ⌈√(modulus-1)⌉
  • Uses modular inverse giant steps via pow(base, -m, modulus)
  • Returns the smallest non-negative exponent; raises ValueError when no solution exists
  • Doctests verified locally with python -m doctest

Reference: https://en.wikipedia.org/wiki/Baby-step_giant-step


— Felipe Fernandes · Systems & Agentic AI Engineer
https://github.com/felipeofdev-ai · https://felipeofdev-ai.github.io/

felipeofdev-ai and others added 2 commits August 3, 2026 17:32
Solves base^x ≡ target (mod modulus) in O(sqrt(modulus)) time with
doctests for solutions, identity cases, and missing-logarithm errors.
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass labels Aug 3, 2026
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Aug 3, 2026
@felipeofdev-ai

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing — CI is green on my side. Happy to adjust anything you need.

— Felipe Fernandes · Systems & Agentic AI Engineer
https://github.com/felipeofdev-ai · https://felipeofdev-ai.github.io/

@cclauss

cclauss commented Sep 13, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, please review.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding BSGS — the structure (baby-step table + giant-step factor via pow(base, -m, modulus)) is clean and readable. The doctests all pass. Unfortunately there's a correctness bug in the step count that makes the function report "no discrete logarithm" for many cases that do have a solution.

The bug

step_count = ceil(isqrt(modulus - 1))

isqrt already returns an integer, so ceil(isqrt(x)) == isqrt(x) — the ceil is a no-op. That makes step_count = floor(sqrt(modulus - 1)). BSGS needs m = ceil(sqrt(n)) so that the giant/baby loops cover every exponent in 0 .. n-1 (the pair (giant, baby) reaches at most m*m - 1). When modulus - 1 isn't a perfect square, m*m - 1 < modulus - 2, so the largest exponents are never checked and a valid log is missed.

Reproducers (all have real solutions, all raise ValueError):

>>> baby_step_giant_step(2, 6, 11)   # 2**9 % 11 == 6, should return 9
ValueError: no discrete logarithm for 6 base 2 modulo 11
>>> baby_step_giant_step(3, 4, 7)    # 3**4 % 7 == 4, should return 4
>>> baby_step_giant_step(2, 7, 13)   # 2**11 % 13 == 7, should return 11

I brute-forced every (base, target) pair over the primes 5..97 and compared against this implementation: 524 pairs that have a solution incorrectly raise ValueError (0 wrong-but-nonzero answers — it never returns a wrong exponent, it just gives up early).

Fix — compute a true ceiling:

step_count = isqrt(modulus - 1) + 1

With that one change the same exhaustive check over primes 5..97 passes with 0 failures. (isqrt(n-1)+1 is a safe ceil(sqrt(n)) for n >= 1; the at-most-one extra baby step is negligible.)

Could you also add a doctest that would have caught this, e.g.:

>>> baby_step_giant_step(2, 6, 11)
9

Nice work overall — this is a one-line fix and then it's solid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants