简体   繁体   English

使用Java生成完全素数

[英]Generating exactly prime number with Java

I'm aware of the function BigInteger.probablePrime(int bitLength, Random rnd) that outputs probably prime number of any bit length. 我知道函数BigInteger.probablePrime(int bitLength,Random rnd)可能输出任何位长的素数。 I want a REAL prime number in Java. 我想在Java中使用真正的素数。 Is there any FOSS library to do so with acceptable performance? 有没有可以接受性能的FOSS库? Thanks in advance! 提前致谢!

EDIT: 编辑:

I'm looking at 1024 & 2048 bit primes. 我正在看1024和2048位素数。


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: 编辑:或者,如果您不相信isProbablePrime足够确定,请使用BigInteger构造函数BigInteger(int bitLength, int certainty, Random rnd)来调整您的确定性阈值:

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/2 certainty ). 新BigInteger表示素数的概率将超过(1 - 1/2 确定性 )。 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. 如果您不相信Java BigInteger类使用它们(如果它们记录了使用的测试会很好),请使用Rabin-Miller测试。

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. 有一些方法可以生成具有可接受性能的非常大的质数,但除了进入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. 看看它是这样的: probablePrime()返回的数字不是素数的可能性低于你和你认识的每个人被光照击中的可能性。 Twice. 两次。 On a single day. 在一天。

Just don't worry about it. 只是不要担心。

You could also use the constructor of BigInteger to generate a real prime: 您还可以使用BigInteger的构造函数生成一个真正的素数:

BigInteger(int bitLength, int certainty, Random rnd)

The time to execute is proportional to the certainty, but on my Core i7 it isn't a problem. 执行时间与确定性成正比,但在我的Core i7上它不是问题。

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;
}
Random rnd = new SecureRandom();
System.out.println(BigInteger.probablePrime(bitLength, rnd));

The probability that a BigInteger returned by method probablePrime() is composite does not exceed 2^-100. 方法probablePrime()返回的BigInteger是复合的概率不超过2 ^ -100。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM