Comparing version 0.15.2 to 0.16.0
@@ -25,3 +25,3 @@ // Utils | ||
function BN(number, base) { | ||
function BN(number, base, endian) { | ||
// May be `new BN(bn)` ? | ||
@@ -41,4 +41,9 @@ if (number !== null && | ||
if (base === 'le' || base === 'be') { | ||
endian = base; | ||
base = 10; | ||
} | ||
if (number !== null) | ||
this._init(number || 0, base || 10); | ||
this._init(number || 0, base || 10, endian || 'be'); | ||
} | ||
@@ -51,3 +56,3 @@ if (typeof module === 'object') | ||
BN.prototype._init = function init(number, base) { | ||
BN.prototype._init = function init(number, base, endian) { | ||
if (typeof number === 'number') { | ||
@@ -70,23 +75,3 @@ if (number < 0) { | ||
} else if (typeof number === 'object') { | ||
// Perhaps a Uint8Array | ||
assert(typeof number.length === 'number'); | ||
this.length = Math.ceil(number.length / 3); | ||
this.words = new Array(this.length); | ||
for (var i = 0; i < this.length; i++) | ||
this.words[i] = 0; | ||
// Assume big-endian | ||
var off = 0; | ||
for (var i = number.length - 1, j = 0; i >= 0; i -= 3) { | ||
var w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); | ||
this.words[j] |= (w << off) & 0x3ffffff; | ||
this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | ||
off += 24; | ||
if (off >= 26) { | ||
off -= 26; | ||
j++; | ||
} | ||
} | ||
return this.strip(); | ||
return this._initArray(number, base, endian); | ||
} | ||
@@ -113,2 +98,37 @@ if (base === 'hex') | ||
BN.prototype._initArray = function _initArray(number, base, endian) { | ||
// Perhaps a Uint8Array | ||
assert(typeof number.length === 'number'); | ||
this.length = Math.ceil(number.length / 3); | ||
this.words = new Array(this.length); | ||
for (var i = 0; i < this.length; i++) | ||
this.words[i] = 0; | ||
var off = 0; | ||
if (endian === 'be') { | ||
for (var i = number.length - 1, j = 0; i >= 0; i -= 3) { | ||
var w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); | ||
this.words[j] |= (w << off) & 0x3ffffff; | ||
this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | ||
off += 24; | ||
if (off >= 26) { | ||
off -= 26; | ||
j++; | ||
} | ||
} | ||
} else if (endian === 'le') { | ||
for (var i = 0, j = 0; i < number.length; i += 3) { | ||
var w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); | ||
this.words[j] |= (w << off) & 0x3ffffff; | ||
this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | ||
off += 24; | ||
if (off >= 26) { | ||
off -= 26; | ||
j++; | ||
} | ||
} | ||
} | ||
return this.strip(); | ||
}; | ||
BN.prototype._parseHex = function parseHex(number, start) { | ||
@@ -115,0 +135,0 @@ // Create possibly bigger array to ensure that it fits the number |
{ | ||
"name": "bn.js", | ||
"version": "0.15.2", | ||
"version": "0.16.0", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
@@ -45,2 +45,9 @@ var assert = require('assert'); | ||
it('should import little endian', function() { | ||
assert.equal(new BN([1,2,3], 10, 'le').toString(16), '30201'); | ||
assert.equal(new BN([1,2,3,4], 10, 'le').toString(16), '4030201'); | ||
assert.equal(new BN([1,2,3,4,5], 10, 'le').toString(16), '504030201'); | ||
assert.equal(new BN([1,2,3,4,5,6,7,8], 10, 'le').toString(16), '807060504030201'); | ||
}); | ||
it('should return proper bitLength', function() { | ||
@@ -47,0 +54,0 @@ assert.equal(new BN(0).bitLength(), 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
81784
2418