[Pkg-javascript-commits] [node-diffie-hellman] 30/88: generate primes with other generators

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 20e5a0e260fd87c1bb17b4c8eb8abd7dcaa40884
Author: Calvin Metcalf <calvin.metcalf at state.ma.us>
Date:   Sun Nov 16 15:24:19 2014 -0500

    generate primes with other generators
---
 generatePrime.js | 79 +++++++++++++++++++++++++++++++-------------------------
 inject.js        | 14 +++++-----
 test.js          | 17 +++++++-----
 3 files changed, 63 insertions(+), 47 deletions(-)

diff --git a/generatePrime.js b/generatePrime.js
index cc97c43..38d047b 100644
--- a/generatePrime.js
+++ b/generatePrime.js
@@ -8,6 +8,11 @@ var MillerRabin = require('miller-rabin');
 var millerRabin = new MillerRabin();
 var ONE = new BN(1);
 var TWO = new BN(2);
+var FIVE = new BN(5);
+var SIX = new BN(6);
+var TEN = new BN(10);
+var THREE = new BN(3);
+var SEVEN = new BN(7);
 var ELEVEN = new BN(11);
 var FOUR = new BN(4);
 var TWELVE = new BN(12);
@@ -47,60 +52,64 @@ 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 findPrime(bits, gen ,crypto) {
+  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);
-    while (out.mod(TWENTYFOUR).cmp(ELEVEN)) {
-      out.iadd(FOUR);
+    if (!gen.cmp(TWO)) {
+      while (out.mod(TWENTYFOUR).cmp(ELEVEN)) {
+        out.iadd(FOUR);
+      }
+      comp = {
+        major: [TWENTYFOUR],
+        minor: [TWELVE]
+      };
+    } else if (!gen.cmp(FIVE)) {
+      rem = out.mod(TEN);
+      while (rem.cmp(THREE)) {
+        out.iadd(FOUR);
+        rem = out.mod(TEN);
+      }
+      comp = {
+        major: [FOUR, SIX],
+        minor: [TWO, THREE]
+      };
+    } else {
+      comp = {
+        major: [FOUR],
+        minor: [TWO]
+      }
     }
     return out;
   }
   var num = generateRandom(bits);
 
 
-  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(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)) {
+    runs++;
+    if (simpleSieve(n2) &&
+      fermatTest(n2) &&
+      millerRabin.test(n2) &&
+      simpleSieve(num) &&
+      fermatTest(num) &&
+      millerRabin.test(num)) {
       return num;
     }
-    num.iadd(TWENTYFOUR);
-    n2.iadd(TWELVE);
+    num.iadd(comp.major[runs%comp.major.length]);
+    n2.iadd(comp.minor[runs%comp.minor.length]);
   }
 
 }
\ No newline at end of file
diff --git a/inject.js b/inject.js
index 49eb8f0..85fdcf7 100644
--- a/inject.js
+++ b/inject.js
@@ -11,9 +11,7 @@ module.exports = function (crypto, exports) {
 	}
 	exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
 	function DiffieHellman(prime, enc, generator, genc) {
-		if (typeof prime === 'number') {
-			return new DH(generatePrime(prime, crypto), new Buffer([2]), crypto, true);
-		}
+		
 		if (Buffer.isBuffer(enc) ||
 			(typeof enc === 'string' && ['hex', 'binary', 'base64'].indexOf(enc) === -1)) {
 			genc = generator;
@@ -23,12 +21,16 @@ module.exports = function (crypto, exports) {
 		enc = enc || 'binary';
 		genc = genc || 'binary';
 		generator = generator || new Buffer([2]);
-		if (!Buffer.isBuffer(prime)) {
-			prime = new Buffer(prime, enc);
-		}
 		if (!Buffer.isBuffer(generator)) {
 			generator = new Buffer(generator, genc);
 		}
+		if (typeof prime === 'number') {
+			return new DH(generatePrime(prime, generator, crypto), generator, crypto, true);
+		}
+		if (!Buffer.isBuffer(prime)) {
+			prime = new Buffer(prime, enc);
+		}
+		
 		return new DH(prime, generator, crypto, true);
 	};
 }
\ No newline at end of file
diff --git a/test.js b/test.js
index 19d7928..31f852a 100644
--- a/test.js
+++ b/test.js
@@ -35,14 +35,14 @@ function run(i) {
 }
 
 
-function bylen(t) {
+function bylen(t, generator) {
 	return function (len){
 		t.test('' + len, function (t) {
-			t.plan(5);
-			var dh2 = myCrypto.createDiffieHellman(len);
+			t.plan(6);
+			var dh2 = myCrypto.createDiffieHellman(len, generator);
 			var prime2 = dh2.getPrime();
 			var p2 = prime2.toString('hex');
-			var dh1 = nodeCrypto.createDiffieHellman(prime2, 2);
+			var dh1 = nodeCrypto.createDiffieHellman(prime2, generator);
 			//console.log('error', dh1.verifyError)
 			var p1 = dh1.getPrime().toString('hex');
 			t.equals(typeof dh1.setPublicKey, typeof dh2.setPublicKey, 'same methods');
@@ -50,6 +50,7 @@ function bylen(t) {
 			dh1.generateKeys();
 			dh2.generateKeys();
 			t.equals(p1, p2, 'equal primes');
+			t.equals(dh1.getGenerator('hex'), dh2.getGenerator('hex'), 'equal generators');
 			var pubk1 = dh1.getPublicKey();
 			var pubk2 = dh2.getPublicKey();
 			t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys');
@@ -83,8 +84,12 @@ function bylen2(t) {
 	};
 }
 
-test('create primes', function (t) {
-	var f = bylen(t);
+test('create primes gen 2', function (t) {
+	var f = bylen(t, new Buffer([2]));
+	lens2.forEach(f);
+});
+test('create primes gen 5', function (t) {
+	var f = bylen(t, new Buffer([5]));
 	lens2.forEach(f);
 });
 

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