security/nss/lib/freebl/mpi/tests/mptest-4a.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 * mptest4a - modular exponentiation speed test
michael@0 3 *
michael@0 4 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include <stdio.h>
michael@0 9 #include <stdlib.h>
michael@0 10 #include <string.h>
michael@0 11 #include <limits.h>
michael@0 12 #include <time.h>
michael@0 13
michael@0 14 #include <sys/time.h>
michael@0 15
michael@0 16 #include "mpi.h"
michael@0 17 #include "mpprime.h"
michael@0 18
michael@0 19 typedef struct {
michael@0 20 unsigned int sec;
michael@0 21 unsigned int usec;
michael@0 22 } instant_t;
michael@0 23
michael@0 24 instant_t now(void)
michael@0 25 {
michael@0 26 struct timeval clk;
michael@0 27 instant_t res;
michael@0 28
michael@0 29 res.sec = res.usec = 0;
michael@0 30
michael@0 31 if(gettimeofday(&clk, NULL) != 0)
michael@0 32 return res;
michael@0 33
michael@0 34 res.sec = clk.tv_sec;
michael@0 35 res.usec = clk.tv_usec;
michael@0 36
michael@0 37 return res;
michael@0 38 }
michael@0 39
michael@0 40 extern mp_err s_mp_pad();
michael@0 41
michael@0 42 int main(int argc, char *argv[])
michael@0 43 {
michael@0 44 int ix, num, prec = 8;
michael@0 45 unsigned int d;
michael@0 46 instant_t start, finish;
michael@0 47 time_t seed;
michael@0 48 mp_int a, m, c;
michael@0 49
michael@0 50 seed = time(NULL);
michael@0 51
michael@0 52 if(argc < 2) {
michael@0 53 fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]);
michael@0 54 return 1;
michael@0 55 }
michael@0 56
michael@0 57 if((num = atoi(argv[1])) < 0)
michael@0 58 num = -num;
michael@0 59
michael@0 60 if(!num) {
michael@0 61 fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
michael@0 62 return 1;
michael@0 63 }
michael@0 64
michael@0 65 if(argc > 2) {
michael@0 66 if((prec = atoi(argv[2])) <= 0)
michael@0 67 prec = 8;
michael@0 68 }
michael@0 69
michael@0 70 printf("Test 3a: Modular exponentiation timing test\n"
michael@0 71 "Precision: %d digits (%d bits)\n"
michael@0 72 "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);
michael@0 73
michael@0 74 mp_init_size(&a, prec);
michael@0 75 mp_init_size(&m, prec);
michael@0 76 mp_init_size(&c, prec);
michael@0 77 s_mp_pad(&a, prec);
michael@0 78 s_mp_pad(&m, prec);
michael@0 79 s_mp_pad(&c, prec);
michael@0 80
michael@0 81 printf("Testing modular exponentiation ... \n");
michael@0 82 srand((unsigned int)seed);
michael@0 83
michael@0 84 start = now();
michael@0 85 for(ix = 0; ix < num; ix++) {
michael@0 86 mpp_random(&a);
michael@0 87 mpp_random(&c);
michael@0 88 mpp_random(&m);
michael@0 89 mp_exptmod(&a, &c, &m, &c);
michael@0 90 }
michael@0 91 finish = now();
michael@0 92
michael@0 93 d = (finish.sec - start.sec) * 1000000;
michael@0 94 d -= start.usec; d += finish.usec;
michael@0 95
michael@0 96 printf("Total time elapsed: %u usec\n", d);
michael@0 97 printf("Time per exponentiation: %u usec (%.3f sec)\n",
michael@0 98 (d / num), (double)(d / num) / 1000000);
michael@0 99
michael@0 100 mp_clear(&c);
michael@0 101 mp_clear(&a);
michael@0 102 mp_clear(&m);
michael@0 103
michael@0 104 return 0;
michael@0 105 }

mercurial