Skip to content

Commit

Permalink
fix iaddn sign issue (#216)
Browse files Browse the repository at this point in the history
When we add number to BN instance and this number equal to number in
BN instance with negative value we should receive zero with lack of sign.
  • Loading branch information
fanatid committed Jul 7, 2019
1 parent 8a8d9ba commit 44650ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@

// Possible sign change
if (this.negative !== 0) {
if (this.length === 1 && (this.words[0] | 0) < num) {
if (this.length === 1 && (this.words[0] | 0) <= num) {
this.words[0] = num - (this.words[0] | 0);
this.negative = 0;
return this;
Expand Down
5 changes: 5 additions & 0 deletions test/arithmetic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ describe('BN.js/Arithmetic', function () {
new BN(0).iaddn(0x4000000);
}, /^Error: Assertion failed$/);
});

it('should reset sign if value equal to value in instance', function () {
var a = new BN(-1);
assert.equal(a.addn(1).toString(), '0');
});
});

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

0 comments on commit 44650ad

Please sign in to comment.