added libtommath-0.14
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 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
diff --git a/bn.pdf b/bn.pdf
index 6b5a2d1..59de4e3 100644
Binary files a/bn.pdf and b/bn.pdf differ
diff --git a/bn.tex b/bn.tex
index 5c40174..d5a34db 100644
--- a/bn.tex
+++ b/bn.tex
@@ -1,15 +1,15 @@
\documentclass{article}
\begin{document}
-\title{LibTomMath v0.13 \\ A Free Multiple Precision Integer Library}
+\title{LibTomMath v0.14 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
\newpage
\section{Introduction}
-``LibTomMath'' is a free and open source library that provides multiple-precision integer functions required to form a basis
-of a public key cryptosystem. LibTomMath is written entire in portable ISO C source code and designed to have an application
-interface much like that of MPI from Michael Fromberger.
+``LibTomMath'' is a free and open source library that provides multiple-precision integer functions required to form a
+basis of a public key cryptosystem. LibTomMath is written entire in portable ISO C source code and designed to have an
+application interface much like that of MPI from Michael Fromberger.
LibTomMath was written from scratch by Tom St Denis but designed to be drop in replacement for the MPI package. The
algorithms within the library are derived from descriptions as provided in the Handbook of Applied Cryptography and Knuth's
@@ -23,8 +23,7 @@ LibTomMath was designed with the following goals in mind:
\item Be written entirely in portable C.
\end{enumerate}
-All three goals have been achieved. Particularly the speed increase goal. For example, a 512-bit modular exponentiation
-is eight times faster\footnote{On an Athlon XP with GCC 3.2} with LibTomMath compared to MPI.
+All three goals have been achieved to one extent or another (actual figures depend on what platform you are using).
Being compatible with MPI means that applications that already use it can be ported fairly quickly. Currently there are
a few differences but there are many similarities. In fact the average MPI based application can be ported in under 15
@@ -54,16 +53,26 @@ make install
Now within your application include ``tommath.h'' and link against libtommath.a to get MPI-like functionality.
+\subsection{Microsoft Visual C++}
+A makefile is also provided for MSVC (\textit{tested against MSVC 6.00 with SP5}) which allows the library to be used
+with that compiler as well. To build the library type
+
+\begin{verbatim}
+nmake -f makefile.msvc
+\end{verbatim}
+
+Which will build ``tommath.lib''.
+
\section{Programming with LibTomMath}
\subsection{The mp\_int Structure}
All multiple precision integers are stored in a structure called \textbf{mp\_int}. A multiple precision integer is
-essentially an array of \textbf{mp\_digit}. mp\_digit is defined at the top of bn.h. Its type can be changed to suit
-a particular platform.
+essentially an array of \textbf{mp\_digit}. mp\_digit is defined at the top of ``tommath.h''. The type can be changed
+to suit a particular platform.
-For example, when \textbf{MP\_8BIT} is defined\footnote{When building bn.c.} a mp\_digit is a unsigned char and holds
-seven bits. Similarly when \textbf{MP\_16BIT} is defined a mp\_digit is a unsigned short and holds 15 bits.
-By default a mp\_digit is a unsigned long and holds 28 bits.
+For example, when \textbf{MP\_8BIT} is defined a mp\_digit is a unsigned char and holds seven bits. Similarly
+when \textbf{MP\_16BIT} is defined a mp\_digit is a unsigned short and holds 15 bits. By default a mp\_digit is a
+unsigned long and holds 28 bits which is optimal for most 32 and 64 bit processors.
The choice of digit is particular to the platform at hand and what available multipliers are provided. For
MP\_8BIT either a $8 \times 8 \Rightarrow 16$ or $16 \times 16 \Rightarrow 16$ multiplier is optimal. When
@@ -83,20 +92,19 @@ $W$ is the number of bits in a digit (default is 28).
\subsection{Calling Functions}
Most functions expect pointers to mp\_int's as parameters. To save on memory usage it is possible to have source
-variables as destinations. For example:
+variables as destinations. The arguements are read left to right so to compute $x + y = z$ you would pass the arguments
+in the order $x, y, z$. For example:
\begin{verbatim}
mp_add(&x, &y, &x); /* x = x + y */
- mp_mul(&x, &z, &x); /* x = x * z */
- mp_div_2(&x, &x); /* x = x / 2 */
+ mp_mul(&y, &x, &z); /* z = y * x */
+ mp_div_2(&x, &y); /* y = x / 2 */
\end{verbatim}
-\section{Quick Overview}
+\subsection{Return Values}
+All functions that return errors will return \textbf{MP\_OKAY} if the function was succesful. It will return
+\textbf{MP\_MEM} if it ran out of heap memory or \textbf{MP\_VAL} if one of the arguements is out of range.
\subsection{Basic Functionality}
-Essentially all LibTomMath functions return one of three values to indicate if the function worked as desired. A
-function will return \textbf{MP\_OKAY} if the function was successful. A function will return \textbf{MP\_MEM} if
-it ran out of memory and \textbf{MP\_VAL} if the input was invalid.
-
Before an mp\_int can be used it must be initialized with
\begin{verbatim}
@@ -106,7 +114,7 @@ int mp_init(mp_int *a);
For example, consider the following.
\begin{verbatim}
-#include "bn.h"
+#include "tommath.h"
int main(void)
{
mp_int num;
@@ -383,6 +391,18 @@ in $c$ and returns success.
This function requires $O(N)$ additional digits of memory and $O(2 \cdot N)$ time.
+\subsubsection{mp\_mul\_2(mp\_int *a, mp\_int *b)}
+Multiplies $a$ by two and stores in $b$. This function is hard coded todo a shift by one place so it is faster
+than calling mp\_mul\_2d with a count of one.
+
+This function requires $O(N)$ additional digits of memory and $O(N)$ time.
+
+\subsubsection{mp\_div\_2(mp\_int *a, mp\_int *b)}
+Divides $a$ by two and stores in $b$. This function is hard coded todo a shift by one place so it is faster
+than calling mp\_div\_2d with a count of one.
+
+This function requires $O(N)$ additional digits of memory and $O(N)$ time.
+
\subsubsection{mp\_mod\_2d(mp\_int *a, int b, mp\_int *c)}
Performs the action of reducing $a$ modulo $2^b$ and stores the result in $c$. If the shift count $b$ is less than
or equal to zero the function places $a$ in $c$ and returns success.
@@ -412,7 +432,7 @@ of $c$ is the maximum length of the two inputs.
\subsection{Basic Arithmetic}
\subsubsection{mp\_cmp(mp\_int *a, mp\_int *b)}
-Performs a \textbf{signed} comparison between $a$ and $b$ returning \textbf{MP\_GT} is $a$ is larger than $b$.
+Performs a \textbf{signed} comparison between $a$ and $b$ returning \textbf{MP\_GT} if $a$ is larger than $b$.
This function requires no additional memory and $O(N)$ time.
@@ -559,57 +579,6 @@ A very useful observation is that multiplying by $R = \beta^n$ amounts to perfor
requires no single precision multiplications.
\section{Timing Analysis}
-\subsection{Observed Timings}
-A simple test program ``demo.c'' was developed which builds with either MPI or LibTomMath (without modification). The
-test was conducted on an AMD Athlon XP processor with 266Mhz DDR memory and the GCC 3.2 compiler\footnote{With build
-options ``-O3 -fomit-frame-pointer -funroll-loops''}. The multiplications and squarings were repeated 100,000 times
-each while the modular exponentiation (exptmod) were performed 50 times each. The ``inversions'' refers to multiplicative
-inversions modulo an odd number of a given size. The RDTSC (Read Time Stamp Counter) instruction was used to measure the
-time the entire iterations took and was divided by the number of iterations to get an average. The following results
-were observed.
-
-\begin{small}
-\begin{center}
-\begin{tabular}{c|c|c|c}
-\hline \textbf{Operation} & \textbf{Size (bits)} & \textbf{Time with MPI (cycles)} & \textbf{Time with LibTomMath (cycles)} \\
-\hline
-Inversion & 128 & 264,083 & 59,782 \\
-Inversion & 256 & 549,370 & 146,915 \\
-Inversion & 512 & 1,675,975 & 367,172 \\
-Inversion & 1024 & 5,237,957 & 1,054,158 \\
-Inversion & 2048 & 17,871,944 & 3,459,683 \\
-Inversion & 4096 & 66,610,468 & 11,834,556 \\
-\hline
-Multiply & 128 & 1,426 & 451 \\
-Multiply & 256 & 2,551 & 958 \\
-Multiply & 512 & 7,913 & 2,476 \\
-Multiply & 1024 & 28,496 & 7,927 \\
-Multiply & 2048 & 109,897 & 28,224 \\
-Multiply & 4096 & 469,970 & 101,171 \\
-\hline
-Square & 128 & 1,319 & 511 \\
-Square & 256 & 1,776 & 947 \\
-Square & 512 & 5,399 & 2,153 \\
-Square & 1024 & 18,991 & 5,733 \\
-Square & 2048 & 72,126 & 17,621 \\
-Square & 4096 & 306,269 & 67,576 \\
-\hline
-Exptmod & 512 & 32,021,586 & 3,118,435 \\
-Exptmod & 768 & 97,595,492 & 8,493,633 \\
-Exptmod & 1024 & 223,302,532 & 17,715,899 \\
-Exptmod & 2048 & 1,682,223,369 & 114,936,361 \\
-Exptmod & 2560 & 3,268,615,571 & 229,402,426 \\
-Exptmod & 3072 & 5,597,240,141 & 367,403,840 \\
-Exptmod & 4096 & 13,347,270,891 & 779,058,433
-
-\end{tabular}
-\end{center}
-\end{small}
-
-Note that the figures do fluctuate but their magnitudes are relatively intact. The purpose of the chart is not to
-get an exact timing but to compare the two libraries. For example, in all of the tests the exact time for a 512-bit
-squaring operation was not the same. The observed times were all approximately 2,500 cycles, more importantly they
-were always faster than the timings observed with MPI by about the same magnitude.
\subsection{Digit Size}
The first major constribution to the time savings is the fact that 28 bits are stored per digit instead of the MPI
@@ -619,29 +588,59 @@ A savings of $64^2 - 37^2 = 2727$ single precision multiplications.
\subsection{Multiplication Algorithms}
For most inputs a typical baseline $O(n^2)$ multiplier is used which is similar to that of MPI. There are two variants
-of the baseline multiplier. The normal and the fast variants. The normal baseline multiplier is the exact same as the
-algorithm from MPI. The fast baseline multiplier is optimized for cases where the number of input digits $N$ is less
-than or equal to $2^{w}/\beta^2$. Where $w$ is the number of bits in a \textbf{mp\_word}. By default a mp\_word is
-64-bits which means $N \le 256$ is allowed which represents numbers upto $7168$ bits.
-
-The fast baseline multiplier is optimized by removing the carry operations from the inner loop. This is often referred
-to as the ``comba'' method since it computes the products a columns first then figures out the carries. This has the
-effect of making a very simple and paralizable inner loop.
-
-For large inputs, typically 80 digits\footnote{By default that is 2240-bits or more.} or more the Karatsuba method is
-used. This method has significant overhead but an asymptotic running time of $O(n^{1.584})$ which means for fairly large
-inputs this method is faster. The Karatsuba implementation is recursive which means for extremely large inputs they
-will benefit from the algorithm.
+of the baseline multiplier. The normal and the fast comba variant. The normal baseline multiplier is the exact same as
+the algorithm from MPI. The fast comba baseline multiplier is optimized for cases where the number of input digits $N$
+is less than or equal to $2^{w}/\beta^2$. Where $w$ is the number of bits in a \textbf{mp\_word} or simply $lg(\beta)$.
+By default a mp\_word is 64-bits which means $N \le 256$ is allowed which represents numbers upto $7,168$ bits. However,
+since the Karatsuba multiplier (discussed below) will kick in before that size the slower baseline algorithm (that MPI
+uses) should never really be used in a default configuration.
+
+The fast comba baseline multiplier is optimized by removing the carry operations from the inner loop. This is often
+referred to as the ``comba'' method since it computes the products a columns first then figures out the carries. To
+accomodate this the result of the inner multiplications must be stored in words large enough not to lose the carry bits.
+This is why there is a limit of $2^{w}/\beta^2$ digits in the input. This optimization has the effect of making a
+very simple and efficient inner loop.
+
+\subsubsection{Karatsuba Multiplier}
+For large inputs, typically 80 digits\footnote{By default that is 2240-bits or more.} or more the Karatsuba multiplication
+method is used. This method has significant overhead but an asymptotic running time of $O(n^{1.584})$ which means for
+fairly large inputs this method is faster than the baseline (or comba) algorithm. The Karatsuba implementation is
+recursive which means for extremely large inputs they will benefit from the algorithm.
+
+The algorithm is based on the observation that if
+
+\begin{eqnarray}
+x = x_0 + x_1\beta \nonumber \\
+y = y_0 + y_1\beta
+\end{eqnarray}
+
+Where $x_0, x_1, y_0, y_1$ are half the size of their respective summand than
+
+\begin{equation}
+x \cdot y = x_1y_1\beta^2 + ((x_1 - y_1)(x_0 - y_0) + x_0y_0 + x_1y_1)\beta + x_0y_0
+\end{equation}
+
+It is trivial that from this only three products have to be produced: $x_0y_0, x_1y_1, (x_1-y_1)(x_0-y_0)$ which
+are all of half size numbers. A multiplication of two half size numbers requires only $1 \over 4$ of the
+original work which means with no recursion the Karatsuba algorithm achieves a running time of ${3n^2}\over 4$.
+The routine provided does recursion which is where the $O(n^{1.584})$ work factor comes from.
+
+The multiplication by $\beta$ and $\beta^2$ amount to digit shift operations.
+The extra overhead in the Karatsuba method comes from extracting the half size numbers $x_0, x_1, y_0, y_1$ and
+performing the various smaller calculations.
+
+The library has been fairly optimized to extract the digits using hard-coded routines instead of the hire
+level functions however there is still significant overhead to optimize away.
MPI only implements the slower baseline multiplier where carries are dealt with in the inner loop. As a result even at
smaller numbers (below the Karatsuba cutoff) the LibTomMath multipliers are faster.
\subsection{Squaring Algorithms}
-Similar to the multiplication algorithms there are two baseline squaring algorithms. Both have an asymptotic running
-time of $O((t^2 + t)/2)$. The normal baseline squaring is the same from MPI and the fast is a ``comba'' squaring
-algorithm. The comba method is used if the number of digits $N$ is less than $2^{w-1}/\beta^2$ which by default
-covers numbers upto $3584$ bits.
+Similar to the multiplication algorithms there are two baseline squaring algorithms. Both have an asymptotic
+running time of $O((t^2 + t)/2)$. The normal baseline squaring is the same from MPI and the fast method is
+a ``comba'' squaring algorithm. The comba method is used if the number of digits $N$ is less than
+$2^{w-1}/\beta^2$ which by default covers numbers upto $3,584$ bits.
There is also a Karatsuba squaring method which achieves a running time of $O(n^{1.584})$ after considerably large
inputs.
@@ -653,25 +652,31 @@ than MPI is.
LibTomMath implements a sliding window $k$-ary left to right exponentiation algorithm. For a given exponent size $L$ an
appropriate window size $k$ is chosen. There are always at most $L$ modular squarings and $\lfloor L/k \rfloor$ modular
-multiplications. The $k$-ary method works by precomputing values $g(x) = b^x$ for $0 \le x < 2^k$ and a given base
+multiplications. The $k$-ary method works by precomputing values $g(x) = b^x$ for $2^{k-1} \le x < 2^k$ and a given base
$b$. Then the multiplications are grouped in windows of $k$ bits. The sliding window technique has the benefit
that it can skip multiplications if there are zero bits following or preceding a window. Consider the exponent
$e = 11110001_2$ if $k = 2$ then there will be a two squarings, a multiplication of $g(3)$, two squarings, a multiplication
-of $g(3)$, four squarings and and a multiplication by $g(1)$. In total there are 8 squarings and 3 multiplications.
+of $g(3)$, four squarings and and a multiplication by $g(1)$. In total there are 8 squarings and 3 multiplications.
-MPI uses a binary square-multiply method. For the same exponent $e$ it would have had 8 squarings and 5 multiplications.
-There is a precomputation phase for the method LibTomMath uses but it generally cuts down considerably on the number
-of multiplications. Consider a 512-bit exponent. The worst case for the LibTomMath method results in 512 squarings and
-124 multiplications. The MPI method would have 512 squarings and 512 multiplications. Randomly every $2k$ bits another
-multiplication is saved via the sliding-window technique on top of the savings the $k$-ary method provides.
+MPI uses a binary square-multiply method for exponentiation. For the same exponent $e = 11110001_2$ it would have had to
+perform 8 squarings and 5 multiplications. There is a precomputation phase for the method LibTomMath uses but it
+generally cuts down considerably on the number of multiplications. Consider a 512-bit exponent. The worst case for the
+LibTomMath method results in 512 squarings and 124 multiplications. The MPI method would have 512 squarings
+and 512 multiplications. Randomly every $2k$ bits another multiplication is saved via the sliding-window
+technique on top of the savings the $k$-ary method provides.
Both LibTomMath and MPI use Barrett reduction instead of division to reduce the numbers modulo the modulus given.
However, LibTomMath can take advantage of the fact that the multiplications required within the Barrett reduction
-do not have to give full precision. As a result the reduction step is much faster and just as accurate. The LibTomMath code
-will automatically determine at run-time (e.g. when its called) whether the faster multiplier can be used. The
+do not have to give full precision. As a result the reduction step is much faster and just as accurate. The LibTomMath
+code will automatically determine at run-time (e.g. when its called) whether the faster multiplier can be used. The
faster multipliers have also been optimized into the two variants (baseline and comba baseline).
LibTomMath also has a variant of the exptmod function that uses Montgomery reductions instead of Barrett reductions
-which is faser. As a result of all these changes exponentiation in LibTomMath is much faster than compared to MPI.
+which is faster. The code will automatically detect when the Montgomery version can be used (\textit{Requires the
+modulus to be odd and below the MONTGOMERY\_EXPT\_CUTOFF size}). The Montgomery routine is essentially a copy of the
+Barrett exponentiation routine except it uses Montgomery reduction.
+
+As a result of all these changes exponentiation in LibTomMath is much faster than compared to MPI. On most ALU-strong
+processors (AMD Athlon for instance) exponentiation in LibTomMath is often more then ten times faster than MPI.
\end{document}
diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c
index 249ff43..1cd0150 100644
--- a/bn_fast_mp_invmod.c
+++ b/bn_fast_mp_invmod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_fast_mp_montgomery_reduce.c b/bn_fast_mp_montgomery_reduce.c
index 2e03936..17be2e4 100644
--- a/bn_fast_mp_montgomery_reduce.c
+++ b/bn_fast_mp_montgomery_reduce.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -100,14 +100,18 @@ fast_mp_montgomery_reduce (mp_int * a, mp_int * m, mp_digit mp)
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
}
- /* nox fix rest of carries */
- for (++ix; ix <= m->used * 2 + 1; ix++) {
- W[ix] += (W[ix - 1] >> ((mp_word) DIGIT_BIT));
- }
{
register mp_digit *tmpa;
- register mp_word *_W;
+ register mp_word *_W, *_W1;
+
+ /* nox fix rest of carries */
+ _W1 = W + ix;
+ _W = W + ++ix;
+
+ for (; ix <= m->used * 2 + 1; ix++) {
+ *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
+ }
/* copy out, A = A/b^n
*
diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c
index dc0c33e..3cba3e1 100644
--- a/bn_fast_s_mp_mul_digs.c
+++ b/bn_fast_s_mp_mul_digs.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_fast_s_mp_mul_high_digs.c b/bn_fast_s_mp_mul_high_digs.c
index 3458d96..4a21441 100644
--- a/bn_fast_s_mp_mul_high_digs.c
+++ b/bn_fast_s_mp_mul_high_digs.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c
index 2b945ba..093bc89 100644
--- a/bn_fast_s_mp_sqr.c
+++ b/bn_fast_s_mp_sqr.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_2expt.c b/bn_mp_2expt.c
index 71d04e9..415aa1e 100644
--- a/bn_mp_2expt.c
+++ b/bn_mp_2expt.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_abs.c b/bn_mp_abs.c
index 9e6956e..60a3470 100644
--- a/bn_mp_abs.c
+++ b/bn_mp_abs.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_add.c b/bn_mp_add.c
index a8addfb..02f130a 100644
--- a/bn_mp_add.c
+++ b/bn_mp_add.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c
index 1b30fa4..0fe5ad3 100644
--- a/bn_mp_add_d.c
+++ b/bn_mp_add_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_addmod.c b/bn_mp_addmod.c
index abc3719..4ad89e0 100644
--- a/bn_mp_addmod.c
+++ b/bn_mp_addmod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_and.c b/bn_mp_and.c
index 6c05d68..8c18fdc 100644
--- a/bn_mp_and.c
+++ b/bn_mp_and.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_clamp.c b/bn_mp_clamp.c
index 3741f62..f2839a1 100644
--- a/bn_mp_clamp.c
+++ b/bn_mp_clamp.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_clear.c b/bn_mp_clear.c
index e88e95a..8273ac9 100644
--- a/bn_mp_clear.c
+++ b/bn_mp_clear.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_cmp.c b/bn_mp_cmp.c
index ca0c463..391eca3 100644
--- a/bn_mp_cmp.c
+++ b/bn_mp_cmp.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_cmp_d.c b/bn_mp_cmp_d.c
index 1506b87..f2725cf 100644
--- a/bn_mp_cmp_d.c
+++ b/bn_mp_cmp_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_cmp_mag.c b/bn_mp_cmp_mag.c
index 6d4a02d..a40b518 100644
--- a/bn_mp_cmp_mag.c
+++ b/bn_mp_cmp_mag.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_copy.c b/bn_mp_copy.c
index 68705a4..10ab6a6 100644
--- a/bn_mp_copy.c
+++ b/bn_mp_copy.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_count_bits.c b/bn_mp_count_bits.c
index 09992d4..3833ce6 100644
--- a/bn_mp_count_bits.c
+++ b/bn_mp_count_bits.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_div.c b/bn_mp_div.c
index 3954c9f..96e7e6f 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c
index 284f330..787f87a 100644
--- a/bn_mp_div_2.c
+++ b/bn_mp_div_2.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -46,6 +46,7 @@ mp_div_2 (mp_int * a, mp_int * b)
*tmpb++ = 0;
}
}
+ b->sign = a->sign;
mp_clamp (b);
return MP_OKAY;
}
diff --git a/bn_mp_div_2d.c b/bn_mp_div_2d.c
index 9d24e10..c208f5e 100644
--- a/bn_mp_div_2d.c
+++ b/bn_mp_div_2d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -51,7 +51,9 @@ mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
}
/* shift by as many digits in the bit count */
- mp_rshd (c, b / DIGIT_BIT);
+ if (b >= DIGIT_BIT) {
+ mp_rshd (c, b / DIGIT_BIT);
+ }
/* shift any bit count < DIGIT_BIT */
D = (mp_digit) (b % DIGIT_BIT);
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index e43802a..b7de4d1 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_exch.c b/bn_mp_exch.c
index 2ccaf9e..526c4c9 100644
--- a/bn_mp_exch.c
+++ b/bn_mp_exch.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_expt_d.c b/bn_mp_expt_d.c
index e5106be..602785b 100644
--- a/bn_mp_expt_d.c
+++ b/bn_mp_expt_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index ea58a4f..8b3f27f 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_exptmod_fast.c b/bn_mp_exptmod_fast.c
index fe0dfcb..902a894 100644
--- a/bn_mp_exptmod_fast.c
+++ b/bn_mp_exptmod_fast.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c
index 35b287e..df68841 100644
--- a/bn_mp_gcd.c
+++ b/bn_mp_gcd.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 91c1867..0a0a33b 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_init.c b/bn_mp_init.c
index 024bfd9..ae5c30f 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_init_copy.c b/bn_mp_init_copy.c
index f79d2b1..f87ad16 100644
--- a/bn_mp_init_copy.c
+++ b/bn_mp_init_copy.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index 4dc46b0..ce25b91 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_invmod.c b/bn_mp_invmod.c
index 006efd2..4e2c1f7 100644
--- a/bn_mp_invmod.c
+++ b/bn_mp_invmod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c
index 95aee42..bfe7bfc 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_karatsuba_mul.c b/bn_mp_karatsuba_mul.c
index bee8eaa..79358fb 100644
--- a/bn_mp_karatsuba_mul.c
+++ b/bn_mp_karatsuba_mul.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -37,8 +37,7 @@ int
mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
{
mp_int x0, x1, y0, y1, t1, t2, x0y0, x1y1;
- int B, err, x;
-
+ int B, err;
err = MP_MEM;
@@ -59,13 +58,13 @@ mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
goto Y0;
/* init temps */
- if (mp_init (&t1) != MP_OKAY)
+ if (mp_init_size (&t1, B * 2) != MP_OKAY)
goto Y1;
- if (mp_init (&t2) != MP_OKAY)
+ if (mp_init_size (&t2, B * 2) != MP_OKAY)
goto T1;
- if (mp_init (&x0y0) != MP_OKAY)
+ if (mp_init_size (&x0y0, B * 2) != MP_OKAY)
goto T2;
- if (mp_init (&x1y1) != MP_OKAY)
+ if (mp_init_size (&x1y1, B * 2) != MP_OKAY)
goto X0Y0;
/* now shift the digits */
@@ -76,18 +75,32 @@ mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
x1.used = a->used - B;
y1.used = b->used - B;
- /* we copy the digits directly instead of using higher level functions
- * since we also need to shift the digits
- */
- for (x = 0; x < B; x++) {
- x0.dp[x] = a->dp[x];
- y0.dp[x] = b->dp[x];
- }
- for (x = B; x < a->used; x++) {
- x1.dp[x - B] = a->dp[x];
- }
- for (x = B; x < b->used; x++) {
- y1.dp[x - B] = b->dp[x];
+ {
+ register int x;
+ register mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
+
+ /* we copy the digits directly instead of using higher level functions
+ * since we also need to shift the digits
+ */
+ tmpa = a->dp;
+ tmpb = b->dp;
+
+ tmpx = x0.dp;
+ tmpy = y0.dp;
+ for (x = 0; x < B; x++) {
+ *tmpx++ = *tmpa++;
+ *tmpy++ = *tmpb++;
+ }
+
+ tmpx = x1.dp;
+ for (x = B; x < a->used; x++) {
+ *tmpx++ = *tmpa++;
+ }
+
+ tmpy = y1.dp;
+ for (x = B; x < b->used; x++) {
+ *tmpy++ = *tmpb++;
+ }
}
/* only need to clamp the lower words since by definition the upper words x1/y1 must
diff --git a/bn_mp_karatsuba_sqr.c b/bn_mp_karatsuba_sqr.c
index 3078588..241b392 100644
--- a/bn_mp_karatsuba_sqr.c
+++ b/bn_mp_karatsuba_sqr.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -23,8 +23,7 @@ int
mp_karatsuba_sqr (mp_int * a, mp_int * b)
{
mp_int x0, x1, t1, t2, x0x0, x1x1;
- int B, err, x;
-
+ int B, err;
err = MP_MEM;
@@ -41,22 +40,31 @@ mp_karatsuba_sqr (mp_int * a, mp_int * b)
goto X0;
/* init temps */
- if (mp_init (&t1) != MP_OKAY)
+ if (mp_init_size (&t1, a->used * 2) != MP_OKAY)
goto X1;
- if (mp_init (&t2) != MP_OKAY)
+ if (mp_init_size (&t2, a->used * 2) != MP_OKAY)
goto T1;
- if (mp_init (&x0x0) != MP_OKAY)
+ if (mp_init_size (&x0x0, B * 2) != MP_OKAY)
goto T2;
- if (mp_init (&x1x1) != MP_OKAY)
+ if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY)
goto X0X0;
- /* now shift the digits */
- for (x = 0; x < B; x++) {
- x0.dp[x] = a->dp[x];
- }
+ {
+ register int x;
+ register mp_digit *dst, *src;
+
+ src = a->dp;
+
+ /* now shift the digits */
+ dst = x0.dp;
+ for (x = 0; x < B; x++) {
+ *dst++ = *src++;
+ }
- for (x = B; x < a->used; x++) {
- x1.dp[x - B] = a->dp[x];
+ dst = x1.dp;
+ for (x = B; x < a->used; x++) {
+ *dst++ = *src++;
+ }
}
x0.used = B;
@@ -77,7 +85,7 @@ mp_karatsuba_sqr (mp_int * a, mp_int * b)
goto X1X1; /* t1 = (x1 - x0) * (y1 - y0) */
/* add x0y0 */
- if (mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
+ if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
goto X1X1; /* t2 = x0y0 + x1y1 */
if (mp_sub (&t2, &t1, &t1) != MP_OKAY)
goto X1X1; /* t1 = x0y0 + x1y1 - (x1-x0)*(y1-y0) */
diff --git a/bn_mp_lcm.c b/bn_mp_lcm.c
index 60d5461..75b5829 100644
--- a/bn_mp_lcm.c
+++ b/bn_mp_lcm.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c
index 44b0588..6242957 100644
--- a/bn_mp_lshd.c
+++ b/bn_mp_lshd.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -31,16 +31,31 @@ mp_lshd (mp_int * a, int b)
return res;
}
- /* increment the used by the shift amount than copy upwards */
- a->used += b;
- for (x = a->used - 1; x >= b; x--) {
- a->dp[x] = a->dp[x - b];
- }
+ {
+ register mp_digit *tmpa, *tmpaa;
+
+ /* increment the used by the shift amount than copy upwards */
+ a->used += b;
+
+ /* top */
+ tmpa = a->dp + a->used - 1;
+
+ /* base */
+ tmpaa = a->dp + a->used - 1 - b;
+
+ /* much like mp_rshd this is implemented using a sliding window
+ * except the window goes the otherway around. Copying from
+ * the bottom to the top. see bn_mp_rshd.c for more info.
+ */
+ for (x = a->used - 1; x >= b; x--) {
+ *tmpa-- = *tmpaa--;
+ }
- /* zero the lower digits */
- for (x = 0; x < b; x++) {
- a->dp[x] = 0;
+ /* zero the lower digits */
+ tmpa = a->dp;
+ for (x = 0; x < b; x++) {
+ *tmpa++ = 0;
+ }
}
- mp_clamp (a);
return MP_OKAY;
}
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index c4a7374..e6027ce 100644
--- a/bn_mp_mod.c
+++ b/bn_mp_mod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_mod_2d.c b/bn_mp_mod_2d.c
index 4c6f1f1..fda6335 100644
--- a/bn_mp_mod_2d.c
+++ b/bn_mp_mod_2d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_mod_d.c b/bn_mp_mod_d.c
index b557381..42f3807 100644
--- a/bn_mp_mod_d.c
+++ b/bn_mp_mod_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_montgomery_calc_normalization.c b/bn_mp_montgomery_calc_normalization.c
index 06252de..b942eba 100644
--- a/bn_mp_montgomery_calc_normalization.c
+++ b/bn_mp_montgomery_calc_normalization.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index 586142a..e64435c 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_montgomery_setup.c b/bn_mp_montgomery_setup.c
index c739895..dfdc51a 100644
--- a/bn_mp_montgomery_setup.c
+++ b/bn_mp_montgomery_setup.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -18,36 +18,29 @@
int
mp_montgomery_setup (mp_int * a, mp_digit * mp)
{
- mp_int t, tt;
- int res;
+ unsigned long x, b;
- if ((res = mp_init (&t)) != MP_OKAY) {
- return res;
- }
+/* fast inversion mod 2^32
+ *
+ * Based on the fact that
+ *
+ * XA = 1 (mod 2^n) => (X(2-XA)) A = 1 (mod 2^2n)
+ * => 2*X*A - X*X*A*A = 1
+ * => 2*(1) - (1) = 1
+ */
+ b = a->dp[0];
- if ((res = mp_init (&tt)) != MP_OKAY) {
- goto __T;
+ if ((b & 1) == 0) {
+ return MP_VAL;
}
- /* tt = b */
- tt.dp[0] = 0;
- tt.dp[1] = 1;
- tt.used = 2;
-
- /* t = m mod b */
- t.dp[0] = a->dp[0];
- t.used = 1;
-
- /* t = 1/m mod b */
- if ((res = mp_invmod (&t, &tt, &t)) != MP_OKAY) {
- goto __TT;
- }
+ x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2^4 */
+ x *= 2 - b * x; /* here x*a==1 mod 2^8 */
+ x *= 2 - b * x; /* here x*a==1 mod 2^16; each step doubles the nb of bits */
+ x *= 2 - b * x; /* here x*a==1 mod 2^32 */
/* t = -1/m mod b */
- *mp = ((mp_digit) 1 << ((mp_digit) DIGIT_BIT)) - t.dp[0];
+ *mp = ((mp_digit) 1 << ((mp_digit) DIGIT_BIT)) - (x & MP_MASK);
- res = MP_OKAY;
-__TT:mp_clear (&tt);
-__T:mp_clear (&t);
- return res;
+ return MP_OKAY;
}
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index 064fc85..5ccd6e4 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_mul_2.c b/bn_mp_mul_2.c
index 8174d0a..fca7125 100644
--- a/bn_mp_mul_2.c
+++ b/bn_mp_mul_2.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -50,6 +50,11 @@ mp_mul_2 (mp_int * a, mp_int * b)
if ((res = mp_grow (b, b->used + 1)) != MP_OKAY) {
return res;
}
+
+ /* after the grow *tmpb is no longer valid so we have to reset it!
+ * (this bug took me about 17 minutes to find...!)
+ */
+ tmpb = b->dp + b->used;
}
/* add a MSB of 1 */
*tmpb = 1;
@@ -61,5 +66,6 @@ mp_mul_2 (mp_int * a, mp_int * b)
*tmpb++ = 0;
}
}
+ b->sign = a->sign;
return MP_OKAY;
}
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
index 97ee26c..faa9a7f 100644
--- a/bn_mp_mul_2d.c
+++ b/bn_mp_mul_2d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -32,9 +32,11 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
}
/* shift by as many digits in the bit count */
- if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
- return res;
- }
+ if (b >= DIGIT_BIT) {
+ if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
+ return res;
+ }
+ }
c->used = c->alloc;
/* shift any bit count < DIGIT_BIT */
diff --git a/bn_mp_mul_d.c b/bn_mp_mul_d.c
index 164bcac..f4458bb 100644
--- a/bn_mp_mul_d.c
+++ b/bn_mp_mul_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_mulmod.c b/bn_mp_mulmod.c
index abdf77b..2e3400a 100644
--- a/bn_mp_mulmod.c
+++ b/bn_mp_mulmod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_n_root.c b/bn_mp_n_root.c
index eb49b3f..1e12fee 100644
--- a/bn_mp_n_root.c
+++ b/bn_mp_n_root.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_neg.c b/bn_mp_neg.c
index fd2e497..73f948a 100644
--- a/bn_mp_neg.c
+++ b/bn_mp_neg.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_or.c b/bn_mp_or.c
index e821bac..466a171 100644
--- a/bn_mp_or.c
+++ b/bn_mp_or.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index dc13534..b944d57 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_read_signed_bin.c b/bn_mp_read_signed_bin.c
index 8a9df88..448e327 100644
--- a/bn_mp_read_signed_bin.c
+++ b/bn_mp_read_signed_bin.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c
index 16e2f29..378d1fa 100644
--- a/bn_mp_read_unsigned_bin.c
+++ b/bn_mp_read_unsigned_bin.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_reduce.c b/bn_mp_reduce.c
index 8f15458..5d85f42 100644
--- a/bn_mp_reduce.c
+++ b/bn_mp_reduce.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_rshd.c b/bn_mp_rshd.c
index 39e631e..ef1a6bf 100644
--- a/bn_mp_rshd.c
+++ b/bn_mp_rshd.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -20,7 +20,6 @@ mp_rshd (mp_int * a, int b)
{
int x;
-
/* if b <= 0 then ignore it */
if (b <= 0) {
return;
@@ -32,14 +31,34 @@ mp_rshd (mp_int * a, int b)
return;
}
- /* shift the digits down */
- for (x = 0; x < (a->used - b); x++) {
- a->dp[x] = a->dp[x + b];
- }
+ {
+ register mp_digit *tmpa, *tmpaa;
+
+ /* shift the digits down */
+
+ /* base */
+ tmpa = a->dp;
+
+ /* offset into digits */
+ tmpaa = a->dp + b;
+
+ /* this is implemented as a sliding window where the window is b-digits long
+ * and digits from the top of the window are copied to the bottom
+ *
+ * e.g.
+
+ b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
+ /\ | ---->
+ \-------------------/ ---->
+ */
+ for (x = 0; x < (a->used - b); x++) {
+ *tmpa++ = *tmpaa++;
+ }
- /* zero the top digits */
- for (; x < a->used; x++) {
- a->dp[x] = 0;
+ /* zero the top digits */
+ for (; x < a->used; x++) {
+ *tmpa++ = 0;
+ }
}
mp_clamp (a);
}
diff --git a/bn_mp_set.c b/bn_mp_set.c
index aeaf9cd..d749463 100644
--- a/bn_mp_set.c
+++ b/bn_mp_set.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_set_int.c b/bn_mp_set_int.c
index f22ab69..1d6bce7 100644
--- a/bn_mp_set_int.c
+++ b/bn_mp_set_int.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index 3cc506b..c3f1aa9 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_signed_bin_size.c b/bn_mp_signed_bin_size.c
index d50ae7a..fe8f499 100644
--- a/bn_mp_signed_bin_size.c
+++ b/bn_mp_signed_bin_size.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index c8b5cb7..99ebdf0 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_sqrmod.c b/bn_mp_sqrmod.c
index 44f608f..c32d562 100644
--- a/bn_mp_sqrmod.c
+++ b/bn_mp_sqrmod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_sub.c b/bn_mp_sub.c
index 1366c55..6558e5d 100644
--- a/bn_mp_sub.c
+++ b/bn_mp_sub.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index aebc414..7ca565e 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_submod.c b/bn_mp_submod.c
index 16fee71..1606ff0 100644
--- a/bn_mp_submod.c
+++ b/bn_mp_submod.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_to_signed_bin.c b/bn_mp_to_signed_bin.c
index 41abac1..023c80f 100644
--- a/bn_mp_to_signed_bin.c
+++ b/bn_mp_to_signed_bin.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index eec9f75..8f5eeb7 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_unsigned_bin_size.c b/bn_mp_unsigned_bin_size.c
index bee88e6..558efb0 100644
--- a/bn_mp_unsigned_bin_size.c
+++ b/bn_mp_unsigned_bin_size.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_xor.c b/bn_mp_xor.c
index 4a2ff9b..6622074 100644
--- a/bn_mp_xor.c
+++ b/bn_mp_xor.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_mp_zero.c b/bn_mp_zero.c
index 27ca5bd..c86cabd 100644
--- a/bn_mp_zero.c
+++ b/bn_mp_zero.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_radix.c b/bn_radix.c
index 205c148..1f06389 100644
--- a/bn_radix.c
+++ b/bn_radix.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_reverse.c b/bn_reverse.c
index 50109d7..c24aa27 100644
--- a/bn_reverse.c
+++ b/bn_reverse.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_s_mp_add.c b/bn_s_mp_add.c
index 328ec06..314db79 100644
--- a/bn_s_mp_add.c
+++ b/bn_s_mp_add.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
@@ -55,8 +55,14 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
register int i;
/* alias for digit pointers */
+
+ /* first input */
tmpa = a->dp;
+
+ /* second input */
tmpb = b->dp;
+
+ /* destination */
tmpc = c->dp;
u = 0;
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index f2b0d13..0243449 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index a43a593..ba52d11 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_s_mp_sqr.c b/bn_s_mp_sqr.c
index a0ec38b..fcb2767 100644
--- a/bn_s_mp_sqr.c
+++ b/bn_s_mp_sqr.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index fe15d23..a5683dd 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
diff --git a/bncore.c b/bncore.c
index 8863935..ba9fbf9 100644
--- a/bncore.c
+++ b/bncore.c
@@ -10,10 +10,13 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
-int KARATSUBA_MUL_CUTOFF = 80, /* Min. number of digits before Karatsuba multiplication is used. */
- KARATSUBA_SQR_CUTOFF = 80, /* Min. number of digits before Karatsuba squaring is used. */
- MONTGOMERY_EXPT_CUTOFF = 74; /* max. number of digits that montgomery reductions will help for */
+/* configured for a AMD Duron Morgan core with etc/tune.c */
+int KARATSUBA_MUL_CUTOFF = 73, /* Min. number of digits before Karatsuba multiplication is used. */
+ KARATSUBA_SQR_CUTOFF = 121, /* Min. number of digits before Karatsuba squaring is used. */
+ MONTGOMERY_EXPT_CUTOFF = 128; /* max. number of digits that montgomery reductions will help for */
+
+
diff --git a/changes.txt b/changes.txt
index c31526e..284d40e 100644
--- a/changes.txt
+++ b/changes.txt
@@ -1,3 +1,16 @@
+Mar 15th, 2003
+v0.14 -- Tons of manual updates
+ -- cleaned up the directory
+ -- added MSVC makefiles
+ -- source changes [that I don't recall]
+ -- Fixed up the lshd/rshd code to use pointer aliasing
+ -- Fixed up the mul_2d and div_2d to not call rshd/lshd unless needed
+ -- Fixed up etc/tune.c a tad
+ -- fixed up demo/demo.c to output comma-delimited results of timing
+ also fixed up timing demo to use a finer granularity for various functions
+ -- fixed up demo/demo.c testing to pause during testing so my Duron won't catch on fire
+ [stays around 31-35C during testing :-)]
+
Feb 13th, 2003
v0.13 -- tons of minor speed-ups in low level add, sub, mul_2 and div_2 which propagate
to other functions like mp_invmod, mp_div, etc...
diff --git a/demo/demo.c b/demo/demo.c
index 8bf9acd..0d79021 100644
--- a/demo/demo.c
+++ b/demo/demo.c
@@ -69,18 +69,32 @@ int mp_reduce_setup(mp_int *a, mp_int *b)
}
return mp_div(a, b, a, NULL);
}
+
+int mp_rand(mp_int *a, int c)
+{
+ long z = abs(rand()) & 65535;
+ mp_set(a, z?z:1);
+ while (c--) {
+ s_mp_lshd(a, 1);
+ mp_add_d(a, abs(rand()), a);
+ }
+ return MP_OKAY;
+}
#endif
char cmd[4096], buf[4096];
int main(void)
{
mp_int a, b, c, d, e, f;
- unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, inv_n;
+ unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, inv_n,
+ div2_n, mul2_n;
unsigned rr;
+ int cnt;
#ifdef TIMER
int n;
ulong64 tt;
+ FILE *log;
#endif
mp_init(&a);
@@ -90,60 +104,66 @@ int main(void)
mp_init(&e);
mp_init(&f);
-
#ifdef TIMER
-goto multtime;
-
printf("CLOCKS_PER_SEC == %lu\n", CLOCKS_PER_SEC);
- mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
- mp_read_radix(&b, "340282366920938463463574607431768211455", 10);
- while (a.used * DIGIT_BIT < 8192) {
+goto expttime;
+
+ log = fopen("add.log", "w");
+ for (cnt = 4; cnt <= 128; cnt += 4) {
+ mp_rand(&a, cnt);
+ mp_rand(&b, cnt);
reset();
for (rr = 0; rr < 10000000; rr++) {
mp_add(&a, &b, &c);
}
tt = rdtsc();
printf("Adding\t\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
- mp_sqr(&a, &a);
- mp_sqr(&b, &b);
+ fprintf(log, "%d,%9llu\n", cnt, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
}
+ fclose(log);
- mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
- mp_read_radix(&b, "340282366920938463463574607431768211455", 10);
- while (a.used * DIGIT_BIT < 8192) {
+ log = fopen("sub.log", "w");
+ for (cnt = 4; cnt <= 128; cnt += 4) {
+ mp_rand(&a, cnt);
+ mp_rand(&b, cnt);
reset();
for (rr = 0; rr < 10000000; rr++) {
mp_sub(&a, &b, &c);
}
tt = rdtsc();
- printf("Subtracting\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
- mp_sqr(&a, &a);
- mp_sqr(&b, &b);
+ printf("Subtracting\t\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
+ fprintf(log, "%d,%9llu\n", cnt, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
}
+ fclose(log);
multtime:
- mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
- while (a.used * DIGIT_BIT < 8192) {
+ log = fopen("sqr.log", "w");
+ for (cnt = 4; cnt <= 128; cnt += 4) {
+ mp_rand(&a, cnt);
reset();
for (rr = 0; rr < 250000; rr++) {
mp_sqr(&a, &b);
}
tt = rdtsc();
printf("Squaring\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
- mp_copy(&b, &a);
+ fprintf(log, "%d,%9llu\n", cnt, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
}
+ fclose(log);
- mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
- while (a.used * DIGIT_BIT < 8192) {
+ log = fopen("mult.log", "w");
+ for (cnt = 4; cnt <= 128; cnt += 4) {
+ mp_rand(&a, cnt);
+ mp_rand(&b, cnt);
reset();
for (rr = 0; rr < 250000; rr++) {
- mp_mul(&a, &a, &b);
+ mp_mul(&a, &b, &c);
}
tt = rdtsc();
printf("Multiplying\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
- mp_copy(&b, &a);
+ fprintf(log, "%d,%9llu\n", cnt, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
}
+ fclose(log);
expttime:
{
@@ -157,6 +177,7 @@ expttime:
"1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979",
NULL
};
+ log = fopen("expt.log", "w");
for (n = 0; primes[n]; n++) {
mp_read_radix(&a, primes[n], 10);
mp_zero(&b);
@@ -183,12 +204,21 @@ expttime:
exit(0);
}
printf("Exponentiating\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
+ fprintf(log, "%d,%9llu\n", cnt, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
}
}
-
- mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
- mp_read_radix(&b, "234892374891378913789237289378973232333", 10);
- while (a.used * DIGIT_BIT < 8192) {
+ fclose(log);
+invtime:
+ log = fopen("invmod.log", "w");
+ for (cnt = 4; cnt <= 128; cnt += 4) {
+ mp_rand(&a, cnt);
+ mp_rand(&b, cnt);
+
+ do {
+ mp_add_d(&b, 1, &b);
+ mp_gcd(&a, &b, &c);
+ } while (mp_cmp_d(&c, 1) != MP_EQ);
+
reset();
for (rr = 0; rr < 10000; rr++) {
mp_invmod(&b, &a, &c);
@@ -200,16 +230,18 @@ expttime:
return 0;
}
printf("Inverting mod\t%4d-bit => %9llu/sec, %9llu ticks\n", mp_count_bits(&a), (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt, tt);
- mp_sqr(&a, &a);
- mp_sqr(&b, &b);
+ fprintf(log, "%d,%9llu\n", cnt, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
}
+ fclose(log);
return 0;
#endif
- inv_n = expt_n = lcm_n = gcd_n = add_n = sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = 0;
+ div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n =
+ sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = 0;
for (;;) {
+ if (!(++cnt & 15)) sleep(3);
/* randomly clear and re-init one variable, this has the affect of triming the alloc space */
switch (abs(rand()) % 7) {
@@ -223,7 +255,7 @@ expttime:
}
- printf("%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%5d\r", add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, expt_n, inv_n, _ifuncs);
+ printf("%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu ", 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);
fgets(cmd, 4095, stdin);
cmd[strlen(cmd)-1] = 0;
printf("%s ]\r",cmd); fflush(stdout);
@@ -386,7 +418,29 @@ draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
- }
+ } else if (!strcmp(cmd, "div2")) { ++div2_n;
+ fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 10);
+ fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 10);
+ mp_div_2(&a, &c);
+ if (mp_cmp(&c, &b) != MP_EQ) {
+ printf("div_2 %lu failure\n", div2_n);
+ draw(&a);
+ draw(&b);
+ draw(&c);
+ return 0;
+ }
+ } else if (!strcmp(cmd, "mul2")) { ++mul2_n;
+ fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 10);
+ fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 10);
+ mp_mul_2(&a, &c);
+ if (mp_cmp(&c, &b) != MP_EQ) {
+ printf("mul_2 %lu failure\n", mul2_n);
+ draw(&a);
+ draw(&b);
+ draw(&c);
+ return 0;
+ }
+ }
}
return 0;
diff --git a/etc/makefile b/etc/makefile
index bf4befb..81f692c 100644
--- a/etc/makefile
+++ b/etc/makefile
@@ -17,4 +17,4 @@ mersenne: mersenne.o
$(CC) mersenne.o $(LIBNAME) -o mersenne
clean:
- rm -f *.o *.exe pprime tune mersenne
\ No newline at end of file
+ rm -f *.log *.o *.obj *.exe pprime tune mersenne
\ No newline at end of file
diff --git a/etc/makefile.msvc b/etc/makefile.msvc
new file mode 100644
index 0000000..6011cf3
--- /dev/null
+++ b/etc/makefile.msvc
@@ -0,0 +1,14 @@
+#MSVC Makefile
+#
+#Tom St Denis
+
+CFLAGS = /I../ /Ogityb2 /Gs /DWIN32 /W3
+
+pprime: pprime.obj
+ cl pprime.obj ../tommath.lib
+
+mersenne: mersenne.obj
+ cl mersenne.obj ../tommath.lib
+
+tune: tune.obj
+ cl tune.obj ../tommath.lib
\ No newline at end of file
diff --git a/etc/mersenne.c b/etc/mersenne.c
index 04b2d98..fa6a856 100644
--- a/etc/mersenne.c
+++ b/etc/mersenne.c
@@ -3,14 +3,14 @@
* Tom St Denis, tomstdenis@iahu.ca
*/
#include <time.h>
-#include <bn.h>
+#include <tommath.h>
int
is_mersenne (long s, int *pp)
{
- mp_int n, u, mu;
- int res, k;
- long ss;
+ mp_int n, u, mu;
+ int res, k;
+ long ss;
*pp = 0;
@@ -85,7 +85,7 @@ __N:mp_clear (&n);
long
i_sqrt (long x)
{
- long x1, x2;
+ long x1, x2;
x2 = 16;
do {
@@ -104,7 +104,7 @@ i_sqrt (long x)
int
isprime (long k)
{
- long y, z;
+ long y, z;
y = i_sqrt (k);
for (z = 2; z <= y; z++) {
@@ -118,9 +118,9 @@ isprime (long k)
int
main (void)
{
- int pp;
- long k;
- clock_t tt;
+ int pp;
+ long k;
+ clock_t tt;
k = 3;
diff --git a/etc/pprime.c b/etc/pprime.c
index 6fea3da..6285924 100644
--- a/etc/pprime.c
+++ b/etc/pprime.c
@@ -8,10 +8,10 @@
#include "tommath.h"
/* fast square root */
-static mp_digit
+static mp_digit
i_sqrt (mp_word x)
{
- mp_word x1, x2;
+ mp_word x1, x2;
x2 = x;
do {
@@ -28,10 +28,10 @@ i_sqrt (mp_word x)
/* generates a prime digit */
-static mp_digit
+static mp_digit
prime_digit ()
{
- mp_digit r, x, y, next;
+ mp_digit r, x, y, next;
/* make a DIGIT_BIT-bit random number */
for (r = x = 0; x < DIGIT_BIT; x++) {
@@ -141,8 +141,8 @@ prime_digit ()
int
pprime (int k, int li, mp_int * p, mp_int * q)
{
- mp_int a, b, c, n, x, y, z, v;
- int res, ii;
+ mp_int a, b, c, n, x, y, z, v;
+ int res, ii;
static const mp_digit bases[] = { 2, 3, 5, 7, 11, 13, 17, 19 };
/* single digit ? */
@@ -329,10 +329,10 @@ __C:mp_clear (&c);
int
main (void)
{
- mp_int p, q;
- char buf[4096];
- int k, li;
- clock_t t1;
+ mp_int p, q;
+ char buf[4096];
+ int k, li;
+ clock_t t1;
srand (time (NULL));
diff --git a/etc/tune.c b/etc/tune.c
index 73a44b7..f50edab 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -8,19 +8,19 @@
clock_t
time_mult (void)
{
- clock_t t1;
- int x, y;
- mp_int a, b, c;
+ clock_t t1;
+ int x, y;
+ mp_int a, b, c;
mp_init (&a);
mp_init (&b);
mp_init (&c);
t1 = clock ();
- for (x = 8; x <= 128; x += 8) {
- for (y = 0; y < 1000; y++) {
- mp_rand (&a, x);
- mp_rand (&b, x);
+ for (x = 4; x <= 128; x += 4) {
+ mp_rand (&a, x);
+ mp_rand (&b, x);
+ for (y = 0; y < 10000; y++) {
mp_mul (&a, &b, &c);
}
}
@@ -33,17 +33,17 @@ time_mult (void)
clock_t
time_sqr (void)
{
- clock_t t1;
- int x, y;
- mp_int a, b;
+ clock_t t1;
+ int x, y;
+ mp_int a, b;
mp_init (&a);
mp_init (&b);
t1 = clock ();
- for (x = 8; x <= 128; x += 8) {
- for (y = 0; y < 1000; y++) {
- mp_rand (&a, x);
+ for (x = 4; x <= 128; x += 4) {
+ mp_rand (&a, x);
+ for (y = 0; y < 10000; y++) {
mp_sqr (&a, &b);
}
}
@@ -52,20 +52,54 @@ time_sqr (void)
return clock () - t1;
}
+clock_t
+time_expt (void)
+{
+ clock_t t1;
+ int x, y;
+ mp_int a, b, c, d;
+
+ mp_init (&a);
+ mp_init (&b);
+ mp_init (&c);
+ mp_init (&d);
+
+ t1 = clock ();
+ for (x = 4; x <= 128; x += 4) {
+ mp_rand (&a, x);
+ mp_rand (&b, x);
+ mp_rand (&c, x);
+ if (mp_iseven (&c) != 0) {
+ mp_add_d (&c, 1, &c);
+ }
+ for (y = 0; y < 10; y++) {
+ mp_exptmod (&a, &b, &c, &d);
+ }
+ }
+ mp_clear (&d);
+ mp_clear (&c);
+ mp_clear (&b);
+ mp_clear (&a);
+
+ return clock () - t1;
+}
+
int
main (void)
{
- int best_mult, best_square;
- clock_t best, ti;
+ int best_mult, best_square, best_exptmod;
+ clock_t best, ti;
+ FILE *log;
- best_mult = best_square = 0;
+ best_mult = best_square = best_exptmod = 0;
/* tune multiplication first */
+ log = fopen ("mult.log", "w");
best = CLOCKS_PER_SEC * 1000;
- for (KARATSUBA_MUL_CUTOFF = 8; KARATSUBA_MUL_CUTOFF <= 128;
- KARATSUBA_MUL_CUTOFF++) {
+ for (KARATSUBA_MUL_CUTOFF = 8; KARATSUBA_MUL_CUTOFF <= 128; KARATSUBA_MUL_CUTOFF++) {
ti = time_mult ();
printf ("%4d : %9lu\r", KARATSUBA_MUL_CUTOFF, ti);
+ fprintf (log, "%d, %lu\n", KARATSUBA_MUL_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
printf ("New best: %lu, %d \n", ti, KARATSUBA_MUL_CUTOFF);
@@ -73,13 +107,15 @@ main (void)
best_mult = KARATSUBA_MUL_CUTOFF;
}
}
+ fclose (log);
/* tune squaring */
+ log = fopen ("sqr.log", "w");
best = CLOCKS_PER_SEC * 1000;
- for (KARATSUBA_SQR_CUTOFF = 8; KARATSUBA_SQR_CUTOFF <= 128;
- KARATSUBA_SQR_CUTOFF++) {
+ for (KARATSUBA_SQR_CUTOFF = 8; KARATSUBA_SQR_CUTOFF <= 128; KARATSUBA_SQR_CUTOFF++) {
ti = time_sqr ();
printf ("%4d : %9lu\r", KARATSUBA_SQR_CUTOFF, ti);
+ fprintf (log, "%d, %lu\n", KARATSUBA_SQR_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
printf ("New best: %lu, %d \n", ti, KARATSUBA_SQR_CUTOFF);
@@ -87,10 +123,30 @@ main (void)
best_square = KARATSUBA_SQR_CUTOFF;
}
}
+ fclose (log);
+
+ /* tune exptmod */
+ KARATSUBA_MUL_CUTOFF = best_mult;
+ KARATSUBA_SQR_CUTOFF = best_square;
+
+ log = fopen ("expt.log", "w");
+ best = CLOCKS_PER_SEC * 1000;
+ for (MONTGOMERY_EXPT_CUTOFF = 8; MONTGOMERY_EXPT_CUTOFF <= 192; MONTGOMERY_EXPT_CUTOFF++) {
+ ti = time_expt ();
+ printf ("%4d : %9lu\r", MONTGOMERY_EXPT_CUTOFF, ti);
+ fflush (stdout);
+ fprintf (log, "%d : %lu\r", MONTGOMERY_EXPT_CUTOFF, ti);
+ if (ti < best) {
+ printf ("New best: %lu, %d\n", ti, MONTGOMERY_EXPT_CUTOFF);
+ best = ti;
+ best_exptmod = MONTGOMERY_EXPT_CUTOFF;
+ }
+ }
+ fclose (log);
printf
- ("\n\n\nKaratsuba Multiplier Cutoff: %d\nKaratsuba Squaring Cutoff: %d\n",
- best_mult, best_square);
+ ("\n\n\nKaratsuba Multiplier Cutoff: %d\nKaratsuba Squaring Cutoff: %d\nMontgomery exptmod Cutoff: %d\n",
+ best_mult, best_square, best_exptmod);
return 0;
}
diff --git a/makefile b/makefile
index 9e6127d..856274b 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,6 @@
CFLAGS += -I./ -Wall -W -Wshadow -O3 -fomit-frame-pointer -funroll-loops
-VERSION=0.13
+VERSION=0.14
default: libtommath.a
@@ -60,7 +60,7 @@ docs: docdvi
rm -f bn.log bn.aux bn.dvi
clean:
- rm -f *.pdf *.o *.a *.exe etclib/*.o demo/demo.o test ltmtest mpitest mtest/mtest mtest/mtest.exe \
+ rm -f *.pdf *.o *.a *.obj *.lib *.exe etclib/*.o demo/demo.o test ltmtest mpitest mtest/mtest mtest/mtest.exe \
bn.log bn.aux bn.dvi *.log *.s mpi.c
cd etc ; make clean
diff --git a/makefile.msvc b/makefile.msvc
new file mode 100644
index 0000000..7c5f763
--- /dev/null
+++ b/makefile.msvc
@@ -0,0 +1,26 @@
+#MSVC Makefile
+#
+#Tom St Denis
+
+CFLAGS = /I. /Ogityb2 /Gs /DWIN32 /W3
+
+default: library
+
+OBJECTS=bncore.obj bn_mp_init.obj bn_mp_clear.obj bn_mp_exch.obj bn_mp_grow.obj bn_mp_shrink.obj \
+bn_mp_clamp.obj bn_mp_zero.obj bn_mp_set.obj bn_mp_set_int.obj bn_mp_init_size.obj bn_mp_copy.obj \
+bn_mp_init_copy.obj bn_mp_abs.obj bn_mp_neg.obj bn_mp_cmp_mag.obj bn_mp_cmp.obj bn_mp_cmp_d.obj \
+bn_mp_rshd.obj bn_mp_lshd.obj bn_mp_mod_2d.obj bn_mp_div_2d.obj bn_mp_mul_2d.obj bn_mp_div_2.obj \
+bn_mp_mul_2.obj bn_s_mp_add.obj bn_s_mp_sub.obj bn_fast_s_mp_mul_digs.obj bn_s_mp_mul_digs.obj \
+bn_fast_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs.obj bn_fast_s_mp_sqr.obj bn_s_mp_sqr.obj \
+bn_mp_add.obj bn_mp_sub.obj bn_mp_karatsuba_mul.obj bn_mp_mul.obj bn_mp_karatsuba_sqr.obj \
+bn_mp_sqr.obj bn_mp_div.obj bn_mp_mod.obj bn_mp_add_d.obj bn_mp_sub_d.obj bn_mp_mul_d.obj \
+bn_mp_div_d.obj bn_mp_mod_d.obj bn_mp_expt_d.obj bn_mp_addmod.obj bn_mp_submod.obj \
+bn_mp_mulmod.obj bn_mp_sqrmod.obj bn_mp_gcd.obj bn_mp_lcm.obj bn_fast_mp_invmod.obj bn_mp_invmod.obj \
+bn_mp_reduce.obj bn_mp_montgomery_setup.obj bn_fast_mp_montgomery_reduce.obj bn_mp_montgomery_reduce.obj \
+bn_mp_exptmod_fast.obj bn_mp_exptmod.obj bn_mp_2expt.obj bn_mp_n_root.obj bn_mp_jacobi.obj bn_reverse.obj \
+bn_mp_count_bits.obj bn_mp_read_unsigned_bin.obj bn_mp_read_signed_bin.obj bn_mp_to_unsigned_bin.obj \
+bn_mp_to_signed_bin.obj bn_mp_unsigned_bin_size.obj bn_mp_signed_bin_size.obj bn_radix.obj \
+bn_mp_xor.obj bn_mp_and.obj bn_mp_or.obj bn_mp_rand.obj bn_mp_montgomery_calc_normalization.obj
+
+library: $(OBJECTS)
+ lib /out:tommath.lib $(OBJECTS)
diff --git a/mtest/mtest.c b/mtest/mtest.c
index 3759d15..245c0d7 100644
--- a/mtest/mtest.c
+++ b/mtest/mtest.c
@@ -41,7 +41,7 @@ void rand_num(mp_int *a)
unsigned char buf[512];
top:
- size = 1 + ((fgetc(rng)*fgetc(rng)) % 96);
+ size = 1 + ((fgetc(rng)*fgetc(rng)) % 512);
buf[0] = (fgetc(rng)&1)?1:0;
fread(buf+1, 1, size, rng);
for (n = 0; n < size; n++) {
@@ -57,7 +57,7 @@ void rand_num2(mp_int *a)
unsigned char buf[512];
top:
- size = 1 + ((fgetc(rng)*fgetc(rng)) % 96);
+ size = 1 + ((fgetc(rng)*fgetc(rng)) % 512);
buf[0] = (fgetc(rng)&1)?1:0;
fread(buf+1, 1, size, rng);
for (n = 0; n < size; n++) {
@@ -72,6 +72,8 @@ int main(void)
int n;
mp_int a, b, c, d, e;
char buf[4096];
+
+ static int tests[] = { 11, 12 };
mp_init(&a);
mp_init(&b);
@@ -89,7 +91,7 @@ int main(void)
}
for (;;) {
- n = 4; // fgetc(rng) % 11;
+ n = fgetc(rng) % 13;
if (n == 0) {
/* add tests */
@@ -235,7 +237,24 @@ int main(void)
printf("%s\n", buf);
mp_todecimal(&c, buf);
printf("%s\n", buf);
- }
+ } else if (n == 11) {
+ rand_num(&a);
+ mp_mul_2(&a, &a);
+ mp_div_2(&a, &b);
+ printf("div2\n");
+ mp_todecimal(&a, buf);
+ printf("%s\n", buf);
+ mp_todecimal(&b, buf);
+ printf("%s\n", buf);
+ } else if (n == 12) {
+ rand_num2(&a);
+ mp_mul_2(&a, &b);
+ printf("mul2\n");
+ mp_todecimal(&a, buf);
+ printf("%s\n", buf);
+ mp_todecimal(&b, buf);
+ printf("%s\n", buf);
+ }
}
fclose(rng);
return 0;
diff --git a/timings.txt b/timings.txt
deleted file mode 100644
index 128649e..0000000
--- a/timings.txt
+++ /dev/null
@@ -1,36 +0,0 @@
-CLOCKS_PER_SEC == 1000
-Adding 128-bit => 14534883/sec, 688 ticks
-Adding 256-bit => 11037527/sec, 906 ticks
-Adding 512-bit => 8650519/sec, 1156 ticks
-Adding 1024-bit => 5871990/sec, 1703 ticks
-Adding 2048-bit => 3575259/sec, 2797 ticks
-Adding 4096-bit => 2018978/sec, 4953 ticks
-Subtracting 128-bit => 11025358/sec, 907 ticks
-Subtracting 256-bit => 9149130/sec, 1093 ticks
-Subtracting 512-bit => 7440476/sec, 1344 ticks
-Subtracting 1024-bit => 5078720/sec, 1969 ticks
-Subtracting 2048-bit => 3168567/sec, 3156 ticks
-Subtracting 4096-bit => 1833852/sec, 5453 ticks
-Squaring 128-bit => 3205128/sec, 78 ticks
-Squaring 256-bit => 1592356/sec, 157 ticks
-Squaring 512-bit => 696378/sec, 359 ticks
-Squaring 1024-bit => 266808/sec, 937 ticks
-Squaring 2048-bit => 85999/sec, 2907 ticks
-Squaring 4096-bit => 21949/sec, 11390 ticks
-Multiplying 128-bit => 3205128/sec, 78 ticks
-Multiplying 256-bit => 1592356/sec, 157 ticks
-Multiplying 512-bit => 615763/sec, 406 ticks
-Multiplying 1024-bit => 192752/sec, 1297 ticks
-Multiplying 2048-bit => 53510/sec, 4672 ticks
-Multiplying 4096-bit => 14801/sec, 16890 ticks
-Exponentiating 513-bit => 531/sec, 47 ticks
-Exponentiating 769-bit => 177/sec, 141 ticks
-Exponentiating 1025-bit => 88/sec, 282 ticks
-Exponentiating 2049-bit => 13/sec, 1890 ticks
-Exponentiating 2561-bit => 6/sec, 3812 ticks
-Exponentiating 3073-bit => 4/sec, 6031 ticks
-Exponentiating 4097-bit => 1/sec, 12843 ticks
-Inverting mod 128-bit => 19160/sec, 5219 ticks
-Inverting mod 256-bit => 8290/sec, 12062 ticks
-Inverting mod 512-bit => 3565/sec, 28047 ticks
-Inverting mod 1024-bit => 1305/sec, 76594 ticks
\ No newline at end of file
diff --git a/timings2.txt b/timings2.txt
deleted file mode 100644
index 0b87e21..0000000
--- a/timings2.txt
+++ /dev/null
@@ -1,36 +0,0 @@
-CLOCKS_PER_SEC == 1000
-Adding 128-bit => 15600624/sec, 641 ticks
-Adding 256-bit => 12804097/sec, 781 ticks
-Adding 512-bit => 10000000/sec, 1000 ticks
-Adding 1024-bit => 7032348/sec, 1422 ticks
-Adding 2048-bit => 4076640/sec, 2453 ticks
-Adding 4096-bit => 2424242/sec, 4125 ticks
-Subtracting 128-bit => 10845986/sec, 922 ticks
-Subtracting 256-bit => 9416195/sec, 1062 ticks
-Subtracting 512-bit => 7710100/sec, 1297 ticks
-Subtracting 1024-bit => 5159958/sec, 1938 ticks
-Subtracting 2048-bit => 3299241/sec, 3031 ticks
-Subtracting 4096-bit => 1987676/sec, 5031 ticks
-Squaring 128-bit => 3205128/sec, 78 ticks
-Squaring 256-bit => 1592356/sec, 157 ticks
-Squaring 512-bit => 696378/sec, 359 ticks
-Squaring 1024-bit => 266524/sec, 938 ticks
-Squaring 2048-bit => 86505/sec, 2890 ticks
-Squaring 4096-bit => 22471/sec, 11125 ticks
-Multiplying 128-bit => 3205128/sec, 78 ticks
-Multiplying 256-bit => 1592356/sec, 157 ticks
-Multiplying 512-bit => 615763/sec, 406 ticks
-Multiplying 1024-bit => 190548/sec, 1312 ticks
-Multiplying 2048-bit => 54418/sec, 4594 ticks
-Multiplying 4096-bit => 14897/sec, 16781 ticks
-Exponentiating 513-bit => 531/sec, 47 ticks
-Exponentiating 769-bit => 177/sec, 141 ticks
-Exponentiating 1025-bit => 84/sec, 297 ticks
-Exponentiating 2049-bit => 13/sec, 1875 ticks
-Exponentiating 2561-bit => 6/sec, 3766 ticks
-Exponentiating 3073-bit => 4/sec, 6000 ticks
-Exponentiating 4097-bit => 1/sec, 12750 ticks
-Inverting mod 128-bit => 17301/sec, 578 ticks
-Inverting mod 256-bit => 8103/sec, 1234 ticks
-Inverting mod 512-bit => 3422/sec, 2922 ticks
-Inverting mod 1024-bit => 1330/sec, 7516 ticks
\ No newline at end of file
diff --git a/timings3.txt b/timings3.txt
deleted file mode 100644
index f269c2b..0000000
--- a/timings3.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-Exponentiating 513-bit => 531/sec, 94 ticks
-Exponentiating 769-bit => 187/sec, 266 ticks
-Exponentiating 1025-bit => 88/sec, 562 ticks
-Exponentiating 2049-bit => 13/sec, 3719 ticks
-
diff --git a/tommath.h b/tommath.h
index 9db1781..eb8a488 100644
--- a/tommath.h
+++ b/tommath.h
@@ -10,7 +10,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://libtommath.iahu.ca
+ * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#ifndef BN_H_
#define BN_H_