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 package test.java.math.BigInteger;
33 
34 import java.math.BigInteger;
35 import java.util.Random;
36 import jdk.test.lib.RandomFactory;
37 
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40 
41 // Android-changed: Replace error printing with asserts.
42 public class ModPow {
43 
44     @Test
testModPow()45     public void testModPow() {
46         Random rnd = new Random(1234);
47 
48         for (int i=0; i<2000; i++) {
49             // Clamp random modulus to a positive value or modPow() will
50             // throw an ArithmeticException.
51             BigInteger m = new BigInteger(800, rnd).max(BigInteger.ONE);
52             BigInteger base = new BigInteger(16, rnd);
53             if (rnd.nextInt() % 2 == 0)
54                 base = base.negate();
55             BigInteger exp = new BigInteger(8, rnd);
56 
57             BigInteger z = base.modPow(exp, m);
58             BigInteger w = base.pow(exp.intValue()).mod(m);
59             Assert.assertEquals(z, w,
60                     base +" ** " + exp + " mod "+ m + " modPow : " + z + "pow.mod: " + w);
61         }
62     }
63 }
64