Back to index

lightning-sunbird  0.9+nobinonly
mptest-4a.c
Go to the documentation of this file.
00001 /* 
00002  *  mptest4a - modular exponentiation speed test 
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Michael J. Fromberger.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 /* $Id: mptest-4a.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00040 
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044 #include <limits.h>
00045 #include <time.h>
00046 
00047 #include <sys/time.h>
00048 
00049 #include "mpi.h"
00050 #include "mpprime.h"
00051 
00052 typedef struct {
00053   unsigned int  sec;
00054   unsigned int  usec;
00055 } instant_t;
00056 
00057 instant_t now(void)
00058 {
00059   struct timeval clk;
00060   instant_t      res;
00061 
00062   res.sec = res.usec = 0;
00063 
00064   if(gettimeofday(&clk, NULL) != 0)
00065     return res;
00066 
00067   res.sec = clk.tv_sec;
00068   res.usec = clk.tv_usec;
00069 
00070   return res;
00071 }
00072 
00073 extern mp_err s_mp_pad();
00074 
00075 int main(int argc, char *argv[])
00076 {
00077   int        ix, num, prec = 8;
00078   unsigned   int d;
00079   instant_t  start, finish;
00080   time_t     seed;
00081   mp_int     a, m, c;
00082 
00083   seed = time(NULL);
00084 
00085   if(argc < 2) {
00086     fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]);
00087     return 1;
00088   }
00089 
00090   if((num = atoi(argv[1])) < 0)
00091     num = -num;
00092 
00093   if(!num) {
00094     fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
00095     return 1;
00096   }
00097 
00098   if(argc > 2) {
00099     if((prec = atoi(argv[2])) <= 0)
00100       prec = 8;
00101   }
00102   
00103   printf("Test 3a: Modular exponentiation timing test\n"
00104         "Precision:  %d digits (%d bits)\n"
00105         "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);
00106 
00107   mp_init_size(&a, prec);
00108   mp_init_size(&m, prec);
00109   mp_init_size(&c, prec);
00110   s_mp_pad(&a, prec);
00111   s_mp_pad(&m, prec);
00112   s_mp_pad(&c, prec);
00113 
00114   printf("Testing modular exponentiation ... \n");
00115   srand((unsigned int)seed);
00116 
00117   start = now();
00118   for(ix = 0; ix < num; ix++) {
00119     mpp_random(&a);
00120     mpp_random(&c);
00121     mpp_random(&m);
00122     mp_exptmod(&a, &c, &m, &c);
00123   }
00124   finish = now();
00125 
00126   d = (finish.sec - start.sec) * 1000000;
00127   d -= start.usec; d += finish.usec;
00128 
00129   printf("Total time elapsed:        %u usec\n", d);
00130   printf("Time per exponentiation:   %u usec (%.3f sec)\n", 
00131         (d / num), (double)(d / num) / 1000000);
00132 
00133   mp_clear(&c);
00134   mp_clear(&a);
00135   mp_clear(&m);
00136 
00137   return 0;
00138 }