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