diff --git a/src/biginteger/bn.interface.js b/src/biginteger/bn.interface.js index 2185d0b6..84226097 100644 --- a/src/biginteger/bn.interface.js +++ b/src/biginteger/bn.interface.js @@ -28,7 +28,7 @@ export default class BigInteger { * BigInteger increment in place */ iinc() { - this.value.iadd(one.value); + this.value.iadd(new BN(1)); return this; } @@ -44,7 +44,7 @@ export default class BigInteger { * BigInteger decrement in place */ idec() { - this.value.isub(one.value); + this.value.isub(new BN(1)); return this; } @@ -151,8 +151,13 @@ export default class BigInteger { * Note: this and and n must be relatively prime * @param {BigInteger} n modulo * @return {BigInteger} x such that this*x = 1 mod n + * @throws {Error} if the inverse does not exist */ modInv(n) { + // invm returns a wrong result if the inverse does not exist + if (!this.gcd(n).isOne()) { + throw new Error('Inverse does not exist'); + } return new BigInteger(this.value.invm(n.value)); } @@ -251,7 +256,7 @@ export default class BigInteger { } isOne() { - return this.equal(one); + return this.value.eq(new BN(1)); } isNegative() { @@ -320,5 +325,3 @@ export default class BigInteger { return this.value.toArrayLike(Uint8Array, endian, length); } } - -const one = new BigInteger(1); diff --git a/src/biginteger/native.interface.js b/src/biginteger/native.interface.js index bafc05dd..20397b13 100644 --- a/src/biginteger/native.interface.js +++ b/src/biginteger/native.interface.js @@ -175,9 +175,14 @@ export default class BigInteger { * Note: this and and n must be relatively prime * @param {BigInteger} n modulo * @return {BigInteger} x such that this*x = 1 mod n + * @throws {Error} if the inverse does not exist */ modInv(n) { - return this._egcd(n).x.add(n).mod(n); + const { gcd, x } = this._egcd(n); + if (!gcd.isOne()) { + throw new Error('Inverse does not exist'); + } + return x.add(n).mod(n); } /** diff --git a/test/general/biginteger.js b/test/general/biginteger.js index b317c27d..b4fd1f90 100644 --- a/test/general/biginteger.js +++ b/test/general/biginteger.js @@ -149,6 +149,7 @@ module.exports = () => describe('BigInteger interface', function() { const n = new BigInteger(moduloBN.toString()); const expected = baseBN.invm(moduloBN); expect(a.modInv(n).toString()).to.equal(expected.toString()); + expect(() => a.mul(n).modInv(n)).to.throw('Inverse does not exist'); }); it('getBit is correct', async function() {