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