Skip to content

Commit 91845a5

Browse files
committed
Add BSGS algorithm to DIRECTORY.md
1 parent 1c6026e commit 91845a5

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@
445445
- 📄 [ConvolutionFFT](src/main/java/com/thealgorithms/maths/ConvolutionFFT.java)
446446
- 📄 [CrossCorrelation](src/main/java/com/thealgorithms/maths/CrossCorrelation.java)
447447
- 📄 [DeterminantOfMatrix](src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java)
448+
- [DiscreteLogarithmBSGS](src/main/java/com/thealgorithms/maths/DiscreteLogarithmBSGS.java)
448449
- 📄 [DigitalRoot](src/main/java/com/thealgorithms/maths/DigitalRoot.java)
449450
- 📄 [DistanceFormula](src/main/java/com/thealgorithms/maths/DistanceFormula.java)
450451
- 📄 [DudeneyNumber](src/main/java/com/thealgorithms/maths/DudeneyNumber.java)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Baby-Step Giant-Step algorithm for the Discrete Logarithm Problem.
8+
*
9+
* <p>
10+
* Solves for x in: a^x ≡ b (mod m),
11+
* where a, b, m are given, and m is prime (or a has order in modulo m).
12+
*
13+
* <p>
14+
* Time complexity: O(√m)
15+
* Space complexity: O(√m)
16+
*/
17+
public class DiscreteLogarithmBSGS {
18+
19+
/**
20+
* Computes x such that (a^x) % m == b.
21+
* Returns -1 if no solution exists.
22+
*/
23+
public static long discreteLog(long a, long b, long m) {
24+
a %= m;
25+
b %= m;
26+
27+
if (b == 1) {
28+
return 0;
29+
}
30+
31+
long n = (long) Math.ceil(Math.sqrt(m));
32+
33+
Map<Long, Long> babySteps = new HashMap<>();
34+
35+
long value = 1;
36+
for (long i = 0; i < n; i++) {
37+
babySteps.put(value, i);
38+
value = (value * a) % m;
39+
}
40+
41+
long factor = modPow(a, m - n - 1, m);
42+
43+
long gamma = b;
44+
for (long j = 0; j <= n; j++) {
45+
if (babySteps.containsKey(gamma)) {
46+
return j * n + babySteps.get(gamma);
47+
}
48+
gamma = (gamma * factor) % m;
49+
}
50+
51+
return -1; // no solution
52+
}
53+
54+
/** Fast modular exponentiation */
55+
public static long modPow(long base, long exp, long mod) {
56+
long result = 1;
57+
base %= mod;
58+
59+
while (exp > 0) {
60+
if ((exp & 1) == 1) {
61+
result = (result * base) % mod;
62+
}
63+
base = (base * base) % mod;
64+
exp >>= 1;
65+
}
66+
return result;
67+
}
68+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class DiscreteLogarithmBSGSTest {
8+
9+
@Test
10+
public void testSmallValues() {
11+
assertEquals(3, DiscreteLogarithmBSGS.discreteLog(2, 8, 13));
12+
assertEquals(4, DiscreteLogarithmBSGS.discreteLog(3, 81, 1000000007));
13+
}
14+
15+
@Test
16+
public void testNoSolution() {
17+
assertEquals(-1, DiscreteLogarithmBSGS.discreteLog(10, 5, 17));
18+
}
19+
20+
@Test
21+
public void testLargeMod() {
22+
long x = DiscreteLogarithmBSGS.discreteLog(5, 243, 1000003);
23+
assertEquals(5, x);
24+
}
25+
}

0 commit comments

Comments
 (0)