95ed2de4227f607b2634c00126b9972b3dcdbaee
[samba.git] / source4 / heimdal / lib / hcrypto / libtommath / etc / 2kprime.c
1 /* Makes safe primes of a 2k nature */
2 #include <tommath.h>
3 #include <time.h>
4
5 static int sizes[] = {256, 512, 768, 1024, 1536, 2048, 3072, 4096};
6
7 int main(void)
8 {
9    char buf[2000];
10    size_t x;
11    mp_bool y;
12    mp_int q, p;
13    FILE *out;
14    clock_t t1;
15    mp_digit z;
16
17    mp_init_multi(&q, &p, NULL);
18
19    out = fopen("2kprime.1", "w");
20    if (out != NULL) {
21       for (x = 0; x < (sizeof(sizes) / sizeof(sizes[0])); x++) {
22 top:
23          mp_2expt(&q, sizes[x]);
24          mp_add_d(&q, 3uL, &q);
25          z = -3;
26
27          t1 = clock();
28          for (;;) {
29             mp_sub_d(&q, 4uL, &q);
30             z += 4uL;
31
32             if (z > MP_MASK) {
33                printf("No primes of size %d found\n", sizes[x]);
34                break;
35             }
36
37             if ((clock() - t1) > CLOCKS_PER_SEC) {
38                printf(".");
39                fflush(stdout);
40                /*            sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC); */
41                t1 = clock();
42             }
43
44             /* quick test on q */
45             mp_prime_is_prime(&q, 1, &y);
46             if (y == MP_NO) {
47                continue;
48             }
49
50             /* find (q-1)/2 */
51             mp_sub_d(&q, 1uL, &p);
52             mp_div_2(&p, &p);
53             mp_prime_is_prime(&p, 3, &y);
54             if (y == MP_NO) {
55                continue;
56             }
57
58             /* test on q */
59             mp_prime_is_prime(&q, 3, &y);
60             if (y == MP_NO) {
61                continue;
62             }
63
64             break;
65          }
66
67          if (y == MP_NO) {
68             ++sizes[x];
69             goto top;
70          }
71
72          mp_to_decimal(&q, buf, sizeof(buf));
73          printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
74          fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
75          fflush(out);
76       }
77       fclose(out);
78    }
79
80    return 0;
81 }