< prev index next >

test/jdk/java/math/BigInteger/ModPow.java

Print this page


   1 /*
   2  * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4181191
  26  * @summary test BigInteger modPow method




  27  */
  28 import java.math.BigInteger;
  29 import java.util.Random;

  30 
  31 public class ModPow {
  32     public static void main(String[] args) {
  33         Random rnd = new Random(1234);
  34 
  35         for (int i=0; i<2000; i++) {
  36             BigInteger m = new BigInteger(800, rnd);


  37             BigInteger base = new BigInteger(16, rnd);
  38             if (rnd.nextInt() % 1 == 0)
  39                 base = base.negate();
  40             BigInteger exp = new BigInteger(8, rnd);
  41 
  42             BigInteger z = base.modPow(exp, m);
  43             BigInteger w = base.pow(exp.intValue()).mod(m);
  44             if (!z.equals(w)){
  45                 System.err.println(base +" ** " + exp + " mod "+ m);
  46                 System.err.println("modPow : " + z);
  47                 System.err.println("pow.mod: " + w);
  48                 throw new RuntimeException("BigInteger modPow failure.");
  49             }
  50         }
  51     }
  52 }
   1 /*
   2  * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4181191 8215759
  26  * @summary test BigInteger modPow method (use -Dseed=X to set PRNG seed)
  27  * @library /test/lib
  28  * @build jdk.test.lib.RandomFactory
  29  * @run main ModPow
  30  * @key randomness
  31  */
  32 import java.math.BigInteger;
  33 import java.util.Random;
  34 import jdk.test.lib.RandomFactory;
  35 
  36 public class ModPow {
  37     public static void main(String[] args) {
  38         Random rnd = RandomFactory.getRandom();
  39 
  40         for (int i=0; i<2000; i++) {
  41             // Clamp random modulus to a positive value or modPow() will
  42             // throw an ArithmeticException.
  43             BigInteger m = new BigInteger(800, rnd).max(BigInteger.ONE);
  44             BigInteger base = new BigInteger(16, rnd);
  45             if (rnd.nextInt() % 1 == 0)
  46                 base = base.negate();
  47             BigInteger exp = new BigInteger(8, rnd);
  48 
  49             BigInteger z = base.modPow(exp, m);
  50             BigInteger w = base.pow(exp.intValue()).mod(m);
  51             if (!z.equals(w)){
  52                 System.err.println(base +" ** " + exp + " mod "+ m);
  53                 System.err.println("modPow : " + z);
  54                 System.err.println("pow.mod: " + w);
  55                 throw new RuntimeException("BigInteger modPow failure.");
  56             }
  57         }
  58     }
  59 }
< prev index next >