aboutsummaryrefslogtreecommitdiff
path: root/node_modules/miller-rabin/lib/mr.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/miller-rabin/lib/mr.js')
-rw-r--r--node_modules/miller-rabin/lib/mr.js30
1 files changed, 16 insertions, 14 deletions
diff --git a/node_modules/miller-rabin/lib/mr.js b/node_modules/miller-rabin/lib/mr.js
index a9e935b1b..60d2a8ef9 100644
--- a/node_modules/miller-rabin/lib/mr.js
+++ b/node_modules/miller-rabin/lib/mr.js
@@ -10,20 +10,24 @@ MillerRabin.create = function create(rand) {
return new MillerRabin(rand);
};
-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));
+};
MillerRabin.prototype.test = function test(n, k, cb) {
var len = n.bitLength();
@@ -35,7 +39,6 @@ MillerRabin.prototype.test = function test(n, k, cb) {
// Find d and s, (n - 1) = (2 ^ s) * d;
var n1 = n.subn(1);
- var n2 = n1.subn(1);
for (var s = 0; !n1.testn(s); s++) {}
var d = n.shrn(s);
@@ -43,7 +46,7 @@ MillerRabin.prototype.test = function test(n, k, cb) {
var prime = true;
for (; k > 0; k--) {
- var a = this._rand(n2);
+ var a = this._randrange(new bn(2), n1);
if (cb)
cb(a);
@@ -77,14 +80,13 @@ MillerRabin.prototype.getDivisor = function getDivisor(n, k) {
// Find d and s, (n - 1) = (2 ^ s) * d;
var n1 = n.subn(1);
- var n2 = n1.subn(1);
for (var s = 0; !n1.testn(s); s++) {}
var d = n.shrn(s);
var rn1 = n1.toRed(red);
for (; k > 0; k--) {
- var a = this._rand(n2);
+ var a = this._randrange(new bn(2), n1);
var g = n.gcd(a);
if (g.cmpn(1) !== 0)