BigIntegerValue.pow(IntegerValue)
java上的指数是整数,但我有大整数值。
我尝试验证签名 GOST 3410,我得到了这个代码 pow,但它太长了..
有什么想法吗?为了获得 P 和 Q,我使用了 bouncy CaSTLe.. 但我不知道如何在 bouncy caSTLe 上进行验证,因为不知道如何查看该值.. 谢谢。
public static BigInteger pow_manual(BigInteger x, BigInteger y) {
if (y.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException();
}
BigInteger z = x; // z will successively become x^2, x^4, x^8, x^16, x^32...
BigInteger result = BigInteger.ONE;
byte[] bytes = y.toByteArray();
for (int i = bytes.length - 1; i >= 0; i--) {
byte bits = bytes[i];
for (int j = 0; j < 8; j++) {
if ((bits & 1) != 0) {
result = result.multiply(z);
}
// short cut out if there are no more bits to handle:
if ((bits >>= 1) == 0 && i == 0) {
return result;
}
z = z.multiply(z);
}
}
return result;
}
最佳答案
您可以使用BigInteger
类的专门设计 modPow
方法
自
((A^z1 * y^z2) mod P) mod Q == ((((A^z1) mod P) * ((y^z2) mod P)) mod P) mod Q
可以放
BigInteger A = ...
BigInteger y = ...
BigInteger z1 = ...
BigInteger z2 = ...
BigInteger P = ...
BigInteger Q = ...
BigInteger result = (A.modPow(z1, P).multiply(y.modPow(z2, P))).mod(P).mod(Q);
关于java - 我怎样才能快速 ((A^z1 * y^z2) mod P) mod Q,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284672/