drprime.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Makes safe primes of a DR nature */
  2. #include <tommath.h>
  3. int sizes[] = { 1+256/DIGIT_BIT, 1+512/DIGIT_BIT, 1+768/DIGIT_BIT, 1+1024/DIGIT_BIT, 1+2048/DIGIT_BIT, 1+4096/DIGIT_BIT };
  4. int main(void)
  5. {
  6. int res, x, y;
  7. char buf[4096];
  8. FILE *out;
  9. mp_int a, b;
  10. mp_init(&a);
  11. mp_init(&b);
  12. out = fopen("drprimes.txt", "w");
  13. for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
  14. top:
  15. printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT);
  16. mp_grow(&a, sizes[x]);
  17. mp_zero(&a);
  18. for (y = 1; y < sizes[x]; y++) {
  19. a.dp[y] = MP_MASK;
  20. }
  21. /* make a DR modulus */
  22. a.dp[0] = -1;
  23. a.used = sizes[x];
  24. /* now loop */
  25. res = 0;
  26. for (;;) {
  27. a.dp[0] += 4;
  28. if (a.dp[0] >= MP_MASK) break;
  29. mp_prime_is_prime(&a, 1, &res);
  30. if (res == 0) continue;
  31. printf("."); fflush(stdout);
  32. mp_sub_d(&a, 1, &b);
  33. mp_div_2(&b, &b);
  34. mp_prime_is_prime(&b, 3, &res);
  35. if (res == 0) continue;
  36. mp_prime_is_prime(&a, 3, &res);
  37. if (res == 1) break;
  38. }
  39. if (res != 1) {
  40. printf("Error not DR modulus\n"); sizes[x] += 1; goto top;
  41. } else {
  42. mp_toradix(&a, buf, 10);
  43. printf("\n\np == %s\n\n", buf);
  44. fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf); fflush(out);
  45. }
  46. }
  47. fclose(out);
  48. mp_clear(&a);
  49. mp_clear(&b);
  50. return 0;
  51. }
  52. /* $Source: /cvs/libtom/libtommath/etc/drprime.c,v $ */
  53. /* $Revision: 1.2 $ */
  54. /* $Date: 2005/05/05 14:38:47 $ */