用Java生成精确的素数
我知道函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:或者,如果您不相信 isProbablePrime 具有足够大的确定性,请使用 BigInteger 构造函数
BigInteger(int bitLength, int certainty, Random rnd)
可以让您调整确定性阈值:用于加密目的的概率测试保证限制误报的概率——这并不是说存在一些会偷偷溜过的陷阱数字,这只是你希望概率有多低的问题。如果您不信任 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: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.
有一些方法可以生成性能可接受的非常大的素数,但除了进入吉尼斯世界纪录之外,对于大多数用途来说密度不够。
这样看:
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.
您还可以使用 BigInteger 的构造函数来生成实素数:
执行时间与确定性成正比,但在我的 Core i7 上这不是问题。
You could also use the constructor of
BigInteger
to generate a real prime:The time to execute is proportional to the certainty, but on my Core i7 it isn't a problem.
制作一个方法并包装它。
Make a method and wrap it.
probablePrime() 返回
是合数不超过2^-100。BigInteger
的概率The probability that a
BigInteger
returned by methodprobablePrime()
is composite does not exceed 2^-100.