security/nss/lib/freebl/mpi/tests/mptest-5a.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 5a: Greatest common divisor speed test, binary vs. Euclid
     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>
    15 #include <time.h>
    17 #include <sys/time.h>
    19 #include "mpi.h"
    20 #include "mpprime.h"
    22 typedef struct {
    23   unsigned int  sec;
    24   unsigned int  usec;
    25 } instant_t;
    27 instant_t now(void)
    28 {
    29   struct timeval clk;
    30   instant_t      res;
    32   res.sec = res.usec = 0;
    34   if(gettimeofday(&clk, NULL) != 0)
    35     return res;
    37   res.sec = clk.tv_sec;
    38   res.usec = clk.tv_usec;
    40   return res;
    41 }
    43 #define PRECISION 16
    45 int main(int argc, char *argv[])
    46 {
    47   int          ix, num, prec = PRECISION;
    48   mp_int       a, b, c, d;
    49   instant_t    start, finish;
    50   time_t       seed;
    51   unsigned int d1, d2;
    53   seed = time(NULL);
    55   if(argc < 2) {
    56     fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
    57     return 1;
    58   }
    60   if((num = atoi(argv[1])) < 0)
    61     num = -num;
    63   printf("Test 5a: Euclid vs. Binary, a GCD speed test\n\n"
    64 	 "Number of tests: %d\n"
    65 	 "Precision:       %d digits\n\n", num, prec);
    67   mp_init_size(&a, prec);
    68   mp_init_size(&b, prec);
    69   mp_init(&c);
    70   mp_init(&d);
    72   printf("Verifying accuracy ... \n");
    73   srand((unsigned int)seed);
    74   for(ix = 0; ix < num; ix++) {
    75     mpp_random_size(&a, prec);
    76     mpp_random_size(&b, prec);
    78     mp_gcd(&a, &b, &c);
    79     mp_bgcd(&a, &b, &d);
    81     if(mp_cmp(&c, &d) != 0) {
    82       printf("Error!  Results not accurate:\n");
    83       printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    84       printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    85       printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    86       printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
    88       mp_clear(&a); mp_clear(&b); mp_clear(&c); mp_clear(&d);
    89       return 1;
    90     }
    91   }
    92   mp_clear(&d);
    93   printf("Accuracy confirmed for the %d test samples\n", num);
    95   printf("Testing Euclid ... \n");
    96   srand((unsigned int)seed);
    97   start = now();
    98   for(ix = 0; ix < num; ix++) {
    99     mpp_random_size(&a, prec);
   100     mpp_random_size(&b, prec);
   101     mp_gcd(&a, &b, &c);
   103   }
   104   finish = now();
   106   d1 = (finish.sec - start.sec) * 1000000;
   107   d1 -= start.usec; d1 += finish.usec;
   109   printf("Testing binary ... \n");
   110   srand((unsigned int)seed);
   111   start = now();
   112   for(ix = 0; ix < num; ix++) {
   113     mpp_random_size(&a, prec);
   114     mpp_random_size(&b, prec);
   115     mp_bgcd(&a, &b, &c);
   116   }
   117   finish = now();
   119   d2 = (finish.sec - start.sec) * 1000000;
   120   d2 -= start.usec; d2 += finish.usec;
   122   printf("Euclidean algorithm time: %u usec\n", d1);
   123   printf("Binary algorithm time:    %u usec\n", d2);
   124   printf("Improvement:              %.2f%%\n",
   125          (1.0 - ((double)d2 / (double)d1)) * 100.0);
   127   mp_clear(&c);
   128   mp_clear(&b);
   129   mp_clear(&a);
   131   return 0;
   132 }

mercurial