For the latest news and information visit The GNU Crypto project | ||
Prev Class | Next Class | Frames | No Frames | |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
gnu.crypto.util.Prime
public class Prime
extends java.lang.Object
Method Summary | |
static boolean |
|
static boolean |
|
static boolean |
|
static boolean |
|
static boolean |
|
static boolean |
|
public static boolean hasSmallPrimeDivisor(BigInteger w)
Trial division for the first 1000 small primes. Returnstrue
if at least one small prime, among the first 1000 ones, was found to divide the designated number. Retuensfalse
otherwise.
- Parameters:
w
- the number to test.
- Returns:
true
if at least one small prime was found to divide the designated number.
public static boolean isProbablePrime(BigInteger w)
public static boolean isProbablePrime(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.
- Parameters:
w
- the integer to test.certainty
- the certainty with which to compute the test.
- Returns:
true
iff the designated number has no small prime divisor passes the Euler criterion, and optionally a Miller-Rabin test.
public static boolean passEulerCriterion(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 whenbn == 1 mod 8
and2
is a quartic residue, i.e.bn
is of the forma^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 ifp
is prime thena^((p-1)/2)
is congruent toJacobi(a,p)
, modulop
.Jacobi(a, p)
is a function which is+1
if a is a square modulop
, and-1
if it is not. Fora = 2
, this is particularly simple. It's+1
ifp == +/-1 (mod 8)
, and-1
ifm == +/-3 (mod 8)
. Ifp == 3 (mod 4)
, then all a strong test does is compute2^((p-1)/2)
. and see if it's+1
or-1
. (Euler's criterion says which it should be.) Ifp == 5 (mod 8)
, then2^((p-1)/2)
is-1
, so the initial step in a strong test, looking at2^((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 isp == 1 (mod 8)
. In this case, we expect2^((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 callp
prime as well. Only if the result is+1
, indicating that2
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 at2^((p-1)/8)
if there was a cheap way to determine if2
were expected to be a quartic residue or not. Dirichlet proved that2
is a quadratic residue iffp
is of the forma^2 + (8*b^2)
. All primes== 1 (mod 4)
can be expressed asa^2 + (2*b)^2
, but I see no cheap way to evaluate this condition."
- Parameters:
w
- the number to test.
- Returns:
true
iff the designated number passes Euler criterion as implemented by Colin Plumb in his bnlib version 1.1.
public static boolean passFermatLittleTheorem(BigInteger w, int t)
Checks Fermat's Little Theorem for base b; i.e.b**(w-1) == 1 (mod w)
.
- Parameters:
w
- the number to test.t
- the number of random bases to test.
- Returns:
true
iffb**(w-1) == 1 (mod w)
, for some b.
public static boolean passMillerRabin(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 forq
,n=18
is enough while forp
,n=6
(512 bits) orn=3 (1024 bits) are enough to yield robust primality tests. The values used are from table 4.4 given in Note 4.49.
- Parameters:
- Returns:
true
iff the designated number passes the Miller- Rabin probabilistic primality test for a computed number of rounds.