I need something faster than BigInteger.probablePrime

i have an method:
public SSPSharedPair createSharedPair() { // slow method
Timer timer = new Timer() {{ this.run(); }};

Random random = this.getRandom();
System.out.println("random: "+timer.step().relative()+"ms");

BigInteger generator = new BigInteger(String.valueOf(this.generatorInt()));
System.out.println("generator: "+timer.step().relative()+"ms");

BigInteger prime = BigInteger.probablePrime(this.bitLength(), random);
System.out.println("prime: "+timer.step().relative()+"ms");

SSPSharedPair sspSharedPair = new SSPSharedPair(generator, prime);
System.out.println("pair: "+timer.step().relative()+"ms");

return sspSharedPair;
}
public SSPSharedPair createSharedPair() { // slow method
Timer timer = new Timer() {{ this.run(); }};

Random random = this.getRandom();
System.out.println("random: "+timer.step().relative()+"ms");

BigInteger generator = new BigInteger(String.valueOf(this.generatorInt()));
System.out.println("generator: "+timer.step().relative()+"ms");

BigInteger prime = BigInteger.probablePrime(this.bitLength(), random);
System.out.println("prime: "+timer.step().relative()+"ms");

SSPSharedPair sspSharedPair = new SSPSharedPair(generator, prime);
System.out.println("pair: "+timer.step().relative()+"ms");

return sspSharedPair;
}
the result of which:
random: 1ms
generator: 0ms
prime: 6621ms
pair: 0ms
random: 1ms
generator: 0ms
prime: 6621ms
pair: 0ms
this.bitLength() is always 2048 this.getRandom() is always new SecureRandom()
20 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @swimer! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3w ago
This is not a valid benchmark! If you want to create proper benchmarks, you should use JMH Java specifically optimizes code that is executed frequently
swimer
swimerOP3w ago
Well, the problem is that it takes a long time to complete
dan1st
dan1st3w ago
otherwise you'll get wrong measurements it's slow initially but it gets faster when calling it often
swimer
swimerOP3w ago
are there no more stable methods in java? im using jdk 21
dan1st
dan1st3w ago
What do you mean with stable?
swimer
swimerOP3w ago
I want to generate a number as quickly as possible and ideally without the "slowly at first" conditions
dan1st
dan1st3w ago
CDS could help with that but it's a bit difficult to set up For testing: Can you try with Random instead of SecureRandom? I want to know whether the SecureRandom or the actual BigInteger.probablyPrime os tje ossie CDS can improve startup time by making stuff faster to load though I don't think it would do much for your BigInteger test (without the AOT cache)
swimer
swimerOP3w ago
new Random() takes 1083ms ThreadLocalRandom.current() takes 608ms
dan1st
dan1st3w ago
and how long does the BigInteger.probablePrime take with those?
swimer
swimerOP3w ago
it's probablePrime with them
dan1st
dan1st3w ago
then the issue is the random number generation Are you on Windows?
swimer
swimerOP3w ago
the creation of an instance is performed once at startup, so it does not matter how long it takes to create a random yes exactly
dan1st
dan1st3w ago
you might be able to pick a default SecureRandom RNG
swimer
swimerOP3w ago
I would like to generate a 2048 bit number, but it takes too long. I could roughly calculate how many bits 1 digit takes and generate it through for, but is it correct? I'm not sure that generating each number separately will be faster
dan1st
dan1st3w ago
there are different SecureRandom providers https://docs.oracle.com/en/java/javase/21/docs/specs/security/standard-names.html#securerandom-number-generation-algorithms Try the following:
SecureRandom.getInstance("NativePRNG")
SecureRandom.getInstance("NativePRNGBlocking")
SecureRandom.getInstance("NativePRNGNonBlocking")
SecureRandom.getInstance("PKCS11")
SecureRandom.getInstance("SHA1PRNG")
SecureRandom.getInstance("Windows-PRNG")
SecureRandom.getInstance("NativePRNG")
SecureRandom.getInstance("NativePRNGBlocking")
SecureRandom.getInstance("NativePRNGNonBlocking")
SecureRandom.getInstance("PKCS11")
SecureRandom.getInstance("SHA1PRNG")
SecureRandom.getInstance("Windows-PRNG")
or use Linux :p
swimer
swimerOP3w ago
I'm just making a server that will be on Linux in the future Windows-PRNG - 3200ms SHA1PRNG - 1400ms PKCS11 - not available
dan1st
dan1st3w ago
and the others? 1400ms is pretty good
Madjosz
Madjosz3w ago
RANDOM.ORG - True Random Number Service
RANDOM.ORG offers true random numbers to anyone on the Internet. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?