[Pkg-javascript-commits] [node-diffie-hellman] 59/88: avoid prime rundway skew
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 4 10:19:17 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 fd818d71789e6aff9dbc4c3c6b2979163e2690c2
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date: Fri Jun 19 14:25:34 2015 -0400
avoid prime rundway skew
---
lib/generatePrime.js | 65 +++++++++++++++-------------------------------------
1 file changed, 19 insertions(+), 46 deletions(-)
diff --git a/lib/generatePrime.js b/lib/generatePrime.js
index 248b5b1..32e053c 100644
--- a/lib/generatePrime.js
+++ b/lib/generatePrime.js
@@ -71,62 +71,35 @@ function findPrime(bits, gen) {
}
}
gen = new BN(gen);
- var runs, comp;
- function generateRandom(bits) {
- runs = -1;
- var out = new BN(randomBytes(Math.ceil(bits / 8)));
- while (out.bitLength() > bits) {
- out.ishrn(1);
+
+ var num, n2;
+
+ while (true) {
+ num = new BN(randomBytes(Math.ceil(bits / 8)));
+ while (num.bitLength() > bits) {
+ num.ishrn(1);
}
- if (out.isEven()) {
- out.iadd(ONE);
+ if (num.isEven()) {
+ num.iadd(ONE);
}
- if (!out.testn(1)) {
- out.iadd(TWO);
+ if (!num.testn(1)) {
+ num.iadd(TWO);
}
if (!gen.cmp(TWO)) {
- while (out.mod(TWENTYFOUR).cmp(ELEVEN)) {
- out.iadd(FOUR);
+ while (num.mod(TWENTYFOUR).cmp(ELEVEN)) {
+ num.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);
+ while (num.mod(TEN).cmp(THREE)) {
+ num.iadd(FOUR);
}
- comp = {
- major: [FOUR, SIXTEEN],
- minor: [TWO, EIGHT]
- };
- } else {
- comp = {
- major: [FOUR],
- minor: [TWO]
- };
- }
- return out;
- }
- var num = generateRandom(bits);
-
- var n2 = num.shrn(1);
-
- while (true) {
- while (num.bitLength() > bits) {
- num = generateRandom(bits);
- n2 = num.shrn(1);
}
- runs++;
- if (simpleSieve(n2) && simpleSieve(num) &&
- fermatTest(n2) && fermatTest(num) &&
+ n2 = num.shrn(1);
+ if (simpleSieve(n2) && simpleSieve(num) &&
+ fermatTest(n2) && fermatTest(num) &&
millerRabin.test(n2) && millerRabin.test(num)) {
return num;
}
- num.iadd(comp.major[runs%comp.major.length]);
- n2.iadd(comp.minor[runs%comp.minor.length]);
}
-}
\ 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