www.usr.com/support/gpl/USR9107_release.1.4.tar.gz
[bcm963xx.git] / userapps / opensource / sshd / libtommath / etc / drprime.c
1 /* Makes safe primes of a DR nature */
2 #include <tommath.h>
3
4 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 };
5 int main(void)
6 {
7    int res, x, y;
8    char buf[4096];
9    FILE *out;
10    mp_int a, b;
11    
12    mp_init(&a);
13    mp_init(&b);
14    
15    out = fopen("drprimes.txt", "w");
16    for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
17    top:
18        printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT);
19        mp_grow(&a, sizes[x]);
20        mp_zero(&a);
21        for (y = 1; y < sizes[x]; y++) {
22            a.dp[y] = MP_MASK;
23        }
24        
25        /* make a DR modulus */
26        a.dp[0] = -1;
27        a.used = sizes[x];
28        
29        /* now loop */
30        res = 0;
31        for (;;) { 
32           a.dp[0] += 4;
33           if (a.dp[0] >= MP_MASK) break;
34           mp_prime_is_prime(&a, 1, &res);
35           if (res == 0) continue;
36           printf("."); fflush(stdout);
37           mp_sub_d(&a, 1, &b);
38           mp_div_2(&b, &b);
39           mp_prime_is_prime(&b, 3, &res);  
40           if (res == 0) continue;
41           mp_prime_is_prime(&a, 3, &res);
42           if (res == 1) break;
43         }
44         
45         if (res != 1) {
46            printf("Error not DR modulus\n"); sizes[x] += 1; goto top;
47         } else {
48            mp_toradix(&a, buf, 10);
49            printf("\n\np == %s\n\n", buf);
50            fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf); fflush(out);
51         }           
52    }
53    fclose(out);
54    
55    mp_clear(&a);
56    mp_clear(&b);
57    
58    return 0;
59 }
60