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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:3c50feb2a3e7
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/. */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <limits.h>
15 #include <time.h>
16
17 #include <sys/time.h>
18
19 #include "mpi.h"
20 #include "mpprime.h"
21
22 typedef struct {
23 unsigned int sec;
24 unsigned int usec;
25 } instant_t;
26
27 instant_t now(void)
28 {
29 struct timeval clk;
30 instant_t res;
31
32 res.sec = res.usec = 0;
33
34 if(gettimeofday(&clk, NULL) != 0)
35 return res;
36
37 res.sec = clk.tv_sec;
38 res.usec = clk.tv_usec;
39
40 return res;
41 }
42
43 #define PRECISION 16
44
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;
52
53 seed = time(NULL);
54
55 if(argc < 2) {
56 fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
57 return 1;
58 }
59
60 if((num = atoi(argv[1])) < 0)
61 num = -num;
62
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);
66
67 mp_init_size(&a, prec);
68 mp_init_size(&b, prec);
69 mp_init(&c);
70 mp_init(&d);
71
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);
77
78 mp_gcd(&a, &b, &c);
79 mp_bgcd(&a, &b, &d);
80
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);
87
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);
94
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);
102
103 }
104 finish = now();
105
106 d1 = (finish.sec - start.sec) * 1000000;
107 d1 -= start.usec; d1 += finish.usec;
108
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();
118
119 d2 = (finish.sec - start.sec) * 1000000;
120 d2 -= start.usec; d2 += finish.usec;
121
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);
126
127 mp_clear(&c);
128 mp_clear(&b);
129 mp_clear(&a);
130
131 return 0;
132 }

mercurial