2
|
1 #include <time.h> |
|
2 |
|
3 #ifdef IOWNANATHLON |
|
4 #include <unistd.h> |
|
5 #define SLEEP sleep(4) |
|
6 #else |
|
7 #define SLEEP |
|
8 #endif |
|
9 |
|
10 #include "tommath.h" |
|
11 |
|
12 void ndraw(mp_int *a, char *name) |
|
13 { |
|
14 char buf[4096]; |
|
15 printf("%s: ", name); |
|
16 mp_toradix(a, buf, 64); |
|
17 printf("%s\n", buf); |
|
18 } |
|
19 |
|
20 static void draw(mp_int *a) |
|
21 { |
|
22 ndraw(a, ""); |
|
23 } |
|
24 |
|
25 |
|
26 unsigned long lfsr = 0xAAAAAAAAUL; |
|
27 |
|
28 int lbit(void) |
|
29 { |
|
30 if (lfsr & 0x80000000UL) { |
|
31 lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL; |
|
32 return 1; |
|
33 } else { |
|
34 lfsr <<= 1; |
|
35 return 0; |
|
36 } |
|
37 } |
|
38 |
|
39 int myrng(unsigned char *dst, int len, void *dat) |
|
40 { |
|
41 int x; |
|
42 for (x = 0; x < len; x++) dst[x] = rand() & 0xFF; |
|
43 return len; |
|
44 } |
|
45 |
|
46 |
|
47 |
|
48 char cmd[4096], buf[4096]; |
|
49 int main(void) |
|
50 { |
|
51 mp_int a, b, c, d, e, f; |
|
52 unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, inv_n, |
|
53 div2_n, mul2_n, add_d_n, sub_d_n, t; |
|
54 unsigned rr; |
|
55 int i, n, err, cnt, ix, old_kara_m, old_kara_s; |
|
56 |
|
57 |
|
58 mp_init(&a); |
|
59 mp_init(&b); |
|
60 mp_init(&c); |
|
61 mp_init(&d); |
|
62 mp_init(&e); |
|
63 mp_init(&f); |
|
64 |
|
65 srand(time(NULL)); |
|
66 |
142
|
67 #if 0 |
2
|
68 // test mp_get_int |
|
69 printf("Testing: mp_get_int\n"); |
|
70 for(i=0;i<1000;++i) { |
142
|
71 t = ((unsigned long)rand()*rand()+1)&0xFFFFFFFF; |
2
|
72 mp_set_int(&a,t); |
|
73 if (t!=mp_get_int(&a)) { |
|
74 printf("mp_get_int() bad result!\n"); |
|
75 return 1; |
|
76 } |
|
77 } |
|
78 mp_set_int(&a,0); |
|
79 if (mp_get_int(&a)!=0) |
|
80 { printf("mp_get_int() bad result!\n"); |
|
81 return 1; |
|
82 } |
|
83 mp_set_int(&a,0xffffffff); |
|
84 if (mp_get_int(&a)!=0xffffffff) |
|
85 { printf("mp_get_int() bad result!\n"); |
|
86 return 1; |
|
87 } |
|
88 |
|
89 // test mp_sqrt |
|
90 printf("Testing: mp_sqrt\n"); |
142
|
91 for (i=0;i<1000;++i) { |
2
|
92 printf("%6d\r", i); fflush(stdout); |
|
93 n = (rand()&15)+1; |
|
94 mp_rand(&a,n); |
|
95 if (mp_sqrt(&a,&b) != MP_OKAY) |
|
96 { printf("mp_sqrt() error!\n"); |
|
97 return 1; |
|
98 } |
|
99 mp_n_root(&a,2,&a); |
|
100 if (mp_cmp_mag(&b,&a) != MP_EQ) |
|
101 { printf("mp_sqrt() bad result!\n"); |
|
102 return 1; |
|
103 } |
|
104 } |
|
105 |
|
106 printf("\nTesting: mp_is_square\n"); |
142
|
107 for (i=0;i<1000;++i) { |
2
|
108 printf("%6d\r", i); fflush(stdout); |
|
109 |
|
110 /* test mp_is_square false negatives */ |
|
111 n = (rand()&7)+1; |
|
112 mp_rand(&a,n); |
|
113 mp_sqr(&a,&a); |
|
114 if (mp_is_square(&a,&n)!=MP_OKAY) { |
|
115 printf("fn:mp_is_square() error!\n"); |
|
116 return 1; |
|
117 } |
|
118 if (n==0) { |
|
119 printf("fn:mp_is_square() bad result!\n"); |
|
120 return 1; |
|
121 } |
|
122 |
|
123 /* test for false positives */ |
|
124 mp_add_d(&a, 1, &a); |
|
125 if (mp_is_square(&a,&n)!=MP_OKAY) { |
|
126 printf("fp:mp_is_square() error!\n"); |
|
127 return 1; |
|
128 } |
|
129 if (n==1) { |
|
130 printf("fp:mp_is_square() bad result!\n"); |
|
131 return 1; |
|
132 } |
|
133 |
|
134 } |
|
135 printf("\n\n"); |
|
136 |
|
137 /* test for size */ |
142
|
138 for (ix = 10; ix < 256; ix++) { |
2
|
139 printf("Testing (not safe-prime): %9d bits \r", ix); fflush(stdout); |
|
140 err = mp_prime_random_ex(&a, 8, ix, (rand()&1)?LTM_PRIME_2MSB_OFF:LTM_PRIME_2MSB_ON, myrng, NULL); |
|
141 if (err != MP_OKAY) { |
|
142 printf("failed with err code %d\n", err); |
|
143 return EXIT_FAILURE; |
|
144 } |
|
145 if (mp_count_bits(&a) != ix) { |
|
146 printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix); |
|
147 return EXIT_FAILURE; |
|
148 } |
|
149 } |
|
150 |
142
|
151 for (ix = 16; ix < 256; ix++) { |
2
|
152 printf("Testing ( safe-prime): %9d bits \r", ix); fflush(stdout); |
|
153 err = mp_prime_random_ex(&a, 8, ix, ((rand()&1)?LTM_PRIME_2MSB_OFF:LTM_PRIME_2MSB_ON)|LTM_PRIME_SAFE, myrng, NULL); |
|
154 if (err != MP_OKAY) { |
|
155 printf("failed with err code %d\n", err); |
|
156 return EXIT_FAILURE; |
|
157 } |
|
158 if (mp_count_bits(&a) != ix) { |
|
159 printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix); |
|
160 return EXIT_FAILURE; |
|
161 } |
|
162 /* let's see if it's really a safe prime */ |
|
163 mp_sub_d(&a, 1, &a); |
|
164 mp_div_2(&a, &a); |
|
165 mp_prime_is_prime(&a, 8, &cnt); |
|
166 if (cnt != MP_YES) { |
|
167 printf("sub is not prime!\n"); |
|
168 return EXIT_FAILURE; |
|
169 } |
|
170 } |
|
171 |
|
172 printf("\n\n"); |
|
173 |
|
174 mp_read_radix(&a, "123456", 10); |
|
175 mp_toradix_n(&a, buf, 10, 3); |
|
176 printf("a == %s\n", buf); |
|
177 mp_toradix_n(&a, buf, 10, 4); |
|
178 printf("a == %s\n", buf); |
|
179 mp_toradix_n(&a, buf, 10, 30); |
|
180 printf("a == %s\n", buf); |
|
181 |
|
182 |
|
183 #if 0 |
|
184 for (;;) { |
|
185 fgets(buf, sizeof(buf), stdin); |
|
186 mp_read_radix(&a, buf, 10); |
|
187 mp_prime_next_prime(&a, 5, 1); |
|
188 mp_toradix(&a, buf, 10); |
|
189 printf("%s, %lu\n", buf, a.dp[0] & 3); |
|
190 } |
|
191 #endif |
|
192 |
|
193 /* test mp_cnt_lsb */ |
|
194 printf("testing mp_cnt_lsb...\n"); |
|
195 mp_set(&a, 1); |
|
196 for (ix = 0; ix < 1024; ix++) { |
|
197 if (mp_cnt_lsb(&a) != ix) { |
|
198 printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a)); |
|
199 return 0; |
|
200 } |
|
201 mp_mul_2(&a, &a); |
|
202 } |
|
203 |
|
204 /* test mp_reduce_2k */ |
|
205 printf("Testing mp_reduce_2k...\n"); |
142
|
206 for (cnt = 3; cnt <= 128; ++cnt) { |
2
|
207 mp_digit tmp; |
|
208 mp_2expt(&a, cnt); |
|
209 mp_sub_d(&a, 2, &a); /* a = 2**cnt - 2 */ |
|
210 |
|
211 |
|
212 printf("\nTesting %4d bits", cnt); |
|
213 printf("(%d)", mp_reduce_is_2k(&a)); |
|
214 mp_reduce_2k_setup(&a, &tmp); |
|
215 printf("(%d)", tmp); |
142
|
216 for (ix = 0; ix < 1000; ix++) { |
2
|
217 if (!(ix & 127)) {printf("."); fflush(stdout); } |
|
218 mp_rand(&b, (cnt/DIGIT_BIT + 1) * 2); |
|
219 mp_copy(&c, &b); |
|
220 mp_mod(&c, &a, &c); |
|
221 mp_reduce_2k(&b, &a, 1); |
|
222 if (mp_cmp(&c, &b)) { |
|
223 printf("FAILED\n"); |
|
224 exit(0); |
|
225 } |
|
226 } |
|
227 } |
|
228 |
|
229 /* test mp_div_3 */ |
|
230 printf("Testing mp_div_3...\n"); |
|
231 mp_set(&d, 3); |
142
|
232 for (cnt = 0; cnt < 10000; ) { |
2
|
233 mp_digit r1, r2; |
|
234 |
|
235 if (!(++cnt & 127)) printf("%9d\r", cnt); |
|
236 mp_rand(&a, abs(rand()) % 128 + 1); |
|
237 mp_div(&a, &d, &b, &e); |
|
238 mp_div_3(&a, &c, &r2); |
|
239 |
|
240 if (mp_cmp(&b, &c) || mp_cmp_d(&e, r2)) { |
|
241 printf("\n\nmp_div_3 => Failure\n"); |
|
242 } |
|
243 } |
|
244 printf("\n\nPassed div_3 testing\n"); |
|
245 |
|
246 /* test the DR reduction */ |
|
247 printf("testing mp_dr_reduce...\n"); |
142
|
248 for (cnt = 2; cnt < 32; cnt++) { |
2
|
249 printf("%d digit modulus\n", cnt); |
|
250 mp_grow(&a, cnt); |
|
251 mp_zero(&a); |
|
252 for (ix = 1; ix < cnt; ix++) { |
|
253 a.dp[ix] = MP_MASK; |
|
254 } |
|
255 a.used = cnt; |
142
|
256 a.dp[0] = 3; |
2
|
257 |
|
258 mp_rand(&b, cnt - 1); |
|
259 mp_copy(&b, &c); |
|
260 |
|
261 rr = 0; |
|
262 do { |
|
263 if (!(rr & 127)) { printf("%9lu\r", rr); fflush(stdout); } |
|
264 mp_sqr(&b, &b); mp_add_d(&b, 1, &b); |
|
265 mp_copy(&b, &c); |
|
266 |
|
267 mp_mod(&b, &a, &b); |
142
|
268 mp_dr_reduce(&c, &a, (((mp_digit)1)<<DIGIT_BIT)-a.dp[0]); |
2
|
269 |
|
270 if (mp_cmp(&b, &c) != MP_EQ) { |
|
271 printf("Failed on trial %lu\n", rr); exit(-1); |
|
272 |
|
273 } |
142
|
274 } while (++rr < 500); |
2
|
275 printf("Passed DR test for %d digits\n", cnt); |
|
276 } |
|
277 |
|
278 #endif |
|
279 |
|
280 div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n = |
|
281 sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = add_d_n = sub_d_n= 0; |
|
282 |
|
283 /* force KARA and TOOM to enable despite cutoffs */ |
|
284 KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 110; |
|
285 TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 150; |
|
286 |
|
287 for (;;) { |
|
288 /* randomly clear and re-init one variable, this has the affect of triming the alloc space */ |
|
289 switch (abs(rand()) % 7) { |
|
290 case 0: mp_clear(&a); mp_init(&a); break; |
|
291 case 1: mp_clear(&b); mp_init(&b); break; |
|
292 case 2: mp_clear(&c); mp_init(&c); break; |
|
293 case 3: mp_clear(&d); mp_init(&d); break; |
|
294 case 4: mp_clear(&e); mp_init(&e); break; |
|
295 case 5: mp_clear(&f); mp_init(&f); break; |
|
296 case 6: break; /* don't clear any */ |
|
297 } |
|
298 |
|
299 |
|
300 printf("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ", add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n); |
|
301 fgets(cmd, 4095, stdin); |
|
302 cmd[strlen(cmd)-1] = 0; |
|
303 printf("%s ]\r",cmd); fflush(stdout); |
|
304 if (!strcmp(cmd, "mul2d")) { ++mul2d_n; |
|
305 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
306 fgets(buf, 4095, stdin); sscanf(buf, "%d", &rr); |
|
307 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
308 |
|
309 mp_mul_2d(&a, rr, &a); |
|
310 a.sign = b.sign; |
|
311 if (mp_cmp(&a, &b) != MP_EQ) { |
|
312 printf("mul2d failed, rr == %d\n",rr); |
|
313 draw(&a); |
|
314 draw(&b); |
|
315 return 0; |
|
316 } |
|
317 } else if (!strcmp(cmd, "div2d")) { ++div2d_n; |
|
318 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
319 fgets(buf, 4095, stdin); sscanf(buf, "%d", &rr); |
|
320 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
321 |
|
322 mp_div_2d(&a, rr, &a, &e); |
|
323 a.sign = b.sign; |
|
324 if (a.used == b.used && a.used == 0) { a.sign = b.sign = MP_ZPOS; } |
|
325 if (mp_cmp(&a, &b) != MP_EQ) { |
|
326 printf("div2d failed, rr == %d\n",rr); |
|
327 draw(&a); |
|
328 draw(&b); |
|
329 return 0; |
|
330 } |
|
331 } else if (!strcmp(cmd, "add")) { ++add_n; |
|
332 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
333 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
334 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
335 mp_copy(&a, &d); |
|
336 mp_add(&d, &b, &d); |
|
337 if (mp_cmp(&c, &d) != MP_EQ) { |
|
338 printf("add %lu failure!\n", add_n); |
|
339 draw(&a);draw(&b);draw(&c);draw(&d); |
|
340 return 0; |
|
341 } |
|
342 |
|
343 /* test the sign/unsigned storage functions */ |
|
344 |
|
345 rr = mp_signed_bin_size(&c); |
|
346 mp_to_signed_bin(&c, (unsigned char *)cmd); |
|
347 memset(cmd+rr, rand()&255, sizeof(cmd)-rr); |
|
348 mp_read_signed_bin(&d, (unsigned char *)cmd, rr); |
|
349 if (mp_cmp(&c, &d) != MP_EQ) { |
|
350 printf("mp_signed_bin failure!\n"); |
|
351 draw(&c); |
|
352 draw(&d); |
|
353 return 0; |
|
354 } |
|
355 |
|
356 |
|
357 rr = mp_unsigned_bin_size(&c); |
|
358 mp_to_unsigned_bin(&c, (unsigned char *)cmd); |
|
359 memset(cmd+rr, rand()&255, sizeof(cmd)-rr); |
|
360 mp_read_unsigned_bin(&d, (unsigned char *)cmd, rr); |
|
361 if (mp_cmp_mag(&c, &d) != MP_EQ) { |
|
362 printf("mp_unsigned_bin failure!\n"); |
|
363 draw(&c); |
|
364 draw(&d); |
|
365 return 0; |
|
366 } |
|
367 |
|
368 } else if (!strcmp(cmd, "sub")) { ++sub_n; |
|
369 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
370 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
371 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
372 mp_copy(&a, &d); |
|
373 mp_sub(&d, &b, &d); |
|
374 if (mp_cmp(&c, &d) != MP_EQ) { |
|
375 printf("sub %lu failure!\n", sub_n); |
|
376 draw(&a);draw(&b);draw(&c);draw(&d); |
|
377 return 0; |
|
378 } |
|
379 } else if (!strcmp(cmd, "mul")) { ++mul_n; |
|
380 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
381 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
382 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
383 mp_copy(&a, &d); |
|
384 mp_mul(&d, &b, &d); |
|
385 if (mp_cmp(&c, &d) != MP_EQ) { |
|
386 printf("mul %lu failure!\n", mul_n); |
|
387 draw(&a);draw(&b);draw(&c);draw(&d); |
|
388 return 0; |
|
389 } |
|
390 } else if (!strcmp(cmd, "div")) { ++div_n; |
|
391 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
392 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
393 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
394 fgets(buf, 4095, stdin); mp_read_radix(&d, buf, 64); |
|
395 |
|
396 mp_div(&a, &b, &e, &f); |
|
397 if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) { |
|
398 printf("div %lu failure!\n", div_n); |
|
399 draw(&a);draw(&b);draw(&c);draw(&d); draw(&e); draw(&f); |
|
400 return 0; |
|
401 } |
|
402 |
|
403 } else if (!strcmp(cmd, "sqr")) { ++sqr_n; |
|
404 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
405 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
406 mp_copy(&a, &c); |
|
407 mp_sqr(&c, &c); |
|
408 if (mp_cmp(&b, &c) != MP_EQ) { |
|
409 printf("sqr %lu failure!\n", sqr_n); |
|
410 draw(&a);draw(&b);draw(&c); |
|
411 return 0; |
|
412 } |
|
413 } else if (!strcmp(cmd, "gcd")) { ++gcd_n; |
|
414 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
415 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
416 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
417 mp_copy(&a, &d); |
|
418 mp_gcd(&d, &b, &d); |
|
419 d.sign = c.sign; |
|
420 if (mp_cmp(&c, &d) != MP_EQ) { |
|
421 printf("gcd %lu failure!\n", gcd_n); |
|
422 draw(&a);draw(&b);draw(&c);draw(&d); |
|
423 return 0; |
|
424 } |
|
425 } else if (!strcmp(cmd, "lcm")) { ++lcm_n; |
|
426 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
427 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
428 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
429 mp_copy(&a, &d); |
|
430 mp_lcm(&d, &b, &d); |
|
431 d.sign = c.sign; |
|
432 if (mp_cmp(&c, &d) != MP_EQ) { |
|
433 printf("lcm %lu failure!\n", lcm_n); |
|
434 draw(&a);draw(&b);draw(&c);draw(&d); |
|
435 return 0; |
|
436 } |
|
437 } else if (!strcmp(cmd, "expt")) { ++expt_n; |
|
438 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
439 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
440 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
441 fgets(buf, 4095, stdin); mp_read_radix(&d, buf, 64); |
|
442 mp_copy(&a, &e); |
|
443 mp_exptmod(&e, &b, &c, &e); |
|
444 if (mp_cmp(&d, &e) != MP_EQ) { |
|
445 printf("expt %lu failure!\n", expt_n); |
|
446 draw(&a);draw(&b);draw(&c);draw(&d); draw(&e); |
|
447 return 0; |
|
448 } |
|
449 } else if (!strcmp(cmd, "invmod")) { ++inv_n; |
|
450 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
451 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
452 fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 64); |
|
453 mp_invmod(&a, &b, &d); |
|
454 mp_mulmod(&d,&a,&b,&e); |
|
455 if (mp_cmp_d(&e, 1) != MP_EQ) { |
|
456 printf("inv [wrong value from MPI?!] failure\n"); |
|
457 draw(&a);draw(&b);draw(&c);draw(&d); |
|
458 mp_gcd(&a, &b, &e); |
|
459 draw(&e); |
|
460 return 0; |
|
461 } |
|
462 |
|
463 } else if (!strcmp(cmd, "div2")) { ++div2_n; |
|
464 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
465 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
466 mp_div_2(&a, &c); |
|
467 if (mp_cmp(&c, &b) != MP_EQ) { |
|
468 printf("div_2 %lu failure\n", div2_n); |
|
469 draw(&a); |
|
470 draw(&b); |
|
471 draw(&c); |
|
472 return 0; |
|
473 } |
|
474 } else if (!strcmp(cmd, "mul2")) { ++mul2_n; |
|
475 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
476 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
477 mp_mul_2(&a, &c); |
|
478 if (mp_cmp(&c, &b) != MP_EQ) { |
|
479 printf("mul_2 %lu failure\n", mul2_n); |
|
480 draw(&a); |
|
481 draw(&b); |
|
482 draw(&c); |
|
483 return 0; |
|
484 } |
|
485 } else if (!strcmp(cmd, "add_d")) { ++add_d_n; |
|
486 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
487 fgets(buf, 4095, stdin); sscanf(buf, "%d", &ix); |
|
488 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
489 mp_add_d(&a, ix, &c); |
|
490 if (mp_cmp(&b, &c) != MP_EQ) { |
|
491 printf("add_d %lu failure\n", add_d_n); |
|
492 draw(&a); |
|
493 draw(&b); |
|
494 draw(&c); |
|
495 printf("d == %d\n", ix); |
|
496 return 0; |
|
497 } |
|
498 } else if (!strcmp(cmd, "sub_d")) { ++sub_d_n; |
|
499 fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 64); |
|
500 fgets(buf, 4095, stdin); sscanf(buf, "%d", &ix); |
|
501 fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 64); |
|
502 mp_sub_d(&a, ix, &c); |
|
503 if (mp_cmp(&b, &c) != MP_EQ) { |
|
504 printf("sub_d %lu failure\n", sub_d_n); |
|
505 draw(&a); |
|
506 draw(&b); |
|
507 draw(&c); |
|
508 printf("d == %d\n", ix); |
|
509 return 0; |
|
510 } |
|
511 } |
|
512 } |
|
513 return 0; |
|
514 } |
|
515 |