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", "error",
"always" "always"
] ]
},
"globals": {
"BigInt": "readonly"
} }
} }

View File

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