[英]BigIntegers to the power of BigInteger (Schnorr signature)
I am trying to implement Schnorr signature algorithm in Java. 我正在尝试用Java实现Schnorr签名算法。 I faced with problem to calculate power with big exponent (such as MD5 hash number).
我面临着用大指数计算功率的问题(例如MD5哈希数)。
Is there any way to get BigInteger in power of BigInteger? 有没有办法让BigInteger掌握BigInteger的力量?
I need to calculate (a^x*b^y) % z where y is extremely large number. 我需要计算(a ^ x * b ^ y)%z,其中y是非常大的数。 Are there any method of calculating such expressions?
有没有计算这种表达的方法?
Thanks 谢谢
For the Schnorr Signature Algorithm, you actually want a combined power and modulus operation. 对于Schnorr签名算法,您实际上需要组合功率和模数运算。 Just doing a power operation by itself makes no sense, because of the potentially enormous size of the numbers involved.
仅仅进行一次动力操作是没有意义的,因为所涉及的数字可能是巨大的。
You need to use the modPow
method of the BigInteger
class . 您需要使用
BigInteger
类的modPow
方法。
I finally I found the solution. 我终于找到了解决方案。 I can calculate my expression very fast using this technique:
使用这种技术,我可以非常快速地计算出我的表情:
(a * b) % p = ((a % p) * (b % p)) % p
So my example will look like this: 因此,我的示例如下所示:
(a^x * b^y) % z = ( ((a^x) % z) * ((b^y) % z) ) % z;
or, using BigInteger in Java: 或者,在Java中使用BigInteger:
BigInteger result = a.modPow(x, z).multiply( b.modPow(y, z) ).mod(z);
No. The maximum value a BigInteger supports is 2 Integer.MAX_VALUE -1. 否。BigInteger支持的最大值为2 Integer.MAX_VALUE -1。 This clarifying sentence was added to the BigInteger javadoc in Java 8, but the implementation has been the same for quite a while.
这个澄清的句子已添加到Java 8中的BigInteger Javadoc中,但是实现了相当一段时间了。
BigInteger must support values in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive) and may support values outside of that range.
BigInteger必须支持-2 Integer.MAX_VALUE (不包括)到+2 Integer.MAX_VALUE (不包括)之间的值,并且可以支持该范围之外的值。
As others have pointed out, you may want to use modPow
instead of calculating intermediate values. 正如其他人指出的那样,您可能要使用
modPow
而不是计算中间值。
As a comparison, there are an estimated 10 80 (or 2 265 ) atoms in the universe. 作为比较,宇宙中估计有10 80 (或2 265 )个原子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.