Skip to content

Commit 6277fd7

Browse files
backport(4.x): Fix imuln/muln with zero (backport of #313) (#314)
Co-authored-by: CanardMandarin <thibault.marboud@gmail.com>
1 parent ac0d4af commit 6277fd7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/bn.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@
19211921
this.words[i] = carry;
19221922
this.length++;
19231923
}
1924+
this.length = num === 0 ? 1 : this.length;
19241925

19251926
return this;
19261927
};

test/arithmetic-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ describe('BN.js/Arithmetic', function () {
290290
new BN(0).imuln(0x4000000);
291291
}, /^Error: Assertion failed$/);
292292
});
293+
294+
it('should strip length on zero multiplication', function () {
295+
var a = new BN(42);
296+
assert.equal(a.clone().imuln(0).isZero(), true);
297+
assert.equal(a.clone().muln(0).isZero(), true);
298+
299+
var b = new BN('1222222225255589');
300+
assert.equal(b.clone().imuln(0).isZero(), true);
301+
assert.equal(b.clone().muln(0).isZero(), true);
302+
});
293303
});
294304

295305
describe('.pow()', function () {

0 commit comments

Comments
 (0)