1 /*
2 * Copyright (c) 2014, 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 *
81 long seed = System.nanoTime();
82 Random rand1 = new Random();
83 long seed1 = System.nanoTime();
84 rand.setSeed(seed);
85 rand1.setSeed(seed1);
86
87 for (int j = 0; j < 1000000; j++) {
88 int rand_int = rand1.nextInt(3136)+32;
89 int rand_int1 = rand1.nextInt(3136)+32;
90 b1 = new BigInteger(rand_int, rand);
91 b2 = new BigInteger(rand_int1, rand);
92
93 oldres = base_multiply(b1,b2);
94 newres = new_multiply(b1,b2);
95
96 oldsum = oldsum.add(oldres);
97 newsum = newsum.add(newres);
98
99 if (!bytecompare(oldres,newres)) {
100 System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
101 System.out.println(b1);
102 System.out.println(b2);
103 throw new Exception("Failed");
104 }
105 }
106 if (!bytecompare(oldsum,newsum)) {
107 System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));
108 throw new Exception("Failed");
109 } else {
110 System.out.println("Success");
111 }
112 }
113 }
|
1 /*
2 * Copyright (c) 2014, 2015, 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 *
81 long seed = System.nanoTime();
82 Random rand1 = new Random();
83 long seed1 = System.nanoTime();
84 rand.setSeed(seed);
85 rand1.setSeed(seed1);
86
87 for (int j = 0; j < 1000000; j++) {
88 int rand_int = rand1.nextInt(3136)+32;
89 int rand_int1 = rand1.nextInt(3136)+32;
90 b1 = new BigInteger(rand_int, rand);
91 b2 = new BigInteger(rand_int1, rand);
92
93 oldres = base_multiply(b1,b2);
94 newres = new_multiply(b1,b2);
95
96 oldsum = oldsum.add(oldres);
97 newsum = newsum.add(newres);
98
99 if (!bytecompare(oldres,newres)) {
100 System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
101 throw new Exception("Failed");
102 }
103 }
104
105 // Test carry propagation. Multiple carries during bignum
106 // multiplication are rare (especially when using 64-bit
107 // arithmetic) so we have to provoke them deliberately.
108 for (int j = 4; j <= 396; j += 4) {
109 byte[] bytes = new byte[j];
110 Arrays.fill(bytes, (byte)255);
111 b1 = new BigInteger(bytes);
112 b2 = new BigInteger(bytes);
113
114 oldres = base_multiply(b1,b2);
115 newres = new_multiply(b1,b2);
116
117 oldsum = oldsum.add(oldres);
118 newsum = newsum.add(newres);
119
120 if (!bytecompare(oldres,newres)) {
121 System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
122 System.out.println(b1);
123 System.out.println(b2);
124 throw new Exception("Failed");
125 }
126 }
127
128 if (!bytecompare(oldsum,newsum)) {
129 System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));
130 throw new Exception("Failed");
131 } else {
132 System.out.println("Success");
133 }
134 }
135 }
|