michael@0: /* michael@0: * invmod.c michael@0: * michael@0: * Compute modular inverses michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "mpi.h" michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: mp_int a, m; michael@0: mp_err res; michael@0: char *buf; michael@0: int len, out = 0; michael@0: michael@0: if(argc < 3) { michael@0: fprintf(stderr, "Usage: %s \n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: mp_init(&a); mp_init(&m); michael@0: mp_read_radix(&a, argv[1], 10); michael@0: mp_read_radix(&m, argv[2], 10); michael@0: michael@0: if(mp_cmp(&a, &m) > 0) michael@0: mp_mod(&a, &m, &a); michael@0: michael@0: switch((res = mp_invmod(&a, &m, &a))) { michael@0: case MP_OKAY: michael@0: len = mp_radix_size(&a, 10); michael@0: buf = malloc(len); michael@0: michael@0: mp_toradix(&a, buf, 10); michael@0: printf("%s\n", buf); michael@0: free(buf); michael@0: break; michael@0: michael@0: case MP_UNDEF: michael@0: printf("No inverse\n"); michael@0: out = 1; michael@0: break; michael@0: michael@0: default: michael@0: printf("error: %s (%d)\n", mp_strerror(res), res); michael@0: out = 2; michael@0: break; michael@0: } michael@0: michael@0: mp_clear(&a); michael@0: mp_clear(&m); michael@0: michael@0: return out; michael@0: }