public final class NumberTheory
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.math.BigInteger |
ONE
BigInteger constant 1
|
static java.math.BigInteger |
TWO
BigInteger constant 2
|
static java.math.BigInteger |
ZERO
BigInteger constant 0
|
| Modifier and Type | Method and Description |
|---|---|
static int[] |
extGcd(int a,
int b)
Extended Euclidean algorithm for computing the greatest
common divisor of two integers.
|
static int |
gcd(int a,
int b)
Euclidean algorithm for computing the greatest common divisor
of two integers.
|
static java.math.BigInteger |
getStrongPrime(int x,
java.util.Random random)
Returns a random strong prime.
|
static boolean |
hasSmallFactors(java.math.BigInteger b)
Test the given BigInteger b for small prime factors.
|
static boolean |
isProbablePrime(java.math.BigInteger b)
Perform a probabilistic primality test to determine
if b is prime.
|
static boolean |
millerRabin(java.math.BigInteger n)
Perform the Miller-Rabin probabilistic primality test to determine
if b is prime.
|
static java.math.BigInteger |
nextPrime(java.math.BigInteger b)
Return the smallest prime greater than or equal to b.
|
public static final java.math.BigInteger ZERO
public static final java.math.BigInteger ONE
public static final java.math.BigInteger TWO
public static int gcd(int a,
int b)
public static int[] extGcd(int a,
int b)
public static java.math.BigInteger nextPrime(java.math.BigInteger b)
public static java.math.BigInteger getStrongPrime(int x,
java.util.Random random)
This method implements an algorithm published in RSA LABORATORIES' CryptoBytes, Volume 3, No. 1 - Spring 1997 (Acrobat .PDF, 285k), page 9.
x - the strength of the prime numberrandom - the random number generator to usepublic static boolean isProbablePrime(java.math.BigInteger b)
This method can be used as drop-in replacement for the BigInteger.isProbablePrime() method offering much better performance. This is achieved by first filtering out number divisable by small primes and then using the Miller-Rabin test with tighter bounds.
This method only works for positive numbers.
public static boolean millerRabin(java.math.BigInteger n)
This is basically the same algorithm as BigInteger.isProbablePrime() but uses tighter bounds for 2-80 based on the length of the number to achieve significantly better average performance.
As implemented here the algorithm was adapted from the Handbook of Applied Cryptography by Menezes, van Oorscho, and Vanstone. This method should only be used to test large positive numbers.
public static boolean hasSmallFactors(java.math.BigInteger b)