[Pkg-javascript-commits] [node-diffie-hellman] 14/88: clean up prime generation

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:19:12 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-diffie-hellman.

commit 721d40b6418dfce6e356af62f297a1cced5679a7
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Wed Nov 5 09:28:53 2014 -0500

    clean up prime generation
---
 dh.js            |  4 ++--
 generatePrime.js | 43 +++++++++++++++++++++++++++++--------------
 test.js          | 17 +++++++----------
 3 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/dh.js b/dh.js
index 812c785..ddcd441 100644
--- a/dh.js
+++ b/dh.js
@@ -62,14 +62,14 @@ DH.prototype.setPublicKey = function (pub, enc) {
 		pub = new Buffer(pub, enc);
 	}
 	this._pub = new BN(pub);
-}
+};
 DH.prototype.setPrivateKey = function (priv, enc) {
 	enc = enc || 'utf8';
 	if (!Buffer.isBuffer(priv)) {
 		priv = new Buffer(priv, enc);
 	}
 	this._priv = new BN(priv);
-}
+};
 function returnValue(bn, enc) {
 	var buf = new Buffer(bn.toArray());
 	if (!enc) {
diff --git a/generatePrime.js b/generatePrime.js
index 42bb142..57d8d5f 100644
--- a/generatePrime.js
+++ b/generatePrime.js
@@ -1,3 +1,4 @@
+
 module.exports = generatePrime;
 
 var goodPrimes = {
@@ -13,7 +14,6 @@ function generatePrime(len, crypto) {
     return findPrime(len, crypto);
   }
 }
-
 // based on find-prime by Kenan Yildirim
 // https://github.com/KenanY/find-prime
 
@@ -63,9 +63,15 @@ function isProbablePrime(n, t) {
   while (i < lowprimes.length) {
     var m = lowprimes[i],
       j = i + 1;
-    while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
+    while (j < lowprimes.length && m < lplim) {
+      m *= lowprimes[j++];
+    }
     m = x.modn(m);
-    while (i < j) if (m % lowprimes[i++] === 0) return false;
+    while (i < j) {
+      if (m % lowprimes[i++] === 0) {
+        return false;
+      }
+    }
   }
   return millerRabin(x, t);
 }
@@ -87,37 +93,46 @@ function millerRabin(n, t) {
   if (k <= 0) return false;
   var r = n1.shrn(k);
   t = (t + 1) >> 1;
-  if (t > lowprimes.length) t = lowprimes.length;
+  if (t > lowprimes.length) {
+    t = lowprimes.length;
+  }
   var a;
-  var j, bases = []
+  var j, bases = [];
   for (var i = 0; i < t; ++i) {
     for (;;) {
-      j = lowprimes[Math.floor(Math.random() * lowprimes.length)]
+      j = lowprimes[Math.floor(Math.random() * lowprimes.length)];
       if (bases.indexOf(j) == -1) break;
     }
     bases.push(j);
     a = new BN(j);
     var y = a.toRed(mp).redPow(r).fromRed();
-    if (y.cmp(new BN(1)) != 0 && y.cmp(n1) != 0) {
-      var j = 1
-      while (j++ < k && y.cmp(n1) != 0) {
+    if (y.cmp(new BN(1)) != 0 && y.cmp(n1) !== 0) {
+      j = 1;
+      while (j++ < k && y.cmp(n1) !== 0) {
         y = y.toRed(mp).redPow(new BN(2)).fromRed();
-        if (y.cmp(new BN(1)) == 0) return false
+        if (y.cmp(new BN(1)) === 0) {
+          return false;
+        }
+      }
+      if (y.cmp(n1) !== 0) {
+        return false;
       }
-      if (y.cmp(n1) != 0) return false
     }
   }
-  return true
+  return true;
 }
 function findPrime(bits, crypto) {
 
   function generateRandom(bits) {
     var bytes = bits >> 3;
     bytes = bytes || 1;
-    var out = new BN(crypto.randomBytes(bits/8));
+    var out = new BN(crypto.randomBytes(bytes));
     while (out.bitLength() > bits) {
       out.ishrn(1);
     }
+    if (out.isEven()) {
+      out.iadd(new BN(1));
+    }
     return out;
   }
   var num = generateRandom(bits);
@@ -131,7 +146,7 @@ function findPrime(bits, crypto) {
     if (num.bitLength() > bits) {
       num = generateRandom(bits);
     }
-
+    console.log(num.toString());
     if(isProbablePrime(num, mrTests)) {
       return num;
     }
diff --git a/test.js b/test.js
index 99f4ca7..fac89c4 100644
--- a/test.js
+++ b/test.js
@@ -5,16 +5,13 @@ var myCrypto = require('./');
 var mods = [
    'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'/*, 'modp17', 'modp18'*/
 ];
-var lens = [128, 64, 256,
-  224,
-  192,
-  25519];
-  var lens2 = [256,
-  224,
-  192,
-  512,
-  384,
-  1024];
+var lens = [
+  64, 128, 384, 512, 1024, //slow
+  192, 224, 256, 25519  //fast
+ ];
+ var lens2 = [
+  64, 128, 384, 512, 1024,
+  192, 224, 256];
 function run(i) {
 	mods.forEach(function (mod) {
 		test(mod + ' run ' + i, function (t){

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-diffie-hellman.git



More information about the Pkg-javascript-commits mailing list