Fixed eslint problem with BigInt. It's also good for its use with webpack/babel

This commit is contained in:
Juan Hernández Serrano 2019-03-25 18:10:40 +01:00
parent 1ede4f712c
commit 1c5039b827
2 changed files with 85 additions and 82 deletions

View File

@ -23,5 +23,8 @@
"error",
"always"
]
},
"globals": {
"BigInt": "readonly"
}
}

View File

@ -9,7 +9,7 @@
*/
const abs = function (a) {
a = BigInt(a);
return (a >= 0n) ? a : -a;
return (a >= BigInt(0)) ? a : -a;
};
/**
@ -23,15 +23,15 @@ const abs = function (a) {
const gcd = function (a, b) {
a = abs(a);
b = abs(b);
let shift = 0n;
while (!((a | b) & 1n)) {
a >>= 1n;
b >>= 1n;
let shift = BigInt(0);
while (!((a | b) & BigInt(1))) {
a >>= BigInt(1);
b >>= BigInt(1);
shift++;
}
while (!(a & 1n)) a >>= 1n;
while (!(a & BigInt(1))) a >>= BigInt(1);
do {
while (!(b & 1n)) b >>= 1n;
while (!(b & BigInt(1))) b >>= BigInt(1);
if (a > b) {
let x = a;
a = b;
@ -88,12 +88,12 @@ const toZn = function (a, n) {
const eGcd = function (a, b) {
a = BigInt(a);
b = BigInt(b);
let x = 0n;
let y = 1n;
let u = 1n;
let v = 0n;
let x = BigInt(0);
let y = BigInt(1);
let u = BigInt(1);
let v = BigInt(0);
while (a !== 0n) {
while (a !== BigInt(0)) {
let q = b / a;
let r = b % a;
let m = x - (u * q);
@ -109,7 +109,7 @@ const eGcd = function (a, b) {
b: b,
x: x,
y: y
}
};
};
/**
@ -122,7 +122,7 @@ const eGcd = function (a, b) {
*/
const modInv = function (a, n) {
let egcd = eGcd(a, n);
if (egcd.b !== 1n) {
if (egcd.b !== BigInt(1)) {
return null; // modular inverse does not exist
} else {
return toZn(egcd.x, n);
@ -142,15 +142,15 @@ const modPow = function (a, b, n) {
n = BigInt(n);
a = toZn(a, n);
b = BigInt(b);
if (b < 0n) {
if (b < BigInt(0)) {
return modInv(modPow(a, abs(b), n), n);
}
let result = 1n;
let result = BigInt(1);
let x = a;
while (b > 0) {
var leastSignificantBit = b % 2n;
b = b / 2n;
if (leastSignificantBit == 1n) {
var leastSignificantBit = b % BigInt(2);
b = b / BigInt(2);
if (leastSignificantBit == BigInt(1)) {
result = result * x;
result = result % n;
}