|
1 /* |
|
2 * Simple test driver for MPI library |
|
3 * |
|
4 * Test 8: Probabilistic primality tester |
|
5 * |
|
6 * This Source Code Form is subject to the terms of the Mozilla Public |
|
7 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
9 |
|
10 #include <stdio.h> |
|
11 #include <stdlib.h> |
|
12 #include <string.h> |
|
13 #include <ctype.h> |
|
14 #include <limits.h> |
|
15 #include <time.h> |
|
16 |
|
17 #define MP_IOFUNC 1 |
|
18 #include "mpi.h" |
|
19 |
|
20 #include "mpprime.h" |
|
21 |
|
22 int main(int argc, char *argv[]) |
|
23 { |
|
24 int ix; |
|
25 mp_digit num; |
|
26 mp_int a; |
|
27 |
|
28 srand(time(NULL)); |
|
29 |
|
30 if(argc < 2) { |
|
31 fprintf(stderr, "Usage: %s <a>\n", argv[0]); |
|
32 return 1; |
|
33 } |
|
34 |
|
35 printf("Test 8: Probabilistic primality testing\n\n"); |
|
36 |
|
37 mp_init(&a); |
|
38 |
|
39 mp_read_radix(&a, argv[1], 10); |
|
40 |
|
41 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
|
42 |
|
43 printf("\nChecking for divisibility by small primes ... \n"); |
|
44 num = 170; |
|
45 if(mpp_divis_primes(&a, &num) == MP_YES) { |
|
46 printf("it is not prime\n"); |
|
47 goto CLEANUP; |
|
48 } |
|
49 printf("Passed that test (not divisible by any small primes).\n"); |
|
50 |
|
51 for(ix = 0; ix < 10; ix++) { |
|
52 printf("\nPerforming Rabin-Miller test, iteration %d\n", ix + 1); |
|
53 |
|
54 if(mpp_pprime(&a, 5) == MP_NO) { |
|
55 printf("it is not prime\n"); |
|
56 goto CLEANUP; |
|
57 } |
|
58 } |
|
59 printf("All tests passed; a is probably prime\n"); |
|
60 |
|
61 CLEANUP: |
|
62 mp_clear(&a); |
|
63 |
|
64 return 0; |
|
65 } |