views:

213

answers:

3

Hi, I'm aware of the function BigInteger.probablePrime(int bitLength, Random rnd) that outputs probably prime number of any bit length. I want a REAL prime number in Java. Is there any FOSS library to do so with acceptable performance? Thanks in advance!

EDIT:

I'm looking at 1024 & 2048 bit primes.

A: 

Make a method and wrap it.

BigInteger definitePrime(int bits, Random rnd) {
    BigInteger prime = new BigInteger("4");
    while(!isPrime(prime)) prime = BigInteger.probablePrime(bits,rnd);
    return prime;
}
glowcoder
Sorry, did you understand what I want?
Viet
+4  A: 

There are some methods to generate very large primes with acceptable performance, but not with sufficient density for most purposes other than getting into the Guiness Book of Records.

Look at it like this: the likelihood that a number returned by probablePrime() is not prime is lower than the likelihood of you and everyone you know getting hit by lighting. Twice. On a single day.

Just don't worry about it.

Michael Borgwardt
Thanks but I'm looking for cryptographically secure random prime numbers.
Viet
@Viet, I'm assuming you want to implement an RSA algorithm? Even the official implementations of these need to use probable primes.
teehoo
the cryptographic community uses probable prime tests to generate cryptographically secure random prime numbers.
Jason S
@teehoo: they don't *need* to use probable primes, but in practice they do.
Jason S
@Jason, I meant if they want to find a prime in an acceptable amount of time they need to :)
teehoo
So "probable" is an understatement, but they didn't want to name the method `almostAlwaysButOnceInABlueMoonNotPrime()`... ;-)
Jesper
@Viet: Then use probablePrime(). That's what is for. Nobody in the whole wide world knows a better method. And if they did, it would most likely mean that prime-based cryptography has become useless.
Michael Borgwardt
Thanks guys! @teehoo: Any proof of that statement? I know that OpenSSL uses probably prime numbers http://www.openssl.org/docs/crypto/BN_generate_prime.html .
Viet
"there is no software, FOSS or otherwise, that can decide in acceptable time whether a given large number is prime or not." -- not necessarily true
Jason S
@Jason: right, I learned something from your post and removed that statement, though it still hinges on the definition on "acceptable time".
Michael Borgwardt
@Viet, check out "Choice of p and q" at http://project.cyberpunk.ru/idb/cryptography.htmlThis article was published by NIST sometime ago.
teehoo
@Michael: agreed. devil's always in the details. :-)
Jason S
+2  A: 

edit: Or, if you don't trust the isProbablePrime to be large enough certainty, use the BigInteger constructor BigInteger(int bitLength, int certainty, Random rnd) that lets you tune your certainty threshold:

certainty - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed (1 - 1/2certainty). The execution time of this constructor is proportional to the value of this parameter.

Probabilistic tests used for cryptographic purposes are guaranteed to bound the probability of false positives -- it's not like there's some gotcha numbers that exist that will sneak through, it's just a matter of how low you want the probability to be. If you don't trust the Java BigInteger class to use these (it would be nice if they documented what test was used), use the Rabin-Miller test.

Jason S
"fast" is very, very relative here, of course.
Michael Borgwardt
+1 - however, it should be noted that the worst case of AKS is `O((logN)^12)`. That is fast compared with factorizing a prime `N`, but not fast in absolute terms.
Stephen C
+1 thanks! Adding certainty surely increases security.
Viet
"Adding certainty surely increases security" -- you can be fairly certain but not absolutely certain. ;-)
Jason S