Comparing version 0.1.7 to 0.2.0
@@ -894,2 +894,28 @@ var assert = require('assert'); | ||
BN.prototype._fastSqrtm = function _fastSqrtm(odd, num) { | ||
assert(this.mont); | ||
return r; | ||
}; | ||
// Square root over p | ||
BN.prototype.montSqrt = function montSqrt() { | ||
assert(!this.sign, 'mont works only with positives'); | ||
assert(this.mont, 'montInvm works only with mont numbers'); | ||
assert(this.cmp(0) !== 0); | ||
var mont = this.mont; | ||
// Fast case | ||
if (mont.m.andl(3) === 3) { | ||
var pow = mont.m.add(1).ishr(2); | ||
var r = this.montPow(pow); | ||
return r; | ||
} | ||
// TODO(indutny): Tonelli-Shanks algorithm | ||
throw new Error('Not implemented yet, for p % 4 !== 3'); | ||
}; | ||
BN.prototype.montInvm = function montInvm() { | ||
@@ -915,2 +941,32 @@ assert(!this.sign, 'mont works only with positives'); | ||
BN.prototype.montPow = function montPow(num, base) { | ||
if (!(num instanceof BN)) | ||
num = new BN(num, base); | ||
assert(this.mont && !num.mont, 'montPow(montNum, normalNum)'); | ||
var w = []; | ||
var q = num.clone(); | ||
while (q.cmp(0) !== 0) { | ||
w.push(q.andl(1)); | ||
q.ishr(1); | ||
} | ||
// Skip leading zeroes | ||
var res = this; | ||
for (var i = 0; i < w.length; i++, res = res.montSqr()) { | ||
if (w[i] !== 0) | ||
break; | ||
} | ||
if (++i < w.length) { | ||
for (var q = res.montSqr(); i < w.length; i++, q = q.montSqr()) { | ||
if (w[i] === 0) | ||
continue; | ||
res = res.montMul(q); | ||
} | ||
} | ||
return res; | ||
}; | ||
BN.mont = function mont(num, base) { | ||
@@ -917,0 +973,0 @@ return new Mont(num, base); |
{ | ||
"name": "bn.js", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
@@ -178,3 +178,29 @@ var assert = require('assert'); | ||
assert(c.cmp(a.mul(b).mod(p192)) === 0); | ||
assert.equal(a.toMont(m).montPow(3).fromMont().cmp(a.sqr().mul(a)), 0); | ||
assert.equal(a.toMont(m).montPow(4).fromMont().cmp(a.sqr().sqr()), 0); | ||
assert.equal(a.toMont(m).montPow(8).fromMont().cmp(a.sqr().sqr().sqr()), 0); | ||
assert.equal(a.toMont(m).montPow(9).fromMont() | ||
.cmp(a.sqr().sqr().sqr().mul(a)), 0); | ||
}); | ||
it('should sqrtm numbers', function() { | ||
var p = bn(263); | ||
var m = bn.mont(p); | ||
var q = bn(11).toMont(m); | ||
var qr = q.montSqrt(true, p); | ||
assert.equal(qr.montSqr().cmp(q), 0); | ||
var qr = q.montSqrt(false, p); | ||
assert.equal(qr.montSqr().cmp(q), 0); | ||
var p = bn( | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
var m = bn.mont(p); | ||
var q = bn(13).toMont(m); | ||
var qr = q.montSqrt(true, p); | ||
assert.equal(qr.montSqr().cmp(q), 0); | ||
var qr = q.montSqrt(false, p); | ||
assert.equal(qr.montSqr().cmp(q), 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
32048
1029