Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * mptest-4b.c |
michael@0 | 3 | * |
michael@0 | 4 | * Test speed of a large modular exponentiation of a primitive element |
michael@0 | 5 | * modulo a prime. |
michael@0 | 6 | * |
michael@0 | 7 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 8 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 9 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 10 | |
michael@0 | 11 | #include <stdio.h> |
michael@0 | 12 | #include <stdlib.h> |
michael@0 | 13 | #include <string.h> |
michael@0 | 14 | #include <limits.h> |
michael@0 | 15 | #include <time.h> |
michael@0 | 16 | |
michael@0 | 17 | #include <sys/time.h> |
michael@0 | 18 | |
michael@0 | 19 | #include "mpi.h" |
michael@0 | 20 | #include "mpprime.h" |
michael@0 | 21 | |
michael@0 | 22 | char *g_prime = |
michael@0 | 23 | "34BD53C07350E817CCD49721020F1754527959C421C1533244769D4CF060A8B1C3DA" |
michael@0 | 24 | "25094BE723FB1E2369B55FEEBBE0FAC16425161BF82684062B5EC5D7D47D1B23C117" |
michael@0 | 25 | "0FA19745E44A55E148314E582EB813AC9EE5126295E2E380CACC2F6D206B293E5ED9" |
michael@0 | 26 | "23B54EE961A8C69CD625CE4EC38B70C649D7F014432AEF3A1C93"; |
michael@0 | 27 | char *g_gen = "5"; |
michael@0 | 28 | |
michael@0 | 29 | typedef struct { |
michael@0 | 30 | unsigned int sec; |
michael@0 | 31 | unsigned int usec; |
michael@0 | 32 | } instant_t; |
michael@0 | 33 | |
michael@0 | 34 | instant_t now(void) |
michael@0 | 35 | { |
michael@0 | 36 | struct timeval clk; |
michael@0 | 37 | instant_t res; |
michael@0 | 38 | |
michael@0 | 39 | res.sec = res.usec = 0; |
michael@0 | 40 | |
michael@0 | 41 | if(gettimeofday(&clk, NULL) != 0) |
michael@0 | 42 | return res; |
michael@0 | 43 | |
michael@0 | 44 | res.sec = clk.tv_sec; |
michael@0 | 45 | res.usec = clk.tv_usec; |
michael@0 | 46 | |
michael@0 | 47 | return res; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | extern mp_err s_mp_pad(); |
michael@0 | 51 | |
michael@0 | 52 | int main(int argc, char *argv[]) |
michael@0 | 53 | { |
michael@0 | 54 | instant_t start, finish; |
michael@0 | 55 | mp_int prime, gen, expt, res; |
michael@0 | 56 | unsigned int ix, diff; |
michael@0 | 57 | int num; |
michael@0 | 58 | |
michael@0 | 59 | srand(time(NULL)); |
michael@0 | 60 | |
michael@0 | 61 | if(argc < 2) { |
michael@0 | 62 | fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]); |
michael@0 | 63 | return 1; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | if((num = atoi(argv[1])) < 0) |
michael@0 | 67 | num = -num; |
michael@0 | 68 | |
michael@0 | 69 | if(num == 0) |
michael@0 | 70 | ++num; |
michael@0 | 71 | |
michael@0 | 72 | mp_init(&prime); mp_init(&gen); mp_init(&res); |
michael@0 | 73 | mp_read_radix(&prime, g_prime, 16); |
michael@0 | 74 | mp_read_radix(&gen, g_gen, 16); |
michael@0 | 75 | |
michael@0 | 76 | mp_init_size(&expt, USED(&prime) - 1); |
michael@0 | 77 | s_mp_pad(&expt, USED(&prime) - 1); |
michael@0 | 78 | |
michael@0 | 79 | printf("Testing %d modular exponentations ... \n", num); |
michael@0 | 80 | |
michael@0 | 81 | start = now(); |
michael@0 | 82 | for(ix = 0; ix < num; ix++) { |
michael@0 | 83 | mpp_random(&expt); |
michael@0 | 84 | mp_exptmod(&gen, &expt, &prime, &res); |
michael@0 | 85 | } |
michael@0 | 86 | finish = now(); |
michael@0 | 87 | |
michael@0 | 88 | diff = (finish.sec - start.sec) * 1000000; |
michael@0 | 89 | diff += finish.usec; diff -= start.usec; |
michael@0 | 90 | |
michael@0 | 91 | printf("%d operations took %u usec (%.3f sec)\n", |
michael@0 | 92 | num, diff, (double)diff / 1000000.0); |
michael@0 | 93 | printf("That is %.3f sec per operation.\n", |
michael@0 | 94 | ((double)diff / 1000000.0) / num); |
michael@0 | 95 | |
michael@0 | 96 | mp_clear(&expt); |
michael@0 | 97 | mp_clear(&res); |
michael@0 | 98 | mp_clear(&gen); |
michael@0 | 99 | mp_clear(&prime); |
michael@0 | 100 | |
michael@0 | 101 | return 0; |
michael@0 | 102 | } |