michael@0: /* michael@0: * mptest-4b.c michael@0: * michael@0: * Test speed of a large modular exponentiation of a primitive element michael@0: * modulo a prime. 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: char *g_prime = michael@0: "34BD53C07350E817CCD49721020F1754527959C421C1533244769D4CF060A8B1C3DA" michael@0: "25094BE723FB1E2369B55FEEBBE0FAC16425161BF82684062B5EC5D7D47D1B23C117" michael@0: "0FA19745E44A55E148314E582EB813AC9EE5126295E2E380CACC2F6D206B293E5ED9" michael@0: "23B54EE961A8C69CD625CE4EC38B70C649D7F014432AEF3A1C93"; michael@0: char *g_gen = "5"; 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: instant_t start, finish; michael@0: mp_int prime, gen, expt, res; michael@0: unsigned int ix, diff; michael@0: int num; michael@0: michael@0: srand(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 == 0) michael@0: ++num; michael@0: michael@0: mp_init(&prime); mp_init(&gen); mp_init(&res); michael@0: mp_read_radix(&prime, g_prime, 16); michael@0: mp_read_radix(&gen, g_gen, 16); michael@0: michael@0: mp_init_size(&expt, USED(&prime) - 1); michael@0: s_mp_pad(&expt, USED(&prime) - 1); michael@0: michael@0: printf("Testing %d modular exponentations ... \n", num); michael@0: michael@0: start = now(); michael@0: for(ix = 0; ix < num; ix++) { michael@0: mpp_random(&expt); michael@0: mp_exptmod(&gen, &expt, &prime, &res); michael@0: } michael@0: finish = now(); michael@0: michael@0: diff = (finish.sec - start.sec) * 1000000; michael@0: diff += finish.usec; diff -= start.usec; michael@0: michael@0: printf("%d operations took %u usec (%.3f sec)\n", michael@0: num, diff, (double)diff / 1000000.0); michael@0: printf("That is %.3f sec per operation.\n", michael@0: ((double)diff / 1000000.0) / num); michael@0: michael@0: mp_clear(&expt); michael@0: mp_clear(&res); michael@0: mp_clear(&gen); michael@0: mp_clear(&prime); michael@0: michael@0: return 0; michael@0: }