public class Prime
extends java.lang.Object
A collection of prime number related utilities used in this library.
Modifier and Type | Method and Description |
---|---|
static java.math.BigInteger |
generatePrimeIkiArttirarak(int M) |
static java.math.BigInteger |
generatePrimeIkiArttirarak2(int M,
java.math.BigInteger ilkAsal,
int anahBoy) |
static java.math.BigInteger |
generatePrimeIkiArttirarakSmallPrimeBenden(int M) |
static java.math.BigInteger |
generatePrimeYenidenRastgele(int M) |
static boolean |
hasSmallPrimeDivisor(java.math.BigInteger w)
Trial division for the first 1000 small primes.
|
static boolean |
isProbablePrime(java.math.BigInteger w)
Calls the method with same name and two arguments using the
pre-configured value for
DO_MILLER_RABIN . |
static boolean |
isProbablePrime(java.math.BigInteger w,
boolean doTrialDivision,
boolean doFermat) |
static boolean |
isProbablePrime(java.math.BigInteger w,
int certainty)
This implementation does not rely solely on the Miller-Rabin strong
probabilistic primality test to claim the primality of the designated
number.
|
static boolean |
isProbablePrime(java.math.BigInteger w,
int certainty,
boolean doTrialDivision,
boolean doFermat) |
static java.math.BigInteger |
kBitInteger(int k) |
static void |
krleriYaz() |
static java.math.BigInteger[] |
lucasSequenceElements(java.math.BigInteger aP,
java.math.BigInteger aQ,
java.math.BigInteger aK,
java.math.BigInteger aPrime,
java.math.BigInteger aTwoInverse) |
static java.math.BigInteger |
modPrimeSqrt(java.math.BigInteger ag,
java.math.BigInteger aPrime) |
static boolean |
passEulerCriterion(java.math.BigInteger w)
Java port of Colin Plumb primality test (Euler Criterion)
implementation for a base of 2 --from bnlib-1.1 release, function
primeTest() in prime.c.
|
static boolean |
passFermatLittleTheorem(java.math.BigInteger w,
int t)
Checks Fermat's Little Theorem for base b; i.e.
|
static boolean |
passMillerRabin(java.math.BigInteger n,
int t)
Applies the Miller-Rabin strong probabilistic primality test.
|
public static java.math.BigInteger kBitInteger(int k)
public static java.math.BigInteger generatePrimeYenidenRastgele(int M)
public static void krleriYaz()
public static java.math.BigInteger generatePrimeIkiArttirarak(int M)
public static java.math.BigInteger generatePrimeIkiArttirarak2(int M, java.math.BigInteger ilkAsal, int anahBoy)
public static java.math.BigInteger generatePrimeIkiArttirarakSmallPrimeBenden(int M)
public static boolean hasSmallPrimeDivisor(java.math.BigInteger w)
Trial division for the first 1000 small primes.
Returns true
if at least one small prime, among the first
1000 ones, was found to divide the designated number. Retuens false
otherwise.
w
- the number to test.true
if at least one small prime was found to divide
the designated number.public static boolean passEulerCriterion(java.math.BigInteger w)
Java port of Colin Plumb primality test (Euler Criterion) implementation for a base of 2 --from bnlib-1.1 release, function primeTest() in prime.c. this is his comments; (bn is our w).
"Now, check that bn is prime. If it passes to the base 2, it's prime beyond all reasonable doubt, and everything else is just gravy, but it gives people warm fuzzies to do it.
This starts with verifying Euler's criterion for a base of 2. This is
the fastest pseudoprimality test that I know of, saving a modular squaring
over a Fermat test, as well as being stronger. 7/8 of the time, it's as
strong as a strong pseudoprimality test, too. (The exception being when
bn == 1 mod 8
and 2
is a quartic residue, i.e.
bn
is of the form a^2 + (8*b)^2
.) The precise
series of tricks used here is not documented anywhere, so here's an
explanation. Euler's criterion states that if p
is prime
then a^((p-1)/2)
is congruent to Jacobi(a,p)
,
modulo p
. Jacobi(a, p)
is a function which is
+1
if a is a square modulo p
, and -1
if it is not. For a = 2
, this is particularly simple. It's
+1
if p == +/-1 (mod 8)
, and -1
if
m == +/-3 (mod 8)
. If p == 3 (mod 4)
, then all
a strong test does is compute 2^((p-1)/2)
. and see if it's
+1
or -1
. (Euler's criterion says which
it should be.) If p == 5 (mod 8)
, then 2^((p-1)/2)
is -1
, so the initial step in a strong test, looking at
2^((p-1)/4)
, is wasted --you're not going to find a
+/-1
before then if it is prime, and it shouldn't
have either of those values if it isn't. So don't bother.
The remaining case is p == 1 (mod 8)
. In this case, we
expect 2^((p-1)/2) == 1 (mod p)
, so we expect that the
square root of this, 2^((p-1)/4)
, will be +/-1 (mod p)
. Evaluating this saves us a modular squaring 1/4 of the time. If
it's -1
, a strong pseudoprimality test would call p
prime as well. Only if the result is +1
, indicating that
2
is not only a quadratic residue, but a quartic one as well,
does a strong pseudoprimality test verify more things than this test does.
Good enough.
We could back that down another step, looking at 2^((p-1)/8)
if there was a cheap way to determine if 2
were expected to
be a quartic residue or not. Dirichlet proved that 2
is a
quadratic residue iff p
is of the form a^2 + (8*b^2)
.
All primes == 1 (mod 4)
can be expressed as a^2 +
(2*b)^2
, but I see no cheap way to evaluate this condition."
w
- the number to test.true
iff the designated number passes Euler criterion
as implemented by Colin Plumb in his bnlib version 1.1.public static boolean passFermatLittleTheorem(java.math.BigInteger w, int t)
Checks Fermat's Little Theorem for base b; i.e.
b**(w-1) == 1 (mod w)
.
w
- the number to test.t
- the number of random bases to test.true
iff b**(w-1) == 1 (mod w)
,
for some b.public static boolean passMillerRabin(java.math.BigInteger n, int t)
Applies the Miller-Rabin strong probabilistic primality test.
The HAC (Handbook of Applied Cryptography), Alfred Menezes & al. Note
4.57 states that for q
, n=18
is enough while
for p
, n=6
(512 bits) or n=3
(1024
bits) are enough to yield robust primality tests. The values used
are from table 4.4 given in Note 4.49.
n
- t
- true
iff the designated number passes the Miller-
Rabin probabilistic primality test for a computed number of rounds.public static boolean isProbablePrime(java.math.BigInteger w)
Calls the method with same name and two arguments using the
pre-configured value for DO_MILLER_RABIN
.
w
- the integer to test.true
iff the designated number has no small prime
divisor passes the Euler criterion, and optionally a Miller-Rabin test.public static boolean isProbablePrime(java.math.BigInteger w, boolean doTrialDivision, boolean doFermat)
public static boolean isProbablePrime(java.math.BigInteger w, int certainty)
This implementation does not rely solely on the Miller-Rabin strong probabilistic primality test to claim the primality of the designated number. It instead, tries dividing the designated number by the first 1000 small primes, and if no divisor was found, invokes a port of Colin Plumb's implementation of the Euler Criterion, then tries the Miller-Rabin test.
w
- the integer to test.certainty
- the certainty with which to compute the test.
already found to be a probable prime, then also do a Miller-Rabin test.true
iff the designated number has no small prime
divisor passes the Euler criterion, and optionally a Miller-Rabin test.public static boolean isProbablePrime(java.math.BigInteger w, int certainty, boolean doTrialDivision, boolean doFermat)
public static java.math.BigInteger modPrimeSqrt(java.math.BigInteger ag, java.math.BigInteger aPrime)
public static java.math.BigInteger[] lucasSequenceElements(java.math.BigInteger aP, java.math.BigInteger aQ, java.math.BigInteger aK, java.math.BigInteger aPrime, java.math.BigInteger aTwoInverse)
Copyright © 2025. All rights reserved.