diff --git a/lib/bn.js b/lib/bn.js index 8532b5b..703e9ef 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -1056,6 +1056,7 @@ var bl9 = b9 & 0x1fff; var bh9 = b9 >>> 13; + out.negative = self.negative ^ num.negative; out.length = 19; /* k = 0 */ lo = Math.imul(al0, bl0); diff --git a/test/arithmetic-test.js b/test/arithmetic-test.js index c87fe31..e51a7f1 100644 --- a/test/arithmetic-test.js +++ b/test/arithmetic-test.js @@ -159,12 +159,21 @@ describe('BN.js/Arithmetic', function () { function testMethod (name, mul) { describe(name, function () { it('should multiply numbers of different signs', function () { - assert.equal(mul(new BN(0x1001), new BN(0x1234)).toString(16), - '1235234'); - assert.equal(mul(new BN(-0x1001), new BN(0x1234)).toString(16), - '-1235234'); - assert.equal(mul(new BN(-0x1001), new BN(-0x1234)).toString(16), - '1235234'); + var offsets = [ + 1, // smallMulTo + 250, // comb10MulTo + 1000, // bigMulTo + 15000 // jumboMulTo + ]; + + for (var i = 0; i < offsets.length; ++i) { + var x = new BN(1).ishln(offsets[i]); + + assert.equal(mul(x, x).isNeg(), false); + assert.equal(mul(x, x.neg()).isNeg(), true); + assert.equal(mul(x.neg(), x).isNeg(), true); + assert.equal(mul(x.neg(), x.neg()).isNeg(), false); + } }); it('should multiply with carry', function () { diff --git a/util/genCombMulTo.js b/util/genCombMulTo.js index 242354a..8b456c7 100644 --- a/util/genCombMulTo.js +++ b/util/genCombMulTo.js @@ -23,6 +23,7 @@ function genCombMulTo (alen, blen) { src.push('var bh' + i + ' = b' + i + ' >>> 13;'); } src.push(''); + src.push('out.negative = self.negative ^ num.negative;'); src.push('out.length = ' + len + ';'); for (var k = 0; k < len; k++) { diff --git a/util/genCombMulTo10.js b/util/genCombMulTo10.js index c87c19d..4214ffd 100644 --- a/util/genCombMulTo10.js +++ b/util/genCombMulTo10.js @@ -22,6 +22,7 @@ function genCombMulTo (alen, blen) { src.push('var bh' + i + ' = b' + i + ' >>> 13;'); } src.push(''); + src.push('out.negative = self.negative ^ num.negative;'); src.push('out.length = ' + len + ';'); for (var k = 0; k < len; k++) {