PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/security/nss/lib/freebl/mpi/utils/gcd.c

http://github.com/zpao/v8monkey
C | 119 lines | 59 code | 18 blank | 42 comment | 11 complexity | 37a24a192869ec799ee4c8978d9373ed MD5 | raw file
  1/*
  2 *  gcd.c
  3 *
  4 *  Greatest common divisor
  5 *
  6 * ***** BEGIN LICENSE BLOCK *****
  7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8 *
  9 * The contents of this file are subject to the Mozilla Public License Version
 10 * 1.1 (the "License"); you may not use this file except in compliance with
 11 * the License. You may obtain a copy of the License at
 12 * http://www.mozilla.org/MPL/
 13 *
 14 * Software distributed under the License is distributed on an "AS IS" basis,
 15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 16 * for the specific language governing rights and limitations under the
 17 * License.
 18 *
 19 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
 20 *
 21 * The Initial Developer of the Original Code is
 22 * Michael J. Fromberger.
 23 * Portions created by the Initial Developer are Copyright (C) 1998
 24 * the Initial Developer. All Rights Reserved.
 25 *
 26 * Contributor(s):
 27 *
 28 * Alternatively, the contents of this file may be used under the terms of
 29 * either the GNU General Public License Version 2 or later (the "GPL"), or
 30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 31 * in which case the provisions of the GPL or the LGPL are applicable instead
 32 * of those above. If you wish to allow use of your version of this file only
 33 * under the terms of either the GPL or the LGPL, and not to allow others to
 34 * use your version of this file under the terms of the MPL, indicate your
 35 * decision by deleting the provisions above and replace them with the notice
 36 * and other provisions required by the GPL or the LGPL. If you do not delete
 37 * the provisions above, a recipient may use your version of this file under
 38 * the terms of any one of the MPL, the GPL or the LGPL.
 39 *
 40 * ***** END LICENSE BLOCK ***** */
 41/* $Id: gcd.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
 42
 43#include <stdio.h>
 44#include <stdlib.h>
 45#include <string.h>
 46
 47#include "mpi.h"
 48
 49char     *g_prog = NULL;
 50
 51void print_mp_int(mp_int *mp, FILE *ofp);
 52
 53int main(int argc, char *argv[]) 
 54{
 55  mp_int   a, b, x, y;
 56  mp_err   res;
 57  int      ext = 0;
 58
 59  g_prog = argv[0];
 60
 61  if(argc < 3) {
 62    fprintf(stderr, "Usage: %s <a> <b>\n", g_prog);
 63    return 1;
 64  }
 65
 66  mp_init(&a); mp_read_radix(&a, argv[1], 10);
 67  mp_init(&b); mp_read_radix(&b, argv[2], 10);
 68
 69  /* If we were called 'xgcd', compute x, y so that g = ax + by */
 70  if(strcmp(g_prog, "xgcd") == 0) {
 71    ext = 1;
 72    mp_init(&x); mp_init(&y);
 73  }
 74
 75  if(ext) {
 76    if((res = mp_xgcd(&a, &b, &a, &x, &y)) != MP_OKAY) {
 77      fprintf(stderr, "%s: error: %s\n", g_prog, mp_strerror(res));
 78      mp_clear(&a); mp_clear(&b);
 79      mp_clear(&x); mp_clear(&y);
 80      return 1;
 81    }
 82  } else {
 83    if((res = mp_gcd(&a, &b, &a)) != MP_OKAY) {
 84      fprintf(stderr, "%s: error: %s\n", g_prog,
 85	      mp_strerror(res));
 86      mp_clear(&a); mp_clear(&b);
 87      return 1;
 88    }
 89  }
 90
 91  print_mp_int(&a, stdout); 
 92  if(ext) {
 93    fputs("x = ", stdout); print_mp_int(&x, stdout); 
 94    fputs("y = ", stdout); print_mp_int(&y, stdout); 
 95  }
 96
 97  mp_clear(&a); mp_clear(&b);
 98
 99  if(ext) {
100    mp_clear(&x);
101    mp_clear(&y);
102  }
103
104  return 0;
105
106}
107
108void print_mp_int(mp_int *mp, FILE *ofp)
109{
110  char  *buf;
111  int    len;
112
113  len = mp_radix_size(mp, 10);
114  buf = calloc(len, sizeof(char));
115  mp_todecimal(mp, buf);
116  fprintf(ofp, "%s\n", buf);
117  free(buf);
118
119}