pprime.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Generates provable primes
  2. *
  3. * See http://gmail.com:8080/papers/pp.pdf for more info.
  4. *
  5. * Tom St Denis, tomstdenis@gmail.com, http://tom.gmail.com
  6. */
  7. #include <time.h>
  8. #include "tommath.h"
  9. int n_prime;
  10. FILE *primes;
  11. /* fast square root */
  12. static mp_digit
  13. i_sqrt (mp_word x)
  14. {
  15. mp_word x1, x2;
  16. x2 = x;
  17. do {
  18. x1 = x2;
  19. x2 = x1 - ((x1 * x1) - x) / (2 * x1);
  20. } while (x1 != x2);
  21. if (x1 * x1 > x) {
  22. --x1;
  23. }
  24. return x1;
  25. }
  26. /* generates a prime digit */
  27. static void gen_prime (void)
  28. {
  29. mp_digit r, x, y, next;
  30. FILE *out;
  31. out = fopen("pprime.dat", "wb");
  32. /* write first set of primes */
  33. r = 3; fwrite(&r, 1, sizeof(mp_digit), out);
  34. r = 5; fwrite(&r, 1, sizeof(mp_digit), out);
  35. r = 7; fwrite(&r, 1, sizeof(mp_digit), out);
  36. r = 11; fwrite(&r, 1, sizeof(mp_digit), out);
  37. r = 13; fwrite(&r, 1, sizeof(mp_digit), out);
  38. r = 17; fwrite(&r, 1, sizeof(mp_digit), out);
  39. r = 19; fwrite(&r, 1, sizeof(mp_digit), out);
  40. r = 23; fwrite(&r, 1, sizeof(mp_digit), out);
  41. r = 29; fwrite(&r, 1, sizeof(mp_digit), out);
  42. r = 31; fwrite(&r, 1, sizeof(mp_digit), out);
  43. /* get square root, since if 'r' is composite its factors must be < than this */
  44. y = i_sqrt (r);
  45. next = (y + 1) * (y + 1);
  46. for (;;) {
  47. do {
  48. r += 2; /* next candidate */
  49. r &= MP_MASK;
  50. if (r < 31) break;
  51. /* update sqrt ? */
  52. if (next <= r) {
  53. ++y;
  54. next = (y + 1) * (y + 1);
  55. }
  56. /* loop if divisible by 3,5,7,11,13,17,19,23,29 */
  57. if ((r % 3) == 0) {
  58. x = 0;
  59. continue;
  60. }
  61. if ((r % 5) == 0) {
  62. x = 0;
  63. continue;
  64. }
  65. if ((r % 7) == 0) {
  66. x = 0;
  67. continue;
  68. }
  69. if ((r % 11) == 0) {
  70. x = 0;
  71. continue;
  72. }
  73. if ((r % 13) == 0) {
  74. x = 0;
  75. continue;
  76. }
  77. if ((r % 17) == 0) {
  78. x = 0;
  79. continue;
  80. }
  81. if ((r % 19) == 0) {
  82. x = 0;
  83. continue;
  84. }
  85. if ((r % 23) == 0) {
  86. x = 0;
  87. continue;
  88. }
  89. if ((r % 29) == 0) {
  90. x = 0;
  91. continue;
  92. }
  93. /* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
  94. for (x = 30; x <= y; x += 30) {
  95. if ((r % (x + 1)) == 0) {
  96. x = 0;
  97. break;
  98. }
  99. if ((r % (x + 7)) == 0) {
  100. x = 0;
  101. break;
  102. }
  103. if ((r % (x + 11)) == 0) {
  104. x = 0;
  105. break;
  106. }
  107. if ((r % (x + 13)) == 0) {
  108. x = 0;
  109. break;
  110. }
  111. if ((r % (x + 17)) == 0) {
  112. x = 0;
  113. break;
  114. }
  115. if ((r % (x + 19)) == 0) {
  116. x = 0;
  117. break;
  118. }
  119. if ((r % (x + 23)) == 0) {
  120. x = 0;
  121. break;
  122. }
  123. if ((r % (x + 29)) == 0) {
  124. x = 0;
  125. break;
  126. }
  127. }
  128. } while (x == 0);
  129. if (r > 31) { fwrite(&r, 1, sizeof(mp_digit), out); printf("%9d\r", r); fflush(stdout); }
  130. if (r < 31) break;
  131. }
  132. fclose(out);
  133. }
  134. void load_tab(void)
  135. {
  136. primes = fopen("pprime.dat", "rb");
  137. if (primes == NULL) {
  138. gen_prime();
  139. primes = fopen("pprime.dat", "rb");
  140. }
  141. fseek(primes, 0, SEEK_END);
  142. n_prime = ftell(primes) / sizeof(mp_digit);
  143. }
  144. mp_digit prime_digit(void)
  145. {
  146. int n;
  147. mp_digit d;
  148. n = abs(rand()) % n_prime;
  149. fseek(primes, n * sizeof(mp_digit), SEEK_SET);
  150. fread(&d, 1, sizeof(mp_digit), primes);
  151. return d;
  152. }
  153. /* makes a prime of at least k bits */
  154. int
  155. pprime (int k, int li, mp_int * p, mp_int * q)
  156. {
  157. mp_int a, b, c, n, x, y, z, v;
  158. int res, ii;
  159. static const mp_digit bases[] = { 2, 3, 5, 7, 11, 13, 17, 19 };
  160. /* single digit ? */
  161. if (k <= (int) DIGIT_BIT) {
  162. mp_set (p, prime_digit ());
  163. return MP_OKAY;
  164. }
  165. if ((res = mp_init (&c)) != MP_OKAY) {
  166. return res;
  167. }
  168. if ((res = mp_init (&v)) != MP_OKAY) {
  169. goto LBL_C;
  170. }
  171. /* product of first 50 primes */
  172. if ((res =
  173. mp_read_radix (&v,
  174. "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
  175. 10)) != MP_OKAY) {
  176. goto LBL_V;
  177. }
  178. if ((res = mp_init (&a)) != MP_OKAY) {
  179. goto LBL_V;
  180. }
  181. /* set the prime */
  182. mp_set (&a, prime_digit ());
  183. if ((res = mp_init (&b)) != MP_OKAY) {
  184. goto LBL_A;
  185. }
  186. if ((res = mp_init (&n)) != MP_OKAY) {
  187. goto LBL_B;
  188. }
  189. if ((res = mp_init (&x)) != MP_OKAY) {
  190. goto LBL_N;
  191. }
  192. if ((res = mp_init (&y)) != MP_OKAY) {
  193. goto LBL_X;
  194. }
  195. if ((res = mp_init (&z)) != MP_OKAY) {
  196. goto LBL_Y;
  197. }
  198. /* now loop making the single digit */
  199. while (mp_count_bits (&a) < k) {
  200. fprintf (stderr, "prime has %4d bits left\r", k - mp_count_bits (&a));
  201. fflush (stderr);
  202. top:
  203. mp_set (&b, prime_digit ());
  204. /* now compute z = a * b * 2 */
  205. if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */
  206. goto LBL_Z;
  207. }
  208. if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */
  209. goto LBL_Z;
  210. }
  211. if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
  212. goto LBL_Z;
  213. }
  214. /* n = z + 1 */
  215. if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
  216. goto LBL_Z;
  217. }
  218. /* check (n, v) == 1 */
  219. if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
  220. goto LBL_Z;
  221. }
  222. if (mp_cmp_d (&y, 1) != MP_EQ)
  223. goto top;
  224. /* now try base x=bases[ii] */
  225. for (ii = 0; ii < li; ii++) {
  226. mp_set (&x, bases[ii]);
  227. /* compute x^a mod n */
  228. if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
  229. goto LBL_Z;
  230. }
  231. /* if y == 1 loop */
  232. if (mp_cmp_d (&y, 1) == MP_EQ)
  233. continue;
  234. /* now x^2a mod n */
  235. if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
  236. goto LBL_Z;
  237. }
  238. if (mp_cmp_d (&y, 1) == MP_EQ)
  239. continue;
  240. /* compute x^b mod n */
  241. if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
  242. goto LBL_Z;
  243. }
  244. /* if y == 1 loop */
  245. if (mp_cmp_d (&y, 1) == MP_EQ)
  246. continue;
  247. /* now x^2b mod n */
  248. if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
  249. goto LBL_Z;
  250. }
  251. if (mp_cmp_d (&y, 1) == MP_EQ)
  252. continue;
  253. /* compute x^c mod n == x^ab mod n */
  254. if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
  255. goto LBL_Z;
  256. }
  257. /* if y == 1 loop */
  258. if (mp_cmp_d (&y, 1) == MP_EQ)
  259. continue;
  260. /* now compute (x^c mod n)^2 */
  261. if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
  262. goto LBL_Z;
  263. }
  264. /* y should be 1 */
  265. if (mp_cmp_d (&y, 1) != MP_EQ)
  266. continue;
  267. break;
  268. }
  269. /* no bases worked? */
  270. if (ii == li)
  271. goto top;
  272. {
  273. char buf[4096];
  274. mp_toradix(&n, buf, 10);
  275. printf("Certificate of primality for:\n%s\n\n", buf);
  276. mp_toradix(&a, buf, 10);
  277. printf("A == \n%s\n\n", buf);
  278. mp_toradix(&b, buf, 10);
  279. printf("B == \n%s\n\nG == %d\n", buf, bases[ii]);
  280. printf("----------------------------------------------------------------\n");
  281. }
  282. /* a = n */
  283. mp_copy (&n, &a);
  284. }
  285. /* get q to be the order of the large prime subgroup */
  286. mp_sub_d (&n, 1, q);
  287. mp_div_2 (q, q);
  288. mp_div (q, &b, q, NULL);
  289. mp_exch (&n, p);
  290. res = MP_OKAY;
  291. LBL_Z:mp_clear (&z);
  292. LBL_Y:mp_clear (&y);
  293. LBL_X:mp_clear (&x);
  294. LBL_N:mp_clear (&n);
  295. LBL_B:mp_clear (&b);
  296. LBL_A:mp_clear (&a);
  297. LBL_V:mp_clear (&v);
  298. LBL_C:mp_clear (&c);
  299. return res;
  300. }
  301. int
  302. main (void)
  303. {
  304. mp_int p, q;
  305. char buf[4096];
  306. int k, li;
  307. clock_t t1;
  308. srand (time (NULL));
  309. load_tab();
  310. printf ("Enter # of bits: \n");
  311. fgets (buf, sizeof (buf), stdin);
  312. sscanf (buf, "%d", &k);
  313. printf ("Enter number of bases to try (1 to 8):\n");
  314. fgets (buf, sizeof (buf), stdin);
  315. sscanf (buf, "%d", &li);
  316. mp_init (&p);
  317. mp_init (&q);
  318. t1 = clock ();
  319. pprime (k, li, &p, &q);
  320. t1 = clock () - t1;
  321. printf ("\n\nTook %ld ticks, %d bits\n", t1, mp_count_bits (&p));
  322. mp_toradix (&p, buf, 10);
  323. printf ("P == %s\n", buf);
  324. mp_toradix (&q, buf, 10);
  325. printf ("Q == %s\n", buf);
  326. return 0;
  327. }
  328. /* $Source: /cvs/libtom/libtommath/etc/pprime.c,v $ */
  329. /* $Revision: 1.3 $ */
  330. /* $Date: 2006/03/31 14:18:47 $ */