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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * metime.c
michael@0 3 *
michael@0 4 * Modular exponentiation timing test
michael@0 5 *
michael@0 6 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 7 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 9
michael@0 10 #include <stdio.h>
michael@0 11 #include <stdlib.h>
michael@0 12 #include <string.h>
michael@0 13 #include <limits.h>
michael@0 14 #include <time.h>
michael@0 15
michael@0 16 #include "mpi.h"
michael@0 17 #include "mpprime.h"
michael@0 18
michael@0 19 double clk_to_sec(clock_t start, clock_t stop);
michael@0 20
michael@0 21 int main(int argc, char *argv[])
michael@0 22 {
michael@0 23 int ix, num, prec = 8;
michael@0 24 unsigned int seed;
michael@0 25 clock_t start, stop;
michael@0 26 double sec;
michael@0 27
michael@0 28 mp_int a, m, c;
michael@0 29
michael@0 30 if(getenv("SEED") != NULL)
michael@0 31 seed = abs(atoi(getenv("SEED")));
michael@0 32 else
michael@0 33 seed = (unsigned int)time(NULL);
michael@0 34
michael@0 35 if(argc < 2) {
michael@0 36 fprintf(stderr, "Usage: %s <num-tests> [<nbits>]\n", argv[0]);
michael@0 37 return 1;
michael@0 38 }
michael@0 39
michael@0 40 if((num = atoi(argv[1])) < 0)
michael@0 41 num = -num;
michael@0 42
michael@0 43 if(!num) {
michael@0 44 fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
michael@0 45 return 1;
michael@0 46 }
michael@0 47
michael@0 48 if(argc > 2) {
michael@0 49 if((prec = atoi(argv[2])) <= 0)
michael@0 50 prec = 8;
michael@0 51 else
michael@0 52 prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
michael@0 53
michael@0 54 }
michael@0 55
michael@0 56 printf("Modular exponentiation timing test\n"
michael@0 57 "Precision: %d digits (%d bits)\n"
michael@0 58 "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);
michael@0 59
michael@0 60 mp_init_size(&a, prec);
michael@0 61 mp_init_size(&m, prec);
michael@0 62 mp_init_size(&c, prec);
michael@0 63
michael@0 64 srand(seed);
michael@0 65
michael@0 66 start = clock();
michael@0 67 for(ix = 0; ix < num; ix++) {
michael@0 68
michael@0 69 mpp_random_size(&a, prec);
michael@0 70 mpp_random_size(&c, prec);
michael@0 71 mpp_random_size(&m, prec);
michael@0 72 /* set msb and lsb of m */
michael@0 73 DIGIT(&m,0) |= 1;
michael@0 74 DIGIT(&m, USED(&m)-1) |= (mp_digit)1 << (DIGIT_BIT - 1);
michael@0 75 if (mp_cmp(&a, &m) > 0)
michael@0 76 mp_sub(&a, &m, &a);
michael@0 77
michael@0 78 mp_exptmod(&a, &c, &m, &c);
michael@0 79 }
michael@0 80 stop = clock();
michael@0 81
michael@0 82 sec = clk_to_sec(start, stop);
michael@0 83
michael@0 84 printf("Total: %.3f seconds\n", sec);
michael@0 85 printf("Individual: %.3f seconds\n", sec / num);
michael@0 86
michael@0 87 mp_clear(&c);
michael@0 88 mp_clear(&a);
michael@0 89 mp_clear(&m);
michael@0 90
michael@0 91 return 0;
michael@0 92 }
michael@0 93
michael@0 94 double clk_to_sec(clock_t start, clock_t stop)
michael@0 95 {
michael@0 96 return (double)(stop - start) / CLOCKS_PER_SEC;
michael@0 97 }
michael@0 98
michael@0 99 /*------------------------------------------------------------------------*/
michael@0 100 /* HERE THERE BE DRAGONS */

mercurial