PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://github.com/zpao/v8monkey
C | 121 lines | 60 code | 20 blank | 41 comment | 5 complexity | 412f6d7c0fe046b12b08dbb5861bff20 MD5 | raw file
  1/*
  2 *  lap.c
  3 *
  4 *  Find least annihilating power of a mod m
  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: lap.c,v 1.4 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
 42
 43#include <stdio.h>
 44#include <stdlib.h>
 45#include <signal.h>
 46
 47#include "mpi.h"
 48
 49void sig_catch(int ign);
 50
 51int g_quit = 0;
 52
 53int main(int argc, char *argv[])
 54{
 55  mp_int   a, m, p, k;
 56
 57  if(argc < 3) {
 58    fprintf(stderr, "Usage: %s <a> <m>\n", argv[0]);
 59    return 1;
 60  }
 61
 62  mp_init(&a);
 63  mp_init(&m);
 64  mp_init(&p);
 65  mp_add_d(&p, 1, &p);
 66
 67  mp_read_radix(&a, argv[1], 10);
 68  mp_read_radix(&m, argv[2], 10);
 69
 70  mp_init_copy(&k, &a);
 71
 72  signal(SIGINT, sig_catch);
 73#ifndef __OS2__
 74  signal(SIGHUP, sig_catch);
 75#endif
 76  signal(SIGTERM, sig_catch);
 77
 78  while(mp_cmp(&p, &m) < 0) {
 79    if(g_quit) {
 80	int  len;
 81	char *buf;
 82
 83	len = mp_radix_size(&p, 10);
 84	buf = malloc(len);
 85	mp_toradix(&p, buf, 10);
 86	
 87	fprintf(stderr, "Terminated at: %s\n", buf);
 88	free(buf);
 89	return 1;
 90    }
 91    if(mp_cmp_d(&k, 1) == 0) {
 92      int    len;
 93      char  *buf;
 94
 95      len = mp_radix_size(&p, 10);
 96      buf = malloc(len);
 97      mp_toradix(&p, buf, 10);
 98
 99      printf("%s\n", buf);
100
101      free(buf);
102      break;
103    }
104
105    mp_mulmod(&k, &a, &m, &k);
106    mp_add_d(&p, 1, &p);
107  }
108
109  if(mp_cmp(&p, &m) >= 0) 
110    printf("No annihilating power.\n");
111
112  mp_clear(&p);
113  mp_clear(&m);
114  mp_clear(&a);
115  return 0;
116}
117
118void sig_catch(int ign)
119{
120  g_quit = 1;
121}