germain.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Copyright (c) 1995 Colin Plumb. All rights reserved.
  3. * For licensing and other legal details, see the file legal.c.
  4. *
  5. * Sophie Germain prime generation using the bignum library and sieving.
  6. */
  7. #ifndef HAVE_CONFIG_H
  8. #define HAVE_CONFIG_H 0
  9. #endif
  10. #if HAVE_CONFIG_H
  11. #include "bnconfig.h"
  12. #endif
  13. /*
  14. * Some compilers complain about #if FOO if FOO isn't defined,
  15. * so do the ANSI-mandated thing explicitly...
  16. */
  17. #ifndef NO_ASSERT_H
  18. #define NO_ASSERT_H 0
  19. #endif
  20. #if !NO_ASSERT_H
  21. #include <assert.h>
  22. #else
  23. #define assert(x) (void)0
  24. #endif
  25. #define BNDEBUG 1
  26. #ifndef BNDEBUG
  27. #define BNDEBUG 0
  28. #endif
  29. #if BNDEBUG
  30. #include <stdio.h>
  31. #endif
  32. #include "bn.h"
  33. #include "germain.h"
  34. #include "jacobi.h"
  35. #include "lbnmem.h" /* For lbnMemWipe */
  36. #include "sieve.h"
  37. #include "kludge.h"
  38. /* Size of the sieve area (can be up to 65536/8 = 8192) */
  39. #define SIEVE 8192
  40. static unsigned const confirm[] = {2, 3, 5, 7, 11, 13, 17};
  41. #define CONFIRMTESTS (sizeof(confirm)/sizeof(*confirm))
  42. #if BNDEBUG
  43. /*
  44. * For sanity checking the sieve, we check for small divisors of the numbers
  45. * we get back. This takes "rem", a partially reduced form of the prime,
  46. * "div" a divisor to check for, and "order", a parameter of the "order"
  47. * of Sophie Germain primes (0 = normal primes, 1 = Sophie Germain primes,
  48. * 2 = 4*p+3 is also prime, etc.) and does the check. It just complains
  49. * to stdout if the check fails.
  50. */
  51. static void
  52. germainSanity(unsigned rem, unsigned div, unsigned order)
  53. {
  54. unsigned mul = 1;
  55. rem %= div;
  56. if (!rem)
  57. printf("bn div by %u!\n", div);
  58. while (order--) {
  59. rem += rem+1;
  60. if (rem >= div)
  61. rem -= div;
  62. mul += mul;
  63. if (!rem)
  64. printf("%u*bn+%u div by %u!\n", mul, mul-1, div);
  65. }
  66. }
  67. #endif /* BNDEBUG */
  68. /*
  69. * Helper function that does the slow primality test.
  70. * bn is the input bignum; a, e and bn2 are temporary buffers that are
  71. * allocated by the caller to save overhead. bn2 is filled with
  72. * a copy of 2^order*bn+2^order-1 if bn is found to be prime.
  73. *
  74. * Returns 0 if both bn and bn2 are prime, >0 if not prime, and -1 on
  75. * error (out of memory). If not prime, the return value is the number
  76. * of modular exponentiations performed. Prints a '+' or '-' on the
  77. * given FILE (if any) for each test that is passed by bn, and a '*'
  78. * for each test that is passed by bn2.
  79. *
  80. * The testing consists of strong pseudoprimality tests, to the bases given
  81. * in the confirm[] array above. (Also called Miller-Rabin, although that's
  82. * not technically correct if we're using fixed bases.) Some people worry
  83. * that this might not be enough. Number theorists may wish to generate
  84. * primality proofs, but for random inputs, this returns non-primes with
  85. * a probability which is quite negligible, which is good enough.
  86. *
  87. * It has been proved (see Carl Pomerance, "On the Distribution of
  88. * Pseudoprimes", Math. Comp. v.37 (1981) pp. 587-593) that the number of
  89. * pseudoprimes (composite numbers that pass a Fermat test to the base 2)
  90. * less than x is bounded by:
  91. * exp(ln(x)^(5/14)) <= P_2(x) ### CHECK THIS FORMULA - it looks wrong! ###
  92. * P_2(x) <= x * exp(-1/2 * ln(x) * ln(ln(ln(x))) / ln(ln(x))).
  93. * Thus, the local density of Pseudoprimes near x is at most
  94. * exp(-1/2 * ln(x) * ln(ln(ln(x))) / ln(ln(x))), and at least
  95. * exp(ln(x)^(5/14) - ln(x)). Here are some values of this function
  96. * for various k-bit numbers x = 2^k:
  97. * Bits Density <= Bit equivalent Density >= Bit equivalent
  98. * 128 3.577869e-07 21.414396 4.202213e-37 120.840190
  99. * 192 4.175629e-10 31.157288 4.936250e-56 183.724558
  100. * 256 5.804314e-13 40.647940 4.977813e-75 246.829095
  101. * 384 1.578039e-18 59.136573 3.938861e-113 373.400096
  102. * 512 5.858255e-24 77.175803 2.563353e-151 500.253110
  103. * 768 1.489276e-34 112.370944 7.872825e-228 754.422724
  104. * 1024 6.633188e-45 146.757062 1.882404e-304 1008.953565
  105. *
  106. * As you can see, there's quite a bit of slop between these estimates.
  107. * In fact, the density of pseudoprimes is conjectured to be closer to the
  108. * square of that upper bound. E.g. the density of pseudoprimes of size
  109. * 256 is around 3 * 10^-27. The density of primes is very high, from
  110. * 0.005636 at 256 bits to 0.001409 at 1024 bits, i.e. more than 10^-3.
  111. *
  112. * For those people used to cryptographic levels of security where the
  113. * 56 bits of DES key space is too small because it's exhaustible with
  114. * custom hardware searching engines, note that you are not generating
  115. * 50,000,000 primes per second on each of 56,000 custom hardware chips
  116. * for several hours. The chances that another Dinosaur Killer asteroid
  117. * will land today is about 10^-11 or 2^-36, so it would be better to
  118. * spend your time worrying about *that*. Well, okay, there should be
  119. * some derating for the chance that astronomers haven't seen it yet,
  120. * but I think you get the idea. For a good feel about the probability
  121. * of various events, I have heard that a good book is by E'mile Borel,
  122. * "Les Probabilite's et la vie". (The 's are accents, not apostrophes.)
  123. *
  124. * For more on the subject, try "Finding Four Million Large Random Primes",
  125. * by Ronald Rivest, in Advancess in Cryptology: Proceedings of Crypto
  126. * '90. He used a small-divisor test, then a Fermat test to the base 2,
  127. * and then 8 iterations of a Miller-Rabin test. About 718 million random
  128. * 256-bit integers were generated, 43,741,404 passed the small divisor
  129. * test, 4,058,000 passed the Fermat test, and all 4,058,000 passed all
  130. * 8 iterations of the Miller-Rabin test, proving their primality beyond
  131. * most reasonable doubts.
  132. *
  133. * If the probability of getting a pseudoprime is some small p, then the
  134. * probability of not getting it in t trials is (1-p)^t. Remember that,
  135. * for small p, (1-p)^(1/p) ~ 1/e, the base of natural logarithms.
  136. * (This is more commonly expressed as e = lim_{x\to\infty} (1+1/x)^x.)
  137. * Thus, (1-p)^t ~ e^(-p*t) = exp(-p*t). So the odds of being able to
  138. * do this many tests without seeing a pseudoprime if you assume that
  139. * p = 10^-6 (one in a million) is one in 57.86. If you assume that
  140. * p = 2*10^-6, it's one in 3347.6. So it's implausible that the density
  141. * of pseudoprimes is much more than one millionth the density of primes.
  142. *
  143. * He also gives a theoretical argument that the chance of finding a
  144. * 256-bit non-prime which satisfies one Fermat test to the base 2 is
  145. * less than 10^-22. The small divisor test improves this number, and
  146. * if the numbers are 512 bits (as needed for a 1024-bit key) the odds
  147. * of failure shrink to about 10^-44. Thus, he concludes, for practical
  148. * purposes *one* Fermat test to the base 2 is sufficient.
  149. */
  150. static int
  151. germainPrimeTest(struct BigNum const *bn, struct BigNum *bn2, struct BigNum *e,
  152. struct BigNum *a, unsigned order, int (*f)(void *arg, int c), void *arg)
  153. {
  154. int err;
  155. unsigned i;
  156. int j;
  157. unsigned k, l, n;
  158. #if BNDEBUG /* Debugging */
  159. /*
  160. * This is debugging code to test the sieving stage.
  161. * If the sieving is wrong, it will let past numbers with
  162. * small divisors. The prime test here will still work, and
  163. * weed them out, but you'll be doing a lot more slow tests,
  164. * and presumably excluding from consideration some other numbers
  165. * which might be prime. This check just verifies that none
  166. * of the candidates have any small divisors. If this
  167. * code is enabled and never triggers, you can feel quite
  168. * confident that the sieving is doing its job.
  169. */
  170. i = bnLSWord(bn);
  171. if (!(i % 2)) printf("bn div by 2!");
  172. i = bnModQ(bn, 51051); /* 51051 = 3 * 7 * 11 * 13 * 17 */
  173. germainSanity(i, 3, order);
  174. germainSanity(i, 7, order);
  175. germainSanity(i, 11, order);
  176. germainSanity(i, 13, order);
  177. germainSanity(i, 17, order);
  178. i = bnModQ(bn, 63365); /* 63365 = 5 * 19 * 23 * 29 */
  179. germainSanity(i, 5, order);
  180. germainSanity(i, 19, order);
  181. germainSanity(i, 23, order);
  182. germainSanity(i, 29, order);
  183. i = bnModQ(bn, 47027); /* 47027 = 31 * 37 * 41 */
  184. germainSanity(i, 31, order);
  185. germainSanity(i, 37, order);
  186. germainSanity(i, 41, order);
  187. #endif
  188. /*
  189. * First, check whether bn is prime. This uses a fast primality
  190. * test which usually obviates the need to do one of the
  191. * confirmation tests later. See prime.c for a full explanation.
  192. * We check bn first because it's one bit smaller, saving one
  193. * modular squaring, and because we might be able to save another
  194. * when testing it. (1/4 of the time.) A small speed hack,
  195. * but finding big Sophie Germain primes is *slow*.
  196. */
  197. if (bnCopy(e, bn) < 0)
  198. return -1;
  199. (void)bnSubQ(e, 1);
  200. l = bnLSWord(e);
  201. j = 1; /* Where to start in prime array for strong prime tests */
  202. if (l & 7) {
  203. bnRShift(e, 1);
  204. if (bnTwoExpMod(a, e, bn) < 0)
  205. return -1;
  206. if ((l & 7) == 6) {
  207. /* bn == 7 mod 8, expect +1 */
  208. if (bnBits(a) != 1)
  209. return 1; /* Not prime */
  210. k = 1;
  211. } else {
  212. /* bn == 3 or 5 mod 8, expect -1 == bn-1 */
  213. if (bnAddQ(a, 1) < 0)
  214. return -1;
  215. if (bnCmp(a, bn) != 0)
  216. return 1; /* Not prime */
  217. k = 1;
  218. if (l & 4) {
  219. /* bn == 5 mod 8, make odd for strong tests */
  220. bnRShift(e, 1);
  221. k = 2;
  222. }
  223. }
  224. } else {
  225. /* bn == 1 mod 8, expect 2^((bn-1)/4) == +/-1 mod bn */
  226. bnRShift(e, 2);
  227. if (bnTwoExpMod(a, e, bn) < 0)
  228. return -1;
  229. if (bnBits(a) == 1) {
  230. j = 0; /* Re-do strong prime test to base 2 */
  231. } else {
  232. if (bnAddQ(a, 1) < 0)
  233. return -1;
  234. if (bnCmp(a, bn) != 0)
  235. return 1; /* Not prime */
  236. }
  237. k = 2 + bnMakeOdd(e);
  238. }
  239. /*
  240. * It's prime! Now check higher-order forms bn2 = 2*bn+1, 4*bn+3,
  241. * etc. Since bn2 == 3 mod 4, a strong pseudoprimality test boils
  242. * down to looking at a^((bn2-1)/2) mod bn and seeing if it's +/-1.
  243. * (+1 if bn2 is == 7 mod 8, -1 if it's == 3)
  244. * Of course, that exponent is just the previous bn2 or bn...
  245. */
  246. if (bnCopy(bn2, bn) < 0)
  247. return -1;
  248. for (n = 0; n < order; n++) {
  249. /*
  250. * Print a success indicator: the sign of Jacobi(2,bn2),
  251. * which is available to us in l. bn2 = 2*bn + 1. Since bn
  252. * is odd, bn2 must be == 3 mod 4, so the options modulo 8
  253. * are 3 and 7. 3 if l == 1 mod 4, 7 if l == 3 mod 4.
  254. * The sign of the Jacobi symbol is - and + for these cases,
  255. * respectively.
  256. */
  257. if (f && (err = f(arg, "-+"[(l >> 1) & 1])) < 0)
  258. return err;
  259. /* Exponent is previous bn2 */
  260. if (bnCopy(e, bn2) < 0 || bnLShift(bn2, 1) < 0)
  261. return -1;
  262. (void)bnAddQ(bn2, 1); /* Can't overflow */
  263. if (bnTwoExpMod(a, e, bn2) < 0)
  264. return -1;
  265. if (n | l) { /* Expect + */
  266. if (bnBits(a) != 1)
  267. return 2+n; /* Not prime */
  268. } else {
  269. if (bnAddQ(a, 1) < 0)
  270. return -1;
  271. if (bnCmp(a, bn2) != 0)
  272. return 2+n; /* Not prime */
  273. }
  274. l = bnLSWord(bn2);
  275. }
  276. /* Final success indicator - it's in the bag. */
  277. if (f && (err = f(arg, '*')) < 0)
  278. return err;
  279. /*
  280. * Success! We have found a prime! Now go on to confirmation
  281. * tests... k is an amount by which we know it's safe to shift
  282. * down e. j = 1 unless the test to the base 2 could stand to be
  283. * re-done (it wasn't *quite* a strong test), in which case it's 0.
  284. *
  285. * Here, we do the full strong pseudoprimality test. This proves
  286. * that a number is composite, or says that it's probably prime.
  287. *
  288. * For the given base a, find bn-1 = 2^k * e, then find
  289. * x == a^e (mod bn).
  290. * If x == +1 -> strong pseudoprime to base a
  291. * Otherwise, repeat k times:
  292. * If x == -1, -> strong pseudoprime
  293. * x = x^2 (mod bn)
  294. * If x = +1 -> composite
  295. * If we reach the end of the iteration and x is *not* +1, at the
  296. * end, it is composite. But it's also composite if the result
  297. * *is* +1. Which means that the squaring actually only has to
  298. * proceed k-1 times. If x is not -1 by then, it's composite
  299. * no matter what the result of the squaring is.
  300. *
  301. * For the multiples 2*bn+1, 4*bn+3, etc. then k = 1 (and e is
  302. * the previous multiple of bn) so the squaring loop is never
  303. * actually executed at all.
  304. */
  305. for (i = j; i < CONFIRMTESTS; i++) {
  306. if (bnCopy(e, bn) < 0)
  307. return -1;
  308. bnRShift(e, k);
  309. k += bnMakeOdd(e);
  310. (void)bnSetQ(a, confirm[i]);
  311. if (bnExpMod(a, a, e, bn) < 0)
  312. return -1;
  313. if (bnBits(a) != 1) {
  314. l = k;
  315. for (;;) {
  316. if (bnAddQ(a, 1) < 0)
  317. return -1;
  318. if (bnCmp(a, bn) == 0) /* Was result bn-1? */
  319. break; /* Prime */
  320. if (!--l)
  321. return (1+order)*i+2; /* Fail */
  322. /* This part is executed once, on average. */
  323. (void)bnSubQ(a, 1); /* Restore a */
  324. if (bnSquare(a, a) < 0 || bnMod(a, a, bn) < 0)
  325. return -1;
  326. if (bnBits(a) == 1)
  327. return (1+order)*i+1; /* Fail */
  328. }
  329. }
  330. if (bnCopy(bn2, bn) < 0)
  331. return -1;
  332. /* Only do the following if we're not re-doing base 2 */
  333. if (i) for (n = 0; n < order; n++) {
  334. if (bnCopy(e, bn2) < 0 || bnLShift(bn2, 1) < 0)
  335. return -1;
  336. (void)bnAddQ(bn2, 1);
  337. /* Print success indicator for previous test */
  338. j = bnJacobiQ(confirm[i], bn2);
  339. if (f && (err = f(arg, j < 0 ? '-' : '+')) < 0)
  340. return err;
  341. /* Check that p^e == Jacobi(p,bn2) (mod bn2) */
  342. (void)bnSetQ(a, confirm[i]);
  343. if (bnExpMod(a, a, e, bn2) < 0)
  344. return -1;
  345. /*
  346. * FIXME: Actually, we don't need to compute the
  347. * Jacobi symbol externally... it never happens that
  348. * a = +/-1 but it's the wrong one. So we can just
  349. * look at a and use its sign. Find a proof somewhere.
  350. */
  351. if (j < 0) {
  352. /* Not a Q.R., should have a = bn2-1 */
  353. if (bnAddQ(a, 1) < 0)
  354. return -1;
  355. if (bnCmp(a, bn2) != 0) /* Was result bn2-1? */
  356. return (1+order)*i+n+2; /* Fail */
  357. } else {
  358. /* Quadratic residue, should have a = 1 */
  359. if (bnBits(a) != 1)
  360. return (1+order)*i+n+2; /* Fail */
  361. }
  362. }
  363. /* Final success indicator for the base confirm[i]. */
  364. if (f && (err = f(arg, '*')) < 0)
  365. return err;
  366. }
  367. return 0; /* Prime! */
  368. }
  369. /*
  370. * Add x*y to bn, which is usually (but not always) < 65536.
  371. * Do it in a simple linear manner.
  372. */
  373. static int
  374. bnAddMult(struct BigNum *bn, unsigned long x, unsigned y)
  375. {
  376. unsigned long z = (unsigned long)x * y;
  377. while (z > 65535) {
  378. if (bnAddQ(bn, 65535) < 0)
  379. return -1;
  380. z -= 65535;
  381. }
  382. return bnAddQ(bn, (unsigned)z);
  383. }
  384. /*
  385. * Modifies the bignum to return the next Sophie Germain prime >= the
  386. * input value. Sohpie Germain primes are number such that p is
  387. * prime and 2*p+1 is also prime.
  388. *
  389. * This is actually parameterized: it generates primes p such that "order"
  390. * multiples-plus-two are also prime, 2*p+1, 2*(2*p+1)+1 = 4*p+3, etc.
  391. *
  392. * Returns >=0 on success or -1 on failure (out of memory). On success,
  393. * the return value is the number of modular exponentiations performed
  394. * (excluding the final confirmations). This never gives up searching.
  395. *
  396. * The FILE *f argument, if non-NULL, has progress indicators written
  397. * to it. A dot (.) is written every time a primeality test is failed,
  398. * a plus (+) or minus (-) when the smaller prime of the pair passes a
  399. * test, and a star (*) when the larger one does. Finally, a slash (/)
  400. * is printed when the sieve was emptied without finding a prime and is
  401. * being refilled.
  402. *
  403. * Apologies to structured programmers for all the GOTOs.
  404. */
  405. int
  406. germainPrimeGen(struct BigNum *bn, unsigned order,
  407. int (*f)(void *arg, int c), void *arg)
  408. {
  409. int retval;
  410. unsigned p, prev;
  411. unsigned inc;
  412. struct BigNum a, e, bn2;
  413. int modexps = 0;
  414. #ifdef MSDOS
  415. unsigned char *sieve;
  416. #else
  417. unsigned char sieve[SIEVE];
  418. #endif
  419. #ifdef MSDOS
  420. sieve = lbnMemAlloc(SIEVE);
  421. if (!sieve)
  422. return -1;
  423. #endif
  424. bnBegin(&a);
  425. bnBegin(&e);
  426. bnBegin(&bn2);
  427. /*
  428. * Obviously, the prime we find must be odd. Further, if 2*p+1
  429. * is also to be prime (order > 0) then p != 1 (mod 3), lest
  430. * 2*p+1 == 3 (mod 3). Added to p != 3 (mod 3), p == 2 (mod 3)
  431. * and p == 5 (mod 6).
  432. * If order > 2 and we care about 4*p+3 and 8*p+7, then similarly
  433. * p == 4 (mod 5), so p == 29 (mod 30).
  434. * So pick the step size for searching based on the order
  435. * and increse bn until it's == -1 (mod inc).
  436. *
  437. * mod 7 doesn't have a unique value for p because 2 -> 5 -> 4 -> 2,
  438. * nor does mod 11, and I don't want to think about things past
  439. * that. The required order would be impractically high, in any case.
  440. */
  441. inc = order ? ((order > 2) ? 30 : 6) : 2;
  442. if (bnAddQ(bn, inc-1 - bnModQ(bn, inc)) < 0)
  443. goto failed;
  444. for (;;) {
  445. if (sieveBuild(sieve, SIEVE, bn, inc, order) < 0)
  446. goto failed;
  447. p = prev = 0;
  448. if (sieve[0] & 1 || (p = sieveSearch(sieve, SIEVE, p)) != 0) {
  449. do {
  450. /* Adjust bn to have the right value. */
  451. assert(p >= prev);
  452. if (bnAddMult(bn, p-prev, inc) < 0)
  453. goto failed;
  454. prev = p;
  455. /* Okay, do the strong tests. */
  456. retval = germainPrimeTest(bn, &bn2, &e, &a,
  457. order, f, arg);
  458. if (retval <= 0)
  459. goto done;
  460. modexps += retval;
  461. if (f && (retval = f(arg, '.')) < 0)
  462. goto done;
  463. /* And try again */
  464. p = sieveSearch(sieve, SIEVE, p);
  465. } while (p);
  466. }
  467. /* Ran out of sieve space - increase bn and keep trying. */
  468. if (bnAddMult(bn, (unsigned long)SIEVE*8-prev, inc) < 0)
  469. goto failed;
  470. if (f && (retval = f(arg, '/')) < 0)
  471. goto done;
  472. } /* for (;;) */
  473. failed:
  474. retval = -1;
  475. done:
  476. bnEnd(&bn2);
  477. bnEnd(&e);
  478. bnEnd(&a);
  479. #ifdef MSDOS
  480. lbnMemFree(sieve, SIEVE);
  481. #else
  482. lbnMemWipe(sieve, sizeof(sieve));
  483. #endif
  484. return retval < 0 ? retval : modexps+(order+1)*CONFIRMTESTS;
  485. }
  486. int
  487. germainPrimeGenStrong(struct BigNum *bn, struct BigNum const *step,
  488. unsigned order, int (*f)(void *arg, int c), void *arg)
  489. {
  490. int retval;
  491. unsigned p, prev;
  492. struct BigNum a, e, bn2;
  493. int modexps = 0;
  494. #ifdef MSDOS
  495. unsigned char *sieve;
  496. #else
  497. unsigned char sieve[SIEVE];
  498. #endif
  499. #ifdef MSDOS
  500. sieve = lbnMemAlloc(SIEVE);
  501. if (!sieve)
  502. return -1;
  503. #endif
  504. bnBegin(&a);
  505. bnBegin(&e);
  506. bnBegin(&bn2);
  507. for (;;) {
  508. if (sieveBuildBig(sieve, SIEVE, bn, step, order) < 0)
  509. goto failed;
  510. p = prev = 0;
  511. if (sieve[0] & 1 || (p = sieveSearch(sieve, SIEVE, p)) != 0) {
  512. do {
  513. /*
  514. * Adjust bn to have the right value,
  515. * adding (p-prev) * 2*step.
  516. */
  517. assert(p >= prev);
  518. /* Compute delta into a */
  519. if (bnMulQ(&a, step, p-prev) < 0)
  520. goto failed;
  521. if (bnAdd(bn, &a) < 0)
  522. goto failed;
  523. prev = p;
  524. /* Okay, do the strong tests. */
  525. retval = germainPrimeTest(bn, &bn2, &e, &a,
  526. order, f, arg);
  527. if (retval <= 0)
  528. goto done;
  529. modexps += retval;
  530. if (f && (retval = f(arg, '.')) < 0)
  531. goto done;
  532. /* And try again */
  533. p = sieveSearch(sieve, SIEVE, p);
  534. } while (p);
  535. }
  536. /* Ran out of sieve space - increase bn and keep trying. */
  537. #if SIEVE*8 == 65536
  538. /* Corner case that will never actually happen */
  539. if (!prev) {
  540. if (bnAdd(bn, step) < 0)
  541. goto failed;
  542. p = 65535;
  543. } else {
  544. p = (unsigned)(SIEVE*8 - prev);
  545. }
  546. #else
  547. p = SIEVE*8 - prev;
  548. #endif
  549. if (bnMulQ(&a, step, p) < 0 || bnAdd(bn, &a) < 0)
  550. goto failed;
  551. if (f && (retval = f(arg, '/')) < 0)
  552. goto done;
  553. } /* for (;;) */
  554. failed:
  555. retval = -1;
  556. done:
  557. bnEnd(&bn2);
  558. bnEnd(&e);
  559. bnEnd(&a);
  560. #ifdef MSDOS
  561. lbnMemFree(sieve, SIEVE);
  562. #else
  563. lbnMemWipe(sieve, sizeof(sieve));
  564. #endif
  565. return retval < 0 ? retval : modexps+(order+1)*CONFIRMTESTS;
  566. }