Precisely prime number generation with Java

I am aware of the function BigInteger.probablePrime (int bitLength, Random rnd), which outputs probably a prime number of any bit length. I want a REAL prime in Java. Is there any FOSS library for this with acceptable performance? Thanks in advance!

EDIT:

I look at 1024 and 2048 bits of primes.

+4
source share
5 answers

edit: Or, if you do not trust isProbablePrime with enough confidence, use the BigInteger constructor BigInteger BigInteger(int bitLength, int certainty, Random rnd) , which allows you to set the certainty threshold:

certainty is a measure of the uncertainty that the caller wants to endure. The likelihood that the new BigInteger represents a prime number will exceed (1 - 1/2 certainty ). The runtime of this constructor is proportional to the value of this parameter.

The probabilistic tests used for cryptographic purposes guarantee the binding of the probability of false positives - it's not like there are any numbers found that will make their way through them, it's just a matter of how low the probability should be. If you don't trust the Java BigInteger class to use them (it would be nice if they documented which test was used), use the Rabin-Miller test.

+10
source

There are several ways to generate very large primes with acceptable performance, but not with sufficient density for most purposes, except to get into the Guiness Record Book.

Look at it this way: the probability that the number returned by probablePrime() not prime is less than the probability that you and everyone you know are covered. Twice. One day.

Just don’t worry about it.

+4
source

You can also use the BigInteger constructor to create a real prime number:

 BigInteger(int bitLength, int certainty, Random rnd) 

Runtime is proportional to certainty, but on my Core i7 this is not a problem.

+2
source

Make a way and wrap it.

 BigInteger definitePrime(int bits, Random rnd) { BigInteger prime = new BigInteger("4"); while(!isPrime(prime)) prime = BigInteger.probablePrime(bits,rnd); return prime; } 
+1
source
 Random rnd = new SecureRandom(); System.out.println(BigInteger.probablePrime(bitLength, rnd)); 

The probability that a BigInteger returned by the probablePrime() method is composite does not exceed 2 ^ -100.

0
source

Source: https://habr.com/ru/post/1310494/


All Articles