简体   繁体   English

java.math.BigInteger,pow方法仅接受int

[英]java.math.BigInteger, pow method only accepts int

I'm doing a DSA Signature Verification and Validation assignment, I need to perform a calculation which is like this: v = (((g)^u1(y)^u2) mod p) mod q , where g, u1, y, u2, p and q are all BigInteger objects, I got stuck with g^u1 and y^u2 , I want to use the pow method to do the calculation, but this method only accepts int parameter, so I'm not able to calculate g^u1 like this: 我正在执行DSA签名验证和确认分配,我需要执行如下计算: v = (((g)^u1(y)^u2) mod p) mod q ,其中g,u1,y ,u2,p和q都是BigInteger对象,我陷入了g^u1y^u2困境,我想使用pow方法进行计算,但是该方法仅接受int参数,因此我无法像这样计算g^u1

g.pow(u1);

It's resonable that this method only accepts int parameter, because in this case, g is a 1024 bit prime, and u1 here equals 666075361584433975742185154706661067887879287196 , the result of g^u1 will become extremely large. 可以接受的是,此方法仅接受int参数,因为在这种情况下, g是1024位素数,并且u1在这里等于666075361584433975742185154706661067887879287196g^u1的结果将变得非常大。 My question is, how should I perform the calculation of v = (((g)^u1(y)^u2) mod p) mod q . 我的问题是,我应该如何执行v = (((g)^u1(y)^u2) mod p) mod q

Indeed g^u1 becomes very large. 实际上g^u1变得非常大。 However, you can take advantage of the fact that 但是,您可以利用以下事实

a * b  mod  n

can be computed as 可以计算为

(a mod n  *  b mod n)  mod  n

Here, a = g ^ u1 and b = y ^ u2 , and these computations can be done efficiently using modPow . 这里, a = g ^ u1b = y ^ u2 ,并且可以使用modPow有效地完成这些计算。 ((g)^u1(y)^u2) mod p will be: ((g)^u1(y)^u2) mod p将是:

g.modPow(u1,p).multiply(y.modPow(u2,p)).mod(p)

You are going to do mod p anyway - so why don't you use BigInteger.modPow() 无论如何,您都将要进行mod p那么为什么不使用BigInteger.modPow()

Note that 注意

g^u1 * y^u2 mod p == (g^u1 modp * y^u2 mod p) mod p

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

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