added libtommath-0.39
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 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381
diff --git a/bn.pdf b/bn.pdf
index 79f4d4b..392b649 100644
Binary files a/bn.pdf and b/bn.pdf differ
diff --git a/bn.tex b/bn.tex
index 430a599..e8eb994 100644
--- a/bn.tex
+++ b/bn.tex
@@ -49,7 +49,7 @@
\begin{document}
\frontmatter
\pagestyle{empty}
-\title{LibTomMath User Manual \\ v0.38}
+\title{LibTomMath User Manual \\ v0.39}
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
diff --git a/bn_error.c b/bn_error.c
index f58387d..655d48f 100644
--- a/bn_error.c
+++ b/bn_error.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
static const struct {
diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c
index aa89dd7..73ff994 100644
--- a/bn_fast_mp_invmod.c
+++ b/bn_fast_mp_invmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes the modular inverse via binary extended euclidean algorithm,
diff --git a/bn_fast_mp_montgomery_reduce.c b/bn_fast_mp_montgomery_reduce.c
index 518f2d4..b768048 100644
--- a/bn_fast_mp_montgomery_reduce.c
+++ b/bn_fast_mp_montgomery_reduce.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes xR**-1 == x (mod N) via Montgomery Reduction
diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c
index 9ca3cfe..52887bb 100644
--- a/bn_fast_s_mp_mul_digs.c
+++ b/bn_fast_s_mp_mul_digs.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Fast (comba) multiplier
diff --git a/bn_fast_s_mp_mul_high_digs.c b/bn_fast_s_mp_mul_high_digs.c
index 0c6e839..31a7bac 100644
--- a/bn_fast_s_mp_mul_high_digs.c
+++ b/bn_fast_s_mp_mul_high_digs.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* this is a modified version of fast_s_mul_digs that only produces
diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c
index 7a5642c..28517c4 100644
--- a/bn_fast_s_mp_sqr.c
+++ b/bn_fast_s_mp_sqr.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* the jist of squaring...
diff --git a/bn_mp_2expt.c b/bn_mp_2expt.c
index e24cae4..782779f 100644
--- a/bn_mp_2expt.c
+++ b/bn_mp_2expt.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes a = 2**b
diff --git a/bn_mp_abs.c b/bn_mp_abs.c
index f8d50d8..30028a3 100644
--- a/bn_mp_abs.c
+++ b/bn_mp_abs.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = |a|
diff --git a/bn_mp_add.c b/bn_mp_add.c
index bfd827b..92ea18f 100644
--- a/bn_mp_add.c
+++ b/bn_mp_add.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* high level addition (handles signs) */
diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c
index 18026d7..4b0a9f1 100644
--- a/bn_mp_add_d.c
+++ b/bn_mp_add_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* single digit addition */
diff --git a/bn_mp_addmod.c b/bn_mp_addmod.c
index d1d07d8..aafafcf 100644
--- a/bn_mp_addmod.c
+++ b/bn_mp_addmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* d = a + b (mod c) */
diff --git a/bn_mp_and.c b/bn_mp_and.c
index cb8e54c..7f9e2fd 100644
--- a/bn_mp_and.c
+++ b/bn_mp_and.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* AND two ints together */
diff --git a/bn_mp_clamp.c b/bn_mp_clamp.c
index 3fc6b4d..64e98b9 100644
--- a/bn_mp_clamp.c
+++ b/bn_mp_clamp.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* trim unused digits
diff --git a/bn_mp_clear.c b/bn_mp_clear.c
index 46aa421..76701bb 100644
--- a/bn_mp_clear.c
+++ b/bn_mp_clear.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* clear one (frees) */
diff --git a/bn_mp_clear_multi.c b/bn_mp_clear_multi.c
index 2fdf125..3cefd8d 100644
--- a/bn_mp_clear_multi.c
+++ b/bn_mp_clear_multi.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#include <stdarg.h>
diff --git a/bn_mp_cmp.c b/bn_mp_cmp.c
index 2348066..a37fe82 100644
--- a/bn_mp_cmp.c
+++ b/bn_mp_cmp.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* compare two ints (signed)*/
diff --git a/bn_mp_cmp_d.c b/bn_mp_cmp_d.c
index 3843911..1498da5 100644
--- a/bn_mp_cmp_d.c
+++ b/bn_mp_cmp_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* compare a digit */
diff --git a/bn_mp_cmp_mag.c b/bn_mp_cmp_mag.c
index 45e9c6b..484ac3f 100644
--- a/bn_mp_cmp_mag.c
+++ b/bn_mp_cmp_mag.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* compare maginitude of two ints (unsigned) */
diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c
index 03d694b..d3be7b5 100644
--- a/bn_mp_cnt_lsb.c
+++ b/bn_mp_cnt_lsb.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
static const int lnz[16] = {
diff --git a/bn_mp_copy.c b/bn_mp_copy.c
index 701b489..265da4b 100644
--- a/bn_mp_copy.c
+++ b/bn_mp_copy.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* copy, b = a */
diff --git a/bn_mp_count_bits.c b/bn_mp_count_bits.c
index 52a9907..60b3adf 100644
--- a/bn_mp_count_bits.c
+++ b/bn_mp_count_bits.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* returns the number of bits in an int */
diff --git a/bn_mp_div.c b/bn_mp_div.c
index f031f53..a9f9732 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#ifdef BN_MP_DIV_SMALL
diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c
index 4566580..434fb5e 100644
--- a/bn_mp_div_2.c
+++ b/bn_mp_div_2.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = a/2 */
diff --git a/bn_mp_div_2d.c b/bn_mp_div_2d.c
index 4c9bbdd..84961a8 100644
--- a/bn_mp_div_2d.c
+++ b/bn_mp_div_2d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
diff --git a/bn_mp_div_3.c b/bn_mp_div_3.c
index f386109..85000e2 100644
--- a/bn_mp_div_3.c
+++ b/bn_mp_div_3.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* divide by three (based on routine from MPI and the GMP manual) */
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index e93bfda..baa9284 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
static int s_is_power_of_two(mp_digit b, int *p)
diff --git a/bn_mp_dr_is_modulus.c b/bn_mp_dr_is_modulus.c
index 6dfd9b6..82b4239 100644
--- a/bn_mp_dr_is_modulus.c
+++ b/bn_mp_dr_is_modulus.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if a number is a valid DR modulus */
diff --git a/bn_mp_dr_reduce.c b/bn_mp_dr_reduce.c
index 988c08e..e56effd 100644
--- a/bn_mp_dr_reduce.c
+++ b/bn_mp_dr_reduce.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
diff --git a/bn_mp_dr_setup.c b/bn_mp_dr_setup.c
index e17c052..0214164 100644
--- a/bn_mp_dr_setup.c
+++ b/bn_mp_dr_setup.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines the setup value */
diff --git a/bn_mp_exch.c b/bn_mp_exch.c
index f1cbb1d..8e6927d 100644
--- a/bn_mp_exch.c
+++ b/bn_mp_exch.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* swap the elements of two integers, for cases where you can't simply swap the
diff --git a/bn_mp_expt_d.c b/bn_mp_expt_d.c
index 908bea1..a6cffba 100644
--- a/bn_mp_expt_d.c
+++ b/bn_mp_expt_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* calculate c = a**b using a square-multiply algorithm */
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index 2514e2c..30c19e1 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
diff --git a/bn_mp_exptmod_fast.c b/bn_mp_exptmod_fast.c
index 7073dee..e6662d5 100644
--- a/bn_mp_exptmod_fast.c
+++ b/bn_mp_exptmod_fast.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
diff --git a/bn_mp_exteuclid.c b/bn_mp_exteuclid.c
index 9a1f16c..1253bd8 100644
--- a/bn_mp_exteuclid.c
+++ b/bn_mp_exteuclid.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Extended euclidean algorithm of (a, b) produces
diff --git a/bn_mp_fread.c b/bn_mp_fread.c
index 5fa23f9..acb58de 100644
--- a/bn_mp_fread.c
+++ b/bn_mp_fread.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* read a bigint from a file stream in ASCII */
diff --git a/bn_mp_fwrite.c b/bn_mp_fwrite.c
index e70e155..8bf32eb 100644
--- a/bn_mp_fwrite.c
+++ b/bn_mp_fwrite.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
int mp_fwrite(mp_int *a, int radix, FILE *stream)
diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c
index db03dbb..e9cf306 100644
--- a/bn_mp_gcd.c
+++ b/bn_mp_gcd.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Greatest Common Divisor using the binary method */
@@ -22,21 +22,13 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
int k, u_lsb, v_lsb, res;
/* either zero than gcd is the largest */
- if (mp_iszero (a) == 1 && mp_iszero (b) == 0) {
+ if (mp_iszero (a) == MP_YES) {
return mp_abs (b, c);
}
- if (mp_iszero (a) == 0 && mp_iszero (b) == 1) {
+ if (mp_iszero (b) == MP_YES) {
return mp_abs (a, c);
}
- /* optimized. At this point if a == 0 then
- * b must equal zero too
- */
- if (mp_iszero (a) == 1) {
- mp_zero(c);
- return MP_OKAY;
- }
-
/* get copies of a and b we can modify */
if ((res = mp_init_copy (&u, a)) != MP_OKAY) {
return res;
diff --git a/bn_mp_get_int.c b/bn_mp_get_int.c
index 25942db..5d8a624 100644
--- a/bn_mp_get_int.c
+++ b/bn_mp_get_int.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* get the lower 32-bits of an mp_int */
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index af987a3..a433226 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* grow as required */
diff --git a/bn_mp_init.c b/bn_mp_init.c
index 6e935de..1d40bed 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* init a new mp_int */
diff --git a/bn_mp_init_copy.c b/bn_mp_init_copy.c
index 1d00607..0898aa3 100644
--- a/bn_mp_init_copy.c
+++ b/bn_mp_init_copy.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* creates "a" then copies b into it */
diff --git a/bn_mp_init_multi.c b/bn_mp_init_multi.c
index de23432..ec4f769 100644
--- a/bn_mp_init_multi.c
+++ b/bn_mp_init_multi.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#include <stdarg.h>
diff --git a/bn_mp_init_set.c b/bn_mp_init_set.c
index a7380a8..aa0b2c3 100644
--- a/bn_mp_init_set.c
+++ b/bn_mp_init_set.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* initialize and set a digit */
diff --git a/bn_mp_init_set_int.c b/bn_mp_init_set_int.c
index 793ad86..ab4f2f2 100644
--- a/bn_mp_init_set_int.c
+++ b/bn_mp_init_set_int.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* initialize and set a digit */
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index 3e5724a..947d978 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* init an mp_init for a given size */
diff --git a/bn_mp_invmod.c b/bn_mp_invmod.c
index d4e4e5f..5a2f3e6 100644
--- a/bn_mp_invmod.c
+++ b/bn_mp_invmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* hac 14.61, pp608 */
diff --git a/bn_mp_invmod_slow.c b/bn_mp_invmod_slow.c
index 325282f..415f3d6 100644
--- a/bn_mp_invmod_slow.c
+++ b/bn_mp_invmod_slow.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* hac 14.61, pp608 */
diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c
index 42be22d..fe32c60 100644
--- a/bn_mp_is_square.c
+++ b/bn_mp_is_square.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Check if remainders are possible squares - fast exclude non-squares */
diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c
index e21ee4b..58196a3 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes the jacobi c = (a | n) (or Legendre if n is prime)
diff --git a/bn_mp_karatsuba_mul.c b/bn_mp_karatsuba_mul.c
index fe49694..176dfdd 100644
--- a/bn_mp_karatsuba_mul.c
+++ b/bn_mp_karatsuba_mul.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* c = |a| * |b| using Karatsuba Multiplication using
diff --git a/bn_mp_karatsuba_sqr.c b/bn_mp_karatsuba_sqr.c
index ff8a1f6..2c7163f 100644
--- a/bn_mp_karatsuba_sqr.c
+++ b/bn_mp_karatsuba_sqr.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Karatsuba squaring, computes b = a*a using three
diff --git a/bn_mp_lcm.c b/bn_mp_lcm.c
index 66c2c8e..dcda59a 100644
--- a/bn_mp_lcm.c
+++ b/bn_mp_lcm.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes least common multiple as |a*b|/(a, b) */
diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c
index 79e1e21..b4e4e63 100644
--- a/bn_mp_lshd.c
+++ b/bn_mp_lshd.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift left a certain amount of digits */
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index 364b1f9..9f54b10 100644
--- a/bn_mp_mod.c
+++ b/bn_mp_mod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* c = a mod b, 0 <= c < b */
diff --git a/bn_mp_mod_2d.c b/bn_mp_mod_2d.c
index f191008..6dd7c20 100644
--- a/bn_mp_mod_2d.c
+++ b/bn_mp_mod_2d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* calc a value mod 2**b */
diff --git a/bn_mp_mod_d.c b/bn_mp_mod_d.c
index 5ac6fff..a083270 100644
--- a/bn_mp_mod_d.c
+++ b/bn_mp_mod_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
int
diff --git a/bn_mp_montgomery_calc_normalization.c b/bn_mp_montgomery_calc_normalization.c
index a8c4582..bbaea36 100644
--- a/bn_mp_montgomery_calc_normalization.c
+++ b/bn_mp_montgomery_calc_normalization.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/*
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index fedfbb7..0c44f02 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
diff --git a/bn_mp_montgomery_setup.c b/bn_mp_montgomery_setup.c
index 28a3716..afa8b6e 100644
--- a/bn_mp_montgomery_setup.c
+++ b/bn_mp_montgomery_setup.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* setups the montgomery reduction stuff */
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index e13e4c9..73dbd6b 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* high level multiplication (handles sign) */
diff --git a/bn_mp_mul_2.c b/bn_mp_mul_2.c
index 65416a2..c68a71a 100644
--- a/bn_mp_mul_2.c
+++ b/bn_mp_mul_2.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = a*2 */
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
index 671b31e..01e5acf 100644
--- a/bn_mp_mul_2d.c
+++ b/bn_mp_mul_2d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift left by a certain bit count */
diff --git a/bn_mp_mul_d.c b/bn_mp_mul_d.c
index 7944d9c..9b29925 100644
--- a/bn_mp_mul_d.c
+++ b/bn_mp_mul_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiply by a digit */
diff --git a/bn_mp_mulmod.c b/bn_mp_mulmod.c
index 83b3449..35ac5e6 100644
--- a/bn_mp_mulmod.c
+++ b/bn_mp_mulmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* d = a * b (mod c) */
diff --git a/bn_mp_n_root.c b/bn_mp_n_root.c
index fef65e3..3a8fc2c 100644
--- a/bn_mp_n_root.c
+++ b/bn_mp_n_root.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* find the n'th root of an integer
diff --git a/bn_mp_neg.c b/bn_mp_neg.c
index 587fe60..d83e371 100644
--- a/bn_mp_neg.c
+++ b/bn_mp_neg.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = -a */
diff --git a/bn_mp_or.c b/bn_mp_or.c
index 1655e39..127c2af 100644
--- a/bn_mp_or.c
+++ b/bn_mp_or.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* OR two ints together */
diff --git a/bn_mp_prime_fermat.c b/bn_mp_prime_fermat.c
index 59bcb86..423b8e2 100644
--- a/bn_mp_prime_fermat.c
+++ b/bn_mp_prime_fermat.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* performs one Fermat test.
diff --git a/bn_mp_prime_is_divisible.c b/bn_mp_prime_is_divisible.c
index 1d30653..770e6ca 100644
--- a/bn_mp_prime_is_divisible.c
+++ b/bn_mp_prime_is_divisible.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if an integers is divisible by one
diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index d45bf58..9843834 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* performs a variable number of rounds of Miller-Rabin
diff --git a/bn_mp_prime_miller_rabin.c b/bn_mp_prime_miller_rabin.c
index fbe055b..33c6893 100644
--- a/bn_mp_prime_miller_rabin.c
+++ b/bn_mp_prime_miller_rabin.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Miller-Rabin test of "a" to the base of "b" as described in
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index 7b2be90..9fc2181 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* finds the next prime after the number "a" using "t" trials
diff --git a/bn_mp_prime_rabin_miller_trials.c b/bn_mp_prime_rabin_miller_trials.c
index 4bcf74e..b27b4d9 100644
--- a/bn_mp_prime_rabin_miller_trials.c
+++ b/bn_mp_prime_rabin_miller_trials.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c
index 98f3dbb..14a7b06 100644
--- a/bn_mp_prime_random_ex.c
+++ b/bn_mp_prime_random_ex.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* makes a truly random prime of a given size (bits),
diff --git a/bn_mp_radix_size.c b/bn_mp_radix_size.c
index 68783fc..971df18 100644
--- a/bn_mp_radix_size.c
+++ b/bn_mp_radix_size.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* returns size of ASCII reprensentation */
diff --git a/bn_mp_radix_smap.c b/bn_mp_radix_smap.c
index 7a8aa49..2f13864 100644
--- a/bn_mp_radix_smap.c
+++ b/bn_mp_radix_smap.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* chars used in radix conversions */
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index 6dc2abe..bf7a42a 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* makes a pseudo-random int of a given size */
diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c
index cbd0983..0a7b421 100644
--- a/bn_mp_read_radix.c
+++ b/bn_mp_read_radix.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* read a string [ASCII] in a given radix */
diff --git a/bn_mp_read_signed_bin.c b/bn_mp_read_signed_bin.c
index 0b913fd..8e7a9f3 100644
--- a/bn_mp_read_signed_bin.c
+++ b/bn_mp_read_signed_bin.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* read signed bin, big endian, first byte is 0==positive or 1==negative */
diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c
index 84b996f..c968e72 100644
--- a/bn_mp_read_unsigned_bin.c
+++ b/bn_mp_read_unsigned_bin.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reads a unsigned char array, assumes the msb is stored first [big endian] */
diff --git a/bn_mp_reduce.c b/bn_mp_reduce.c
index aa18eab..8f0d118 100644
--- a/bn_mp_reduce.c
+++ b/bn_mp_reduce.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduces x mod m, assumes 0 < x < m**2, mu is
diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c
index a23fd20..35c1668 100644
--- a/bn_mp_reduce_2k.c
+++ b/bn_mp_reduce_2k.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduces a modulo n where n is of the form 2**p - d */
diff --git a/bn_mp_reduce_2k_l.c b/bn_mp_reduce_2k_l.c
index 638caf4..a30d148 100644
--- a/bn_mp_reduce_2k_l.c
+++ b/bn_mp_reduce_2k_l.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduces a modulo n where n is of the form 2**p - d
diff --git a/bn_mp_reduce_2k_setup.c b/bn_mp_reduce_2k_setup.c
index 30b6ff9..bf22998 100644
--- a/bn_mp_reduce_2k_setup.c
+++ b/bn_mp_reduce_2k_setup.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines the setup value */
diff --git a/bn_mp_reduce_2k_setup_l.c b/bn_mp_reduce_2k_setup_l.c
index 8e21c0e..7dcf914 100644
--- a/bn_mp_reduce_2k_setup_l.c
+++ b/bn_mp_reduce_2k_setup_l.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines the setup value */
diff --git a/bn_mp_reduce_is_2k.c b/bn_mp_reduce_is_2k.c
index c34dcf1..54df1f2 100644
--- a/bn_mp_reduce_is_2k.c
+++ b/bn_mp_reduce_is_2k.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if mp_reduce_2k can be used */
diff --git a/bn_mp_reduce_is_2k_l.c b/bn_mp_reduce_is_2k_l.c
index e3a7fae..0527394 100644
--- a/bn_mp_reduce_is_2k_l.c
+++ b/bn_mp_reduce_is_2k_l.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if reduce_2k_l can be used */
diff --git a/bn_mp_reduce_setup.c b/bn_mp_reduce_setup.c
index 46ae229..b50a33d 100644
--- a/bn_mp_reduce_setup.c
+++ b/bn_mp_reduce_setup.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* pre-calculate the value required for Barrett reduction
diff --git a/bn_mp_rshd.c b/bn_mp_rshd.c
index 37ff66d..ce8a453 100644
--- a/bn_mp_rshd.c
+++ b/bn_mp_rshd.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift right a certain amount of digits */
diff --git a/bn_mp_set.c b/bn_mp_set.c
index eec7bfb..9c5c0d1 100644
--- a/bn_mp_set.c
+++ b/bn_mp_set.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* set to a digit */
diff --git a/bn_mp_set_int.c b/bn_mp_set_int.c
index 202c70c..caf32eb 100644
--- a/bn_mp_set_int.c
+++ b/bn_mp_set_int.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* set a 32-bit const */
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index b6cda9a..6c561cf 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shrink a bignum */
diff --git a/bn_mp_signed_bin_size.c b/bn_mp_signed_bin_size.c
index 178187a..a57ab45 100644
--- a/bn_mp_signed_bin_size.c
+++ b/bn_mp_signed_bin_size.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* get the size for an signed equivalent */
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index 675a87b..239954c 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes b = a*a */
diff --git a/bn_mp_sqrmod.c b/bn_mp_sqrmod.c
index 3cf6ab7..493347c 100644
--- a/bn_mp_sqrmod.c
+++ b/bn_mp_sqrmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* c = a * a (mod b) */
diff --git a/bn_mp_sqrt.c b/bn_mp_sqrt.c
index 7785737..c8142ef 100644
--- a/bn_mp_sqrt.c
+++ b/bn_mp_sqrt.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* this function is less generic than mp_n_root, simpler and faster */
diff --git a/bn_mp_sub.c b/bn_mp_sub.c
index 4714aaf..f58dc09 100644
--- a/bn_mp_sub.c
+++ b/bn_mp_sub.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* high level subtraction (handles signs) */
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index 7208092..0fefac7 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* single digit subtraction */
diff --git a/bn_mp_submod.c b/bn_mp_submod.c
index 79fa787..99c1928 100644
--- a/bn_mp_submod.c
+++ b/bn_mp_submod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* d = a - b (mod c) */
diff --git a/bn_mp_to_signed_bin.c b/bn_mp_to_signed_bin.c
index 6365659..c6397fe 100644
--- a/bn_mp_to_signed_bin.c
+++ b/bn_mp_to_signed_bin.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in signed [big endian] format */
diff --git a/bn_mp_to_signed_bin_n.c b/bn_mp_to_signed_bin_n.c
index bea0762..302c610 100644
--- a/bn_mp_to_signed_bin_n.c
+++ b/bn_mp_to_signed_bin_n.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in signed [big endian] format */
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index 18e3d97..8458ade 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in unsigned [big endian] format */
diff --git a/bn_mp_to_unsigned_bin_n.c b/bn_mp_to_unsigned_bin_n.c
index 4a1778b..d6671c9 100644
--- a/bn_mp_to_unsigned_bin_n.c
+++ b/bn_mp_to_unsigned_bin_n.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in unsigned [big endian] format */
diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c
index 69de0da..f3e5ad5 100644
--- a/bn_mp_toom_mul.c
+++ b/bn_mp_toom_mul.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiplication using the Toom-Cook 3-way algorithm
diff --git a/bn_mp_toom_sqr.c b/bn_mp_toom_sqr.c
index 871c75f..67cf414 100644
--- a/bn_mp_toom_sqr.c
+++ b/bn_mp_toom_sqr.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* squaring using Toom-Cook 3-way algorithm */
diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c
index 4caeccc..31231b3 100644
--- a/bn_mp_toradix.c
+++ b/bn_mp_toradix.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* stores a bignum as a ASCII string in a given radix (2..64) */
diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c
index 4790ac8..14a7994 100644
--- a/bn_mp_toradix_n.c
+++ b/bn_mp_toradix_n.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* stores a bignum as a ASCII string in a given radix (2..64)
diff --git a/bn_mp_unsigned_bin_size.c b/bn_mp_unsigned_bin_size.c
index 21be05c..0cd8113 100644
--- a/bn_mp_unsigned_bin_size.c
+++ b/bn_mp_unsigned_bin_size.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* get the size for an unsigned equivalent */
diff --git a/bn_mp_xor.c b/bn_mp_xor.c
index 56becb4..b7adbac 100644
--- a/bn_mp_xor.c
+++ b/bn_mp_xor.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* XOR two ints together */
diff --git a/bn_mp_zero.c b/bn_mp_zero.c
index 7e18317..e1cf4a4 100644
--- a/bn_mp_zero.c
+++ b/bn_mp_zero.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* set to zero */
diff --git a/bn_prime_tab.c b/bn_prime_tab.c
index ce130ef..2a76c91 100644
--- a/bn_prime_tab.c
+++ b/bn_prime_tab.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
const mp_digit ltm_prime_tab[] = {
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
diff --git a/bn_reverse.c b/bn_reverse.c
index bcd0649..e42c510 100644
--- a/bn_reverse.c
+++ b/bn_reverse.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reverse an array, used for radix code */
diff --git a/bn_s_mp_add.c b/bn_s_mp_add.c
index 6976e62..e9f4ce5 100644
--- a/bn_s_mp_add.c
+++ b/bn_s_mp_add.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* low level addition, based on HAC pp.594, Algorithm 14.7 */
diff --git a/bn_s_mp_exptmod.c b/bn_s_mp_exptmod.c
index 12d981b..3ac0d61 100644
--- a/bn_s_mp_exptmod.c
+++ b/bn_s_mp_exptmod.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#ifdef MP_LOW_MEM
#define TAB_SIZE 32
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index a925e12..82e4ae3 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiplies |a| * |b| and only computes upto digs digits of result
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index e9505c8..aa767ea 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiplies |a| * |b| and does not compute the lower digs digits
diff --git a/bn_s_mp_sqr.c b/bn_s_mp_sqr.c
index 4648296..e584213 100644
--- a/bn_s_mp_sqr.c
+++ b/bn_s_mp_sqr.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index 4f7d47d..bd636b7 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
diff --git a/bncore.c b/bncore.c
index fa4e64e..23d6718 100644
--- a/bncore.c
+++ b/bncore.c
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Known optimal configurations
diff --git a/changes.txt b/changes.txt
index cf65066..9498d36 100644
--- a/changes.txt
+++ b/changes.txt
@@ -1,3 +1,10 @@
+April 4th, 2006
+v0.39 -- Jim Wigginton pointed out my Montgomery examples in figures 6.4 and 6.6 were off by one, k should be 9 not 8
+ -- Bruce Guenter suggested I use --tag=CC for libtool builds where the compiler may think it's C++.
+ -- "mm" from sci.crypt pointed out that my mp_gcd was sub-optimal (I also updated and corrected the book)
+ -- updated some of the @@ tags in tommath.src to reflect source changes.
+ -- updated email and url info in all source files
+
Jan 26th, 2006
v0.38 -- broken makefile.shared fixed
-- removed some carry stores that were not required [updated text]
diff --git a/etc/mersenne.c b/etc/mersenne.c
index 5697559..ae6725a 100644
--- a/etc/mersenne.c
+++ b/etc/mersenne.c
@@ -1,6 +1,6 @@
/* Finds Mersenne primes using the Lucas-Lehmer test
*
- * Tom St Denis, tomstdenis@iahu.ca
+ * Tom St Denis, tomstdenis@gmail.com
*/
#include <time.h>
#include <tommath.h>
diff --git a/etc/pprime.c b/etc/pprime.c
index d3a4afe..9f94423 100644
--- a/etc/pprime.c
+++ b/etc/pprime.c
@@ -1,8 +1,8 @@
/* Generates provable primes
*
- * See http://iahu.ca:8080/papers/pp.pdf for more info.
+ * See http://gmail.com:8080/papers/pp.pdf for more info.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://tom.iahu.ca
+ * Tom St Denis, tomstdenis@gmail.com, http://tom.gmail.com
*/
#include <time.h>
#include "tommath.h"
diff --git a/etc/tune.c b/etc/tune.c
index 15a977b..7c07d22 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -1,6 +1,6 @@
/* Tune the Karatsuba parameters
*
- * Tom St Denis, tomstdenis@iahu.ca
+ * Tom St Denis, tomstdenis@gmail.com
*/
#include <tommath.h>
#include <time.h>
diff --git a/makefile b/makefile
index d228411..e08a888 100644
--- a/makefile
+++ b/makefile
@@ -3,7 +3,7 @@
#Tom St Denis
#version of library
-VERSION=0.38
+VERSION=0.39
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
diff --git a/makefile.shared b/makefile.shared
index 45b2f63..8522d44 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -1,9 +1,9 @@
#Makefile for GCC
#
#Tom St Denis
-VERSION=0:38
+VERSION=0:39
-CC = libtool --mode=compile gcc
+CC = libtool --mode=compile --tag=CC gcc
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
@@ -86,7 +86,8 @@ $(LIBNAME): $(OBJECTS)
libtool --mode=link gcc *.lo -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION)
install: $(LIBNAME)
- libtool --mode=install install -c $(LIBNAME) $(LIBPATH)/$(LIBNAME)
+ install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH)
+ libtool --mode=install install -c $(LIBNAME) $(DESTDIR)$(LIBPATH)/$(LIBNAME)
install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)
diff --git a/poster.pdf b/poster.pdf
index c975ba3..1f705cf 100644
Binary files a/poster.pdf and b/poster.pdf differ
diff --git a/pre_gen/mpi.c b/pre_gen/mpi.c
index 166d171..2274e9a 100644
--- a/pre_gen/mpi.c
+++ b/pre_gen/mpi.c
@@ -13,7 +13,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
static const struct {
@@ -64,7 +64,7 @@ char *mp_error_to_string(int code)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes the modular inverse via binary extended euclidean algorithm,
@@ -216,7 +216,7 @@ LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes xR**-1 == x (mod N) via Montgomery Reduction
@@ -392,7 +392,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Fast (comba) multiplier
@@ -503,7 +503,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* this is a modified version of fast_s_mul_digs that only produces
@@ -605,7 +605,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* the jist of squaring...
@@ -723,7 +723,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes a = 2**b
@@ -775,7 +775,7 @@ mp_2expt (mp_int * a, int b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = |a|
@@ -822,7 +822,7 @@ mp_abs (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* high level addition (handles signs) */
@@ -879,7 +879,7 @@ int mp_add (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* single digit addition */
@@ -995,7 +995,7 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* d = a + b (mod c) */
@@ -1040,7 +1040,7 @@ mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* AND two ints together */
@@ -1101,7 +1101,7 @@ mp_and (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* trim unused digits
@@ -1149,7 +1149,7 @@ mp_clamp (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* clear one (frees) */
@@ -1197,7 +1197,7 @@ mp_clear (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#include <stdarg.h>
@@ -1235,7 +1235,7 @@ void mp_clear_multi(mp_int *mp, ...)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* compare two ints (signed)*/
@@ -1282,7 +1282,7 @@ mp_cmp (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* compare a digit */
@@ -1330,7 +1330,7 @@ int mp_cmp_d(mp_int * a, mp_digit b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* compare maginitude of two ints (unsigned) */
@@ -1389,7 +1389,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
static const int lnz[16] = {
@@ -1446,7 +1446,7 @@ int mp_cnt_lsb(mp_int *a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* copy, b = a */
@@ -1518,7 +1518,7 @@ mp_copy (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* returns the number of bits in an int */
@@ -1567,7 +1567,7 @@ mp_count_bits (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#ifdef BN_MP_DIV_SMALL
@@ -1863,7 +1863,7 @@ LBL_Q:mp_clear (&q);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = a/2 */
@@ -1935,7 +1935,7 @@ int mp_div_2(mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
@@ -2036,7 +2036,7 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* divide by three (based on routine from MPI and the GMP manual) */
@@ -2119,7 +2119,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
static int s_is_power_of_two(mp_digit b, int *p)
@@ -2233,7 +2233,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if a number is a valid DR modulus */
@@ -2280,7 +2280,7 @@ int mp_dr_is_modulus(mp_int *a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
@@ -2378,7 +2378,7 @@ top:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines the setup value */
@@ -2414,7 +2414,7 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* swap the elements of two integers, for cases where you can't simply swap the
@@ -2452,7 +2452,7 @@ mp_exch (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* calculate c = a**b using a square-multiply algorithm */
@@ -2513,7 +2513,7 @@ int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
@@ -2629,7 +2629,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
@@ -2954,7 +2954,7 @@ LBL_M:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Extended euclidean algorithm of (a, b) produces
@@ -3040,7 +3040,7 @@ _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* read a bigint from a file stream in ASCII */
@@ -3111,7 +3111,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
int mp_fwrite(mp_int *a, int radix, FILE *stream)
@@ -3167,7 +3167,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Greatest Common Divisor using the binary method */
@@ -3177,21 +3177,13 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
int k, u_lsb, v_lsb, res;
/* either zero than gcd is the largest */
- if (mp_iszero (a) == 1 && mp_iszero (b) == 0) {
+ if (mp_iszero (a) == MP_YES) {
return mp_abs (b, c);
}
- if (mp_iszero (a) == 0 && mp_iszero (b) == 1) {
+ if (mp_iszero (b) == MP_YES) {
return mp_abs (a, c);
}
- /* optimized. At this point if a == 0 then
- * b must equal zero too
- */
- if (mp_iszero (a) == 1) {
- mp_zero(c);
- return MP_OKAY;
- }
-
/* get copies of a and b we can modify */
if ((res = mp_init_copy (&u, a)) != MP_OKAY) {
return res;
@@ -3284,7 +3276,7 @@ LBL_U:mp_clear (&v);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* get the lower 32-bits of an mp_int */
@@ -3333,7 +3325,7 @@ unsigned long mp_get_int(mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* grow as required */
@@ -3394,7 +3386,7 @@ int mp_grow (mp_int * a, int size)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* init a new mp_int */
@@ -3444,7 +3436,7 @@ int mp_init (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* creates "a" then copies b into it */
@@ -3480,7 +3472,7 @@ int mp_init_copy (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#include <stdarg.h>
@@ -3543,7 +3535,7 @@ int mp_init_multi(mp_int *mp, ...)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* initialize and set a digit */
@@ -3579,7 +3571,7 @@ int mp_init_set (mp_int * a, mp_digit b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* initialize and set a digit */
@@ -3614,7 +3606,7 @@ int mp_init_set_int (mp_int * a, unsigned long b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* init an mp_init for a given size */
@@ -3666,7 +3658,7 @@ int mp_init_size (mp_int * a, int size)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* hac 14.61, pp608 */
@@ -3713,7 +3705,7 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* hac 14.61, pp608 */
@@ -3892,7 +3884,7 @@ LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Check if remainders are possible squares - fast exclude non-squares */
@@ -4005,7 +3997,7 @@ ERR:mp_clear(&t);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes the jacobi c = (a | n) (or Legendre if n is prime)
@@ -4114,7 +4106,7 @@ LBL_A1:mp_clear (&a1);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* c = |a| * |b| using Karatsuba Multiplication using
@@ -4285,7 +4277,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Karatsuba squaring, computes b = a*a using three
@@ -4410,7 +4402,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes least common multiple as |a*b|/(a, b) */
@@ -4474,7 +4466,7 @@ LBL_T:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift left a certain amount of digits */
@@ -4545,7 +4537,7 @@ int mp_lshd (mp_int * a, int b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* c = a mod b, 0 <= c < b */
@@ -4597,7 +4589,7 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* calc a value mod 2**b */
@@ -4656,7 +4648,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
int
@@ -4687,7 +4679,7 @@ mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/*
@@ -4750,7 +4742,7 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
@@ -4872,7 +4864,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* setups the montgomery reduction stuff */
@@ -4935,7 +4927,7 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* high level multiplication (handles sign) */
@@ -5005,7 +4997,7 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = a*2 */
@@ -5091,7 +5083,7 @@ int mp_mul_2(mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift left by a certain bit count */
@@ -5180,7 +5172,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiply by a digit */
@@ -5263,7 +5255,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* d = a * b (mod c) */
@@ -5307,7 +5299,7 @@ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* find the n'th root of an integer
@@ -5443,7 +5435,7 @@ LBL_T1:mp_clear (&t1);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* b = -a */
@@ -5487,7 +5479,7 @@ int mp_neg (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* OR two ints together */
@@ -5541,7 +5533,7 @@ int mp_or (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* performs one Fermat test.
@@ -5607,7 +5599,7 @@ LBL_T:mp_clear (&t);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if an integers is divisible by one
@@ -5661,7 +5653,7 @@ int mp_prime_is_divisible (mp_int * a, int *result)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* performs a variable number of rounds of Miller-Rabin
@@ -5748,7 +5740,7 @@ LBL_B:mp_clear (&b);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Miller-Rabin test of "a" to the base of "b" as described in
@@ -5855,7 +5847,7 @@ LBL_N1:mp_clear (&n1);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* finds the next prime after the number "a" using "t" trials
@@ -6029,7 +6021,7 @@ LBL_ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
@@ -6085,7 +6077,7 @@ int mp_prime_rabin_miller_trials(int size)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* makes a truly random prime of a given size (bits),
@@ -6214,7 +6206,7 @@ error:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* returns size of ASCII reprensentation */
@@ -6296,7 +6288,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* chars used in radix conversions */
@@ -6324,7 +6316,7 @@ const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* makes a pseudo-random int of a given size */
@@ -6383,7 +6375,7 @@ mp_rand (mp_int * a, int digits)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* read a string [ASCII] in a given radix */
@@ -6472,7 +6464,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* read signed bin, big endian, first byte is 0==positive or 1==negative */
@@ -6517,7 +6509,7 @@ int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reads a unsigned char array, assumes the msb is stored first [big endian] */
@@ -6576,7 +6568,7 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduces x mod m, assumes 0 < x < m**2, mu is
@@ -6680,7 +6672,7 @@ CLEANUP:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduces a modulo n where n is of the form 2**p - d */
@@ -6745,7 +6737,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reduces a modulo n where n is of the form 2**p - d
@@ -6811,7 +6803,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines the setup value */
@@ -6862,7 +6854,7 @@ int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines the setup value */
@@ -6910,7 +6902,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if mp_reduce_2k can be used */
@@ -6966,7 +6958,7 @@ int mp_reduce_is_2k(mp_int *a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* determines if reduce_2k_l can be used */
@@ -7014,7 +7006,7 @@ int mp_reduce_is_2k_l(mp_int *a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* pre-calculate the value required for Barrett reduction
@@ -7052,7 +7044,7 @@ int mp_reduce_setup (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shift right a certain amount of digits */
@@ -7128,7 +7120,7 @@ void mp_rshd (mp_int * a, int b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* set to a digit */
@@ -7161,7 +7153,7 @@ void mp_set (mp_int * a, mp_digit b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* set a 32-bit const */
@@ -7213,7 +7205,7 @@ int mp_set_int (mp_int * a, unsigned long b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* shrink a bignum */
@@ -7252,7 +7244,7 @@ int mp_shrink (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* get the size for an signed equivalent */
@@ -7283,7 +7275,7 @@ int mp_signed_bin_size (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* computes b = a*a */
@@ -7345,7 +7337,7 @@ if (a->used >= KARATSUBA_SQR_CUTOFF) {
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* c = a * a (mod b) */
@@ -7390,7 +7382,7 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* this function is less generic than mp_n_root, simpler and faster */
@@ -7475,7 +7467,7 @@ E2: mp_clear(&t1);
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* high level subtraction (handles signs) */
@@ -7538,7 +7530,7 @@ mp_sub (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* single digit subtraction */
@@ -7635,7 +7627,7 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* d = a - b (mod c) */
@@ -7681,7 +7673,7 @@ mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in signed [big endian] format */
@@ -7718,7 +7710,7 @@ int mp_to_signed_bin (mp_int * a, unsigned char *b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in signed [big endian] format */
@@ -7753,7 +7745,7 @@ int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in unsigned [big endian] format */
@@ -7805,7 +7797,7 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* store in unsigned [big endian] format */
@@ -7840,7 +7832,7 @@ int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiplication using the Toom-Cook 3-way algorithm
@@ -8128,7 +8120,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* squaring using Toom-Cook 3-way algorithm */
@@ -8358,7 +8350,7 @@ ERR:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* stores a bignum as a ASCII string in a given radix (2..64) */
@@ -8437,7 +8429,7 @@ int mp_toradix (mp_int * a, char *str, int radix)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* stores a bignum as a ASCII string in a given radix (2..64)
@@ -8529,7 +8521,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* get the size for an unsigned equivalent */
@@ -8561,7 +8553,7 @@ int mp_unsigned_bin_size (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* XOR two ints together */
@@ -8616,7 +8608,7 @@ mp_xor (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* set to zero */
@@ -8656,7 +8648,7 @@ void mp_zero (mp_int * a)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
const mp_digit ltm_prime_tab[] = {
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
@@ -8721,7 +8713,7 @@ const mp_digit ltm_prime_tab[] = {
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* reverse an array, used for radix code */
@@ -8764,7 +8756,7 @@ bn_reverse (unsigned char *s, int len)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* low level addition, based on HAC pp.594, Algorithm 14.7 */
@@ -8877,7 +8869,7 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#ifdef MP_LOW_MEM
#define TAB_SIZE 32
@@ -9133,7 +9125,7 @@ LBL_M:
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiplies |a| * |b| and only computes upto digs digits of result
@@ -9227,7 +9219,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* multiplies |a| * |b| and does not compute the lower digs digits
@@ -9312,7 +9304,7 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
@@ -9400,7 +9392,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
@@ -9493,7 +9485,7 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
/* Known optimal configurations
diff --git a/tommath.h b/tommath.h
index 80db43c..3095104 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://math.libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
*/
#ifndef BN_H_
#define BN_H_
diff --git a/tommath.pdf b/tommath.pdf
index a25f3b6..c9571d8 100644
Binary files a/tommath.pdf and b/tommath.pdf differ
diff --git a/tommath.src b/tommath.src
index f81d8d8..4065822 100644
--- a/tommath.src
+++ b/tommath.src
@@ -66,7 +66,7 @@ QUALCOMM Australia \\
}
}
\maketitle
-This text has been placed in the public domain. This text corresponds to the v0.38 release of the
+This text has been placed in the public domain. This text corresponds to the v0.39 release of the
LibTomMath project.
\begin{alltt}
@@ -268,7 +268,7 @@ and fast modular inversion, which we consider practical oversights. These optim
any form of useful performance in non-trivial applications.
To solve this problem the focus of this text is on the practical aspects of implementing a multiple precision integer
-package. As a case study the ``LibTomMath''\footnote{Available at \url{http://math.libtomcrypt.org}} package is used
+package. As a case study the ``LibTomMath''\footnote{Available at \url{http://math.libtomcrypt.com}} package is used
to demonstrate algorithms with real implementations\footnote{In the ISO C programming language.} that have been field
tested and work very well. The LibTomMath library is freely available on the Internet for all uses and this text
discusses a very large portion of the inner workings of the library.
@@ -3730,6 +3730,7 @@ $0 \le r < \lfloor x/2^k \rfloor + n$. As a result at most a single subtraction
\hline $6$ & $x/2 = 139$ \\
\hline $7$ & $x + n = 396$, $x/2 = 198$ \\
\hline $8$ & $x/2 = 99$ \\
+\hline $9$ & $x + n = 356$, $x/2 = 178$ \\
\hline
\end{tabular}
\end{center}
@@ -3738,8 +3739,8 @@ $0 \le r < \lfloor x/2^k \rfloor + n$. As a result at most a single subtraction
\label{fig:MONT1}
\end{figure}
-Consider the example in figure~\ref{fig:MONT1} which reduces $x = 5555$ modulo $n = 257$ when $k = 8$. The result of the algorithm $r = 99$ is
-congruent to the value of $2^{-8} \cdot 5555 \mbox{ (mod }257\mbox{)}$. When $r$ is multiplied by $2^8$ modulo $257$ the correct residue
+Consider the example in figure~\ref{fig:MONT1} which reduces $x = 5555$ modulo $n = 257$ when $k = 9$ (note $\beta^k = 512$ which is larger than $n$). The result of
+the algorithm $r = 178$ is congruent to the value of $2^{-9} \cdot 5555 \mbox{ (mod }257\mbox{)}$. When $r$ is multiplied by $2^9$ modulo $257$ the correct residue
$r \equiv 158$ is produced.
Let $k = \lfloor lg(n) \rfloor + 1$ represent the number of bits in $n$. The current algorithm requires $2k^2$ single precision shifts
@@ -3751,10 +3752,10 @@ Fortunately there exists an alternative representation of the algorithm.
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{Montgomery Reduction} (modified I). \\
-\textbf{Input}. Integer $x$, $n$ and $k$ \\
+\textbf{Input}. Integer $x$, $n$ and $k$ ($2^k > n$) \\
\textbf{Output}. $2^{-k}x \mbox{ (mod }n\mbox{)}$ \\
\hline \\
-1. for $t$ from $0$ to $k - 1$ do \\
+1. for $t$ from $1$ to $k$ do \\
\hspace{3mm}1.1 If the $t$'th bit of $x$ is one then \\
\hspace{6mm}1.1.1 $x \leftarrow x + 2^tn$ \\
2. Return $x/2^k$. \\
@@ -3782,7 +3783,8 @@ precision shifts has now been reduced from $2k^2$ to $k^2 + k$ which is only a s
\hline $6$ & $8896$ & $10001011000000$ \\
\hline $7$ & $x + 2^{6}n = 25344$ & $110001100000000$ \\
\hline $8$ & $25344$ & $110001100000000$ \\
-\hline -- & $x/2^k = 99$ & \\
+\hline $9$ & $x + 2^{7}n = 91136$ & $10110010000000000$ \\
+\hline -- & $x/2^k = 178$ & \\
\hline
\end{tabular}
\end{center}
@@ -3791,7 +3793,7 @@ precision shifts has now been reduced from $2k^2$ to $k^2 + k$ which is only a s
\label{fig:MONT2}
\end{figure}
-Figure~\ref{fig:MONT2} demonstrates the modified algorithm reducing $x = 5555$ modulo $n = 257$ with $k = 8$.
+Figure~\ref{fig:MONT2} demonstrates the modified algorithm reducing $x = 5555$ modulo $n = 257$ with $k = 9$.
With this algorithm a single shift right at the end is the only right shift required to reduce the input instead of $k$ right shifts inside the
loop. Note that for the iterations $t = 2, 5, 6$ and $8$ where the result $x$ is not changed. In those iterations the $t$'th bit of $x$ is
zero and the appropriate multiple of $n$ does not need to be added to force the $t$'th bit of the result to zero.
@@ -3805,7 +3807,7 @@ previous algorithm re-written to compute the Montgomery reduction in this new fa
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{Montgomery Reduction} (modified II). \\
-\textbf{Input}. Integer $x$, $n$ and $k$ \\
+\textbf{Input}. Integer $x$, $n$ and $k$ ($\beta^k > n$) \\
\textbf{Output}. $\beta^{-k}x \mbox{ (mod }n\mbox{)}$ \\
\hline \\
1. for $t$ from $0$ to $k - 1$ do \\
@@ -4937,15 +4939,15 @@ a Left-to-Right algorithm is used to process the remaining few bits.
EXAM,bn_s_mp_exptmod.c
-Lines @26,if@ through @40,}@ determine the optimal window size based on the length of the exponent in bits. The window divisions are sorted
+Lines @31,if@ through @45,}@ determine the optimal window size based on the length of the exponent in bits. The window divisions are sorted
from smallest to greatest so that in each \textbf{if} statement only one condition must be tested. For example, by the \textbf{if} statement
-on line @32,if@ the value of $x$ is already known to be greater than $140$.
+on line @37,if@ the value of $x$ is already known to be greater than $140$.
The conditional piece of code beginning on line @42,ifdef@ allows the window size to be restricted to five bits. This logic is used to ensure
the table of precomputed powers of $G$ remains relatively small.
-The for loop on line @49,for@ initializes the $M$ array while lines @59,mp_init@ and @62,mp_reduce@ compute the value of $\mu$ required for
-Barrett reduction.
+The for loop on line @60,for@ initializes the $M$ array while lines @71,mp_init@ and @75,mp_reduce@ through @85,}@ initialize the reduction
+function that will be used for this modulus.
-- More later.
@@ -5228,23 +5230,23 @@ algorithm with only the quotient is
mp_div(&a, &b, &c, NULL); /* c = [a/b] */
\end{verbatim}
-Lines @37,if@ and @42,if@ handle the two trivial cases of inputs which are division by zero and dividend smaller than the divisor
-respectively. After the two trivial cases all of the temporary variables are initialized. Line @76,neg@ determines the sign of
-the quotient and line @77,sign@ ensures that both $x$ and $y$ are positive.
+Lines @108,if@ and @113,if@ handle the two trivial cases of inputs which are division by zero and dividend smaller than the divisor
+respectively. After the two trivial cases all of the temporary variables are initialized. Line @147,neg@ determines the sign of
+the quotient and line @148,sign@ ensures that both $x$ and $y$ are positive.
-The number of bits in the leading digit is calculated on line @80,norm@. Implictly an mp\_int with $r$ digits will require $lg(\beta)(r-1) + k$ bits
+The number of bits in the leading digit is calculated on line @151,norm@. Implictly an mp\_int with $r$ digits will require $lg(\beta)(r-1) + k$ bits
of precision which when reduced modulo $lg(\beta)$ produces the value of $k$. In this case $k$ is the number of bits in the leading digit which is
exactly what is required. For the algorithm to operate $k$ must equal $lg(\beta) - 1$ and when it does not the inputs must be normalized by shifting
them to the left by $lg(\beta) - 1 - k$ bits.
Throughout the variables $n$ and $t$ will represent the highest digit of $x$ and $y$ respectively. These are first used to produce the
-leading digit of the quotient. The loop beginning on line @113,for@ will produce the remainder of the quotient digits.
+leading digit of the quotient. The loop beginning on line @184,for@ will produce the remainder of the quotient digits.
-The conditional ``continue'' on line @114,if@ is used to prevent the algorithm from reading past the leading edge of $x$ which can occur when the
+The conditional ``continue'' on line @186,continue@ is used to prevent the algorithm from reading past the leading edge of $x$ which can occur when the
algorithm eliminates multiple non-zero digits in a single iteration. This ensures that $x_i$ is always non-zero since by definition the digits
above the $i$'th position $x$ must be zero in order for the quotient to be precise\footnote{Precise as far as integer division is concerned.}.
-Lines @142,t1@, @143,t1@ and @150,t2@ through @152,t2@ manually construct the high accuracy estimations by setting the digits of the two mp\_int
+Lines @214,t1@, @216,t1@ and @222,t2@ through @225,t2@ manually construct the high accuracy estimations by setting the digits of the two mp\_int
variables directly.
\section{Single Digit Helpers}
@@ -5742,33 +5744,30 @@ and will produce the greatest common divisor.
\textbf{Input}. mp\_int $a$ and $b$ \\
\textbf{Output}. The greatest common divisor $c = (a, b)$. \\
\hline \\
-1. If $a = 0$ and $b \ne 0$ then \\
-\hspace{3mm}1.1 $c \leftarrow b$ \\
+1. If $a = 0$ then \\
+\hspace{3mm}1.1 $c \leftarrow \vert b \vert $ \\
\hspace{3mm}1.2 Return(\textit{MP\_OKAY}). \\
-2. If $a \ne 0$ and $b = 0$ then \\
-\hspace{3mm}2.1 $c \leftarrow a$ \\
+2. If $b = 0$ then \\
+\hspace{3mm}2.1 $c \leftarrow \vert a \vert $ \\
\hspace{3mm}2.2 Return(\textit{MP\_OKAY}). \\
-3. If $a = b = 0$ then \\
-\hspace{3mm}3.1 $c \leftarrow 1$ \\
-\hspace{3mm}3.2 Return(\textit{MP\_OKAY}). \\
-4. $u \leftarrow \vert a \vert, v \leftarrow \vert b \vert$ \\
-5. $k \leftarrow 0$ \\
-6. While $u.used > 0$ and $v.used > 0$ and $u_0 \equiv v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{3mm}6.1 $k \leftarrow k + 1$ \\
-\hspace{3mm}6.2 $u \leftarrow \lfloor u / 2 \rfloor$ \\
-\hspace{3mm}6.3 $v \leftarrow \lfloor v / 2 \rfloor$ \\
-7. While $u.used > 0$ and $u_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{3mm}7.1 $u \leftarrow \lfloor u / 2 \rfloor$ \\
-8. While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{3mm}8.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
-9. While $v.used > 0$ \\
-\hspace{3mm}9.1 If $\vert u \vert > \vert v \vert$ then \\
-\hspace{6mm}9.1.1 Swap $u$ and $v$. \\
-\hspace{3mm}9.2 $v \leftarrow \vert v \vert - \vert u \vert$ \\
-\hspace{3mm}9.3 While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{6mm}9.3.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
-10. $c \leftarrow u \cdot 2^k$ \\
-11. Return(\textit{MP\_OKAY}). \\
+3. $u \leftarrow \vert a \vert, v \leftarrow \vert b \vert$ \\
+4. $k \leftarrow 0$ \\
+5. While $u.used > 0$ and $v.used > 0$ and $u_0 \equiv v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{3mm}5.1 $k \leftarrow k + 1$ \\
+\hspace{3mm}5.2 $u \leftarrow \lfloor u / 2 \rfloor$ \\
+\hspace{3mm}5.3 $v \leftarrow \lfloor v / 2 \rfloor$ \\
+6. While $u.used > 0$ and $u_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{3mm}6.1 $u \leftarrow \lfloor u / 2 \rfloor$ \\
+7. While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{3mm}7.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
+8. While $v.used > 0$ \\
+\hspace{3mm}8.1 If $\vert u \vert > \vert v \vert$ then \\
+\hspace{6mm}8.1.1 Swap $u$ and $v$. \\
+\hspace{3mm}8.2 $v \leftarrow \vert v \vert - \vert u \vert$ \\
+\hspace{3mm}8.3 While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{6mm}8.3.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
+9. $c \leftarrow u \cdot 2^k$ \\
+10. Return(\textit{MP\_OKAY}). \\
\hline
\end{tabular}
\end{center}
@@ -5780,17 +5779,17 @@ This algorithm will produce the greatest common divisor of two mp\_ints $a$ and
Knuth \cite[pp. 338]{TAOCPV2} but has been modified to be simpler to explain. In theory it achieves the same asymptotic working time as
Algorithm B and in practice this appears to be true.
-The first three steps handle the cases where either one of or both inputs are zero. If either input is zero the greatest common divisor is the
+The first two steps handle the cases where either one of or both inputs are zero. If either input is zero the greatest common divisor is the
largest input or zero if they are both zero. If the inputs are not trivial than $u$ and $v$ are assigned the absolute values of
$a$ and $b$ respectively and the algorithm will proceed to reduce the pair.
-Step six will divide out any common factors of two and keep track of the count in the variable $k$. After this step two is no longer a
+Step five will divide out any common factors of two and keep track of the count in the variable $k$. After this step, two is no longer a
factor of the remaining greatest common divisor between $u$ and $v$ and can be safely evenly divided out of either whenever they are even. Step
-seven and eight ensure that the $u$ and $v$ respectively have no more factors of two. At most only one of the while loops will iterate since
+six and seven ensure that the $u$ and $v$ respectively have no more factors of two. At most only one of the while--loops will iterate since
they cannot both be even.
-By step nine both of $u$ and $v$ are odd which is required for the inner logic. First the pair are swapped such that $v$ is equal to
-or greater than $u$. This ensures that the subtraction on step 9.2 will always produce a positive and even result. Step 9.3 removes any
+By step eight both of $u$ and $v$ are odd which is required for the inner logic. First the pair are swapped such that $v$ is equal to
+or greater than $u$. This ensures that the subtraction on step 8.2 will always produce a positive and even result. Step 8.3 removes any
factors of two from the difference $u$ to ensure that in the next iteration of the loop both are once again odd.
After $v = 0$ occurs the variable $u$ has the greatest common divisor of the pair $\left < u, v \right >$ just after step six. The result
@@ -5801,17 +5800,17 @@ EXAM,bn_mp_gcd.c
This function makes use of the macros mp\_iszero and mp\_iseven. The former evaluates to $1$ if the input mp\_int is equivalent to the
integer zero otherwise it evaluates to $0$. The latter evaluates to $1$ if the input mp\_int represents a non-zero even integer otherwise
it evaluates to $0$. Note that just because mp\_iseven may evaluate to $0$ does not mean the input is odd, it could also be zero. The three
-trivial cases of inputs are handled on lines @25,zero@ through @34,}@. After those lines the inputs are assumed to be non-zero.
+trivial cases of inputs are handled on lines @23,zero@ through @29,}@. After those lines the inputs are assumed to be non-zero.
-Lines @36,if@ and @40,if@ make local copies $u$ and $v$ of the inputs $a$ and $b$ respectively. At this point the common factors of two
-must be divided out of the two inputs. The while loop on line @49,while@ iterates so long as both are even. The local integer $k$ is used to
-keep track of how many factors of $2$ are pulled out of both values. It is assumed that the number of factors will not exceed the maximum
-value of a C ``int'' data type\footnote{Strictly speaking no array in C may have more than entries than are accessible by an ``int'' so this is not
-a limitation.}.
+Lines @32,if@ and @36,if@ make local copies $u$ and $v$ of the inputs $a$ and $b$ respectively. At this point the common factors of two
+must be divided out of the two inputs. The block starting at line @43,common@ removes common factors of two by first counting the number of trailing
+zero bits in both. The local integer $k$ is used to keep track of how many factors of $2$ are pulled out of both values. It is assumed that
+the number of factors will not exceed the maximum value of a C ``int'' data type\footnote{Strictly speaking no array in C may have more than
+entries than are accessible by an ``int'' so this is not a limitation.}.
-At this point there are no more common factors of two in the two values. The while loops on lines @60,while@ and @65,while@ remove any independent
-factors of two such that both $u$ and $v$ are guaranteed to be an odd integer before hitting the main body of the algorithm. The while loop
-on line @71, while@ performs the reduction of the pair until $v$ is equal to zero. The unsigned comparison and subtraction algorithms are used in
+At this point there are no more common factors of two in the two values. The divisions by a power of two on lines @60,div_2d@ and @67,div_2d@ remove
+any independent factors of two such that both $u$ and $v$ are guaranteed to be an odd integer before hitting the main body of the algorithm. The while loop
+on line @72, while@ performs the reduction of the pair until $v$ is equal to zero. The unsigned comparison and subtraction algorithms are used in
place of the full signed routines since both values are guaranteed to be positive and the result of the subtraction is guaranteed to be non-negative.
\section{Least Common Multiple}
diff --git a/tommath.tex b/tommath.tex
index 791cc28..c79a537 100644
--- a/tommath.tex
+++ b/tommath.tex
@@ -66,7 +66,7 @@ QUALCOMM Australia \\
}
}
\maketitle
-This text has been placed in the public domain. This text corresponds to the v0.38 release of the
+This text has been placed in the public domain. This text corresponds to the v0.39 release of the
LibTomMath project.
\begin{alltt}
@@ -268,7 +268,7 @@ and fast modular inversion, which we consider practical oversights. These optim
any form of useful performance in non-trivial applications.
To solve this problem the focus of this text is on the practical aspects of implementing a multiple precision integer
-package. As a case study the ``LibTomMath''\footnote{Available at \url{http://math.libtomcrypt.org}} package is used
+package. As a case study the ``LibTomMath''\footnote{Available at \url{http://math.libtomcrypt.com}} package is used
to demonstrate algorithms with real implementations\footnote{In the ISO C programming language.} that have been field
tested and work very well. The LibTomMath library is freely available on the Internet for all uses and this text
discusses a very large portion of the inner workings of the library.
@@ -788,33 +788,6 @@ decrementally.
\hspace{-5.1mm}{\bf File}: bn\_mp\_init.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* init a new mp_int */
-018 int mp_init (mp_int * a)
-019 \{
-020 int i;
-021
-022 /* allocate memory required and clear it */
-023 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC);
-024 if (a->dp == NULL) \{
-025 return MP_MEM;
-026 \}
-027
-028 /* set the digits to zero */
-029 for (i = 0; i < MP_PREC; i++) \{
-030 a->dp[i] = 0;
-031 \}
-032
-033 /* set the used to zero, allocated digits to the default precision
-034 * and sign to positive */
-035 a->used = 0;
-036 a->alloc = MP_PREC;
-037 a->sign = MP_ZPOS;
-038
-039 return MP_OKAY;
-040 \}
-041 #endif
-042
\end{alltt}
\end{small}
@@ -822,7 +795,7 @@ One immediate observation of this initializtion function is that it does not ret
is assumed that the caller has already allocated memory for the mp\_int structure, typically on the application stack. The
call to mp\_init() is used only to initialize the members of the structure to a known default state.
-Here we see (line 23) the memory allocation is performed first. This allows us to exit cleanly and quickly
+Here we see (line 24) the memory allocation is performed first. This allows us to exit cleanly and quickly
if there is an error. If the allocation fails the routine will return \textbf{MP\_MEM} to the caller to indicate there
was a memory error. The function XMALLOC is what actually allocates the memory. Technically XMALLOC is not a function
but a macro defined in ``tommath.h``. By default, XMALLOC will evaluate to malloc() which is the C library's built--in
@@ -830,11 +803,11 @@ memory allocation routine.
In order to assure the mp\_int is in a known state the digits must be set to zero. On most platforms this could have been
accomplished by using calloc() instead of malloc(). However, to correctly initialize a integer type to a given value in a
-portable fashion you have to actually assign the value. The for loop (line 29) performs this required
+portable fashion you have to actually assign the value. The for loop (line 30) performs this required
operation.
After the memory has been successfully initialized the remainder of the members are initialized
-(lines 33 through 34) to their respective default states. At this point the algorithm has succeeded and
+(lines 34 through 35) to their respective default states. At this point the algorithm has succeeded and
a success code is returned to the calling function. If this function returns \textbf{MP\_OKAY} it is safe to assume the
mp\_int structure has been properly initialized and is safe to use with other functions within the library.
@@ -879,46 +852,21 @@ with the exception of algorithms mp\_init, mp\_init\_copy, mp\_init\_size and mp
\hspace{-5.1mm}{\bf File}: bn\_mp\_clear.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* clear one (frees) */
-018 void
-019 mp_clear (mp_int * a)
-020 \{
-021 int i;
-022
-023 /* only do anything if a hasn't been freed previously */
-024 if (a->dp != NULL) \{
-025 /* first zero the digits */
-026 for (i = 0; i < a->used; i++) \{
-027 a->dp[i] = 0;
-028 \}
-029
-030 /* free ram */
-031 XFREE(a->dp);
-032
-033 /* reset members to make debugging easier */
-034 a->dp = NULL;
-035 a->alloc = a->used = 0;
-036 a->sign = MP_ZPOS;
-037 \}
-038 \}
-039 #endif
-040
\end{alltt}
\end{small}
-The algorithm only operates on the mp\_int if it hasn't been previously cleared. The if statement (line 24)
+The algorithm only operates on the mp\_int if it hasn't been previously cleared. The if statement (line 25)
checks to see if the \textbf{dp} member is not \textbf{NULL}. If the mp\_int is a valid mp\_int then \textbf{dp} cannot be
\textbf{NULL} in which case the if statement will evaluate to true.
-The digits of the mp\_int are cleared by the for loop (line 26) which assigns a zero to every digit. Similar to mp\_init()
+The digits of the mp\_int are cleared by the for loop (line 27) which assigns a zero to every digit. Similar to mp\_init()
the digits are assigned zero instead of using block memory operations (such as memset()) since this is more portable.
The digits are deallocated off the heap via the XFREE macro. Similar to XMALLOC the XFREE macro actually evaluates to
a standard C library function. In this case the free() function. Since free() only deallocates the memory the pointer
-still has to be reset to \textbf{NULL} manually (line 34).
+still has to be reset to \textbf{NULL} manually (line 35).
-Now that the digits have been cleared and deallocated the other members are set to their final values (lines 35 and 36).
+Now that the digits have been cleared and deallocated the other members are set to their final values (lines 36 and 37).
\section{Maintenance Algorithms}
@@ -973,44 +921,6 @@ assumed to contain undefined values they are initially set to zero.
\hspace{-5.1mm}{\bf File}: bn\_mp\_grow.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* grow as required */
-018 int mp_grow (mp_int * a, int size)
-019 \{
-020 int i;
-021 mp_digit *tmp;
-022
-023 /* if the alloc size is smaller alloc more ram */
-024 if (a->alloc < size) \{
-025 /* ensure there are always at least MP_PREC digits extra on top */
-026 size += (MP_PREC * 2) - (size % MP_PREC);
-027
-028 /* reallocate the array a->dp
-029 *
-030 * We store the return in a temporary variable
-031 * in case the operation failed we don't want
-032 * to overwrite the dp member of a.
-033 */
-034 tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size);
-035 if (tmp == NULL) \{
-036 /* reallocation failed but "a" is still valid [can be freed] */
-037 return MP_MEM;
-038 \}
-039
-040 /* reallocation succeeded so set a->dp */
-041 a->dp = tmp;
-042
-043 /* zero excess digits */
-044 i = a->alloc;
-045 a->alloc = size;
-046 for (; i < a->alloc; i++) \{
-047 a->dp[i] = 0;
-048 \}
-049 \}
-050 return MP_OKAY;
-051 \}
-052 #endif
-053
\end{alltt}
\end{small}
@@ -1019,7 +929,7 @@ if the \textbf{alloc} member of the mp\_int is smaller than the requested digit
the function skips the re-allocation part thus saving time.
When a re-allocation is performed it is turned into an optimal request to save time in the future. The requested digit count is
-padded upwards to 2nd multiple of \textbf{MP\_PREC} larger than \textbf{alloc} (line 26). The XREALLOC function is used
+padded upwards to 2nd multiple of \textbf{MP\_PREC} larger than \textbf{alloc} (line 25). The XREALLOC function is used
to re-allocate the memory. As per the other functions XREALLOC is actually a macro which evaluates to realloc by default. The realloc
function leaves the base of the allocation intact which means the first \textbf{alloc} digits of the mp\_int are the same as before
the re-allocation. All that is left is to clear the newly allocated digits and return.
@@ -1071,46 +981,17 @@ correct no further memory re-allocations are required to work with the mp\_int.
\hspace{-5.1mm}{\bf File}: bn\_mp\_init\_size.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* init an mp_init for a given size */
-018 int mp_init_size (mp_int * a, int size)
-019 \{
-020 int x;
-021
-022 /* pad size so there are always extra digits */
-023 size += (MP_PREC * 2) - (size % MP_PREC);
-024
-025 /* alloc mem */
-026 a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
-027 if (a->dp == NULL) \{
-028 return MP_MEM;
-029 \}
-030
-031 /* set the members */
-032 a->used = 0;
-033 a->alloc = size;
-034 a->sign = MP_ZPOS;
-035
-036 /* zero the digits */
-037 for (x = 0; x < size; x++) \{
-038 a->dp[x] = 0;
-039 \}
-040
-041 return MP_OKAY;
-042 \}
-043 #endif
-044
\end{alltt}
\end{small}
-The number of digits $b$ requested is padded (line 23) by first augmenting it to the next multiple of
+The number of digits $b$ requested is padded (line 24) by first augmenting it to the next multiple of
\textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result. If the memory can be successfully allocated the
mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be
-returned (line 28).
+returned (line 29).
The digits are allocated and set to zero at the same time with the calloc() function (line @25,XCALLOC@). The
\textbf{used} count is set to zero, the \textbf{alloc} count set to the padded digit count and the \textbf{sign} flag set
-to \textbf{MP\_ZPOS} to achieve a default valid mp\_int state (lines 32, 33 and 34). If the function
+to \textbf{MP\_ZPOS} to achieve a default valid mp\_int state (lines 33, 34 and 35). If the function
returns succesfully then it is correct to assume that the mp\_int structure is in a valid state for the remainder of the
functions to work with.
@@ -1148,46 +1029,6 @@ initialization which allows for quick recovery from runtime errors.
\hspace{-5.1mm}{\bf File}: bn\_mp\_init\_multi.c
\vspace{-3mm}
\begin{alltt}
-016 #include <stdarg.h>
-017
-018 int mp_init_multi(mp_int *mp, ...)
-019 \{
-020 mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
-021 int n = 0; /* Number of ok inits */
-022 mp_int* cur_arg = mp;
-023 va_list args;
-024
-025 va_start(args, mp); /* init args to next argument from caller */
-026 while (cur_arg != NULL) \{
-027 if (mp_init(cur_arg) != MP_OKAY) \{
-028 /* Oops - error! Back-track and mp_clear what we already
-029 succeeded in init-ing, then return error.
-030 */
-031 va_list clean_args;
-032
-033 /* end the current list */
-034 va_end(args);
-035
-036 /* now start cleaning up */
-037 cur_arg = mp;
-038 va_start(clean_args, mp);
-039 while (n--) \{
-040 mp_clear(cur_arg);
-041 cur_arg = va_arg(clean_args, mp_int*);
-042 \}
-043 va_end(clean_args);
-044 res = MP_MEM;
-045 break;
-046 \}
-047 n++;
-048 cur_arg = va_arg(args, mp_int*);
-049 \}
-050 va_end(args);
-051 return res; /* Assumed ok, if error flagged above. */
-052 \}
-053
-054 #endif
-055
\end{alltt}
\end{small}
@@ -1197,8 +1038,8 @@ structures in an actual C array they are simply passed as arguments to the funct
appended on the right.
The function uses the ``stdarg.h'' \textit{va} functions to step portably through the arguments to the function. A count
-$n$ of succesfully initialized mp\_int structures is maintained (line 47) such that if a failure does occur,
-the algorithm can backtrack and free the previously initialized structures (lines 27 to 46).
+$n$ of succesfully initialized mp\_int structures is maintained (line 48) such that if a failure does occur,
+the algorithm can backtrack and free the previously initialized structures (lines 28 to 47).
\subsection{Clamping Excess Digits}
@@ -1249,38 +1090,13 @@ when all of the digits are zero to ensure that the mp\_int is valid at all times
\hspace{-5.1mm}{\bf File}: bn\_mp\_clamp.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* trim unused digits
-018 *
-019 * This is used to ensure that leading zero digits are
-020 * trimed and the leading "used" digit will be non-zero
-021 * Typically very fast. Also fixes the sign if there
-022 * are no more leading digits
-023 */
-024 void
-025 mp_clamp (mp_int * a)
-026 \{
-027 /* decrease used while the most significant digit is
-028 * zero.
-029 */
-030 while (a->used > 0 && a->dp[a->used - 1] == 0) \{
-031 --(a->used);
-032 \}
-033
-034 /* reset the sign flag if used == 0 */
-035 if (a->used == 0) \{
-036 a->sign = MP_ZPOS;
-037 \}
-038 \}
-039 #endif
-040
\end{alltt}
\end{small}
-Note on line 27 how to test for the \textbf{used} count is made on the left of the \&\& operator. In the C programming
+Note on line 28 how to test for the \textbf{used} count is made on the left of the \&\& operator. In the C programming
language the terms to \&\& are evaluated left to right with a boolean short-circuit if any condition fails. This is
important since if the \textbf{used} is zero the test on the right would fetch below the array. That is obviously
-undesirable. The parenthesis on line 30 is used to make sure the \textbf{used} count is decremented and not
+undesirable. The parenthesis on line 31 is used to make sure the \textbf{used} count is decremented and not
the pointer ``a''.
\section*{Exercises}
@@ -1363,70 +1179,21 @@ implement the pseudo-code.
\hspace{-5.1mm}{\bf File}: bn\_mp\_copy.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* copy, b = a */
-018 int
-019 mp_copy (mp_int * a, mp_int * b)
-020 \{
-021 int res, n;
-022
-023 /* if dst == src do nothing */
-024 if (a == b) \{
-025 return MP_OKAY;
-026 \}
-027
-028 /* grow dest */
-029 if (b->alloc < a->used) \{
-030 if ((res = mp_grow (b, a->used)) != MP_OKAY) \{
-031 return res;
-032 \}
-033 \}
-034
-035 /* zero b and copy the parameters over */
-036 \{
-037 register mp_digit *tmpa, *tmpb;
-038
-039 /* pointer aliases */
-040
-041 /* source */
-042 tmpa = a->dp;
-043
-044 /* destination */
-045 tmpb = b->dp;
-046
-047 /* copy all the digits */
-048 for (n = 0; n < a->used; n++) \{
-049 *tmpb++ = *tmpa++;
-050 \}
-051
-052 /* clear high digits */
-053 for (; n < b->used; n++) \{
-054 *tmpb++ = 0;
-055 \}
-056 \}
-057
-058 /* copy used count and sign */
-059 b->used = a->used;
-060 b->sign = a->sign;
-061 return MP_OKAY;
-062 \}
-063 #endif
-064
\end{alltt}
\end{small}
Occasionally a dependent algorithm may copy an mp\_int effectively into itself such as when the input and output
mp\_int structures passed to a function are one and the same. For this case it is optimal to return immediately without
-copying digits (line 24).
+copying digits (line 25).
The mp\_int $b$ must have enough digits to accomodate the used digits of the mp\_int $a$. If $b.alloc$ is less than
-$a.used$ the algorithm mp\_grow is used to augment the precision of $b$ (lines 29 to 33). In order to
+$a.used$ the algorithm mp\_grow is used to augment the precision of $b$ (lines 30 to 33). In order to
simplify the inner loop that copies the digits from $a$ to $b$, two aliases $tmpa$ and $tmpb$ point directly at the digits
-of the mp\_ints $a$ and $b$ respectively. These aliases (lines 42 and 45) allow the compiler to access the digits without first dereferencing the
+of the mp\_ints $a$ and $b$ respectively. These aliases (lines 43 and 46) allow the compiler to access the digits without first dereferencing the
mp\_int pointers and then subsequently the pointer to the digits.
-After the aliases are established the digits from $a$ are copied into $b$ (lines 48 to 50) and then the excess
-digits of $b$ are set to zero (lines 53 to 55). Both ``for'' loops make use of the pointer aliases and in
+After the aliases are established the digits from $a$ are copied into $b$ (lines 49 to 51) and then the excess
+digits of $b$ are set to zero (lines 54 to 56). Both ``for'' loops make use of the pointer aliases and in
fact the alias for $b$ is carried through into the second ``for'' loop to clear the excess digits. This optimization
allows the alias to stay in a machine register fairly easy between the two loops.
@@ -1514,19 +1281,6 @@ such this algorithm will perform two operations in one step.
\hspace{-5.1mm}{\bf File}: bn\_mp\_init\_copy.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* creates "a" then copies b into it */
-018 int mp_init_copy (mp_int * a, mp_int * b)
-019 \{
-020 int res;
-021
-022 if ((res = mp_init (a)) != MP_OKAY) \{
-023 return res;
-024 \}
-025 return mp_copy (b, a);
-026 \}
-027 #endif
-028
\end{alltt}
\end{small}
@@ -1562,23 +1316,6 @@ This algorithm simply resets a mp\_int to the default state.
\hspace{-5.1mm}{\bf File}: bn\_mp\_zero.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* set to zero */
-018 void mp_zero (mp_int * a)
-019 \{
-020 int n;
-021 mp_digit *tmp;
-022
-023 a->sign = MP_ZPOS;
-024 a->used = 0;
-025
-026 tmp = a->dp;
-027 for (n = 0; n < a->alloc; n++) \{
-028 *tmp++ = 0;
-029 \}
-030 \}
-031 #endif
-032
\end{alltt}
\end{small}
@@ -1617,34 +1354,10 @@ logic to handle it.
\hspace{-5.1mm}{\bf File}: bn\_mp\_abs.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* b = |a|
-018 *
-019 * Simple function copies the input and fixes the sign to positive
-020 */
-021 int
-022 mp_abs (mp_int * a, mp_int * b)
-023 \{
-024 int res;
-025
-026 /* copy a to b */
-027 if (a != b) \{
-028 if ((res = mp_copy (a, b)) != MP_OKAY) \{
-029 return res;
-030 \}
-031 \}
-032
-033 /* force the sign of b to positive */
-034 b->sign = MP_ZPOS;
-035
-036 return MP_OKAY;
-037 \}
-038 #endif
-039
\end{alltt}
\end{small}
-This fairly trivial algorithm first eliminates non--required duplications (line 27) and then sets the
+This fairly trivial algorithm first eliminates non--required duplications (line 28) and then sets the
\textbf{sign} flag to \textbf{MP\_ZPOS}.
\subsection{Integer Negation}
@@ -1682,31 +1395,10 @@ zero as negative.
\hspace{-5.1mm}{\bf File}: bn\_mp\_neg.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* b = -a */
-018 int mp_neg (mp_int * a, mp_int * b)
-019 \{
-020 int res;
-021 if (a != b) \{
-022 if ((res = mp_copy (a, b)) != MP_OKAY) \{
-023 return res;
-024 \}
-025 \}
-026
-027 if (mp_iszero(b) != MP_YES) \{
-028 b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
-029 \} else \{
-030 b->sign = MP_ZPOS;
-031 \}
-032
-033 return MP_OKAY;
-034 \}
-035 #endif
-036
\end{alltt}
\end{small}
-Like mp\_abs() this function avoids non--required duplications (line 21) and then sets the sign. We
+Like mp\_abs() this function avoids non--required duplications (line 22) and then sets the sign. We
have to make sure that only non--zero values get a \textbf{sign} of \textbf{MP\_NEG}. If the mp\_int is zero
than the \textbf{sign} is hard--coded to \textbf{MP\_ZPOS}.
@@ -1741,22 +1433,12 @@ single digit is set (\textit{modulo $\beta$}) and the \textbf{used} count is adj
\hspace{-5.1mm}{\bf File}: bn\_mp\_set.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* set to a digit */
-018 void mp_set (mp_int * a, mp_digit b)
-019 \{
-020 mp_zero (a);
-021 a->dp[0] = b & MP_MASK;
-022 a->used = (a->dp[0] != 0) ? 1 : 0;
-023 \}
-024 #endif
-025
\end{alltt}
\end{small}
-First we zero (line 20) the mp\_int to make sure that the other members are initialized for a
+First we zero (line 21) the mp\_int to make sure that the other members are initialized for a
small positive constant. mp\_zero() ensures that the \textbf{sign} is positive and the \textbf{used} count
-is zero. Next we set the digit and reduce it modulo $\beta$ (line 21). After this step we have to
+is zero. Next we set the digit and reduce it modulo $\beta$ (line 22). After this step we have to
check if the resulting digit is zero or not. If it is not then we set the \textbf{used} count to one, otherwise
to zero.
@@ -1803,42 +1485,13 @@ Excess zero digits are trimmed in steps 2.1 and 3 by using higher level algorith
\hspace{-5.1mm}{\bf File}: bn\_mp\_set\_int.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* set a 32-bit const */
-018 int mp_set_int (mp_int * a, unsigned long b)
-019 \{
-020 int x, res;
-021
-022 mp_zero (a);
-023
-024 /* set four bits at a time */
-025 for (x = 0; x < 8; x++) \{
-026 /* shift the number up four bits */
-027 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) \{
-028 return res;
-029 \}
-030
-031 /* OR in the top four bits of the source */
-032 a->dp[0] |= (b >> 28) & 15;
-033
-034 /* shift the source up to the next four bits */
-035 b <<= 4;
-036
-037 /* ensure that digits are not clamped off */
-038 a->used += 1;
-039 \}
-040 mp_clamp (a);
-041 return MP_OKAY;
-042 \}
-043 #endif
-044
\end{alltt}
\end{small}
This function sets four bits of the number at a time to handle all practical \textbf{DIGIT\_BIT} sizes. The weird
-addition on line 38 ensures that the newly added in bits are added to the number of digits. While it may not
-seem obvious as to why the digit counter does not grow exceedingly large it is because of the shift on line 27
-as well as the call to mp\_clamp() on line 40. Both functions will clamp excess leading digits which keeps
+addition on line 39 ensures that the newly added in bits are added to the number of digits. While it may not
+seem obvious as to why the digit counter does not grow exceedingly large it is because of the shift on line 28
+as well as the call to mp\_clamp() on line 41. Both functions will clamp excess leading digits which keeps
the number of used digits low.
\section{Comparisons}
@@ -1899,46 +1552,10 @@ the zero'th digit. If after all of the digits have been compared, no difference
\hspace{-5.1mm}{\bf File}: bn\_mp\_cmp\_mag.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* compare maginitude of two ints (unsigned) */
-018 int mp_cmp_mag (mp_int * a, mp_int * b)
-019 \{
-020 int n;
-021 mp_digit *tmpa, *tmpb;
-022
-023 /* compare based on # of non-zero digits */
-024 if (a->used > b->used) \{
-025 return MP_GT;
-026 \}
-027
-028 if (a->used < b->used) \{
-029 return MP_LT;
-030 \}
-031
-032 /* alias for a */
-033 tmpa = a->dp + (a->used - 1);
-034
-035 /* alias for b */
-036 tmpb = b->dp + (a->used - 1);
-037
-038 /* compare based on digits */
-039 for (n = 0; n < a->used; ++n, --tmpa, --tmpb) \{
-040 if (*tmpa > *tmpb) \{
-041 return MP_GT;
-042 \}
-043
-044 if (*tmpa < *tmpb) \{
-045 return MP_LT;
-046 \}
-047 \}
-048 return MP_EQ;
-049 \}
-050 #endif
-051
\end{alltt}
\end{small}
-The two if statements (lines 24 and 28) compare the number of digits in the two inputs. These two are
+The two if statements (lines 25 and 29) compare the number of digits in the two inputs. These two are
performed before all of the digits are compared since it is a very cheap test to perform and can potentially save
considerable time. The implementation given is also not valid without those two statements. $b.alloc$ may be
smaller than $a.used$, meaning that undefined values will be read from $b$ past the end of the array of digits.
@@ -1978,36 +1595,12 @@ $\vert a \vert < \vert b \vert$. Step number four will compare the two when the
\hspace{-5.1mm}{\bf File}: bn\_mp\_cmp.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* compare two ints (signed)*/
-018 int
-019 mp_cmp (mp_int * a, mp_int * b)
-020 \{
-021 /* compare based on sign */
-022 if (a->sign != b->sign) \{
-023 if (a->sign == MP_NEG) \{
-024 return MP_LT;
-025 \} else \{
-026 return MP_GT;
-027 \}
-028 \}
-029
-030 /* compare digits */
-031 if (a->sign == MP_NEG) \{
-032 /* if negative compare opposite direction */
-033 return mp_cmp_mag(b, a);
-034 \} else \{
-035 return mp_cmp_mag(a, b);
-036 \}
-037 \}
-038 #endif
-039
\end{alltt}
\end{small}
-The two if statements (lines 22 and 23) perform the initial sign comparison. If the signs are not the equal then which ever
-has the positive sign is larger. The inputs are compared (line 31) based on magnitudes. If the signs were both
-negative then the unsigned comparison is performed in the opposite direction (line 33). Otherwise, the signs are assumed to
+The two if statements (lines 23 and 24) perform the initial sign comparison. If the signs are not the equal then which ever
+has the positive sign is larger. The inputs are compared (line 32) based on magnitudes. If the signs were both
+negative then the unsigned comparison is performed in the opposite direction (line 34). Otherwise, the signs are assumed to
be both positive and a forward direction unsigned comparison is performed.
\section*{Exercises}
@@ -2131,114 +1724,24 @@ The final carry is stored in $c_{max}$ and digits above $max$ upto $oldused$ are
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_add.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
-018 int
-019 s_mp_add (mp_int * a, mp_int * b, mp_int * c)
-020 \{
-021 mp_int *x;
-022 int olduse, res, min, max;
-023
-024 /* find sizes, we let |a| <= |b| which means we have to sort
-025 * them. "x" will point to the input with the most digits
-026 */
-027 if (a->used > b->used) \{
-028 min = b->used;
-029 max = a->used;
-030 x = a;
-031 \} else \{
-032 min = a->used;
-033 max = b->used;
-034 x = b;
-035 \}
-036
-037 /* init result */
-038 if (c->alloc < max + 1) \{
-039 if ((res = mp_grow (c, max + 1)) != MP_OKAY) \{
-040 return res;
-041 \}
-042 \}
-043
-044 /* get old used digit count and set new one */
-045 olduse = c->used;
-046 c->used = max + 1;
-047
-048 \{
-049 register mp_digit u, *tmpa, *tmpb, *tmpc;
-050 register int i;
-051
-052 /* alias for digit pointers */
-053
-054 /* first input */
-055 tmpa = a->dp;
-056
-057 /* second input */
-058 tmpb = b->dp;
-059
-060 /* destination */
-061 tmpc = c->dp;
-062
-063 /* zero the carry */
-064 u = 0;
-065 for (i = 0; i < min; i++) \{
-066 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
-067 *tmpc = *tmpa++ + *tmpb++ + u;
-068
-069 /* U = carry bit of T[i] */
-070 u = *tmpc >> ((mp_digit)DIGIT_BIT);
-071
-072 /* take away carry bit from T[i] */
-073 *tmpc++ &= MP_MASK;
-074 \}
-075
-076 /* now copy higher words if any, that is in A+B
-077 * if A or B has more digits add those in
-078 */
-079 if (min != max) \{
-080 for (; i < max; i++) \{
-081 /* T[i] = X[i] + U */
-082 *tmpc = x->dp[i] + u;
-083
-084 /* U = carry bit of T[i] */
-085 u = *tmpc >> ((mp_digit)DIGIT_BIT);
-086
-087 /* take away carry bit from T[i] */
-088 *tmpc++ &= MP_MASK;
-089 \}
-090 \}
-091
-092 /* add carry */
-093 *tmpc++ = u;
-094
-095 /* clear digits above oldused */
-096 for (i = c->used; i < olduse; i++) \{
-097 *tmpc++ = 0;
-098 \}
-099 \}
-100
-101 mp_clamp (c);
-102 return MP_OKAY;
-103 \}
-104 #endif
-105
\end{alltt}
\end{small}
-We first sort (lines 27 to 35) the inputs based on magnitude and determine the $min$ and $max$ variables.
+We first sort (lines 28 to 36) the inputs based on magnitude and determine the $min$ and $max$ variables.
Note that $x$ is a pointer to an mp\_int assigned to the largest input, in effect it is a local alias. Next we
-grow the destination (37 to 42) ensure that it can accomodate the result of the addition.
+grow the destination (38 to 42) ensure that it can accomodate the result of the addition.
Similar to the implementation of mp\_copy this function uses the braced code and local aliases coding style. The three aliases that are on
-lines 55, 58 and 61 represent the two inputs and destination variables respectively. These aliases are used to ensure the
+lines 56, 59 and 62 represent the two inputs and destination variables respectively. These aliases are used to ensure the
compiler does not have to dereference $a$, $b$ or $c$ (respectively) to access the digits of the respective mp\_int.
-The initial carry $u$ will be cleared (line 64), note that $u$ is of type mp\_digit which ensures type
-compatibility within the implementation. The initial addition (line 65 to 74) adds digits from
+The initial carry $u$ will be cleared (line 65), note that $u$ is of type mp\_digit which ensures type
+compatibility within the implementation. The initial addition (line 66 to 75) adds digits from
both inputs until the smallest input runs out of digits. Similarly the conditional addition loop
-(line 80 to 90) adds the remaining digits from the larger of the two inputs. The addition is finished
-with the final carry being stored in $tmpc$ (line 93). Note the ``++'' operator within the same expression.
-After line 93, $tmpc$ will point to the $c.used$'th digit of the mp\_int $c$. This is useful
-for the next loop (line 96 to 99) which set any old upper digits to zero.
+(line 81 to 90) adds the remaining digits from the larger of the two inputs. The addition is finished
+with the final carry being stored in $tmpc$ (line 94). Note the ``++'' operator within the same expression.
+After line 94, $tmpc$ will point to the $c.used$'th digit of the mp\_int $c$. This is useful
+for the next loop (line 97 to 99) which set any old upper digits to zero.
\subsection{Low Level Subtraction}
The low level unsigned subtraction algorithm is very similar to the low level unsigned addition algorithm. The principle difference is that the
@@ -2322,96 +1825,25 @@ If $b$ has a smaller magnitude than $a$ then step 9 will force the carry and cop
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_sub.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
-018 int
-019 s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
-020 \{
-021 int olduse, res, min, max;
-022
-023 /* find sizes */
-024 min = b->used;
-025 max = a->used;
-026
-027 /* init result */
-028 if (c->alloc < max) \{
-029 if ((res = mp_grow (c, max)) != MP_OKAY) \{
-030 return res;
-031 \}
-032 \}
-033 olduse = c->used;
-034 c->used = max;
-035
-036 \{
-037 register mp_digit u, *tmpa, *tmpb, *tmpc;
-038 register int i;
-039
-040 /* alias for digit pointers */
-041 tmpa = a->dp;
-042 tmpb = b->dp;
-043 tmpc = c->dp;
-044
-045 /* set carry to zero */
-046 u = 0;
-047 for (i = 0; i < min; i++) \{
-048 /* T[i] = A[i] - B[i] - U */
-049 *tmpc = *tmpa++ - *tmpb++ - u;
-050
-051 /* U = carry bit of T[i]
-052 * Note this saves performing an AND operation since
-053 * if a carry does occur it will propagate all the way to the
-054 * MSB. As a result a single shift is enough to get the carry
-055 */
-056 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
-057
-058 /* Clear carry from T[i] */
-059 *tmpc++ &= MP_MASK;
-060 \}
-061
-062 /* now copy higher words if any, e.g. if A has more digits than B */
-063 for (; i < max; i++) \{
-064 /* T[i] = A[i] - U */
-065 *tmpc = *tmpa++ - u;
-066
-067 /* U = carry bit of T[i] */
-068 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
-069
-070 /* Clear carry from T[i] */
-071 *tmpc++ &= MP_MASK;
-072 \}
-073
-074 /* clear digits above used (since we may not have grown result above) */
-
-075 for (i = c->used; i < olduse; i++) \{
-076 *tmpc++ = 0;
-077 \}
-078 \}
-079
-080 mp_clamp (c);
-081 return MP_OKAY;
-082 \}
-083
-084 #endif
-085
\end{alltt}
\end{small}
Like low level addition we ``sort'' the inputs. Except in this case the sorting is hardcoded
-(lines 24 and 25). In reality the $min$ and $max$ variables are only aliases and are only
+(lines 25 and 26). In reality the $min$ and $max$ variables are only aliases and are only
used to make the source code easier to read. Again the pointer alias optimization is used
within this algorithm. The aliases $tmpa$, $tmpb$ and $tmpc$ are initialized
-(lines 41, 42 and 43) for $a$, $b$ and $c$ respectively.
+(lines 42, 43 and 44) for $a$, $b$ and $c$ respectively.
-The first subtraction loop (lines 46 through 60) subtract digits from both inputs until the smaller of
+The first subtraction loop (lines 47 through 61) subtract digits from both inputs until the smaller of
the two inputs has been exhausted. As remarked earlier there is an implementation reason for using the ``awkward''
-method of extracting the carry (line 56). The traditional method for extracting the carry would be to shift
+method of extracting the carry (line 57). The traditional method for extracting the carry would be to shift
by $lg(\beta)$ positions and logically AND the least significant bit. The AND operation is required because all of
the bits above the $\lg(\beta)$'th bit will be set to one after a carry occurs from subtraction. This carry
extraction requires two relatively cheap operations to extract the carry. The other method is to simply shift the
most significant bit to the least significant bit thus extracting the carry with a single cheap operation. This
optimization only works on twos compliment machines which is a safe assumption to make.
-If $a$ has a larger magnitude than $b$ an additional loop (lines 63 through 72) is required to propagate
+If $a$ has a larger magnitude than $b$ an additional loop (lines 64 through 73) is required to propagate
the carry through $a$ and copy the result to $c$.
\subsection{High Level Addition}
@@ -2495,40 +1927,6 @@ within algorithm s\_mp\_add will force $-0$ to become $0$.
\hspace{-5.1mm}{\bf File}: bn\_mp\_add.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* high level addition (handles signs) */
-018 int mp_add (mp_int * a, mp_int * b, mp_int * c)
-019 \{
-020 int sa, sb, res;
-021
-022 /* get sign of both inputs */
-023 sa = a->sign;
-024 sb = b->sign;
-025
-026 /* handle two cases, not four */
-027 if (sa == sb) \{
-028 /* both positive or both negative */
-029 /* add their magnitudes, copy the sign */
-030 c->sign = sa;
-031 res = s_mp_add (a, b, c);
-032 \} else \{
-033 /* one positive, the other negative */
-034 /* subtract the one with the greater magnitude from */
-035 /* the one of the lesser magnitude. The result gets */
-036 /* the sign of the one with the greater magnitude. */
-037 if (mp_cmp_mag (a, b) == MP_LT) \{
-038 c->sign = sb;
-039 res = s_mp_sub (b, a, c);
-040 \} else \{
-041 c->sign = sa;
-042 res = s_mp_sub (a, b, c);
-043 \}
-044 \}
-045 return res;
-046 \}
-047
-048 #endif
-049
\end{alltt}
\end{small}
@@ -2602,51 +2000,11 @@ algorithm from producing $-a - -a = -0$ as a result.
\hspace{-5.1mm}{\bf File}: bn\_mp\_sub.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* high level subtraction (handles signs) */
-018 int
-019 mp_sub (mp_int * a, mp_int * b, mp_int * c)
-020 \{
-021 int sa, sb, res;
-022
-023 sa = a->sign;
-024 sb = b->sign;
-025
-026 if (sa != sb) \{
-027 /* subtract a negative from a positive, OR */
-028 /* subtract a positive from a negative. */
-029 /* In either case, ADD their magnitudes, */
-030 /* and use the sign of the first number. */
-031 c->sign = sa;
-032 res = s_mp_add (a, b, c);
-033 \} else \{
-034 /* subtract a positive from a positive, OR */
-035 /* subtract a negative from a negative. */
-036 /* First, take the difference between their */
-037 /* magnitudes, then... */
-038 if (mp_cmp_mag (a, b) != MP_LT) \{
-039 /* Copy the sign from the first */
-040 c->sign = sa;
-041 /* The first has a larger or equal magnitude */
-042 res = s_mp_sub (a, b, c);
-043 \} else \{
-044 /* The result has the *opposite* sign from */
-045 /* the first number. */
-046 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
-047 /* The second has a larger magnitude */
-048 res = s_mp_sub (b, a, c);
-049 \}
-050 \}
-051 return res;
-052 \}
-053
-054 #endif
-055
\end{alltt}
\end{small}
Much like the implementation of algorithm mp\_add the variable $res$ is used to catch the return code of the unsigned addition or subtraction operations
-and forward it to the end of the function. On line 38 the ``not equal to'' \textbf{MP\_LT} expression is used to emulate a
+and forward it to the end of the function. On line 39 the ``not equal to'' \textbf{MP\_LT} expression is used to emulate a
``greater than or equal to'' comparison.
\section{Bit and Digit Shifting}
@@ -2714,74 +2072,11 @@ Step 8 clears any leading digits of $b$ in case it originally had a larger magni
\hspace{-5.1mm}{\bf File}: bn\_mp\_mul\_2.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* b = a*2 */
-018 int mp_mul_2(mp_int * a, mp_int * b)
-019 \{
-020 int x, res, oldused;
-021
-022 /* grow to accomodate result */
-023 if (b->alloc < a->used + 1) \{
-024 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) \{
-025 return res;
-026 \}
-027 \}
-028
-029 oldused = b->used;
-030 b->used = a->used;
-031
-032 \{
-033 register mp_digit r, rr, *tmpa, *tmpb;
-034
-035 /* alias for source */
-036 tmpa = a->dp;
-037
-038 /* alias for dest */
-039 tmpb = b->dp;
-040
-041 /* carry */
-042 r = 0;
-043 for (x = 0; x < a->used; x++) \{
-044
-045 /* get what will be the *next* carry bit from the
-046 * MSB of the current digit
-047 */
-048 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
-049
-050 /* now shift up this digit, add in the carry [from the previous] */
-051 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
-052
-053 /* copy the carry that would be from the source
-054 * digit into the next iteration
-055 */
-056 r = rr;
-057 \}
-058
-059 /* new leading digit? */
-060 if (r != 0) \{
-061 /* add a MSB which is always 1 at this point */
-062 *tmpb = 1;
-063 ++(b->used);
-064 \}
-065
-066 /* now zero any excess digits on the destination
-067 * that we didn't write to
-068 */
-069 tmpb = b->dp + b->used;
-070 for (x = b->used; x < oldused; x++) \{
-071 *tmpb++ = 0;
-072 \}
-073 \}
-074 b->sign = a->sign;
-075 return MP_OKAY;
-076 \}
-077 #endif
-078
\end{alltt}
\end{small}
This implementation is essentially an optimized implementation of s\_mp\_add for the case of doubling an input. The only noteworthy difference
-is the use of the logical shift operator on line 51 to perform a single precision doubling.
+is the use of the logical shift operator on line 52 to perform a single precision doubling.
\subsection{Division by Two}
A division by two can just as easily be accomplished with a logical shift right as multiplication by two can be with a logical shift left.
@@ -2829,55 +2124,6 @@ least significant bit not the most significant bit.
\hspace{-5.1mm}{\bf File}: bn\_mp\_div\_2.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* b = a/2 */
-018 int mp_div_2(mp_int * a, mp_int * b)
-019 \{
-020 int x, res, oldused;
-021
-022 /* copy */
-023 if (b->alloc < a->used) \{
-024 if ((res = mp_grow (b, a->used)) != MP_OKAY) \{
-025 return res;
-026 \}
-027 \}
-028
-029 oldused = b->used;
-030 b->used = a->used;
-031 \{
-032 register mp_digit r, rr, *tmpa, *tmpb;
-033
-034 /* source alias */
-035 tmpa = a->dp + b->used - 1;
-036
-037 /* dest alias */
-038 tmpb = b->dp + b->used - 1;
-039
-040 /* carry */
-041 r = 0;
-042 for (x = b->used - 1; x >= 0; x--) \{
-043 /* get the carry for the next iteration */
-044 rr = *tmpa & 1;
-045
-046 /* shift the current digit, add in carry and store */
-047 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
-048
-049 /* forward carry to next iteration */
-050 r = rr;
-051 \}
-052
-053 /* zero excess digits */
-054 tmpb = b->dp + b->used;
-055 for (x = b->used; x < oldused; x++) \{
-056 *tmpb++ = 0;
-057 \}
-058 \}
-059 b->sign = a->sign;
-060 mp_clamp (b);
-061 return MP_OKAY;
-062 \}
-063 #endif
-064
\end{alltt}
\end{small}
@@ -2951,61 +2197,13 @@ step 8 sets the lower $b$ digits to zero.
\hspace{-5.1mm}{\bf File}: bn\_mp\_lshd.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* shift left a certain amount of digits */
-018 int mp_lshd (mp_int * a, int b)
-019 \{
-020 int x, res;
-021
-022 /* if its less than zero return */
-023 if (b <= 0) \{
-024 return MP_OKAY;
-025 \}
-026
-027 /* grow to fit the new digits */
-028 if (a->alloc < a->used + b) \{
-029 if ((res = mp_grow (a, a->used + b)) != MP_OKAY) \{
-030 return res;
-031 \}
-032 \}
-033
-034 \{
-035 register mp_digit *top, *bottom;
-036
-037 /* increment the used by the shift amount then copy upwards */
-038 a->used += b;
-039
-040 /* top */
-041 top = a->dp + a->used - 1;
-042
-043 /* base */
-044 bottom = a->dp + a->used - 1 - b;
-045
-046 /* much like mp_rshd this is implemented using a sliding window
-047 * except the window goes the otherway around. Copying from
-048 * the bottom to the top. see bn_mp_rshd.c for more info.
-049 */
-050 for (x = a->used - 1; x >= b; x--) \{
-051 *top-- = *bottom--;
-052 \}
-053
-054 /* zero the lower digits */
-055 top = a->dp;
-056 for (x = 0; x < b; x++) \{
-057 *top++ = 0;
-058 \}
-059 \}
-060 return MP_OKAY;
-061 \}
-062 #endif
-063
\end{alltt}
\end{small}
-The if statement (line 23) ensures that the $b$ variable is greater than zero since we do not interpret negative
+The if statement (line 24) ensures that the $b$ variable is greater than zero since we do not interpret negative
shift counts properly. The \textbf{used} count is incremented by $b$ before the copy loop begins. This elminates
-the need for an additional variable in the for loop. The variable $top$ (line 41) is an alias
-for the leading digit while $bottom$ (line 44) is an alias for the trailing edge. The aliases form a
+the need for an additional variable in the for loop. The variable $top$ (line 42) is an alias
+for the leading digit while $bottom$ (line 45) is an alias for the trailing edge. The aliases form a
window of exactly $b$ digits over the input.
\subsection{Division by $x$}
@@ -3058,64 +2256,11 @@ Once the window copy is complete the upper digits must be zeroed and the \textbf
\hspace{-5.1mm}{\bf File}: bn\_mp\_rshd.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* shift right a certain amount of digits */
-018 void mp_rshd (mp_int * a, int b)
-019 \{
-020 int x;
-021
-022 /* if b <= 0 then ignore it */
-023 if (b <= 0) \{
-024 return;
-025 \}
-026
-027 /* if b > used then simply zero it and return */
-028 if (a->used <= b) \{
-029 mp_zero (a);
-030 return;
-031 \}
-032
-033 \{
-034 register mp_digit *bottom, *top;
-035
-036 /* shift the digits down */
-037
-038 /* bottom */
-039 bottom = a->dp;
-040
-041 /* top [offset into digits] */
-042 top = a->dp + b;
-043
-044 /* this is implemented as a sliding window where
-045 * the window is b-digits long and digits from
-046 * the top of the window are copied to the bottom
-047 *
-048 * e.g.
-049
-050 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
-051 /\symbol{92} | ---->
-052 \symbol{92}-------------------/ ---->
-053 */
-054 for (x = 0; x < (a->used - b); x++) \{
-055 *bottom++ = *top++;
-056 \}
-057
-058 /* zero the top digits */
-059 for (; x < a->used; x++) \{
-060 *bottom++ = 0;
-061 \}
-062 \}
-063
-064 /* remove excess digits */
-065 a->used -= b;
-066 \}
-067 #endif
-068
\end{alltt}
\end{small}
The only noteworthy element of this routine is the lack of a return type since it cannot fail. Like mp\_lshd() we
-form a sliding window except we copy in the other direction. After the window (line 59) we then zero
+form a sliding window except we copy in the other direction. After the window (line 60) we then zero
the upper digits of the input to make sure the result is correct.
\section{Powers of Two}
@@ -3179,82 +2324,16 @@ complete. It is possible to optimize this algorithm down to a $O(n)$ algorithm
\hspace{-5.1mm}{\bf File}: bn\_mp\_mul\_2d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* shift left by a certain bit count */
-018 int mp_mul_2d (mp_int * a, int b, mp_int * c)
-019 \{
-020 mp_digit d;
-021 int res;
-022
-023 /* copy */
-024 if (a != c) \{
-025 if ((res = mp_copy (a, c)) != MP_OKAY) \{
-026 return res;
-027 \}
-028 \}
-029
-030 if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) \{
-031 if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) \{
-032 return res;
-033 \}
-034 \}
-035
-036 /* shift by as many digits in the bit count */
-037 if (b >= (int)DIGIT_BIT) \{
-038 if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) \{
-039 return res;
-040 \}
-041 \}
-042
-043 /* shift any bit count < DIGIT_BIT */
-044 d = (mp_digit) (b % DIGIT_BIT);
-045 if (d != 0) \{
-046 register mp_digit *tmpc, shift, mask, r, rr;
-047 register int x;
-048
-049 /* bitmask for carries */
-050 mask = (((mp_digit)1) << d) - 1;
-051
-052 /* shift for msbs */
-053 shift = DIGIT_BIT - d;
-054
-055 /* alias */
-056 tmpc = c->dp;
-057
-058 /* carry */
-059 r = 0;
-060 for (x = 0; x < c->used; x++) \{
-061 /* get the higher bits of the current word */
-062 rr = (*tmpc >> shift) & mask;
-063
-064 /* shift the current word and OR in the carry */
-065 *tmpc = ((*tmpc << d) | r) & MP_MASK;
-066 ++tmpc;
-067
-068 /* set the carry to the carry bits of the current word */
-069 r = rr;
-070 \}
-071
-072 /* set final carry */
-073 if (r != 0) \{
-074 c->dp[(c->used)++] = r;
-075 \}
-076 \}
-077 mp_clamp (c);
-078 return MP_OKAY;
-079 \}
-080 #endif
-081
\end{alltt}
\end{small}
-The shifting is performed in--place which means the first step (line 24) is to copy the input to the
+The shifting is performed in--place which means the first step (line 25) is to copy the input to the
destination. We avoid calling mp\_copy() by making sure the mp\_ints are different. The destination then
-has to be grown (line 31) to accomodate the result.
+has to be grown (line 32) to accomodate the result.
If the shift count $b$ is larger than $lg(\beta)$ then a call to mp\_lshd() is used to handle all of the multiples
of $lg(\beta)$. Leaving only a remaining shift of $lg(\beta) - 1$ or fewer bits left. Inside the actual shift
-loop (lines 45 to 76) we make use of pre--computed values $shift$ and $mask$. These are used to
+loop (lines 46 to 76) we make use of pre--computed values $shift$ and $mask$. These are used to
extract the carry bit(s) to pass into the next iteration of the loop. The $r$ and $rr$ variables form a
chain between consecutive iterations to propagate the carry.
@@ -3302,86 +2381,6 @@ by using algorithm mp\_mod\_2d.
\hspace{-5.1mm}{\bf File}: bn\_mp\_div\_2d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* shift right by a certain bit count (store quotient in c, optional remaind
- er in d) */
-018 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
-019 \{
-020 mp_digit D, r, rr;
-021 int x, res;
-022 mp_int t;
-023
-024
-025 /* if the shift count is <= 0 then we do no work */
-026 if (b <= 0) \{
-027 res = mp_copy (a, c);
-028 if (d != NULL) \{
-029 mp_zero (d);
-030 \}
-031 return res;
-032 \}
-033
-034 if ((res = mp_init (&t)) != MP_OKAY) \{
-035 return res;
-036 \}
-037
-038 /* get the remainder */
-039 if (d != NULL) \{
-040 if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) \{
-041 mp_clear (&t);
-042 return res;
-043 \}
-044 \}
-045
-046 /* copy */
-047 if ((res = mp_copy (a, c)) != MP_OKAY) \{
-048 mp_clear (&t);
-049 return res;
-050 \}
-051
-052 /* shift by as many digits in the bit count */
-053 if (b >= (int)DIGIT_BIT) \{
-054 mp_rshd (c, b / DIGIT_BIT);
-055 \}
-056
-057 /* shift any bit count < DIGIT_BIT */
-058 D = (mp_digit) (b % DIGIT_BIT);
-059 if (D != 0) \{
-060 register mp_digit *tmpc, mask, shift;
-061
-062 /* mask */
-063 mask = (((mp_digit)1) << D) - 1;
-064
-065 /* shift for lsb */
-066 shift = DIGIT_BIT - D;
-067
-068 /* alias */
-069 tmpc = c->dp + (c->used - 1);
-070
-071 /* carry */
-072 r = 0;
-073 for (x = c->used - 1; x >= 0; x--) \{
-074 /* get the lower bits of this word in a temp */
-075 rr = *tmpc & mask;
-076
-077 /* shift the current word and mix in the carry bits from the previous
- word */
-078 *tmpc = (*tmpc >> D) | (r << shift);
-079 --tmpc;
-080
-081 /* set the carry to the carry bits of the current word found above */
-082 r = rr;
-083 \}
-084 \}
-085 mp_clamp (c);
-086 if (d != NULL) \{
-087 mp_exch (&t, d);
-088 \}
-089 mp_clear (&t);
-090 return MP_OKAY;
-091 \}
-092 #endif
-093
\end{alltt}
\end{small}
@@ -3436,44 +2435,6 @@ is copied to $b$, leading digits are removed and the remaining leading digit is
\hspace{-5.1mm}{\bf File}: bn\_mp\_mod\_2d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* calc a value mod 2**b */
-018 int
-019 mp_mod_2d (mp_int * a, int b, mp_int * c)
-020 \{
-021 int x, res;
-022
-023 /* if b is <= 0 then zero the int */
-024 if (b <= 0) \{
-025 mp_zero (c);
-026 return MP_OKAY;
-027 \}
-028
-029 /* if the modulus is larger than the value than return */
-030 if (b >= (int) (a->used * DIGIT_BIT)) \{
-031 res = mp_copy (a, c);
-032 return res;
-033 \}
-034
-035 /* copy */
-036 if ((res = mp_copy (a, c)) != MP_OKAY) \{
-037 return res;
-038 \}
-039
-040 /* zero digits above the last digit of the modulus */
-041 for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x+
- +) \{
-042 c->dp[x] = 0;
-043 \}
-044 /* clear the digit that is not completely outside/inside the modulus */
-045 c->dp[b / DIGIT_BIT] &=
-046 (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digi
- t) 1));
-047 mp_clamp (c);
-048 return MP_OKAY;
-049 \}
-050 #endif
-051
\end{alltt}
\end{small}
@@ -3482,8 +2443,8 @@ than the input we just mp\_copy() the input and return right away. After this p
perform some work to produce the remainder.
Recalling that reducing modulo $2^k$ and a binary ``and'' with $2^k - 1$ are numerically equivalent we can quickly reduce
-the number. First we zero any digits above the last digit in $2^b$ (line 41). Next we reduce the
-leading digit of both (line 45) and then mp\_clamp().
+the number. First we zero any digits above the last digit in $2^b$ (line 42). Next we reduce the
+leading digit of both (line 46) and then mp\_clamp().
\section*{Exercises}
\begin{tabular}{cl}
@@ -3643,91 +2604,20 @@ exceed the precision requested.
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_mul\_digs.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* multiplies |a| * |b| and only computes upto digs digits of result
-018 * HAC pp. 595, Algorithm 14.12 Modified so you can control how
-019 * many digits of output are created.
-020 */
-021 int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
-022 \{
-023 mp_int t;
-024 int res, pa, pb, ix, iy;
-025 mp_digit u;
-026 mp_word r;
-027 mp_digit tmpx, *tmpt, *tmpy;
-028
-029 /* can we use the fast multiplier? */
-030 if (((digs) < MP_WARRAY) &&
-031 MIN (a->used, b->used) <
-032 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) \{
-033 return fast_s_mp_mul_digs (a, b, c, digs);
-034 \}
-035
-036 if ((res = mp_init_size (&t, digs)) != MP_OKAY) \{
-037 return res;
-038 \}
-039 t.used = digs;
-040
-041 /* compute the digits of the product directly */
-042 pa = a->used;
-043 for (ix = 0; ix < pa; ix++) \{
-044 /* set the carry to zero */
-045 u = 0;
-046
-047 /* limit ourselves to making digs digits of output */
-048 pb = MIN (b->used, digs - ix);
-049
-050 /* setup some aliases */
-051 /* copy of the digit from a used within the nested loop */
-052 tmpx = a->dp[ix];
-053
-054 /* an alias for the destination shifted ix places */
-055 tmpt = t.dp + ix;
-056
-057 /* an alias for the digits of b */
-058 tmpy = b->dp;
-059
-060 /* compute the columns of the output and propagate the carry */
-061 for (iy = 0; iy < pb; iy++) \{
-062 /* compute the column as a mp_word */
-063 r = ((mp_word)*tmpt) +
-064 ((mp_word)tmpx) * ((mp_word)*tmpy++) +
-065 ((mp_word) u);
-066
-067 /* the new column is the lower part of the result */
-068 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
-069
-070 /* get the carry word from the result */
-071 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
-072 \}
-073 /* set carry if it is placed below digs */
-074 if (ix + iy < digs) \{
-075 *tmpt = u;
-076 \}
-077 \}
-078
-079 mp_clamp (&t);
-080 mp_exch (&t, c);
-081
-082 mp_clear (&t);
-083 return MP_OKAY;
-084 \}
-085 #endif
-086
\end{alltt}
\end{small}
-First we determine (line 30) if the Comba method can be used first since it's faster. The conditions for
+First we determine (line 31) if the Comba method can be used first since it's faster. The conditions for
sing the Comba routine are that min$(a.used, b.used) < \delta$ and the number of digits of output is less than
\textbf{MP\_WARRAY}. This new constant is used to control the stack usage in the Comba routines. By default it is
set to $\delta$ but can be reduced when memory is at a premium.
If we cannot use the Comba method we proceed to setup the baseline routine. We allocate the the destination mp\_int
-$t$ (line 36) to the exact size of the output to avoid further re--allocations. At this point we now
+$t$ (line 37) to the exact size of the output to avoid further re--allocations. At this point we now
begin the $O(n^2)$ loop.
This implementation of multiplication has the caveat that it can be trimmed to only produce a variable number of
-digits as output. In each iteration of the outer loop the $pb$ variable is set (line 48) to the maximum
+digits as output. In each iteration of the outer loop the $pb$ variable is set (line 49) to the maximum
number of inner loop iterations.
Inside the inner loop we calculate $\hat r$ as the mp\_word product of the two mp\_digits and the addition of the
@@ -3735,7 +2625,7 @@ carry from the previous iteration. A particularly important observation is that
C compilers (GCC for instance) can recognize that a $N \times N \rightarrow 2N$ multiplication is all that
is required for the product. In x86 terms for example, this means using the MUL instruction.
-Each digit of the product is stored in turn (line 68) and the carry propagated (line 71) to the
+Each digit of the product is stored in turn (line 69) and the carry propagated (line 72) to the
next iteration.
\subsection{Faster Multiplication by the ``Comba'' Method}
@@ -3912,102 +2802,14 @@ and addition operations in the nested loop in parallel.
\hspace{-5.1mm}{\bf File}: bn\_fast\_s\_mp\_mul\_digs.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* Fast (comba) multiplier
-018 *
-019 * This is the fast column-array [comba] multiplier. It is
-020 * designed to compute the columns of the product first
-021 * then handle the carries afterwards. This has the effect
-022 * of making the nested loops that compute the columns very
-023 * simple and schedulable on super-scalar processors.
-024 *
-025 * This has been modified to produce a variable number of
-026 * digits of output so if say only a half-product is required
-027 * you don't have to compute the upper half (a feature
-028 * required for fast Barrett reduction).
-029 *
-030 * Based on Algorithm 14.12 on pp.595 of HAC.
-031 *
-032 */
-033 int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
-034 \{
-035 int olduse, res, pa, ix, iz;
-036 mp_digit W[MP_WARRAY];
-037 register mp_word _W;
-038
-039 /* grow the destination as required */
-040 if (c->alloc < digs) \{
-041 if ((res = mp_grow (c, digs)) != MP_OKAY) \{
-042 return res;
-043 \}
-044 \}
-045
-046 /* number of output digits to produce */
-047 pa = MIN(digs, a->used + b->used);
-048
-049 /* clear the carry */
-050 _W = 0;
-051 for (ix = 0; ix < pa; ix++) \{
-052 int tx, ty;
-053 int iy;
-054 mp_digit *tmpx, *tmpy;
-055
-056 /* get offsets into the two bignums */
-057 ty = MIN(b->used-1, ix);
-058 tx = ix - ty;
-059
-060 /* setup temp aliases */
-061 tmpx = a->dp + tx;
-062 tmpy = b->dp + ty;
-063
-064 /* this is the number of times the loop will iterrate, essentially
-065 while (tx++ < a->used && ty-- >= 0) \{ ... \}
-066 */
-067 iy = MIN(a->used-tx, ty+1);
-068
-069 /* execute loop */
-070 for (iz = 0; iz < iy; ++iz) \{
-071 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
-072
-073 \}
-074
-075 /* store term */
-076 W[ix] = ((mp_digit)_W) & MP_MASK;
-077
-078 /* make next carry */
-079 _W = _W >> ((mp_word)DIGIT_BIT);
-080 \}
-081
-082 /* setup dest */
-083 olduse = c->used;
-084 c->used = pa;
-085
-086 \{
-087 register mp_digit *tmpc;
-088 tmpc = c->dp;
-089 for (ix = 0; ix < pa+1; ix++) \{
-090 /* now extract the previous digit [below the carry] */
-091 *tmpc++ = W[ix];
-092 \}
-093
-094 /* clear unused digits [that existed in the old copy of c] */
-095 for (; ix < olduse; ix++) \{
-096 *tmpc++ = 0;
-097 \}
-098 \}
-099 mp_clamp (c);
-100 return MP_OKAY;
-101 \}
-102 #endif
-103
\end{alltt}
\end{small}
-As per the pseudo--code we first calculate $pa$ (line 47) as the number of digits to output. Next we begin the outer loop
-to produce the individual columns of the product. We use the two aliases $tmpx$ and $tmpy$ (lines 61, 62) to point
+As per the pseudo--code we first calculate $pa$ (line 48) as the number of digits to output. Next we begin the outer loop
+to produce the individual columns of the product. We use the two aliases $tmpx$ and $tmpy$ (lines 62, 63) to point
inside the two multiplicands quickly.
-The inner loop (lines 70 to 73) of this implementation is where the tradeoff come into play. Originally this comba
+The inner loop (lines 71 to 74) of this implementation is where the tradeoff come into play. Originally this comba
implementation was ``row--major'' which means it adds to each of the columns in each pass. After the outer loop it would then fix
the carries. This was very fast except it had an annoying drawback. You had to read a mp\_word and two mp\_digits and write
one mp\_word per iteration. On processors such as the Athlon XP and P4 this did not matter much since the cache bandwidth
@@ -4015,8 +2817,8 @@ is very high and it can keep the ALU fed with data. It did, however, matter on
slower and also often doesn't exist. This new algorithm only performs two reads per iteration under the assumption that the
compiler has aliased $\_ \hat W$ to a CPU register.
-After the inner loop we store the current accumulator in $W$ and shift $\_ \hat W$ (lines 76, 79) to forward it as
-a carry for the next pass. After the outer loop we use the final carry (line 76) as the last digit of the product.
+After the inner loop we store the current accumulator in $W$ and shift $\_ \hat W$ (lines 77, 80) to forward it as
+a carry for the next pass. After the outer loop we use the final carry (line 77) as the last digit of the product.
\subsection{Polynomial Basis Multiplication}
To break the $O(n^2)$ barrier in multiplication requires a completely different look at integer multiplication. In the following algorithms
@@ -4203,160 +3005,12 @@ The remaining steps 13 through 18 compute the Karatsuba polynomial through a var
\hspace{-5.1mm}{\bf File}: bn\_mp\_karatsuba\_mul.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* c = |a| * |b| using Karatsuba Multiplication using
-018 * three half size multiplications
-019 *
-020 * Let B represent the radix [e.g. 2**DIGIT_BIT] and
-021 * let n represent half of the number of digits in
-022 * the min(a,b)
-023 *
-024 * a = a1 * B**n + a0
-025 * b = b1 * B**n + b0
-026 *
-027 * Then, a * b =>
-028 a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
-029 *
-030 * Note that a1b1 and a0b0 are used twice and only need to be
-031 * computed once. So in total three half size (half # of
-032 * digit) multiplications are performed, a0b0, a1b1 and
-033 * (a1+b1)(a0+b0)
-034 *
-035 * Note that a multiplication of half the digits requires
-036 * 1/4th the number of single precision multiplications so in
-037 * total after one call 25% of the single precision multiplications
-038 * are saved. Note also that the call to mp_mul can end up back
-039 * in this function if the a0, a1, b0, or b1 are above the threshold.
-040 * This is known as divide-and-conquer and leads to the famous
-041 * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
-042 * the standard O(N**2) that the baseline/comba methods use.
-043 * Generally though the overhead of this method doesn't pay off
-044 * until a certain size (N ~ 80) is reached.
-045 */
-046 int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
-047 \{
-048 mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
-049 int B, err;
-050
-051 /* default the return code to an error */
-052 err = MP_MEM;
-053
-054 /* min # of digits */
-055 B = MIN (a->used, b->used);
-056
-057 /* now divide in two */
-058 B = B >> 1;
-059
-060 /* init copy all the temps */
-061 if (mp_init_size (&x0, B) != MP_OKAY)
-062 goto ERR;
-063 if (mp_init_size (&x1, a->used - B) != MP_OKAY)
-064 goto X0;
-065 if (mp_init_size (&y0, B) != MP_OKAY)
-066 goto X1;
-067 if (mp_init_size (&y1, b->used - B) != MP_OKAY)
-068 goto Y0;
-069
-070 /* init temps */
-071 if (mp_init_size (&t1, B * 2) != MP_OKAY)
-072 goto Y1;
-073 if (mp_init_size (&x0y0, B * 2) != MP_OKAY)
-074 goto T1;
-075 if (mp_init_size (&x1y1, B * 2) != MP_OKAY)
-076 goto X0Y0;
-077
-078 /* now shift the digits */
-079 x0.used = y0.used = B;
-080 x1.used = a->used - B;
-081 y1.used = b->used - B;
-082
-083 \{
-084 register int x;
-085 register mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
-086
-087 /* we copy the digits directly instead of using higher level functions
-088 * since we also need to shift the digits
-089 */
-090 tmpa = a->dp;
-091 tmpb = b->dp;
-092
-093 tmpx = x0.dp;
-094 tmpy = y0.dp;
-095 for (x = 0; x < B; x++) \{
-096 *tmpx++ = *tmpa++;
-097 *tmpy++ = *tmpb++;
-098 \}
-099
-100 tmpx = x1.dp;
-101 for (x = B; x < a->used; x++) \{
-102 *tmpx++ = *tmpa++;
-103 \}
-104
-105 tmpy = y1.dp;
-106 for (x = B; x < b->used; x++) \{
-107 *tmpy++ = *tmpb++;
-108 \}
-109 \}
-110
-111 /* only need to clamp the lower words since by definition the
-112 * upper words x1/y1 must have a known number of digits
-113 */
-114 mp_clamp (&x0);
-115 mp_clamp (&y0);
-116
-117 /* now calc the products x0y0 and x1y1 */
-118 /* after this x0 is no longer required, free temp [x0==t2]! */
-119 if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
-120 goto X1Y1; /* x0y0 = x0*y0 */
-121 if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
-122 goto X1Y1; /* x1y1 = x1*y1 */
-123
-124 /* now calc x1+x0 and y1+y0 */
-125 if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
-126 goto X1Y1; /* t1 = x1 - x0 */
-127 if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
-128 goto X1Y1; /* t2 = y1 - y0 */
-129 if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
-130 goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
-131
-132 /* add x0y0 */
-133 if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
-134 goto X1Y1; /* t2 = x0y0 + x1y1 */
-135 if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
-136 goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
-137
-138 /* shift by B */
-139 if (mp_lshd (&t1, B) != MP_OKAY)
-140 goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
-141 if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
-142 goto X1Y1; /* x1y1 = x1y1 << 2*B */
-143
-144 if (mp_add (&x0y0, &t1, &t1) != MP_OKAY)
-145 goto X1Y1; /* t1 = x0y0 + t1 */
-146 if (mp_add (&t1, &x1y1, c) != MP_OKAY)
-147 goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
-148
-149 /* Algorithm succeeded set the return code to MP_OKAY */
-150 err = MP_OKAY;
-151
-152 X1Y1:mp_clear (&x1y1);
-153 X0Y0:mp_clear (&x0y0);
-154 T1:mp_clear (&t1);
-155 Y1:mp_clear (&y1);
-156 Y0:mp_clear (&y0);
-157 X1:mp_clear (&x1);
-158 X0:mp_clear (&x0);
-159 ERR:
-160 return err;
-161 \}
-162 #endif
-163
\end{alltt}
\end{small}
The new coding element in this routine, not seen in previous routines, is the usage of goto statements. The conventional
wisdom is that goto statements should be avoided. This is generally true, however when every single function call can fail, it makes sense
-to handle error recovery with a single piece of code. Lines 61 to 75 handle initializing all of the temporary variables
+to handle error recovery with a single piece of code. Lines 62 to 76 handle initializing all of the temporary variables
required. Note how each of the if statements goes to a different label in case of failure. This allows the routine to correctly free only
the temporaries that have been successfully allocated so far.
@@ -4366,13 +3020,13 @@ number of digits for the next section of code.
The first algebraic portion of the algorithm is to split the two inputs into their halves. However, instead of using mp\_mod\_2d and mp\_rshd
to extract the halves, the respective code has been placed inline within the body of the function. To initialize the halves, the \textbf{used} and
-\textbf{sign} members are copied first. The first for loop on line 101 copies the lower halves. Since they are both the same magnitude it
-is simpler to calculate both lower halves in a single loop. The for loop on lines 106 and 106 calculate the upper halves $x1$ and
+\textbf{sign} members are copied first. The first for loop on line 96 copies the lower halves. Since they are both the same magnitude it
+is simpler to calculate both lower halves in a single loop. The for loop on lines 102 and 107 calculate the upper halves $x1$ and
$y1$ respectively.
By inlining the calculation of the halves, the Karatsuba multiplier has a slightly lower overhead and can be used for smaller magnitude inputs.
-When line 150 is reached, the algorithm has completed succesfully. The ``error status'' variable $err$ is set to \textbf{MP\_OKAY} so that
+When line 151 is reached, the algorithm has completed succesfully. The ``error status'' variable $err$ is set to \textbf{MP\_OKAY} so that
the same code that handles errors can be used to clear the temporary variables and return.
\subsection{Toom-Cook $3$-Way Multiplication}
@@ -4490,271 +3144,6 @@ result $a \cdot b$ is produced.
\hspace{-5.1mm}{\bf File}: bn\_mp\_toom\_mul.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* multiplication using the Toom-Cook 3-way algorithm
-018 *
-019 * Much more complicated than Karatsuba but has a lower
-020 * asymptotic running time of O(N**1.464). This algorithm is
-021 * only particularly useful on VERY large inputs
-022 * (we're talking 1000s of digits here...).
-023 */
-024 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
-025 \{
-026 mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
-027 int res, B;
-028
-029 /* init temps */
-030 if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
-031 &a0, &a1, &a2, &b0, &b1,
-032 &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) \{
-033 return res;
-034 \}
-035
-036 /* B */
-037 B = MIN(a->used, b->used) / 3;
-038
-039 /* a = a2 * B**2 + a1 * B + a0 */
-040 if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) \{
-041 goto ERR;
-042 \}
-043
-044 if ((res = mp_copy(a, &a1)) != MP_OKAY) \{
-045 goto ERR;
-046 \}
-047 mp_rshd(&a1, B);
-048 mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
-049
-050 if ((res = mp_copy(a, &a2)) != MP_OKAY) \{
-051 goto ERR;
-052 \}
-053 mp_rshd(&a2, B*2);
-054
-055 /* b = b2 * B**2 + b1 * B + b0 */
-056 if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) \{
-057 goto ERR;
-058 \}
-059
-060 if ((res = mp_copy(b, &b1)) != MP_OKAY) \{
-061 goto ERR;
-062 \}
-063 mp_rshd(&b1, B);
-064 mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
-065
-066 if ((res = mp_copy(b, &b2)) != MP_OKAY) \{
-067 goto ERR;
-068 \}
-069 mp_rshd(&b2, B*2);
-070
-071 /* w0 = a0*b0 */
-072 if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) \{
-073 goto ERR;
-074 \}
-075
-076 /* w4 = a2 * b2 */
-077 if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) \{
-078 goto ERR;
-079 \}
-080
-081 /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
-082 if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) \{
-083 goto ERR;
-084 \}
-085 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) \{
-086 goto ERR;
-087 \}
-088 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) \{
-089 goto ERR;
-090 \}
-091 if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) \{
-092 goto ERR;
-093 \}
-094
-095 if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) \{
-096 goto ERR;
-097 \}
-098 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) \{
-099 goto ERR;
-100 \}
-101 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) \{
-102 goto ERR;
-103 \}
-104 if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) \{
-105 goto ERR;
-106 \}
-107
-108 if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) \{
-109 goto ERR;
-110 \}
-111
-112 /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
-113 if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) \{
-114 goto ERR;
-115 \}
-116 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) \{
-117 goto ERR;
-118 \}
-119 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) \{
-120 goto ERR;
-121 \}
-122 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) \{
-123 goto ERR;
-124 \}
-125
-126 if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) \{
-127 goto ERR;
-128 \}
-129 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) \{
-130 goto ERR;
-131 \}
-132 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) \{
-133 goto ERR;
-134 \}
-135 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) \{
-136 goto ERR;
-137 \}
-138
-139 if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) \{
-140 goto ERR;
-141 \}
-142
-143
-144 /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
-145 if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) \{
-146 goto ERR;
-147 \}
-148 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) \{
-149 goto ERR;
-150 \}
-151 if ((res = mp_add(&b2, &b1, &tmp2)) != MP_OKAY) \{
-152 goto ERR;
-153 \}
-154 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) \{
-155 goto ERR;
-156 \}
-157 if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) \{
-158 goto ERR;
-159 \}
-160
-161 /* now solve the matrix
-162
-163 0 0 0 0 1
-164 1 2 4 8 16
-165 1 1 1 1 1
-166 16 8 4 2 1
-167 1 0 0 0 0
-168
-169 using 12 subtractions, 4 shifts,
-170 2 small divisions and 1 small multiplication
-171 */
-172
-173 /* r1 - r4 */
-174 if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) \{
-175 goto ERR;
-176 \}
-177 /* r3 - r0 */
-178 if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) \{
-179 goto ERR;
-180 \}
-181 /* r1/2 */
-182 if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) \{
-183 goto ERR;
-184 \}
-185 /* r3/2 */
-186 if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) \{
-187 goto ERR;
-188 \}
-189 /* r2 - r0 - r4 */
-190 if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) \{
-191 goto ERR;
-192 \}
-193 if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) \{
-194 goto ERR;
-195 \}
-196 /* r1 - r2 */
-197 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) \{
-198 goto ERR;
-199 \}
-200 /* r3 - r2 */
-201 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) \{
-202 goto ERR;
-203 \}
-204 /* r1 - 8r0 */
-205 if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) \{
-206 goto ERR;
-207 \}
-208 if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) \{
-209 goto ERR;
-210 \}
-211 /* r3 - 8r4 */
-212 if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) \{
-213 goto ERR;
-214 \}
-215 if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) \{
-216 goto ERR;
-217 \}
-218 /* 3r2 - r1 - r3 */
-219 if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) \{
-220 goto ERR;
-221 \}
-222 if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) \{
-223 goto ERR;
-224 \}
-225 if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) \{
-226 goto ERR;
-227 \}
-228 /* r1 - r2 */
-229 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) \{
-230 goto ERR;
-231 \}
-232 /* r3 - r2 */
-233 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) \{
-234 goto ERR;
-235 \}
-236 /* r1/3 */
-237 if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) \{
-238 goto ERR;
-239 \}
-240 /* r3/3 */
-241 if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) \{
-242 goto ERR;
-243 \}
-244
-245 /* at this point shift W[n] by B*n */
-246 if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) \{
-247 goto ERR;
-248 \}
-249 if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) \{
-250 goto ERR;
-251 \}
-252 if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) \{
-253 goto ERR;
-254 \}
-255 if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) \{
-256 goto ERR;
-257 \}
-258
-259 if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) \{
-260 goto ERR;
-261 \}
-262 if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) \{
-263 goto ERR;
-264 \}
-265 if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) \{
-266 goto ERR;
-267 \}
-268 if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) \{
-269 goto ERR;
-270 \}
-271
-272 ERR:
-273 mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
-274 &a0, &a1, &a2, &b0, &b1,
-275 &b2, &tmp1, &tmp2, NULL);
-276 return res;
-277 \}
-278
-279 #endif
-280
\end{alltt}
\end{small}
@@ -4763,12 +3152,12 @@ large numbers. For example, a 10,000 digit multiplication takes approximaly 99,
Toom--Cook than a Comba or baseline approach (this is a savings of more than 99$\%$). For most ``crypto'' sized numbers this
algorithm is not practical as Karatsuba has a much lower cutoff point.
-First we split $a$ and $b$ into three roughly equal portions. This has been accomplished (lines 40 to 69) with
+First we split $a$ and $b$ into three roughly equal portions. This has been accomplished (lines 41 to 70) with
combinations of mp\_rshd() and mp\_mod\_2d() function calls. At this point $a = a2 \cdot \beta^2 + a1 \cdot \beta + a0$ and similiarly
for $b$.
Next we compute the five points $w0, w1, w2, w3$ and $w4$. Recall that $w0$ and $w4$ can be computed directly from the portions so
-we get those out of the way first (lines 72 and 77). Next we compute $w1, w2$ and $w3$ using Horners method.
+we get those out of the way first (lines 73 and 78). Next we compute $w1, w2$ and $w3$ using Horners method.
After this point we solve for the actual values of $w1, w2$ and $w3$ by reducing the $5 \times 5$ system which is relatively
straight forward.
@@ -4817,58 +3206,11 @@ s\_mp\_mul\_digs will clear it.
\hspace{-5.1mm}{\bf File}: bn\_mp\_mul.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* high level multiplication (handles sign) */
-018 int mp_mul (mp_int * a, mp_int * b, mp_int * c)
-019 \{
-020 int res, neg;
-021 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
-022
-023 /* use Toom-Cook? */
-024 #ifdef BN_MP_TOOM_MUL_C
-025 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) \{
-026 res = mp_toom_mul(a, b, c);
-027 \} else
-028 #endif
-029 #ifdef BN_MP_KARATSUBA_MUL_C
-030 /* use Karatsuba? */
-031 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) \{
-032 res = mp_karatsuba_mul (a, b, c);
-033 \} else
-034 #endif
-035 \{
-036 /* can we use the fast multiplier?
-037 *
-038 * The fast multiplier can be used if the output will
-039 * have less than MP_WARRAY digits and the number of
-040 * digits won't affect carry propagation
-041 */
-042 int digs = a->used + b->used + 1;
-043
-044 #ifdef BN_FAST_S_MP_MUL_DIGS_C
-045 if ((digs < MP_WARRAY) &&
-046 MIN(a->used, b->used) <=
-047 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) \{
-048 res = fast_s_mp_mul_digs (a, b, c, digs);
-049 \} else
-050 #endif
-051 #ifdef BN_S_MP_MUL_DIGS_C
-052 res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
-053 #else
-054 res = MP_VAL;
-055 #endif
-056
-057 \}
-058 c->sign = (c->used > 0) ? neg : MP_ZPOS;
-059 return res;
-060 \}
-061 #endif
-062
\end{alltt}
\end{small}
-The implementation is rather simplistic and is not particularly noteworthy. Line 23 computes the sign of the result using the ``?''
-operator from the C programming language. Line 47 computes $\delta$ using the fact that $1 << k$ is equal to $2^k$.
+The implementation is rather simplistic and is not particularly noteworthy. Line 22 computes the sign of the result using the ``?''
+operator from the C programming language. Line 48 computes $\delta$ using the fact that $1 << k$ is equal to $2^k$.
\section{Squaring}
\label{sec:basesquare}
@@ -4969,78 +3311,13 @@ results calculated so far. This involves expensive carry propagation which will
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_sqr.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
-018 int s_mp_sqr (mp_int * a, mp_int * b)
-019 \{
-020 mp_int t;
-021 int res, ix, iy, pa;
-022 mp_word r;
-023 mp_digit u, tmpx, *tmpt;
-024
-025 pa = a->used;
-026 if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) \{
-027 return res;
-028 \}
-029
-030 /* default used is maximum possible size */
-031 t.used = 2*pa + 1;
-032
-033 for (ix = 0; ix < pa; ix++) \{
-034 /* first calculate the digit at 2*ix */
-035 /* calculate double precision result */
-036 r = ((mp_word) t.dp[2*ix]) +
-037 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
-038
-039 /* store lower part in result */
-040 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
-041
-042 /* get the carry */
-043 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
-044
-045 /* left hand side of A[ix] * A[iy] */
-046 tmpx = a->dp[ix];
-047
-048 /* alias for where to store the results */
-049 tmpt = t.dp + (2*ix + 1);
-050
-051 for (iy = ix + 1; iy < pa; iy++) \{
-052 /* first calculate the product */
-053 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
-054
-055 /* now calculate the double precision result, note we use
-056 * addition instead of *2 since it's easier to optimize
-057 */
-058 r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
-059
-060 /* store lower part */
-061 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
-062
-063 /* get carry */
-064 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
-065 \}
-066 /* propagate upwards */
-067 while (u != ((mp_digit) 0)) \{
-068 r = ((mp_word) *tmpt) + ((mp_word) u);
-069 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
-070 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
-071 \}
-072 \}
-073
-074 mp_clamp (&t);
-075 mp_exch (&t, b);
-076 mp_clear (&t);
-077 return MP_OKAY;
-078 \}
-079 #endif
-080
\end{alltt}
\end{small}
-Inside the outer loop (line 33) the square term is calculated on line 36. The carry (line 43) has been
+Inside the outer loop (line 34) the square term is calculated on line 37. The carry (line 44) has been
extracted from the mp\_word accumulator using a right shift. Aliases for $a_{ix}$ and $t_{ix+iy}$ are initialized
-(lines 46 and 49) to simplify the inner loop. The doubling is performed using two
-additions (line 58) since it is usually faster than shifting, if not at least as fast.
+(lines 47 and 50) to simplify the inner loop. The doubling is performed using two
+additions (line 59) since it is usually faster than shifting, if not at least as fast.
The important observation is that the inner loop does not begin at $iy = 0$ like for multiplication. As such the inner loops
get progressively shorter as the algorithm proceeds. This is what leads to the savings compared to using a multiplication to
@@ -5122,101 +3399,6 @@ only to even outputs and it is the square of the term at the $\lfloor ix / 2 \rf
\hspace{-5.1mm}{\bf File}: bn\_fast\_s\_mp\_sqr.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* the jist of squaring...
-018 * you do like mult except the offset of the tmpx [one that
-019 * starts closer to zero] can't equal the offset of tmpy.
-020 * So basically you set up iy like before then you min it with
-021 * (ty-tx) so that it never happens. You double all those
-022 * you add in the inner loop
-023
-024 After that loop you do the squares and add them in.
-025 */
-026
-027 int fast_s_mp_sqr (mp_int * a, mp_int * b)
-028 \{
-029 int olduse, res, pa, ix, iz;
-030 mp_digit W[MP_WARRAY], *tmpx;
-031 mp_word W1;
-032
-033 /* grow the destination as required */
-034 pa = a->used + a->used;
-035 if (b->alloc < pa) \{
-036 if ((res = mp_grow (b, pa)) != MP_OKAY) \{
-037 return res;
-038 \}
-039 \}
-040
-041 /* number of output digits to produce */
-042 W1 = 0;
-043 for (ix = 0; ix < pa; ix++) \{
-044 int tx, ty, iy;
-045 mp_word _W;
-046 mp_digit *tmpy;
-047
-048 /* clear counter */
-049 _W = 0;
-050
-051 /* get offsets into the two bignums */
-052 ty = MIN(a->used-1, ix);
-053 tx = ix - ty;
-054
-055 /* setup temp aliases */
-056 tmpx = a->dp + tx;
-057 tmpy = a->dp + ty;
-058
-059 /* this is the number of times the loop will iterrate, essentially
-060 while (tx++ < a->used && ty-- >= 0) \{ ... \}
-061 */
-062 iy = MIN(a->used-tx, ty+1);
-063
-064 /* now for squaring tx can never equal ty
-065 * we halve the distance since they approach at a rate of 2x
-066 * and we have to round because odd cases need to be executed
-067 */
-068 iy = MIN(iy, (ty-tx+1)>>1);
-069
-070 /* execute loop */
-071 for (iz = 0; iz < iy; iz++) \{
-072 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
-073 \}
-074
-075 /* double the inner product and add carry */
-076 _W = _W + _W + W1;
-077
-078 /* even columns have the square term in them */
-079 if ((ix&1) == 0) \{
-080 _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
-081 \}
-082
-083 /* store it */
-084 W[ix] = (mp_digit)(_W & MP_MASK);
-085
-086 /* make next carry */
-087 W1 = _W >> ((mp_word)DIGIT_BIT);
-088 \}
-089
-090 /* setup dest */
-091 olduse = b->used;
-092 b->used = a->used+a->used;
-093
-094 \{
-095 mp_digit *tmpb;
-096 tmpb = b->dp;
-097 for (ix = 0; ix < pa; ix++) \{
-098 *tmpb++ = W[ix] & MP_MASK;
-099 \}
-100
-101 /* clear unused digits [that existed in the old copy of c] */
-102 for (; ix < olduse; ix++) \{
-103 *tmpb++ = 0;
-104 \}
-105 \}
-106 mp_clamp (b);
-107 return MP_OKAY;
-108 \}
-109 #endif
-110
\end{alltt}
\end{small}
@@ -5326,113 +3508,11 @@ ratio of 1:7. } than simpler operations such as addition.
\hspace{-5.1mm}{\bf File}: bn\_mp\_karatsuba\_sqr.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* Karatsuba squaring, computes b = a*a using three
-018 * half size squarings
-019 *
-020 * See comments of karatsuba_mul for details. It
-021 * is essentially the same algorithm but merely
-022 * tuned to perform recursive squarings.
-023 */
-024 int mp_karatsuba_sqr (mp_int * a, mp_int * b)
-025 \{
-026 mp_int x0, x1, t1, t2, x0x0, x1x1;
-027 int B, err;
-028
-029 err = MP_MEM;
-030
-031 /* min # of digits */
-032 B = a->used;
-033
-034 /* now divide in two */
-035 B = B >> 1;
-036
-037 /* init copy all the temps */
-038 if (mp_init_size (&x0, B) != MP_OKAY)
-039 goto ERR;
-040 if (mp_init_size (&x1, a->used - B) != MP_OKAY)
-041 goto X0;
-042
-043 /* init temps */
-044 if (mp_init_size (&t1, a->used * 2) != MP_OKAY)
-045 goto X1;
-046 if (mp_init_size (&t2, a->used * 2) != MP_OKAY)
-047 goto T1;
-048 if (mp_init_size (&x0x0, B * 2) != MP_OKAY)
-049 goto T2;
-050 if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY)
-051 goto X0X0;
-052
-053 \{
-054 register int x;
-055 register mp_digit *dst, *src;
-056
-057 src = a->dp;
-058
-059 /* now shift the digits */
-060 dst = x0.dp;
-061 for (x = 0; x < B; x++) \{
-062 *dst++ = *src++;
-063 \}
-064
-065 dst = x1.dp;
-066 for (x = B; x < a->used; x++) \{
-067 *dst++ = *src++;
-068 \}
-069 \}
-070
-071 x0.used = B;
-072 x1.used = a->used - B;
-073
-074 mp_clamp (&x0);
-075
-076 /* now calc the products x0*x0 and x1*x1 */
-077 if (mp_sqr (&x0, &x0x0) != MP_OKAY)
-078 goto X1X1; /* x0x0 = x0*x0 */
-079 if (mp_sqr (&x1, &x1x1) != MP_OKAY)
-080 goto X1X1; /* x1x1 = x1*x1 */
-081
-082 /* now calc (x1+x0)**2 */
-083 if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
-084 goto X1X1; /* t1 = x1 - x0 */
-085 if (mp_sqr (&t1, &t1) != MP_OKAY)
-086 goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
-087
-088 /* add x0y0 */
-089 if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
-090 goto X1X1; /* t2 = x0x0 + x1x1 */
-091 if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
-092 goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
-093
-094 /* shift by B */
-095 if (mp_lshd (&t1, B) != MP_OKAY)
-096 goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
-097 if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
-098 goto X1X1; /* x1x1 = x1x1 << 2*B */
-099
-100 if (mp_add (&x0x0, &t1, &t1) != MP_OKAY)
-101 goto X1X1; /* t1 = x0x0 + t1 */
-102 if (mp_add (&t1, &x1x1, b) != MP_OKAY)
-103 goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
-104
-105 err = MP_OKAY;
-106
-107 X1X1:mp_clear (&x1x1);
-108 X0X0:mp_clear (&x0x0);
-109 T2:mp_clear (&t2);
-110 T1:mp_clear (&t1);
-111 X1:mp_clear (&x1);
-112 X0:mp_clear (&x0);
-113 ERR:
-114 return err;
-115 \}
-116 #endif
-117
\end{alltt}
\end{small}
This implementation is largely based on the implementation of algorithm mp\_karatsuba\_mul. It uses the same inline style to copy and
-shift the input into the two halves. The loop from line 53 to line 69 has been modified since only one input exists. The \textbf{used}
+shift the input into the two halves. The loop from line 54 to line 70 has been modified since only one input exists. The \textbf{used}
count of both $x0$ and $x1$ is fixed up and $x0$ is clamped before the calculations begin. At this point $x1$ and $x0$ are valid equivalents
to the respective halves as if mp\_rshd and mp\_mod\_2d had been used.
@@ -5486,45 +3566,6 @@ neither of the polynomial basis algorithms should be used then either the Comba
\hspace{-5.1mm}{\bf File}: bn\_mp\_sqr.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* computes b = a*a */
-018 int
-019 mp_sqr (mp_int * a, mp_int * b)
-020 \{
-021 int res;
-022
-023 #ifdef BN_MP_TOOM_SQR_C
-024 /* use Toom-Cook? */
-025 if (a->used >= TOOM_SQR_CUTOFF) \{
-026 res = mp_toom_sqr(a, b);
-027 /* Karatsuba? */
-028 \} else
-029 #endif
-030 #ifdef BN_MP_KARATSUBA_SQR_C
-031 if (a->used >= KARATSUBA_SQR_CUTOFF) \{
-032 res = mp_karatsuba_sqr (a, b);
-033 \} else
-034 #endif
-035 \{
-036 #ifdef BN_FAST_S_MP_SQR_C
-037 /* can we use the fast comba multiplier? */
-038 if ((a->used * 2 + 1) < MP_WARRAY &&
-039 a->used <
-040 (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) \{
-041 res = fast_s_mp_sqr (a, b);
-042 \} else
-043 #endif
-044 #ifdef BN_S_MP_SQR_C
-045 res = s_mp_sqr (a, b);
-046 #else
-047 res = MP_VAL;
-048 #endif
-049 \}
-050 b->sign = MP_ZPOS;
-051 return res;
-052 \}
-053 #endif
-054
\end{alltt}
\end{small}
@@ -5778,93 +3819,12 @@ performed at most twice, and on average once. However, if $a \ge b^2$ than it wi
\hspace{-5.1mm}{\bf File}: bn\_mp\_reduce.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* reduces x mod m, assumes 0 < x < m**2, mu is
-018 * precomputed via mp_reduce_setup.
-019 * From HAC pp.604 Algorithm 14.42
-020 */
-021 int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
-022 \{
-023 mp_int q;
-024 int res, um = m->used;
-025
-026 /* q = x */
-027 if ((res = mp_init_copy (&q, x)) != MP_OKAY) \{
-028 return res;
-029 \}
-030
-031 /* q1 = x / b**(k-1) */
-032 mp_rshd (&q, um - 1);
-033
-034 /* according to HAC this optimization is ok */
-035 if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) \{
-036 if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) \{
-037 goto CLEANUP;
-038 \}
-039 \} else \{
-040 #ifdef BN_S_MP_MUL_HIGH_DIGS_C
-041 if ((res = s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) \{
-042 goto CLEANUP;
-043 \}
-044 #elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
-045 if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) \{
-046 goto CLEANUP;
-047 \}
-048 #else
-049 \{
-050 res = MP_VAL;
-051 goto CLEANUP;
-052 \}
-053 #endif
-054 \}
-055
-056 /* q3 = q2 / b**(k+1) */
-057 mp_rshd (&q, um + 1);
-058
-059 /* x = x mod b**(k+1), quick (no division) */
-060 if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) \{
-061 goto CLEANUP;
-062 \}
-063
-064 /* q = q * m mod b**(k+1), quick (no division) */
-065 if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) \{
-066 goto CLEANUP;
-067 \}
-068
-069 /* x = x - q */
-070 if ((res = mp_sub (x, &q, x)) != MP_OKAY) \{
-071 goto CLEANUP;
-072 \}
-073
-074 /* If x < 0, add b**(k+1) to it */
-075 if (mp_cmp_d (x, 0) == MP_LT) \{
-076 mp_set (&q, 1);
-077 if ((res = mp_lshd (&q, um + 1)) != MP_OKAY)
-078 goto CLEANUP;
-079 if ((res = mp_add (x, &q, x)) != MP_OKAY)
-080 goto CLEANUP;
-081 \}
-082
-083 /* Back off if it's too big */
-084 while (mp_cmp (x, m) != MP_LT) \{
-085 if ((res = s_mp_sub (x, m, x)) != MP_OKAY) \{
-086 goto CLEANUP;
-087 \}
-088 \}
-089
-090 CLEANUP:
-091 mp_clear (&q);
-092
-093 return res;
-094 \}
-095 #endif
-096
\end{alltt}
\end{small}
The first multiplication that determines the quotient can be performed by only producing the digits from $m - 1$ and up. This essentially halves
the number of single precision multiplications required. However, the optimization is only safe if $\beta$ is much larger than the number of digits
-in the modulus. In the source code this is evaluated on lines 36 to 43 where algorithm s\_mp\_mul\_high\_digs is used when it is
+in the modulus. In the source code this is evaluated on lines 36 to 44 where algorithm s\_mp\_mul\_high\_digs is used when it is
safe to do so.
\subsection{The Barrett Setup Algorithm}
@@ -5897,21 +3857,6 @@ is equivalent and much faster. The final value is computed by taking the intege
\hspace{-5.1mm}{\bf File}: bn\_mp\_reduce\_setup.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* pre-calculate the value required for Barrett reduction
-018 * For a given modulus "b" it calulates the value required in "a"
-019 */
-020 int mp_reduce_setup (mp_int * a, mp_int * b)
-021 \{
-022 int res;
-023
-024 if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) \{
-025 return res;
-026 \}
-027 return mp_div (a, b, a, NULL);
-028 \}
-029 #endif
-030
\end{alltt}
\end{small}
@@ -5976,6 +3921,7 @@ $0 \le r < \lfloor x/2^k \rfloor + n$. As a result at most a single subtraction
\hline $6$ & $x/2 = 139$ \\
\hline $7$ & $x + n = 396$, $x/2 = 198$ \\
\hline $8$ & $x/2 = 99$ \\
+\hline $9$ & $x + n = 356$, $x/2 = 178$ \\
\hline
\end{tabular}
\end{center}
@@ -5984,8 +3930,8 @@ $0 \le r < \lfloor x/2^k \rfloor + n$. As a result at most a single subtraction
\label{fig:MONT1}
\end{figure}
-Consider the example in figure~\ref{fig:MONT1} which reduces $x = 5555$ modulo $n = 257$ when $k = 8$. The result of the algorithm $r = 99$ is
-congruent to the value of $2^{-8} \cdot 5555 \mbox{ (mod }257\mbox{)}$. When $r$ is multiplied by $2^8$ modulo $257$ the correct residue
+Consider the example in figure~\ref{fig:MONT1} which reduces $x = 5555$ modulo $n = 257$ when $k = 9$ (note $\beta^k = 512$ which is larger than $n$). The result of
+the algorithm $r = 178$ is congruent to the value of $2^{-9} \cdot 5555 \mbox{ (mod }257\mbox{)}$. When $r$ is multiplied by $2^9$ modulo $257$ the correct residue
$r \equiv 158$ is produced.
Let $k = \lfloor lg(n) \rfloor + 1$ represent the number of bits in $n$. The current algorithm requires $2k^2$ single precision shifts
@@ -5997,10 +3943,10 @@ Fortunately there exists an alternative representation of the algorithm.
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{Montgomery Reduction} (modified I). \\
-\textbf{Input}. Integer $x$, $n$ and $k$ \\
+\textbf{Input}. Integer $x$, $n$ and $k$ ($2^k > n$) \\
\textbf{Output}. $2^{-k}x \mbox{ (mod }n\mbox{)}$ \\
\hline \\
-1. for $t$ from $0$ to $k - 1$ do \\
+1. for $t$ from $1$ to $k$ do \\
\hspace{3mm}1.1 If the $t$'th bit of $x$ is one then \\
\hspace{6mm}1.1.1 $x \leftarrow x + 2^tn$ \\
2. Return $x/2^k$. \\
@@ -6028,7 +3974,8 @@ precision shifts has now been reduced from $2k^2$ to $k^2 + k$ which is only a s
\hline $6$ & $8896$ & $10001011000000$ \\
\hline $7$ & $x + 2^{6}n = 25344$ & $110001100000000$ \\
\hline $8$ & $25344$ & $110001100000000$ \\
-\hline -- & $x/2^k = 99$ & \\
+\hline $9$ & $x + 2^{7}n = 91136$ & $10110010000000000$ \\
+\hline -- & $x/2^k = 178$ & \\
\hline
\end{tabular}
\end{center}
@@ -6037,7 +3984,7 @@ precision shifts has now been reduced from $2k^2$ to $k^2 + k$ which is only a s
\label{fig:MONT2}
\end{figure}
-Figure~\ref{fig:MONT2} demonstrates the modified algorithm reducing $x = 5555$ modulo $n = 257$ with $k = 8$.
+Figure~\ref{fig:MONT2} demonstrates the modified algorithm reducing $x = 5555$ modulo $n = 257$ with $k = 9$.
With this algorithm a single shift right at the end is the only right shift required to reduce the input instead of $k$ right shifts inside the
loop. Note that for the iterations $t = 2, 5, 6$ and $8$ where the result $x$ is not changed. In those iterations the $t$'th bit of $x$ is
zero and the appropriate multiple of $n$ does not need to be added to force the $t$'th bit of the result to zero.
@@ -6051,7 +3998,7 @@ previous algorithm re-written to compute the Montgomery reduction in this new fa
\begin{center}
\begin{tabular}{l}
\hline Algorithm \textbf{Montgomery Reduction} (modified II). \\
-\textbf{Input}. Integer $x$, $n$ and $k$ \\
+\textbf{Input}. Integer $x$, $n$ and $k$ ($\beta^k > n$) \\
\textbf{Output}. $\beta^{-k}x \mbox{ (mod }n\mbox{)}$ \\
\hline \\
1. for $t$ from $0$ to $k - 1$ do \\
@@ -6169,110 +4116,11 @@ multiplications.
\hspace{-5.1mm}{\bf File}: bn\_mp\_montgomery\_reduce.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* computes xR**-1 == x (mod N) via Montgomery Reduction */
-018 int
-019 mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
-020 \{
-021 int ix, res, digs;
-022 mp_digit mu;
-023
-024 /* can the fast reduction [comba] method be used?
-025 *
-026 * Note that unlike in mul you're safely allowed *less*
-027 * than the available columns [255 per default] since carries
-028 * are fixed up in the inner loop.
-029 */
-030 digs = n->used * 2 + 1;
-031 if ((digs < MP_WARRAY) &&
-032 n->used <
-033 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) \{
-034 return fast_mp_montgomery_reduce (x, n, rho);
-035 \}
-036
-037 /* grow the input as required */
-038 if (x->alloc < digs) \{
-039 if ((res = mp_grow (x, digs)) != MP_OKAY) \{
-040 return res;
-041 \}
-042 \}
-043 x->used = digs;
-044
-045 for (ix = 0; ix < n->used; ix++) \{
-046 /* mu = ai * rho mod b
-047 *
-048 * The value of rho must be precalculated via
-049 * montgomery_setup() such that
-050 * it equals -1/n0 mod b this allows the
-051 * following inner loop to reduce the
-052 * input one digit at a time
-053 */
-054 mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK);
-055
-056 /* a = a + mu * m * b**i */
-057 \{
-058 register int iy;
-059 register mp_digit *tmpn, *tmpx, u;
-060 register mp_word r;
-061
-062 /* alias for digits of the modulus */
-063 tmpn = n->dp;
-064
-065 /* alias for the digits of x [the input] */
-066 tmpx = x->dp + ix;
-067
-068 /* set the carry to zero */
-069 u = 0;
-070
-071 /* Multiply and add in place */
-072 for (iy = 0; iy < n->used; iy++) \{
-073 /* compute product and sum */
-074 r = ((mp_word)mu) * ((mp_word)*tmpn++) +
-075 ((mp_word) u) + ((mp_word) * tmpx);
-076
-077 /* get carry */
-078 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
-079
-080 /* fix digit */
-081 *tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
-082 \}
-083 /* At this point the ix'th digit of x should be zero */
-084
-085
-086 /* propagate carries upwards as required*/
-087 while (u) \{
-088 *tmpx += u;
-089 u = *tmpx >> DIGIT_BIT;
-090 *tmpx++ &= MP_MASK;
-091 \}
-092 \}
-093 \}
-094
-095 /* at this point the n.used'th least
-096 * significant digits of x are all zero
-097 * which means we can shift x to the
-098 * right by n.used digits and the
-099 * residue is unchanged.
-100 */
-101
-102 /* x = x/b**n.used */
-103 mp_clamp(x);
-104 mp_rshd (x, n->used);
-105
-106 /* if x >= n then x = x - n */
-107 if (mp_cmp_mag (x, n) != MP_LT) \{
-108 return s_mp_sub (x, n, x);
-109 \}
-110
-111 return MP_OKAY;
-112 \}
-113 #endif
-114
\end{alltt}
\end{small}
-This is the baseline implementation of the Montgomery reduction algorithm. Lines 30 to 35 determine if the Comba based
-routine can be used instead. Line 48 computes the value of $\mu$ for that particular iteration of the outer loop.
+This is the baseline implementation of the Montgomery reduction algorithm. Lines 31 to 36 determine if the Comba based
+routine can be used instead. Line 47 computes the value of $\mu$ for that particular iteration of the outer loop.
The multiplication $\mu n \beta^{ix}$ is performed in one step in the inner loop. The alias $tmpx$ refers to the $ix$'th digit of $x$ and
the alias $tmpn$ refers to the modulus $n$.
@@ -6360,170 +4208,17 @@ stored in the destination $x$.
\hspace{-5.1mm}{\bf File}: bn\_fast\_mp\_montgomery\_reduce.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* computes xR**-1 == x (mod N) via Montgomery Reduction
-018 *
-019 * This is an optimized implementation of montgomery_reduce
-020 * which uses the comba method to quickly calculate the columns of the
-021 * reduction.
-022 *
-023 * Based on Algorithm 14.32 on pp.601 of HAC.
-024 */
-025 int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
-026 \{
-027 int ix, res, olduse;
-028 mp_word W[MP_WARRAY];
-029
-030 /* get old used count */
-031 olduse = x->used;
-032
-033 /* grow a as required */
-034 if (x->alloc < n->used + 1) \{
-035 if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) \{
-036 return res;
-037 \}
-038 \}
-039
-040 /* first we have to get the digits of the input into
-041 * an array of double precision words W[...]
-042 */
-043 \{
-044 register mp_word *_W;
-045 register mp_digit *tmpx;
-046
-047 /* alias for the W[] array */
-048 _W = W;
-049
-050 /* alias for the digits of x*/
-051 tmpx = x->dp;
-052
-053 /* copy the digits of a into W[0..a->used-1] */
-054 for (ix = 0; ix < x->used; ix++) \{
-055 *_W++ = *tmpx++;
-056 \}
-057
-058 /* zero the high words of W[a->used..m->used*2] */
-059 for (; ix < n->used * 2 + 1; ix++) \{
-060 *_W++ = 0;
-061 \}
-062 \}
-063
-064 /* now we proceed to zero successive digits
-065 * from the least significant upwards
-066 */
-067 for (ix = 0; ix < n->used; ix++) \{
-068 /* mu = ai * m' mod b
-069 *
-070 * We avoid a double precision multiplication (which isn't required)
-071 * by casting the value down to a mp_digit. Note this requires
-072 * that W[ix-1] have the carry cleared (see after the inner loop)
-073 */
-074 register mp_digit mu;
-075 mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
-076
-077 /* a = a + mu * m * b**i
-078 *
-079 * This is computed in place and on the fly. The multiplication
-080 * by b**i is handled by offseting which columns the results
-081 * are added to.
-082 *
-083 * Note the comba method normally doesn't handle carries in the
-084 * inner loop In this case we fix the carry from the previous
-085 * column since the Montgomery reduction requires digits of the
-086 * result (so far) [see above] to work. This is
-087 * handled by fixing up one carry after the inner loop. The
-088 * carry fixups are done in order so after these loops the
-089 * first m->used words of W[] have the carries fixed
-090 */
-091 \{
-092 register int iy;
-093 register mp_digit *tmpn;
-094 register mp_word *_W;
-095
-096 /* alias for the digits of the modulus */
-097 tmpn = n->dp;
-098
-099 /* Alias for the columns set by an offset of ix */
-100 _W = W + ix;
-101
-102 /* inner loop */
-103 for (iy = 0; iy < n->used; iy++) \{
-104 *_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
-105 \}
-106 \}
-107
-108 /* now fix carry for next digit, W[ix+1] */
-109 W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
-110 \}
-111
-112 /* now we have to propagate the carries and
-113 * shift the words downward [all those least
-114 * significant digits we zeroed].
-115 */
-116 \{
-117 register mp_digit *tmpx;
-118 register mp_word *_W, *_W1;
-119
-120 /* nox fix rest of carries */
-121
-122 /* alias for current word */
-123 _W1 = W + ix;
-124
-125 /* alias for next word, where the carry goes */
-126 _W = W + ++ix;
-127
-128 for (; ix <= n->used * 2 + 1; ix++) \{
-129 *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
-130 \}
-131
-132 /* copy out, A = A/b**n
-133 *
-134 * The result is A/b**n but instead of converting from an
-135 * array of mp_word to mp_digit than calling mp_rshd
-136 * we just copy them in the right order
-137 */
-138
-139 /* alias for destination word */
-140 tmpx = x->dp;
-141
-142 /* alias for shifted double precision result */
-143 _W = W + n->used;
-144
-145 for (ix = 0; ix < n->used + 1; ix++) \{
-146 *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
-147 \}
-148
-149 /* zero oldused digits, if the input a was larger than
-150 * m->used+1 we'll have to clear the digits
-151 */
-152 for (; ix < olduse; ix++) \{
-153 *tmpx++ = 0;
-154 \}
-155 \}
-156
-157 /* set the max used and clamp */
-158 x->used = n->used + 1;
-159 mp_clamp (x);
-160
-161 /* if A >= m then A = A - m */
-162 if (mp_cmp_mag (x, n) != MP_LT) \{
-163 return s_mp_sub (x, n, x);
-164 \}
-165 return MP_OKAY;
-166 \}
-167 #endif
-168
\end{alltt}
\end{small}
-The $\hat W$ array is first filled with digits of $x$ on line 50 then the rest of the digits are zeroed on line 54. Both loops share
+The $\hat W$ array is first filled with digits of $x$ on line 48 then the rest of the digits are zeroed on line 55. Both loops share
the same alias variables to make the code easier to read.
The value of $\mu$ is calculated in an interesting fashion. First the value $\hat W_{ix}$ is reduced modulo $\beta$ and cast to a mp\_digit. This
-forces the compiler to use a single precision multiplication and prevents any concerns about loss of precision. Line 109 fixes the carry
+forces the compiler to use a single precision multiplication and prevents any concerns about loss of precision. Line 110 fixes the carry
for the next iteration of the loop by propagating the carry from $\hat W_{ix}$ to $\hat W_{ix+1}$.
-The for loop on line 108 propagates the rest of the carries upwards through the columns. The for loop on line 125 reduces the columns
+The for loop on line 109 propagates the rest of the carries upwards through the columns. The for loop on line 126 reduces the columns
modulo $\beta$ and shifts them $k$ places at the same time. The alias $\_ \hat W$ actually refers to the array $\hat W$ starting at the $n.used$'th
digit, that is $\_ \hat W_{t} = \hat W_{n.used + t}$.
@@ -6560,46 +4255,6 @@ to calculate $1/n_0$ when $\beta$ is a power of two.
\hspace{-5.1mm}{\bf File}: bn\_mp\_montgomery\_setup.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* setups the montgomery reduction stuff */
-018 int
-019 mp_montgomery_setup (mp_int * n, mp_digit * rho)
-020 \{
-021 mp_digit x, b;
-022
-023 /* fast inversion mod 2**k
-024 *
-025 * Based on the fact that
-026 *
-027 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
-028 * => 2*X*A - X*X*A*A = 1
-029 * => 2*(1) - (1) = 1
-030 */
-031 b = n->dp[0];
-032
-033 if ((b & 1) == 0) \{
-034 return MP_VAL;
-035 \}
-036
-037 x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
-038 x *= 2 - b * x; /* here x*a==1 mod 2**8 */
-039 #if !defined(MP_8BIT)
-040 x *= 2 - b * x; /* here x*a==1 mod 2**16 */
-041 #endif
-042 #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
-043 x *= 2 - b * x; /* here x*a==1 mod 2**32 */
-044 #endif
-045 #ifdef MP_64BIT
-046 x *= 2 - b * x; /* here x*a==1 mod 2**64 */
-047 #endif
-048
-049 /* rho = -1/m mod b */
-050 *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
-051
-052 return MP_OKAY;
-053 \}
-054 #endif
-055
\end{alltt}
\end{small}
@@ -6792,97 +4447,22 @@ at step 3.
\hspace{-5.1mm}{\bf File}: bn\_mp\_dr\_reduce.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
-018 *
-019 * Based on algorithm from the paper
-020 *
-021 * "Generating Efficient Primes for Discrete Log Cryptosystems"
-022 * Chae Hoon Lim, Pil Joong Lee,
-023 * POSTECH Information Research Laboratories
-024 *
-025 * The modulus must be of a special format [see manual]
-026 *
-027 * Has been modified to use algorithm 7.10 from the LTM book instead
-028 *
-029 * Input x must be in the range 0 <= x <= (n-1)**2
-030 */
-031 int
-032 mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
-033 \{
-034 int err, i, m;
-035 mp_word r;
-036 mp_digit mu, *tmpx1, *tmpx2;
-037
-038 /* m = digits in modulus */
-039 m = n->used;
-040
-041 /* ensure that "x" has at least 2m digits */
-042 if (x->alloc < m + m) \{
-043 if ((err = mp_grow (x, m + m)) != MP_OKAY) \{
-044 return err;
-045 \}
-046 \}
-047
-048 /* top of loop, this is where the code resumes if
-049 * another reduction pass is required.
-050 */
-051 top:
-052 /* aliases for digits */
-053 /* alias for lower half of x */
-054 tmpx1 = x->dp;
-055
-056 /* alias for upper half of x, or x/B**m */
-057 tmpx2 = x->dp + m;
-058
-059 /* set carry to zero */
-060 mu = 0;
-061
-062 /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
-063 for (i = 0; i < m; i++) \{
-064 r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
-065 *tmpx1++ = (mp_digit)(r & MP_MASK);
-066 mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
-067 \}
-068
-069 /* set final carry */
-070 *tmpx1++ = mu;
-071
-072 /* zero words above m */
-073 for (i = m + 1; i < x->used; i++) \{
-074 *tmpx1++ = 0;
-075 \}
-076
-077 /* clamp, sub and return */
-078 mp_clamp (x);
-079
-080 /* if x >= n then subtract and reduce again
-081 * Each successive "recursion" makes the input smaller and smaller.
-082 */
-083 if (mp_cmp_mag (x, n) != MP_LT) \{
-084 s_mp_sub(x, n, x);
-085 goto top;
-086 \}
-087 return MP_OKAY;
-088 \}
-089 #endif
-090
\end{alltt}
\end{small}
-The first step is to grow $x$ as required to $2m$ digits since the reduction is performed in place on $x$. The label on line 51 is where
+The first step is to grow $x$ as required to $2m$ digits since the reduction is performed in place on $x$. The label on line 52 is where
the algorithm will resume if further reduction passes are required. In theory it could be placed at the top of the function however, the size of
the modulus and question of whether $x$ is large enough are invariant after the first pass meaning that it would be a waste of time.
The aliases $tmpx1$ and $tmpx2$ refer to the digits of $x$ where the latter is offset by $m$ digits. By reading digits from $x$ offset by $m$ digits
-a division by $\beta^m$ can be simulated virtually for free. The loop on line 63 performs the bulk of the work (\textit{corresponds to step 4 of algorithm 7.11})
+a division by $\beta^m$ can be simulated virtually for free. The loop on line 64 performs the bulk of the work (\textit{corresponds to step 4 of algorithm 7.11})
in this algorithm.
-By line 70 the pointer $tmpx1$ points to the $m$'th digit of $x$ which is where the final carry will be placed. Similarly by line 73 the
+By line 67 the pointer $tmpx1$ points to the $m$'th digit of $x$ which is where the final carry will be placed. Similarly by line 74 the
same pointer will point to the $m+1$'th digit where the zeroes will be placed.
Since the algorithm is only valid if both $x$ and $n$ are greater than zero an unsigned comparison suffices to determine if another pass is required.
-With the same logic at line 84 the value of $x$ is known to be greater than or equal to $n$ meaning that an unsigned subtraction can be used
+With the same logic at line 81 the value of $x$ is known to be greater than or equal to $n$ meaning that an unsigned subtraction can be used
as well. Since the destination of the subtraction is the larger of the inputs the call to algorithm s\_mp\_sub cannot fail and the return code
does not need to be checked.
@@ -6910,19 +4490,6 @@ completeness.
\hspace{-5.1mm}{\bf File}: bn\_mp\_dr\_setup.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* determines the setup value */
-018 void mp_dr_setup(mp_int *a, mp_digit *d)
-019 \{
-020 /* the casts are required if DIGIT_BIT is one less than
-021 * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
-022 */
-023 *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
-024 ((mp_word)a->dp[0]));
-025 \}
-026
-027 #endif
-028
\end{alltt}
\end{small}
@@ -6958,30 +4525,6 @@ step 3 then $n$ must be of Diminished Radix form.
\hspace{-5.1mm}{\bf File}: bn\_mp\_dr\_is\_modulus.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* determines if a number is a valid DR modulus */
-018 int mp_dr_is_modulus(mp_int *a)
-019 \{
-020 int ix;
-021
-022 /* must be at least two digits */
-023 if (a->used < 2) \{
-024 return 0;
-025 \}
-026
-027 /* must be of the form b**k - a [a <= b] so all
-028 * but the first digit must be equal to -1 (mod b).
-029 */
-030 for (ix = 1; ix < a->used; ix++) \{
-031 if (a->dp[ix] != MP_MASK) \{
-032 return 0;
-033 \}
-034 \}
-035 return 1;
-036 \}
-037
-038 #endif
-039
\end{alltt}
\end{small}
@@ -7025,53 +4568,11 @@ shift which makes the algorithm fairly inexpensive to use.
\hspace{-5.1mm}{\bf File}: bn\_mp\_reduce\_2k.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* reduces a modulo n where n is of the form 2**p - d */
-018 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
-019 \{
-020 mp_int q;
-021 int p, res;
-022
-023 if ((res = mp_init(&q)) != MP_OKAY) \{
-024 return res;
-025 \}
-026
-027 p = mp_count_bits(n);
-028 top:
-029 /* q = a/2**p, a = a mod 2**p */
-030 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) \{
-031 goto ERR;
-032 \}
-033
-034 if (d != 1) \{
-035 /* q = q * d */
-036 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) \{
-037 goto ERR;
-038 \}
-039 \}
-040
-041 /* a = a + q */
-042 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) \{
-043 goto ERR;
-044 \}
-045
-046 if (mp_cmp_mag(a, n) != MP_LT) \{
-047 s_mp_sub(a, n, a);
-048 goto top;
-049 \}
-050
-051 ERR:
-052 mp_clear(&q);
-053 return res;
-054 \}
-055
-056 #endif
-057
\end{alltt}
\end{small}
The algorithm mp\_count\_bits calculates the number of bits in an mp\_int which is used to find the initial value of $p$. The call to mp\_div\_2d
-on line 30 calculates both the quotient $q$ and the remainder $a$ required. By doing both in a single function call the code size
+on line 31 calculates both the quotient $q$ and the remainder $a$ required. By doing both in a single function call the code size
is kept fairly small. The multiplication by $k$ is only performed if $k > 1$. This allows reductions modulo $2^p - 1$ to be performed without
any multiplications.
@@ -7109,34 +4610,6 @@ is sufficient to solve for $k$. Alternatively if $n$ has more than one digit th
\hspace{-5.1mm}{\bf File}: bn\_mp\_reduce\_2k\_setup.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* determines the setup value */
-018 int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
-019 \{
-020 int res, p;
-021 mp_int tmp;
-022
-023 if ((res = mp_init(&tmp)) != MP_OKAY) \{
-024 return res;
-025 \}
-026
-027 p = mp_count_bits(a);
-028 if ((res = mp_2expt(&tmp, p)) != MP_OKAY) \{
-029 mp_clear(&tmp);
-030 return res;
-031 \}
-032
-033 if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) \{
-034 mp_clear(&tmp);
-035 return res;
-036 \}
-037
-038 *d = tmp.dp[0];
-039 mp_clear(&tmp);
-040 return MP_OKAY;
-041 \}
-042 #endif
-043
\end{alltt}
\end{small}
@@ -7181,39 +4654,6 @@ This algorithm quickly determines if a modulus is of the form required for algor
\hspace{-5.1mm}{\bf File}: bn\_mp\_reduce\_is\_2k.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* determines if mp_reduce_2k can be used */
-018 int mp_reduce_is_2k(mp_int *a)
-019 \{
-020 int ix, iy, iw;
-021 mp_digit iz;
-022
-023 if (a->used == 0) \{
-024 return MP_NO;
-025 \} else if (a->used == 1) \{
-026 return MP_YES;
-027 \} else if (a->used > 1) \{
-028 iy = mp_count_bits(a);
-029 iz = 1;
-030 iw = 1;
-031
-032 /* Test every bit from the second digit up, must be 1 */
-033 for (ix = DIGIT_BIT; ix < iy; ix++) \{
-034 if ((a->dp[iw] & iz) == 0) \{
-035 return MP_NO;
-036 \}
-037 iz <<= 1;
-038 if (iz > (mp_digit)MP_MASK) \{
-039 ++iw;
-040 iz = 1;
-041 \}
-042 \}
-043 \}
-044 return MP_YES;
-045 \}
-046
-047 #endif
-048
\end{alltt}
\end{small}
@@ -7386,51 +4826,13 @@ iteration of the loop moves the bits of the exponent $b$ upwards to the most sig
\hspace{-5.1mm}{\bf File}: bn\_mp\_expt\_d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* calculate c = a**b using a square-multiply algorithm */
-018 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
-019 \{
-020 int res, x;
-021 mp_int g;
-022
-023 if ((res = mp_init_copy (&g, a)) != MP_OKAY) \{
-024 return res;
-025 \}
-026
-027 /* set initial result */
-028 mp_set (c, 1);
-029
-030 for (x = 0; x < (int) DIGIT_BIT; x++) \{
-031 /* square */
-032 if ((res = mp_sqr (c, c)) != MP_OKAY) \{
-033 mp_clear (&g);
-034 return res;
-035 \}
-036
-037 /* if the bit is set multiply */
-038 if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) \{
-039 if ((res = mp_mul (c, &g, c)) != MP_OKAY) \{
-040 mp_clear (&g);
-041 return res;
-042 \}
-043 \}
-044
-045 /* shift to next bit */
-046 b <<= 1;
-047 \}
-048
-049 mp_clear (&g);
-050 return MP_OKAY;
-051 \}
-052 #endif
-053
\end{alltt}
\end{small}
-Line 28 sets the initial value of the result to $1$. Next the loop on line 30 steps through each bit of the exponent starting from
-the most significant down towards the least significant. The invariant squaring operation placed on line 32 is performed first. After
+Line 29 sets the initial value of the result to $1$. Next the loop on line 31 steps through each bit of the exponent starting from
+the most significant down towards the least significant. The invariant squaring operation placed on line 33 is performed first. After
the squaring the result $c$ is multiplied by the base $g$ if and only if the most significant bit of the exponent is set. The shift on line
-46 moves all of the bits of the exponent upwards towards the most significant location.
+47 moves all of the bits of the exponent upwards towards the most significant location.
\section{$k$-ary Exponentiation}
When calculating an exponentiation the most time consuming bottleneck is the multiplications which are in general a small factor
@@ -7611,110 +5013,16 @@ algorithm since their arguments are essentially the same (\textit{two mp\_ints a
\hspace{-5.1mm}{\bf File}: bn\_mp\_exptmod.c
\vspace{-3mm}
\begin{alltt}
-016
-017
-018 /* this is a shell function that calls either the normal or Montgomery
-019 * exptmod functions. Originally the call to the montgomery code was
-020 * embedded in the normal function but that wasted alot of stack space
-021 * for nothing (since 99% of the time the Montgomery code would be called)
-022 */
-023 int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
-024 \{
-025 int dr;
-026
-027 /* modulus P must be positive */
-028 if (P->sign == MP_NEG) \{
-029 return MP_VAL;
-030 \}
-031
-032 /* if exponent X is negative we have to recurse */
-033 if (X->sign == MP_NEG) \{
-034 #ifdef BN_MP_INVMOD_C
-035 mp_int tmpG, tmpX;
-036 int err;
-037
-038 /* first compute 1/G mod P */
-039 if ((err = mp_init(&tmpG)) != MP_OKAY) \{
-040 return err;
-041 \}
-042 if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) \{
-043 mp_clear(&tmpG);
-044 return err;
-045 \}
-046
-047 /* now get |X| */
-048 if ((err = mp_init(&tmpX)) != MP_OKAY) \{
-049 mp_clear(&tmpG);
-050 return err;
-051 \}
-052 if ((err = mp_abs(X, &tmpX)) != MP_OKAY) \{
-053 mp_clear_multi(&tmpG, &tmpX, NULL);
-054 return err;
-055 \}
-056
-057 /* and now compute (1/G)**|X| instead of G**X [X < 0] */
-058 err = mp_exptmod(&tmpG, &tmpX, P, Y);
-059 mp_clear_multi(&tmpG, &tmpX, NULL);
-060 return err;
-061 #else
-062 /* no invmod */
-063 return MP_VAL;
-064 #endif
-065 \}
-066
-067 /* modified diminished radix reduction */
-068 #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defin
- ed(BN_S_MP_EXPTMOD_C)
-069 if (mp_reduce_is_2k_l(P) == MP_YES) \{
-070 return s_mp_exptmod(G, X, P, Y, 1);
-071 \}
-072 #endif
-073
-074 #ifdef BN_MP_DR_IS_MODULUS_C
-075 /* is it a DR modulus? */
-076 dr = mp_dr_is_modulus(P);
-077 #else
-078 /* default to no */
-079 dr = 0;
-080 #endif
-081
-082 #ifdef BN_MP_REDUCE_IS_2K_C
-083 /* if not, is it a unrestricted DR modulus? */
-084 if (dr == 0) \{
-085 dr = mp_reduce_is_2k(P) << 1;
-086 \}
-087 #endif
-088
-089 /* if the modulus is odd or dr != 0 use the montgomery method */
-090 #ifdef BN_MP_EXPTMOD_FAST_C
-091 if (mp_isodd (P) == 1 || dr != 0) \{
-092 return mp_exptmod_fast (G, X, P, Y, dr);
-093 \} else \{
-094 #endif
-095 #ifdef BN_S_MP_EXPTMOD_C
-096 /* otherwise use the generic Barrett reduction technique */
-097 return s_mp_exptmod (G, X, P, Y, 0);
-098 #else
-099 /* no exptmod for evens */
-100 return MP_VAL;
-101 #endif
-102 #ifdef BN_MP_EXPTMOD_FAST_C
-103 \}
-104 #endif
-105 \}
-106
-107 #endif
-108
\end{alltt}
\end{small}
-In order to keep the algorithms in a known state the first step on line 28 is to reject any negative modulus as input. If the exponent is
+In order to keep the algorithms in a known state the first step on line 29 is to reject any negative modulus as input. If the exponent is
negative the algorithm tries to perform a modular exponentiation with the modular inverse of the base $G$. The temporary variable $tmpG$ is assigned
the modular inverse of $G$ and $tmpX$ is assigned the absolute value of $X$. The algorithm will recuse with these new values with a positive
exponent.
-If the exponent is positive the algorithm resumes the exponentiation. Line 76 determines if the modulus is of the restricted Diminished Radix
-form. If it is not line 69 attempts to determine if it is of a unrestricted Diminished Radix form. The integer $dr$ will take on one
+If the exponent is positive the algorithm resumes the exponentiation. Line 77 determines if the modulus is of the restricted Diminished Radix
+form. If it is not line 70 attempts to determine if it is of a unrestricted Diminished Radix form. The integer $dr$ will take on one
of three values.
\begin{enumerate}
@@ -7884,252 +5192,18 @@ a Left-to-Right algorithm is used to process the remaining few bits.
\hspace{-5.1mm}{\bf File}: bn\_s\_mp\_exptmod.c
\vspace{-3mm}
\begin{alltt}
-016 #ifdef MP_LOW_MEM
-017 #define TAB_SIZE 32
-018 #else
-019 #define TAB_SIZE 256
-020 #endif
-021
-022 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmod
- e)
-023 \{
-024 mp_int M[TAB_SIZE], res, mu;
-025 mp_digit buf;
-026 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
-027 int (*redux)(mp_int*,mp_int*,mp_int*);
-028
-029 /* find window size */
-030 x = mp_count_bits (X);
-031 if (x <= 7) \{
-032 winsize = 2;
-033 \} else if (x <= 36) \{
-034 winsize = 3;
-035 \} else if (x <= 140) \{
-036 winsize = 4;
-037 \} else if (x <= 450) \{
-038 winsize = 5;
-039 \} else if (x <= 1303) \{
-040 winsize = 6;
-041 \} else if (x <= 3529) \{
-042 winsize = 7;
-043 \} else \{
-044 winsize = 8;
-045 \}
-046
-047 #ifdef MP_LOW_MEM
-048 if (winsize > 5) \{
-049 winsize = 5;
-050 \}
-051 #endif
-052
-053 /* init M array */
-054 /* init first cell */
-055 if ((err = mp_init(&M[1])) != MP_OKAY) \{
-056 return err;
-057 \}
-058
-059 /* now init the second half of the array */
-060 for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
-061 if ((err = mp_init(&M[x])) != MP_OKAY) \{
-062 for (y = 1<<(winsize-1); y < x; y++) \{
-063 mp_clear (&M[y]);
-064 \}
-065 mp_clear(&M[1]);
-066 return err;
-067 \}
-068 \}
-069
-070 /* create mu, used for Barrett reduction */
-071 if ((err = mp_init (&mu)) != MP_OKAY) \{
-072 goto LBL_M;
-073 \}
-074
-075 if (redmode == 0) \{
-076 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) \{
-077 goto LBL_MU;
-078 \}
-079 redux = mp_reduce;
-080 \} else \{
-081 if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) \{
-082 goto LBL_MU;
-083 \}
-084 redux = mp_reduce_2k_l;
-085 \}
-086
-087 /* create M table
-088 *
-089 * The M table contains powers of the base,
-090 * e.g. M[x] = G**x mod P
-091 *
-092 * The first half of the table is not
-093 * computed though accept for M[0] and M[1]
-094 */
-095 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) \{
-096 goto LBL_MU;
-097 \}
-098
-099 /* compute the value at M[1<<(winsize-1)] by squaring
-100 * M[1] (winsize-1) times
-101 */
-102 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) \{
-103 goto LBL_MU;
-104 \}
-105
-106 for (x = 0; x < (winsize - 1); x++) \{
-107 /* square it */
-108 if ((err = mp_sqr (&M[1 << (winsize - 1)],
-109 &M[1 << (winsize - 1)])) != MP_OKAY) \{
-110 goto LBL_MU;
-111 \}
-112
-113 /* reduce modulo P */
-114 if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) \{
-115 goto LBL_MU;
-116 \}
-117 \}
-118
-119 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
-120 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
-121 */
-122 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) \{
-123 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) \{
-124 goto LBL_MU;
-125 \}
-126 if ((err = redux (&M[x], P, &mu)) != MP_OKAY) \{
-127 goto LBL_MU;
-128 \}
-129 \}
-130
-131 /* setup result */
-132 if ((err = mp_init (&res)) != MP_OKAY) \{
-133 goto LBL_MU;
-134 \}
-135 mp_set (&res, 1);
-136
-137 /* set initial mode and bit cnt */
-138 mode = 0;
-139 bitcnt = 1;
-140 buf = 0;
-141 digidx = X->used - 1;
-142 bitcpy = 0;
-143 bitbuf = 0;
-144
-145 for (;;) \{
-146 /* grab next digit as required */
-147 if (--bitcnt == 0) \{
-148 /* if digidx == -1 we are out of digits */
-149 if (digidx == -1) \{
-150 break;
-151 \}
-152 /* read next digit and reset the bitcnt */
-153 buf = X->dp[digidx--];
-154 bitcnt = (int) DIGIT_BIT;
-155 \}
-156
-157 /* grab the next msb from the exponent */
-158 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
-159 buf <<= (mp_digit)1;
-160
-161 /* if the bit is zero and mode == 0 then we ignore it
-162 * These represent the leading zero bits before the first 1 bit
-163 * in the exponent. Technically this opt is not required but it
-164 * does lower the # of trivial squaring/reductions used
-165 */
-166 if (mode == 0 && y == 0) \{
-167 continue;
-168 \}
-169
-170 /* if the bit is zero and mode == 1 then we square */
-171 if (mode == 1 && y == 0) \{
-172 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
-173 goto LBL_RES;
-174 \}
-175 if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
-176 goto LBL_RES;
-177 \}
-178 continue;
-179 \}
-180
-181 /* else we add it to the window */
-182 bitbuf |= (y << (winsize - ++bitcpy));
-183 mode = 2;
-184
-185 if (bitcpy == winsize) \{
-186 /* ok window is filled so square as required and multiply */
-187 /* square first */
-188 for (x = 0; x < winsize; x++) \{
-189 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
-190 goto LBL_RES;
-191 \}
-192 if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
-193 goto LBL_RES;
-194 \}
-195 \}
-196
-197 /* then multiply */
-198 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) \{
-199 goto LBL_RES;
-200 \}
-201 if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
-202 goto LBL_RES;
-203 \}
-204
-205 /* empty window and reset */
-206 bitcpy = 0;
-207 bitbuf = 0;
-208 mode = 1;
-209 \}
-210 \}
-211
-212 /* if bits remain then square/multiply */
-213 if (mode == 2 && bitcpy > 0) \{
-214 /* square then multiply if the bit is set */
-215 for (x = 0; x < bitcpy; x++) \{
-216 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
-217 goto LBL_RES;
-218 \}
-219 if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
-220 goto LBL_RES;
-221 \}
-222
-223 bitbuf <<= 1;
-224 if ((bitbuf & (1 << winsize)) != 0) \{
-225 /* then multiply */
-226 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) \{
-227 goto LBL_RES;
-228 \}
-229 if ((err = redux (&res, P, &mu)) != MP_OKAY) \{
-230 goto LBL_RES;
-231 \}
-232 \}
-233 \}
-234 \}
-235
-236 mp_exch (&res, Y);
-237 err = MP_OKAY;
-238 LBL_RES:mp_clear (&res);
-239 LBL_MU:mp_clear (&mu);
-240 LBL_M:
-241 mp_clear(&M[1]);
-242 for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
-243 mp_clear (&M[x]);
-244 \}
-245 return err;
-246 \}
-247 #endif
-248
\end{alltt}
\end{small}
-Lines 31 through 41 determine the optimal window size based on the length of the exponent in bits. The window divisions are sorted
+Lines 32 through 46 determine the optimal window size based on the length of the exponent in bits. The window divisions are sorted
from smallest to greatest so that in each \textbf{if} statement only one condition must be tested. For example, by the \textbf{if} statement
-on line 33 the value of $x$ is already known to be greater than $140$.
+on line 38 the value of $x$ is already known to be greater than $140$.
-The conditional piece of code beginning on line 47 allows the window size to be restricted to five bits. This logic is used to ensure
+The conditional piece of code beginning on line 48 allows the window size to be restricted to five bits. This logic is used to ensure
the table of precomputed powers of $G$ remains relatively small.
-The for loop on line 60 initializes the $M$ array while lines 61 and 76 compute the value of $\mu$ required for
-Barrett reduction.
+The for loop on line 61 initializes the $M$ array while lines 72 and 77 through 86 initialize the reduction
+function that will be used for this modulus.
-- More later.
@@ -8163,35 +5237,6 @@ equivalent to $m \cdot 2^k$. By this logic when $m = 1$ a quick power of two ca
\hspace{-5.1mm}{\bf File}: bn\_mp\_2expt.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* computes a = 2**b
-018 *
-019 * Simple algorithm which zeroes the int, grows it then just sets one bit
-020 * as required.
-021 */
-022 int
-023 mp_2expt (mp_int * a, int b)
-024 \{
-025 int res;
-026
-027 /* zero a as per default */
-028 mp_zero (a);
-029
-030 /* grow a to accomodate the single bit */
-031 if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) \{
-032 return res;
-033 \}
-034
-035 /* set the used count of where the bit will go */
-036 a->used = b / DIGIT_BIT + 1;
-037
-038 /* put the single bit in its place */
-039 a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
-040
-041 return MP_OKAY;
-042 \}
-043 #endif
-044
\end{alltt}
\end{small}
@@ -8440,279 +5485,6 @@ respectively be replaced with a zero.
\hspace{-5.1mm}{\bf File}: bn\_mp\_div.c
\vspace{-3mm}
\begin{alltt}
-016
-017 #ifdef BN_MP_DIV_SMALL
-018
-019 /* slower bit-bang division... also smaller */
-020 int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
-021 \{
-022 mp_int ta, tb, tq, q;
-023 int res, n, n2;
-024
-025 /* is divisor zero ? */
-026 if (mp_iszero (b) == 1) \{
-027 return MP_VAL;
-028 \}
-029
-030 /* if a < b then q=0, r = a */
-031 if (mp_cmp_mag (a, b) == MP_LT) \{
-032 if (d != NULL) \{
-033 res = mp_copy (a, d);
-034 \} else \{
-035 res = MP_OKAY;
-036 \}
-037 if (c != NULL) \{
-038 mp_zero (c);
-039 \}
-040 return res;
-041 \}
-042
-043 /* init our temps */
-044 if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != MP_OKAY)) \{
-045 return res;
-046 \}
-047
-048
-049 mp_set(&tq, 1);
-050 n = mp_count_bits(a) - mp_count_bits(b);
-051 if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
-052 ((res = mp_abs(b, &tb)) != MP_OKAY) ||
-053 ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
-054 ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) \{
-055 goto LBL_ERR;
-056 \}
-057
-058 while (n-- >= 0) \{
-059 if (mp_cmp(&tb, &ta) != MP_GT) \{
-060 if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
-061 ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) \{
-062 goto LBL_ERR;
-063 \}
-064 \}
-065 if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
-066 ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) \{
-067 goto LBL_ERR;
-068 \}
-069 \}
-070
-071 /* now q == quotient and ta == remainder */
-072 n = a->sign;
-073 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
-074 if (c != NULL) \{
-075 mp_exch(c, &q);
-076 c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
-077 \}
-078 if (d != NULL) \{
-079 mp_exch(d, &ta);
-080 d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
-081 \}
-082 LBL_ERR:
-083 mp_clear_multi(&ta, &tb, &tq, &q, NULL);
-084 return res;
-085 \}
-086
-087 #else
-088
-089 /* integer signed division.
-090 * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
-091 * HAC pp.598 Algorithm 14.20
-092 *
-093 * Note that the description in HAC is horribly
-094 * incomplete. For example, it doesn't consider
-095 * the case where digits are removed from 'x' in
-096 * the inner loop. It also doesn't consider the
-097 * case that y has fewer than three digits, etc..
-098 *
-099 * The overall algorithm is as described as
-100 * 14.20 from HAC but fixed to treat these cases.
-101 */
-102 int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
-103 \{
-104 mp_int q, x, y, t1, t2;
-105 int res, n, t, i, norm, neg;
-106
-107 /* is divisor zero ? */
-108 if (mp_iszero (b) == 1) \{
-109 return MP_VAL;
-110 \}
-111
-112 /* if a < b then q=0, r = a */
-113 if (mp_cmp_mag (a, b) == MP_LT) \{
-114 if (d != NULL) \{
-115 res = mp_copy (a, d);
-116 \} else \{
-117 res = MP_OKAY;
-118 \}
-119 if (c != NULL) \{
-120 mp_zero (c);
-121 \}
-122 return res;
-123 \}
-124
-125 if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) \{
-126 return res;
-127 \}
-128 q.used = a->used + 2;
-129
-130 if ((res = mp_init (&t1)) != MP_OKAY) \{
-131 goto LBL_Q;
-132 \}
-133
-134 if ((res = mp_init (&t2)) != MP_OKAY) \{
-135 goto LBL_T1;
-136 \}
-137
-138 if ((res = mp_init_copy (&x, a)) != MP_OKAY) \{
-139 goto LBL_T2;
-140 \}
-141
-142 if ((res = mp_init_copy (&y, b)) != MP_OKAY) \{
-143 goto LBL_X;
-144 \}
-145
-146 /* fix the sign */
-147 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
-148 x.sign = y.sign = MP_ZPOS;
-149
-150 /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
-151 norm = mp_count_bits(&y) % DIGIT_BIT;
-152 if (norm < (int)(DIGIT_BIT-1)) \{
-153 norm = (DIGIT_BIT-1) - norm;
-154 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) \{
-155 goto LBL_Y;
-156 \}
-157 if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) \{
-158 goto LBL_Y;
-159 \}
-160 \} else \{
-161 norm = 0;
-162 \}
-163
-164 /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
-165 n = x.used - 1;
-166 t = y.used - 1;
-167
-168 /* while (x >= y*b**n-t) do \{ q[n-t] += 1; x -= y*b**\{n-t\} \} */
-169 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) \{ /* y = y*b**\{n-t\} */
-170 goto LBL_Y;
-171 \}
-172
-173 while (mp_cmp (&x, &y) != MP_LT) \{
-174 ++(q.dp[n - t]);
-175 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) \{
-176 goto LBL_Y;
-177 \}
-178 \}
-179
-180 /* reset y by shifting it back down */
-181 mp_rshd (&y, n - t);
-182
-183 /* step 3. for i from n down to (t + 1) */
-184 for (i = n; i >= (t + 1); i--) \{
-185 if (i > x.used) \{
-186 continue;
-187 \}
-188
-189 /* step 3.1 if xi == yt then set q\{i-t-1\} to b-1,
-190 * otherwise set q\{i-t-1\} to (xi*b + x\{i-1\})/yt */
-191 if (x.dp[i] == y.dp[t]) \{
-192 q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
-193 \} else \{
-194 mp_word tmp;
-195 tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
-196 tmp |= ((mp_word) x.dp[i - 1]);
-197 tmp /= ((mp_word) y.dp[t]);
-198 if (tmp > (mp_word) MP_MASK)
-199 tmp = MP_MASK;
-200 q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
-201 \}
-202
-203 /* while (q\{i-t-1\} * (yt * b + y\{t-1\})) >
-204 xi * b**2 + xi-1 * b + xi-2
-205
-206 do q\{i-t-1\} -= 1;
-207 */
-208 q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
-209 do \{
-210 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK;
-211
-212 /* find left hand */
-213 mp_zero (&t1);
-214 t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
-215 t1.dp[1] = y.dp[t];
-216 t1.used = 2;
-217 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) \{
-218 goto LBL_Y;
-219 \}
-220
-221 /* find right hand */
-222 t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
-223 t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
-224 t2.dp[2] = x.dp[i];
-225 t2.used = 3;
-226 \} while (mp_cmp_mag(&t1, &t2) == MP_GT);
-227
-228 /* step 3.3 x = x - q\{i-t-1\} * y * b**\{i-t-1\} */
-229 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) \{
-230 goto LBL_Y;
-231 \}
-232
-233 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{
-234 goto LBL_Y;
-235 \}
-236
-237 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) \{
-238 goto LBL_Y;
-239 \}
-240
-241 /* if x < 0 then \{ x = x + y*b**\{i-t-1\}; q\{i-t-1\} -= 1; \} */
-242 if (x.sign == MP_NEG) \{
-243 if ((res = mp_copy (&y, &t1)) != MP_OKAY) \{
-244 goto LBL_Y;
-245 \}
-246 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{
-247 goto LBL_Y;
-248 \}
-249 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) \{
-250 goto LBL_Y;
-251 \}
-252
-253 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
-254 \}
-255 \}
-256
-257 /* now q is the quotient and x is the remainder
-258 * [which we have to normalize]
-259 */
-260
-261 /* get sign before writing to c */
-262 x.sign = x.used == 0 ? MP_ZPOS : a->sign;
-263
-264 if (c != NULL) \{
-265 mp_clamp (&q);
-266 mp_exch (&q, c);
-267 c->sign = neg;
-268 \}
-269
-270 if (d != NULL) \{
-271 mp_div_2d (&x, norm, &x, NULL);
-272 mp_exch (&x, d);
-273 \}
-274
-275 res = MP_OKAY;
-276
-277 LBL_Y:mp_clear (&y);
-278 LBL_X:mp_clear (&x);
-279 LBL_T2:mp_clear (&t2);
-280 LBL_T1:mp_clear (&t1);
-281 LBL_Q:mp_clear (&q);
-282 return res;
-283 \}
-284
-285 #endif
-286
-287 #endif
-288
\end{alltt}
\end{small}
@@ -8724,23 +5496,23 @@ algorithm with only the quotient is
mp_div(&a, &b, &c, NULL); /* c = [a/b] */
\end{verbatim}
-Lines 37 and 44 handle the two trivial cases of inputs which are division by zero and dividend smaller than the divisor
-respectively. After the two trivial cases all of the temporary variables are initialized. Line 105 determines the sign of
-the quotient and line 76 ensures that both $x$ and $y$ are positive.
+Lines 109 and 113 handle the two trivial cases of inputs which are division by zero and dividend smaller than the divisor
+respectively. After the two trivial cases all of the temporary variables are initialized. Line 148 determines the sign of
+the quotient and line 148 ensures that both $x$ and $y$ are positive.
-The number of bits in the leading digit is calculated on line 105. Implictly an mp\_int with $r$ digits will require $lg(\beta)(r-1) + k$ bits
+The number of bits in the leading digit is calculated on line 151. Implictly an mp\_int with $r$ digits will require $lg(\beta)(r-1) + k$ bits
of precision which when reduced modulo $lg(\beta)$ produces the value of $k$. In this case $k$ is the number of bits in the leading digit which is
exactly what is required. For the algorithm to operate $k$ must equal $lg(\beta) - 1$ and when it does not the inputs must be normalized by shifting
them to the left by $lg(\beta) - 1 - k$ bits.
Throughout the variables $n$ and $t$ will represent the highest digit of $x$ and $y$ respectively. These are first used to produce the
-leading digit of the quotient. The loop beginning on line 183 will produce the remainder of the quotient digits.
+leading digit of the quotient. The loop beginning on line 184 will produce the remainder of the quotient digits.
-The conditional ``continue'' on line 114 is used to prevent the algorithm from reading past the leading edge of $x$ which can occur when the
+The conditional ``continue'' on line 187 is used to prevent the algorithm from reading past the leading edge of $x$ which can occur when the
algorithm eliminates multiple non-zero digits in a single iteration. This ensures that $x_i$ is always non-zero since by definition the digits
above the $i$'th position $x$ must be zero in order for the quotient to be precise\footnote{Precise as far as integer division is concerned.}.
-Lines 130, 130 and 134 through 134 manually construct the high accuracy estimations by setting the digits of the two mp\_int
+Lines 214, 216 and 223 through 225 manually construct the high accuracy estimations by setting the digits of the two mp\_int
variables directly.
\section{Single Digit Helpers}
@@ -8778,99 +5550,6 @@ This algorithm initiates a temporary mp\_int with the value of the single digit
\hspace{-5.1mm}{\bf File}: bn\_mp\_add\_d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* single digit addition */
-018 int
-019 mp_add_d (mp_int * a, mp_digit b, mp_int * c)
-020 \{
-021 int res, ix, oldused;
-022 mp_digit *tmpa, *tmpc, mu;
-023
-024 /* grow c as required */
-025 if (c->alloc < a->used + 1) \{
-026 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) \{
-027 return res;
-028 \}
-029 \}
-030
-031 /* if a is negative and |a| >= b, call c = |a| - b */
-032 if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) \{
-033 /* temporarily fix sign of a */
-034 a->sign = MP_ZPOS;
-035
-036 /* c = |a| - b */
-037 res = mp_sub_d(a, b, c);
-038
-039 /* fix sign */
-040 a->sign = c->sign = MP_NEG;
-041
-042 /* clamp */
-043 mp_clamp(c);
-044
-045 return res;
-046 \}
-047
-048 /* old number of used digits in c */
-049 oldused = c->used;
-050
-051 /* sign always positive */
-052 c->sign = MP_ZPOS;
-053
-054 /* source alias */
-055 tmpa = a->dp;
-056
-057 /* destination alias */
-058 tmpc = c->dp;
-059
-060 /* if a is positive */
-061 if (a->sign == MP_ZPOS) \{
-062 /* add digit, after this we're propagating
-063 * the carry.
-064 */
-065 *tmpc = *tmpa++ + b;
-066 mu = *tmpc >> DIGIT_BIT;
-067 *tmpc++ &= MP_MASK;
-068
-069 /* now handle rest of the digits */
-070 for (ix = 1; ix < a->used; ix++) \{
-071 *tmpc = *tmpa++ + mu;
-072 mu = *tmpc >> DIGIT_BIT;
-073 *tmpc++ &= MP_MASK;
-074 \}
-075 /* set final carry */
-076 ix++;
-077 *tmpc++ = mu;
-078
-079 /* setup size */
-080 c->used = a->used + 1;
-081 \} else \{
-082 /* a was negative and |a| < b */
-083 c->used = 1;
-084
-085 /* the result is a single digit */
-086 if (a->used == 1) \{
-087 *tmpc++ = b - a->dp[0];
-088 \} else \{
-089 *tmpc++ = b;
-090 \}
-091
-092 /* setup count so the clearing of oldused
-093 * can fall through correctly
-094 */
-095 ix = 1;
-096 \}
-097
-098 /* now zero to oldused */
-099 while (ix++ < oldused) \{
-100 *tmpc++ = 0;
-101 \}
-102 mp_clamp(c);
-103
-104 return MP_OKAY;
-105 \}
-106
-107 #endif
-108
\end{alltt}
\end{small}
@@ -8921,66 +5600,6 @@ Unlike the full multiplication algorithms this algorithm does not require any si
\hspace{-5.1mm}{\bf File}: bn\_mp\_mul\_d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* multiply by a digit */
-018 int
-019 mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
-020 \{
-021 mp_digit u, *tmpa, *tmpc;
-022 mp_word r;
-023 int ix, res, olduse;
-024
-025 /* make sure c is big enough to hold a*b */
-026 if (c->alloc < a->used + 1) \{
-027 if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) \{
-028 return res;
-029 \}
-030 \}
-031
-032 /* get the original destinations used count */
-033 olduse = c->used;
-034
-035 /* set the sign */
-036 c->sign = a->sign;
-037
-038 /* alias for a->dp [source] */
-039 tmpa = a->dp;
-040
-041 /* alias for c->dp [dest] */
-042 tmpc = c->dp;
-043
-044 /* zero carry */
-045 u = 0;
-046
-047 /* compute columns */
-048 for (ix = 0; ix < a->used; ix++) \{
-049 /* compute product and carry sum for this term */
-050 r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
-051
-052 /* mask off higher bits to get a single digit */
-053 *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
-054
-055 /* send carry into next iteration */
-056 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
-057 \}
-058
-059 /* store final carry [if any] and increment ix offset */
-060 *tmpc++ = u;
-061 ++ix;
-062
-063 /* now zero digits above the top */
-064 while (ix++ < olduse) \{
-065 *tmpc++ = 0;
-066 \}
-067
-068 /* set used count */
-069 c->used = a->used + 1;
-070 mp_clamp(c);
-071
-072 return MP_OKAY;
-073 \}
-074 #endif
-075
\end{alltt}
\end{small}
@@ -9036,104 +5655,13 @@ from chapter seven.
\hspace{-5.1mm}{\bf File}: bn\_mp\_div\_d.c
\vspace{-3mm}
\begin{alltt}
-016
-017 static int s_is_power_of_two(mp_digit b, int *p)
-018 \{
-019 int x;
-020
-021 for (x = 1; x < DIGIT_BIT; x++) \{
-022 if (b == (((mp_digit)1)<<x)) \{
-023 *p = x;
-024 return 1;
-025 \}
-026 \}
-027 return 0;
-028 \}
-029
-030 /* single digit division (based on routine from MPI) */
-031 int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
-032 \{
-033 mp_int q;
-034 mp_word w;
-035 mp_digit t;
-036 int res, ix;
-037
-038 /* cannot divide by zero */
-039 if (b == 0) \{
-040 return MP_VAL;
-041 \}
-042
-043 /* quick outs */
-044 if (b == 1 || mp_iszero(a) == 1) \{
-045 if (d != NULL) \{
-046 *d = 0;
-047 \}
-048 if (c != NULL) \{
-049 return mp_copy(a, c);
-050 \}
-051 return MP_OKAY;
-052 \}
-053
-054 /* power of two ? */
-055 if (s_is_power_of_two(b, &ix) == 1) \{
-056 if (d != NULL) \{
-057 *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
-058 \}
-059 if (c != NULL) \{
-060 return mp_div_2d(a, ix, c, NULL);
-061 \}
-062 return MP_OKAY;
-063 \}
-064
-065 #ifdef BN_MP_DIV_3_C
-066 /* three? */
-067 if (b == 3) \{
-068 return mp_div_3(a, c, d);
-069 \}
-070 #endif
-071
-072 /* no easy answer [c'est la vie]. Just division */
-073 if ((res = mp_init_size(&q, a->used)) != MP_OKAY) \{
-074 return res;
-075 \}
-076
-077 q.used = a->used;
-078 q.sign = a->sign;
-079 w = 0;
-080 for (ix = a->used - 1; ix >= 0; ix--) \{
-081 w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
-082
-083 if (w >= b) \{
-084 t = (mp_digit)(w / b);
-085 w -= ((mp_word)t) * ((mp_word)b);
-086 \} else \{
-087 t = 0;
-088 \}
-089 q.dp[ix] = (mp_digit)t;
-090 \}
-091
-092 if (d != NULL) \{
-093 *d = (mp_digit)w;
-094 \}
-095
-096 if (c != NULL) \{
-097 mp_clamp(&q);
-098 mp_exch(&q, c);
-099 \}
-100 mp_clear(&q);
-101
-102 return res;
-103 \}
-104
-105 #endif
-106
\end{alltt}
\end{small}
Like the implementation of algorithm mp\_div this algorithm allows either of the quotient or remainder to be passed as a \textbf{NULL} pointer to
indicate the respective value is not required. This allows a trivial single digit modular reduction algorithm, mp\_mod\_d to be created.
-The division and remainder on lines 43 and @45,%@ can be replaced often by a single division on most processors. For example, the 32-bit x86 based
+The division and remainder on lines 44 and @45,%@ can be replaced often by a single division on most processors. For example, the 32-bit x86 based
processors can divide a 64-bit quantity by a 32-bit quantity and produce the quotient and remainder simultaneously. Unfortunately the GCC
compiler does not recognize that optimization and will actually produce two function calls to find the quotient and remainder respectively.
@@ -9201,119 +5729,6 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where
\hspace{-5.1mm}{\bf File}: bn\_mp\_n\_root.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* find the n'th root of an integer
-018 *
-019 * Result found such that (c)**b <= a and (c+1)**b > a
-020 *
-021 * This algorithm uses Newton's approximation
-022 * x[i+1] = x[i] - f(x[i])/f'(x[i])
-023 * which will find the root in log(N) time where
-024 * each step involves a fair bit. This is not meant to
-025 * find huge roots [square and cube, etc].
-026 */
-027 int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
-028 \{
-029 mp_int t1, t2, t3;
-030 int res, neg;
-031
-032 /* input must be positive if b is even */
-033 if ((b & 1) == 0 && a->sign == MP_NEG) \{
-034 return MP_VAL;
-035 \}
-036
-037 if ((res = mp_init (&t1)) != MP_OKAY) \{
-038 return res;
-039 \}
-040
-041 if ((res = mp_init (&t2)) != MP_OKAY) \{
-042 goto LBL_T1;
-043 \}
-044
-045 if ((res = mp_init (&t3)) != MP_OKAY) \{
-046 goto LBL_T2;
-047 \}
-048
-049 /* if a is negative fudge the sign but keep track */
-050 neg = a->sign;
-051 a->sign = MP_ZPOS;
-052
-053 /* t2 = 2 */
-054 mp_set (&t2, 2);
-055
-056 do \{
-057 /* t1 = t2 */
-058 if ((res = mp_copy (&t2, &t1)) != MP_OKAY) \{
-059 goto LBL_T3;
-060 \}
-061
-062 /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
-063
-064 /* t3 = t1**(b-1) */
-065 if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) \{
-066 goto LBL_T3;
-067 \}
-068
-069 /* numerator */
-070 /* t2 = t1**b */
-071 if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) \{
-072 goto LBL_T3;
-073 \}
-074
-075 /* t2 = t1**b - a */
-076 if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) \{
-077 goto LBL_T3;
-078 \}
-079
-080 /* denominator */
-081 /* t3 = t1**(b-1) * b */
-082 if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) \{
-083 goto LBL_T3;
-084 \}
-085
-086 /* t3 = (t1**b - a)/(b * t1**(b-1)) */
-087 if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) \{
-088 goto LBL_T3;
-089 \}
-090
-091 if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) \{
-092 goto LBL_T3;
-093 \}
-094 \} while (mp_cmp (&t1, &t2) != MP_EQ);
-095
-096 /* result can be off by a few so check */
-097 for (;;) \{
-098 if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) \{
-099 goto LBL_T3;
-100 \}
-101
-102 if (mp_cmp (&t2, a) == MP_GT) \{
-103 if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) \{
-104 goto LBL_T3;
-105 \}
-106 \} else \{
-107 break;
-108 \}
-109 \}
-110
-111 /* reset the sign of a first */
-112 a->sign = neg;
-113
-114 /* set the result */
-115 mp_exch (&t1, c);
-116
-117 /* set the sign of the result */
-118 c->sign = neg;
-119
-120 res = MP_OKAY;
-121
-122 LBL_T3:mp_clear (&t3);
-123 LBL_T2:mp_clear (&t2);
-124 LBL_T1:mp_clear (&t1);
-125 return res;
-126 \}
-127 #endif
-128
\end{alltt}
\end{small}
@@ -9355,42 +5770,6 @@ the integers from $0$ to $\beta - 1$.
\hspace{-5.1mm}{\bf File}: bn\_mp\_rand.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* makes a pseudo-random int of a given size */
-018 int
-019 mp_rand (mp_int * a, int digits)
-020 \{
-021 int res;
-022 mp_digit d;
-023
-024 mp_zero (a);
-025 if (digits <= 0) \{
-026 return MP_OKAY;
-027 \}
-028
-029 /* first place a random non-zero digit */
-030 do \{
-031 d = ((mp_digit) abs (rand ())) & MP_MASK;
-032 \} while (d == 0);
-033
-034 if ((res = mp_add_d (a, d, a)) != MP_OKAY) \{
-035 return res;
-036 \}
-037
-038 while (--digits > 0) \{
-039 if ((res = mp_lshd (a, 1)) != MP_OKAY) \{
-040 return res;
-041 \}
-042
-043 if ((res = mp_add_d (a, ((mp_digit) abs (rand ())), a)) != MP_OKAY) \{
-044 return res;
-045 \}
-046 \}
-047
-048 return MP_OKAY;
-049 \}
-050 #endif
-051
\end{alltt}
\end{small}
@@ -9473,72 +5852,6 @@ as part of larger input without any significant problem.
\hspace{-5.1mm}{\bf File}: bn\_mp\_read\_radix.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* read a string [ASCII] in a given radix */
-018 int mp_read_radix (mp_int * a, const char *str, int radix)
-019 \{
-020 int y, res, neg;
-021 char ch;
-022
-023 /* zero the digit bignum */
-024 mp_zero(a);
-025
-026 /* make sure the radix is ok */
-027 if (radix < 2 || radix > 64) \{
-028 return MP_VAL;
-029 \}
-030
-031 /* if the leading digit is a
-032 * minus set the sign to negative.
-033 */
-034 if (*str == '-') \{
-035 ++str;
-036 neg = MP_NEG;
-037 \} else \{
-038 neg = MP_ZPOS;
-039 \}
-040
-041 /* set the integer to the default of zero */
-042 mp_zero (a);
-043
-044 /* process each digit of the string */
-045 while (*str) \{
-046 /* if the radix < 36 the conversion is case insensitive
-047 * this allows numbers like 1AB and 1ab to represent the same value
-048 * [e.g. in hex]
-049 */
-050 ch = (char) ((radix < 36) ? toupper (*str) : *str);
-051 for (y = 0; y < 64; y++) \{
-052 if (ch == mp_s_rmap[y]) \{
-053 break;
-054 \}
-055 \}
-056
-057 /* if the char was found in the map
-058 * and is less than the given radix add it
-059 * to the number, otherwise exit the loop.
-060 */
-061 if (y < radix) \{
-062 if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) \{
-063 return res;
-064 \}
-065 if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) \{
-066 return res;
-067 \}
-068 \} else \{
-069 break;
-070 \}
-071 ++str;
-072 \}
-073
-074 /* set the sign only if a != 0 */
-075 if (mp_iszero(a) != 1) \{
-076 a->sign = neg;
-077 \}
-078 return MP_OKAY;
-079 \}
-080 #endif
-081
\end{alltt}
\end{small}
@@ -9603,62 +5916,6 @@ are required instead of a series of $n \times k$ divisions. One design flaw of
\hspace{-5.1mm}{\bf File}: bn\_mp\_toradix.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* stores a bignum as a ASCII string in a given radix (2..64) */
-018 int mp_toradix (mp_int * a, char *str, int radix)
-019 \{
-020 int res, digs;
-021 mp_int t;
-022 mp_digit d;
-023 char *_s = str;
-024
-025 /* check range of the radix */
-026 if (radix < 2 || radix > 64) \{
-027 return MP_VAL;
-028 \}
-029
-030 /* quick out if its zero */
-031 if (mp_iszero(a) == 1) \{
-032 *str++ = '0';
-033 *str = '\symbol{92}0';
-034 return MP_OKAY;
-035 \}
-036
-037 if ((res = mp_init_copy (&t, a)) != MP_OKAY) \{
-038 return res;
-039 \}
-040
-041 /* if it is negative output a - */
-042 if (t.sign == MP_NEG) \{
-043 ++_s;
-044 *str++ = '-';
-045 t.sign = MP_ZPOS;
-046 \}
-047
-048 digs = 0;
-049 while (mp_iszero (&t) == 0) \{
-050 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) \{
-051 mp_clear (&t);
-052 return res;
-053 \}
-054 *str++ = mp_s_rmap[d];
-055 ++digs;
-056 \}
-057
-058 /* reverse the digits of the string. In this case _s points
-059 * to the first digit [exluding the sign] of the number]
-060 */
-061 bn_reverse ((unsigned char *)_s, digs);
-062
-063 /* append a NULL so the string is properly terminated */
-064 *str = '\symbol{92}0';
-065
-066 mp_clear (&t);
-067 return MP_OKAY;
-068 \}
-069
-070 #endif
-071
\end{alltt}
\end{small}
@@ -9788,33 +6045,30 @@ and will produce the greatest common divisor.
\textbf{Input}. mp\_int $a$ and $b$ \\
\textbf{Output}. The greatest common divisor $c = (a, b)$. \\
\hline \\
-1. If $a = 0$ and $b \ne 0$ then \\
-\hspace{3mm}1.1 $c \leftarrow b$ \\
+1. If $a = 0$ then \\
+\hspace{3mm}1.1 $c \leftarrow \vert b \vert $ \\
\hspace{3mm}1.2 Return(\textit{MP\_OKAY}). \\
-2. If $a \ne 0$ and $b = 0$ then \\
-\hspace{3mm}2.1 $c \leftarrow a$ \\
+2. If $b = 0$ then \\
+\hspace{3mm}2.1 $c \leftarrow \vert a \vert $ \\
\hspace{3mm}2.2 Return(\textit{MP\_OKAY}). \\
-3. If $a = b = 0$ then \\
-\hspace{3mm}3.1 $c \leftarrow 1$ \\
-\hspace{3mm}3.2 Return(\textit{MP\_OKAY}). \\
-4. $u \leftarrow \vert a \vert, v \leftarrow \vert b \vert$ \\
-5. $k \leftarrow 0$ \\
-6. While $u.used > 0$ and $v.used > 0$ and $u_0 \equiv v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{3mm}6.1 $k \leftarrow k + 1$ \\
-\hspace{3mm}6.2 $u \leftarrow \lfloor u / 2 \rfloor$ \\
-\hspace{3mm}6.3 $v \leftarrow \lfloor v / 2 \rfloor$ \\
-7. While $u.used > 0$ and $u_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{3mm}7.1 $u \leftarrow \lfloor u / 2 \rfloor$ \\
-8. While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{3mm}8.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
-9. While $v.used > 0$ \\
-\hspace{3mm}9.1 If $\vert u \vert > \vert v \vert$ then \\
-\hspace{6mm}9.1.1 Swap $u$ and $v$. \\
-\hspace{3mm}9.2 $v \leftarrow \vert v \vert - \vert u \vert$ \\
-\hspace{3mm}9.3 While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
-\hspace{6mm}9.3.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
-10. $c \leftarrow u \cdot 2^k$ \\
-11. Return(\textit{MP\_OKAY}). \\
+3. $u \leftarrow \vert a \vert, v \leftarrow \vert b \vert$ \\
+4. $k \leftarrow 0$ \\
+5. While $u.used > 0$ and $v.used > 0$ and $u_0 \equiv v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{3mm}5.1 $k \leftarrow k + 1$ \\
+\hspace{3mm}5.2 $u \leftarrow \lfloor u / 2 \rfloor$ \\
+\hspace{3mm}5.3 $v \leftarrow \lfloor v / 2 \rfloor$ \\
+6. While $u.used > 0$ and $u_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{3mm}6.1 $u \leftarrow \lfloor u / 2 \rfloor$ \\
+7. While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{3mm}7.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
+8. While $v.used > 0$ \\
+\hspace{3mm}8.1 If $\vert u \vert > \vert v \vert$ then \\
+\hspace{6mm}8.1.1 Swap $u$ and $v$. \\
+\hspace{3mm}8.2 $v \leftarrow \vert v \vert - \vert u \vert$ \\
+\hspace{3mm}8.3 While $v.used > 0$ and $v_0 \equiv 0 \mbox{ (mod }2\mbox{)}$ \\
+\hspace{6mm}8.3.1 $v \leftarrow \lfloor v / 2 \rfloor$ \\
+9. $c \leftarrow u \cdot 2^k$ \\
+10. Return(\textit{MP\_OKAY}). \\
\hline
\end{tabular}
\end{center}
@@ -9826,17 +6080,17 @@ This algorithm will produce the greatest common divisor of two mp\_ints $a$ and
Knuth \cite[pp. 338]{TAOCPV2} but has been modified to be simpler to explain. In theory it achieves the same asymptotic working time as
Algorithm B and in practice this appears to be true.
-The first three steps handle the cases where either one of or both inputs are zero. If either input is zero the greatest common divisor is the
+The first two steps handle the cases where either one of or both inputs are zero. If either input is zero the greatest common divisor is the
largest input or zero if they are both zero. If the inputs are not trivial than $u$ and $v$ are assigned the absolute values of
$a$ and $b$ respectively and the algorithm will proceed to reduce the pair.
-Step six will divide out any common factors of two and keep track of the count in the variable $k$. After this step two is no longer a
+Step five will divide out any common factors of two and keep track of the count in the variable $k$. After this step, two is no longer a
factor of the remaining greatest common divisor between $u$ and $v$ and can be safely evenly divided out of either whenever they are even. Step
-seven and eight ensure that the $u$ and $v$ respectively have no more factors of two. At most only one of the while loops will iterate since
+six and seven ensure that the $u$ and $v$ respectively have no more factors of two. At most only one of the while--loops will iterate since
they cannot both be even.
-By step nine both of $u$ and $v$ are odd which is required for the inner logic. First the pair are swapped such that $v$ is equal to
-or greater than $u$. This ensures that the subtraction on step 9.2 will always produce a positive and even result. Step 9.3 removes any
+By step eight both of $u$ and $v$ are odd which is required for the inner logic. First the pair are swapped such that $v$ is equal to
+or greater than $u$. This ensures that the subtraction on step 8.2 will always produce a positive and even result. Step 8.3 removes any
factors of two from the difference $u$ to ensure that in the next iteration of the loop both are once again odd.
After $v = 0$ occurs the variable $u$ has the greatest common divisor of the pair $\left < u, v \right >$ just after step six. The result
@@ -9846,117 +6100,23 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear
\hspace{-5.1mm}{\bf File}: bn\_mp\_gcd.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* Greatest Common Divisor using the binary method */
-018 int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
-019 \{
-020 mp_int u, v;
-021 int k, u_lsb, v_lsb, res;
-022
-023 /* either zero than gcd is the largest */
-024 if (mp_iszero (a) == 1 && mp_iszero (b) == 0) \{
-025 return mp_abs (b, c);
-026 \}
-027 if (mp_iszero (a) == 0 && mp_iszero (b) == 1) \{
-028 return mp_abs (a, c);
-029 \}
-030
-031 /* optimized. At this point if a == 0 then
-032 * b must equal zero too
-033 */
-034 if (mp_iszero (a) == 1) \{
-035 mp_zero(c);
-036 return MP_OKAY;
-037 \}
-038
-039 /* get copies of a and b we can modify */
-040 if ((res = mp_init_copy (&u, a)) != MP_OKAY) \{
-041 return res;
-042 \}
-043
-044 if ((res = mp_init_copy (&v, b)) != MP_OKAY) \{
-045 goto LBL_U;
-046 \}
-047
-048 /* must be positive for the remainder of the algorithm */
-049 u.sign = v.sign = MP_ZPOS;
-050
-051 /* B1. Find the common power of two for u and v */
-052 u_lsb = mp_cnt_lsb(&u);
-053 v_lsb = mp_cnt_lsb(&v);
-054 k = MIN(u_lsb, v_lsb);
-055
-056 if (k > 0) \{
-057 /* divide the power of two out */
-058 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) \{
-059 goto LBL_V;
-060 \}
-061
-062 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) \{
-063 goto LBL_V;
-064 \}
-065 \}
-066
-067 /* divide any remaining factors of two out */
-068 if (u_lsb != k) \{
-069 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) \{
-070 goto LBL_V;
-071 \}
-072 \}
-073
-074 if (v_lsb != k) \{
-075 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) \{
-076 goto LBL_V;
-077 \}
-078 \}
-079
-080 while (mp_iszero(&v) == 0) \{
-081 /* make sure v is the largest */
-082 if (mp_cmp_mag(&u, &v) == MP_GT) \{
-083 /* swap u and v to make sure v is >= u */
-084 mp_exch(&u, &v);
-085 \}
-086
-087 /* subtract smallest from largest */
-088 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) \{
-089 goto LBL_V;
-090 \}
-091
-092 /* Divide out all factors of two */
-093 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) \{
-094 goto LBL_V;
-095 \}
-096 \}
-097
-098 /* multiply by 2**k which we divided out at the beginning */
-099 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) \{
-100 goto LBL_V;
-101 \}
-102 c->sign = MP_ZPOS;
-103 res = MP_OKAY;
-104 LBL_V:mp_clear (&u);
-105 LBL_U:mp_clear (&v);
-106 return res;
-107 \}
-108 #endif
-109
\end{alltt}
\end{small}
This function makes use of the macros mp\_iszero and mp\_iseven. The former evaluates to $1$ if the input mp\_int is equivalent to the
integer zero otherwise it evaluates to $0$. The latter evaluates to $1$ if the input mp\_int represents a non-zero even integer otherwise
it evaluates to $0$. Note that just because mp\_iseven may evaluate to $0$ does not mean the input is odd, it could also be zero. The three
-trivial cases of inputs are handled on lines 24 through 37. After those lines the inputs are assumed to be non-zero.
+trivial cases of inputs are handled on lines 24 through 30. After those lines the inputs are assumed to be non-zero.
-Lines 34 and 40 make local copies $u$ and $v$ of the inputs $a$ and $b$ respectively. At this point the common factors of two
-must be divided out of the two inputs. The while loop on line 80 iterates so long as both are even. The local integer $k$ is used to
-keep track of how many factors of $2$ are pulled out of both values. It is assumed that the number of factors will not exceed the maximum
-value of a C ``int'' data type\footnote{Strictly speaking no array in C may have more than entries than are accessible by an ``int'' so this is not
-a limitation.}.
+Lines 32 and 37 make local copies $u$ and $v$ of the inputs $a$ and $b$ respectively. At this point the common factors of two
+must be divided out of the two inputs. The block starting at line 44 removes common factors of two by first counting the number of trailing
+zero bits in both. The local integer $k$ is used to keep track of how many factors of $2$ are pulled out of both values. It is assumed that
+the number of factors will not exceed the maximum value of a C ``int'' data type\footnote{Strictly speaking no array in C may have more than
+entries than are accessible by an ``int'' so this is not a limitation.}.
-At this point there are no more common factors of two in the two values. The while loops on lines 80 and 80 remove any independent
-factors of two such that both $u$ and $v$ are guaranteed to be an odd integer before hitting the main body of the algorithm. The while loop
-on line 80 performs the reduction of the pair until $v$ is equal to zero. The unsigned comparison and subtraction algorithms are used in
+At this point there are no more common factors of two in the two values. The divisions by a power of two on lines 62 and 68 remove
+any independent factors of two such that both $u$ and $v$ are guaranteed to be an odd integer before hitting the main body of the algorithm. The while loop
+on line 73 performs the reduction of the pair until $v$ is equal to zero. The unsigned comparison and subtraction algorithms are used in
place of the full signed routines since both values are guaranteed to be positive and the result of the subtraction is guaranteed to be non-negative.
\section{Least Common Multiple}
@@ -9995,47 +6155,6 @@ dividing the product of the two inputs by their greatest common divisor.
\hspace{-5.1mm}{\bf File}: bn\_mp\_lcm.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* computes least common multiple as |a*b|/(a, b) */
-018 int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
-019 \{
-020 int res;
-021 mp_int t1, t2;
-022
-023
-024 if ((res = mp_init_multi (&t1, &t2, NULL)) != MP_OKAY) \{
-025 return res;
-026 \}
-027
-028 /* t1 = get the GCD of the two inputs */
-029 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) \{
-030 goto LBL_T;
-031 \}
-032
-033 /* divide the smallest by the GCD */
-034 if (mp_cmp_mag(a, b) == MP_LT) \{
-035 /* store quotient in t2 such that t2 * b is the LCM */
-036 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) \{
-037 goto LBL_T;
-038 \}
-039 res = mp_mul(b, &t2, c);
-040 \} else \{
-041 /* store quotient in t2 such that t2 * a is the LCM */
-042 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) \{
-043 goto LBL_T;
-044 \}
-045 res = mp_mul(a, &t2, c);
-046 \}
-047
-048 /* fix the sign to positive */
-049 c->sign = MP_ZPOS;
-050
-051 LBL_T:
-052 mp_clear_multi (&t1, &t2, NULL);
-053 return res;
-054 \}
-055 #endif
-056
\end{alltt}
\end{small}
@@ -10195,92 +6314,6 @@ $\left ( {p' \over a'} \right )$ which is multiplied against the current Jacobi
\hspace{-5.1mm}{\bf File}: bn\_mp\_jacobi.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* computes the jacobi c = (a | n) (or Legendre if n is prime)
-018 * HAC pp. 73 Algorithm 2.149
-019 */
-020 int mp_jacobi (mp_int * a, mp_int * p, int *c)
-021 \{
-022 mp_int a1, p1;
-023 int k, s, r, res;
-024 mp_digit residue;
-025
-026 /* if p <= 0 return MP_VAL */
-027 if (mp_cmp_d(p, 0) != MP_GT) \{
-028 return MP_VAL;
-029 \}
-030
-031 /* step 1. if a == 0, return 0 */
-032 if (mp_iszero (a) == 1) \{
-033 *c = 0;
-034 return MP_OKAY;
-035 \}
-036
-037 /* step 2. if a == 1, return 1 */
-038 if (mp_cmp_d (a, 1) == MP_EQ) \{
-039 *c = 1;
-040 return MP_OKAY;
-041 \}
-042
-043 /* default */
-044 s = 0;
-045
-046 /* step 3. write a = a1 * 2**k */
-047 if ((res = mp_init_copy (&a1, a)) != MP_OKAY) \{
-048 return res;
-049 \}
-050
-051 if ((res = mp_init (&p1)) != MP_OKAY) \{
-052 goto LBL_A1;
-053 \}
-054
-055 /* divide out larger power of two */
-056 k = mp_cnt_lsb(&a1);
-057 if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) \{
-058 goto LBL_P1;
-059 \}
-060
-061 /* step 4. if e is even set s=1 */
-062 if ((k & 1) == 0) \{
-063 s = 1;
-064 \} else \{
-065 /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
-066 residue = p->dp[0] & 7;
-067
-068 if (residue == 1 || residue == 7) \{
-069 s = 1;
-070 \} else if (residue == 3 || residue == 5) \{
-071 s = -1;
-072 \}
-073 \}
-074
-075 /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
-076 if ( ((p->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) \{
-077 s = -s;
-078 \}
-079
-080 /* if a1 == 1 we're done */
-081 if (mp_cmp_d (&a1, 1) == MP_EQ) \{
-082 *c = s;
-083 \} else \{
-084 /* n1 = n mod a1 */
-085 if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) \{
-086 goto LBL_P1;
-087 \}
-088 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) \{
-089 goto LBL_P1;
-090 \}
-091 *c = s * r;
-092 \}
-093
-094 /* done */
-095 res = MP_OKAY;
-096 LBL_P1:mp_clear (&p1);
-097 LBL_A1:mp_clear (&a1);
-098 return res;
-099 \}
-100 #endif
-101
\end{alltt}
\end{small}
@@ -10295,9 +6328,9 @@ After a local copy of $a$ is made all of the factors of two are divided out and
bit of $k$ is required, however, it makes the algorithm simpler to follow to perform an addition. In practice an exclusive-or and addition have the same
processor requirements and neither is faster than the other.
-Line 61 through 70 determines the value of $\left ( { 2 \over p } \right )^k$. If the least significant bit of $k$ is zero than
+Line 58 through 71 determines the value of $\left ( { 2 \over p } \right )^k$. If the least significant bit of $k$ is zero than
$k$ is even and the value is one. Otherwise, the value of $s$ depends on which residue class $p$ belongs to modulo eight. The value of
-$(-1)^{(p-1)(a'-1)/4}$ is compute and multiplied against $s$ on lines 75 through 73.
+$(-1)^{(p-1)(a'-1)/4}$ is compute and multiplied against $s$ on lines 71 through 74.
Finally, if $a1$ does not equal one the algorithm must recurse and compute $\left ( {p' \over a'} \right )$.
@@ -10406,30 +6439,6 @@ then only a couple of additions or subtractions will be required to adjust the i
\hspace{-5.1mm}{\bf File}: bn\_mp\_invmod.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* hac 14.61, pp608 */
-018 int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
-019 \{
-020 /* b cannot be negative */
-021 if (b->sign == MP_NEG || mp_iszero(b) == 1) \{
-022 return MP_VAL;
-023 \}
-024
-025 #ifdef BN_FAST_MP_INVMOD_C
-026 /* if the modulus is odd we can use a faster routine instead */
-027 if (mp_isodd (b) == 1) \{
-028 return fast_mp_invmod (a, b, c);
-029 \}
-030 #endif
-031
-032 #ifdef BN_MP_INVMOD_SLOW_C
-033 return mp_invmod_slow(a, b, c);
-034 #endif
-035
-036 return MP_VAL;
-037 \}
-038 #endif
-039
\end{alltt}
\end{small}
@@ -10501,37 +6510,6 @@ This algorithm attempts to determine if a candidate integer $n$ is composite by
\hspace{-5.1mm}{\bf File}: bn\_mp\_prime\_is\_divisible.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* determines if an integers is divisible by one
-018 * of the first PRIME_SIZE primes or not
-019 *
-020 * sets result to 0 if not, 1 if yes
-021 */
-022 int mp_prime_is_divisible (mp_int * a, int *result)
-023 \{
-024 int err, ix;
-025 mp_digit res;
-026
-027 /* default to not */
-028 *result = MP_NO;
-029
-030 for (ix = 0; ix < PRIME_SIZE; ix++) \{
-031 /* what is a mod LBL_prime_tab[ix] */
-032 if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) \{
-033 return err;
-034 \}
-035
-036 /* is the residue zero? */
-037 if (res == 0) \{
-038 *result = MP_YES;
-039 return MP_OKAY;
-040 \}
-041 \}
-042
-043 return MP_OKAY;
-044 \}
-045 #endif
-046
\end{alltt}
\end{small}
@@ -10542,48 +6520,6 @@ mp\_digit. The table \_\_prime\_tab is defined in the following file.
\hspace{-5.1mm}{\bf File}: bn\_prime\_tab.c
\vspace{-3mm}
\begin{alltt}
-016 const mp_digit ltm_prime_tab[] = \{
-017 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
-018 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
-019 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
-020 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F,
-021 #ifndef MP_8BIT
-022 0x0083,
-023 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
-024 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
-025 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
-026 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137,
-027
-028 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167,
-029 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199,
-030 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9,
-031 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7,
-032 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239,
-033 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265,
-034 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293,
-035 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF,
-036
-037 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301,
-038 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B,
-039 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371,
-040 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD,
-041 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5,
-042 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419,
-043 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449,
-044 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B,
-045
-046 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7,
-047 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503,
-048 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529,
-049 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F,
-050 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
-051 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
-052 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
-053 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
-054 #endif
-055 \};
-056 #endif
-057
\end{alltt}
\end{small}
@@ -10630,49 +6566,6 @@ determine the result.
\hspace{-5.1mm}{\bf File}: bn\_mp\_prime\_fermat.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* performs one Fermat test.
-018 *
-019 * If "a" were prime then b**a == b (mod a) since the order of
-020 * the multiplicative sub-group would be phi(a) = a-1. That means
-021 * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a).
-022 *
-023 * Sets result to 1 if the congruence holds, or zero otherwise.
-024 */
-025 int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
-026 \{
-027 mp_int t;
-028 int err;
-029
-030 /* default to composite */
-031 *result = MP_NO;
-032
-033 /* ensure b > 1 */
-034 if (mp_cmp_d(b, 1) != MP_GT) \{
-035 return MP_VAL;
-036 \}
-037
-038 /* init t */
-039 if ((err = mp_init (&t)) != MP_OKAY) \{
-040 return err;
-041 \}
-042
-043 /* compute t = b**a mod a */
-044 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) \{
-045 goto LBL_T;
-046 \}
-047
-048 /* is it equal to b? */
-049 if (mp_cmp (&t, b) == MP_EQ) \{
-050 *result = MP_YES;
-051 \}
-052
-053 err = MP_OKAY;
-054 LBL_T:mp_clear (&t);
-055 return err;
-056 \}
-057 #endif
-058
\end{alltt}
\end{small}
@@ -10725,90 +6618,6 @@ composite then it is \textit{probably} prime.
\hspace{-5.1mm}{\bf File}: bn\_mp\_prime\_miller\_rabin.c
\vspace{-3mm}
\begin{alltt}
-016
-017 /* Miller-Rabin test of "a" to the base of "b" as described in
-018 * HAC pp. 139 Algorithm 4.24
-019 *
-020 * Sets result to 0 if definitely composite or 1 if probably prime.
-021 * Randomly the chance of error is no more than 1/4 and often
-022 * very much lower.
-023 */
-024 int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
-025 \{
-026 mp_int n1, y, r;
-027 int s, j, err;
-028
-029 /* default */
-030 *result = MP_NO;
-031
-032 /* ensure b > 1 */
-033 if (mp_cmp_d(b, 1) != MP_GT) \{
-034 return MP_VAL;
-035 \}
-036
-037 /* get n1 = a - 1 */
-038 if ((err = mp_init_copy (&n1, a)) != MP_OKAY) \{
-039 return err;
-040 \}
-041 if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) \{
-042 goto LBL_N1;
-043 \}
-044
-045 /* set 2**s * r = n1 */
-046 if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) \{
-047 goto LBL_N1;
-048 \}
-049
-050 /* count the number of least significant bits
-051 * which are zero
-052 */
-053 s = mp_cnt_lsb(&r);
-054
-055 /* now divide n - 1 by 2**s */
-056 if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) \{
-057 goto LBL_R;
-058 \}
-059
-060 /* compute y = b**r mod a */
-061 if ((err = mp_init (&y)) != MP_OKAY) \{
-062 goto LBL_R;
-063 \}
-064 if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) \{
-065 goto LBL_Y;
-066 \}
-067
-068 /* if y != 1 and y != n1 do */
-069 if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) \{
-070 j = 1;
-071 /* while j <= s-1 and y != n1 */
-072 while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) \{
-073 if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) \{
-074 goto LBL_Y;
-075 \}
-076
-077 /* if y == 1 then composite */
-078 if (mp_cmp_d (&y, 1) == MP_EQ) \{
-079 goto LBL_Y;
-080 \}
-081
-082 ++j;
-083 \}
-084
-085 /* if y != n1 then composite */
-086 if (mp_cmp (&y, &n1) != MP_EQ) \{
-087 goto LBL_Y;
-088 \}
-089 \}
-090
-091 /* probably prime now */
-092 *result = MP_YES;
-093 LBL_Y:mp_clear (&y);
-094 LBL_R:mp_clear (&r);
-095 LBL_N1:mp_clear (&n1);
-096 return err;
-097 \}
-098 #endif
-099
\end{alltt}
\end{small}