Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bn: Fix sign in comb10MulTo #99

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 15 additions & 6 deletions test/arithmetic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions util/genCombMulTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
1 change: 1 addition & 0 deletions util/genCombMulTo10.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down