Comparing version 0.10.0 to 0.10.1
4
1.js
@@ -12,6 +12,6 @@ var bn = require('./'); | ||
console.time('montSqr'); | ||
for (var i = 0; i < 10000000; i++) { | ||
t += a.muln(13589).length; | ||
for (var i = 0; i < 400000; i++) { | ||
t += am.redSqr().length; | ||
} | ||
console.timeEnd('montSqr'); | ||
console.log(t); |
@@ -363,3 +363,2 @@ // Utils | ||
BN.prototype.bitLength = function bitLength() { | ||
this.strip(); | ||
var hi = 0; | ||
@@ -372,3 +371,2 @@ var w = this.words[this.length - 1]; | ||
BN.prototype.byteLength = function byteLength() { | ||
this.strip(); | ||
var hi = 0; | ||
@@ -444,6 +442,13 @@ var w = this.words[this.length - 1]; | ||
BN.prototype.add = function add(num) { | ||
if (num.sign && !this.sign) | ||
return this.sub(num.neg()); | ||
else if (!num.sign && this.sign) | ||
return num.sub(this.neg()); | ||
if (num.sign && !this.sign) { | ||
num.sign = false; | ||
var res = this.sub(num); | ||
num.sign = true; | ||
return res; | ||
} else if (!num.sign && this.sign) { | ||
this.sign = false; | ||
var res = num.sub(this); | ||
this.sign = true; | ||
return res; | ||
} | ||
@@ -578,4 +583,4 @@ if (this.length > num.length) | ||
// note that ncarry could be >= 0x3ffffff | ||
var ncarry = 0; | ||
var rword = 0; | ||
var ncarry = carry >>> 26; | ||
var rword = carry & 0x3ffffff; | ||
var maxJ = Math.min(k, num.length - 1); | ||
@@ -586,4 +591,3 @@ for (var j = Math.max(0, k - this.length + 1); j <= maxJ; j++) { | ||
var b = num.words[j]; | ||
var r = a * b + carry; | ||
carry = 0; | ||
var r = a * b; | ||
@@ -721,3 +725,3 @@ var lo = r & 0x3ffffff; | ||
var mask = 0x3ffffff ^ ((0x3ffffff >> r) << r); | ||
var maskedWords = extended && new Array(s); | ||
var maskedWords = extended; | ||
@@ -729,5 +733,7 @@ if (s !== 0) { | ||
// Extended mode, copy masked part | ||
if (extended) | ||
if (maskedWords) { | ||
for (var i = 0; i < s; i++) | ||
maskedWords[i] = this.words[i]; | ||
maskedWords.words[i] = this.words[i]; | ||
maskedWords.length = s; | ||
} | ||
@@ -753,4 +759,4 @@ for (var i = s; i <= this.length; i++) | ||
// Push carried bits as a mask | ||
if (extended && carry !== 0) | ||
maskedWords.push(carry); | ||
if (maskedWords && carry !== 0) | ||
maskedWords.words[maskedWords.length++] = carry; | ||
} | ||
@@ -764,8 +770,3 @@ | ||
if (extended) { | ||
var lo = new BN(null); | ||
if (maskedWords.length === 0) | ||
maskedWord.push(0); | ||
lo.words = maskedWords; | ||
lo.length = maskedWords.length; | ||
return { hi: this.strip(), lo: lo.strip() }; | ||
return { hi: this.strip(), lo: maskedWords }; | ||
} | ||
@@ -1002,3 +1003,2 @@ | ||
x1 = x1.clone(); | ||
var x2 = new BN(0); | ||
@@ -1011,3 +1011,3 @@ while (a.cmpn(1) !== 0 && b.cmpn(1) !== 0) { | ||
else | ||
x1 = x1.add(p).ishrn(1); | ||
x1.iadd(p).ishrn(1); | ||
} | ||
@@ -1019,3 +1019,3 @@ while (b.isEven()) { | ||
else | ||
x2 = x2.add(p).ishrn(1); | ||
x2.iadd(p).ishrn(1); | ||
} | ||
@@ -1031,5 +1031,5 @@ if (a.cmp(b) >= 0) { | ||
if (a.cmpn(1) === 0) | ||
return x1.mod(p); | ||
return x1; | ||
else | ||
return x2.mod(p); | ||
return x2; | ||
}; | ||
@@ -1039,3 +1039,3 @@ | ||
BN.prototype.invm = function invm(num) { | ||
return this._egcd(new BN(1), num); | ||
return this._egcd(new BN(1), num).mod(num); | ||
}; | ||
@@ -1281,9 +1281,9 @@ | ||
// num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) | ||
var r = num.clone(); | ||
var r = num; | ||
var rlen; | ||
do { | ||
var pair = r.ishrn(this.n, 0, true); | ||
r = this.k.mulTo(pair.hi, this.tmp); | ||
r = pair.lo.iadd(r); | ||
var pair = r.ishrn(this.n, 0, this.tmp); | ||
r = pair.hi.imul(this.k); | ||
r = r.iadd(pair.lo); | ||
rlen = r.bitLength(); | ||
@@ -1293,6 +1293,8 @@ } while (rlen > this.n); | ||
var cmp = rlen < this.n ? -1 : r.cmp(this.p); | ||
if (cmp === 0) | ||
r = new BN(0); | ||
else if (cmp > 0) | ||
if (cmp === 0) { | ||
r.words[0] = 0; | ||
r.length = 1; | ||
} else if (cmp > 0) { | ||
r.isub(this.p); | ||
} | ||
@@ -1495,3 +1497,7 @@ return r; | ||
Red.prototype.invm = function invm(a) { | ||
return a.invm(this.m)._forceRed(this); | ||
var inv = a._egcd(new BN(1), this.m); | ||
if (inv.sign) | ||
return this.imod(inv).redNeg(); | ||
else | ||
return this.imod(inv); | ||
}; | ||
@@ -1498,0 +1504,0 @@ |
{ | ||
"name": "bn.js", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
@@ -107,3 +107,4 @@ var assert = require('assert'); | ||
16); | ||
assert.equal(p.ireduce(num).toString(16), num.mod(p.p).toString(16)); | ||
var exp = num.mod(p.p).toString(16); | ||
assert.equal(p.ireduce(num).toString(16), exp); | ||
@@ -115,5 +116,6 @@ var regr = new BN('f7e46df64c1815962bf7bc9c56128798' + | ||
16); | ||
assert.equal(p.ireduce(regr).toString(16), regr.mod(p.p).toString(16)); | ||
var exp = regr.mod(p.p).toString(16); | ||
assert.equal(p.ireduce(regr).toString(16), exp); | ||
}); | ||
}); | ||
}); |
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
53514
1719