security/nss/lib/freebl/mpi/tests/mptest-3a.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  * Simple test driver for MPI library
     3  *
     4  * Test 3a: Multiplication vs. squaring timing test
     5  *
     6  * This Source Code Form is subject to the terms of the Mozilla Public
     7  * License, v. 2.0. If a copy of the MPL was not distributed with this
     8  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    10 #include <stdio.h>
    11 #include <stdlib.h>
    12 #include <string.h>
    13 #include <ctype.h>
    14 #include <limits.h>
    16 #include <time.h>
    18 #include "mpi.h"
    19 #include "mpprime.h"
    21 int main(int argc, char *argv[])
    22 {
    23   int        ix, num, prec = 8;
    24   double     d1, d2;
    25   clock_t    start, finish;
    26   time_t     seed;
    27   mp_int     a, c, d;
    29   seed = time(NULL);
    31   if(argc < 2) {
    32     fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]);
    33     return 1;
    34   }
    36   if((num = atoi(argv[1])) < 0)
    37     num = -num;
    39   if(!num) {
    40     fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
    41     return 1;
    42   }
    44   if(argc > 2) {
    45     if((prec = atoi(argv[2])) <= 0)
    46       prec = 8;
    47     else
    48       prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
    49   }
    51   printf("Test 3a: Multiplication vs squaring timing test\n"
    52 	 "Precision:  %d digits (%u bits)\n"
    53 	 "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);
    55   mp_init_size(&a, prec);
    57   mp_init(&c); mp_init(&d);
    59   printf("Verifying accuracy ... \n");
    60   srand((unsigned int)seed);
    61   for(ix = 0; ix < num; ix++) {
    62     mpp_random_size(&a, prec);
    63     mp_mul(&a, &a, &c);
    64     mp_sqr(&a, &d);
    66     if(mp_cmp(&c, &d) != 0) {
    67       printf("Error!  Results not accurate:\n");
    68       printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    69       printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    70       printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
    71       mp_sub(&c, &d, &d);
    72       printf("dif "); mp_print(&d, stdout); fputc('\n', stdout);
    73       mp_clear(&c); mp_clear(&d);
    74       mp_clear(&a);
    75       return 1;
    76     }
    77   }
    78   printf("Accuracy is confirmed for the %d test samples\n", num);
    79   mp_clear(&d);
    81   printf("Testing squaring ... \n");
    82   srand((unsigned int)seed);
    83   start = clock();
    84   for(ix = 0; ix < num; ix++) {
    85     mpp_random_size(&a, prec);
    86     mp_sqr(&a, &c);
    87   }
    88   finish = clock();
    90   d2 = (double)(finish - start) / CLOCKS_PER_SEC;
    92   printf("Testing multiplication ... \n");
    93   srand((unsigned int)seed);
    94   start = clock();
    95   for(ix = 0; ix < num; ix++) {
    96     mpp_random(&a);
    97     mp_mul(&a, &a, &c);
    98   }
    99   finish = clock();
   101   d1 = (double)(finish - start) / CLOCKS_PER_SEC;
   103   printf("Multiplication time: %.3f sec (%.3f each)\n", d1, d1 / num);
   104   printf("Squaring time:       %.3f sec (%.3f each)\n", d2, d2 / num);
   105   printf("Improvement:         %.2f%%\n", (1.0 - (d2 / d1)) * 100.0);
   107   mp_clear(&c);
   108   mp_clear(&a);
   110   return 0;
   111 }

mercurial