[Pkg-javascript-commits] [node-diffie-hellman] 27/88: quicker iteration

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:19:14 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 47cedb69bc5d900b45529e358fdf35e55eebdd24
Author: Calvin Metcalf <calvin.metcalf at state.ma.us>
Date:   Sun Nov 9 15:38:01 2014 -0500

    quicker iteration
---
 generatePrime.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 71 insertions(+), 15 deletions(-)

diff --git a/generatePrime.js b/generatePrime.js
index bf32f48..44be34e 100644
--- a/generatePrime.js
+++ b/generatePrime.js
@@ -9,21 +9,51 @@ var ONE = new BN(1);
 var TWO = new BN(2);
 var ELEVEN = new BN(11);
 var FOUR = new BN(4);
+var TWELVE = new BN(12);
+var primes = null;
+
+function _getPrimes() {
+  if (primes !== null)
+    return primes;
+
+  var limit = 0x100000;
+  var res = [];
+  res[0] = 2;
+  for (var i = 1, k = 3; k < limit; k += 2) {
+    var sqrt = Math.ceil(Math.sqrt(k));
+    for (var j = 0; j < i && res[j] <= sqrt; j++)
+      if (k % res[j] === 0)
+        break;
+
+    if (i !== j && res[j] <= sqrt)
+      continue;
+
+    res[i++] = k;
+  }
+  primes = res;
+  return res;
+}
+function simpleSieve(p) {
+  var primes = _getPrimes();
+
+  for (var i = 0; i < primes.length; i++)
+    if (p.modn(primes[i]) === 0)
+      return false;
+
+  return true;
+}
+function fermatTest(p) {
+  var red = BN.mont(p);
+  return TWO.toRed(red).redPow(p.subn(1)).fromRed().cmpn(1) === 0;
+}
 function findPrime(bits, crypto) {
 
   function generateRandom(bits) {
-    var bytes = bits >> 3;
-    bytes = bytes || 1;
-    var out = new BN(crypto.randomBytes(bytes));
-    while (out.bitLength() > bits) {
-      out.ishrn(1);
-    }
-    if (out.isEven()) {
-      out.iadd(ONE);
-    }
-    if (!out.testn(1)) {
-      out.iadd(TWO);
-    }
+    var r = crypto.randomBytes(Math.ceil(bits / 8));
+    r[0] |= 0xc0;
+    r[r.length - 1] |= 3;
+
+    var out = new BN(r);
     while (out.mod(TWENTYFOUR).cmp(ELEVEN)) {
       out.iadd(FOUR);
     }
@@ -33,18 +63,44 @@ function findPrime(bits, crypto) {
 
 
   var runs = 0;
-
+  var n2 = num.shrn(1);
   while (true) {
     runs++;
     if (num.bitLength() > bits) {
       num = generateRandom(bits);
+      n2 = num.shrn(1);
+    }
+    if (!simpleSieve(n2)) {
+      num.iadd(TWENTYFOUR);
+      n2.iadd(TWELVE);
+      continue;
+    }
+    if (!fermatTest(n2)) {
+      num.iadd(TWENTYFOUR);
+      n2.iadd(TWELVE);
+      continue;
     }
-    if(millerRabin.test(num) && millerRabin.test(num.shrn(1))) {
+    if (!millerRabin.test(n2)) {
+      num.iadd(TWENTYFOUR);
+      n2.iadd(TWELVE);
+      continue;
+    }
+    if (!simpleSieve(num)) {
+      num.iadd(TWENTYFOUR);
+      n2.iadd(TWELVE);
+      continue;
+    }
+    if (!fermatTest(num)) {
+      num.iadd(TWENTYFOUR);
+      n2.iadd(TWELVE);
+      continue;
+    }
+    if (millerRabin.test(num)) {
       console.log('found in', runs);
       return num;
     }
-
     num.iadd(TWENTYFOUR);
+    n2.iadd(TWELVE);
   }
 
 }
\ No newline at end of file

-- 
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