Comparing version 2.0.5 to 2.1.0
@@ -57,25 +57,3 @@ (function (module, exports) { | ||
if (typeof number === 'number') { | ||
if (number < 0) { | ||
this.sign = true; | ||
number = -number; | ||
} | ||
if (number < 0x4000000) { | ||
this.words = [ number & 0x3ffffff ]; | ||
this.length = 1; | ||
} else if (number < 0x10000000000000) { | ||
this.words = [ | ||
number & 0x3ffffff, | ||
(number / 0x4000000) & 0x3ffffff | ||
]; | ||
this.length = 2; | ||
} else { | ||
assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) | ||
this.words = [ | ||
number & 0x3ffffff, | ||
(number / 0x4000000) & 0x3ffffff, | ||
1 | ||
]; | ||
this.length = 3; | ||
} | ||
return; | ||
return this._initNumber(number, base, endian); | ||
} else if (typeof number === 'object') { | ||
@@ -102,4 +80,40 @@ return this._initArray(number, base, endian); | ||
this.strip(); | ||
if (endian !== 'le') | ||
return; | ||
this._initArray(this.toArray(), base, endian); | ||
}; | ||
BN.prototype._initNumber = function _initNumber(number, base, endian) { | ||
if (number < 0) { | ||
this.sign = true; | ||
number = -number; | ||
} | ||
if (number < 0x4000000) { | ||
this.words = [ number & 0x3ffffff ]; | ||
this.length = 1; | ||
} else if (number < 0x10000000000000) { | ||
this.words = [ | ||
number & 0x3ffffff, | ||
(number / 0x4000000) & 0x3ffffff | ||
]; | ||
this.length = 2; | ||
} else { | ||
assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) | ||
this.words = [ | ||
number & 0x3ffffff, | ||
(number / 0x4000000) & 0x3ffffff, | ||
1 | ||
]; | ||
this.length = 3; | ||
} | ||
if (endian !== 'le') | ||
return; | ||
// Reverse the bytes | ||
this._initArray(this.toArray(), base, endian); | ||
}; | ||
BN.prototype._initArray = function _initArray(number, base, endian) { | ||
@@ -428,3 +442,3 @@ // Perhaps a Uint8Array | ||
BN.prototype.toArray = function toArray() { | ||
BN.prototype.toArray = function toArray(endian) { | ||
this.strip(); | ||
@@ -435,8 +449,18 @@ var res = new Array(this.byteLength()); | ||
var q = this.clone(); | ||
for (var i = 0; q.cmpn(0) !== 0; i++) { | ||
var b = q.andln(0xff); | ||
q.ishrn(8); | ||
if (endian !== 'le') { | ||
// Assume big-endian | ||
for (var i = 0; q.cmpn(0) !== 0; i++) { | ||
var b = q.andln(0xff); | ||
q.ishrn(8); | ||
// Assume big-endian | ||
res[res.length - i - 1] = b; | ||
res[res.length - i - 1] = b; | ||
} | ||
} else { | ||
// Assume little-endian | ||
for (var i = 0; q.cmpn(0) !== 0; i++) { | ||
var b = q.andln(0xff); | ||
q.ishrn(8); | ||
res[i] = b; | ||
} | ||
} | ||
@@ -443,0 +467,0 @@ |
{ | ||
"name": "bn.js", | ||
"version": "2.0.5", | ||
"version": "2.1.0", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
@@ -30,2 +30,5 @@ var assert = require('assert'); | ||
it('should accept two-limb LE number', function() { | ||
assert.equal(new BN(0x4123456, null, 'le').toString(16), '56341204'); | ||
}); | ||
}); | ||
@@ -82,2 +85,7 @@ | ||
}); | ||
it('should accept base-16 LE integer', function() { | ||
assert.equal(new BN('1A6B765D8CDF', 16, 'le').toString(16), | ||
'df8c5d766b1a'); | ||
}); | ||
}); | ||
@@ -108,2 +116,5 @@ | ||
'807060504030201'); | ||
assert.equal(new BN([ 1, 2, 3, 4 ]).toArray('le').join(','), '4,3,2,1'); | ||
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ]).toArray('le').join(','), | ||
'8,7,6,5,4,3,2,1'); | ||
}); | ||
@@ -110,0 +121,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3038
110869
14