2

BigIntegerValue.pow(整数値)

Javaの指数は整数ですが、Biginteger値がありました。

署名を検証しようとしました GOST 3410、私はこのコードを取得しましたが、長すぎます..

アイデアはありますか?P と Q を取得するために、私は弾むキャッスルを使用しています..しかし、値を確認する方法がわからないため、弾むキャッスルで確認する方法がわかりません..ありがとう。

    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;
}
4

1 に答える 1

3

クラスの特別に設計された modPowメソッドを使用できますBigInteger

以来

  ((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);
于 2014-03-09T16:32:05.867 に答える