Skip to content

Refactor ModPow: fix overflow, simplify loop, add tests#1315

Merged
williamfiset merged 1 commit intomasterfrom
refactor-modpow
Apr 2, 2026
Merged

Refactor ModPow: fix overflow, simplify loop, add tests#1315
williamfiset merged 1 commit intomasterfrom
refactor-modpow

Conversation

@williamfiset
Copy link
Copy Markdown
Owner

Summary

  • Replace fragile MAX/MIN overflow guards with overflow-safe mulMod via BigInteger, supporting the full long range
  • Simplify bit-mask exponentiation loop to standard binary exponentiation
  • Normalize base into [0, mod) upfront instead of repeated +mod % mod in the loop
  • Remove main() and inline test helpers (randLong, RANDOM)
  • Add 11 JUnit 5 tests including randomized validation against BigInteger.modPow

Test plan

  • bazel test //src/test/java/com/williamfiset/algorithms/math:ModPowTest — all 11 tests pass

🤖 Generated with Claude Code

Replace fragile MAX/MIN overflow guards with overflow-safe mulMod via
BigInteger, supporting the full long range. Simplify the bit-mask
exponentiation loop to standard binary exponentiation. Remove inline
main() test code and add 11 JUnit 5 tests including a randomized check
against BigInteger.modPow.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@williamfiset williamfiset merged commit 0fcf135 into master Apr 2, 2026
2 checks passed
@williamfiset williamfiset deleted the refactor-modpow branch April 2, 2026 23:07
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