iaik.utils
Class NumberTheory

java.lang.Object
  |
  +--iaik.utils.NumberTheory

public final class NumberTheory
extends Object

Some useful number theoretic utility methods.

Version:
File Revision 12

Field Summary
static BigInteger ONE
          BigInteger constant 1
static BigInteger TWO
          BigInteger constant 2
static BigInteger ZERO
          BigInteger constant 0
 
Method Summary
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 BigInteger getStrongPrime(int x, Random random)
          Returns a random strong prime.
static boolean hasSmallFactors(BigInteger b)
          Test the given BigInteger b for small prime factors.
static boolean isProbablePrime(BigInteger b)
          Perform a probabilistic primality test to determine if b is prime.
static boolean millerRabin(BigInteger n)
          Perform the Miller-Rabin probabilistic primality test to determine if b is prime.
static BigInteger nextPrime(BigInteger b)
          Return the smallest prime greater than or equal to b.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZERO

public static final BigInteger ZERO
BigInteger constant 0

ONE

public static final BigInteger ONE
BigInteger constant 1

TWO

public static final BigInteger TWO
BigInteger constant 2
Method Detail

gcd

public static int gcd(int a,
                      int b)
Euclidean algorithm for computing the greatest common divisor of two integers.

extGcd

public static int[] extGcd(int a,
                           int b)
Extended Euclidean algorithm for computing the greatest common divisor of two integers.
Returns:
{d,x,y} satisfying ax + by = d

nextPrime

public static BigInteger nextPrime(BigInteger b)
Return the smallest prime greater than or equal to b.

getStrongPrime

public static BigInteger getStrongPrime(int x,
                                        Random random)
Returns a random strong prime.

This method implements an algorithm published in RSA LABORATORIES' CryptoBytes, Volume 3, Number 1 - Spring 1997, page 9.

Parameters:
x - the strength of the prime number
random - the random number generator to use
Returns:
the new generated random strong prime

isProbablePrime

public static boolean isProbablePrime(BigInteger b)
Perform a probabilistic primality test to determine if b is prime. This method returns true if b is probable prime and false if it is definitely composite. The probability of returning true if b is composite is fixed at 2-80 for this implementation. This bound is a reasonable choice for most cryptographic operations.

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.

See Also:
hasSmallFactors(java.math.BigInteger), millerRabin(java.math.BigInteger)

millerRabin

public static boolean millerRabin(BigInteger n)
Perform the Miller-Rabin probabilistic primality test to determine if b is prime. This method returns true if b is probable prime and false if it is definitely composite. The probability of returning true if b is composite is fixed at 2-80 for this implementation. This bound is a reasonable choice for most cryptographic operations.

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.


hasSmallFactors

public static boolean hasSmallFactors(BigInteger b)
Test the given BigInteger b for small prime factors. In other words, if this method returns true, the number is composite, if it returns false, it may be a prime. This method should not be used directly, use isProbablePrime() instead. Also note that it may not work for very small numbers.

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK