Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Rational.rationalize = function(n, d)
{
if (d.isNegative())
{
n = n.negate();
d = d.negate();
}
var s = BigInteger.gcd(n, d);
if (s.isUnit())
return new Rational(n, d);
else if (s.equals(d))
{
var nn = n.divide(s);
if (nn.isSmall)
return nn.valueOf();
return nn;
}
else
return new Rational(n.divide(s), d.divide(s));
}
export function gcd(x: BigInteger, y: BigInteger): BigInteger {
return bigInteger.gcd(x, y)
}
function generateRSAKeys() {
var primeP = util.getRandomBigIntPrime(minPrimeNumber, maxPrimeNumber);
var primeQ = util.getRandomBigIntPrime(minPrimeNumber, maxPrimeNumber);
var N = primeP.times(primeQ);
var F = primeQ.prev().times(primeP.prev());
var E = bigInt(3674911);
do {
E = util.getRandomBigIntPrime(2, F.prev());
} while(bigInt.gcd(E, F).notEquals(1));
var D = E.modInv(F);
var key = {};
key.publicKey = { N, E };
key.privateKey = { N, E, D, P: primeP, Q: primeQ, F };
return key;
}
static generate(keysize) {
const e = bigInt(65537);
let p;
let q;
let totient;
do {
p = this.randomPrime(keysize / 2);
q = this.randomPrime(keysize / 2);
totient = bigInt.lcm(
p.prev(),
q.prev()
);
} while (bigInt.gcd(e, totient).notEquals(1) || p.minus(q).abs().shiftRight(keysize / 2 - 100).isZero());
return {
e,
n: p.multiply(q),
d: e.modInv(totient),
};
}