Back to index

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