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

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
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/

@felipeofdev-ai
felipeofdev-ai force-pushed the feat/baby-step-giant-step branch from 366652e to 979770d Compare August 4, 2026 02:55
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.

1 participant