comparison libtommath/mtest/mtest.c @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children 8bba51a55704
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
37 #include <stdio.h> 37 #include <stdio.h>
38 #include <stdlib.h> 38 #include <stdlib.h>
39 #include <time.h> 39 #include <time.h>
40 #include "mpi.c" 40 #include "mpi.c"
41 41
42 #ifdef LTM_MTEST_REAL_RAND
43 #define getRandChar() fgetc(rng)
42 FILE *rng; 44 FILE *rng;
45 #else
46 #define getRandChar() (rand()&0xFF)
47 #endif
43 48
44 void rand_num(mp_int *a) 49 void rand_num(mp_int *a)
45 { 50 {
46 int n, size; 51 int size;
47 unsigned char buf[2048]; 52 unsigned char buf[2048];
48 53 size_t sz;
49 size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101; 54
50 buf[0] = (fgetc(rng)&1)?1:0; 55 size = 1 + ((getRandChar()<<8) + getRandChar()) % 101;
51 fread(buf+1, 1, size, rng); 56 buf[0] = (getRandChar()&1)?1:0;
52 while (buf[1] == 0) buf[1] = fgetc(rng); 57 #ifdef LTM_MTEST_REAL_RAND
58 sz = fread(buf+1, 1, size, rng);
59 #else
60 sz = 1;
61 while (sz < (unsigned)size) {
62 buf[sz] = getRandChar();
63 ++sz;
64 }
65 #endif
66 if (sz != (unsigned)size) {
67 fprintf(stderr, "\nWarning: fread failed\n\n");
68 }
69 while (buf[1] == 0) buf[1] = getRandChar();
53 mp_read_raw(a, buf, 1+size); 70 mp_read_raw(a, buf, 1+size);
54 } 71 }
55 72
56 void rand_num2(mp_int *a) 73 void rand_num2(mp_int *a)
57 { 74 {
58 int n, size; 75 int size;
59 unsigned char buf[2048]; 76 unsigned char buf[2048];
60 77 size_t sz;
61 size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101; 78
62 buf[0] = (fgetc(rng)&1)?1:0; 79 size = 10 + ((getRandChar()<<8) + getRandChar()) % 101;
63 fread(buf+1, 1, size, rng); 80 buf[0] = (getRandChar()&1)?1:0;
64 while (buf[1] == 0) buf[1] = fgetc(rng); 81 #ifdef LTM_MTEST_REAL_RAND
82 sz = fread(buf+1, 1, size, rng);
83 #else
84 sz = 1;
85 while (sz < (unsigned)size) {
86 buf[sz] = getRandChar();
87 ++sz;
88 }
89 #endif
90 if (sz != (unsigned)size) {
91 fprintf(stderr, "\nWarning: fread failed\n\n");
92 }
93 while (buf[1] == 0) buf[1] = getRandChar();
65 mp_read_raw(a, buf, 1+size); 94 mp_read_raw(a, buf, 1+size);
66 } 95 }
67 96
68 #define mp_to64(a, b) mp_toradix(a, b, 64) 97 #define mp_to64(a, b) mp_toradix(a, b, 64)
69 98
70 int main(void) 99 int main(int argc, char *argv[])
71 { 100 {
72 int n, tmp; 101 int n, tmp;
102 long long max;
73 mp_int a, b, c, d, e; 103 mp_int a, b, c, d, e;
104 #ifdef MTEST_NO_FULLSPEED
74 clock_t t1; 105 clock_t t1;
106 #endif
75 char buf[4096]; 107 char buf[4096];
76 108
77 mp_init(&a); 109 mp_init(&a);
78 mp_init(&b); 110 mp_init(&b);
79 mp_init(&c); 111 mp_init(&c);
80 mp_init(&d); 112 mp_init(&d);
81 mp_init(&e); 113 mp_init(&e);
114
115 if (argc > 1) {
116 max = strtol(argv[1], NULL, 0);
117 if (max < 0) {
118 if (max > -64) {
119 max = (1 << -(max)) + 1;
120 } else {
121 max = 1;
122 }
123 } else if (max == 0) {
124 max = 1;
125 }
126 }
127 else {
128 max = 0;
129 }
82 130
83 131
84 /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */ 132 /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
85 /* 133 /*
86 mp_set(&a, 1); 134 mp_set(&a, 1);
96 mp_mul_2(&a, &a); 144 mp_mul_2(&a, &a);
97 mp_sub_d(&a, 1, &a); 145 mp_sub_d(&a, 1, &a);
98 } 146 }
99 */ 147 */
100 148
149 #ifdef LTM_MTEST_REAL_RAND
101 rng = fopen("/dev/urandom", "rb"); 150 rng = fopen("/dev/urandom", "rb");
102 if (rng == NULL) { 151 if (rng == NULL) {
103 rng = fopen("/dev/random", "rb"); 152 rng = fopen("/dev/random", "rb");
104 if (rng == NULL) { 153 if (rng == NULL) {
105 fprintf(stderr, "\nWarning: stdin used as random source\n\n"); 154 fprintf(stderr, "\nWarning: stdin used as random source\n\n");
106 rng = stdin; 155 rng = stdin;
107 } 156 }
108 } 157 }
109 158 #else
159 srand(23);
160 #endif
161
162 #ifdef MTEST_NO_FULLSPEED
110 t1 = clock(); 163 t1 = clock();
164 #endif
111 for (;;) { 165 for (;;) {
112 #if 0 166 #ifdef MTEST_NO_FULLSPEED
113 if (clock() - t1 > CLOCKS_PER_SEC) { 167 if (clock() - t1 > CLOCKS_PER_SEC) {
114 sleep(2); 168 sleep(2);
115 t1 = clock(); 169 t1 = clock();
116 } 170 }
117 #endif 171 #endif
118 n = fgetc(rng) % 15; 172 n = getRandChar() % 15;
173
174 if (max != 0) {
175 --max;
176 if (max == 0)
177 n = 255;
178 }
119 179
120 if (n == 0) { 180 if (n == 0) {
121 /* add tests */ 181 /* add tests */
122 rand_num(&a); 182 rand_num(&a);
123 rand_num(&b); 183 rand_num(&b);
178 printf("%s\n", buf); 238 printf("%s\n", buf);
179 } else if (n == 5) { 239 } else if (n == 5) {
180 /* mul_2d test */ 240 /* mul_2d test */
181 rand_num(&a); 241 rand_num(&a);
182 mp_copy(&a, &b); 242 mp_copy(&a, &b);
183 n = fgetc(rng) & 63; 243 n = getRandChar() & 63;
184 mp_mul_2d(&b, n, &b); 244 mp_mul_2d(&b, n, &b);
185 mp_to64(&a, buf); 245 mp_to64(&a, buf);
186 printf("mul2d\n"); 246 printf("mul2d\n");
187 printf("%s\n", buf); 247 printf("%s\n", buf);
188 printf("%d\n", n); 248 printf("%d\n", n);
190 printf("%s\n", buf); 250 printf("%s\n", buf);
191 } else if (n == 6) { 251 } else if (n == 6) {
192 /* div_2d test */ 252 /* div_2d test */
193 rand_num(&a); 253 rand_num(&a);
194 mp_copy(&a, &b); 254 mp_copy(&a, &b);
195 n = fgetc(rng) & 63; 255 n = getRandChar() & 63;
196 mp_div_2d(&b, n, &b, NULL); 256 mp_div_2d(&b, n, &b, NULL);
197 mp_to64(&a, buf); 257 mp_to64(&a, buf);
198 printf("div2d\n"); 258 printf("div2d\n");
199 printf("%s\n", buf); 259 printf("%s\n", buf);
200 printf("%d\n", n); 260 printf("%d\n", n);
295 printf("sub_d\n"); 355 printf("sub_d\n");
296 mp_to64(&a, buf); 356 mp_to64(&a, buf);
297 printf("%s\n%d\n", buf, tmp); 357 printf("%s\n%d\n", buf, tmp);
298 mp_to64(&b, buf); 358 mp_to64(&b, buf);
299 printf("%s\n", buf); 359 printf("%s\n", buf);
300 } 360 } else if (n == 255) {
301 } 361 printf("exit\n");
362 break;
363 }
364
365 }
366 #ifdef LTM_MTEST_REAL_RAND
302 fclose(rng); 367 fclose(rng);
368 #endif
303 return 0; 369 return 0;
304 } 370 }
305 371
306 /* $Source: /cvs/libtom/libtommath/mtest/mtest.c,v $ */ 372 /* $Source$ */
307 /* $Revision: 1.2 $ */ 373 /* $Revision$ */
308 /* $Date: 2005/05/05 14:38:47 $ */ 374 /* $Date$ */