Skip to content

Commit

Permalink
lib: fix BN#imuln for negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed May 28, 2018
1 parent 98ac84e commit 6a90bcb
Show file tree
Hide file tree
Showing 2 changed files with 10 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 @@ -1908,6 +1908,9 @@
};

BN.prototype.imuln = function imuln (num) {
var isNegNum = num < 0;
if (isNegNum) num = -num;

assert(typeof num === 'number');
assert(num < 0x4000000);

Expand All @@ -1928,7 +1931,7 @@
this.length++;
}

return this;
return isNegNum ? this.ineg() : this;
};

BN.prototype.muln = function muln (num) {
Expand Down
6 changes: 6 additions & 0 deletions test/arithmetic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ describe('BN.js/Arithmetic', function () {
new BN(0).imuln(0x4000000);
}, /^Error: Assertion failed$/);
});

it('should negate number if number is negative', function () {
var a = new BN('dead', 16);

assert.equal(a.muln(-1).toString(16), a.neg().toString(16));
});
});

describe('.pow()', function () {
Expand Down

0 comments on commit 6a90bcb

Please sign in to comment.