Java Utililty Methods BigInteger is Prime

List of utility methods to do BigInteger is Prime

Description

The list of methods to do BigInteger is Prime are organized into topic(s).

Method

BigIntegercalculateMPrime(BigInteger n, byte[] message)
Method taken (renamed) from SpongyCastle ECDSASigner class.
if (n.bitLength() > message.length * 8) {
    return new BigInteger(1, message);
} else {
    int messageBitLength = message.length * 8;
    BigInteger trunc = new BigInteger(1, message);
    if (messageBitLength - n.bitLength() > 0) {
        trunc = trunc.shiftRight(messageBitLength - n.bitLength());
    return trunc;
BigIntegerfnvHash(final byte[] data, final BigInteger fnvOffs, final BigInteger fnvPrime, final BigInteger mod)
fnv Hash
BigInteger hash = fnvOffs;
for (final byte next : data) {
    final BigInteger bigIntValue = BigInteger.valueOf(next & 0xff);
    hash = hash.xor(bigIntValue);
    hash = hash.multiply(fnvPrime).mod(mod);
return hash;
booleanhasSqrtModPrime(BigInteger r, BigInteger p)
has Sqrt Mod Prime
BigInteger two = new BigInteger("2");
return r.modPow(p.subtract(BigInteger.ONE).divide(two), p).equals(BigInteger.ONE);
booleanisBigPrime(BigInteger number)
is Big Prime
BigInteger random = createRandomInteger(number, new Random());
return testPrime(number, random);
booleanisCoprime(BigInteger a, BigInteger b)
Returns true if A and B are coprime.
return a.gcd(b).equals(BigInteger.ONE);
booleanisFermatPrime(BigInteger f)
Fermat number deterministic primality test (Fermat number is an integer in the form Fn = 22^n + 1).
if (f.signum() <= 0)
    return false;
if (f.bitLength() <= 31) {
    switch (f.intValue()) {
    case 3:
    case 5:
    case 17:
    case 257:
...
booleanisPrime(BigInteger value)
Checks if value is a prime number,
if (value.signum() < 0) {
    throw new IllegalArgumentException("Only positive values permitted.");
BigInteger two = new BigInteger("2");
if (value.compareTo(two) < 0) {
    return false;
if (value.equals(two)) {
...
BooleanprimeProcessPart(BigInteger from)
prime Process Part
return null;
BigIntegersqrtModPrime(BigInteger rSquare, BigInteger p)
sqrt Mod Prime
BigInteger two = new BigInteger("2");
BigInteger z = two;
while (hasSqrtModPrime(z, p)) {
    z = z.add(BigInteger.ONE);
if (!hasSqrtModPrime(rSquare, p)) {
    throw new UnknownError("r has no square root");
} else {
...