bignumber.js icon indicating copy to clipboard operation
bignumber.js copied to clipboard

modInverse needed

Open mccoysc opened this issue 7 years ago • 1 comments

as the title

mccoysc avatar Feb 05 '19 11:02 mccoysc

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;
}

Light-Wizzard avatar Apr 08 '19 23:04 Light-Wizzard