1 /* Generates provable primes
3 * See http://gmail.com:8080/papers/pp.pdf for more info.
5 * Tom St Denis, tomstdenis@gmail.com, http://tom.gmail.com
13 /* fast square root */
22 x2 = x1 - ((x1 * x1) - x) / (2 * x1);
33 /* generates a prime digit */
34 static void gen_prime (void)
36 mp_digit r, x, y, next;
39 out = fopen("pprime.dat", "wb");
41 /* write first set of primes */
42 r = 3; fwrite(&r, 1, sizeof(mp_digit), out);
43 r = 5; fwrite(&r, 1, sizeof(mp_digit), out);
44 r = 7; fwrite(&r, 1, sizeof(mp_digit), out);
45 r = 11; fwrite(&r, 1, sizeof(mp_digit), out);
46 r = 13; fwrite(&r, 1, sizeof(mp_digit), out);
47 r = 17; fwrite(&r, 1, sizeof(mp_digit), out);
48 r = 19; fwrite(&r, 1, sizeof(mp_digit), out);
49 r = 23; fwrite(&r, 1, sizeof(mp_digit), out);
50 r = 29; fwrite(&r, 1, sizeof(mp_digit), out);
51 r = 31; fwrite(&r, 1, sizeof(mp_digit), out);
53 /* get square root, since if 'r' is composite its factors must be < than this */
55 next = (y + 1) * (y + 1);
59 r += 2; /* next candidate */
66 next = (y + 1) * (y + 1);
69 /* loop if divisible by 3,5,7,11,13,17,19,23,29 */
107 /* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
108 for (x = 30; x <= y; x += 30) {
109 if ((r % (x + 1)) == 0) {
113 if ((r % (x + 7)) == 0) {
117 if ((r % (x + 11)) == 0) {
121 if ((r % (x + 13)) == 0) {
125 if ((r % (x + 17)) == 0) {
129 if ((r % (x + 19)) == 0) {
133 if ((r % (x + 23)) == 0) {
137 if ((r % (x + 29)) == 0) {
143 if (r > 31) { fwrite(&r, 1, sizeof(mp_digit), out); printf("%9d\r", r); fflush(stdout); }
152 primes = fopen("pprime.dat", "rb");
153 if (primes == NULL) {
155 primes = fopen("pprime.dat", "rb");
157 fseek(primes, 0, SEEK_END);
158 n_prime = ftell(primes) / sizeof(mp_digit);
161 mp_digit prime_digit(void)
166 n = labs(rand()) % n_prime;
167 fseek(primes, n * sizeof(mp_digit), SEEK_SET);
168 fread(&d, 1, sizeof(mp_digit), primes);
173 /* makes a prime of at least k bits */
175 pprime (int k, int li, mp_int * p, mp_int * q)
177 mp_int a, b, c, n, x, y, z, v;
179 static const mp_digit bases[] = { 2, 3, 5, 7, 11, 13, 17, 19 };
182 if (k <= (int) DIGIT_BIT) {
183 mp_set (p, prime_digit ());
187 if ((res = mp_init (&c)) != MP_OKAY) {
191 if ((res = mp_init (&v)) != MP_OKAY) {
195 /* product of first 50 primes */
198 "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
203 if ((res = mp_init (&a)) != MP_OKAY) {
208 mp_set (&a, prime_digit ());
210 if ((res = mp_init (&b)) != MP_OKAY) {
214 if ((res = mp_init (&n)) != MP_OKAY) {
218 if ((res = mp_init (&x)) != MP_OKAY) {
222 if ((res = mp_init (&y)) != MP_OKAY) {
226 if ((res = mp_init (&z)) != MP_OKAY) {
230 /* now loop making the single digit */
231 while (mp_count_bits (&a) < k) {
232 fprintf (stderr, "prime has %4d bits left\r", k - mp_count_bits (&a));
235 mp_set (&b, prime_digit ());
237 /* now compute z = a * b * 2 */
238 if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */
242 if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */
246 if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
251 if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
255 /* check (n, v) == 1 */
256 if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
260 if (mp_cmp_d (&y, 1) != MP_EQ)
263 /* now try base x=bases[ii] */
264 for (ii = 0; ii < li; ii++) {
265 mp_set (&x, bases[ii]);
267 /* compute x^a mod n */
268 if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
273 if (mp_cmp_d (&y, 1) == MP_EQ)
277 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
281 if (mp_cmp_d (&y, 1) == MP_EQ)
284 /* compute x^b mod n */
285 if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
290 if (mp_cmp_d (&y, 1) == MP_EQ)
294 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
298 if (mp_cmp_d (&y, 1) == MP_EQ)
301 /* compute x^c mod n == x^ab mod n */
302 if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
307 if (mp_cmp_d (&y, 1) == MP_EQ)
310 /* now compute (x^c mod n)^2 */
311 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
316 if (mp_cmp_d (&y, 1) != MP_EQ)
321 /* no bases worked? */
328 mp_toradix(&n, buf, 10);
329 printf("Certificate of primality for:\n%s\n\n", buf);
330 mp_toradix(&a, buf, 10);
331 printf("A == \n%s\n\n", buf);
332 mp_toradix(&b, buf, 10);
333 printf("B == \n%s\n\nG == %d\n", buf, bases[ii]);
334 printf("----------------------------------------------------------------\n");
341 /* get q to be the order of the large prime subgroup */
344 mp_div (q, &b, q, NULL);
372 printf ("Enter # of bits: \n");
373 fgets (buf, sizeof (buf), stdin);
374 sscanf (buf, "%d", &k);
376 printf ("Enter number of bases to try (1 to 8):\n");
377 fgets (buf, sizeof (buf), stdin);
378 sscanf (buf, "%d", &li);
385 pprime (k, li, &p, &q);
388 printf ("\n\nTook %ld ticks, %d bits\n", t1, mp_count_bits (&p));
390 mp_toradix (&p, buf, 10);
391 printf ("P == %s\n", buf);
392 mp_toradix (&q, buf, 10);
393 printf ("Q == %s\n", buf);
398 /* $Source: /cvs/libtom/libtommath/etc/pprime.c,v $ */
399 /* $Revision: 1.3 $ */
400 /* $Date: 2006/03/31 14:18:47 $ */