Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/galois/_domains/_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def lookup(a: int, b: int) -> int: # pragma: no cover
return 0

m = LOG[a]
return EXP[(m * b) % (ORDER - 1)] # TODO: Do b % (ORDER - 1) first? b could be very large and overflow int64
return EXP[(m * (b % (ORDER - 1))) % (ORDER - 1)]


class log_ufunc(_ufunc.log_ufunc):
Expand Down
9 changes: 6 additions & 3 deletions src/galois/_prime.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,10 @@ def miller_rabin_primality_test(n: int, a: int = 2, rounds: int = 1) -> bool:
r, s = r // 2, s + 1
assert 2**s * r == n - 1

for t in range(rounds):
initial_a = a
bases = (p for p in PRIMES if p != initial_a)

for _ in range(rounds):
y = pow(a, r, n)
if y not in [1, n - 1]:
j = 0
Expand All @@ -611,7 +614,7 @@ def miller_rabin_primality_test(n: int, a: int = 2, rounds: int = 1) -> bool:
if y != n - 1:
return False # a is a strong witness to the compositness of n

a = PRIMES[t]
a = next(bases)

return True # n is a probable prime

Expand Down Expand Up @@ -999,7 +1002,7 @@ def _adjust_base_and_exponent(n: int, base: int, exponent: int) -> tuple[int, in
# Try to convert the even exponent of a factored negative number into the next largest odd power
while exponent > 2:
if exponent % 2 == 0:
base *= 2
base **= 2
exponent //= 2
else:
return -base, exponent
Expand Down
23 changes: 23 additions & 0 deletions tests/fields/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,29 @@ def test_square(field_power):
assert z.dtype == dtype


def test_power_large_exponent():
"""
Regression test for int64 overflow in lookup-table exponentiation.

In the lookup power function, the index is computed as (m * b) % (ORDER - 1) where
m = LOG[a]. When b is large, m * b can overflow int64 before the modulo is applied,
producing a wrong result. The correct computation reduces b first:
(m * (b % (ORDER - 1))) % (ORDER - 1).
"""
GF = galois.GF(2**8, compile="jit-lookup") # Small field with lookup tables
assert GF.ufunc_mode == "jit-lookup"

# Pick an element whose discrete log is large to maximize the m * b product.
# LOG[primitive_element] = 1, so alpha^254 has LOG value 254 (= ORDER - 2).
x = GF.primitive_element**254

# Exponent fits in int64, but m * b = 254 * 10^17 ≈ 2.5e19 overflows int64 max ≈ 9.2e18
big_exp = 10**17
small_exp = big_exp % (GF.order - 1) # Equivalent exponent, no overflow risk

assert x**big_exp == x**small_exp


def test_log(field_log):
GF, X, Z = field_log["GF"], field_log["X"], field_log["Z"]
dtype = random.choice(GF.dtypes)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def test_perfect_power():
assert galois.perfect_power(125) == (5, 3)
assert galois.perfect_power(216) == (6, 3)

# Negative integers with odd exponents
assert galois.perfect_power(-64) == (-4, 3)
assert galois.perfect_power(-729) == (-9, 3)


def test_trial_division():
n = 2**4 * 17**3 * 113 * 15013
Expand Down
14 changes: 14 additions & 0 deletions tests/test_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,17 @@ def test_miller_rabin_primality_test():
pseudoprimes = [2047, 3277, 4033, 4681, 8321, 15841, 29341, 42799, 49141, 52633, 65281, 74665, 80581, 85489, 88357, 90751, 104653, 130561, 196093, 220729, 233017, 252601, 253241, 256999, 271951, 280601, 314821, 357761, 390937, 458989, 476971, 486737] # fmt: skip
assert [galois.miller_rabin_primality_test(p, a=2) for p in pseudoprimes] == [True] * len(pseudoprimes)
assert [galois.miller_rabin_primality_test(p, a=3) for p in pseudoprimes] == [False] * len(pseudoprimes)


def test_miller_rabin_primality_test_rounds_use_distinct_bases():
"""
Regression test: with default a=2, the second round should test base 3 (the next prime),
not base 2 again.

2047 = 23 * 89 is a strong pseudoprime to base 2 but base 3 is a witness.
With rounds=2 and default a=2, the function should catch 2047 on the second round
(testing base 3), but the bug causes it to test base 2 twice and miss it.
"""
assert galois.miller_rabin_primality_test(2047, a=2, rounds=1) # base 2 is a strong liar
assert not galois.miller_rabin_primality_test(2047, a=3, rounds=1) # base 3 is a witness
assert not galois.miller_rabin_primality_test(2047, a=2, rounds=2) # should use bases {2, 3}
Loading