Back to index

lightning-sunbird  0.9+nobinonly
mptest-4b.c
Go to the documentation of this file.
00001 /*
00002  * mptest-4b.c
00003  *
00004  * Test speed of a large modular exponentiation of a primitive element
00005  * modulo a prime.
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: mptest-4b.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 #include <limits.h>
00048 #include <time.h>
00049 
00050 #include <sys/time.h>
00051 
00052 #include "mpi.h"
00053 #include "mpprime.h"
00054 
00055 char *g_prime = 
00056   "34BD53C07350E817CCD49721020F1754527959C421C1533244769D4CF060A8B1C3DA"
00057   "25094BE723FB1E2369B55FEEBBE0FAC16425161BF82684062B5EC5D7D47D1B23C117"
00058   "0FA19745E44A55E148314E582EB813AC9EE5126295E2E380CACC2F6D206B293E5ED9"
00059   "23B54EE961A8C69CD625CE4EC38B70C649D7F014432AEF3A1C93";
00060 char *g_gen = "5";
00061 
00062 typedef struct {
00063   unsigned int  sec;
00064   unsigned int  usec;
00065 } instant_t;
00066 
00067 instant_t now(void)
00068 {
00069   struct timeval clk;
00070   instant_t      res;
00071 
00072   res.sec = res.usec = 0;
00073 
00074   if(gettimeofday(&clk, NULL) != 0)
00075     return res;
00076 
00077   res.sec = clk.tv_sec;
00078   res.usec = clk.tv_usec;
00079 
00080   return res;
00081 }
00082 
00083 extern mp_err s_mp_pad();
00084 
00085 int main(int argc, char *argv[])
00086 {
00087   instant_t    start, finish;
00088   mp_int       prime, gen, expt, res;
00089   unsigned int ix, diff;
00090   int          num;
00091 
00092   srand(time(NULL));
00093 
00094   if(argc < 2) {
00095     fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
00096     return 1;
00097   }
00098 
00099   if((num = atoi(argv[1])) < 0)
00100     num = -num;
00101 
00102   if(num == 0)
00103     ++num;
00104 
00105   mp_init(&prime); mp_init(&gen); mp_init(&res);
00106   mp_read_radix(&prime, g_prime, 16);
00107   mp_read_radix(&gen, g_gen, 16);
00108 
00109   mp_init_size(&expt, USED(&prime) - 1);
00110   s_mp_pad(&expt, USED(&prime) - 1);
00111 
00112   printf("Testing %d modular exponentations ... \n", num);
00113 
00114   start = now();
00115   for(ix = 0; ix < num; ix++) {
00116     mpp_random(&expt);
00117     mp_exptmod(&gen, &expt, &prime, &res);
00118   }
00119   finish = now();
00120 
00121   diff = (finish.sec - start.sec) * 1000000;
00122   diff += finish.usec; diff -= start.usec;
00123 
00124   printf("%d operations took %u usec (%.3f sec)\n",
00125         num, diff, (double)diff / 1000000.0);
00126   printf("That is %.3f sec per operation.\n",
00127         ((double)diff / 1000000.0) / num);
00128 
00129   mp_clear(&expt);
00130   mp_clear(&res);
00131   mp_clear(&gen);
00132   mp_clear(&prime);
00133 
00134   return 0;
00135 }