Other minor dependencies updates in 7.1.x patch

This commit is contained in:
Christopher J. Brody
2018-11-22 09:57:22 -05:00
parent d918c7a83c
commit 5a5f544a48
18 changed files with 401 additions and 237 deletions
+31 -18
View File
@@ -785,29 +785,44 @@ var bigInt = (function (undefined) {
if (n.isUnit()) return false;
if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
if (n.lesser(25)) return true;
if (n.lesser(49)) return true;
// we don't know if it's prime: let the other functions figure it out
}
BigInteger.prototype.isPrime = function () {
var isPrime = isBasicPrime(this);
if (isPrime !== undefined) return isPrime;
var n = this.abs(),
nPrev = n.prev();
var a = [2, 3, 5, 7, 11, 13, 17, 19],
function millerRabinTest(n, a) {
var nPrev = n.prev(),
b = nPrev,
r = 0,
d, t, i, x;
while (b.isEven()) b = b.divide(2);
for (i = 0; i < a.length; i++) {
while (b.isEven()) b = b.divide(2), r++;
next : for (i = 0; i < a.length; i++) {
if (n.lesser(a[i])) continue;
x = bigInt(a[i]).modPow(b, n);
if (x.equals(Integer[1]) || x.equals(nPrev)) continue;
for (t = true, d = b; t && d.lesser(nPrev); d = d.multiply(2)) {
for (d = r - 1; d != 0; d--) {
x = x.square().mod(n);
if (x.equals(nPrev)) t = false;
if (x.isUnit()) return false;
if (x.equals(nPrev)) continue next;
}
if (t) return false;
return false;
}
return true;
}
// Set "strict" to true to force GRH-supported lower bound of 2*log(N)^2
BigInteger.prototype.isPrime = function (strict) {
var isPrime = isBasicPrime(this);
if (isPrime !== undefined) return isPrime;
var n = this.abs();
var bits = n.bitLength();
if(bits <= 64)
return millerRabinTest(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022]);
var logN = Math.log(2) * bits;
var t = Math.ceil((strict === true) ? (2 * Math.pow(logN, 2)) : logN);
for (var a = [], i = 0; i < t; i++) {
a.push(bigInt(i + 2));
}
return millerRabinTest(n, a);
};
SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
@@ -816,12 +831,10 @@ var bigInt = (function (undefined) {
if (isPrime !== undefined) return isPrime;
var n = this.abs();
var t = iterations === undefined ? 5 : iterations;
// use the Fermat primality test
for (var i = 0; i < t; i++) {
var a = bigInt.randBetween(2, n.minus(2));
if (!a.modPow(n.prev(), n).isUnit()) return false; // definitely composite
for (var a = [], i = 0; i < t; i++) {
a.push(bigInt.randBetween(2, n.minus(2)));
}
return true; // large chance of being prime
return millerRabinTest(n, a);
};
SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;