diff --git a/src/main/java/com/williamfiset/algorithms/math/ModPow.java b/src/main/java/com/williamfiset/algorithms/math/ModPow.java index 72163afe4..203e20655 100644 --- a/src/main/java/com/williamfiset/algorithms/math/ModPow.java +++ b/src/main/java/com/williamfiset/algorithms/math/ModPow.java @@ -25,31 +25,36 @@ public static long modPow(long a, long n, long mod) { if (n < 0) { if (gcd(a, mod) != 1) throw new ArithmeticException("If n < 0 then must have gcd(a, mod) = 1"); - return modPow(modInv(a, mod), -n, mod); + a = modInv(a, mod); + n = -n; } - // Normalize base into [0, mod) - a = ((a % mod) + mod) % mod; + // Overflow-safe normalization of base into [0, mod) + a %= mod; + if (a < 0) + a += mod; - long result = 1; - while (n > 0) { + long result = 1 % mod; + while (n != 0) { if ((n & 1) == 1) result = mulMod(result, a, mod); a = mulMod(a, a, mod); - n >>= 1; + n >>>= 1; } return result; } private static long modInv(long a, long m) { - a = ((a % m) + m) % m; - long x = egcd(a, m)[1]; - return ((x % m) + m) % m; + a %= m; + if (a < 0) + a += m; + long x = egcd(a, m)[1] % m; + return x < 0 ? x + m : x; } private static long[] egcd(long a, long b) { if (b == 0) - return new long[] {a < 0 ? -a : a, 1L, 0L}; + return new long[] { a < 0 ? -a : a, 1L, 0L }; long[] v = egcd(b, a % b); long tmp = v[1] - v[2] * (a / b); v[1] = v[2]; @@ -58,9 +63,7 @@ private static long[] egcd(long a, long b) { } private static long gcd(long a, long b) { - a = Math.abs(a); - b = Math.abs(b); - return b == 0 ? a : gcd(b, a % b); + return b == 0 ? Math.abs(a) : gcd(b, a % b); } /** Overflow-safe modular multiplication: (a * b) % mod. */ @@ -70,4 +73,4 @@ private static long mulMod(long a, long b, long mod) { .mod(java.math.BigInteger.valueOf(mod)) .longValue(); } -} +} \ No newline at end of file diff --git a/src/test/java/com/williamfiset/algorithms/math/ModPowTest.java b/src/test/java/com/williamfiset/algorithms/math/ModPowTest.java index d69e9ad3d..a002fabe5 100644 --- a/src/test/java/com/williamfiset/algorithms/math/ModPowTest.java +++ b/src/test/java/com/williamfiset/algorithms/math/ModPowTest.java @@ -18,30 +18,27 @@ public void basicPositiveExponent() { @Test public void negativeBase() { // (-45)^12345 mod 987654321 - long expected = - BigInteger.valueOf(-45) - .modPow(BigInteger.valueOf(12345), BigInteger.valueOf(987654321)) - .longValue(); + long expected = BigInteger.valueOf(-45) + .modPow(BigInteger.valueOf(12345), BigInteger.valueOf(987654321)) + .longValue(); assertThat(ModPow.modPow(-45, 12345, 987654321)).isEqualTo(expected); } @Test public void negativeExponent() { // 6^-66 mod 101 = 84 - long expected = - BigInteger.valueOf(6) - .modPow(BigInteger.valueOf(-66), BigInteger.valueOf(101)) - .longValue(); + long expected = BigInteger.valueOf(6) + .modPow(BigInteger.valueOf(-66), BigInteger.valueOf(101)) + .longValue(); assertThat(ModPow.modPow(6, -66, 101)).isEqualTo(expected); } @Test public void negativeBaseAndExponent() { // (-5)^-7 mod 1009 - long expected = - BigInteger.valueOf(-5) - .modPow(BigInteger.valueOf(-7), BigInteger.valueOf(1009)) - .longValue(); + long expected = BigInteger.valueOf(-5) + .modPow(BigInteger.valueOf(-7), BigInteger.valueOf(1009)) + .longValue(); assertThat(ModPow.modPow(-5, -7, 1009)).isEqualTo(expected); } @@ -68,8 +65,7 @@ public void largeValues() { long a = 1_000_000_000L; long n = 1_000_000_000L; long mod = 999_999_937L; - long expected = - BigInteger.valueOf(a).modPow(BigInteger.valueOf(n), BigInteger.valueOf(mod)).longValue(); + long expected = BigInteger.valueOf(a).modPow(BigInteger.valueOf(n), BigInteger.valueOf(mod)).longValue(); assertThat(ModPow.modPow(a, n, mod)).isEqualTo(expected); } @@ -92,9 +88,85 @@ public void matchesBigIntegerRandomized() { long a = rng.nextLong(-1_000_000_000L, 1_000_000_000L); long n = rng.nextLong(0, 1_000_000_000L); long mod = rng.nextLong(1, 1_000_000_000L); - long expected = - BigInteger.valueOf(a).modPow(BigInteger.valueOf(n), BigInteger.valueOf(mod)).longValue(); + long expected = BigInteger.valueOf(a).modPow(BigInteger.valueOf(n), BigInteger.valueOf(mod)).longValue(); assertThat(ModPow.modPow(a, n, mod)).isEqualTo(expected); } } + + @Test + public void modOneWithZeroExponent() { + // 1 % mod must be used as the starting result, not 1 + assertThat(ModPow.modPow(5, 0, 1)).isEqualTo(0); + } + + @Test + public void exponentLongMinValue() { + // -Long.MIN_VALUE overflows; negative exponent needs coprime base + long mod = 1_000_000_007L; + long expected = BigInteger.valueOf(3) + .modPow(BigInteger.valueOf(Long.MIN_VALUE), BigInteger.valueOf(mod)) + .longValue(); + assertThat(ModPow.modPow(3, Long.MIN_VALUE, mod)).isEqualTo(expected); + } + + @Test + public void exponentLongMaxValue() { + long mod = 1_000_000_007L; + long expected = BigInteger.valueOf(3) + .modPow(BigInteger.valueOf(Long.MAX_VALUE), BigInteger.valueOf(mod)) + .longValue(); + assertThat(ModPow.modPow(3, Long.MAX_VALUE, mod)).isEqualTo(expected); + } + + @Test + public void hugeModulus() { + // mod > 2^62: (a % mod) + mod used to overflow + long mod = Long.MAX_VALUE; + long[] bases = { -5, -(Long.MAX_VALUE - 5), Long.MIN_VALUE, 3, Long.MAX_VALUE - 1 }; + for (long a : bases) { + long expected = BigInteger.valueOf(a) + .modPow(BigInteger.valueOf(12345), BigInteger.valueOf(mod)) + .longValue(); + assertThat(ModPow.modPow(a, 12345, mod)).isEqualTo(expected); + } + } + + @Test + public void hugeModulusNegativeExponent() { + long mod = Long.MAX_VALUE; + long expected = BigInteger.valueOf(-3) + .modPow(BigInteger.valueOf(-777), BigInteger.valueOf(mod)) + .longValue(); + assertThat(ModPow.modPow(-3, -777, mod)).isEqualTo(expected); + } + + @Test + public void matchesBigIntegerRandomizedFullRange() { + ThreadLocalRandom rng = ThreadLocalRandom.current(); + for (int i = 0; i < 500; i++) { + long a = rng.nextLong(); + long n = rng.nextLong(0, Long.MAX_VALUE); + long mod = rng.nextLong(1, Long.MAX_VALUE); + long expected = BigInteger.valueOf(a).modPow(BigInteger.valueOf(n), BigInteger.valueOf(mod)).longValue(); + assertThat(ModPow.modPow(a, n, mod)).isEqualTo(expected); + } + } + + @Test + public void matchesBigIntegerRandomizedNegativeExponent() { + ThreadLocalRandom rng = ThreadLocalRandom.current(); + int tested = 0; + while (tested < 300) { + long a = rng.nextLong(); + long n = rng.nextLong(Long.MIN_VALUE, 0); + long mod = rng.nextLong(1, Long.MAX_VALUE); + BigInteger bigA = BigInteger.valueOf(a); + BigInteger bigMod = BigInteger.valueOf(mod); + if (!bigA.gcd(bigMod).equals(BigInteger.ONE)) + continue; // no inverse, skip + long expected = bigA.modPow(BigInteger.valueOf(n), bigMod).longValue(); + assertThat(ModPow.modPow(a, n, mod)).isEqualTo(expected); + tested++; + } + } }