|
1 /* |
|
2 * exptmod.c |
|
3 * |
|
4 * Command line tool to perform modular exponentiation on arbitrary |
|
5 * precision integers. |
|
6 * |
|
7 * This Source Code Form is subject to the terms of the Mozilla Public |
|
8 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
10 |
|
11 #include <stdio.h> |
|
12 #include <stdlib.h> |
|
13 #include <string.h> |
|
14 |
|
15 #include "mpi.h" |
|
16 |
|
17 int main(int argc, char *argv[]) |
|
18 { |
|
19 mp_int a, b, m; |
|
20 mp_err res; |
|
21 char *str; |
|
22 int len, rval = 0; |
|
23 |
|
24 if(argc < 3) { |
|
25 fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]); |
|
26 return 1; |
|
27 } |
|
28 |
|
29 mp_init(&a); mp_init(&b); mp_init(&m); |
|
30 mp_read_radix(&a, argv[1], 10); |
|
31 mp_read_radix(&b, argv[2], 10); |
|
32 mp_read_radix(&m, argv[3], 10); |
|
33 |
|
34 if((res = mp_exptmod(&a, &b, &m, &a)) != MP_OKAY) { |
|
35 fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res)); |
|
36 rval = 1; |
|
37 } else { |
|
38 len = mp_radix_size(&a, 10); |
|
39 str = calloc(len, sizeof(char)); |
|
40 mp_toradix(&a, str, 10); |
|
41 |
|
42 printf("%s\n", str); |
|
43 |
|
44 free(str); |
|
45 } |
|
46 |
|
47 mp_clear(&a); mp_clear(&b); mp_clear(&m); |
|
48 |
|
49 return rval; |
|
50 } |