2

BigIntegerValue.pow(IntegerValue)

exponent on java is Integer, but i had Biginteger Value.

i had try verify signature GOST 3410, i got this code pow, but its to long..

any have idea? to get P and Q, i'm used bouncy Castle.. but i don't have idea how to verify on bouncy castle because a dont know how to see the value.. Thankyou.

    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;
}
Jhohannes Purba
  • 576
  • 1
  • 5
  • 16
  • Maybe try and use [apfloat](http://www.apfloat.org/apfloat_java/) instead? – fge Mar 09 '14 at 16:28
  • Note also that `>>=` carries the sign bit; you should use `>>>=` instead, which doesn't – fge Mar 09 '14 at 16:30

1 Answers1

3

You can use specially designed modPow method of BigInteger class

Since

  ((A^z1 * y^z2) mod P) mod Q == ((((A^z1) mod P) * ((y^z2) mod P)) mod P) mod Q

you can put it

  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);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215