Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6560935

BigInteger.modPow() throws ArithmeticException for negative exponent

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 6
    • core-libs
    • b77
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0-dp"
      Java(TM) SE Runtime Environment (build 1.6.0-dp-b88-34)
      Java HotSpot(TM) Client VM (build 1.6.0-b88-17-release, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Mac OS-X 10.4.9

      A DESCRIPTION OF THE PROBLEM :
      The javadoc for BigInteger says that it supports negative exponents. However, BigInteger throws the following exception when running the following test program:

      import java.math.BigInteger;
      class Foo
      {
          public static void main(String args[])
          {
              BigInteger a = new BigInteger("10", 10);
              BigInteger e = new BigInteger("-3", 10);
              BigInteger m = new BigInteger("10", 10);
              BigInteger result = a.modPow(e, m);
              System.out.println("The ModPow is : " + result);
          }
      }


      $ java Foo
      Exception in thread "main" java.lang.ArithmeticException: BigInteger not invertible.
              at java.math.MutableBigInteger.mutableModInverse(MutableBigInteger.java:1107)
              at java.math.BigInteger.modInverse(BigInteger.java:1992)
              at java.math.BigInteger.modPow(BigInteger.java:1581)
              at Foo.main(Foo.java:9)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See description for test code. Create 3 BigIntegers, one of which is negative. Call modPow() on one of the BigIntegers, using the negative one as the exponent. This will throw an ArithmeticException saying that the BigInteger is not invertible.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Sun's javadoc for BigInteger.modPow() explicitly says:
          Returns a BigInteger whose value is (this^exponent mod m). (Unlike pow, this method permits negative exponents.)

      However, BigInteger does not, apparently, permit negative exponents as currently written.

      ACTUAL -
      See description. Upon trying to take the modPow() of a BigInteger using a negative exponent, it throws an ArithmeticException.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.ArithmeticException: BigInteger not invertible.
              at java.math.MutableBigInteger.mutableModInverse(MutableBigInteger.java:1107)
              at java.math.BigInteger.modInverse(BigInteger.java:1992)
              at java.math.BigInteger.modPow(BigInteger.java:1581)
              at Foo.main(Foo.java:9)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.BigInteger;
      class Foo
      {
          public static void main(String args[])
          {
              BigInteger a = new BigInteger("10", 10);
              BigInteger e = new BigInteger("-3", 10);
              BigInteger m = new BigInteger("10", 10);
              BigInteger result = a.modPow(e, m);
              System.out.println("The ModPow is : " + result);
          }
      }
      ---------- END SOURCE ----------

            darcy Joe Darcy
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: