security/nss/lib/freebl/mpi/tests/mptest-3a.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-3a.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + * Simple test driver for MPI library
     1.6 + *
     1.7 + * Test 3a: Multiplication vs. squaring timing test
     1.8 + *
     1.9 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <stdlib.h>
    1.15 +#include <string.h>
    1.16 +#include <ctype.h>
    1.17 +#include <limits.h>
    1.18 +
    1.19 +#include <time.h>
    1.20 +
    1.21 +#include "mpi.h"
    1.22 +#include "mpprime.h"
    1.23 +
    1.24 +int main(int argc, char *argv[])
    1.25 +{
    1.26 +  int        ix, num, prec = 8;
    1.27 +  double     d1, d2;
    1.28 +  clock_t    start, finish;
    1.29 +  time_t     seed;
    1.30 +  mp_int     a, c, d;
    1.31 +
    1.32 +  seed = time(NULL);
    1.33 +
    1.34 +  if(argc < 2) {
    1.35 +    fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]);
    1.36 +    return 1;
    1.37 +  }
    1.38 +
    1.39 +  if((num = atoi(argv[1])) < 0)
    1.40 +    num = -num;
    1.41 +
    1.42 +  if(!num) {
    1.43 +    fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
    1.44 +    return 1;
    1.45 +  }
    1.46 +
    1.47 +  if(argc > 2) {
    1.48 +    if((prec = atoi(argv[2])) <= 0)
    1.49 +      prec = 8;
    1.50 +    else
    1.51 +      prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
    1.52 +  }
    1.53 +  
    1.54 +  printf("Test 3a: Multiplication vs squaring timing test\n"
    1.55 +	 "Precision:  %d digits (%u bits)\n"
    1.56 +	 "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);
    1.57 +
    1.58 +  mp_init_size(&a, prec);
    1.59 +
    1.60 +  mp_init(&c); mp_init(&d);
    1.61 +
    1.62 +  printf("Verifying accuracy ... \n");
    1.63 +  srand((unsigned int)seed);
    1.64 +  for(ix = 0; ix < num; ix++) {
    1.65 +    mpp_random_size(&a, prec);
    1.66 +    mp_mul(&a, &a, &c);
    1.67 +    mp_sqr(&a, &d);
    1.68 +
    1.69 +    if(mp_cmp(&c, &d) != 0) {
    1.70 +      printf("Error!  Results not accurate:\n");
    1.71 +      printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    1.72 +      printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.73 +      printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
    1.74 +      mp_sub(&c, &d, &d);
    1.75 +      printf("dif "); mp_print(&d, stdout); fputc('\n', stdout);
    1.76 +      mp_clear(&c); mp_clear(&d);
    1.77 +      mp_clear(&a);
    1.78 +      return 1;
    1.79 +    }
    1.80 +  }
    1.81 +  printf("Accuracy is confirmed for the %d test samples\n", num);
    1.82 +  mp_clear(&d);
    1.83 +
    1.84 +  printf("Testing squaring ... \n");
    1.85 +  srand((unsigned int)seed);
    1.86 +  start = clock();
    1.87 +  for(ix = 0; ix < num; ix++) {
    1.88 +    mpp_random_size(&a, prec);
    1.89 +    mp_sqr(&a, &c);
    1.90 +  }
    1.91 +  finish = clock();
    1.92 +
    1.93 +  d2 = (double)(finish - start) / CLOCKS_PER_SEC;
    1.94 +
    1.95 +  printf("Testing multiplication ... \n");
    1.96 +  srand((unsigned int)seed);
    1.97 +  start = clock();
    1.98 +  for(ix = 0; ix < num; ix++) {
    1.99 +    mpp_random(&a);
   1.100 +    mp_mul(&a, &a, &c);
   1.101 +  }
   1.102 +  finish = clock();
   1.103 +
   1.104 +  d1 = (double)(finish - start) / CLOCKS_PER_SEC;
   1.105 +
   1.106 +  printf("Multiplication time: %.3f sec (%.3f each)\n", d1, d1 / num);
   1.107 +  printf("Squaring time:       %.3f sec (%.3f each)\n", d2, d2 / num);
   1.108 +  printf("Improvement:         %.2f%%\n", (1.0 - (d2 / d1)) * 100.0);
   1.109 +
   1.110 +  mp_clear(&c);
   1.111 +  mp_clear(&a);
   1.112 +
   1.113 +  return 0;
   1.114 +}

mercurial