diff --git a/lib/bn.js b/lib/bn.js index 944259b..fe5c726 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -3169,7 +3169,10 @@ Red.prototype.imod = function imod (a) { if (this.prime) return this.prime.ireduce(a)._forceRed(this); - return a.umod(this.m)._forceRed(this); + + var mod = a.umod(this.m)._forceRed(this); + if (mod !== a) mod.copy(a); + return a; }; Red.prototype.neg = function neg (a) { diff --git a/test/red-test.js b/test/red-test.js index a50b2ec..f89f16a 100644 --- a/test/red-test.js +++ b/test/red-test.js @@ -262,4 +262,13 @@ describe('BN.js/Reduction context', function () { red.prime.split(input, output); assert.equal(input.cmp(output), 0); }); + + it('imod should change host object', function () { + var red = BN.red(new BN(13)); + var a = new BN(2).toRed(red); + var b = new BN(7).toRed(red); + var c = a.redIMul(b); + assert.equal(a.toNumber(), 1); + assert.equal(c.toNumber(), 1); + }); });