Skip to content

Commit

Permalink
Rename is_prime_miller_rabin() to miller_rabin_primality_test()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Jul 1, 2021
1 parent b9ed28f commit 0da1449
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/api/primes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Specific primality tests
:toctree:

fermat_primality_test
is_prime_miller_rabin
miller_rabin_primality_test
16 changes: 8 additions & 8 deletions galois/prime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

__all__ = [
"primes", "kth_prime", "prev_prime", "next_prime", "random_prime", "mersenne_exponents", "mersenne_primes",
"is_prime", "is_composite", "fermat_primality_test", "is_prime_miller_rabin"
"is_prime", "is_composite", "fermat_primality_test", "miller_rabin_primality_test"
]


Expand Down Expand Up @@ -292,7 +292,7 @@ def is_prime(n):
This algorithm will first run Fermat's primality test to check :math:`n` for compositeness, see
:obj:`galois.fermat_primality_test`. If it determines :math:`n` is composite, the function will quickly return.
If Fermat's primality test returns `True`, then :math:`n` could be prime or pseudoprime. If so, then the algorithm
will run seven rounds of Miller-Rabin's primality test, see :obj:`galois.is_prime_miller_rabin`. With this many rounds,
will run seven rounds of Miller-Rabin's primality test, see :obj:`galois.miller_rabin_primality_test`. With this many rounds,
a result of `True` should have high probability of :math:`n` being a true prime, not a pseudoprime.
Parameters
Expand Down Expand Up @@ -332,7 +332,7 @@ def is_prime(n):
# If the test returns False, then n is definitely composite
return False

if not is_prime_miller_rabin(n, rounds=7):
if not miller_rabin_primality_test(n, rounds=7):
# If the test returns False, then n is definitely composite
return False

Expand Down Expand Up @@ -432,7 +432,7 @@ def fermat_primality_test(n):


@set_module("galois")
def is_prime_miller_rabin(n, a=None, rounds=1):
def miller_rabin_primality_test(n, a=None, rounds=1):
"""
Determines if :math:`n` is composite.
Expand Down Expand Up @@ -470,7 +470,7 @@ def is_prime_miller_rabin(n, a=None, rounds=1):
primes = [257, 24841, 65497]
for prime in primes:
is_prime = galois.is_prime_miller_rabin(prime)
is_prime = galois.miller_rabin_primality_test(prime)
p, e = galois.factors(prime)
print("Prime = {:5d}, Miller-Rabin Prime Test = {}, Prime factors = {}".format(prime, is_prime, list(p)))
Expand All @@ -479,13 +479,13 @@ def is_prime_miller_rabin(n, a=None, rounds=1):
# Single round of Miller-Rabin, sometimes fooled by pseudoprimes
for pseudoprime in pseudoprimes:
is_prime = galois.is_prime_miller_rabin(pseudoprime)
is_prime = galois.miller_rabin_primality_test(pseudoprime)
p, e = galois.factors(pseudoprime)
print("Pseudoprime = {:5d}, Miller-Rabin Prime Test = {}, Prime factors = {}".format(pseudoprime, is_prime, list(p)))
# 7 rounds of Miller-Rabin, never fooled by pseudoprimes
for pseudoprime in pseudoprimes:
is_prime = galois.is_prime_miller_rabin(pseudoprime, rounds=7)
is_prime = galois.miller_rabin_primality_test(pseudoprime, rounds=7)
p, e = galois.factors(pseudoprime)
print("Pseudoprime = {:5d}, Miller-Rabin Prime Test = {}, Prime factors = {}".format(pseudoprime, is_prime, list(p)))
"""
Expand Down Expand Up @@ -525,7 +525,7 @@ def is_prime_miller_rabin(n, a=None, rounds=1):
rounds -= 1
if rounds > 0:
# On subsequent rounds, don't specify a -- we want new, different a's
return is_prime_miller_rabin(n, rounds=rounds)
return miller_rabin_primality_test(n, rounds=rounds)
else:
# n might be a prime
return True
6 changes: 3 additions & 3 deletions tests/test_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ def test_fermat_primality_test_on_pseudoprimes():
assert galois.fermat_primality_test(pseudoprime) == True


def test_is_prime_miller_rabin_on_primes():
def test_miller_rabin_primality_test_on_primes():
primes = random.choices(galois.prime.PRIMES, k=10)
for prime in primes:
# Miller-Rabin's primality test should never call a prime a composite
assert galois.fermat_primality_test(prime) == True


def test_is_prime_miller_rabin_on_pseudoprimes():
def test_miller_rabin_primality_test_on_pseudoprimes():
# https://oeis.org/A001262
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]
for pseudoprime in pseudoprimes:
# With sufficient rounds, the Miller-Rabin primality test will detect the pseudoprimes as composite
assert galois.is_prime_miller_rabin(pseudoprime, rounds=7) == False
assert galois.miller_rabin_primality_test(pseudoprime, rounds=7) == False

0 comments on commit 0da1449

Please sign in to comment.