michael@0: /* michael@0: * mptest4a - modular exponentiation speed test michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include "mpi.h" michael@0: #include "mpprime.h" michael@0: michael@0: typedef struct { michael@0: unsigned int sec; michael@0: unsigned int usec; michael@0: } instant_t; michael@0: michael@0: instant_t now(void) michael@0: { michael@0: struct timeval clk; michael@0: instant_t res; michael@0: michael@0: res.sec = res.usec = 0; michael@0: michael@0: if(gettimeofday(&clk, NULL) != 0) michael@0: return res; michael@0: michael@0: res.sec = clk.tv_sec; michael@0: res.usec = clk.tv_usec; michael@0: michael@0: return res; michael@0: } michael@0: michael@0: extern mp_err s_mp_pad(); michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: int ix, num, prec = 8; michael@0: unsigned int d; michael@0: instant_t start, finish; michael@0: time_t seed; michael@0: mp_int a, m, c; michael@0: michael@0: seed = time(NULL); michael@0: michael@0: if(argc < 2) { michael@0: fprintf(stderr, "Usage: %s []\n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: if((num = atoi(argv[1])) < 0) michael@0: num = -num; michael@0: michael@0: if(!num) { michael@0: fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: if(argc > 2) { michael@0: if((prec = atoi(argv[2])) <= 0) michael@0: prec = 8; michael@0: } michael@0: michael@0: printf("Test 3a: Modular exponentiation timing test\n" michael@0: "Precision: %d digits (%d bits)\n" michael@0: "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num); michael@0: michael@0: mp_init_size(&a, prec); michael@0: mp_init_size(&m, prec); michael@0: mp_init_size(&c, prec); michael@0: s_mp_pad(&a, prec); michael@0: s_mp_pad(&m, prec); michael@0: s_mp_pad(&c, prec); michael@0: michael@0: printf("Testing modular exponentiation ... \n"); michael@0: srand((unsigned int)seed); michael@0: michael@0: start = now(); michael@0: for(ix = 0; ix < num; ix++) { michael@0: mpp_random(&a); michael@0: mpp_random(&c); michael@0: mpp_random(&m); michael@0: mp_exptmod(&a, &c, &m, &c); michael@0: } michael@0: finish = now(); michael@0: michael@0: d = (finish.sec - start.sec) * 1000000; michael@0: d -= start.usec; d += finish.usec; michael@0: michael@0: printf("Total time elapsed: %u usec\n", d); michael@0: printf("Time per exponentiation: %u usec (%.3f sec)\n", michael@0: (d / num), (double)(d / num) / 1000000); michael@0: michael@0: mp_clear(&c); michael@0: mp_clear(&a); michael@0: mp_clear(&m); michael@0: michael@0: return 0; michael@0: }