|
@@ -1782,47 +1782,47 @@ class BigInteger{
|
|
|
|
|
|
|
|
|
static void implMontgomeryMultiplyChecks
|
|
|
- (int[] a, int[] b, int[] n, int len, int[] product) {
|
|
|
+ (std::vector<int> a, std::vector<int> b, std::vector<int> n, int len, std::vector<int> product) {
|
|
|
if (len % 2 != 0) {
|
|
|
- throw new IllegalArgumentException("input array length must be even: " + len);
|
|
|
+ throw std::invalid_argument("input array length must be even: " + std::to_string(len));
|
|
|
}
|
|
|
|
|
|
if (len < 1) {
|
|
|
- throw new IllegalArgumentException("invalid input length: " + len);
|
|
|
+ throw std::invalid_argument("invalid input length: " + std::to_string(len));
|
|
|
}
|
|
|
|
|
|
- if (len > a.length ||
|
|
|
- len > b.length ||
|
|
|
- len > n.length ||
|
|
|
- (product != null && len > product.length)) {
|
|
|
- throw new IllegalArgumentException("input array length out of bound: " + len);
|
|
|
+ if (len > a.size() ||
|
|
|
+ len > b.size() ||
|
|
|
+ len > n.size() ||
|
|
|
+ (len > product.size())) {
|
|
|
+ throw std::invalid_argument("input array length out of bound: " + len);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- static int[] materialize(int[] z, int len) {
|
|
|
- if (z == null || z.length < len)
|
|
|
- z = new int[len];
|
|
|
+ static std::vector<int> materialize(std::vector<int> z, int len) {
|
|
|
+ if (z.size() < len)
|
|
|
+ z = std::vector<int>(len);
|
|
|
return z;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- static int[] implMontgomeryMultiply(int[] a, int[] b, int[] n, int len,
|
|
|
- long inv, int[] product) {
|
|
|
+ static std::vector<int> implMontgomeryMultiply(std::vector<int> a, std::vector<int> b, std::vector<int> n, int len,
|
|
|
+ std::int64_t inv, std::vector<int> product) {
|
|
|
product = multiplyToLen(a, len, b, len, product);
|
|
|
return montReduce(product, n, len, (int)inv);
|
|
|
}
|
|
|
- static int[] implMontgomerySquare(int[] a, int[] n, int len,
|
|
|
- long inv, int[] product) {
|
|
|
+ static std::vector<int> implMontgomerySquare(std::vector<int> a, std::vector<int> n, int len,
|
|
|
+ std::int64_t inv, std::vector<int> product) {
|
|
|
product = squareToLen(a, len, product);
|
|
|
return montReduce(product, n, len, (int)inv);
|
|
|
}
|
|
|
|
|
|
- static int[] bnExpModThreshTable = {7, 25, 81, 241, 673, 1793,
|
|
|
- Integer.MAX_VALUE};
|
|
|
+ static std::vector<int> bnExpModThreshTable = {7, 25, 81, 241, 673, 1793,
|
|
|
+ std::numeric_limits<int>::max()};
|
|
|
|
|
|
|
|
|
BigInteger oddModPow(BigInteger y, BigInteger z) {
|
|
@@ -1885,31 +1885,31 @@ class BigInteger{
|
|
|
*/
|
|
|
|
|
|
if (y.equals(ONE))
|
|
|
- return this;
|
|
|
+ return *this;
|
|
|
|
|
|
|
|
|
if (signum == 0)
|
|
|
return ZERO;
|
|
|
|
|
|
- int[] base = mag.clone();
|
|
|
- int[] exp = y.mag;
|
|
|
- int[] mod = z.mag;
|
|
|
- int modLen = mod.length;
|
|
|
+ std::vector<int> base = mag.clone();
|
|
|
+ std::vector<int> exp = y.mag;
|
|
|
+ std::vector<int> mod = z.mag;
|
|
|
+ int modLen = mod.size();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((modLen & 1) != 0) {
|
|
|
- int[] x = new int[modLen + 1];
|
|
|
- System.arraycopy(mod, 0, x, 1, modLen);
|
|
|
- mod = x;
|
|
|
+ std::vector<int> x(modlen + 1);
|
|
|
+ std::copy(mod.begin(), mod.begin() + modLen, x.begin());
|
|
|
+ mod = std::move(x);
|
|
|
modLen++;
|
|
|
}
|
|
|
|
|
|
|
|
|
int wbits = 0;
|
|
|
- int ebits = bitLength(exp, exp.length);
|
|
|
+ int ebits = bitLength(exp, exp.size());
|
|
|
|
|
|
if ((ebits != 17) || (exp[0] != 65537)) {
|
|
|
while (ebits > bnExpModThreshTable[wbits]) {
|
|
@@ -1921,21 +1921,21 @@ class BigInteger{
|
|
|
int tblmask = 1 << wbits;
|
|
|
|
|
|
|
|
|
- int[][] table = new int[tblmask][];
|
|
|
+ std::vector<std::vector<int>> table(tblmask);
|
|
|
for (int i=0; i < tblmask; i++)
|
|
|
- table[i] = new int[modLen];
|
|
|
+ table[i] = std::vector<int>(modLen);
|
|
|
|
|
|
|
|
|
|
|
|
- long n0 = (mod[modLen-1] & LONG_MASK) + ((mod[modLen-2] & LONG_MASK) << 32);
|
|
|
- long inv = -MutableBigInteger.inverseMod64(n0);
|
|
|
+ std::int64_t n0 = (mod[modLen-1] & LONG_MASK) + ((mod[modLen-2] & LONG_MASK) << 32);
|
|
|
+ std::int64_t inv = -MutableBigInteger.inverseMod64(n0);
|
|
|
|
|
|
|
|
|
- int[] a = leftShift(base, base.length, modLen << 5);
|
|
|
+ std::vector<int> a = leftShift(base, base.length, modLen << 5);
|
|
|
|
|
|
- MutableBigInteger q = new MutableBigInteger(),
|
|
|
- a2 = new MutableBigInteger(a),
|
|
|
- b2 = new MutableBigInteger(mod);
|
|
|
+ MutableBigInteger q = MutableBigInteger(),
|
|
|
+ a2 = MutableBigInteger(a),
|
|
|
+ b2 = MutableBigInteger(mod);
|
|
|
b2.normalize();
|
|
|
|
|
|
|
|
@@ -1943,18 +1943,18 @@ class BigInteger{
|
|
|
table[0] = r.toIntArray();
|
|
|
|
|
|
|
|
|
- if (table[0].length < modLen) {
|
|
|
- int offset = modLen - table[0].length;
|
|
|
- int[] t2 = new int[modLen];
|
|
|
- System.arraycopy(table[0], 0, t2, offset, table[0].length);
|
|
|
+ if (table[0].size() < modLen) {
|
|
|
+ int offset = modLen - table[0].size();
|
|
|
+ std::vector<int> t2(modLen);
|
|
|
+ std::copy(table[0].begin(),table[0].end(), t2.begin());
|
|
|
table[0] = t2;
|
|
|
}
|
|
|
|
|
|
|
|
|
- int[] b = montgomerySquare(table[0], mod, modLen, inv, null);
|
|
|
+ std::vector<int> b = montgomerySquare(table[0], mod, modLen, inv, null);
|
|
|
|
|
|
|
|
|
- int[] t = Arrays.copyOf(b, modLen);
|
|
|
+ std::vector<int> t(b.begin(), b.begin() + modLen);
|
|
|
|
|
|
|
|
|
for (int i=1; i < tblmask; i++) {
|
|
@@ -1962,7 +1962,7 @@ class BigInteger{
|
|
|
}
|
|
|
|
|
|
|
|
|
- int bitpos = 1 << ((ebits-1) & (32-1));
|
|
|
+ unsigned int bitpos = 1 << ((ebits-1) & (32-1));
|
|
|
|
|
|
int buf = 0;
|
|
|
int elen = exp.length;
|
|
@@ -2286,9 +2286,7 @@ class BigInteger{
|
|
|
} else if (n == 0) {
|
|
|
return this;
|
|
|
} else {
|
|
|
-
|
|
|
-
|
|
|
- return new BigInteger(shiftLeft(mag, -n), signum);
|
|
|
+ return BigInteger(shiftLeft(mag, -n), signum);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -2459,7 +2457,6 @@ class BigInteger{
|
|
|
|
|
|
|
|
|
int getLowestSetBit() {
|
|
|
- @SuppressWarnings("deprecation") int lsb = lowestSetBit - 2;
|
|
|
if (lsb == -2) {
|
|
|
lsb = 0;
|
|
|
if (signum == 0) {
|
|
@@ -2481,7 +2478,6 @@ class BigInteger{
|
|
|
|
|
|
|
|
|
int bitLength() {
|
|
|
- @SuppressWarnings("deprecation") int n = bitLength - 1;
|
|
|
if (n == -1) {
|
|
|
int[] m = mag;
|
|
|
int len = m.length;
|
|
@@ -2508,7 +2504,6 @@ class BigInteger{
|
|
|
|
|
|
|
|
|
int bitCount() {
|
|
|
- @SuppressWarnings("deprecation") int bc = bitCount - 1;
|
|
|
if (bc == -1) {
|
|
|
bc = 0;
|
|
|
|