miller-rabin
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -13,16 +13,20 @@ var bn = require('bn.js'); | ||
MillerRabin.prototype._rand = function _rand(n) { | ||
MillerRabin.prototype._randbelow = function _randbelow(n) { | ||
var len = n.bitLength(); | ||
var buf = this.rand.generate(Math.ceil(len / 8)); | ||
var min_bytes = Math.ceil(len / 8); | ||
// Set low bits | ||
buf[0] |= 3; | ||
// Generage random bytes until a number less than n is found. | ||
// This ensures that 0..n-1 have an equal probability of being selected. | ||
do | ||
var a = new bn(this.rand.generate(min_bytes)); | ||
while (a.cmp(n) >= 0); | ||
// Mask high bits | ||
var mask = len & 0x7; | ||
if (mask !== 0) | ||
buf[buf.length - 1] >>= 7 - mask; | ||
return a; | ||
}; | ||
return new bn(buf); | ||
} | ||
MillerRabin.prototype._randrange = function _randrange(start, stop) { | ||
// Generate a random number greater than or equal to start and less than stop. | ||
var size = stop.sub(start); | ||
return start.add(this._randbelow(size)); | ||
}; | ||
@@ -39,3 +43,2 @@ MillerRabin.prototype.test = function test(n, k, cb) { | ||
var n1 = n.subn(1); | ||
var n2 = n1.subn(1); | ||
for (var s = 0; !n1.testn(s); s++) {} | ||
@@ -48,3 +51,3 @@ var d = n.shrn(s); | ||
for (; k > 0; k--) { | ||
var a = this._rand(n2); | ||
var a = this._randrange(new bn(2), n1); | ||
if (cb) | ||
@@ -83,3 +86,2 @@ cb(a); | ||
var n1 = n.subn(1); | ||
var n2 = n1.subn(1); | ||
for (var s = 0; !n1.testn(s); s++) {} | ||
@@ -91,3 +93,3 @@ var d = n.shrn(s); | ||
for (; k > 0; k--) { | ||
var a = this._rand(n2); | ||
var a = this._randrange(new bn(2), n1); | ||
@@ -94,0 +96,0 @@ var g = n.gcd(a); |
{ | ||
"name": "miller-rabin", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Miller Rabin algorithm for primality test", | ||
@@ -5,0 +5,0 @@ "main": "lib/mr.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
6840
8
132