Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * gcd.c |
michael@0 | 3 | * |
michael@0 | 4 | * Greatest common divisor |
michael@0 | 5 | * |
michael@0 | 6 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | #include <string.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "mpi.h" |
michael@0 | 15 | |
michael@0 | 16 | char *g_prog = NULL; |
michael@0 | 17 | |
michael@0 | 18 | void print_mp_int(mp_int *mp, FILE *ofp); |
michael@0 | 19 | |
michael@0 | 20 | int main(int argc, char *argv[]) |
michael@0 | 21 | { |
michael@0 | 22 | mp_int a, b, x, y; |
michael@0 | 23 | mp_err res; |
michael@0 | 24 | int ext = 0; |
michael@0 | 25 | |
michael@0 | 26 | g_prog = argv[0]; |
michael@0 | 27 | |
michael@0 | 28 | if(argc < 3) { |
michael@0 | 29 | fprintf(stderr, "Usage: %s <a> <b>\n", g_prog); |
michael@0 | 30 | return 1; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | mp_init(&a); mp_read_radix(&a, argv[1], 10); |
michael@0 | 34 | mp_init(&b); mp_read_radix(&b, argv[2], 10); |
michael@0 | 35 | |
michael@0 | 36 | /* If we were called 'xgcd', compute x, y so that g = ax + by */ |
michael@0 | 37 | if(strcmp(g_prog, "xgcd") == 0) { |
michael@0 | 38 | ext = 1; |
michael@0 | 39 | mp_init(&x); mp_init(&y); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | if(ext) { |
michael@0 | 43 | if((res = mp_xgcd(&a, &b, &a, &x, &y)) != MP_OKAY) { |
michael@0 | 44 | fprintf(stderr, "%s: error: %s\n", g_prog, mp_strerror(res)); |
michael@0 | 45 | mp_clear(&a); mp_clear(&b); |
michael@0 | 46 | mp_clear(&x); mp_clear(&y); |
michael@0 | 47 | return 1; |
michael@0 | 48 | } |
michael@0 | 49 | } else { |
michael@0 | 50 | if((res = mp_gcd(&a, &b, &a)) != MP_OKAY) { |
michael@0 | 51 | fprintf(stderr, "%s: error: %s\n", g_prog, |
michael@0 | 52 | mp_strerror(res)); |
michael@0 | 53 | mp_clear(&a); mp_clear(&b); |
michael@0 | 54 | return 1; |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | print_mp_int(&a, stdout); |
michael@0 | 59 | if(ext) { |
michael@0 | 60 | fputs("x = ", stdout); print_mp_int(&x, stdout); |
michael@0 | 61 | fputs("y = ", stdout); print_mp_int(&y, stdout); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | mp_clear(&a); mp_clear(&b); |
michael@0 | 65 | |
michael@0 | 66 | if(ext) { |
michael@0 | 67 | mp_clear(&x); |
michael@0 | 68 | mp_clear(&y); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | return 0; |
michael@0 | 72 | |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | void print_mp_int(mp_int *mp, FILE *ofp) |
michael@0 | 76 | { |
michael@0 | 77 | char *buf; |
michael@0 | 78 | int len; |
michael@0 | 79 | |
michael@0 | 80 | len = mp_radix_size(mp, 10); |
michael@0 | 81 | buf = calloc(len, sizeof(char)); |
michael@0 | 82 | mp_todecimal(mp, buf); |
michael@0 | 83 | fprintf(ofp, "%s\n", buf); |
michael@0 | 84 | free(buf); |
michael@0 | 85 | |
michael@0 | 86 | } |