3 Purpose: Timing tests for the imath library.
4 Author: M. J. Fromberger
6 Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 double clocks_to_seconds(clock_t start
, clock_t end
);
39 double get_multiply_time(int nt
, int prec
);
40 double get_exptmod_time(int nt
, int prec
);
41 mp_int
alloc_values(int nt
, int prec
);
42 void randomize_values(mp_int values
, int nt
, int prec
);
43 void release_values(mp_int values
, int nt
);
44 void mp_int_random(mp_int z
, int prec
);
46 const int g_mul_factor
= 1000;
48 int main(int argc
, char *argv
[]) {
49 int do_mul
= 0, do_exp
= 0, do_header
= 1;
50 int num_tests
, precision
= 0, opt
;
51 mp_size threshold
= 0;
52 unsigned int seed
= (unsigned int)time(NULL
);
54 while ((opt
= getopt(argc
, argv
, "ehmnp:s:t:")) != EOF
) {
66 precision
= atoi(optarg
);
72 threshold
= (mp_size
)atoi(optarg
);
76 "Usage: imtimer [options] <num-tests>\n\n"
77 "Options understood:\n"
78 " -e -- test modular exponentiation speed.\n"
79 " -h -- display this help message.\n"
80 " -m -- test multiplication speed.\n"
81 " -n -- no header line.\n"
82 " -p <dig> -- use values with <dig> digits.\n"
83 " -s <rnd> -- set random seed to <rnd>.\n"
84 " -t <dig> -- set recursion threshold to <dig> digits.\n\n");
91 "Usage: imtimer [options] <num-tests>\n"
92 "[use \"imtimer -h\" for help with options]\n\n");
95 num_tests
= atoi(argv
[optind
]);
100 fprintf(stderr
, "You must request at least one test.\n");
104 fprintf(stderr
, "Precision must be non-negative (0 means default).\n");
107 mp_int_multiply_threshold(threshold
);
109 if (do_header
) printf("NUM\tPREC\tBITS\tREC\tRESULT\n");
110 printf("%d\t%d\t%d\t%u", num_tests
, precision
,
111 (int)(precision
* MP_DIGIT_BIT
), threshold
);
114 double m_time
= get_multiply_time(num_tests
, precision
);
116 printf("\tMUL %.3f %.3f", m_time
, m_time
/ num_tests
);
120 double e_time
= get_exptmod_time(num_tests
, precision
);
122 printf("\tEXP %.3f %.3f", e_time
, e_time
/ num_tests
);
130 double clocks_to_seconds(clock_t start
, clock_t end
) {
131 return (double)(end
- start
) / CLOCKS_PER_SEC
;
134 mp_int
alloc_values(int nt
, int prec
) {
135 mp_int out
= malloc(nt
* sizeof(mpz_t
));
138 if (out
== NULL
) return NULL
;
140 for (i
= 0; i
< nt
; ++i
) {
141 if (mp_int_init_size(out
+ i
, prec
) != MP_OK
) {
142 while (--i
>= 0) mp_int_clear(out
+ i
);
150 void randomize_values(mp_int values
, int nt
, int prec
) {
153 for (i
= 0; i
< nt
; ++i
) mp_int_random(values
+ i
, prec
);
156 void release_values(mp_int values
, int nt
) {
159 for (i
= 0; i
< nt
; ++i
) mp_int_clear(values
+ i
);
164 double get_multiply_time(int nt
, int prec
) {
169 if ((values
= alloc_values(3, prec
)) == NULL
) return 0.0;
170 randomize_values(values
, 2, prec
);
173 for (i
= 0; i
< nt
; ++i
) mp_int_mul(values
, values
+ 1, values
+ 2);
176 release_values(values
, 3);
178 return clocks_to_seconds(start
, end
);
181 double get_exptmod_time(int nt
, int prec
) {
186 if ((values
= alloc_values(4, prec
)) == NULL
) return 0.0;
187 randomize_values(values
, 3, prec
);
190 for (i
= 0; i
< nt
; ++i
)
191 mp_int_exptmod(values
, values
+ 1, values
+ 2, values
+ 3);
194 release_values(values
, 4);
196 return clocks_to_seconds(start
, end
);
199 void mp_int_random(mp_int z
, int prec
) {
202 if (prec
> (int)MP_ALLOC(z
)) prec
= (int)MP_ALLOC(z
);
204 for (i
= 0; i
< prec
; ++i
) {
208 for (j
= 0; j
< (int)sizeof(d
); ++j
) {
209 d
= (d
<< CHAR_BIT
) | (rand() & UCHAR_MAX
);