mawinkle 6 年之前
父节点
当前提交
33d3ead79e
共有 1 个文件被更改,包括 42 次插入47 次删除
  1. 42 47
      kek/BigInteger.cpp

+ 42 - 47
kek/BigInteger.cpp

@@ -1782,47 +1782,47 @@ class BigInteger{
 
     // Range-check everything.
     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);
         }
     }
 
     // Make sure that the int array z (which is expected to contain
     // the result of a Montgomery multiplication) is present and
     // sufficiently large.
-    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;
     }
 
     // These methods are intended to be be replaced by virtual machine
     // intrinsics.
-    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}; // Sentinel
+    static std::vector<int> bnExpModThreshTable = {7, 25, 81, 241, 673, 1793,
+                                                std::numeric_limits<int>::max()}; // Sentinel
 
     
     BigInteger oddModPow(BigInteger y, BigInteger z) {
@@ -1885,31 +1885,31 @@ class BigInteger{
      */
         // Special case for exponent of one
         if (y.equals(ONE))
-            return this;
+            return *this;
 
         // Special case for base of zero
         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();
 
         // Make modLen even. It is conventional to use a cryptographic
         // modulus that is 512, 768, 1024, or 2048 bits, so this code
         // will not normally be executed. However, it is necessary for
         // the correct functioning of the HotSpot intrinsics.
         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++;
         }
 
         // Select an appropriate window size
         int wbits = 0;
-        int ebits = bitLength(exp, exp.length);
+        int ebits = bitLength(exp, exp.size());
         // if exponent is 65537 (0x10001), use minimum window size
         if ((ebits != 17) || (exp[0] != 65537)) {
             while (ebits > bnExpModThreshTable[wbits]) {
@@ -1921,21 +1921,21 @@ class BigInteger{
         int tblmask = 1 << wbits;
 
         // Allocate table for precomputed odd powers of base in Montgomery form
-        int[][] table = new int[tblmask][];
+        std::vector<std::vector<int>> table(tblmask);// = new int[tblmask][];
         for (int i=0; i < tblmask; i++)
-            table[i] = new int[modLen];
+            table[i] = std::vector<int>(modLen);
 
         // Compute the modular inverse of the least significant 64-bit
         // digit of the modulus
-        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);
 
         // Convert base to Montgomery form
-        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(); // MutableBigInteger.divide() assumes that its
                         // divisor is in normal form.
 
@@ -1943,18 +1943,18 @@ class BigInteger{
         table[0] = r.toIntArray();
 
         // Pad table[0] with leading zeros so its length is at least modLen
-        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);// = new int[modLen];
+           std::copy(table[0].begin(),table[0].end(), t2.begin());
            table[0] = t2;
         }
 
         // Set b to the square of the base
-        int[] b = montgomerySquare(table[0], mod, modLen, inv, null);
+        std::vector<int> b = montgomerySquare(table[0], mod, modLen, inv, null);
 
         // Set t to high half of b
-        int[] t = Arrays.copyOf(b, modLen);
+        std::vector<int> t(b.begin(), b.begin() + modLen);
 
         // Fill in the table with odd powers of the base
         for (int i=1; i < tblmask; i++) {
@@ -1962,7 +1962,7 @@ class BigInteger{
         }
 
         // Pre load the window that slides over the exponent
-        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 {
-            // Possible int overflow in {@code -n} is not a trouble,
-            // because shiftLeft considers its argument unsigned
-            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) {  // lowestSetBit not initialized yet
             lsb = 0;
             if (signum == 0) {
@@ -2481,7 +2478,6 @@ class BigInteger{
 
     
     int bitLength() {
-        @SuppressWarnings("deprecation") int n = bitLength - 1;
         if (n == -1) { // bitLength not initialized yet
             int[] m = mag;
             int len = m.length;
@@ -2508,7 +2504,6 @@ class BigInteger{
 
     
     int bitCount() {
-        @SuppressWarnings("deprecation") int bc = bitCount - 1;
         if (bc == -1) {  // bitCount not initialized yet
             bc = 0;      // offset by one to initialize
             // Count the bits in the magnitude