Top Description Inners Fields Constructors Methods
org.bouncycastle.math

public abstract Class Primes

extends Object
Class Inheritance
Imports
java.math.BigInteger, java.security.SecureRandom, org.bouncycastle.crypto.Digest, org.bouncycastle.util.Arrays, .BigIntegers

Utility methods for generating primes and testing for primality.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class
Primes.STOutput

Used to return the output from the Shawe-Taylor Random_Prime Routine

Field Summary

Modifier and TypeField and Description
private static final BigInteger
public static final int
private static final BigInteger
private static final BigInteger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
public static Primes.MROutput

Returns:

an MROutput instance that can be further queried for details.
enhancedMRProbablePrimeTest
(BigInteger
the BigInteger instance to test for primality.
candidate
,
SecureRandom
the source of randomness to use to choose bases.
random
,
int
the number of randomly-chosen bases to perform the test for.
iterations
)

FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test Run several iterations of the Miller-Rabin algorithm with randomly-chosen bases.

private static int
extract32(byte[] bs)

public static Primes.STOutput

Returns:

an STOutput instance containing the requested prime.
generateSTRandomPrime
(Digest
the Digest instance to use (as "Hash()"). Cannot be null.
hash
,
int
the length (in bits) of the prime to be generated. Must be at least 2.
length
,
byte[]
the seed to be used for the generation of the requested prime. Cannot be null or empty.
inputSeed
)

FIPS 186-4 C.6 Shawe-Taylor Random_Prime Routine Construct a provable prime number using a hash function.

public static boolean

Returns:

true if the candidate is found to have any small factors, false otherwise.
hasAnySmallFactors
(BigInteger
the BigInteger instance to test for division by small factors.
candidate
)

A fast check for small divisors, up to some implementation-specific limit.

private static void
hash(Digest d, byte[] input, byte[] output, int outPos)

private static BigInteger
hashGen(Digest d, byte[] seed, int count)

private static boolean
private static boolean
private static Primes.STOutput
implSTRandomPrime(Digest d, int length, byte[] primeSeed)

private static void
inc(byte[] seed, int c)

public static boolean

Returns:

false if any witness to compositeness is found amongst the chosen bases (so candidate is definitely NOT prime), or else true (indicating primality with some probability dependent on the number of iterations that were performed).
isMRProbablePrime
(BigInteger
the BigInteger instance to test for primality.
candidate
,
SecureRandom
the source of randomness to use to choose bases.
random
,
int
the number of randomly-chosen bases to perform the test for.
iterations
)

FIPS 186-4 C.3.1 Miller-Rabin Probabilistic Primality Test Run several iterations of the Miller-Rabin algorithm with randomly-chosen bases.

public static boolean

Returns:

false if the specified base is a witness to compositeness (so candidate is definitely NOT prime), or else true.
isMRProbablePrimeToBase
(BigInteger
the BigInteger instance to test for primality.
candidate
,
BigInteger
the base value to use for this iteration.
base
)

FIPS 186-4 C.3.1 Miller-Rabin Probabilistic Primality Test (to a fixed base).

private static boolean
isPrime32(long x)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ONEback to summary
private static final BigInteger ONE
SMALL_FACTOR_LIMITback to summary
public static final int SMALL_FACTOR_LIMIT
THREEback to summary
private static final BigInteger THREE
TWOback to summary
private static final BigInteger TWO

Constructor Detail

Primesback to summary
public Primes()

Method Detail

checkCandidateback to summary
private static void checkCandidate(BigInteger n, String name)
enhancedMRProbablePrimeTestback to summary
public static Primes.MROutput enhancedMRProbablePrimeTest(BigInteger candidate, SecureRandom random, int iterations)

FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test Run several iterations of the Miller-Rabin algorithm with randomly-chosen bases. This is an alternative to isMRProbablePrime(BigInteger, SecureRandom, int) that provides more information about a composite candidate, which may be useful when generating or validating RSA moduli.

Parameters
candidate:BigInteger

the BigInteger instance to test for primality.

random:SecureRandom

the source of randomness to use to choose bases.

iterations:int

the number of randomly-chosen bases to perform the test for.

Returns:Primes.MROutput

an MROutput instance that can be further queried for details.

extract32back to summary
private static int extract32(byte[] bs)
generateSTRandomPrimeback to summary
public static Primes.STOutput generateSTRandomPrime(Digest hash, int length, byte[] inputSeed)

FIPS 186-4 C.6 Shawe-Taylor Random_Prime Routine Construct a provable prime number using a hash function.

Parameters
hash:Digest

the Digest instance to use (as "Hash()"). Cannot be null.

length:int

the length (in bits) of the prime to be generated. Must be at least 2.

inputSeed:byte[]

the seed to be used for the generation of the requested prime. Cannot be null or empty.

Returns:Primes.STOutput

an STOutput instance containing the requested prime.

hasAnySmallFactorsback to summary
public static boolean hasAnySmallFactors(BigInteger candidate)

A fast check for small divisors, up to some implementation-specific limit.

Parameters
candidate:BigInteger

the BigInteger instance to test for division by small factors.

Returns:boolean

true if the candidate is found to have any small factors, false otherwise.

hashback to summary
private static void hash(Digest d, byte[] input, byte[] output, int outPos)
hashGenback to summary
private static BigInteger hashGen(Digest d, byte[] seed, int count)
implHasAnySmallFactorsback to summary
private static boolean implHasAnySmallFactors(BigInteger x)
implMRProbablePrimeToBaseback to summary
private static boolean implMRProbablePrimeToBase(BigInteger w, BigInteger wSubOne, BigInteger m, int a, BigInteger b)
implSTRandomPrimeback to summary
private static Primes.STOutput implSTRandomPrime(Digest d, int length, byte[] primeSeed)
incback to summary
private static void inc(byte[] seed, int c)
isMRProbablePrimeback to summary
public static boolean isMRProbablePrime(BigInteger candidate, SecureRandom random, int iterations)

FIPS 186-4 C.3.1 Miller-Rabin Probabilistic Primality Test Run several iterations of the Miller-Rabin algorithm with randomly-chosen bases.

Parameters
candidate:BigInteger

the BigInteger instance to test for primality.

random:SecureRandom

the source of randomness to use to choose bases.

iterations:int

the number of randomly-chosen bases to perform the test for.

Returns:boolean

false if any witness to compositeness is found amongst the chosen bases (so candidate is definitely NOT prime), or else true (indicating primality with some probability dependent on the number of iterations that were performed).

isMRProbablePrimeToBaseback to summary
public static boolean isMRProbablePrimeToBase(BigInteger candidate, BigInteger base)

FIPS 186-4 C.3.1 Miller-Rabin Probabilistic Primality Test (to a fixed base). Run a single iteration of the Miller-Rabin algorithm against the specified base.

Parameters
candidate:BigInteger

the BigInteger instance to test for primality.

base:BigInteger

the base value to use for this iteration.

Returns:boolean

false if the specified base is a witness to compositeness (so candidate is definitely NOT prime), or else true.

isPrime32back to summary
private static boolean isPrime32(long x)
org.bouncycastle.math back to summary

public Class Primes.MROutput

extends Object
Class Inheritance

Used to return the output from the Enhanced Miller-Rabin Probabilistic Primality Test

Field Summary

Modifier and TypeField and Description
private BigInteger
private boolean

Constructor Summary

AccessConstructor and Description
private
MROutput(boolean provablyComposite, BigInteger factor)

Method Summary

Modifier and TypeMethod and Description
public BigInteger
public boolean
public boolean
private static Primes.MROutput
private static Primes.MROutput
private static Primes.MROutput
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

factorback to summary
private BigInteger factor
provablyCompositeback to summary
private boolean provablyComposite

Constructor Detail

MROutputback to summary
private MROutput(boolean provablyComposite, BigInteger factor)

Method Detail

getFactorback to summary
public BigInteger getFactor()
isNotPrimePowerback to summary
public boolean isNotPrimePower()
isProvablyCompositeback to summary
public boolean isProvablyComposite()
probablyPrimeback to summary
private static Primes.MROutput probablyPrime()
provablyCompositeNotPrimePowerback to summary
private static Primes.MROutput provablyCompositeNotPrimePower()
provablyCompositeWithFactorback to summary
private static Primes.MROutput provablyCompositeWithFactor(BigInteger factor)
org.bouncycastle.math back to summary

public Class Primes.STOutput

extends Object
Class Inheritance

Used to return the output from the Shawe-Taylor Random_Prime Routine

Field Summary

Modifier and TypeField and Description
private BigInteger
private int
private byte[]

Constructor Summary

AccessConstructor and Description
private
STOutput(BigInteger prime, byte[] primeSeed, int primeGenCounter)

Method Summary

Modifier and TypeMethod and Description
public BigInteger
public int
public byte[]
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

primeback to summary
private BigInteger prime
primeGenCounterback to summary
private int primeGenCounter
primeSeedback to summary
private byte[] primeSeed

Constructor Detail

STOutputback to summary
private STOutput(BigInteger prime, byte[] primeSeed, int primeGenCounter)

Method Detail

getPrimeback to summary
public BigInteger getPrime()
getPrimeGenCounterback to summary
public int getPrimeGenCounter()
getPrimeSeedback to summary
public byte[] getPrimeSeed()