comparison etc/2kprime.c @ 1:22d5cf7d4b1a libtommath

Renaming branch
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:23:46 +0000
parents
children 97db060d0ef5
comparison
equal deleted inserted replaced
-1:000000000000 1:22d5cf7d4b1a
1 /* Makes safe primes of a 2k nature */
2 #include <tommath.h>
3 #include <time.h>
4
5 int sizes[] = {256, 512, 768, 1024, 1536, 2048, 3072, 4096};
6
7 int main(void)
8 {
9 char buf[2000];
10 int x, y;
11 mp_int q, p;
12 FILE *out;
13 clock_t t1;
14 mp_digit z;
15
16 mp_init_multi(&q, &p, NULL);
17
18 out = fopen("2kprime.1", "w");
19 for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) {
20 top:
21 mp_2expt(&q, sizes[x]);
22 mp_add_d(&q, 3, &q);
23 z = -3;
24
25 t1 = clock();
26 for(;;) {
27 mp_sub_d(&q, 4, &q);
28 z += 4;
29
30 if (z > MP_MASK) {
31 printf("No primes of size %d found\n", sizes[x]);
32 break;
33 }
34
35 if (clock() - t1 > CLOCKS_PER_SEC) {
36 printf("."); fflush(stdout);
37 // sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
38 t1 = clock();
39 }
40
41 /* quick test on q */
42 mp_prime_is_prime(&q, 1, &y);
43 if (y == 0) {
44 continue;
45 }
46
47 /* find (q-1)/2 */
48 mp_sub_d(&q, 1, &p);
49 mp_div_2(&p, &p);
50 mp_prime_is_prime(&p, 3, &y);
51 if (y == 0) {
52 continue;
53 }
54
55 /* test on q */
56 mp_prime_is_prime(&q, 3, &y);
57 if (y == 0) {
58 continue;
59 }
60
61 break;
62 }
63
64 if (y == 0) {
65 ++sizes[x];
66 goto top;
67 }
68
69 mp_toradix(&q, buf, 10);
70 printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
71 fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf); fflush(out);
72 }
73
74 return 0;
75 }
76
77
78
79
80