michael@0: /* michael@0: * metime.c michael@0: * michael@0: * Modular exponentiation timing 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 "mpi.h" michael@0: #include "mpprime.h" michael@0: michael@0: double clk_to_sec(clock_t start, clock_t stop); michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: int ix, num, prec = 8; michael@0: unsigned int seed; michael@0: clock_t start, stop; michael@0: double sec; michael@0: michael@0: mp_int a, m, c; michael@0: michael@0: if(getenv("SEED") != NULL) michael@0: seed = abs(atoi(getenv("SEED"))); michael@0: else michael@0: seed = (unsigned int)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: else michael@0: prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT; michael@0: michael@0: } michael@0: michael@0: printf("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: michael@0: srand(seed); michael@0: michael@0: start = clock(); michael@0: for(ix = 0; ix < num; ix++) { michael@0: michael@0: mpp_random_size(&a, prec); michael@0: mpp_random_size(&c, prec); michael@0: mpp_random_size(&m, prec); michael@0: /* set msb and lsb of m */ michael@0: DIGIT(&m,0) |= 1; michael@0: DIGIT(&m, USED(&m)-1) |= (mp_digit)1 << (DIGIT_BIT - 1); michael@0: if (mp_cmp(&a, &m) > 0) michael@0: mp_sub(&a, &m, &a); michael@0: michael@0: mp_exptmod(&a, &c, &m, &c); michael@0: } michael@0: stop = clock(); michael@0: michael@0: sec = clk_to_sec(start, stop); michael@0: michael@0: printf("Total: %.3f seconds\n", sec); michael@0: printf("Individual: %.3f seconds\n", sec / num); 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: } michael@0: michael@0: double clk_to_sec(clock_t start, clock_t stop) michael@0: { michael@0: return (double)(stop - start) / CLOCKS_PER_SEC; michael@0: } michael@0: michael@0: /*------------------------------------------------------------------------*/ michael@0: /* HERE THERE BE DRAGONS */