Mercurial > dropbear
comparison tomsfastmath/mtest/mtest.c @ 643:a362b62d38b2 dropbear-tfm
Add tomsfastmath from git rev bfa4582842bc3bab42e4be4aed5703437049502a
with Makefile.in renamed
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 23 Nov 2011 18:10:20 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
642:33fd2f3499d2 | 643:a362b62d38b2 |
---|---|
1 /* makes a bignum test harness with NUM tests per operation | |
2 * | |
3 * the output is made in the following format [one parameter per line] | |
4 | |
5 operation | |
6 operand1 | |
7 operand2 | |
8 [... operandN] | |
9 result1 | |
10 result2 | |
11 [... resultN] | |
12 | |
13 So for example "a * b mod n" would be | |
14 | |
15 mulmod | |
16 a | |
17 b | |
18 n | |
19 a*b mod n | |
20 | |
21 e.g. if a=3, b=4 n=11 then | |
22 | |
23 mulmod | |
24 3 | |
25 4 | |
26 11 | |
27 1 | |
28 | |
29 */ | |
30 | |
31 #ifdef MP_8BIT | |
32 #define THE_MASK 127 | |
33 #else | |
34 #define THE_MASK 32767 | |
35 #endif | |
36 | |
37 #include <stdio.h> | |
38 #include <stdlib.h> | |
39 #include <time.h> | |
40 #include <tommath.h> | |
41 #define CRYPT | |
42 #include "../src/headers/tfm.h" | |
43 | |
44 FILE *rng; | |
45 | |
46 /* 1-2048 bit numbers */ | |
47 void rand_num(mp_int *a) | |
48 { | |
49 int n, size; | |
50 unsigned char buf[2048]; | |
51 | |
52 size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % (FP_MAX_SIZE/16 - DIGIT_BIT/2); | |
53 buf[0] = (fgetc(rng)&1)?1:0; | |
54 fread(buf+1, 1, size, rng); | |
55 while (buf[1] == 0) buf[1] = fgetc(rng); | |
56 mp_read_raw(a, buf, 1+size); | |
57 } | |
58 | |
59 /* 1-256 bit numbers (to test things like exptmod) */ | |
60 void rand_num2(mp_int *a) | |
61 { | |
62 int n, size; | |
63 unsigned char buf[2048]; | |
64 | |
65 size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % (FP_MAX_SIZE/16 - DIGIT_BIT/2); | |
66 buf[0] = (fgetc(rng)&1)?1:0; | |
67 fread(buf+1, 1, size, rng); | |
68 while (buf[1] == 0) buf[1] = fgetc(rng); | |
69 mp_read_raw(a, buf, 1+size); | |
70 } | |
71 | |
72 #define mp_to64(a, b) mp_toradix(a, b, 64) | |
73 | |
74 int main(void) | |
75 { | |
76 int n, tmp; | |
77 mp_int a, b, c, d, e; | |
78 clock_t t1; | |
79 char buf[4096]; | |
80 | |
81 mp_init(&a); | |
82 mp_init(&b); | |
83 mp_init(&c); | |
84 mp_init(&d); | |
85 mp_init(&e); | |
86 | |
87 | |
88 /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */ | |
89 /* | |
90 mp_set(&a, 1); | |
91 for (n = 1; n < 8192; n++) { | |
92 mp_mul(&a, &a, &c); | |
93 printf("mul\n"); | |
94 mp_to64(&a, buf); | |
95 printf("%s\n%s\n", buf, buf); | |
96 mp_to64(&c, buf); | |
97 printf("%s\n", buf); | |
98 | |
99 mp_add_d(&a, 1, &a); | |
100 mp_mul_2(&a, &a); | |
101 mp_sub_d(&a, 1, &a); | |
102 } | |
103 */ | |
104 | |
105 rng = fopen("/dev/urandom", "rb"); | |
106 if (rng == NULL) { | |
107 rng = fopen("/dev/random", "rb"); | |
108 if (rng == NULL) { | |
109 fprintf(stderr, "\nWarning: stdin used as random source\n\n"); | |
110 rng = stdin; | |
111 } | |
112 } | |
113 | |
114 t1 = clock(); | |
115 for (;;) { | |
116 #if 0 | |
117 if (clock() - t1 > CLOCKS_PER_SEC) { | |
118 sleep(2); | |
119 t1 = clock(); | |
120 } | |
121 #endif | |
122 n = fgetc(rng) % 16; | |
123 if (n == 0) { | |
124 /* add tests */ | |
125 rand_num(&a); | |
126 rand_num(&b); | |
127 mp_add(&a, &b, &c); | |
128 printf("add\n"); | |
129 mp_to64(&a, buf); | |
130 printf("%s\n", buf); | |
131 mp_to64(&b, buf); | |
132 printf("%s\n", buf); | |
133 mp_to64(&c, buf); | |
134 printf("%s\n", buf); | |
135 } else if (n == 1) { | |
136 /* sub tests */ | |
137 rand_num(&a); | |
138 rand_num(&b); | |
139 mp_sub(&a, &b, &c); | |
140 printf("sub\n"); | |
141 mp_to64(&a, buf); | |
142 printf("%s\n", buf); | |
143 mp_to64(&b, buf); | |
144 printf("%s\n", buf); | |
145 mp_to64(&c, buf); | |
146 printf("%s\n", buf); | |
147 } else if (n == 2) { | |
148 /* mul tests */ | |
149 rand_num(&a); | |
150 rand_num(&b); | |
151 mp_mul(&a, &b, &c); | |
152 printf("mul\n"); | |
153 mp_to64(&a, buf); | |
154 printf("%s\n", buf); | |
155 mp_to64(&b, buf); | |
156 printf("%s\n", buf); | |
157 mp_to64(&c, buf); | |
158 printf("%s\n", buf); | |
159 } else if (n == 3) { | |
160 /* div tests */ | |
161 rand_num(&a); | |
162 rand_num(&b); | |
163 mp_div(&a, &b, &c, &d); | |
164 printf("div\n"); | |
165 mp_to64(&a, buf); | |
166 printf("%s\n", buf); | |
167 mp_to64(&b, buf); | |
168 printf("%s\n", buf); | |
169 mp_to64(&c, buf); | |
170 printf("%s\n", buf); | |
171 mp_to64(&d, buf); | |
172 printf("%s\n", buf); | |
173 } else if (n == 4) { | |
174 /* sqr tests */ | |
175 rand_num(&a); | |
176 mp_sqr(&a, &b); | |
177 printf("sqr\n"); | |
178 mp_to64(&a, buf); | |
179 printf("%s\n", buf); | |
180 mp_to64(&b, buf); | |
181 printf("%s\n", buf); | |
182 } else if (n == 5) { | |
183 /* mul_2d test */ | |
184 rand_num(&a); | |
185 mp_copy(&a, &b); | |
186 n = fgetc(rng) & 63; | |
187 mp_mul_2d(&b, n, &b); | |
188 mp_to64(&a, buf); | |
189 printf("mul2d\n"); | |
190 printf("%s\n", buf); | |
191 printf("%d\n", n); | |
192 mp_to64(&b, buf); | |
193 printf("%s\n", buf); | |
194 } else if (n == 6) { | |
195 /* div_2d test */ | |
196 rand_num(&a); | |
197 mp_copy(&a, &b); | |
198 n = fgetc(rng) & 63; | |
199 mp_div_2d(&b, n, &b, NULL); | |
200 mp_to64(&a, buf); | |
201 printf("div2d\n"); | |
202 printf("%s\n", buf); | |
203 printf("%d\n", n); | |
204 mp_to64(&b, buf); | |
205 printf("%s\n", buf); | |
206 } else if (n == 7) { | |
207 /* gcd test */ | |
208 rand_num(&a); | |
209 rand_num(&b); | |
210 a.sign = MP_ZPOS; | |
211 b.sign = MP_ZPOS; | |
212 mp_gcd(&a, &b, &c); | |
213 printf("gcd\n"); | |
214 mp_to64(&a, buf); | |
215 printf("%s\n", buf); | |
216 mp_to64(&b, buf); | |
217 printf("%s\n", buf); | |
218 mp_to64(&c, buf); | |
219 printf("%s\n", buf); | |
220 } else if (n == 8) { | |
221 /* lcm test */ | |
222 rand_num(&a); | |
223 rand_num(&b); | |
224 a.sign = MP_ZPOS; | |
225 b.sign = MP_ZPOS; | |
226 mp_lcm(&a, &b, &c); | |
227 printf("lcm\n"); | |
228 mp_to64(&a, buf); | |
229 printf("%s\n", buf); | |
230 mp_to64(&b, buf); | |
231 printf("%s\n", buf); | |
232 mp_to64(&c, buf); | |
233 printf("%s\n", buf); | |
234 } else if (n == 9) { | |
235 /* exptmod test */ | |
236 rand_num2(&a); | |
237 rand_num2(&b); | |
238 rand_num2(&c); | |
239 a.sign = b.sign = c.sign = 0; | |
240 c.dp[0] |= 1; | |
241 // if (c.used <= 4) continue; | |
242 // if (mp_cmp(&a, &c) != MP_LT) continue; | |
243 // if (mp_cmp(&b, &c) != MP_LT) continue; | |
244 mp_exptmod(&a, &b, &c, &d); | |
245 printf("expt\n"); | |
246 mp_to64(&a, buf); | |
247 printf("%s\n", buf); | |
248 mp_to64(&b, buf); | |
249 printf("%s\n", buf); | |
250 mp_to64(&c, buf); | |
251 printf("%s\n", buf); | |
252 mp_to64(&d, buf); | |
253 printf("%s\n", buf); | |
254 } else if (n == 10) { | |
255 /* invmod test */ | |
256 rand_num2(&a); | |
257 rand_num2(&b); | |
258 b.dp[0] |= 1; | |
259 b.sign = MP_ZPOS; | |
260 a.sign = MP_ZPOS; | |
261 mp_gcd(&a, &b, &c); | |
262 if (mp_cmp_d(&c, 1) != 0) continue; | |
263 if (mp_cmp_d(&b, 1) == 0) continue; | |
264 mp_invmod(&a, &b, &c); | |
265 printf("invmod\n"); | |
266 mp_to64(&a, buf); | |
267 printf("%s\n", buf); | |
268 mp_to64(&b, buf); | |
269 printf("%s\n", buf); | |
270 mp_to64(&c, buf); | |
271 printf("%s\n", buf); | |
272 } else if (n == 11) { | |
273 rand_num(&a); | |
274 mp_mul_2(&a, &a); | |
275 mp_div_2(&a, &b); | |
276 printf("div2\n"); | |
277 mp_to64(&a, buf); | |
278 printf("%s\n", buf); | |
279 mp_to64(&b, buf); | |
280 printf("%s\n", buf); | |
281 } else if (n == 12) { | |
282 rand_num(&a); | |
283 mp_mul_2(&a, &b); | |
284 printf("mul2\n"); | |
285 mp_to64(&a, buf); | |
286 printf("%s\n", buf); | |
287 mp_to64(&b, buf); | |
288 printf("%s\n", buf); | |
289 } else if (n == 13) { | |
290 rand_num(&a); | |
291 tmp = abs(rand()) & THE_MASK; | |
292 mp_add_d(&a, tmp, &b); | |
293 printf("add_d\n"); | |
294 mp_to64(&a, buf); | |
295 printf("%s\n%d\n", buf, tmp); | |
296 mp_to64(&b, buf); | |
297 printf("%s\n", buf); | |
298 } else if (n == 14) { | |
299 rand_num(&a); | |
300 tmp = abs(rand()) & THE_MASK; | |
301 mp_sub_d(&a, tmp, &b); | |
302 printf("sub_d\n"); | |
303 mp_to64(&a, buf); | |
304 printf("%s\n%d\n", buf, tmp); | |
305 mp_to64(&b, buf); | |
306 printf("%s\n", buf); | |
307 } else if (n == 15) { | |
308 rand_num(&a); | |
309 tmp = abs(rand()) & THE_MASK; | |
310 mp_mul_d(&a, tmp, &b); | |
311 printf("mul_d\n"); | |
312 mp_to64(&a, buf); | |
313 printf("%s\n%d\n", buf, tmp); | |
314 mp_to64(&b, buf); | |
315 printf("%s\n", buf); | |
316 } | |
317 } | |
318 fclose(rng); | |
319 return 0; | |
320 } | |
321 | |
322 /* $Source$ */ | |
323 /* $Revision$ */ | |
324 /* $Date$ */ |