bignumber.js
bignumber.js copied to clipboard
modInverse needed
as the title
Take a look at this: Without BigNumber:
function modInverse(thisNumber, thisMod) {
let thatNumber = thisNumber % thisMod;
let x = 0;
for (x = 1; x < thisMod; x++) {
if ((thatNumber * x) % thisMod === 1) {
return x;
}
}
// FIXME: should never get here, in this case I do not know what to return
return 0;
}
I tested the function, it works, it is just a matter of adding BigNumber into the function which I did below and also tested it.
function modInverse(thisNumber, thisMod) {
let thatNumber = new BigNumberJs.BigNumber(thisNumber);
thatNumber = thatNumber.modulo(thisMod);
let x = 0;
for (x = 1; x < thisMod; x++) {
if (thatNumber.times(x).modulo(thisMod).toString() === "1") {
return x;
}
}
// FIXME: should never get here, in this case I do not know what to return
return 0;
}