public BigInteger modPow(BigInteger exponent, BigInteger m) { if (m.sign <= 0) { throw new ArithmeticException("BigInteger: modulus not positive"); } BigInteger _base = this; if (m.isOne() | (exponent.sign > 0 & _base.sign == 0)) { return(BigInteger.ZERO); } if (_base.sign == 0 && exponent.sign == 0) { return(BigInteger.ONE); } if (exponent.sign < 0) { _base = modInverse(m); exponent = exponent.negate(); } // From now on: (m > 0) and (exponent >= 0) BigInteger res = (m.testBit(0)) ? Division.oddModPow(_base.abs(), exponent, m) : Division.evenModPow(_base.abs(), exponent, m); if ((_base.sign < 0) && exponent.testBit(0)) { // -b^e mod m == ((-1 mod m) * (b^e mod m)) mod m res = m.subtract(BigInteger.ONE).multiply(res).mod(m); } // else exponent is even, so base^exp is positive return(res); }
internal static BigInteger pow2ModPow(BigInteger _base, BigInteger exponent, int j) { // PRE: (base > 0), (exponent > 0) and (j > 0) BigInteger res = BigInteger.ONE; BigInteger e = exponent.copy(); BigInteger baseMod2toN = _base.copy(); BigInteger res2; /* * If 'base' is odd then it's coprime with 2^j and phi(2^j) = 2^(j-1); * so we can reduce reduce the exponent (mod 2^(j-1)). */ if (_base.testBit(0)) { inplaceModPow2(e, j - 1); } inplaceModPow2(baseMod2toN, j); for (int i = e.bitLength() - 1; i >= 0; i--) { res2 = res.copy(); inplaceModPow2(res2, j); res = res.multiply(res2); if (BitLevel.testBit(e, i)) { res = res.multiply(baseMod2toN); inplaceModPow2(res, j); } } inplaceModPow2(res, j); return(res); }
public BigInteger modInverse(BigInteger m) { if (m.sign <= 0) { throw new ArithmeticException("BigInteger: modulus not positive"); } // If both are even, no inverse exists if (!(testBit(0) || m.testBit(0))) { throw new ArithmeticException("BigInteger not invertible."); } if (m.isOne()) { return(ZERO); } // From now on: (m > 1) BigInteger res = Division.modInverseMontgomery(abs().mod(m), m); if (res.sign == 0) { throw new ArithmeticException("BigInteger not invertible."); } res = ((sign < 0) ? m.subtract(res) : res); return(res); }
private static int howManyIterations(BigInteger bi, int n) { int i = n - 1; if (bi.sign > 0) { while (!bi.testBit(i)) { i--; } return(n - 1 - i); } else { while (bi.testBit(i)) { i--; } return(n - 1 - Math.Max(i, bi.getLowestSetBit())); } }
internal static bool isProbablePrime(BigInteger n, int certainty) { // PRE: n >= 0; if ((certainty <= 0) || ((n.numberLength == 1) && (n.digits[0] == 2))) { return(true); } // To discard all even numbers if (!n.testBit(0)) { return(false); } // To check if 'n' exists in the table (it fit in 10 bits) if ((n.numberLength == 1) && ((n.digits[0] & 0XFFFFFC00) == 0)) { return(Array.BinarySearch(primes, n.digits[0]) >= 0); } // To check if 'n' is divisible by some prime of the table for (int i = 1; i < primes.Length; i++) { if (Division.remainderArrayByInt(n.digits, n.numberLength, primes[i]) == 0) { return(false); } } // To set the number of iterations necessary for Miller-Rabin test int ix; int bitLength = n.bitLength(); for (ix = 2; bitLength < BITS[ix]; ix++) { ; } certainty = Math.Min(ix, 1 + ((certainty - 1) >> 1)); return(millerRabin(n, certainty)); }
internal static bool isProbablePrime(BigInteger n, int certainty) { // PRE: n >= 0; if ((certainty <= 0) || ((n.numberLength == 1) && (n.digits[0] == 2))) { return true; } // To discard all even numbers if (!n.testBit(0)) { return false; } // To check if 'n' exists in the table (it fit in 10 bits) if ((n.numberLength == 1) && ((n.digits[0] & 0XFFFFFC00) == 0)) { return (Array.BinarySearch(primes, n.digits[0]) >= 0); } // To check if 'n' is divisible by some prime of the table for (int i = 1; i < primes.Length; i++) { if (Division.remainderArrayByInt(n.digits, n.numberLength, primes[i]) == 0) { return false; } } // To set the number of iterations necessary for Miller-Rabin test int ix; int bitLength = n.bitLength(); for (ix = 2; bitLength < BITS[ix]; ix++) { ; } certainty = Math.Min(ix, 1 + ((certainty - 1) >> 1)); return millerRabin(n, certainty); }
internal static BigInteger nextProbablePrime(BigInteger n) { // PRE: n >= 0 int i, j; int certainty; int gapSize = 1024; // for searching of the next probable prime number int[] modules = new int[primes.Length]; bool[] isDivisible = new bool[gapSize]; BigInteger startPoint; BigInteger probPrime; // If n < "last prime of table" searches next prime in the table if ((n.numberLength == 1) && (n.digits[0] >= 0) && (n.digits[0] < primes[primes.Length - 1])) { for (i = 0; n.digits[0] >= primes[i]; i++) { ; } return BIprimes[i]; } /* * Creates a "N" enough big to hold the next probable prime Note that: N < * "next prime" < 2*N */ startPoint = new BigInteger(1, n.numberLength, new int[n.numberLength + 1]); Array.Copy(n.digits, startPoint.digits, n.numberLength); // To fix N to the "next odd number" if (n.testBit(0)) { Elementary.inplaceAdd(startPoint, 2); } else { startPoint.digits[0] |= 1; } // To set the improved certainly of Miller-Rabin j = startPoint.bitLength(); for (certainty = 2; j < BITS[certainty]; certainty++) { ; } // To calculate modules: N mod p1, N mod p2, ... for first primes. for (i = 0; i < primes.Length; i++) { modules[i] = Division.remainder(startPoint, primes[i]) - gapSize; } while (true) { // At this point, all numbers in the gap are initialized as // probably primes Array.Clear(isDivisible, 0, isDivisible.Length); // To discard multiples of first primes for (i = 0; i < primes.Length; i++) { modules[i] = (modules[i] + gapSize) % primes[i]; j = (modules[i] == 0) ? 0 : (primes[i] - modules[i]); for (; j < gapSize; j += primes[i]) { isDivisible[j] = true; } } // To execute Miller-Rabin for non-divisible numbers by all first // primes for (j = 0; j < gapSize; j++) { if (!isDivisible[j]) { probPrime = startPoint.copy(); Elementary.inplaceAdd(probPrime, j); if (millerRabin(probPrime, certainty)) { return probPrime; } } } Elementary.inplaceAdd(startPoint, gapSize); } }
internal static BigInteger nextProbablePrime(BigInteger n) { // PRE: n >= 0 int i, j; int certainty; int gapSize = 1024; // for searching of the next probable prime number int[] modules = new int[primes.Length]; bool[] isDivisible = new bool[gapSize]; BigInteger startPoint; BigInteger probPrime; // If n < "last prime of table" searches next prime in the table if ((n.numberLength == 1) && (n.digits[0] >= 0) && (n.digits[0] < primes[primes.Length - 1])) { for (i = 0; n.digits[0] >= primes[i]; i++) { ; } return(BIprimes[i]); } /* * Creates a "N" enough big to hold the next probable prime Note that: N < * "next prime" < 2*N */ startPoint = new BigInteger(1, n.numberLength, new int[n.numberLength + 1]); Array.Copy(n.digits, startPoint.digits, n.numberLength); // To fix N to the "next odd number" if (n.testBit(0)) { Elementary.inplaceAdd(startPoint, 2); } else { startPoint.digits[0] |= 1; } // To set the improved certainly of Miller-Rabin j = startPoint.bitLength(); for (certainty = 2; j < BITS[certainty]; certainty++) { ; } // To calculate modules: N mod p1, N mod p2, ... for first primes. for (i = 0; i < primes.Length; i++) { modules[i] = Division.remainder(startPoint, primes[i]) - gapSize; } while (true) { // At this point, all numbers in the gap are initialized as // probably primes Array.Clear(isDivisible, 0, isDivisible.Length); // To discard multiples of first primes for (i = 0; i < primes.Length; i++) { modules[i] = (modules[i] + gapSize) % primes[i]; j = (modules[i] == 0) ? 0 : (primes[i] - modules[i]); for (; j < gapSize; j += primes[i]) { isDivisible[j] = true; } } // To execute Miller-Rabin for non-divisible numbers by all first // primes for (j = 0; j < gapSize; j++) { if (!isDivisible[j]) { probPrime = startPoint.copy(); Elementary.inplaceAdd(probPrime, j); if (millerRabin(probPrime, certainty)) { return(probPrime); } } } Elementary.inplaceAdd(startPoint, gapSize); } }
public BigInteger modPow(BigInteger exponent, BigInteger m) { if (m.sign <= 0) { throw new ArithmeticException("BigInteger: modulus not positive"); } BigInteger _base = this; if (m.isOne() | (exponent.sign > 0 & _base.sign == 0)) { return BigInteger.ZERO; } if (_base.sign == 0 && exponent.sign == 0) { return BigInteger.ONE; } if (exponent.sign < 0) { _base = modInverse(m); exponent = exponent.negate(); } // From now on: (m > 0) and (exponent >= 0) BigInteger res = (m.testBit(0)) ? Division.oddModPow(_base.abs(), exponent, m) : Division.evenModPow(_base.abs(), exponent, m); if ((_base.sign < 0) && exponent.testBit(0)) { // -b^e mod m == ((-1 mod m) * (b^e mod m)) mod m res = m.subtract(BigInteger.ONE).multiply(res).mod(m); } // else exponent is even, so base^exp is positive return res; }
public BigInteger modInverse(BigInteger m) { if (m.sign <= 0) { throw new ArithmeticException("BigInteger: modulus not positive"); } // If both are even, no inverse exists if (!(testBit(0) || m.testBit(0))) { throw new ArithmeticException("BigInteger not invertible."); } if (m.isOne()) { return ZERO; } // From now on: (m > 1) BigInteger res = Division.modInverseMontgomery(abs().mod(m), m); if (res.sign == 0) { throw new ArithmeticException("BigInteger not invertible."); } res = ((sign < 0) ? m.subtract(res) : res); return res; }
private static int howManyIterations(BigInteger bi, int n) { int i = n - 1; if (bi.sign > 0) { while (!bi.testBit(i)) i--; return n - 1 - i; } else { while (bi.testBit(i)) i--; return n - 1 - Math.Max(i, bi.getLowestSetBit()); } }
internal static BigInteger pow2ModPow(BigInteger _base, BigInteger exponent, int j) { // PRE: (base > 0), (exponent > 0) and (j > 0) BigInteger res = BigInteger.ONE; BigInteger e = exponent.copy(); BigInteger baseMod2toN = _base.copy(); BigInteger res2; /* * If 'base' is odd then it's coprime with 2^j and phi(2^j) = 2^(j-1); * so we can reduce reduce the exponent (mod 2^(j-1)). */ if (_base.testBit(0)) { inplaceModPow2(e, j - 1); } inplaceModPow2(baseMod2toN, j); for (int i = e.bitLength() - 1; i >= 0; i--) { res2 = res.copy(); inplaceModPow2(res2, j); res = res.multiply(res2); if (BitLevel.testBit(e, i)) { res = res.multiply(baseMod2toN); inplaceModPow2(res, j); } } inplaceModPow2(res, j); return res; }
internal static BigInteger modInverseMontgomery(BigInteger a, BigInteger p) { if (a.sign == 0){ // ZERO hasn't inverse throw new ArithmeticException("BigInteger not invertible"); } if (!p.testBit(0)){ // montgomery inverse require even modulo return modInverseLorencz(a, p); } int m = p.numberLength * 32; // PRE: a \in [1, p - 1] BigInteger u, v, r, s; u = p.copy(); // make copy to use inplace method v = a.copy(); int max = Math.Max(v.numberLength, u.numberLength); r = new BigInteger(1, 1, new int[max + 1]); s = new BigInteger(1, 1, new int[max + 1]); s.digits[0] = 1; // s == 1 && v == 0 int k = 0; int lsbu = u.getLowestSetBit(); int lsbv = v.getLowestSetBit(); int toShift; if (lsbu > lsbv) { BitLevel.inplaceShiftRight(u, lsbu); BitLevel.inplaceShiftRight(v, lsbv); BitLevel.inplaceShiftLeft(r, lsbv); k += lsbu - lsbv; } else { BitLevel.inplaceShiftRight(u, lsbu); BitLevel.inplaceShiftRight(v, lsbv); BitLevel.inplaceShiftLeft(s, lsbu); k += lsbv - lsbu; } r.sign = 1; while (v.signum() > 0) { // INV v >= 0, u >= 0, v odd, u odd (except last iteration when v is even (0)) while (u.compareTo(v) > BigInteger.EQUALS) { Elementary.inplaceSubtract(u, v); toShift = u.getLowestSetBit(); BitLevel.inplaceShiftRight(u, toShift); Elementary.inplaceAdd(r, s); BitLevel.inplaceShiftLeft(s, toShift); k += toShift; } while (u.compareTo(v) <= BigInteger.EQUALS) { Elementary.inplaceSubtract(v, u); if (v.signum() == 0) break; toShift = v.getLowestSetBit(); BitLevel.inplaceShiftRight(v, toShift); Elementary.inplaceAdd(s, r); BitLevel.inplaceShiftLeft(r, toShift); k += toShift; } } if (!u.isOne()){ // in u is stored the gcd throw new ArithmeticException("BigInteger not invertible."); } if (r.compareTo(p) >= BigInteger.EQUALS) { Elementary.inplaceSubtract(r, p); } r = p.subtract(r); // Have pair: ((BigInteger)r, (Integer)k) where r == a^(-1) * 2^k mod (module) int n1 = calcN(p); if (k > m) { r = monPro(r, BigInteger.ONE, p, n1); k = k - m; } r = monPro(r, BigInteger.getPowerOfTwo(m - k), p, n1); return r; }
internal static BigInteger modInverseLorencz(BigInteger a, BigInteger modulo) { int max = Math.Max(a.numberLength, modulo.numberLength); int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation int[] vDigits = new int[max + 1]; Array.Copy(modulo.digits, uDigits, modulo.numberLength); Array.Copy(a.digits, vDigits, a.numberLength); BigInteger u = new BigInteger(modulo.sign, modulo.numberLength, uDigits); BigInteger v = new BigInteger(a.sign, a.numberLength, vDigits); BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO; BigInteger s = new BigInteger(1, 1, new int[max + 1]); s.digits[0] = 1; // r == 0 && s == 1, but with enough place int coefU = 0, coefV = 0; int n = modulo.bitLength(); int k; while (!isPowerOfTwo(u, coefU) && !isPowerOfTwo(v, coefV)) { // modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if k = howManyIterations(u, n); if (k != 0) { BitLevel.inplaceShiftLeft(u, k); if (coefU >= coefV) { BitLevel.inplaceShiftLeft(r, k); } else { BitLevel.inplaceShiftRight(s, Math.Min(coefV - coefU, k)); if (k - ( coefV - coefU ) > 0) { BitLevel.inplaceShiftLeft(r, k - coefV + coefU); } } coefU += k; } k = howManyIterations(v, n); if (k != 0) { BitLevel.inplaceShiftLeft(v, k); if (coefV >= coefU) { BitLevel.inplaceShiftLeft(s, k); } else { BitLevel.inplaceShiftRight(r, Math.Min(coefU - coefV, k)); if (k - ( coefU - coefV ) > 0) { BitLevel.inplaceShiftLeft(s, k - coefU + coefV); } } coefV += k; } if (u.signum() == v.signum()) { if (coefU <= coefV) { Elementary.completeInPlaceSubtract(u, v); Elementary.completeInPlaceSubtract(r, s); } else { Elementary.completeInPlaceSubtract(v, u); Elementary.completeInPlaceSubtract(s, r); } } else { if (coefU <= coefV) { Elementary.completeInPlaceAdd(u, v); Elementary.completeInPlaceAdd(r, s); } else { Elementary.completeInPlaceAdd(v, u); Elementary.completeInPlaceAdd(s, r); } } if (v.signum() == 0 || u.signum() == 0){ throw new ArithmeticException("BigInteger not invertible"); } } if (isPowerOfTwo(v, coefV)) { r = s; if (v.signum() != u.signum()) u = u.negate(); } if (u.testBit(n)) { if (r.signum() < 0) { r = r.negate(); } else { r = modulo.subtract(r); } } if (r.signum() < 0) { r = r.add(modulo); } return r; }
internal static BigInteger modInverseLorencz(BigInteger a, BigInteger modulo) { int max = Math.Max(a.numberLength, modulo.numberLength); int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation int[] vDigits = new int[max + 1]; Array.Copy(modulo.digits, uDigits, modulo.numberLength); Array.Copy(a.digits, vDigits, a.numberLength); BigInteger u = new BigInteger(modulo.sign, modulo.numberLength, uDigits); BigInteger v = new BigInteger(a.sign, a.numberLength, vDigits); BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO; BigInteger s = new BigInteger(1, 1, new int[max + 1]); s.digits[0] = 1; // r == 0 && s == 1, but with enough place int coefU = 0, coefV = 0; int n = modulo.bitLength(); int k; while (!isPowerOfTwo(u, coefU) && !isPowerOfTwo(v, coefV)) { // modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if k = howManyIterations(u, n); if (k != 0) { BitLevel.inplaceShiftLeft(u, k); if (coefU >= coefV) { BitLevel.inplaceShiftLeft(r, k); } else { BitLevel.inplaceShiftRight(s, Math.Min(coefV - coefU, k)); if (k - (coefV - coefU) > 0) { BitLevel.inplaceShiftLeft(r, k - coefV + coefU); } } coefU += k; } k = howManyIterations(v, n); if (k != 0) { BitLevel.inplaceShiftLeft(v, k); if (coefV >= coefU) { BitLevel.inplaceShiftLeft(s, k); } else { BitLevel.inplaceShiftRight(r, Math.Min(coefU - coefV, k)); if (k - (coefU - coefV) > 0) { BitLevel.inplaceShiftLeft(s, k - coefU + coefV); } } coefV += k; } if (u.signum() == v.signum()) { if (coefU <= coefV) { Elementary.completeInPlaceSubtract(u, v); Elementary.completeInPlaceSubtract(r, s); } else { Elementary.completeInPlaceSubtract(v, u); Elementary.completeInPlaceSubtract(s, r); } } else { if (coefU <= coefV) { Elementary.completeInPlaceAdd(u, v); Elementary.completeInPlaceAdd(r, s); } else { Elementary.completeInPlaceAdd(v, u); Elementary.completeInPlaceAdd(s, r); } } if (v.signum() == 0 || u.signum() == 0) { throw new ArithmeticException("BigInteger not invertible"); } } if (isPowerOfTwo(v, coefV)) { r = s; if (v.signum() != u.signum()) { u = u.negate(); } } if (u.testBit(n)) { if (r.signum() < 0) { r = r.negate(); } else { r = modulo.subtract(r); } } if (r.signum() < 0) { r = r.add(modulo); } return(r); }
internal static BigInteger modInverseMontgomery(BigInteger a, BigInteger p) { if (a.sign == 0) { // ZERO hasn't inverse throw new ArithmeticException("BigInteger not invertible"); } if (!p.testBit(0)) { // montgomery inverse require even modulo return(modInverseLorencz(a, p)); } int m = p.numberLength * 32; // PRE: a \in [1, p - 1] BigInteger u, v, r, s; u = p.copy(); // make copy to use inplace method v = a.copy(); int max = Math.Max(v.numberLength, u.numberLength); r = new BigInteger(1, 1, new int[max + 1]); s = new BigInteger(1, 1, new int[max + 1]); s.digits[0] = 1; // s == 1 && v == 0 int k = 0; int lsbu = u.getLowestSetBit(); int lsbv = v.getLowestSetBit(); int toShift; if (lsbu > lsbv) { BitLevel.inplaceShiftRight(u, lsbu); BitLevel.inplaceShiftRight(v, lsbv); BitLevel.inplaceShiftLeft(r, lsbv); k += lsbu - lsbv; } else { BitLevel.inplaceShiftRight(u, lsbu); BitLevel.inplaceShiftRight(v, lsbv); BitLevel.inplaceShiftLeft(s, lsbu); k += lsbv - lsbu; } r.sign = 1; while (v.signum() > 0) { // INV v >= 0, u >= 0, v odd, u odd (except last iteration when v is even (0)) while (u.compareTo(v) > BigInteger.EQUALS) { Elementary.inplaceSubtract(u, v); toShift = u.getLowestSetBit(); BitLevel.inplaceShiftRight(u, toShift); Elementary.inplaceAdd(r, s); BitLevel.inplaceShiftLeft(s, toShift); k += toShift; } while (u.compareTo(v) <= BigInteger.EQUALS) { Elementary.inplaceSubtract(v, u); if (v.signum() == 0) { break; } toShift = v.getLowestSetBit(); BitLevel.inplaceShiftRight(v, toShift); Elementary.inplaceAdd(s, r); BitLevel.inplaceShiftLeft(r, toShift); k += toShift; } } if (!u.isOne()) { // in u is stored the gcd throw new ArithmeticException("BigInteger not invertible."); } if (r.compareTo(p) >= BigInteger.EQUALS) { Elementary.inplaceSubtract(r, p); } r = p.subtract(r); // Have pair: ((BigInteger)r, (Integer)k) where r == a^(-1) * 2^k mod (module) int n1 = calcN(p); if (k > m) { r = monPro(r, BigInteger.ONE, p, n1); k = k - m; } r = monPro(r, BigInteger.getPowerOfTwo(m - k), p, n1); return(r); }