1 /*
2 * bignum_main.c
3 * BigMath
4 *
5 * Created by Curtis Jones on 2007.08.05.
6 * Copyright 2007 __MyCompanyName__. All rights reserved.
7 *
8 */
9
10 #include "bignum.h"
11
12 /*
13 int
14 main (int argc, char *argv[])
15 {
16 bignum_t *num1 = bignum_alloc();
17 bignum_t *num2 = bignum_alloc();
18 bignum_t *num3 = bignum_alloc();
19
20 bignum_set(num1, 2);
21 bignum_set(num2, 23);
22
23 bignum_pow(num3, num1, num2);
24
25 bignum_print( num3 );
26
27 printf("bignum::main().. %lu**%lu = %lu\n", 2, 23, num3->numb[0]);
28
29 return 0;
30 }
31 */
32
33 /*
34 int
35 main (int argc, char *argv[])
36 {
37 srandomdev();
38
39 u_int64_t i = 0;
40 u_int64_t g = 0;
41 u_int64_t b = 0;
42 u_int64_t t = 0;
43
44 while (1) {
45 bignum_t * numA = bignum_alloc();
46 bignum_t * numB = bignum_alloc();
47 bignum_t * numC = bignum_alloc();
48
49 struct timeval t1;
50 struct timeval t2;
51
52 bzero(&t1, sizeof(struct timeval));
53 bzero(&t2, sizeof(struct timeval));
54
55 bignum_set(numA, 0);
56 bignum_set(numB, 0);
57 bignum_set(numC, 0);
58
59 u_int64_t size1 = rand() % 500;
60 u_int64_t size2 = rand() % 500;
61
62 if (size1 > 1 && size2 > 1) {
63 bignum_rnd_mag(numA, size1);
64 bignum_rnd_mag(numB, size2);
65
66 gettimeofday((struct timeval *)&t1, NULL);
67 bignum_gcd(numC, numA, numB); // c = gcd(a, b)
68 gettimeofday((struct timeval *)&t2, NULL);
69
70 t = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
71
72 printf("bignum::main().. SUCCESS [count=%05llu, good=%05llu, bad=%05llu, size1=%04llu, size2=%04llu, time=%07.3f]\n", ++i, ++g, b, size1, size2, (t/1000.0));
73 bignum_print(numC);
74
75 // sleep( 1 );
76 }
77
78 bignum_free( numA );
79 bignum_free( numB );
80 bignum_free( numC );
81 }
82
83 return 0;
84 }
85 */
86
87 /*
88 int
89 main (int argc, char *argv[])
90 {
91 srandomdev();
92
93 u_int64_t i = 0;
94 u_int64_t g = 0;
95 u_int64_t b = 0;
96 u_int64_t t = 0;
97
98 struct timeval t1;
99 struct timeval t2;
100
101 bignum_t * numA = bignum_alloc();
102 bignum_t * numB = bignum_alloc();
103 bignum_t * numC = bignum_alloc();
104 bignum_t * numD = bignum_alloc();
105 bignum_t * numE = bignum_alloc();
106 bignum_t * numF = bignum_alloc();
107 bignum_t * numR = bignum_alloc();
108
109 int j = 0;
110
111 //while (1) {
112 for (j = 0; j < 1; ++j) {
113 bzero(&t1, sizeof(struct timeval));
114 bzero(&t2, sizeof(struct timeval));
115
116 bignum_set(numA, 0);
117 bignum_set(numB, 0);
118 bignum_set(numC, 0);
119 bignum_set(numD, 0);
120 bignum_set(numE, 0);
121 bignum_set(numF, 0);
122 bignum_set(numR, 0);
123
124 u_int64_t size1 = rand() % 5;
125 u_int64_t size2 = rand() % 5;
126
127 if (size1 > 1 && size2 > 1) {
128
129 bignum_rnd_mag(numA, size1);
130 bignum_rnd_mag(numB, size2);
131
132 if (bignum_lt(numA, numB)) {
133 bignum_t *tmp = numA;
134 numA = numB;
135 numB = tmp;
136 }
137
138 gettimeofday((struct timeval *)&t1, NULL);
139 bignum_div(numC, numR, numA, numB); // a / b = c * b + r
140 bignum_mul(numD, numC, numB); // c * b = d
141 bignum_sub(numE, numA, numD); // a - d = e
142 bignum_add(numF, numD, numR); // d + r = f = a
143 gettimeofday((struct timeval *)&t2, NULL);
144
145 t = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
146
147 if (0 == bignum_cmp(numF, numA))
148 printf("bignum::main().. SUCCESS [count=%05llu, good=%05llu, bad=%05llu, size1=%04llu, size2=%04llu, time=%07.3f]\n", ++i, ++g, b, size1, size2, (t/1000.0));
149 else {
150 printf("bignum::main().. FAILED [count=%05llu, good=%05llu, bad=%05llu, size1=%04llu, size2=%04llu, time=%07.3f]\n", ++i, g, ++b, size1, size2, (t/1000.0));
151
152 printf("bignum::main().. A = ");
153 bignum_print( numA );
154 printf("bignum::main().. B = ");
155 bignum_print( numB );
156 printf("bignum::main().. C = ");
157 bignum_print( numC );
158 printf("bignum::main().. D = ");
159 bignum_print( numD );
160 printf("bignum::main().. E = ");
161 bignum_print( numE );
162 printf("bignum::main().. F = ");
163 bignum_print( numF );
164 printf("bignum::main().. R = ");
165 bignum_print( numR );
166 }
167
168 // sleep( 1 );
169 }
170 }
171
172 bignum_free( numA );
173 bignum_free( numB );
174 bignum_free( numC );
175 bignum_free( numD );
176 bignum_free( numE );
177 bignum_free( numF );
178 bignum_free( numR );
179
180 return 0;
181 }
182 */
183
184 /*
185 int
186 main (int argc, char *argv[])
187 {
188 bignum_t * a = bignum_alloc();
189 bignum_t * b = bignum_alloc();
190 bignum_t * c = bignum_alloc();
191
192 //bignum_set(a, 4864);
193 //bignum_set(b, 3458);
194
195 bignum_set(a, 1529);
196 bignum_set(b, 2356);
197
198 bignum_modinv(c, a, b);
199
200 bignum_print( a );
201 bignum_print( b );
202 bignum_print( c );
203
204 return EXIT_SUCCESS;
205 }
206 */
207
208 /*
209 int
210 main (int argc, char *argv[])
211 {
212 bignum_t * a = bignum_alloc();
213 bignum_t * b = bignum_alloc();
214 bignum_t * c = bignum_alloc();
215 bignum_t * d = bignum_alloc();
216
217 //bignum_set(a, 1529);
218 //bignum_set(b, 2356);
219
220 bignum_set_ui8(a, 3, (BIGNUM_BASE_TYPE[]){0x12345678, 0x23456789, 0x00012345});
221 //bignum_set_ui8(b, 3, (BIGNUM_BASE_TYPE[]){0x12345678, 0x23456789, 0x00012344});
222 bignum_set_ui8(b, 2, (BIGNUM_BASE_TYPE[]){0x98765432, 0x45126734});
223
224 //bignum_sub(c, a, b);
225 //bignum_sub(c, b, a);
226 bignum_mul(c, a, b);
227 bignum_div(d, NULL, c, b);
228
229 bignum_print( a );
230 bignum_print( b );
231 bignum_print( c );
232 bignum_print( d );
233
234 return EXIT_SUCCESS;
235 }
236 */
237
238 /* Add two random numbers. Verify the result
239 * with subtraction.
240 *
241 * Return zero on success, non-zero otherwise.
242 *
243 */
244 int
245 test_bignum_add (bignum_t *num_a, bignum_t *num_b)
246 {
247 u_int64_t t = 0;
248 struct timeval t1, t2;
249 bignum_t * num_c = bignum_alloc();
250 bignum_t * num_d = bignum_alloc();
251 char str_a[15], str_b[15], str_c[15], str_d[15];
252
253 bzero(&t1, sizeof(struct timeval));
254 bzero(&t2, sizeof(struct timeval));
255
256 gettimeofday((struct timeval *)&t1, NULL);
257 bignum_add(num_c, num_a, num_b);
258 gettimeofday((struct timeval *)&t2, NULL);
259 bignum_sub(num_d, num_c, num_b);
260
261 bignum_cstr_base(num_a, 15, str_a, 10);
262 bignum_cstr_base(num_b, 15, str_b, 10);
263 bignum_cstr_base(num_c, 15, str_c, 10);
264 bignum_cstr_base(num_d, 15, str_d, 10);
265
266 t = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
267
268 if (bignum_eq(num_a,num_d))
269 printf(" [T | %07.3f] %s + %s = %s and %s - %s = %s\n", (t/1000.0), str_a, str_b, str_c, str_c, str_b, str_d);
270 else
271 printf(" [F | %07.3f] %s + %s = %s and %s - %s = %s\n", (t/1000.0), str_a, str_b, str_c, str_c, str_b, str_d);
272
273 bignum_free( num_c );
274 bignum_free( num_d );
275
276 return 0;
277 }
278
279 /**
280 *
281 *
282 */
283 int
284 test_bignum_sub (bignum_t *num_a, bignum_t *num_b)
285 {
286 u_int64_t t = 0;
287 struct timeval t1, t2;
288 bignum_t * num_c = bignum_alloc();
289 bignum_t * num_d = bignum_alloc();
290 char str_a[15], str_b[15], str_c[15], str_d[15];
291
292 bzero(&t1, sizeof(struct timeval));
293 bzero(&t2, sizeof(struct timeval));
294
295 gettimeofday((struct timeval *)&t1, NULL);
296 bignum_sub(num_c, num_a, num_b);
297 gettimeofday((struct timeval *)&t2, NULL);
298 bignum_add(num_d, num_c, num_b);
299
300 bignum_cstr_base(num_a, 15, str_a, 10);
301 bignum_cstr_base(num_b, 15, str_b, 10);
302 bignum_cstr_base(num_c, 15, str_c, 10);
303 bignum_cstr_base(num_d, 15, str_d, 10);
304
305 t = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
306
307 if (bignum_eq(num_a,num_d))
308 printf(" [T | %07.3f] %s - %s = %s and %s + %s = %s\n", (t/1000.0), str_a, str_b, str_c, str_c, str_b, str_d);
309 else
310 printf(" [T | %07.3f] %s - %s = %s and %s + %s = %s\n", (t/1000.0), str_a, str_b, str_c, str_c, str_b, str_d);
311
312 bignum_free( num_c );
313 bignum_free( num_d );
314
315 return 0;
316 }
317
318 /**
319 *
320 *
321 */
322 int
323 test_bignum_mul (bignum_t *num_a, bignum_t *num_b)
324 {
325 u_int64_t t = 0;
326 struct timeval t1, t2;
327 bignum_t * num_c = bignum_alloc();
328 bignum_t * num_d = bignum_alloc();
329 bignum_t * num_e = bignum_alloc();
330 char str_a[15], str_b[15], str_c[15], str_d[15], str_e[15];
331
332 bzero(&t1, sizeof(struct timeval));
333 bzero(&t2, sizeof(struct timeval));
334
335 gettimeofday((struct timeval *)&t1, NULL);
336 bignum_mul(num_c, num_a, num_b);
337 gettimeofday((struct timeval *)&t2, NULL);
338 bignum_div(num_d, num_e, num_c, num_b);
339
340 bignum_cstr_base(num_a, 15, str_a, 10);
341 bignum_cstr_base(num_b, 15, str_b, 10);
342 bignum_cstr_base(num_c, 15, str_c, 10);
343 bignum_cstr_base(num_d, 15, str_d, 10);
344 bignum_cstr_base(num_e, 15, str_e, 10);
345
346 t = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
347
348 if (bignum_eq(num_a,num_d))
349 printf(" [T | %07.3f] %s * %s = %s and %s / %s = %s r %s\n", (t/1000.0), str_a, str_b, str_c, str_c, str_b, str_d, str_e);
350 else
351 printf(" [F | %07.3f] %s * %s = %s and %s / %s = %s r %s\n", (t/1000.0), str_a, str_b, str_c, str_c, str_b, str_d, str_e);
352
353 bignum_free( num_c );
354 bignum_free( num_d );
355 bignum_free( num_e );
356
357 return 0;
358 }
359
360 /**
361 *
362 *
363 */
364 int
365 test_bignum_div (bignum_t *num_a, bignum_t *num_b)
366 {
367 u_int64_t t = 0;
368 struct timeval t1, t2;
369 bignum_t * num_c = bignum_alloc();
370 bignum_t * num_d = bignum_alloc();
371 bignum_t * num_e = bignum_alloc();
372 bignum_t * num_f = bignum_alloc();
373 char str_a[15], str_b[15], str_c[15], str_d[15], str_f[15];
374
375 bzero(&t1, sizeof(struct timeval));
376 bzero(&t2, sizeof(struct timeval));
377
378 gettimeofday((struct timeval *)&t1, NULL);
379 bignum_div(num_c, num_d, num_a, num_b);
380 gettimeofday((struct timeval *)&t2, NULL);
381 bignum_mul(num_e, num_c, num_b);
382 bignum_add(num_f, num_e, num_d);
383
384 bignum_cstr_base(num_a, 15, str_a, 10);
385 bignum_cstr_base(num_b, 15, str_b, 10);
386 bignum_cstr_base(num_c, 15, str_c, 10);
387 bignum_cstr_base(num_d, 15, str_d, 10);
388 bignum_cstr_base(num_f, 15, str_f, 10);
389
390 t = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
391
392 if (bignum_eq(num_a,num_f))
393 printf(" [T | %07.3f] %s / %s = %s r%s and %s * %s + %s = %s\n", (t/1000.0), str_a, str_b, str_c, str_d, str_c, str_b, str_d, str_f);
394 else
395 printf(" [F | %07.3f] %s / %s = %s r%s and %s * %s + %s = %s\n", (t/1000.0), str_a, str_b, str_c, str_d, str_c, str_b, str_d, str_f);
396
397 bignum_free( num_c );
398 bignum_free( num_d );
399 bignum_free( num_e );
400 bignum_free( num_f );
401
402 return 0;
403 }
404
405 /**
406 * Randomly tests and verifies the basic arithmetic operations.
407 *
408 *
409 */
410 /*
411 int
412 main (int argc, char *argv[])
413 {
414 srandomdev();
415
416 bignum_t * num_a = bignum_alloc();
417 bignum_t * num_b = bignum_alloc();
418
419 while (1)
420 {
421 int op = rand() % 4;
422
423 u_int64_t oofm_a = rand() % 1000;
424 u_int64_t oofm_b = rand() % 1000;
425
426 bignum_rnd_mag(num_a, oofm_a);
427 bignum_rnd_mag(num_b, oofm_b);
428
429 switch (op) {
430 case 0: test_bignum_add(num_a, num_b); break;
431 case 1: test_bignum_sub(num_a, num_b); break;
432 case 2: test_bignum_mul(num_a, num_b); break;
433 case 3: test_bignum_div(num_a, num_b); break;
434 };
435 }
436
437 bignum_free(num_a);
438 bignum_free(num_b);
439
440 return EXIT_SUCCESS;
441 }
442 */
443
444 /**
445 * Determines the average execution time for various operations.
446 *
447 *
448 */
449 /*
450 int
451 main (int argc, char *argv[])
452 {
453 srandomdev();
454
455 struct timeval t1, t2;
456
457 bignum_t * num_a = bignum_alloc();
458 bignum_t * num_b = bignum_alloc();
459 bignum_t * num_c = bignum_alloc();
460 bignum_t * num_d = bignum_alloc();
461
462 u_int64_t average = 0;
463 u_int64_t count = 0;
464 u_int64_t oofm_a = rand() % 1000;
465 u_int64_t oofm_b = rand() % 1000;
466
467 //
468 // ADD
469 //
470
471 for (average=0, count=0; count < 10000; ++count) {
472 bzero(&t1, sizeof(struct timeval));
473 bzero(&t2, sizeof(struct timeval));
474
475 bignum_rnd_mag(num_a, oofm_a);
476 bignum_rnd_mag(num_b, oofm_b);
477
478 gettimeofday((struct timeval *)&t1, NULL);
479 bignum_add(num_c, num_a, num_b);
480 gettimeofday((struct timeval *)&t2, NULL);
481
482 u_int64_t tmp = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
483
484 if (count == 0)
485 average = tmp;
486 else
487 average = ((average * count) + tmp) / (count+1);
488 }
489
490 printf("bignum::main().. ADD cnt=%llu; avg=%llu\n", count, average);
491
492
493 //
494 // SUB
495 //
496
497 for (average=0, count=0; count < 10000; ++count) {
498 bzero(&t1, sizeof(struct timeval));
499 bzero(&t2, sizeof(struct timeval));
500
501 bignum_rnd_mag(num_a, oofm_a);
502 bignum_rnd_mag(num_b, oofm_b);
503
504 gettimeofday((struct timeval *)&t1, NULL);
505 bignum_sub(num_c, num_a, num_b);
506 gettimeofday((struct timeval *)&t2, NULL);
507
508 u_int64_t tmp = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
509
510 if (count == 0)
511 average = tmp;
512 else
513 average = ((average * count) + tmp) / (count+1);
514 }
515
516 printf("bignum::main().. SUB cnt=%llu; avg=%llu\n", count, average);
517
518
519 //
520 // MUL
521 //
522
523 for (average=0, count=0; count < 100; ++count) {
524 bzero(&t1, sizeof(struct timeval));
525 bzero(&t2, sizeof(struct timeval));
526
527 bignum_rnd_mag(num_a, oofm_a);
528 bignum_rnd_mag(num_b, oofm_b);
529
530 gettimeofday((struct timeval *)&t1, NULL);
531 bignum_mul(num_c, num_a, num_b);
532 gettimeofday((struct timeval *)&t2, NULL);
533
534 u_int64_t tmp = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
535
536 if (count == 0)
537 average = tmp;
538 else
539 average = ((average * count) + tmp) / (count+1);
540 }
541
542 printf("bignum::main().. MUL cnt=%llu; avg=%llu\n", count, average);
543
544 //
545 // DIV
546 //
547
548 for (average=0, count=0; count < 100; ++count) {
549 bzero(&t1, sizeof(struct timeval));
550 bzero(&t2, sizeof(struct timeval));
551
552 bignum_rnd_mag(num_a, oofm_a);
553 bignum_rnd_mag(num_b, oofm_b);
554
555 gettimeofday((struct timeval *)&t1, NULL);
556 bignum_div(num_c, num_d, num_a, num_b);
557 gettimeofday((struct timeval *)&t2, NULL);
558
559 u_int64_t tmp = ((t2.tv_sec * 1000000) + t2.tv_usec) - ((t1.tv_sec * 1000000) + t1.tv_usec);
560
561 if (count == 0)
562 average = tmp;
563 else
564 average = ((average * count) + tmp) / (count+1);
565 }
566
567 printf("bignum::main().. DIV cnt=%llu; avg=%llu\n", count, average);
568
569 bignum_free(num_a);
570 bignum_free(num_b);
571 bignum_free(num_c);
572 bignum_free(num_d);
573
574 return EXIT_SUCCESS;
575 }
576 */
577
578 /**
579 * Rubik's cube stuff.
580 *
581 */
582 /*
583 int
584 main (int argc, char *argv)
585 {
586 bignum_init();
587
588 bignum_t * c_fac = bignum_alloc();
589 bignum_t * e_fac = bignum_alloc();
590 bignum_t * c_pow = bignum_alloc();
591 bignum_t * e_pow = bignum_alloc();
592 bignum_t * c_tot = bignum_alloc();
593 bignum_t * e_tot = bignum_alloc();
594 bignum_t * total = bignum_alloc();
595 bignum_t * tmp1 = bignum_alloc();
596
597 char strbuf[15] = { 0 };
598
599 bignum_factorial(c_fac, bignum_eight());
600 bignum_pow(c_pow, bignum_three(), bignum_seven());
601 bignum_mul(c_tot, c_fac, c_pow);
602
603 bignum_factorial(e_fac, bignum_twelve());
604 bignum_pow(e_pow, bignum_two(), bignum_eleven());
605 bignum_mul(e_tot, e_fac, e_pow);
606
607 bignum_mul(tmp1, c_tot, e_tot);
608 bignum_div(total, NULL, tmp1, bignum_two());
609
610 bignum_cstr_base(total, 15, strbuf, 10);
611
612 printf("main::main().. total = %s\n", strbuf);
613
614 bignum_free(c_fac);
615 bignum_free(e_fac);
616 bignum_free(total);
617
618 return EXIT_SUCCESS;
619 }
620 */
621
622 /**
623 * Factorial test.
624 *
625 */
626 int
627 main (int argc, char *argv)
628 {
629 bignum_init();
630
631 bignum_t * num1 = bignum_alloc();
632 bignum_t * num2 = bignum_alloc();
633
634 //char num1str[15] = { 0 };
635 //char num2str[15] = { 0 };
636
637 bignum_set(num1, 10000);
638
639 bignum_factorial(num2, num1);
640
641 //bignum_cstr_base(num1, 15, num1str, 10);
642 //bignum_cstr_base(num2, 15, num2str, 10);
643 //
644 //printf("main::main().. %s! = %s\n", num1str, num2str);
645
646 bignum_free(num1);
647 bignum_free(num2);
648
649 return EXIT_SUCCESS;
650 }
syntax highlighted by Code2HTML, v. 0.9.1