Comparing version 0.11.2 to 0.11.3
5
1.js
@@ -12,6 +12,7 @@ var bn = require('./'); | ||
console.time('montSqr'); | ||
for (var i = 0; i < 400000; i++) { | ||
t += am.redSqr().length; | ||
am.words = new Array(100); | ||
for (var i = 0; i < 4000000; i++) { | ||
t += a.mulTo(a, am).length; | ||
} | ||
console.timeEnd('montSqr'); | ||
console.log(t); |
110
lib/bn.js
@@ -540,11 +540,12 @@ // Utils | ||
// NOTE: This could be potentionally used to generate loop-less multiplications | ||
function _genCombMulTo(len) { | ||
function _genCombMulTo(alen, blen) { | ||
var len = alen + blen - 1; | ||
var src = [ | ||
'var a = this.words, b = num.words, o = out.words, c = 0, w, ' + | ||
'mask = 0x3ffffff, shift = 0x4000000;', | ||
'out.length = ' + (len * 2 - 1) + ';' | ||
'out.length = ' + len + ';' | ||
]; | ||
for (var k = 0; k < len * 2 - 1; k++) { | ||
var minJ = Math.max(0, k - len + 1); | ||
var maxJ = Math.min(k, len - 1); | ||
for (var k = 0; k < len; k++) { | ||
var minJ = Math.max(0, k - alen + 1); | ||
var maxJ = Math.min(k, blen - 1); | ||
@@ -727,36 +728,32 @@ for (var j = minJ; j <= maxJ; j++) { | ||
if (s !== 0) { | ||
hint -= s; | ||
hint = Math.max(0, hint); | ||
hint -= s; | ||
hint = Math.max(0, hint); | ||
// Extended mode, copy masked part | ||
if (maskedWords) { | ||
for (var i = 0; i < s; i++) | ||
maskedWords.words[i] = this.words[i]; | ||
maskedWords.length = s; | ||
} | ||
// Extended mode, copy masked part | ||
if (maskedWords) { | ||
for (var i = 0; i < s; i++) | ||
maskedWords.words[i] = this.words[i]; | ||
maskedWords.length = s; | ||
} | ||
for (var i = s; i <= this.length; i++) | ||
this.words[i - s] = this.words[i]; | ||
if (i - s - 1 > 0) { | ||
this.length = i - s - 1; | ||
} else { | ||
this.words[0] = 0; | ||
this.length = 1; | ||
} | ||
for (var i = s; i <= this.length; i++) | ||
this.words[i - s] = this.words[i]; | ||
if (i - s - 1 > 0) { | ||
this.length = i - s - 1; | ||
} else { | ||
this.words[0] = 0; | ||
this.length = 1; | ||
} | ||
if (r !== 0) { | ||
var carry = 0; | ||
for (var i = this.length - 1; i >= 0 && (carry !== 0 || i >= hint); i--) { | ||
var word = this.words[i]; | ||
this.words[i] = (carry << (26 - r)) | (this.words[i] >> r); | ||
carry = word & mask; | ||
} | ||
// Push carried bits as a mask | ||
if (maskedWords && carry !== 0) | ||
maskedWords.words[maskedWords.length++] = carry; | ||
var carry = 0; | ||
for (var i = this.length - 1; i >= 0 && (carry !== 0 || i >= hint); i--) { | ||
var word = this.words[i]; | ||
this.words[i] = (carry << (26 - r)) | (this.words[i] >> r); | ||
carry = word & mask; | ||
} | ||
// Push carried bits as a mask | ||
if (maskedWords && carry !== 0) | ||
maskedWords.words[maskedWords.length++] = carry; | ||
if (this.length === 0) { | ||
@@ -767,5 +764,4 @@ this.words[0] = 0; | ||
if (extended) { | ||
if (extended) | ||
return { hi: this.strip(), lo: maskedWords }; | ||
} | ||
@@ -1256,4 +1252,5 @@ return this.strip(); | ||
// Pseudo-Mersenne prime | ||
function MPrime(p) { | ||
function MPrime(name, p) { | ||
// P = 2 ^ N - K | ||
this.name = name; | ||
this.p = new BN(p, 16); | ||
@@ -1280,3 +1277,3 @@ this.n = this.p.bitLength(); | ||
var pair = r.ishrn(this.n, 0, this.tmp); | ||
r = pair.hi.imul(this.k); | ||
r = this.imulK(pair.hi); | ||
r = r.iadd(pair.lo); | ||
@@ -1297,5 +1294,10 @@ rlen = r.bitLength(); | ||
MPrime.prototype.imulK = function imulK(num) { | ||
return num.imul(this.k); | ||
}; | ||
function K256() { | ||
MPrime.call( | ||
this, | ||
'k256', | ||
'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); | ||
@@ -1305,5 +1307,37 @@ } | ||
K256.prototype.imulK = function imulK(num) { | ||
// K = 0x1000003d1 = [ 0x40, 0x3d1 ] | ||
num.words[num.length] = 0; | ||
num.words[num.length + 1] = 0; | ||
num.length += 2; | ||
for (var i = num.length - 3; i >= 0; i--) { | ||
var w = num.words[i]; | ||
var hi = w * 0x40; | ||
var lo = w * 0x3d1; | ||
hi += (lo / 0x4000000) | 0; | ||
var uhi = (hi / 0x4000000) | 0; | ||
hi &= 0x3ffffff; | ||
lo &= 0x3ffffff; | ||
num.words[i + 2] += uhi; | ||
num.words[i + 1] += hi; | ||
num.words[i] = lo; | ||
} | ||
for (var i = 0; i < num.length; i++) { | ||
if (num.words[i] >= 0x4000000) { | ||
num.words[i + 1] += num.words[i] >>> 26; | ||
num.words[i] &= 0x3ffffff; | ||
} | ||
} | ||
if (num.words[num.length - 1] === 0) | ||
num.length--; | ||
if (num.words[num.length - 1] === 0) | ||
num.length--; | ||
return num; | ||
}; | ||
function P224() { | ||
MPrime.call( | ||
this, | ||
'p224', | ||
'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); | ||
@@ -1316,2 +1350,3 @@ } | ||
this, | ||
'p192', | ||
'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); | ||
@@ -1325,2 +1360,3 @@ } | ||
this, | ||
'25519', | ||
'7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); | ||
@@ -1327,0 +1363,0 @@ } |
{ | ||
"name": "bn.js", | ||
"version": "0.11.2", | ||
"version": "0.11.3", | ||
"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
55078
1774