jdk/test/java/math/BigInteger/ModPow65537.java
author bpb
Wed, 29 Apr 2015 16:34:49 -0700
changeset 30048 3424bede284d
parent 30046 cf2c86e1819e
child 30436 17827057ef5a
permissions -rw-r--r--
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests Summary: Add ability to initial the random number generator from the system property "seed" and print to STDOUT the seed value actually used. Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     1
/*
29371
6f7f029a6b63 8074460: Always print seeds used in [Splittable]Random instances in java.math tests
bpb
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     4
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     8
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    13
 * accompanied this code).
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    14
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1826
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1826
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1826
diff changeset
    21
 * questions.
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    22
 */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    23
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    24
/*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    25
 * @test
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    26
 * @library /lib/testlibrary/
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    27
 * @build jdk.testlibrary.*
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    28
 * @run main ModPow65537
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    29
 * @bug 4891312 8074460 8078672
29371
6f7f029a6b63 8074460: Always print seeds used in [Splittable]Random instances in java.math tests
bpb
parents: 5506
diff changeset
    30
 * @summary verify that modPow() not broken by the special case for 65537 (use -Dseed=X to set PRNG seed)
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    31
 * @author Andreas Sterbenz
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 29371
diff changeset
    32
 * @key randomness
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    33
 */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    34
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    35
import java.math.BigInteger;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    36
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    37
import java.security.*;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    38
import java.security.spec.*;
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    39
import java.util.Random;
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    40
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    41
public class ModPow65537 {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    42
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    44
        // SunRsaSign uses BigInteger internally
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    45
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    46
        kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537)));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    47
        KeyPair kp = kpg.generateKeyPair();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    48
        testSigning(kp);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    49
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    50
        kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539)));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    51
        kp = kpg.generateKeyPair();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    52
        testSigning(kp);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    53
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    54
        kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3)));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    55
        kp = kpg.generateKeyPair();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    56
        testSigning(kp);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    57
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    58
        // basic known answer test
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    59
        BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    60
        BigInteger mod  = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    61
        BigInteger exp1 = BigInteger.valueOf(65537);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    62
        BigInteger exp2 = BigInteger.valueOf(75537);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    63
        BigInteger exp3 = new BigInteger("13456870775607312149");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    64
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    65
        BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    66
        BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    67
        BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    68
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    69
        if (base.modPow(exp1, mod).equals(res1) == false) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    70
            throw new Exception("Error using " + exp1);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    71
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    72
        if (base.modPow(exp2, mod).equals(res2) == false) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    73
            throw new Exception("Error using " + exp2);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    74
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    75
        if (base.modPow(exp3, mod).equals(res3) == false) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    76
            throw new Exception("Error using " + exp3);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    77
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    78
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    79
        System.out.println("Passed");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    80
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    81
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    82
    private static void testSigning(KeyPair kp) throws Exception {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    83
        System.out.println(kp.getPublic());
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    84
        byte[] data = new byte[1024];
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    85
        Random random = RandomFactory.getRandom();
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    86
        random.nextBytes(data);
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    87
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    88
        Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    89
        sig.initSign(kp.getPrivate());
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    90
        sig.update(data);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    91
        byte[] sigBytes = sig.sign();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    92
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    93
        sig.initVerify(kp.getPublic());
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    94
        sig.update(data);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    95
        if (sig.verify(sigBytes) == false) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    96
            throw new Exception("signature verification failed");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    97
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    98
        System.out.println("OK");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    99
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   100
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   101
}