author | malenkov |
Fri, 16 May 2014 15:51:57 +0400 | |
changeset 25088 | 8d4b058368f0 |
parent 21983 | 586d25bfe206 |
child 26627 | 534c5a51e93e |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
26 |
* @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 |
1826 | 27 |
* @summary tests methods in BigInteger |
28 |
* @run main/timeout=400 BigIntegerTest |
|
29 |
* @author madbot |
|
30 |
*/ |
|
31 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
32 |
import java.io.File; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
33 |
import java.io.FileInputStream; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
34 |
import java.io.FileOutputStream; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
35 |
import java.io.ObjectInputStream; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
36 |
import java.io.ObjectOutputStream; |
1826 | 37 |
import java.math.BigInteger; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
38 |
import java.util.Random; |
1826 | 39 |
|
40 |
/** |
|
41 |
* This is a simple test class created to ensure that the results |
|
42 |
* generated by BigInteger adhere to certain identities. Passing |
|
43 |
* this test is a strong assurance that the BigInteger operations |
|
44 |
* are working correctly. |
|
45 |
* |
|
19060 | 46 |
* Four arguments may be specified which give the number of |
47 |
* decimal digits you desire in the four batches of test numbers. |
|
1826 | 48 |
* |
49 |
* The tests are performed on arrays of random numbers which are |
|
50 |
* generated by a Random class as well as special cases which |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
51 |
* throw in boundary numbers such as 0, 1, maximum sized, etc. |
1826 | 52 |
* |
53 |
*/ |
|
54 |
public class BigIntegerTest { |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
55 |
// |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
56 |
// Bit large number thresholds based on the int thresholds |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
57 |
// defined in BigInteger itself: |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
58 |
// |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
59 |
// KARATSUBA_THRESHOLD = 80 ints = 2560 bits |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
60 |
// TOOM_COOK_THRESHOLD = 240 ints = 7680 bits |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
61 |
// KARATSUBA_SQUARE_THRESHOLD = 128 ints = 4096 bits |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
62 |
// TOOM_COOK_SQUARE_THRESHOLD = 216 ints = 6912 bits |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
63 |
// |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
64 |
// SCHOENHAGE_BASE_CONVERSION_THRESHOLD = 20 ints = 640 bits |
18548 | 65 |
// |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
66 |
// BURNIKEL_ZIEGLER_THRESHOLD = 80 ints = 2560 bits |
19060 | 67 |
// |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
68 |
static final int BITS_KARATSUBA = 2560; |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
69 |
static final int BITS_TOOM_COOK = 7680; |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
70 |
static final int BITS_KARATSUBA_SQUARE = 4096; |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
71 |
static final int BITS_TOOM_COOK_SQUARE = 6912; |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
72 |
static final int BITS_SCHOENHAGE_BASE = 640; |
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
73 |
static final int BITS_BURNIKEL_ZIEGLER = 2560; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
74 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
75 |
static final int ORDER_SMALL = 60; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
76 |
static final int ORDER_MEDIUM = 100; |
19393
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
77 |
// #bits for testing Karatsuba |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
78 |
static final int ORDER_KARATSUBA = 2760; |
19393
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
79 |
// #bits for testing Toom-Cook and Burnikel-Ziegler |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
80 |
static final int ORDER_TOOM_COOK = 8000; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
81 |
// #bits for testing Karatsuba squaring |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
82 |
static final int ORDER_KARATSUBA_SQUARE = 4200; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
83 |
// #bits for testing Toom-Cook squaring |
21983
586d25bfe206
8029514: java/math/BigInteger/BigIntegerTest.java failing since thresholds adjusted in 8022181
bpb
parents:
19393
diff
changeset
|
84 |
static final int ORDER_TOOM_COOK_SQUARE = 7000; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
85 |
|
19060 | 86 |
static final int SIZE = 1000; // numbers per batch |
87 |
||
1826 | 88 |
static Random rnd = new Random(); |
89 |
static boolean failure = false; |
|
90 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
91 |
public static void pow(int order) { |
1826 | 92 |
int failCount1 = 0; |
93 |
||
19060 | 94 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
95 |
// Test identity x^power == x*x*x ... *x |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
96 |
int power = rnd.nextInt(6) + 2; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
97 |
BigInteger x = fetchNumber(order); |
1826 | 98 |
BigInteger y = x.pow(power); |
99 |
BigInteger z = x; |
|
100 |
||
101 |
for (int j=1; j<power; j++) |
|
102 |
z = z.multiply(x); |
|
103 |
||
104 |
if (!y.equals(z)) |
|
105 |
failCount1++; |
|
106 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
107 |
report("pow for " + order + " bits", failCount1); |
1826 | 108 |
} |
109 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
110 |
public static void square(int order) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
111 |
int failCount1 = 0; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
112 |
|
19060 | 113 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
114 |
// Test identity x^2 == x*x |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
115 |
BigInteger x = fetchNumber(order); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
116 |
BigInteger xx = x.multiply(x); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
117 |
BigInteger x2 = x.pow(2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
118 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
119 |
if (!x2.equals(xx)) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
120 |
failCount1++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
121 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
122 |
report("square for " + order + " bits", failCount1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
123 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
124 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
125 |
public static void arithmetic(int order) { |
1826 | 126 |
int failCount = 0; |
127 |
||
19060 | 128 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
129 |
BigInteger x = fetchNumber(order); |
1826 | 130 |
while(x.compareTo(BigInteger.ZERO) != 1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
131 |
x = fetchNumber(order); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
132 |
BigInteger y = fetchNumber(order/2); |
1826 | 133 |
while(x.compareTo(y) == -1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
134 |
y = fetchNumber(order/2); |
1826 | 135 |
if (y.equals(BigInteger.ZERO)) |
136 |
y = y.add(BigInteger.ONE); |
|
137 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
138 |
// Test identity ((x/y))*y + x%y - x == 0 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
139 |
// using separate divide() and remainder() |
1826 | 140 |
BigInteger baz = x.divide(y); |
141 |
baz = baz.multiply(y); |
|
142 |
baz = baz.add(x.remainder(y)); |
|
143 |
baz = baz.subtract(x); |
|
144 |
if (!baz.equals(BigInteger.ZERO)) |
|
145 |
failCount++; |
|
146 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
147 |
report("Arithmetic I for " + order + " bits", failCount); |
1826 | 148 |
|
149 |
failCount = 0; |
|
150 |
for (int i=0; i<100; i++) { |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
151 |
BigInteger x = fetchNumber(order); |
1826 | 152 |
while(x.compareTo(BigInteger.ZERO) != 1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
153 |
x = fetchNumber(order); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
154 |
BigInteger y = fetchNumber(order/2); |
1826 | 155 |
while(x.compareTo(y) == -1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
156 |
y = fetchNumber(order/2); |
1826 | 157 |
if (y.equals(BigInteger.ZERO)) |
158 |
y = y.add(BigInteger.ONE); |
|
159 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
160 |
// Test identity ((x/y))*y + x%y - x == 0 |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
161 |
// using divideAndRemainder() |
1826 | 162 |
BigInteger baz[] = x.divideAndRemainder(y); |
163 |
baz[0] = baz[0].multiply(y); |
|
164 |
baz[0] = baz[0].add(baz[1]); |
|
165 |
baz[0] = baz[0].subtract(x); |
|
166 |
if (!baz[0].equals(BigInteger.ZERO)) |
|
167 |
failCount++; |
|
168 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
169 |
report("Arithmetic II for " + order + " bits", failCount); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
170 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
171 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
172 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
173 |
* Sanity test for Karatsuba and 3-way Toom-Cook multiplication. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
174 |
* For each of the Karatsuba and 3-way Toom-Cook multiplication thresholds, |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
175 |
* construct two factors each with a mag array one element shorter than the |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
176 |
* threshold, and with the most significant bit set and the rest of the bits |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
177 |
* random. Each of these numbers will therefore be below the threshold but |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
178 |
* if shifted left be above the threshold. Call the numbers 'u' and 'v' and |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
179 |
* define random shifts 'a' and 'b' in the range [1,32]. Then we have the |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
180 |
* identity |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
181 |
* <pre> |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
182 |
* (u << a)*(v << b) = (u*v) << (a + b) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
183 |
* </pre> |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
184 |
* For Karatsuba multiplication, the right hand expression will be evaluated |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
185 |
* using the standard naive algorithm, and the left hand expression using |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
186 |
* the Karatsuba algorithm. For 3-way Toom-Cook multiplication, the right |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
187 |
* hand expression will be evaluated using Karatsuba multiplication, and the |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
188 |
* left hand expression using 3-way Toom-Cook multiplication. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
189 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
190 |
public static void multiplyLarge() { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
191 |
int failCount = 0; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
192 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
193 |
BigInteger base = BigInteger.ONE.shiftLeft(BITS_KARATSUBA - 32 - 1); |
19060 | 194 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
195 |
BigInteger x = fetchNumber(BITS_KARATSUBA - 32 - 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
196 |
BigInteger u = base.add(x); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
197 |
int a = 1 + rnd.nextInt(31); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
198 |
BigInteger w = u.shiftLeft(a); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
199 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
200 |
BigInteger y = fetchNumber(BITS_KARATSUBA - 32 - 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
201 |
BigInteger v = base.add(y); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
202 |
int b = 1 + rnd.nextInt(32); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
203 |
BigInteger z = v.shiftLeft(b); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
204 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
205 |
BigInteger multiplyResult = u.multiply(v).shiftLeft(a + b); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
206 |
BigInteger karatsubaMultiplyResult = w.multiply(z); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
207 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
208 |
if (!multiplyResult.equals(karatsubaMultiplyResult)) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
209 |
failCount++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
210 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
211 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
212 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
213 |
report("multiplyLarge Karatsuba", failCount); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
214 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
215 |
failCount = 0; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
216 |
base = base.shiftLeft(BITS_TOOM_COOK - BITS_KARATSUBA); |
19060 | 217 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
218 |
BigInteger x = fetchNumber(BITS_TOOM_COOK - 32 - 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
219 |
BigInteger u = base.add(x); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
220 |
BigInteger u2 = u.shiftLeft(1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
221 |
BigInteger y = fetchNumber(BITS_TOOM_COOK - 32 - 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
222 |
BigInteger v = base.add(y); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
223 |
BigInteger v2 = v.shiftLeft(1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
224 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
225 |
BigInteger multiplyResult = u.multiply(v).shiftLeft(2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
226 |
BigInteger toomCookMultiplyResult = u2.multiply(v2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
227 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
228 |
if (!multiplyResult.equals(toomCookMultiplyResult)) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
229 |
failCount++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
230 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
231 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
232 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
233 |
report("multiplyLarge Toom-Cook", failCount); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
234 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
235 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
236 |
/** |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
237 |
* Sanity test for Karatsuba and 3-way Toom-Cook squaring. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
238 |
* This test is analogous to {@link AbstractMethodError#multiplyLarge} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
239 |
* with both factors being equal. The squaring methods will not be tested |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
240 |
* unless the <code>bigInteger.multiply(bigInteger)</code> tests whether |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
241 |
* the parameter is the same instance on which the method is being invoked |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
242 |
* and calls <code>square()</code> accordingly. |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
243 |
*/ |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
244 |
public static void squareLarge() { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
245 |
int failCount = 0; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
246 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
247 |
BigInteger base = BigInteger.ONE.shiftLeft(BITS_KARATSUBA_SQUARE - 32 - 1); |
19060 | 248 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
249 |
BigInteger x = fetchNumber(BITS_KARATSUBA_SQUARE - 32 - 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
250 |
BigInteger u = base.add(x); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
251 |
int a = 1 + rnd.nextInt(31); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
252 |
BigInteger w = u.shiftLeft(a); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
253 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
254 |
BigInteger squareResult = u.multiply(u).shiftLeft(2*a); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
255 |
BigInteger karatsubaSquareResult = w.multiply(w); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
256 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
257 |
if (!squareResult.equals(karatsubaSquareResult)) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
258 |
failCount++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
259 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
260 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
261 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
262 |
report("squareLarge Karatsuba", failCount); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
263 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
264 |
failCount = 0; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
265 |
base = base.shiftLeft(BITS_TOOM_COOK_SQUARE - BITS_KARATSUBA_SQUARE); |
19060 | 266 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
267 |
BigInteger x = fetchNumber(BITS_TOOM_COOK_SQUARE - 32 - 1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
268 |
BigInteger u = base.add(x); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
269 |
int a = 1 + rnd.nextInt(31); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
270 |
BigInteger w = u.shiftLeft(a); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
271 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
272 |
BigInteger squareResult = u.multiply(u).shiftLeft(2*a); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
273 |
BigInteger toomCookSquareResult = w.multiply(w); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
274 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
275 |
if (!squareResult.equals(toomCookSquareResult)) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
276 |
failCount++; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
277 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
278 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
279 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
280 |
report("squareLarge Toom-Cook", failCount); |
1826 | 281 |
} |
282 |
||
19060 | 283 |
/** |
284 |
* Sanity test for Burnikel-Ziegler division. The Burnikel-Ziegler division |
|
285 |
* algorithm is used when each of the dividend and the divisor has at least |
|
286 |
* a specified number of ints in its representation. This test is based on |
|
287 |
* the observation that if {@code w = u*pow(2,a)} and {@code z = v*pow(2,b)} |
|
288 |
* where {@code abs(u) > abs(v)} and {@code a > b && b > 0}, then if |
|
289 |
* {@code w/z = q1*z + r1} and {@code u/v = q2*v + r2}, then |
|
290 |
* {@code q1 = q2*pow(2,a-b)} and {@code r1 = r2*pow(2,b)}. The test |
|
291 |
* ensures that {@code v} is just under the B-Z threshold and that {@code w} |
|
292 |
* and {@code z} are both over the threshold. This implies that {@code u/v} |
|
293 |
* uses the standard division algorithm and {@code w/z} uses the B-Z |
|
294 |
* algorithm. The results of the two algorithms are then compared using the |
|
295 |
* observation described in the foregoing and if they are not equal a |
|
296 |
* failure is logged. |
|
297 |
*/ |
|
298 |
public static void divideLarge() { |
|
299 |
int failCount = 0; |
|
300 |
||
301 |
BigInteger base = BigInteger.ONE.shiftLeft(BITS_BURNIKEL_ZIEGLER - 33); |
|
302 |
for (int i=0; i<SIZE; i++) { |
|
303 |
BigInteger addend = new BigInteger(BITS_BURNIKEL_ZIEGLER - 34, rnd); |
|
304 |
BigInteger v = base.add(addend); |
|
305 |
||
306 |
BigInteger u = v.multiply(BigInteger.valueOf(2 + rnd.nextInt(Short.MAX_VALUE - 1))); |
|
307 |
||
308 |
if(rnd.nextBoolean()) { |
|
309 |
u = u.negate(); |
|
310 |
} |
|
311 |
if(rnd.nextBoolean()) { |
|
312 |
v = v.negate(); |
|
313 |
} |
|
314 |
||
315 |
int a = 17 + rnd.nextInt(16); |
|
316 |
int b = 1 + rnd.nextInt(16); |
|
317 |
BigInteger w = u.multiply(BigInteger.valueOf(1L << a)); |
|
318 |
BigInteger z = v.multiply(BigInteger.valueOf(1L << b)); |
|
319 |
||
320 |
BigInteger[] divideResult = u.divideAndRemainder(v); |
|
321 |
divideResult[0] = divideResult[0].multiply(BigInteger.valueOf(1L << (a - b))); |
|
322 |
divideResult[1] = divideResult[1].multiply(BigInteger.valueOf(1L << b)); |
|
323 |
BigInteger[] bzResult = w.divideAndRemainder(z); |
|
324 |
||
325 |
if (divideResult[0].compareTo(bzResult[0]) != 0 || |
|
326 |
divideResult[1].compareTo(bzResult[1]) != 0) { |
|
327 |
failCount++; |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
report("divideLarge", failCount); |
|
332 |
} |
|
333 |
||
1826 | 334 |
public static void bitCount() { |
335 |
int failCount = 0; |
|
336 |
||
19060 | 337 |
for (int i=0; i<SIZE*10; i++) { |
1826 | 338 |
int x = rnd.nextInt(); |
339 |
BigInteger bigX = BigInteger.valueOf((long)x); |
|
340 |
int bit = (x < 0 ? 0 : 1); |
|
341 |
int tmp = x, bitCount = 0; |
|
342 |
for (int j=0; j<32; j++) { |
|
343 |
bitCount += ((tmp & 1) == bit ? 1 : 0); |
|
344 |
tmp >>= 1; |
|
345 |
} |
|
346 |
||
347 |
if (bigX.bitCount() != bitCount) { |
|
348 |
//System.err.println(x+": "+bitCount+", "+bigX.bitCount()); |
|
349 |
failCount++; |
|
350 |
} |
|
351 |
} |
|
352 |
report("Bit Count", failCount); |
|
353 |
} |
|
354 |
||
355 |
public static void bitLength() { |
|
356 |
int failCount = 0; |
|
357 |
||
19060 | 358 |
for (int i=0; i<SIZE*10; i++) { |
1826 | 359 |
int x = rnd.nextInt(); |
360 |
BigInteger bigX = BigInteger.valueOf((long)x); |
|
361 |
int signBit = (x < 0 ? 0x80000000 : 0); |
|
362 |
int tmp = x, bitLength, j; |
|
363 |
for (j=0; j<32 && (tmp & 0x80000000)==signBit; j++) |
|
364 |
tmp <<= 1; |
|
365 |
bitLength = 32 - j; |
|
366 |
||
367 |
if (bigX.bitLength() != bitLength) { |
|
368 |
//System.err.println(x+": "+bitLength+", "+bigX.bitLength()); |
|
369 |
failCount++; |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
report("BitLength", failCount); |
|
374 |
} |
|
375 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
376 |
public static void bitOps(int order) { |
1826 | 377 |
int failCount1 = 0, failCount2 = 0, failCount3 = 0; |
378 |
||
19060 | 379 |
for (int i=0; i<SIZE*5; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
380 |
BigInteger x = fetchNumber(order); |
1826 | 381 |
BigInteger y; |
382 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
383 |
// Test setBit and clearBit (and testBit) |
1826 | 384 |
if (x.signum() < 0) { |
385 |
y = BigInteger.valueOf(-1); |
|
386 |
for (int j=0; j<x.bitLength(); j++) |
|
387 |
if (!x.testBit(j)) |
|
388 |
y = y.clearBit(j); |
|
389 |
} else { |
|
390 |
y = BigInteger.ZERO; |
|
391 |
for (int j=0; j<x.bitLength(); j++) |
|
392 |
if (x.testBit(j)) |
|
393 |
y = y.setBit(j); |
|
394 |
} |
|
395 |
if (!x.equals(y)) |
|
396 |
failCount1++; |
|
397 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
398 |
// Test flipBit (and testBit) |
1826 | 399 |
y = BigInteger.valueOf(x.signum()<0 ? -1 : 0); |
400 |
for (int j=0; j<x.bitLength(); j++) |
|
401 |
if (x.signum()<0 ^ x.testBit(j)) |
|
402 |
y = y.flipBit(j); |
|
403 |
if (!x.equals(y)) |
|
404 |
failCount2++; |
|
405 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
406 |
report("clearBit/testBit for " + order + " bits", failCount1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
407 |
report("flipBit/testBit for " + order + " bits", failCount2); |
1826 | 408 |
|
19060 | 409 |
for (int i=0; i<SIZE*5; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
410 |
BigInteger x = fetchNumber(order); |
1826 | 411 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
412 |
// Test getLowestSetBit() |
1826 | 413 |
int k = x.getLowestSetBit(); |
414 |
if (x.signum() == 0) { |
|
415 |
if (k != -1) |
|
416 |
failCount3++; |
|
417 |
} else { |
|
418 |
BigInteger z = x.and(x.negate()); |
|
419 |
int j; |
|
420 |
for (j=0; j<z.bitLength() && !z.testBit(j); j++) |
|
421 |
; |
|
422 |
if (k != j) |
|
423 |
failCount3++; |
|
424 |
} |
|
425 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
426 |
report("getLowestSetBit for " + order + " bits", failCount3); |
1826 | 427 |
} |
428 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
429 |
public static void bitwise(int order) { |
1826 | 430 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
431 |
// Test identity x^y == x|y &~ x&y |
1826 | 432 |
int failCount = 0; |
19060 | 433 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
434 |
BigInteger x = fetchNumber(order); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
435 |
BigInteger y = fetchNumber(order); |
1826 | 436 |
BigInteger z = x.xor(y); |
437 |
BigInteger w = x.or(y).andNot(x.and(y)); |
|
438 |
if (!z.equals(w)) |
|
439 |
failCount++; |
|
440 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
441 |
report("Logic (^ | & ~) for " + order + " bits", failCount); |
1826 | 442 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
443 |
// Test identity x &~ y == ~(~x | y) |
1826 | 444 |
failCount = 0; |
19060 | 445 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
446 |
BigInteger x = fetchNumber(order); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
447 |
BigInteger y = fetchNumber(order); |
1826 | 448 |
BigInteger z = x.andNot(y); |
449 |
BigInteger w = x.not().or(y).not(); |
|
450 |
if (!z.equals(w)) |
|
451 |
failCount++; |
|
452 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
453 |
report("Logic (&~ | ~) for " + order + " bits", failCount); |
1826 | 454 |
} |
455 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
456 |
public static void shift(int order) { |
1826 | 457 |
int failCount1 = 0; |
458 |
int failCount2 = 0; |
|
459 |
int failCount3 = 0; |
|
460 |
||
461 |
for (int i=0; i<100; i++) { |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
462 |
BigInteger x = fetchNumber(order); |
1826 | 463 |
int n = Math.abs(rnd.nextInt()%200); |
464 |
||
465 |
if (!x.shiftLeft(n).equals |
|
466 |
(x.multiply(BigInteger.valueOf(2L).pow(n)))) |
|
467 |
failCount1++; |
|
468 |
||
469 |
BigInteger y[] =x.divideAndRemainder(BigInteger.valueOf(2L).pow(n)); |
|
470 |
BigInteger z = (x.signum()<0 && y[1].signum()!=0 |
|
471 |
? y[0].subtract(BigInteger.ONE) |
|
472 |
: y[0]); |
|
473 |
||
474 |
BigInteger b = x.shiftRight(n); |
|
475 |
||
476 |
if (!b.equals(z)) { |
|
477 |
System.err.println("Input is "+x.toString(2)); |
|
478 |
System.err.println("shift is "+n); |
|
479 |
||
480 |
System.err.println("Divided "+z.toString(2)); |
|
481 |
System.err.println("Shifted is "+b.toString(2)); |
|
482 |
if (b.toString().equals(z.toString())) |
|
483 |
System.err.println("Houston, we have a problem."); |
|
484 |
failCount2++; |
|
485 |
} |
|
486 |
||
487 |
if (!x.shiftLeft(n).shiftRight(n).equals(x)) |
|
488 |
failCount3++; |
|
489 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
490 |
report("baz shiftLeft for " + order + " bits", failCount1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
491 |
report("baz shiftRight for " + order + " bits", failCount2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
492 |
report("baz shiftLeft/Right for " + order + " bits", failCount3); |
1826 | 493 |
} |
494 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
495 |
public static void divideAndRemainder(int order) { |
1826 | 496 |
int failCount1 = 0; |
497 |
||
19060 | 498 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
499 |
BigInteger x = fetchNumber(order).abs(); |
1826 | 500 |
while(x.compareTo(BigInteger.valueOf(3L)) != 1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
501 |
x = fetchNumber(order).abs(); |
1826 | 502 |
BigInteger z = x.divide(BigInteger.valueOf(2L)); |
503 |
BigInteger y[] = x.divideAndRemainder(x); |
|
504 |
if (!y[0].equals(BigInteger.ONE)) { |
|
505 |
failCount1++; |
|
506 |
System.err.println("fail1 x :"+x); |
|
507 |
System.err.println(" y :"+y); |
|
508 |
} |
|
509 |
else if (!y[1].equals(BigInteger.ZERO)) { |
|
510 |
failCount1++; |
|
511 |
System.err.println("fail2 x :"+x); |
|
512 |
System.err.println(" y :"+y); |
|
513 |
} |
|
514 |
||
515 |
y = x.divideAndRemainder(z); |
|
516 |
if (!y[0].equals(BigInteger.valueOf(2))) { |
|
517 |
failCount1++; |
|
518 |
System.err.println("fail3 x :"+x); |
|
519 |
System.err.println(" y :"+y); |
|
520 |
} |
|
521 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
522 |
report("divideAndRemainder for " + order + " bits", failCount1); |
1826 | 523 |
} |
524 |
||
525 |
public static void stringConv() { |
|
526 |
int failCount = 0; |
|
527 |
||
18548 | 528 |
// Generic string conversion. |
1826 | 529 |
for (int i=0; i<100; i++) { |
530 |
byte xBytes[] = new byte[Math.abs(rnd.nextInt())%100+1]; |
|
531 |
rnd.nextBytes(xBytes); |
|
532 |
BigInteger x = new BigInteger(xBytes); |
|
533 |
||
18548 | 534 |
for (int radix=Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) { |
1826 | 535 |
String result = x.toString(radix); |
536 |
BigInteger test = new BigInteger(result, radix); |
|
537 |
if (!test.equals(x)) { |
|
538 |
failCount++; |
|
539 |
System.err.println("BigInteger toString: "+x); |
|
540 |
System.err.println("Test: "+test); |
|
541 |
System.err.println(radix); |
|
542 |
} |
|
543 |
} |
|
544 |
} |
|
18548 | 545 |
|
546 |
// String conversion straddling the Schoenhage algorithm crossover |
|
547 |
// threshold, and at twice and four times the threshold. |
|
548 |
for (int k = 0; k <= 2; k++) { |
|
549 |
int factor = 1 << k; |
|
550 |
int upper = factor * BITS_SCHOENHAGE_BASE + 33; |
|
551 |
int lower = upper - 35; |
|
552 |
||
553 |
for (int bits = upper; bits >= lower; bits--) { |
|
554 |
for (int i = 0; i < 50; i++) { |
|
555 |
BigInteger x = BigInteger.ONE.shiftLeft(bits - 1).or(new BigInteger(bits - 2, rnd)); |
|
556 |
||
557 |
for (int radix = Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) { |
|
558 |
String result = x.toString(radix); |
|
559 |
BigInteger test = new BigInteger(result, radix); |
|
560 |
if (!test.equals(x)) { |
|
561 |
failCount++; |
|
562 |
System.err.println("BigInteger toString: " + x); |
|
563 |
System.err.println("Test: " + test); |
|
564 |
System.err.println(radix); |
|
565 |
} |
|
566 |
} |
|
567 |
} |
|
568 |
} |
|
569 |
} |
|
570 |
||
1826 | 571 |
report("String Conversion", failCount); |
572 |
} |
|
573 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
574 |
public static void byteArrayConv(int order) { |
1826 | 575 |
int failCount = 0; |
576 |
||
19060 | 577 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
578 |
BigInteger x = fetchNumber(order); |
1826 | 579 |
while (x.equals(BigInteger.ZERO)) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
580 |
x = fetchNumber(order); |
1826 | 581 |
BigInteger y = new BigInteger(x.toByteArray()); |
582 |
if (!x.equals(y)) { |
|
583 |
failCount++; |
|
584 |
System.err.println("orig is "+x); |
|
585 |
System.err.println("new is "+y); |
|
586 |
} |
|
587 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
588 |
report("Array Conversion for " + order + " bits", failCount); |
1826 | 589 |
} |
590 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
591 |
public static void modInv(int order) { |
1826 | 592 |
int failCount = 0, successCount = 0, nonInvCount = 0; |
593 |
||
19060 | 594 |
for (int i=0; i<SIZE; i++) { |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
595 |
BigInteger x = fetchNumber(order); |
1826 | 596 |
while(x.equals(BigInteger.ZERO)) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
597 |
x = fetchNumber(order); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
598 |
BigInteger m = fetchNumber(order).abs(); |
1826 | 599 |
while(m.compareTo(BigInteger.ONE) != 1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
600 |
m = fetchNumber(order).abs(); |
1826 | 601 |
|
602 |
try { |
|
603 |
BigInteger inv = x.modInverse(m); |
|
604 |
BigInteger prod = inv.multiply(x).remainder(m); |
|
605 |
||
606 |
if (prod.signum() == -1) |
|
607 |
prod = prod.add(m); |
|
608 |
||
609 |
if (prod.equals(BigInteger.ONE)) |
|
610 |
successCount++; |
|
611 |
else |
|
612 |
failCount++; |
|
613 |
} catch(ArithmeticException e) { |
|
614 |
nonInvCount++; |
|
615 |
} |
|
616 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
617 |
report("Modular Inverse for " + order + " bits", failCount); |
1826 | 618 |
} |
619 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
620 |
public static void modExp(int order1, int order2) { |
1826 | 621 |
int failCount = 0; |
622 |
||
19060 | 623 |
for (int i=0; i<SIZE/10; i++) { |
1826 | 624 |
BigInteger m = fetchNumber(order1).abs(); |
625 |
while(m.compareTo(BigInteger.ONE) != 1) |
|
626 |
m = fetchNumber(order1).abs(); |
|
627 |
BigInteger base = fetchNumber(order2); |
|
628 |
BigInteger exp = fetchNumber(8).abs(); |
|
629 |
||
630 |
BigInteger z = base.modPow(exp, m); |
|
631 |
BigInteger w = base.pow(exp.intValue()).mod(m); |
|
632 |
if (!z.equals(w)) { |
|
633 |
System.err.println("z is "+z); |
|
634 |
System.err.println("w is "+w); |
|
635 |
System.err.println("mod is "+m); |
|
636 |
System.err.println("base is "+base); |
|
637 |
System.err.println("exp is "+exp); |
|
638 |
failCount++; |
|
639 |
} |
|
640 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
641 |
report("Exponentiation I for " + order1 + " and " + |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
642 |
order2 + " bits", failCount); |
1826 | 643 |
} |
644 |
||
645 |
// This test is based on Fermat's theorem |
|
646 |
// which is not ideal because base must not be multiple of modulus |
|
647 |
// and modulus must be a prime or pseudoprime (Carmichael number) |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
648 |
public static void modExp2(int order) { |
1826 | 649 |
int failCount = 0; |
650 |
||
651 |
for (int i=0; i<10; i++) { |
|
652 |
BigInteger m = new BigInteger(100, 5, rnd); |
|
653 |
while(m.compareTo(BigInteger.ONE) != 1) |
|
654 |
m = new BigInteger(100, 5, rnd); |
|
655 |
BigInteger exp = m.subtract(BigInteger.ONE); |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
656 |
BigInteger base = fetchNumber(order).abs(); |
1826 | 657 |
while(base.compareTo(m) != -1) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
658 |
base = fetchNumber(order).abs(); |
1826 | 659 |
while(base.equals(BigInteger.ZERO)) |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
660 |
base = fetchNumber(order).abs(); |
1826 | 661 |
|
662 |
BigInteger one = base.modPow(exp, m); |
|
663 |
if (!one.equals(BigInteger.ONE)) { |
|
664 |
System.err.println("m is "+m); |
|
665 |
System.err.println("base is "+base); |
|
666 |
System.err.println("exp is "+exp); |
|
667 |
failCount++; |
|
668 |
} |
|
669 |
} |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
670 |
report("Exponentiation II for " + order + " bits", failCount); |
1826 | 671 |
} |
672 |
||
673 |
private static final int[] mersenne_powers = { |
|
674 |
521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, |
|
675 |
21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, |
|
676 |
1257787, 1398269, 2976221, 3021377, 6972593, 13466917 }; |
|
677 |
||
678 |
private static final long[] carmichaels = { |
|
679 |
561,1105,1729,2465,2821,6601,8911,10585,15841,29341,41041,46657,52633, |
|
680 |
62745,63973,75361,101101,115921,126217,162401,172081,188461,252601, |
|
681 |
278545,294409,314821,334153,340561,399001,410041,449065,488881,512461, |
|
682 |
225593397919L }; |
|
683 |
||
684 |
// Note: testing the larger ones takes too long. |
|
685 |
private static final int NUM_MERSENNES_TO_TEST = 7; |
|
686 |
// Note: this constant used for computed Carmichaels, not the array above |
|
687 |
private static final int NUM_CARMICHAELS_TO_TEST = 5; |
|
688 |
||
689 |
private static final String[] customer_primes = { |
|
690 |
"120000000000000000000000000000000019", |
|
691 |
"633825300114114700748351603131", |
|
692 |
"1461501637330902918203684832716283019651637554291", |
|
693 |
"779626057591079617852292862756047675913380626199", |
|
694 |
"857591696176672809403750477631580323575362410491", |
|
695 |
"910409242326391377348778281801166102059139832131", |
|
696 |
"929857869954035706722619989283358182285540127919", |
|
697 |
"961301750640481375785983980066592002055764391999", |
|
698 |
"1267617700951005189537696547196156120148404630231", |
|
699 |
"1326015641149969955786344600146607663033642528339" }; |
|
700 |
||
701 |
private static final BigInteger ZERO = BigInteger.ZERO; |
|
702 |
private static final BigInteger ONE = BigInteger.ONE; |
|
703 |
private static final BigInteger TWO = new BigInteger("2"); |
|
704 |
private static final BigInteger SIX = new BigInteger("6"); |
|
705 |
private static final BigInteger TWELVE = new BigInteger("12"); |
|
706 |
private static final BigInteger EIGHTEEN = new BigInteger("18"); |
|
707 |
||
708 |
public static void prime() { |
|
709 |
BigInteger p1, p2, c1; |
|
710 |
int failCount = 0; |
|
711 |
||
712 |
// Test consistency |
|
713 |
for(int i=0; i<10; i++) { |
|
714 |
p1 = BigInteger.probablePrime(100, rnd); |
|
715 |
if (!p1.isProbablePrime(100)) { |
|
716 |
System.err.println("Consistency "+p1.toString(16)); |
|
717 |
failCount++; |
|
718 |
} |
|
719 |
} |
|
720 |
||
721 |
// Test some known Mersenne primes (2^n)-1 |
|
722 |
// The array holds the exponents, not the numbers being tested |
|
723 |
for (int i=0; i<NUM_MERSENNES_TO_TEST; i++) { |
|
724 |
p1 = new BigInteger("2"); |
|
725 |
p1 = p1.pow(mersenne_powers[i]); |
|
726 |
p1 = p1.subtract(BigInteger.ONE); |
|
727 |
if (!p1.isProbablePrime(100)) { |
|
728 |
System.err.println("Mersenne prime "+i+ " failed."); |
|
729 |
failCount++; |
|
730 |
} |
|
731 |
} |
|
732 |
||
733 |
// Test some primes reported by customers as failing in the past |
|
734 |
for (int i=0; i<customer_primes.length; i++) { |
|
735 |
p1 = new BigInteger(customer_primes[i]); |
|
736 |
if (!p1.isProbablePrime(100)) { |
|
737 |
System.err.println("Customer prime "+i+ " failed."); |
|
738 |
failCount++; |
|
739 |
} |
|
740 |
} |
|
741 |
||
742 |
// Test some known Carmichael numbers. |
|
743 |
for (int i=0; i<carmichaels.length; i++) { |
|
744 |
c1 = BigInteger.valueOf(carmichaels[i]); |
|
745 |
if(c1.isProbablePrime(100)) { |
|
746 |
System.err.println("Carmichael "+i+ " reported as prime."); |
|
747 |
failCount++; |
|
748 |
} |
|
749 |
} |
|
750 |
||
751 |
// Test some computed Carmichael numbers. |
|
752 |
// Numbers of the form (6k+1)(12k+1)(18k+1) are Carmichael numbers if |
|
753 |
// each of the factors is prime |
|
754 |
int found = 0; |
|
755 |
BigInteger f1 = new BigInteger(40, 100, rnd); |
|
756 |
while (found < NUM_CARMICHAELS_TO_TEST) { |
|
757 |
BigInteger k = null; |
|
758 |
BigInteger f2, f3; |
|
759 |
f1 = f1.nextProbablePrime(); |
|
760 |
BigInteger[] result = f1.subtract(ONE).divideAndRemainder(SIX); |
|
761 |
if (result[1].equals(ZERO)) { |
|
762 |
k = result[0]; |
|
763 |
f2 = k.multiply(TWELVE).add(ONE); |
|
764 |
if (f2.isProbablePrime(100)) { |
|
765 |
f3 = k.multiply(EIGHTEEN).add(ONE); |
|
766 |
if (f3.isProbablePrime(100)) { |
|
767 |
c1 = f1.multiply(f2).multiply(f3); |
|
768 |
if (c1.isProbablePrime(100)) { |
|
769 |
System.err.println("Computed Carmichael " |
|
770 |
+c1.toString(16)); |
|
771 |
failCount++; |
|
772 |
} |
|
773 |
found++; |
|
774 |
} |
|
775 |
} |
|
776 |
} |
|
777 |
f1 = f1.add(TWO); |
|
778 |
} |
|
779 |
||
780 |
// Test some composites that are products of 2 primes |
|
781 |
for (int i=0; i<50; i++) { |
|
782 |
p1 = BigInteger.probablePrime(100, rnd); |
|
783 |
p2 = BigInteger.probablePrime(100, rnd); |
|
784 |
c1 = p1.multiply(p2); |
|
785 |
if (c1.isProbablePrime(100)) { |
|
786 |
System.err.println("Composite failed "+c1.toString(16)); |
|
787 |
failCount++; |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
for (int i=0; i<4; i++) { |
|
792 |
p1 = BigInteger.probablePrime(600, rnd); |
|
793 |
p2 = BigInteger.probablePrime(600, rnd); |
|
794 |
c1 = p1.multiply(p2); |
|
795 |
if (c1.isProbablePrime(100)) { |
|
796 |
System.err.println("Composite failed "+c1.toString(16)); |
|
797 |
failCount++; |
|
798 |
} |
|
799 |
} |
|
800 |
||
801 |
report("Prime", failCount); |
|
802 |
} |
|
803 |
||
804 |
private static final long[] primesTo100 = { |
|
805 |
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 |
|
806 |
}; |
|
807 |
||
808 |
private static final long[] aPrimeSequence = { |
|
809 |
1999999003L, 1999999013L, 1999999049L, 1999999061L, 1999999081L, |
|
810 |
1999999087L, 1999999093L, 1999999097L, 1999999117L, 1999999121L, |
|
811 |
1999999151L, 1999999171L, 1999999207L, 1999999219L, 1999999271L, |
|
812 |
1999999321L, 1999999373L, 1999999423L, 1999999439L, 1999999499L, |
|
813 |
1999999553L, 1999999559L, 1999999571L, 1999999609L, 1999999613L, |
|
814 |
1999999621L, 1999999643L, 1999999649L, 1999999657L, 1999999747L, |
|
815 |
1999999763L, 1999999777L, 1999999811L, 1999999817L, 1999999829L, |
|
816 |
1999999853L, 1999999861L, 1999999871L, 1999999873 |
|
817 |
}; |
|
818 |
||
819 |
public static void nextProbablePrime() throws Exception { |
|
820 |
int failCount = 0; |
|
821 |
BigInteger p1, p2, p3; |
|
822 |
p1 = p2 = p3 = ZERO; |
|
823 |
||
824 |
// First test nextProbablePrime on the low range starting at zero |
|
825 |
for (int i=0; i<primesTo100.length; i++) { |
|
826 |
p1 = p1.nextProbablePrime(); |
|
827 |
if (p1.longValue() != primesTo100[i]) { |
|
828 |
System.err.println("low range primes failed"); |
|
829 |
System.err.println("p1 is "+p1); |
|
830 |
System.err.println("expected "+primesTo100[i]); |
|
831 |
failCount++; |
|
832 |
} |
|
833 |
} |
|
834 |
||
835 |
// Test nextProbablePrime on a relatively small, known prime sequence |
|
836 |
p1 = BigInteger.valueOf(aPrimeSequence[0]); |
|
837 |
for (int i=1; i<aPrimeSequence.length; i++) { |
|
838 |
p1 = p1.nextProbablePrime(); |
|
839 |
if (p1.longValue() != aPrimeSequence[i]) { |
|
840 |
System.err.println("prime sequence failed"); |
|
841 |
failCount++; |
|
842 |
} |
|
843 |
} |
|
844 |
||
845 |
// Next, pick some large primes, use nextProbablePrime to find the |
|
846 |
// next one, and make sure there are no primes in between |
|
847 |
for (int i=0; i<100; i+=10) { |
|
848 |
p1 = BigInteger.probablePrime(50 + i, rnd); |
|
849 |
p2 = p1.add(ONE); |
|
850 |
p3 = p1.nextProbablePrime(); |
|
851 |
while(p2.compareTo(p3) < 0) { |
|
852 |
if (p2.isProbablePrime(100)){ |
|
853 |
System.err.println("nextProbablePrime failed"); |
|
854 |
System.err.println("along range "+p1.toString(16)); |
|
855 |
System.err.println("to "+p3.toString(16)); |
|
856 |
failCount++; |
|
857 |
break; |
|
858 |
} |
|
859 |
p2 = p2.add(ONE); |
|
860 |
} |
|
861 |
} |
|
862 |
||
863 |
report("nextProbablePrime", failCount); |
|
864 |
} |
|
865 |
||
866 |
public static void serialize() throws Exception { |
|
867 |
int failCount = 0; |
|
868 |
||
869 |
String bitPatterns[] = { |
|
870 |
"ffffffff00000000ffffffff00000000ffffffff00000000", |
|
871 |
"ffffffffffffffffffffffff000000000000000000000000", |
|
872 |
"ffffffff0000000000000000000000000000000000000000", |
|
873 |
"10000000ffffffffffffffffffffffffffffffffffffffff", |
|
874 |
"100000000000000000000000000000000000000000000000", |
|
875 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
|
876 |
"-ffffffff00000000ffffffff00000000ffffffff00000000", |
|
877 |
"-ffffffffffffffffffffffff000000000000000000000000", |
|
878 |
"-ffffffff0000000000000000000000000000000000000000", |
|
879 |
"-10000000ffffffffffffffffffffffffffffffffffffffff", |
|
880 |
"-100000000000000000000000000000000000000000000000", |
|
881 |
"-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
|
882 |
}; |
|
883 |
||
884 |
for(int i = 0; i < bitPatterns.length; i++) { |
|
885 |
BigInteger b1 = new BigInteger(bitPatterns[i], 16); |
|
4527
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
886 |
BigInteger b2 = null; |
1826 | 887 |
|
888 |
File f = new File("serialtest"); |
|
8543
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
889 |
|
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
890 |
try (FileOutputStream fos = new FileOutputStream(f)) { |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
891 |
try (ObjectOutputStream oos = new ObjectOutputStream(fos)) { |
4527
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
892 |
oos.writeObject(b1); |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
893 |
oos.flush(); |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
894 |
} |
1826 | 895 |
|
8543
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
896 |
try (FileInputStream fis = new FileInputStream(f); |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
897 |
ObjectInputStream ois = new ObjectInputStream(fis)) |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
898 |
{ |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
899 |
b2 = (BigInteger)ois.readObject(); |
4527
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
900 |
} |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
901 |
|
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
902 |
if (!b1.equals(b2) || |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
903 |
!b1.equals(b1.or(b2))) { |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
904 |
failCount++; |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
905 |
System.err.println("Serialized failed for hex " + |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
906 |
b1.toString(16)); |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
907 |
} |
1826 | 908 |
} |
909 |
f.delete(); |
|
910 |
} |
|
911 |
||
912 |
for(int i=0; i<10; i++) { |
|
913 |
BigInteger b1 = fetchNumber(rnd.nextInt(100)); |
|
4527
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
914 |
BigInteger b2 = null; |
1826 | 915 |
File f = new File("serialtest"); |
8543
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
916 |
try (FileOutputStream fos = new FileOutputStream(f)) { |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
917 |
try (ObjectOutputStream oos = new ObjectOutputStream(fos)) { |
4527
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
918 |
oos.writeObject(b1); |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
919 |
oos.flush(); |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
920 |
} |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
921 |
|
8543
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
922 |
try (FileInputStream fis = new FileInputStream(f); |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
923 |
ObjectInputStream ois = new ObjectInputStream(fis)) |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
924 |
{ |
e5ec12a932da
7021209: convert lang, math, util to use try-with-resources
smarks
parents:
5506
diff
changeset
|
925 |
b2 = (BigInteger)ois.readObject(); |
4527
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
926 |
} |
f95d12f08613
6908541: Bad resource management in java/math/BigInteger/BigIntegerTest.java
darcy
parents:
1826
diff
changeset
|
927 |
} |
1826 | 928 |
|
929 |
if (!b1.equals(b2) || |
|
930 |
!b1.equals(b1.or(b2))) |
|
931 |
failCount++; |
|
932 |
f.delete(); |
|
933 |
} |
|
934 |
||
935 |
report("Serialize", failCount); |
|
936 |
} |
|
937 |
||
938 |
/** |
|
939 |
* Main to interpret arguments and run several tests. |
|
940 |
* |
|
19060 | 941 |
* Up to three arguments may be given to specify the SIZE of BigIntegers |
942 |
* used for call parameters 1, 2, and 3. The SIZE is interpreted as |
|
1826 | 943 |
* the maximum number of decimal digits that the parameters will have. |
944 |
* |
|
945 |
*/ |
|
946 |
public static void main(String[] args) throws Exception { |
|
947 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
948 |
// Some variables for sizing test numbers in bits |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
949 |
int order1 = ORDER_MEDIUM; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
950 |
int order2 = ORDER_SMALL; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
951 |
int order3 = ORDER_KARATSUBA; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
952 |
int order4 = ORDER_TOOM_COOK; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
953 |
|
1826 | 954 |
if (args.length >0) |
955 |
order1 = (int)((Integer.parseInt(args[0]))* 3.333); |
|
956 |
if (args.length >1) |
|
957 |
order2 = (int)((Integer.parseInt(args[1]))* 3.333); |
|
958 |
if (args.length >2) |
|
959 |
order3 = (int)((Integer.parseInt(args[2]))* 3.333); |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
960 |
if (args.length >3) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
961 |
order4 = (int)((Integer.parseInt(args[3]))* 3.333); |
1826 | 962 |
|
963 |
prime(); |
|
964 |
nextProbablePrime(); |
|
965 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
966 |
arithmetic(order1); // small numbers |
19393
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
967 |
arithmetic(order3); // Karatsuba range |
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
968 |
arithmetic(order4); // Toom-Cook / Burnikel-Ziegler range |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
969 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
970 |
divideAndRemainder(order1); // small numbers |
19393
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
971 |
divideAndRemainder(order3); // Karatsuba range |
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
972 |
divideAndRemainder(order4); // Toom-Cook / Burnikel-Ziegler range |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
973 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
974 |
pow(order1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
975 |
pow(order3); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
976 |
pow(order4); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
977 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
978 |
square(ORDER_MEDIUM); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
979 |
square(ORDER_KARATSUBA_SQUARE); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
980 |
square(ORDER_TOOM_COOK_SQUARE); |
1826 | 981 |
|
982 |
bitCount(); |
|
983 |
bitLength(); |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
984 |
bitOps(order1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
985 |
bitwise(order1); |
1826 | 986 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
987 |
shift(order1); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
988 |
|
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
989 |
byteArrayConv(order1); |
1826 | 990 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
991 |
modInv(order1); // small numbers |
19393
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
992 |
modInv(order3); // Karatsuba range |
d99a9ebf9c10
8022180: BigInteger Burnikel-Ziegler quotient and remainder calculation assumes quotient parameter is zero
bpb
parents:
19061
diff
changeset
|
993 |
modInv(order4); // Toom-Cook / Burnikel-Ziegler range |
1826 | 994 |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
995 |
modExp(order1, order2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
996 |
modExp2(order1); |
1826 | 997 |
|
998 |
stringConv(); |
|
999 |
serialize(); |
|
1000 |
||
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1001 |
multiplyLarge(); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1002 |
squareLarge(); |
19060 | 1003 |
divideLarge(); |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1004 |
|
1826 | 1005 |
if (failure) |
1006 |
throw new RuntimeException("Failure in BigIntegerTest."); |
|
1007 |
} |
|
1008 |
||
1009 |
/* |
|
1010 |
* Get a random or boundary-case number. This is designed to provide |
|
19061
d48848ef5670
8020641: Clean up some code style in recent BigInteger contributions
bpb
parents:
19060
diff
changeset
|
1011 |
* a lot of numbers that will find failure points, such as max sized |
1826 | 1012 |
* numbers, empty BigIntegers, etc. |
1013 |
* |
|
1014 |
* If order is less than 2, order is changed to 2. |
|
1015 |
*/ |
|
1016 |
private static BigInteger fetchNumber(int order) { |
|
1017 |
boolean negative = rnd.nextBoolean(); |
|
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1018 |
int numType = rnd.nextInt(7); |
1826 | 1019 |
BigInteger result = null; |
1020 |
if (order < 2) order = 2; |
|
1021 |
||
1022 |
switch (numType) { |
|
1023 |
case 0: // Empty |
|
1024 |
result = BigInteger.ZERO; |
|
1025 |
break; |
|
1026 |
||
1027 |
case 1: // One |
|
1028 |
result = BigInteger.ONE; |
|
1029 |
break; |
|
1030 |
||
1031 |
case 2: // All bits set in number |
|
1032 |
int numBytes = (order+7)/8; |
|
1033 |
byte[] fullBits = new byte[numBytes]; |
|
1034 |
for(int i=0; i<numBytes; i++) |
|
1035 |
fullBits[i] = (byte)0xff; |
|
1036 |
int excessBits = 8*numBytes - order; |
|
1037 |
fullBits[0] &= (1 << (8-excessBits)) - 1; |
|
1038 |
result = new BigInteger(1, fullBits); |
|
1039 |
break; |
|
1040 |
||
1041 |
case 3: // One bit in number |
|
1042 |
result = BigInteger.ONE.shiftLeft(rnd.nextInt(order)); |
|
1043 |
break; |
|
1044 |
||
1045 |
case 4: // Random bit density |
|
19060 | 1046 |
byte[] val = new byte[(order+7)/8]; |
1047 |
int iterations = rnd.nextInt(order); |
|
1048 |
for (int i=0; i<iterations; i++) { |
|
1049 |
int bitIdx = rnd.nextInt(order); |
|
1050 |
val[bitIdx/8] |= 1 << (bitIdx%8); |
|
1826 | 1051 |
} |
19060 | 1052 |
result = new BigInteger(1, val); |
1826 | 1053 |
break; |
18286
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1054 |
case 5: // Runs of consecutive ones and zeros |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1055 |
result = ZERO; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1056 |
int remaining = order; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1057 |
int bit = rnd.nextInt(2); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1058 |
while (remaining > 0) { |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1059 |
int runLength = Math.min(remaining, rnd.nextInt(order)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1060 |
result = result.shiftLeft(runLength); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1061 |
if (bit > 0) |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1062 |
result = result.add(ONE.shiftLeft(runLength).subtract(ONE)); |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1063 |
remaining -= runLength; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1064 |
bit = 1 - bit; |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1065 |
} |
b38489d5aadf
4837946: Faster multiplication and exponentiation of large integers
bpb
parents:
9035
diff
changeset
|
1066 |
break; |
1826 | 1067 |
|
1068 |
default: // random bits |
|
1069 |
result = new BigInteger(order, rnd); |
|
1070 |
} |
|
1071 |
||
1072 |
if (negative) |
|
1073 |
result = result.negate(); |
|
1074 |
||
1075 |
return result; |
|
1076 |
} |
|
1077 |
||
1078 |
static void report(String testName, int failCount) { |
|
1079 |
System.err.println(testName+": " + |
|
1080 |
(failCount==0 ? "Passed":"Failed("+failCount+")")); |
|
1081 |
if (failCount > 0) |
|
1082 |
failure = true; |
|
1083 |
} |
|
1084 |
} |