Back to index

lightning-sunbird  0.9+nobinonly
exptmod.c
Go to the documentation of this file.
00001 /*
00002  *  exptmod.c
00003  *
00004  * Command line tool to perform modular exponentiation on arbitrary
00005  * precision integers.
00006  *
00007  * ***** BEGIN LICENSE BLOCK *****
00008  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00009  *
00010  * The contents of this file are subject to the Mozilla Public License Version
00011  * 1.1 (the "License"); you may not use this file except in compliance with
00012  * the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS" basis,
00016  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00017  * for the specific language governing rights and limitations under the
00018  * License.
00019  *
00020  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00021  *
00022  * The Initial Developer of the Original Code is
00023  * Michael J. Fromberger.
00024  * Portions created by the Initial Developer are Copyright (C) 1998
00025  * the Initial Developer. All Rights Reserved.
00026  *
00027  * Contributor(s):
00028  *
00029  * Alternatively, the contents of this file may be used under the terms of
00030  * either the GNU General Public License Version 2 or later (the "GPL"), or
00031  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00032  * in which case the provisions of the GPL or the LGPL are applicable instead
00033  * of those above. If you wish to allow use of your version of this file only
00034  * under the terms of either the GPL or the LGPL, and not to allow others to
00035  * use your version of this file under the terms of the MPL, indicate your
00036  * decision by deleting the provisions above and replace them with the notice
00037  * and other provisions required by the GPL or the LGPL. If you do not delete
00038  * the provisions above, a recipient may use your version of this file under
00039  * the terms of any one of the MPL, the GPL or the LGPL.
00040  *
00041  * ***** END LICENSE BLOCK ***** */
00042 /* $Id: exptmod.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 
00048 #include "mpi.h"
00049 
00050 int main(int argc, char *argv[])
00051 {
00052   mp_int  a, b, m;
00053   mp_err  res;
00054   char   *str;
00055   int     len, rval = 0;
00056 
00057   if(argc < 3) {
00058     fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
00059     return 1;
00060   }
00061 
00062   mp_init(&a); mp_init(&b); mp_init(&m);
00063   mp_read_radix(&a, argv[1], 10);
00064   mp_read_radix(&b, argv[2], 10);
00065   mp_read_radix(&m, argv[3], 10);
00066 
00067   if((res = mp_exptmod(&a, &b, &m, &a)) != MP_OKAY) {
00068     fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
00069     rval = 1;
00070   } else {
00071     len = mp_radix_size(&a, 10);
00072     str = calloc(len, sizeof(char));
00073     mp_toradix(&a, str, 10);
00074 
00075     printf("%s\n", str);
00076 
00077     free(str);
00078   }
00079 
00080   mp_clear(&a); mp_clear(&b); mp_clear(&m);
00081 
00082   return rval;
00083 }