security/nss/lib/freebl/mpi/tests/mptest-4b.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-4b.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/*
     1.5 + * mptest-4b.c
     1.6 + *
     1.7 + * Test speed of a large modular exponentiation of a primitive element
     1.8 + * modulo a prime.
     1.9 + *
    1.10 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.11 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.12 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.13 +
    1.14 +#include <stdio.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <string.h>
    1.17 +#include <limits.h>
    1.18 +#include <time.h>
    1.19 +
    1.20 +#include <sys/time.h>
    1.21 +
    1.22 +#include "mpi.h"
    1.23 +#include "mpprime.h"
    1.24 +
    1.25 +char *g_prime = 
    1.26 +  "34BD53C07350E817CCD49721020F1754527959C421C1533244769D4CF060A8B1C3DA"
    1.27 +  "25094BE723FB1E2369B55FEEBBE0FAC16425161BF82684062B5EC5D7D47D1B23C117"
    1.28 +  "0FA19745E44A55E148314E582EB813AC9EE5126295E2E380CACC2F6D206B293E5ED9"
    1.29 +  "23B54EE961A8C69CD625CE4EC38B70C649D7F014432AEF3A1C93";
    1.30 +char *g_gen = "5";
    1.31 +
    1.32 +typedef struct {
    1.33 +  unsigned int  sec;
    1.34 +  unsigned int  usec;
    1.35 +} instant_t;
    1.36 +
    1.37 +instant_t now(void)
    1.38 +{
    1.39 +  struct timeval clk;
    1.40 +  instant_t      res;
    1.41 +
    1.42 +  res.sec = res.usec = 0;
    1.43 +
    1.44 +  if(gettimeofday(&clk, NULL) != 0)
    1.45 +    return res;
    1.46 +
    1.47 +  res.sec = clk.tv_sec;
    1.48 +  res.usec = clk.tv_usec;
    1.49 +
    1.50 +  return res;
    1.51 +}
    1.52 +
    1.53 +extern mp_err s_mp_pad();
    1.54 +
    1.55 +int main(int argc, char *argv[])
    1.56 +{
    1.57 +  instant_t    start, finish;
    1.58 +  mp_int       prime, gen, expt, res;
    1.59 +  unsigned int ix, diff;
    1.60 +  int          num;
    1.61 +
    1.62 +  srand(time(NULL));
    1.63 +
    1.64 +  if(argc < 2) {
    1.65 +    fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
    1.66 +    return 1;
    1.67 +  }
    1.68 +
    1.69 +  if((num = atoi(argv[1])) < 0)
    1.70 +    num = -num;
    1.71 +
    1.72 +  if(num == 0)
    1.73 +    ++num;
    1.74 +
    1.75 +  mp_init(&prime); mp_init(&gen); mp_init(&res);
    1.76 +  mp_read_radix(&prime, g_prime, 16);
    1.77 +  mp_read_radix(&gen, g_gen, 16);
    1.78 +
    1.79 +  mp_init_size(&expt, USED(&prime) - 1);
    1.80 +  s_mp_pad(&expt, USED(&prime) - 1);
    1.81 +
    1.82 +  printf("Testing %d modular exponentations ... \n", num);
    1.83 +
    1.84 +  start = now();
    1.85 +  for(ix = 0; ix < num; ix++) {
    1.86 +    mpp_random(&expt);
    1.87 +    mp_exptmod(&gen, &expt, &prime, &res);
    1.88 +  }
    1.89 +  finish = now();
    1.90 +
    1.91 +  diff = (finish.sec - start.sec) * 1000000;
    1.92 +  diff += finish.usec; diff -= start.usec;
    1.93 +
    1.94 +  printf("%d operations took %u usec (%.3f sec)\n",
    1.95 +	 num, diff, (double)diff / 1000000.0);
    1.96 +  printf("That is %.3f sec per operation.\n",
    1.97 +	 ((double)diff / 1000000.0) / num);
    1.98 +
    1.99 +  mp_clear(&expt);
   1.100 +  mp_clear(&res);
   1.101 +  mp_clear(&gen);
   1.102 +  mp_clear(&prime);
   1.103 +
   1.104 +  return 0;
   1.105 +}

mercurial