Comparing version 1.0.0 to 1.1.0
126
lib/bn.js
@@ -129,3 +129,26 @@ // Utils | ||
BN.prototype._parseHex = function parseHex(number, start) { | ||
function parseHex(str, start, end) { | ||
var r = 0; | ||
var len = Math.min(str.length, end); | ||
for (var i = start; i < len; i++) { | ||
var c = str.charCodeAt(i) - 48; | ||
r <<= 4; | ||
// 'a' - 'f' | ||
if (c >= 49 && c <= 54) | ||
r |= c - 49 + 0xa; | ||
// 'A' - 'F' | ||
else if (c >= 17 && c <= 22) | ||
r |= c - 17 + 0xa; | ||
// '0' - '9' | ||
else | ||
r |= c & 0xf; | ||
} | ||
return r; | ||
} | ||
BN.prototype._parseHex = function _parseHex(number, start) { | ||
// Create possibly bigger array to ensure that it fits the number | ||
@@ -140,3 +163,3 @@ this.length = Math.ceil((number.length - start) / 6); | ||
for (var i = number.length - 6, j = 0; i >= start; i -= 6) { | ||
var w = parseInt(number.slice(i, i + 6), 16); | ||
var w = parseHex(number, i, i + 6); | ||
this.words[j] |= (w << off) & 0x3ffffff; | ||
@@ -151,3 +174,3 @@ this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; | ||
if (i + 6 !== start) { | ||
var w = parseInt(number.slice(start, i + 6), 16); | ||
var w = parseHex(number, start, i + 6); | ||
this.words[j] |= (w << off) & 0x3ffffff; | ||
@@ -159,3 +182,26 @@ this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; | ||
BN.prototype._parseBase = function parseBase(number, base, start) { | ||
function parseBase(str, start, end, mul) { | ||
var r = 0; | ||
var len = Math.min(str.length, end); | ||
for (var i = start; i < len; i++) { | ||
var c = str.charCodeAt(i) - 48; | ||
r *= mul; | ||
// 'a' | ||
if (c >= 49) | ||
r += c - 49 + 0xa; | ||
// 'A' | ||
else if (c >= 17) | ||
r += c - 17 + 0xa; | ||
// '0' - '9' | ||
else | ||
r += c; | ||
} | ||
return r; | ||
} | ||
BN.prototype._parseBase = function _parseBase(number, base, start) { | ||
// Initialize as zero | ||
@@ -165,34 +211,28 @@ this.words = [ 0 ]; | ||
// Find length of limb in base | ||
for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) | ||
limbLen++; | ||
limbLen--; | ||
limbPow = (limbPow / base) | 0; | ||
var total = number.length - start; | ||
var mod = total % limbLen; | ||
var end = Math.min(total, total - mod) + start; | ||
var word = 0; | ||
var q = 1; | ||
var p = 0; | ||
var bigQ = null; | ||
for (var i = start; i < number.length; i++) { | ||
var digit; | ||
var ch = number[i]; | ||
if (base === 10 || ch <= '9') | ||
digit = ch | 0; | ||
else if (ch >= 'a') | ||
digit = ch.charCodeAt(0) - 97 + 10; | ||
else | ||
digit = ch.charCodeAt(0) - 65 + 10; | ||
word *= base; | ||
word += digit; | ||
q *= base; | ||
p++; | ||
for (var i = start; i < end; i += limbLen) { | ||
word = parseBase(number, i, i + limbLen, base); | ||
if (q > 0xfffff) { | ||
assert(q <= 0x3ffffff); | ||
if (!bigQ) | ||
bigQ = new BN(q); | ||
this.mul(bigQ).copy(this); | ||
this.iadd(new BN(word)); | ||
word = 0; | ||
q = 1; | ||
p = 0; | ||
} | ||
this.imuln(limbPow); | ||
this.words[0] += word; | ||
} | ||
if (p !== 0) { | ||
this.mul(new BN(q)).copy(this); | ||
this.iadd(new BN(word)); | ||
if (mod !== 0) { | ||
var pow = 1; | ||
var word = parseBase(number, i, number.length, base); | ||
for (var i = 0; i < mod; i++) | ||
pow *= base; | ||
this.imuln(pow); | ||
this.words[0] += word; | ||
} | ||
@@ -787,2 +827,22 @@ }; | ||
BN.prototype.imuln = function imuln(num) { | ||
assert(typeof num === 'number'); | ||
// Carry | ||
var carry = 0; | ||
for (var i = 0; i < this.length; i++) { | ||
var w = this.words[i] * num; | ||
var lo = (w & 0x3ffffff) + carry; | ||
carry = (w / 0x4000000) | 0; | ||
this.words[i] = lo; | ||
} | ||
if (carry !== 0) { | ||
this.words[i] = carry; | ||
this.length++; | ||
} | ||
return this; | ||
}; | ||
// `this` * `this` | ||
@@ -789,0 +849,0 @@ BN.prototype.sqr = function sqr() { |
{ | ||
"name": "bn.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
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
83187
2470