[Pkg-javascript-commits] [node-diffie-hellman] 35/88: smaller primes

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 cf7bdb8f9abbffe7bae4ed568ae582fdd852ff94
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Tue Dec 2 09:13:34 2014 -0500

    smaller primes
---
 generatePrime.js | 31 ++++++++++++++++++++++++-------
 test.js          |  2 +-
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/generatePrime.js b/generatePrime.js
index 8857f43..00d55af 100644
--- a/generatePrime.js
+++ b/generatePrime.js
@@ -54,15 +54,32 @@ function fermatTest(p) {
   return TWO.toRed(red).redPow(p.subn(1)).fromRed().cmpn(1) === 0;
 }
 function findPrime(bits, gen ,crypto) {
+  if (bits < 16) {
+    // this is what openssl does
+    if (gen === 2 || gen === 5) {
+      return new BN([0x8c, 0x7b]);
+    } else {
+      return new BN([0x8c, 0x27]);
+    }
+  }
+  var rebits = bits;
+  if (bits < 22) {
+    rebits = 22;
+  }
   gen = new BN(gen);
   var runs, comp;
   function generateRandom(bits) {
     runs = -1;
-    var r = crypto.randomBytes(Math.ceil(bits / 8));
-    r[0] |= 0xc0;
-    r[r.length - 1] |= 3;
-    var rem;
-    var out = new BN(r);
+    var out = new BN(crypto.randomBytes(Math.ceil(bits / 8)));
+    while (out.bitLength() > bits) {
+      out.ishrn(1);
+    }
+    if (out.isEven()) {
+      out.iadd(ONE);
+    }
+    if (!out.testn(1)) {
+      out.iadd(TWO);
+    }
     if (!gen.cmp(TWO)) {
       while (out.mod(TWENTYFOUR).cmp(ELEVEN)) {
         out.iadd(FOUR);
@@ -85,7 +102,7 @@ function findPrime(bits, gen ,crypto) {
       comp = {
         major: [FOUR],
         minor: [TWO]
-      }
+      };
     }
     return out;
   }
@@ -96,7 +113,7 @@ function findPrime(bits, gen ,crypto) {
   var n2 = num.shrn(1);
 
   while (true) {
-    if (num.bitLength() > bits) {
+    if (num.bitLength() > rebits) {
       num = generateRandom(bits);
       n2 = num.shrn(1);
     }
diff --git a/test.js b/test.js
index 91f3aff..009d398 100644
--- a/test.js
+++ b/test.js
@@ -9,7 +9,7 @@ function isNode10() {
   return process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) <= 10;
 }
  var lens = [
-  64, 65, 128, 384, 512, 1024,
+  2, 8, 16, 17, 18, 20, 21, 22, 64, 65, 128, 384, 512, 1024,
   192, 224, 256];
    var lens2 = [
   64, 65, 128];

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