|
1 /* |
|
2 * Simple test driver for MPI library |
|
3 * |
|
4 * Test 5: Other number theoretic functions |
|
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 |
|
16 #include "mpi.h" |
|
17 |
|
18 int main(int argc, char *argv[]) |
|
19 { |
|
20 mp_int a, b, c, x, y; |
|
21 |
|
22 if(argc < 3) { |
|
23 fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]); |
|
24 return 1; |
|
25 } |
|
26 |
|
27 printf("Test 5: Number theoretic functions\n\n"); |
|
28 |
|
29 mp_init(&a); |
|
30 mp_init(&b); |
|
31 |
|
32 mp_read_radix(&a, argv[1], 10); |
|
33 mp_read_radix(&b, argv[2], 10); |
|
34 |
|
35 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
|
36 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
37 |
|
38 mp_init(&c); |
|
39 printf("\nc = (a, b)\n"); |
|
40 |
|
41 mp_gcd(&a, &b, &c); |
|
42 printf("Euclid: c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
43 /* |
|
44 mp_bgcd(&a, &b, &c); |
|
45 printf("Binary: c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
46 */ |
|
47 mp_init(&x); |
|
48 mp_init(&y); |
|
49 printf("\nc = (a, b) = ax + by\n"); |
|
50 |
|
51 mp_xgcd(&a, &b, &c, &x, &y); |
|
52 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
53 printf("x = "); mp_print(&x, stdout); fputc('\n', stdout); |
|
54 printf("y = "); mp_print(&y, stdout); fputc('\n', stdout); |
|
55 |
|
56 printf("\nc = a^-1 (mod b)\n"); |
|
57 if(mp_invmod(&a, &b, &c) == MP_UNDEF) { |
|
58 printf("a has no inverse mod b\n"); |
|
59 } else { |
|
60 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
61 } |
|
62 |
|
63 mp_clear(&y); |
|
64 mp_clear(&x); |
|
65 mp_clear(&c); |
|
66 mp_clear(&b); |
|
67 mp_clear(&a); |
|
68 |
|
69 return 0; |
|
70 } |