7

I am implementing an RSA encryption program using Java. Right now I am using BigInteger.probablePrime(1024, rnd) to get prime numbers. Here rnd is a random number generated by Random rnd = new Random() . I need to test various speeds of encryption.

My questions are:

  1. what algorithm does the BigInteger.probablePrime(1024, rnd) use?

  2. what is the difference between the algorithm above and other algorithms: like Rabin-Miller, Fermats, Lucas-Lehmer?

Thank you.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
user1132346
  • 73
  • 1
  • 4
  • Could you specify the language? I'm presuming Java, but presumptions... It would not hurt to read your post and remove errors before posting either. – Maarten Bodewes Jan 05 '12 at 15:30
  • Apologies, yes java. And by errors i presume you mean the way i state bigint. I tried to shorthand it I do understand that is not how to use the method in java – user1132346 Jan 05 '12 at 16:11
  • 1
    I'm aware that not everybody will be able to write English at the same level, but if even the methods are spelled wrong, it will be hard to look up anything from that. Also, please use the code tags for methods. If you cannot spell "BigInteger.probablePrime()", you could revert to copy/pasting it from the online JavaDoc. Now it *looks* like you are not spending any time on the question, and we will return the favor of not spending any time on the answer. – Maarten Bodewes Jan 05 '12 at 16:27
  • Once again I apologize for not responding and a caring manner. This is my first post and I am not familiar with the syntax and manner in which i am to write my questions. It has been noted for future reference. – user1132346 Jan 05 '12 at 16:52
  • It takes some time to get used to that, all my links were badly written so far. Meta stackoverflow has lots of useful tips about markup. And you can hit [edit] on your question and check the changes from mikej. – Maarten Bodewes Jan 05 '12 at 22:15
  • Old question, but just in case anyone else is doing something similar you should use Java's `SecureRandom` class. `Random` is a PRNG and has no guarantee that it will be random enough. – ahjohnston25 Jan 22 '14 at 00:29

2 Answers2

5

BigInteger's probable prime methods use both the Miller-Rabin and Lucas-Lehmer algorithms to test primality.

See the internal method BigInteger.primeToCertainty.

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
2

The Java source code is available for download, so you can look at it yourself. Here is the code for BigInteger.probablePrime(int, Random):

public static BigInteger probablePrime(int bitLength, Random rnd) {

    if (bitLength < 2)
        throw new ArithmeticException("bitLength < 2");

    // The cutoff of 95 was chosen empirically for best performance

    return (bitLength < SMALL_PRIME_THRESHOLD ?
            smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) :
            largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd));
}

The actual tests are contained in the smallPrime() and largePrime() methods, which follow directly in the source code.

Christian Ternus
  • 8,406
  • 24
  • 39
rossum
  • 15,344
  • 1
  • 24
  • 38
  • Both those methods call passesMillerRabin() and passesLucasLehmer(), through the primeToCertainty() method, at least in the OpenJDK 1.7 source that has been linked by Michael. For large primes (> 95 bits) a BitSieve is used. – Maarten Bodewes Jan 05 '12 at 22:11
  • Thank you very much for your help! and thank you for the link to the source code. Much appreciated for all your help. Have a wonderful day. – user1132346 Jan 06 '12 at 18:57