Comparing version 0.13.3 to 0.14.0
128
lib/bn.js
@@ -91,3 +91,3 @@ // Utils | ||
base = 16; | ||
assert(base <= 16); | ||
assert(base === (base | 0) && base >= 2 && base <= 36); | ||
@@ -211,50 +211,78 @@ number = number.toString().replace(/\s+/g, ''); | ||
/* | ||
function _zero(n) { | ||
var code = ''; | ||
for (var i = n - 1; i > 0; i--) { | ||
var pad = ''; | ||
for (var j = 0; j < n - i; j++) | ||
pad += '0'; | ||
code += 'if (w.length === ' + i + ') return \'' + pad + '\' + w;\n'; | ||
} | ||
code += 'return w'; | ||
return new Function('w', code); | ||
var zeros = []; | ||
var groupSizes = []; | ||
var groupBases = []; | ||
var s = ''; | ||
var i = -1; | ||
while (++i < BN.wordSize) { | ||
zeros[i] = s; | ||
s += '0'; | ||
} | ||
groupSizes[0] = 0; | ||
groupSizes[1] = 0; | ||
groupBases[0] = 0; | ||
groupBases[1] = 0; | ||
var base = 2 - 1; | ||
while (++base < 36 + 1) { | ||
var groupSize = 0; | ||
var groupBase = 1; | ||
// TODO: <= | ||
while (groupBase < (1 << BN.wordSize) / base) { | ||
groupBase *= base; | ||
groupSize += 1; | ||
} | ||
groupSizes[base] = groupSize; | ||
groupBases[base] = groupBase; | ||
} | ||
var zero6 = _zero(6); | ||
var zero14 = _zero(14); | ||
*/ | ||
// Sadly chrome apps could not contain `new Function()` calls | ||
function zero6(w) { | ||
if (w.length === 5) return '0' + w; | ||
if (w.length === 4) return '00' + w; | ||
if (w.length === 3) return '000' + w; | ||
if (w.length === 2) return '0000' + w; | ||
if (w.length === 1) return '00000' + w; | ||
return w; | ||
} | ||
var zeros = [ | ||
'', | ||
'0', | ||
'00', | ||
'000', | ||
'0000', | ||
'00000', | ||
'000000', | ||
'0000000', | ||
'00000000', | ||
'000000000', | ||
'0000000000', | ||
'00000000000', | ||
'000000000000', | ||
'0000000000000', | ||
'00000000000000', | ||
'000000000000000', | ||
'0000000000000000', | ||
'00000000000000000', | ||
'000000000000000000', | ||
'0000000000000000000', | ||
'00000000000000000000', | ||
'000000000000000000000', | ||
'0000000000000000000000', | ||
'00000000000000000000000', | ||
'000000000000000000000000', | ||
'0000000000000000000000000' | ||
]; | ||
function zero14(w) { | ||
if (w.length === 13) return '0' + w; | ||
if (w.length === 12) return '00' + w; | ||
if (w.length === 11) return '000' + w; | ||
if (w.length === 10) return '0000' + w; | ||
if (w.length === 9) return '00000' + w; | ||
if (w.length === 8) return '000000' + w; | ||
if (w.length === 7) return '0000000' + w; | ||
if (w.length === 6) return '00000000' + w; | ||
if (w.length === 5) return '000000000' + w; | ||
if (w.length === 4) return '0000000000' + w; | ||
if (w.length === 3) return '00000000000' + w; | ||
if (w.length === 2) return '000000000000' + w; | ||
if (w.length === 1) return '0000000000000' + w; | ||
return w; | ||
} | ||
var groupSizes = [ | ||
0, 0, | ||
25, 16, 12, 11, 10, 9, 8, | ||
8, 7, 7, 7, 7, 6, 6, | ||
6, 6, 6, 6, 6, 5, 5, | ||
5, 5, 5, 5, 5, 5, 5, | ||
5, 5, 5, 5, 5, 5, 5 | ||
]; | ||
// Precomputed divisor for `.toString(10)` = 10 ^ 14 | ||
var div10 = new BN(null); | ||
div10.words = [ 0x7a4000, 0x16bcc4 ]; | ||
div10.length = 2; | ||
var groupBases = [ | ||
0, 0, | ||
33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, | ||
43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, | ||
16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, | ||
6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, | ||
24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 | ||
]; | ||
@@ -272,3 +300,3 @@ BN.prototype.toString = function toString(base) { | ||
if (carry !== 0 || i !== this.length - 1) | ||
out = zero6(word) + out; | ||
out = zeros[6 - word.length] + word + out; | ||
else | ||
@@ -287,3 +315,7 @@ out = word + out; | ||
return out; | ||
} else if (base === 10) { | ||
} else if (base === (base | 0) && base >= 2 && base <= 36) { | ||
// var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); | ||
var groupSize = groupSizes[base]; | ||
// var groupBase = Math.pow(base, groupSize); | ||
var groupBase = groupBases[base]; | ||
var out = ''; | ||
@@ -293,7 +325,7 @@ var c = this.clone(); | ||
while (c.cmpn(0) !== 0) { | ||
var r = c.modn(1000000); | ||
c = c.idivn(1000000); | ||
var r = c.modn(groupBase).toString(base); | ||
c = c.idivn(groupBase); | ||
if (c.cmpn(0) !== 0) | ||
out = zero6(r + '') + out; | ||
out = zeros[groupSize - r.length] + r + out; | ||
else | ||
@@ -308,3 +340,3 @@ out = r + out; | ||
} else { | ||
assert(false, 'Only 16 and 10 base are supported'); | ||
assert(false, 'Base should be between 2 and 36'); | ||
} | ||
@@ -311,0 +343,0 @@ }; |
{ | ||
"name": "bn.js", | ||
"version": "0.13.3", | ||
"version": "0.14.0", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
@@ -28,2 +28,6 @@ var assert = require('assert'); | ||
'10000000000000000'); | ||
var base2 = '11111111111111111111111111111111111111111111111111111'; | ||
assert.equal(new BN(base2, 2).toString(2), base2); | ||
var base36 = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'; | ||
assert.equal(new BN(base36, 36).toString(36), base36); | ||
}); | ||
@@ -30,0 +34,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
61499
7
2001