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

backport(4.x): Fix serious issue in .toString(16) (#295) #309

Merged
merged 1 commit into from
Nov 11, 2024
Merged
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
10 changes: 5 additions & 5 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,16 @@
var w = this.words[i];
var word = (((w << off) | carry) & 0xffffff).toString(16);
carry = (w >>> (24 - off)) & 0xffffff;
if (carry !== 0 || i !== this.length - 1) {
out = zeros[6 - word.length] + word + out;
} else {
out = word + out;
}
off += 2;
if (off >= 26) {
off -= 26;
i--;
}
if (carry !== 0 || i !== this.length - 1) {
out = zeros[6 - word.length] + word + out;
} else {
out = word + out;
}
}
if (carry !== 0) {
out = carry.toString(16) + out;
Expand Down
10 changes: 10 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ var BN = require('../').BN;

describe('BN.js/Utils', function () {
describe('.toString()', function () {
describe('hex no padding', function () {
it('should have same length as input', function () {
var hex = '1';
for (var i = 1; i <= 128; i++) {
var n = new BN(hex, 16);
assert.equal(n.toString(16).length, i);
hex = hex + '0';
}
});
});
describe('binary padding', function () {
it('should have a length of 256', function () {
var a = new BN(0);
Expand Down