Skip to content

Commit

Permalink
bn: fix Red#imod
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid authored and dcousens committed Jun 6, 2018
1 parent 98ac84e commit d87ed90
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/red-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit d87ed90

Please sign in to comment.