-
Notifications
You must be signed in to change notification settings - Fork 20.8k
Add Baby-Step Giant-Step (BSGS) Discrete Logarithm Algorithm #7118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rajucreate
wants to merge
12
commits into
TheAlgorithms:master
Choose a base branch
from
rajucreate:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
91845a5
Add BSGS algorithm to DIRECTORY.md
rajucreate 1539125
Add Wikipedia reference to BSGS algorithm Javadoc
rajucreate 6c83fc8
Fix incorrect BSGS test cases
rajucreate 7d4de81
Fix spacing after private constructor for Checkstyle
rajucreate c448ffb
Mark utility class as final to satisfy Checkstyle
rajucreate f094f3e
Fix PMD useless parentheses violations
rajucreate 4a27eca
Fix formatting for clang-format
rajucreate cf027be
Add newline at end of file to satisfy Checkstyle
rajucreate fd8d4ba
test: improve coverage for DiscreteLogarithmBSGS
rajucreate 864c3d3
test: improve coverage for DiscreteLogarithmBSGS
rajucreate c92e485
Merge branch 'master' into master
rajucreate 29e7dbe
Merge branch 'master' into master
rajucreate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/thealgorithms/maths/DiscreteLogarithmBSGS.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Baby-Step Giant-Step algorithm for the Discrete Logarithm Problem. | ||
| * | ||
| * <p> | ||
| * Solves for x in: a^x ≡ b (mod m), | ||
| * where a, b, m are given, and m is prime (or a has order in modulo m). | ||
| * | ||
| * <p> | ||
| * Time complexity: O(√m) | ||
| * Space complexity: O(√m) | ||
| * | ||
| * @see <a href="https://en.wikipedia.org/wiki/Baby-step_giant-step">Baby-step giant-step algorithm</a> | ||
| */ | ||
| public final class DiscreteLogarithmBSGS { | ||
|
|
||
| private DiscreteLogarithmBSGS() { | ||
| throw new UnsupportedOperationException("Utility class"); | ||
| } | ||
|
|
||
| /** | ||
| * Computes x such that (a^x) % m == b. | ||
| * Returns -1 if no solution exists. | ||
| */ | ||
| public static long discreteLog(long a, long b, long m) { | ||
| a %= m; | ||
| b %= m; | ||
|
|
||
| if (b == 1) { | ||
| return 0; | ||
| } | ||
|
|
||
| long n = (long) Math.ceil(Math.sqrt(m)); | ||
|
|
||
| Map<Long, Long> babySteps = new HashMap<>(); | ||
|
|
||
| long value = 1; | ||
| for (long i = 0; i < n; i++) { | ||
| babySteps.put(value, i); | ||
| value = value * a % m; // PMD fix | ||
| } | ||
|
|
||
| long factor = modPow(a, m - n - 1, m); | ||
|
|
||
| long gamma = b; | ||
| for (long j = 0; j <= n; j++) { | ||
| if (babySteps.containsKey(gamma)) { | ||
| return j * n + babySteps.get(gamma); | ||
| } | ||
| gamma = gamma * factor % m; // PMD fix | ||
| } | ||
|
|
||
| return -1; // no solution | ||
| } | ||
|
|
||
| /** Fast modular exponentiation */ | ||
| public static long modPow(long base, long exp, long mod) { | ||
| long result = 1; | ||
| base %= mod; | ||
|
|
||
| while (exp > 0) { | ||
| if ((exp & 1) == 1) { | ||
| result = result * base % mod; // PMD fix | ||
| } | ||
| base = base * base % mod; // PMD fix | ||
| exp >>= 1; | ||
| } | ||
| return result; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/thealgorithms/maths/DiscreteLogarithmBSGSTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class DiscreteLogarithmBSGSTest { | ||
|
|
||
| @Test | ||
| public void testSmallValues() { | ||
| assertEquals(3, DiscreteLogarithmBSGS.discreteLog(2, 8, 13)); | ||
| assertEquals(4, DiscreteLogarithmBSGS.discreteLog(3, 81, 1000000007)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoSolution() { | ||
| // choose a true NO-SOLUTION example: | ||
| // modulo 15, base 4 generates only {1,4} | ||
| assertEquals(-1, DiscreteLogarithmBSGS.discreteLog(4, 2, 15)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLargeMod() { | ||
| // use a valid solvable case | ||
| long x = DiscreteLogarithmBSGS.discreteLog(5, 5, 1000003); | ||
| assertEquals(1, x); | ||
| } | ||
|
|
||
| @Test | ||
| public void testImmediateReturnCase() { | ||
| // because a^0 ≡ 1 (mod m) | ||
| assertEquals(0, DiscreteLogarithmBSGS.discreteLog(7, 1, 13)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testModPowBranchCoverage() { | ||
| assertEquals(1, DiscreteLogarithmBSGS.modPow(10, 0, 17)); // exp=0 branch | ||
| assertEquals(10 % 17, DiscreteLogarithmBSGS.modPow(10, 1, 17)); // odd exp branch | ||
| assertEquals(100 % 17, DiscreteLogarithmBSGS.modPow(10, 2, 17)); // even exp branch | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a test case with m being not prime. E.g. a=2 b=8 m=9 => x=3 ? @rajucreate