1
|
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 "mpi.c" |
|
41 |
|
42 FILE *rng; |
|
43 |
|
44 void rand_num(mp_int *a) |
|
45 { |
|
46 int n, size; |
|
47 unsigned char buf[2048]; |
|
48 |
|
49 size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 1031; |
|
50 buf[0] = (fgetc(rng)&1)?1:0; |
|
51 fread(buf+1, 1, size, rng); |
|
52 while (buf[1] == 0) buf[1] = fgetc(rng); |
|
53 mp_read_raw(a, buf, 1+size); |
|
54 } |
|
55 |
|
56 void rand_num2(mp_int *a) |
|
57 { |
|
58 int n, size; |
|
59 unsigned char buf[2048]; |
|
60 |
|
61 size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 97; |
|
62 buf[0] = (fgetc(rng)&1)?1:0; |
|
63 fread(buf+1, 1, size, rng); |
|
64 while (buf[1] == 0) buf[1] = fgetc(rng); |
|
65 mp_read_raw(a, buf, 1+size); |
|
66 } |
|
67 |
|
68 #define mp_to64(a, b) mp_toradix(a, b, 64) |
|
69 |
|
70 int main(void) |
|
71 { |
|
72 int n, tmp; |
|
73 mp_int a, b, c, d, e; |
|
74 clock_t t1; |
|
75 char buf[4096]; |
|
76 |
|
77 mp_init(&a); |
|
78 mp_init(&b); |
|
79 mp_init(&c); |
|
80 mp_init(&d); |
|
81 mp_init(&e); |
|
82 |
|
83 |
|
84 /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */ |
|
85 /* |
|
86 mp_set(&a, 1); |
|
87 for (n = 1; n < 8192; n++) { |
|
88 mp_mul(&a, &a, &c); |
|
89 printf("mul\n"); |
|
90 mp_to64(&a, buf); |
|
91 printf("%s\n%s\n", buf, buf); |
|
92 mp_to64(&c, buf); |
|
93 printf("%s\n", buf); |
|
94 |
|
95 mp_add_d(&a, 1, &a); |
|
96 mp_mul_2(&a, &a); |
|
97 mp_sub_d(&a, 1, &a); |
|
98 } |
|
99 */ |
|
100 |
|
101 rng = fopen("/dev/urandom", "rb"); |
|
102 if (rng == NULL) { |
|
103 rng = fopen("/dev/random", "rb"); |
|
104 if (rng == NULL) { |
|
105 fprintf(stderr, "\nWarning: stdin used as random source\n\n"); |
|
106 rng = stdin; |
|
107 } |
|
108 } |
|
109 |
|
110 t1 = clock(); |
|
111 for (;;) { |
|
112 if (clock() - t1 > CLOCKS_PER_SEC) { |
|
113 sleep(2); |
|
114 t1 = clock(); |
|
115 } |
|
116 |
|
117 n = fgetc(rng) % 15; |
|
118 |
|
119 if (n == 0) { |
|
120 /* add tests */ |
|
121 rand_num(&a); |
|
122 rand_num(&b); |
|
123 mp_add(&a, &b, &c); |
|
124 printf("add\n"); |
|
125 mp_to64(&a, buf); |
|
126 printf("%s\n", buf); |
|
127 mp_to64(&b, buf); |
|
128 printf("%s\n", buf); |
|
129 mp_to64(&c, buf); |
|
130 printf("%s\n", buf); |
|
131 } else if (n == 1) { |
|
132 /* sub tests */ |
|
133 rand_num(&a); |
|
134 rand_num(&b); |
|
135 mp_sub(&a, &b, &c); |
|
136 printf("sub\n"); |
|
137 mp_to64(&a, buf); |
|
138 printf("%s\n", buf); |
|
139 mp_to64(&b, buf); |
|
140 printf("%s\n", buf); |
|
141 mp_to64(&c, buf); |
|
142 printf("%s\n", buf); |
|
143 } else if (n == 2) { |
|
144 /* mul tests */ |
|
145 rand_num(&a); |
|
146 rand_num(&b); |
|
147 mp_mul(&a, &b, &c); |
|
148 printf("mul\n"); |
|
149 mp_to64(&a, buf); |
|
150 printf("%s\n", buf); |
|
151 mp_to64(&b, buf); |
|
152 printf("%s\n", buf); |
|
153 mp_to64(&c, buf); |
|
154 printf("%s\n", buf); |
|
155 } else if (n == 3) { |
|
156 /* div tests */ |
|
157 rand_num(&a); |
|
158 rand_num(&b); |
|
159 mp_div(&a, &b, &c, &d); |
|
160 printf("div\n"); |
|
161 mp_to64(&a, buf); |
|
162 printf("%s\n", buf); |
|
163 mp_to64(&b, buf); |
|
164 printf("%s\n", buf); |
|
165 mp_to64(&c, buf); |
|
166 printf("%s\n", buf); |
|
167 mp_to64(&d, buf); |
|
168 printf("%s\n", buf); |
|
169 } else if (n == 4) { |
|
170 /* sqr tests */ |
|
171 rand_num(&a); |
|
172 mp_sqr(&a, &b); |
|
173 printf("sqr\n"); |
|
174 mp_to64(&a, buf); |
|
175 printf("%s\n", buf); |
|
176 mp_to64(&b, buf); |
|
177 printf("%s\n", buf); |
|
178 } else if (n == 5) { |
|
179 /* mul_2d test */ |
|
180 rand_num(&a); |
|
181 mp_copy(&a, &b); |
|
182 n = fgetc(rng) & 63; |
|
183 mp_mul_2d(&b, n, &b); |
|
184 mp_to64(&a, buf); |
|
185 printf("mul2d\n"); |
|
186 printf("%s\n", buf); |
|
187 printf("%d\n", n); |
|
188 mp_to64(&b, buf); |
|
189 printf("%s\n", buf); |
|
190 } else if (n == 6) { |
|
191 /* div_2d test */ |
|
192 rand_num(&a); |
|
193 mp_copy(&a, &b); |
|
194 n = fgetc(rng) & 63; |
|
195 mp_div_2d(&b, n, &b, NULL); |
|
196 mp_to64(&a, buf); |
|
197 printf("div2d\n"); |
|
198 printf("%s\n", buf); |
|
199 printf("%d\n", n); |
|
200 mp_to64(&b, buf); |
|
201 printf("%s\n", buf); |
|
202 } else if (n == 7) { |
|
203 /* gcd test */ |
|
204 rand_num(&a); |
|
205 rand_num(&b); |
|
206 a.sign = MP_ZPOS; |
|
207 b.sign = MP_ZPOS; |
|
208 mp_gcd(&a, &b, &c); |
|
209 printf("gcd\n"); |
|
210 mp_to64(&a, buf); |
|
211 printf("%s\n", buf); |
|
212 mp_to64(&b, buf); |
|
213 printf("%s\n", buf); |
|
214 mp_to64(&c, buf); |
|
215 printf("%s\n", buf); |
|
216 } else if (n == 8) { |
|
217 /* lcm test */ |
|
218 rand_num(&a); |
|
219 rand_num(&b); |
|
220 a.sign = MP_ZPOS; |
|
221 b.sign = MP_ZPOS; |
|
222 mp_lcm(&a, &b, &c); |
|
223 printf("lcm\n"); |
|
224 mp_to64(&a, buf); |
|
225 printf("%s\n", buf); |
|
226 mp_to64(&b, buf); |
|
227 printf("%s\n", buf); |
|
228 mp_to64(&c, buf); |
|
229 printf("%s\n", buf); |
|
230 } else if (n == 9) { |
|
231 /* exptmod test */ |
|
232 rand_num2(&a); |
|
233 rand_num2(&b); |
|
234 rand_num2(&c); |
|
235 // if (c.dp[0]&1) mp_add_d(&c, 1, &c); |
|
236 a.sign = b.sign = c.sign = 0; |
|
237 mp_exptmod(&a, &b, &c, &d); |
|
238 printf("expt\n"); |
|
239 mp_to64(&a, buf); |
|
240 printf("%s\n", buf); |
|
241 mp_to64(&b, buf); |
|
242 printf("%s\n", buf); |
|
243 mp_to64(&c, buf); |
|
244 printf("%s\n", buf); |
|
245 mp_to64(&d, buf); |
|
246 printf("%s\n", buf); |
|
247 } else if (n == 10) { |
|
248 /* invmod test */ |
|
249 rand_num2(&a); |
|
250 rand_num2(&b); |
|
251 b.sign = MP_ZPOS; |
|
252 a.sign = MP_ZPOS; |
|
253 mp_gcd(&a, &b, &c); |
|
254 if (mp_cmp_d(&c, 1) != 0) continue; |
|
255 if (mp_cmp_d(&b, 1) == 0) continue; |
|
256 mp_invmod(&a, &b, &c); |
|
257 printf("invmod\n"); |
|
258 mp_to64(&a, buf); |
|
259 printf("%s\n", buf); |
|
260 mp_to64(&b, buf); |
|
261 printf("%s\n", buf); |
|
262 mp_to64(&c, buf); |
|
263 printf("%s\n", buf); |
|
264 } else if (n == 11) { |
|
265 rand_num(&a); |
|
266 mp_mul_2(&a, &a); |
|
267 mp_div_2(&a, &b); |
|
268 printf("div2\n"); |
|
269 mp_to64(&a, buf); |
|
270 printf("%s\n", buf); |
|
271 mp_to64(&b, buf); |
|
272 printf("%s\n", buf); |
|
273 } else if (n == 12) { |
|
274 rand_num2(&a); |
|
275 mp_mul_2(&a, &b); |
|
276 printf("mul2\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 == 13) { |
|
282 rand_num2(&a); |
|
283 tmp = abs(rand()) & THE_MASK; |
|
284 mp_add_d(&a, tmp, &b); |
|
285 printf("add_d\n"); |
|
286 mp_to64(&a, buf); |
|
287 printf("%s\n%d\n", buf, tmp); |
|
288 mp_to64(&b, buf); |
|
289 printf("%s\n", buf); |
|
290 } else if (n == 14) { |
|
291 rand_num2(&a); |
|
292 tmp = abs(rand()) & THE_MASK; |
|
293 mp_sub_d(&a, tmp, &b); |
|
294 printf("sub_d\n"); |
|
295 mp_to64(&a, buf); |
|
296 printf("%s\n%d\n", buf, tmp); |
|
297 mp_to64(&b, buf); |
|
298 printf("%s\n", buf); |
|
299 } |
|
300 } |
|
301 fclose(rng); |
|
302 return 0; |
|
303 } |