added libtommath-0.16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
diff --git a/bn.pdf b/bn.pdf
index 596c440..d047a83 100644
Binary files a/bn.pdf and b/bn.pdf differ
diff --git a/bn.tex b/bn.tex
index 79547f3..980d6b9 100644
--- a/bn.tex
+++ b/bn.tex
@@ -1,7 +1,7 @@
-\documentclass{article}
+\documentclass[]{report}
\begin{document}
-\title{LibTomMath v0.15 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
+\title{LibTomMath v0.16 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
\newpage
@@ -286,6 +286,9 @@ int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
/* a/b => cb + d == a */
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
+/* c = a^b */
+int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
+
/* c = a mod b, 0 <= c < b */
int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
\end{verbatim}
@@ -1027,6 +1030,32 @@ p == 10951211157166778028568112903923951285881685924091094949001780089679
\end{verbatim}
\end{small}
-
+%\newpage
+%\section*{Appendix B - Function Quick Sheet}
+
+%The following table gives a quick summary of the functions provided within LibTomMath.
+
+%\begin{flushleft}
+%\begin{tiny}
+%\begin{tabular}{|l|l|l|}
+%\hline \textbf{Function Name} & \textbf{Purpose} & \textbf{Notes} \\
+%\hline mp\_init(mp\_int *a) & Initializes a mp\_int & Allocates runtime memory required for an integer \\
+%\hline mp\_clear(mp\_int *a) & Frees the ram used by an mp\_int & \\
+%\hline mp\_exch(mp\_int *a, mp\_int *b) & Swaps two mp\_int structures contents & \\
+%\hline mp\_shrink(mp\_int *a) & Frees unused memory & The mp\_int is still valid and not cleared. \\
+%\hline mp\_grow(mp\_int *a, int size) & Ensures that a has at least $size$ digits allocated & \\
+%\hline mp\_init\_size(mp\_int a, int size) & Inits and ensures it has at least $size$ digits & \\
+%\hline &&\\
+%\hline mp\_zero(mp\_int *a) & $a \leftarrow 0$ & \\
+%\hline mp\_set(mp\_int *a, mp\_digit b) & $a \leftarrow b$ & \\
+%\hline mp\_set\_int(mp\_int *a, unsigned long b) & $a \leftarrow b$ & Only reads upto 32 bits from $b$ \\
+%\hline &&\\
+%\hline mp\_rshd(mp\_int *a, int b) & $a \leftarrow a/\beta^b$ & \\
+%\hline mp\_lshd(mp\_int *a, int b) & $a \leftarrow a \cdot \beta^b$ &\\
+%\hline mp\_div\_2d(mp\_int *a, int b, mp\_int *c, mp\_int *d) & &\\
+%\hline
+%\end{tabular}
+%\end{tiny}
+%\end{flushleft}
\end{document}
diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c
index 38c265e..68cdf1c 100644
--- a/bn_fast_mp_invmod.c
+++ b/bn_fast_mp_invmod.c
@@ -26,6 +26,7 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
mp_int x, y, u, v, B, D;
int res, neg;
+ /* init all our temps */
if ((res = mp_init (&x)) != MP_OKAY) {
goto __ERR;
}
@@ -58,6 +59,7 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
goto __D;
}
+ /* we need |y| */
if ((res = mp_abs (&y, &y)) != MP_OKAY) {
goto __D;
}
@@ -93,13 +95,12 @@ top:
goto __D;
}
}
- /* A = A/2, B = B/2 */
+ /* B = B/2 */
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
goto __D;
}
}
-
/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
/* 5.1 v = v/2 */
@@ -108,12 +109,12 @@ top:
}
/* 5.2 if C,D are even then */
if (mp_iseven (&D) == 0) {
- /* C = (C+y)/2, D = (D-x)/2 */
+ /* D = (D-x)/2 */
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto __D;
}
}
- /* C = C/2, D = D/2 */
+ /* D = D/2 */
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
goto __D;
}
@@ -121,7 +122,7 @@ top:
/* 6. if u >= v then */
if (mp_cmp (&u, &v) != MP_LT) {
- /* u = u - v, A = A - C, B = B - D */
+ /* u = u - v, B = B - D */
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
goto __D;
}
@@ -130,7 +131,7 @@ top:
goto __D;
}
} else {
- /* v - v - u, C = C - A, D = D - B */
+ /* v - v - u, D = D - B */
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
goto __D;
}
diff --git a/bn_fast_mp_montgomery_reduce.c b/bn_fast_mp_montgomery_reduce.c
index 17be2e4..031b410 100644
--- a/bn_fast_mp_montgomery_reduce.c
+++ b/bn_fast_mp_montgomery_reduce.c
@@ -45,12 +45,12 @@ fast_mp_montgomery_reduce (mp_int * a, mp_int * m, mp_digit mp)
_W = W;
tmpa = a->dp;
- /* copy the digits of a */
+ /* copy the digits of a into W[0..a->used-1] */
for (ix = 0; ix < a->used; ix++) {
*_W++ = *tmpa++;
}
- /* zero the high words */
+ /* zero the high words of W[a->used..m->used*2] */
for (; ix < m->used * 2 + 1; ix++) {
*_W++ = 0;
}
diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c
index 0fe5ad3..66d1f07 100644
--- a/bn_mp_add_d.c
+++ b/bn_mp_add_d.c
@@ -21,7 +21,7 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
mp_int t;
int res;
- if ((res = mp_init (&t)) != MP_OKAY) {
+ if ((res = mp_init_size(&t, 1)) != MP_OKAY) {
return res;
}
mp_set (&t, b);
diff --git a/bn_mp_clamp.c b/bn_mp_clamp.c
index f2839a1..c349f7c 100644
--- a/bn_mp_clamp.c
+++ b/bn_mp_clamp.c
@@ -24,8 +24,9 @@
void
mp_clamp (mp_int * a)
{
- while (a->used > 0 && a->dp[a->used - 1] == 0)
+ while (a->used > 0 && a->dp[a->used - 1] == 0) {
--(a->used);
+ }
if (a->used == 0) {
a->sign = MP_ZPOS;
}
diff --git a/bn_mp_copy.c b/bn_mp_copy.c
index 10ab6a6..1bf5f12 100644
--- a/bn_mp_copy.c
+++ b/bn_mp_copy.c
@@ -37,6 +37,7 @@ mp_copy (mp_int * a, mp_int * b)
{
register mp_digit *tmpa, *tmpb;
+ /* point aliases */
tmpa = a->dp;
tmpb = b->dp;
diff --git a/bn_mp_div.c b/bn_mp_div.c
index 8eceec8..3888a4b 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -74,17 +74,19 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
x.sign = y.sign = MP_ZPOS;
/* normalize both x and y, ensure that y >= b/2, [b == 2^DIGIT_BIT] */
- norm = 0;
- while ((y.dp[y.used - 1] & (((mp_digit) 1) << (DIGIT_BIT - 1))) == ((mp_digit) 0)) {
- ++norm;
- if ((res = mp_mul_2 (&x, &x)) != MP_OKAY) {
- goto __Y;
- }
- if ((res = mp_mul_2 (&y, &y)) != MP_OKAY) {
- goto __Y;
- }
+ norm = mp_count_bits(&y) % DIGIT_BIT;
+ if (norm < (DIGIT_BIT-1)) {
+ norm = (DIGIT_BIT-1) - norm;
+ if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
+ goto __Y;
+ }
+ if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
+ goto __Y;
+ }
+ } else {
+ norm = 0;
}
-
+
/* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
n = x.used - 1;
t = y.used - 1;
diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c
index 787f87a..858e8a4 100644
--- a/bn_mp_div_2.c
+++ b/bn_mp_div_2.c
@@ -32,15 +32,26 @@ mp_div_2 (mp_int * a, mp_int * b)
{
register mp_digit r, rr, *tmpa, *tmpb;
+ /* source alias */
tmpa = a->dp + b->used - 1;
+
+ /* dest alias */
tmpb = b->dp + b->used - 1;
+
+ /* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
+ /* get the carry for the next iteration */
rr = *tmpa & 1;
+
+ /* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
+
+ /* forward carry to next iteration */
r = rr;
}
+ /* zero excess digits */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
diff --git a/bn_mp_div_2d.c b/bn_mp_div_2d.c
index 4258c05..75501a4 100644
--- a/bn_mp_div_2d.c
+++ b/bn_mp_div_2d.c
@@ -58,13 +58,23 @@ mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
/* shift any bit count < DIGIT_BIT */
D = (mp_digit) (b % DIGIT_BIT);
if (D != 0) {
+ register mp_digit *tmpc, mask;
+
+ /* mask */
+ mask = (1U << D) - 1U;
+
+ /* alias */
+ tmpc = c->dp + (c->used - 1);
+
+ /* carry */
r = 0;
for (x = c->used - 1; x >= 0; x--) {
/* get the lower bits of this word in a temp */
- rr = c->dp[x] & ((mp_digit) ((1U << D) - 1U));
+ rr = *tmpc & mask;
/* shift the current word and mix in the carry bits from the previous word */
- c->dp[x] = (c->dp[x] >> D) | (r << (DIGIT_BIT - D));
+ *tmpc = (*tmpc >> D) | (r << (DIGIT_BIT - D));
+ --tmpc;
/* set the carry to the carry bits of the current word found above */
r = rr;
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index 4c25a74..4b33a42 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -33,6 +33,7 @@ mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
mp_set (&t, b);
res = mp_div (a, &t, c, &t2);
+ /* set remainder if not null */
if (d != NULL) {
*d = t2.dp[0];
}
diff --git a/bn_mp_exch.c b/bn_mp_exch.c
index 526c4c9..b2cdab7 100644
--- a/bn_mp_exch.c
+++ b/bn_mp_exch.c
@@ -14,6 +14,9 @@
*/
#include <tommath.h>
+/* swap the elements of two integers, for cases where you can't simply swap the
+ * mp_int pointers around
+ */
void
mp_exch (mp_int * a, mp_int * b)
{
diff --git a/bn_mp_expt_d.c b/bn_mp_expt_d.c
index 602785b..144ae07 100644
--- a/bn_mp_expt_d.c
+++ b/bn_mp_expt_d.c
@@ -14,13 +14,13 @@
*/
#include <tommath.h>
+/* calculate c = a^b using a square-multiply algorithm */
int
mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
{
int res, x;
mp_int g;
-
if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
return res;
}
@@ -29,11 +29,13 @@ mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
mp_set (c, 1);
for (x = 0; x < (int) DIGIT_BIT; x++) {
+ /* square */
if ((res = mp_sqr (c, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
+ /* if the bit is set multiply */
if ((b & (mp_digit) (1 << (DIGIT_BIT - 1))) != 0) {
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
mp_clear (&g);
@@ -41,6 +43,7 @@ mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
}
}
+ /* shift to next bit */
b <<= 1;
}
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index a780dbc..b6635f5 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -164,7 +164,7 @@ f_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
mode = 2;
if (bitcpy == winsize) {
- /* ok window is filled so square as required and multiply multiply */
+ /* ok window is filled so square as required and multiply */
/* square first */
for (x = 0; x < winsize; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
diff --git a/bn_mp_exptmod_fast.c b/bn_mp_exptmod_fast.c
index 83c7b7a..0906f27 100644
--- a/bn_mp_exptmod_fast.c
+++ b/bn_mp_exptmod_fast.c
@@ -19,7 +19,7 @@
* Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
* The value of k changes based on the size of the exponent.
*
- * Uses Montgomery reduction
+ * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
*/
int
mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
@@ -29,7 +29,6 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
int (*redux)(mp_int*,mp_int*,mp_digit);
-
/* find window size */
x = mp_count_bits (X);
if (x <= 7) {
@@ -169,7 +168,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
mode = 2;
if (bitcpy == winsize) {
- /* ok window is filled so square as required and multiply multiply */
+ /* ok window is filled so square as required and multiply */
/* square first */
for (x = 0; x < winsize; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c
index df68841..d7cc1d4 100644
--- a/bn_mp_gcd.c
+++ b/bn_mp_gcd.c
@@ -22,7 +22,6 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
mp_int u, v, t;
int k, res, neg;
-
/* either zero than gcd is the largest */
if (mp_iszero (a) == 1 && mp_iszero (b) == 0) {
return mp_copy (b, c);
@@ -55,7 +54,7 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
/* B1. Find power of two */
k = 0;
- while ((u.dp[0] & 1) == 0 && (v.dp[0] & 1) == 0) {
+ while (mp_iseven(&u) == 1 && mp_iseven(&v) == 1) {
++k;
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto __T;
@@ -66,12 +65,14 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
}
/* B2. Initialize */
- if ((u.dp[0] & 1) == 1) {
+ if (mp_isodd(&u) == 1) {
+ /* t = -v */
if ((res = mp_copy (&v, &t)) != MP_OKAY) {
goto __T;
}
t.sign = MP_NEG;
} else {
+ /* t = u */
if ((res = mp_copy (&u, &t)) != MP_OKAY) {
goto __T;
}
@@ -79,7 +80,7 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
do {
/* B3 (and B4). Halve t, if even */
- while (t.used != 0 && (t.dp[0] & 1) == 0) {
+ while (t.used != 0 && mp_iseven(&t) == 1) {
if ((res = mp_div_2 (&t, &t)) != MP_OKAY) {
goto __T;
}
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 369fb4e..9bd5118 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -22,13 +22,15 @@ mp_grow (mp_int * a, int size)
/* if the alloc size is smaller alloc more ram */
if (a->alloc < size) {
- size += (MP_PREC * 2) - (size & (MP_PREC - 1)); /* ensure there are always at least MP_PREC digits extra on top */
+ /* ensure there are always at least MP_PREC digits extra on top */
+ size += (MP_PREC * 2) - (size & (MP_PREC - 1));
a->dp = OPT_CAST realloc (a->dp, sizeof (mp_digit) * size);
if (a->dp == NULL) {
return MP_MEM;
}
+ /* zero excess digits */
n = a->alloc;
a->alloc = size;
for (i = n; i < a->alloc; i++) {
diff --git a/bn_mp_init.c b/bn_mp_init.c
index 7c3ee01..b96e6d9 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -27,9 +27,9 @@ mp_init (mp_int * a)
/* set the used to zero, allocated digit to the default precision
* and sign to positive */
- a->used = 0;
+ a->used = 0;
a->alloc = MP_PREC;
- a->sign = MP_ZPOS;
+ a->sign = MP_ZPOS;
return MP_OKAY;
}
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index 45d8dc5..2408c1a 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -19,8 +19,10 @@ int
mp_init_size (mp_int * a, int size)
{
- /* pad up so there are at least 16 zero digits */
- size += (MP_PREC * 2) - (size & (MP_PREC - 1)); /* ensure there are always at least 16 digits extra on top */
+ /* pad size so there are always extra digits */
+ size += (MP_PREC * 2) - (size & (MP_PREC - 1));
+
+ /* alloc mem */
a->dp = OPT_CAST calloc (sizeof (mp_digit), size);
if (a->dp == NULL) {
return MP_MEM;
diff --git a/bn_mp_mul_2.c b/bn_mp_mul_2.c
index fca7125..fd8db1f 100644
--- a/bn_mp_mul_2.c
+++ b/bn_mp_mul_2.c
@@ -35,17 +35,29 @@ mp_mul_2 (mp_int * a, mp_int * b)
{
register mp_digit r, rr, *tmpa, *tmpb;
- r = 0;
+ /* alias for source */
tmpa = a->dp;
+
+ /* alias for dest */
tmpb = b->dp;
+
+ /* carry */
+ r = 0;
for (x = 0; x < b->used; x++) {
+
+ /* get what will be the *next* carry bit from the MSB of the current digit */
rr = *tmpa >> (DIGIT_BIT - 1);
+
+ /* now shift up this digit, add in the carry [from the previous] */
*tmpb++ = ((*tmpa++ << 1) | r) & MP_MASK;
+
+ /* copy the carry that would be from the source digit into the next iteration */
r = rr;
}
/* new leading digit? */
if (r != 0) {
+ /* do we have to grow to accomodate the new digit? */
if (b->alloc == b->used) {
if ((res = mp_grow (b, b->used + 1)) != MP_OKAY) {
return res;
@@ -56,11 +68,12 @@ mp_mul_2 (mp_int * a, mp_int * b)
*/
tmpb = b->dp + b->used;
}
- /* add a MSB of 1 */
+ /* add a MSB which is always 1 at this point */
*tmpb = 1;
++b->used;
}
+ /* now zero any excess digits on the destination that we didn't write to */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
index 3b336d1..137df30 100644
--- a/bn_mp_mul_2d.c
+++ b/bn_mp_mul_2d.c
@@ -21,7 +21,6 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
mp_digit d, r, rr;
int x, res;
-
/* copy */
if ((res = mp_copy (a, c)) != MP_OKAY) {
return res;
@@ -42,13 +41,23 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
/* shift any bit count < DIGIT_BIT */
d = (mp_digit) (b % DIGIT_BIT);
if (d != 0) {
- r = 0;
+ register mp_digit *tmpc, mask;
+
+ /* bitmask for carries */
+ mask = (1U << d) - 1U;
+
+ /* alias */
+ tmpc = c->dp;
+
+ /* carry */
+ r = 0;
for (x = 0; x < c->used; x++) {
/* get the higher bits of the current word */
- rr = (c->dp[x] >> (DIGIT_BIT - d)) & ((mp_digit) ((1U << d) - 1U));
+ rr = (*tmpc >> (DIGIT_BIT - d)) & mask;
/* shift the current word and OR in the carry */
- c->dp[x] = ((c->dp[x] << d) | r) & MP_MASK;
+ *tmpc = ((*tmpc << d) | r) & MP_MASK;
+ ++tmpc;
/* set the carry to the carry bits of the current word */
r = rr;
diff --git a/bn_radix.c b/bn_radix.c
index 6aeda17..3b4b639 100644
--- a/bn_radix.c
+++ b/bn_radix.c
@@ -17,7 +17,6 @@
/* chars used in radix conversions */
static const char *s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
-
/* read a string [ASCII] in a given radix */
int
mp_read_radix (mp_int * a, char *str, int radix)
diff --git a/changes.txt b/changes.txt
index df7ac4e..6833bdc 100644
--- a/changes.txt
+++ b/changes.txt
@@ -1,3 +1,9 @@
+Mar 29th, 2003
+v0.16 -- Sped up mp_div by making normalization one shift call
+ -- Sped up mp_mul_2d/mp_div_2d by aliasing pointers :-)
+ -- Cleaned up mp_gcd to use the macros for odd/even detection
+ -- Added comments here and there, mostly there but occasionally here too.
+
Mar 22nd, 2003
v0.15 -- Added series of prime testing routines to lib
-- Fixed up etc/tune.c
diff --git a/demo/demo.c b/demo/demo.c
index 8cf6dfe..ff85903 100644
--- a/demo/demo.c
+++ b/demo/demo.c
@@ -140,6 +140,7 @@ int main(void)
#ifdef TIMER
printf("CLOCKS_PER_SEC == %lu\n", CLOCKS_PER_SEC);
+goto sqrtime;
log = fopen("add.log", "w");
for (cnt = 4; cnt <= 128; cnt += 4) {
@@ -170,6 +171,7 @@ int main(void)
fclose(log);
+sqrtime:
log = fopen("sqr.log", "w");
for (cnt = 4; cnt <= 128; cnt += 4) {
mp_rand(&a, cnt);
@@ -197,6 +199,7 @@ int main(void)
}
fclose(log);
+expttime:
{
char *primes[] = {
/* DR moduli */
diff --git a/makefile b/makefile
index 4219e6b..8466163 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,6 @@
CFLAGS += -I./ -Wall -W -Wshadow -O3 -fomit-frame-pointer -funroll-loops
-VERSION=0.15
+VERSION=0.16
default: libtommath.a
diff --git a/pre_gen/mpi.c b/pre_gen/mpi.c
index d659761..3921dc4 100644
--- a/pre_gen/mpi.c
+++ b/pre_gen/mpi.c
@@ -53,6 +53,7 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
mp_int x, y, u, v, B, D;
int res, neg;
+ /* init all our temps */
if ((res = mp_init (&x)) != MP_OKAY) {
goto __ERR;
}
@@ -85,6 +86,7 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
goto __D;
}
+ /* we need |y| */
if ((res = mp_abs (&y, &y)) != MP_OKAY) {
goto __D;
}
@@ -120,13 +122,12 @@ top:
goto __D;
}
}
- /* A = A/2, B = B/2 */
+ /* B = B/2 */
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
goto __D;
}
}
-
/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
/* 5.1 v = v/2 */
@@ -135,12 +136,12 @@ top:
}
/* 5.2 if C,D are even then */
if (mp_iseven (&D) == 0) {
- /* C = (C+y)/2, D = (D-x)/2 */
+ /* D = (D-x)/2 */
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto __D;
}
}
- /* C = C/2, D = D/2 */
+ /* D = D/2 */
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
goto __D;
}
@@ -148,7 +149,7 @@ top:
/* 6. if u >= v then */
if (mp_cmp (&u, &v) != MP_LT) {
- /* u = u - v, A = A - C, B = B - D */
+ /* u = u - v, B = B - D */
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
goto __D;
}
@@ -157,7 +158,7 @@ top:
goto __D;
}
} else {
- /* v - v - u, C = C - A, D = D - B */
+ /* v - v - u, D = D - B */
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
goto __D;
}
@@ -251,12 +252,12 @@ fast_mp_montgomery_reduce (mp_int * a, mp_int * m, mp_digit mp)
_W = W;
tmpa = a->dp;
- /* copy the digits of a */
+ /* copy the digits of a into W[0..a->used-1] */
for (ix = 0; ix < a->used; ix++) {
*_W++ = *tmpa++;
}
- /* zero the high words */
+ /* zero the high words of W[a->used..m->used*2] */
for (; ix < m->used * 2 + 1; ix++) {
*_W++ = 0;
}
@@ -901,7 +902,7 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
mp_int t;
int res;
- if ((res = mp_init (&t)) != MP_OKAY) {
+ if ((res = mp_init_size(&t, 1)) != MP_OKAY) {
return res;
}
mp_set (&t, b);
@@ -995,8 +996,9 @@ mp_and (mp_int * a, mp_int * b, mp_int * c)
void
mp_clamp (mp_int * a)
{
- while (a->used > 0 && a->dp[a->used - 1] == 0)
+ while (a->used > 0 && a->dp[a->used - 1] == 0) {
--(a->used);
+ }
if (a->used == 0) {
a->sign = MP_ZPOS;
}
@@ -1197,6 +1199,7 @@ mp_copy (mp_int * a, mp_int * b)
{
register mp_digit *tmpa, *tmpb;
+ /* point aliases */
tmpa = a->dp;
tmpb = b->dp;
@@ -1331,17 +1334,19 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
x.sign = y.sign = MP_ZPOS;
/* normalize both x and y, ensure that y >= b/2, [b == 2^DIGIT_BIT] */
- norm = 0;
- while ((y.dp[y.used - 1] & (((mp_digit) 1) << (DIGIT_BIT - 1))) == ((mp_digit) 0)) {
- ++norm;
- if ((res = mp_mul_2 (&x, &x)) != MP_OKAY) {
- goto __Y;
- }
- if ((res = mp_mul_2 (&y, &y)) != MP_OKAY) {
- goto __Y;
- }
+ norm = mp_count_bits(&y) % DIGIT_BIT;
+ if (norm < (DIGIT_BIT-1)) {
+ norm = (DIGIT_BIT-1) - norm;
+ if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
+ goto __Y;
+ }
+ if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
+ goto __Y;
+ }
+ } else {
+ norm = 0;
}
-
+
/* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
n = x.used - 1;
t = y.used - 1;
@@ -1491,15 +1496,26 @@ mp_div_2 (mp_int * a, mp_int * b)
{
register mp_digit r, rr, *tmpa, *tmpb;
+ /* source alias */
tmpa = a->dp + b->used - 1;
+
+ /* dest alias */
tmpb = b->dp + b->used - 1;
+
+ /* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
+ /* get the carry for the next iteration */
rr = *tmpa & 1;
+
+ /* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
+
+ /* forward carry to next iteration */
r = rr;
}
+ /* zero excess digits */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
@@ -1573,13 +1589,23 @@ mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
/* shift any bit count < DIGIT_BIT */
D = (mp_digit) (b % DIGIT_BIT);
if (D != 0) {
+ register mp_digit *tmpc, mask;
+
+ /* mask */
+ mask = (1U << D) - 1U;
+
+ /* alias */
+ tmpc = c->dp + (c->used - 1);
+
+ /* carry */
r = 0;
for (x = c->used - 1; x >= 0; x--) {
/* get the lower bits of this word in a temp */
- rr = c->dp[x] & ((mp_digit) ((1U << D) - 1U));
+ rr = *tmpc & mask;
/* shift the current word and mix in the carry bits from the previous word */
- c->dp[x] = (c->dp[x] >> D) | (r << (DIGIT_BIT - D));
+ *tmpc = (*tmpc >> D) | (r << (DIGIT_BIT - D));
+ --tmpc;
/* set the carry to the carry bits of the current word found above */
r = rr;
@@ -1632,6 +1658,7 @@ mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
mp_set (&t, b);
res = mp_div (a, &t, c, &t2);
+ /* set remainder if not null */
if (d != NULL) {
*d = t2.dp[0];
}
@@ -1814,6 +1841,9 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
*/
#include <tommath.h>
+/* swap the elements of two integers, for cases where you can't simply swap the
+ * mp_int pointers around
+ */
void
mp_exch (mp_int * a, mp_int * b)
{
@@ -1993,7 +2023,7 @@ f_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
mode = 2;
if (bitcpy == winsize) {
- /* ok window is filled so square as required and multiply multiply */
+ /* ok window is filled so square as required and multiply */
/* square first */
for (x = 0; x < winsize; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
@@ -2077,7 +2107,7 @@ __M:
* Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
* The value of k changes based on the size of the exponent.
*
- * Uses Montgomery reduction
+ * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
*/
int
mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
@@ -2087,7 +2117,6 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
int (*redux)(mp_int*,mp_int*,mp_digit);
-
/* find window size */
x = mp_count_bits (X);
if (x <= 7) {
@@ -2227,7 +2256,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
mode = 2;
if (bitcpy == winsize) {
- /* ok window is filled so square as required and multiply multiply */
+ /* ok window is filled so square as required and multiply */
/* square first */
for (x = 0; x < winsize; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
@@ -2312,13 +2341,13 @@ __M:
*/
#include <tommath.h>
+/* calculate c = a^b using a square-multiply algorithm */
int
mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
{
int res, x;
mp_int g;
-
if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
return res;
}
@@ -2327,11 +2356,13 @@ mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
mp_set (c, 1);
for (x = 0; x < (int) DIGIT_BIT; x++) {
+ /* square */
if ((res = mp_sqr (c, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
+ /* if the bit is set multiply */
if ((b & (mp_digit) (1 << (DIGIT_BIT - 1))) != 0) {
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
mp_clear (&g);
@@ -2339,6 +2370,7 @@ mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
}
}
+ /* shift to next bit */
b <<= 1;
}
@@ -2373,7 +2405,6 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
mp_int u, v, t;
int k, res, neg;
-
/* either zero than gcd is the largest */
if (mp_iszero (a) == 1 && mp_iszero (b) == 0) {
return mp_copy (b, c);
@@ -2406,7 +2437,7 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
/* B1. Find power of two */
k = 0;
- while ((u.dp[0] & 1) == 0 && (v.dp[0] & 1) == 0) {
+ while (mp_iseven(&u) == 1 && mp_iseven(&v) == 1) {
++k;
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto __T;
@@ -2417,12 +2448,14 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
}
/* B2. Initialize */
- if ((u.dp[0] & 1) == 1) {
+ if (mp_isodd(&u) == 1) {
+ /* t = -v */
if ((res = mp_copy (&v, &t)) != MP_OKAY) {
goto __T;
}
t.sign = MP_NEG;
} else {
+ /* t = u */
if ((res = mp_copy (&u, &t)) != MP_OKAY) {
goto __T;
}
@@ -2430,7 +2463,7 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
do {
/* B3 (and B4). Halve t, if even */
- while (t.used != 0 && (t.dp[0] & 1) == 0) {
+ while (t.used != 0 && mp_iseven(&t) == 1) {
if ((res = mp_div_2 (&t, &t)) != MP_OKAY) {
goto __T;
}
@@ -2495,13 +2528,15 @@ mp_grow (mp_int * a, int size)
/* if the alloc size is smaller alloc more ram */
if (a->alloc < size) {
- size += (MP_PREC * 2) - (size & (MP_PREC - 1)); /* ensure there are always at least MP_PREC digits extra on top */
+ /* ensure there are always at least MP_PREC digits extra on top */
+ size += (MP_PREC * 2) - (size & (MP_PREC - 1));
a->dp = OPT_CAST realloc (a->dp, sizeof (mp_digit) * size);
if (a->dp == NULL) {
return MP_MEM;
}
+ /* zero excess digits */
n = a->alloc;
a->alloc = size;
for (i = n; i < a->alloc; i++) {
@@ -2543,9 +2578,9 @@ mp_init (mp_int * a)
/* set the used to zero, allocated digit to the default precision
* and sign to positive */
- a->used = 0;
+ a->used = 0;
a->alloc = MP_PREC;
- a->sign = MP_ZPOS;
+ a->sign = MP_ZPOS;
return MP_OKAY;
}
@@ -2605,8 +2640,10 @@ int
mp_init_size (mp_int * a, int size)
{
- /* pad up so there are at least 16 zero digits */
- size += (MP_PREC * 2) - (size & (MP_PREC - 1)); /* ensure there are always at least 16 digits extra on top */
+ /* pad size so there are always extra digits */
+ size += (MP_PREC * 2) - (size & (MP_PREC - 1));
+
+ /* alloc mem */
a->dp = OPT_CAST calloc (sizeof (mp_digit), size);
if (a->dp == NULL) {
return MP_MEM;
@@ -3795,17 +3832,29 @@ mp_mul_2 (mp_int * a, mp_int * b)
{
register mp_digit r, rr, *tmpa, *tmpb;
- r = 0;
+ /* alias for source */
tmpa = a->dp;
+
+ /* alias for dest */
tmpb = b->dp;
+
+ /* carry */
+ r = 0;
for (x = 0; x < b->used; x++) {
+
+ /* get what will be the *next* carry bit from the MSB of the current digit */
rr = *tmpa >> (DIGIT_BIT - 1);
+
+ /* now shift up this digit, add in the carry [from the previous] */
*tmpb++ = ((*tmpa++ << 1) | r) & MP_MASK;
+
+ /* copy the carry that would be from the source digit into the next iteration */
r = rr;
}
/* new leading digit? */
if (r != 0) {
+ /* do we have to grow to accomodate the new digit? */
if (b->alloc == b->used) {
if ((res = mp_grow (b, b->used + 1)) != MP_OKAY) {
return res;
@@ -3816,11 +3865,12 @@ mp_mul_2 (mp_int * a, mp_int * b)
*/
tmpb = b->dp + b->used;
}
- /* add a MSB of 1 */
+ /* add a MSB which is always 1 at this point */
*tmpb = 1;
++b->used;
}
+ /* now zero any excess digits on the destination that we didn't write to */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
@@ -3856,7 +3906,6 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
mp_digit d, r, rr;
int x, res;
-
/* copy */
if ((res = mp_copy (a, c)) != MP_OKAY) {
return res;
@@ -3877,13 +3926,23 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
/* shift any bit count < DIGIT_BIT */
d = (mp_digit) (b % DIGIT_BIT);
if (d != 0) {
- r = 0;
+ register mp_digit *tmpc, mask;
+
+ /* bitmask for carries */
+ mask = (1U << d) - 1U;
+
+ /* alias */
+ tmpc = c->dp;
+
+ /* carry */
+ r = 0;
for (x = 0; x < c->used; x++) {
/* get the higher bits of the current word */
- rr = (c->dp[x] >> (DIGIT_BIT - d)) & ((mp_digit) ((1U << d) - 1U));
+ rr = (*tmpc >> (DIGIT_BIT - d)) & mask;
/* shift the current word and OR in the carry */
- c->dp[x] = ((c->dp[x] << d) | r) & MP_MASK;
+ *tmpc = ((*tmpc << d) | r) & MP_MASK;
+ ++tmpc;
/* set the carry to the carry bits of the current word */
r = rr;
@@ -5401,7 +5460,6 @@ const mp_digit __prime_tab[] = {
/* chars used in radix conversions */
static const char *s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
-
/* read a string [ASCII] in a given radix */
int
mp_read_radix (mp_int * a, char *str, int radix)
diff --git a/tommath.h b/tommath.h
index d8f8d9d..cfd9da1 100644
--- a/tommath.h
+++ b/tommath.h
@@ -124,6 +124,12 @@ void mp_exch(mp_int *a, mp_int *b);
/* shrink ram required for a bignum */
int mp_shrink(mp_int *a);
+/* grow an int to a given size */
+int mp_grow(mp_int *a, int size);
+
+/* init to a given number of digits */
+int mp_init_size(mp_int *a, int size);
+
/* ---> Basic Manipulations <--- */
#define mp_iszero(a) (((a)->used == 0) ? 1 : 0)
@@ -139,12 +145,6 @@ void mp_set(mp_int *a, mp_digit b);
/* set a 32-bit const */
int mp_set_int(mp_int *a, unsigned long b);
-/* grow an int to a given size */
-int mp_grow(mp_int *a, int size);
-
-/* init to a given number of digits */
-int mp_init_size(mp_int *a, int size);
-
/* copy, b = a */
int mp_copy(mp_int *a, mp_int *b);