用Java生成精确的素数

发布于 2024-09-02 08:52:55 字数 175 浏览 3 评论 0原文

我知道函数 BigInteger.probablePrime(int bitLength, Random rnd) 可能输出任何位长度的素数。我想要一个 Java 中的实素数。是否有任何 FOSS 库可以做到这一点并且性能可接受?提前致谢!

编辑:

我正在看 1024 & 2048 位素数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

隔纱相望 2024-09-09 08:52:55

编辑:或者,如果您不相信 isProbablePrime 具有足够大的确定性,请使用 BigInteger 构造函数 BigInteger(int bitLength, int certainty, Random rnd) 可以让您调整确定性阈值:

确定性 - 调用者愿意容忍的不确定性的度量。新的 BigInteger 表示素数的概率将超过 (1 - 1/2确定性)。该构造函数的执行时间与该参数的值成正比。

用于加密目的的概率测试保证限制误报的概率——这并不是说存在一些会偷偷溜过的陷阱数字,这只是你希望概率有多低的问题。如果您不信任 Java BigInteger 类使用这些(如果他们记录了使用的测试就好了),请使用 Rabin-Miller 测试。


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.

源来凯始玺欢你 2024-09-09 08:52:55

有一些方法可以生成性能可接受的非常大的素数,但除了进入吉尼斯世界纪录之外,对于大多数用途来说密度不够。

这样看:probablePrime() 返回的数字不是质数的可能性低于您和您认识的每个人被闪电击中的可能性。两次。在某一天。

不用担心。

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.

牵强ㄟ 2024-09-09 08:52:55

您还可以使用 BigInteger 的构造函数来生成实素数:

BigInteger(int bitLength, int certainty, Random rnd)

执行时间与确定性成正比,但在我的 Core i7 上这不是问题。

You could also use the constructor of BigInteger to generate a real prime:

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.

盛夏已如深秋| 2024-09-09 08:52:55

制作一个方法并包装它。

BigInteger definitePrime(int bits, Random rnd) {
    BigInteger prime = new BigInteger("4");
    while(!isPrime(prime)) prime = BigInteger.probablePrime(bits,rnd);
    return prime;
}

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;
}
染火枫林 2024-09-09 08:52:55
Random rnd = new SecureRandom();
System.out.println(BigInteger.probablePrime(bitLength, rnd));

probablePrime() 返回 BigInteger 的概率是合数不超过2^-100。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文