Merge pull request #434 from libtom/simplifications Simplifications
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
diff --git a/.travis.yml b/.travis.yml
index 6c78689..a269c4e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -144,6 +144,7 @@ matrix:
# clang for x86-64 architecture (64-bit longs and 64-bit pointers)
- env: SANITIZER=1 CONV_WARNINGS=relaxed BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-travis-valgrind'
- env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-travis-valgrind'
+ - env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_USE_MEMOPS --with-m64 --with-travis-valgrind'
- env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --c89 --with-m64 --with-travis-valgrind'
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-travis-valgrind --cflags=-DMP_PREC=MP_MIN_PREC'
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-travis-valgrind'
diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 1beab5f..648a654 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -37,8 +37,8 @@ static int mtest_opponent(void)
#ifndef MP_FIXED_CUTOFFS
/* force KARA and TOOM to enable despite cutoffs */
- MP_KARATSUBA_SQR_CUTOFF = MP_KARATSUBA_MUL_CUTOFF = 8;
- MP_TOOM_SQR_CUTOFF = MP_TOOM_MUL_CUTOFF = 16;
+ MP_SQR_KARATSUBA_CUTOFF = MP_MUL_KARATSUBA_CUTOFF = 8;
+ MP_SQR_TOOM_CUTOFF = MP_MUL_TOOM_CUTOFF = 16;
#endif
for (;;) {
diff --git a/demo/test.c b/demo/test.c
index 8e37f71..3b97873 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1866,7 +1866,7 @@ LBL_ERR:
return EXIT_FAILURE;
}
-static int test_s_mp_balance_mul(void)
+static int test_s_mp_mul_balance(void)
{
mp_int a, b, c;
@@ -1881,7 +1881,7 @@ static int test_s_mp_balance_mul(void)
DO(mp_read_radix(&a, na, 64));
DO(mp_read_radix(&b, nb, 64));
- DO(s_mp_balance_mul(&a, &b, &c));
+ DO(s_mp_mul_balance(&a, &b, &c));
DO(mp_read_radix(&b, nc, 64));
@@ -1896,18 +1896,18 @@ LBL_ERR:
return EXIT_FAILURE;
}
-#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
-static int test_s_mp_karatsuba_mul(void)
+#define s_mp_mul_full(a, b, c) s_mp_mul(a, b, c, (a)->used + (b)->used + 1)
+static int test_s_mp_mul_karatsuba(void)
{
mp_int a, b, c, d;
int size;
DOR(mp_init_multi(&a, &b, &c, &d, NULL));
- for (size = MP_KARATSUBA_MUL_CUTOFF; size < MP_KARATSUBA_MUL_CUTOFF + 20; size++) {
+ for (size = MP_MUL_KARATSUBA_CUTOFF; size < MP_MUL_KARATSUBA_CUTOFF + 20; size++) {
DO(mp_rand(&a, size));
DO(mp_rand(&b, size));
- DO(s_mp_karatsuba_mul(&a, &b, &c));
- DO(s_mp_mul(&a,&b,&d));
+ DO(s_mp_mul_karatsuba(&a, &b, &c));
+ DO(s_mp_mul_full(&a,&b,&d));
if (mp_cmp(&c, &d) != MP_EQ) {
fprintf(stderr, "Karatsuba multiplication failed at size %d\n", size);
goto LBL_ERR;
@@ -1921,15 +1921,15 @@ LBL_ERR:
return EXIT_FAILURE;
}
-static int test_s_mp_karatsuba_sqr(void)
+static int test_s_mp_sqr_karatsuba(void)
{
mp_int a, b, c;
int size;
DOR(mp_init_multi(&a, &b, &c, NULL));
- for (size = MP_KARATSUBA_SQR_CUTOFF; size < MP_KARATSUBA_SQR_CUTOFF + 20; size++) {
+ for (size = MP_SQR_KARATSUBA_CUTOFF; size < MP_SQR_KARATSUBA_CUTOFF + 20; size++) {
DO(mp_rand(&a, size));
- DO(s_mp_karatsuba_sqr(&a, &b));
+ DO(s_mp_sqr_karatsuba(&a, &b));
DO(s_mp_sqr(&a, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
fprintf(stderr, "Karatsuba squaring failed at size %d\n", size);
@@ -1944,7 +1944,7 @@ LBL_ERR:
return EXIT_FAILURE;
}
-static int test_s_mp_toom_mul(void)
+static int test_s_mp_mul_toom(void)
{
mp_int a, b, c, d;
int size;
@@ -1965,10 +1965,10 @@ static int test_s_mp_toom_mul(void)
DO(mp_2expt(&c, 99000 - 1000));
DO(mp_add(&b, &c, &b));
- tc_cutoff = MP_TOOM_MUL_CUTOFF;
- MP_TOOM_MUL_CUTOFF = INT_MAX;
+ tc_cutoff = MP_MUL_TOOM_CUTOFF;
+ MP_MUL_TOOM_CUTOFF = INT_MAX;
DO(mp_mul(&a, &b, &c));
- MP_TOOM_MUL_CUTOFF = tc_cutoff;
+ MP_MUL_TOOM_CUTOFF = tc_cutoff;
DO(mp_mul(&a, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
fprintf(stderr, "Toom-Cook 3-way multiplication failed for edgecase f1 * f2\n");
@@ -1976,11 +1976,11 @@ static int test_s_mp_toom_mul(void)
}
#endif
- for (size = MP_TOOM_MUL_CUTOFF; size < MP_TOOM_MUL_CUTOFF + 20; size++) {
+ for (size = MP_MUL_TOOM_CUTOFF; size < MP_MUL_TOOM_CUTOFF + 20; size++) {
DO(mp_rand(&a, size));
DO(mp_rand(&b, size));
- DO(s_mp_toom_mul(&a, &b, &c));
- DO(s_mp_mul(&a,&b,&d));
+ DO(s_mp_mul_toom(&a, &b, &c));
+ DO(s_mp_mul_full(&a,&b,&d));
if (mp_cmp(&c, &d) != MP_EQ) {
fprintf(stderr, "Toom-Cook 3-way multiplication failed at size %d\n", size);
goto LBL_ERR;
@@ -1994,15 +1994,15 @@ LBL_ERR:
return EXIT_FAILURE;
}
-static int test_s_mp_toom_sqr(void)
+static int test_s_mp_sqr_toom(void)
{
mp_int a, b, c;
int size;
DOR(mp_init_multi(&a, &b, &c, NULL));
- for (size = MP_TOOM_SQR_CUTOFF; size < MP_TOOM_SQR_CUTOFF + 20; size++) {
+ for (size = MP_SQR_TOOM_CUTOFF; size < MP_SQR_TOOM_CUTOFF + 20; size++) {
DO(mp_rand(&a, size));
- DO(s_mp_toom_sqr(&a, &b));
+ DO(s_mp_sqr_toom(&a, &b));
DO(s_mp_sqr(&a, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
fprintf(stderr, "Toom-Cook 3-way squaring failed at size %d\n", size);
@@ -2075,7 +2075,7 @@ static int test_s_mp_div_recursive(void)
DOR(mp_init_multi(&a, &b, &c_q, &c_r, &d_q, &d_r, NULL));
- for (size = MP_KARATSUBA_MUL_CUTOFF; size < 3 * MP_KARATSUBA_MUL_CUTOFF; size += 10) {
+ for (size = MP_MUL_KARATSUBA_CUTOFF; size < 3 * MP_MUL_KARATSUBA_CUTOFF; size += 10) {
printf("\rsizes = %d / %d", 10 * size, size);
/* Relation 10:1 */
DO(mp_rand(&a, 10 * size));
@@ -2139,7 +2139,7 @@ static int test_s_mp_div_small(void)
int size;
DOR(mp_init_multi(&a, &b, &c_q, &c_r, &d_q, &d_r, NULL));
- for (size = 1; size < MP_KARATSUBA_MUL_CUTOFF; size += 10) {
+ for (size = 1; size < MP_MUL_KARATSUBA_CUTOFF; size += 10) {
printf("\rsizes = %d / %d", 2 * size, size);
/* Relation 10:1 */
DO(mp_rand(&a, 2 * size));
@@ -2332,11 +2332,11 @@ static int unit_tests(int argc, char **argv)
T1(mp_xor, MP_XOR),
T2(s_mp_div_recursive, S_MP_DIV_RECURSIVE, S_MP_DIV_SCHOOL),
T2(s_mp_div_small, S_MP_DIV_SMALL, S_MP_DIV_SCHOOL),
- T1(s_mp_balance_mul, S_MP_BALANCE_MUL),
- T1(s_mp_karatsuba_mul, S_MP_KARATSUBA_MUL),
- T1(s_mp_karatsuba_sqr, S_MP_KARATSUBA_SQR),
- T1(s_mp_toom_mul, S_MP_TOOM_MUL),
- T1(s_mp_toom_sqr, S_MP_TOOM_SQR)
+ T1(s_mp_mul_balance, S_MP_MUL_BALANCE),
+ T1(s_mp_mul_karatsuba, S_MP_MUL_KARATSUBA),
+ T1(s_mp_sqr_karatsuba, S_MP_SQR_KARATSUBA),
+ T1(s_mp_mul_toom, S_MP_MUL_TOOM),
+ T1(s_mp_sqr_toom, S_MP_SQR_TOOM)
#undef T2
#undef T1
};
diff --git a/demo/timing.c b/demo/timing.c
index bb2d6c4..4385a08 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -247,18 +247,18 @@ int main(int argc, char **argv)
if (should_test("mulsqr", argc, argv) != 0) {
/* do mult/square twice, first without karatsuba and second with */
- old_kara_m = MP_KARATSUBA_MUL_CUTOFF;
- old_kara_s = MP_KARATSUBA_SQR_CUTOFF;
+ old_kara_m = MP_MUL_KARATSUBA_CUTOFF;
+ old_kara_s = MP_SQR_KARATSUBA_CUTOFF;
/* currently toom-cook cut-off is too high to kick in, so we just use the karatsuba values */
old_toom_m = old_kara_m;
old_toom_s = old_kara_s;
for (ix = 0; ix < 3; ix++) {
printf("With%s Karatsuba, With%s Toom\n", (ix == 1) ? "" : "out", (ix == 2) ? "" : "out");
- MP_KARATSUBA_MUL_CUTOFF = (ix == 1) ? old_kara_m : 9999;
- MP_KARATSUBA_SQR_CUTOFF = (ix == 1) ? old_kara_s : 9999;
- MP_TOOM_MUL_CUTOFF = (ix == 2) ? old_toom_m : 9999;
- MP_TOOM_SQR_CUTOFF = (ix == 2) ? old_toom_s : 9999;
+ MP_MUL_KARATSUBA_CUTOFF = (ix == 1) ? old_kara_m : 9999;
+ MP_SQR_KARATSUBA_CUTOFF = (ix == 1) ? old_kara_s : 9999;
+ MP_MUL_TOOM_CUTOFF = (ix == 2) ? old_toom_m : 9999;
+ MP_SQR_TOOM_CUTOFF = (ix == 2) ? old_toom_s : 9999;
log = FOPEN((ix == 0) ? "logs/mult" MP_TIMING_VERSION ".log" : (ix == 1) ? "logs/mult_kara" MP_TIMING_VERSION ".log" :
"logs/mult_toom" MP_TIMING_VERSION ".log", "w");
diff --git a/etc/tune.c b/etc/tune.c
index be78ce3..9657910 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -58,7 +58,7 @@ static int s_number_of_test_loops;
static int s_stabilization_extra;
static int s_offset = 1;
-#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
+#define s_mp_mul_full(a, b, c) s_mp_mul(a, b, c, (a)->used + (b)->used + 1)
static uint64_t s_time_mul(int size)
{
int x;
@@ -87,7 +87,7 @@ static uint64_t s_time_mul(int size)
goto LBL_ERR;
}
if (s_check_result == 1) {
- if ((e = s_mp_mul(&a,&b,&d)) != MP_OKAY) {
+ if ((e = s_mp_mul_full(&a,&b,&d)) != MP_OKAY) {
t1 = UINT64_MAX;
goto LBL_ERR;
}
@@ -247,8 +247,8 @@ static void s_usage(char *s)
}
struct cutoffs {
- int KARATSUBA_MUL, KARATSUBA_SQR;
- int TOOM_MUL, TOOM_SQR;
+ int MUL_KARATSUBA, SQR_KARATSUBA;
+ int MUL_TOOM, SQR_TOOM;
};
const struct cutoffs max_cutoffs =
@@ -256,18 +256,18 @@ const struct cutoffs max_cutoffs =
static void set_cutoffs(const struct cutoffs *c)
{
- MP_KARATSUBA_MUL_CUTOFF = c->KARATSUBA_MUL;
- MP_KARATSUBA_SQR_CUTOFF = c->KARATSUBA_SQR;
- MP_TOOM_MUL_CUTOFF = c->TOOM_MUL;
- MP_TOOM_SQR_CUTOFF = c->TOOM_SQR;
+ MP_MUL_KARATSUBA_CUTOFF = c->MUL_KARATSUBA;
+ MP_SQR_KARATSUBA_CUTOFF = c->SQR_KARATSUBA;
+ MP_MUL_TOOM_CUTOFF = c->MUL_TOOM;
+ MP_SQR_TOOM_CUTOFF = c->SQR_TOOM;
}
static void get_cutoffs(struct cutoffs *c)
{
- c->KARATSUBA_MUL = MP_KARATSUBA_MUL_CUTOFF;
- c->KARATSUBA_SQR = MP_KARATSUBA_SQR_CUTOFF;
- c->TOOM_MUL = MP_TOOM_MUL_CUTOFF;
- c->TOOM_SQR = MP_TOOM_SQR_CUTOFF;
+ c->MUL_KARATSUBA = MP_MUL_KARATSUBA_CUTOFF;
+ c->SQR_KARATSUBA = MP_SQR_KARATSUBA_CUTOFF;
+ c->MUL_TOOM = MP_MUL_TOOM_CUTOFF;
+ c->SQR_TOOM = MP_SQR_TOOM_CUTOFF;
}
@@ -292,7 +292,7 @@ int main(int argc, char **argv)
s_number_of_test_loops = 64;
s_stabilization_extra = 3;
- MP_ZERO_BUFFER(&args, sizeof(args));
+ s_mp_zero_buf(&args, sizeof(args));
args.testmode = 0;
args.verbose = 0;
@@ -414,13 +414,13 @@ int main(int argc, char **argv)
s_usage(argv[0]);
}
str = argv[opt];
- MP_KARATSUBA_MUL_CUTOFF = (int)s_strtol(str, &endptr, "[1/4] No value for MP_KARATSUBA_MUL_CUTOFF given");
+ MP_MUL_KARATSUBA_CUTOFF = (int)s_strtol(str, &endptr, "[1/4] No value for MP_MUL_KARATSUBA_CUTOFF given");
str = endptr + 1;
- MP_KARATSUBA_SQR_CUTOFF = (int)s_strtol(str, &endptr, "[2/4] No value for MP_KARATSUBA_SQR_CUTOFF given");
+ MP_SQR_KARATSUBA_CUTOFF = (int)s_strtol(str, &endptr, "[2/4] No value for MP_SQR_KARATSUBA_CUTOFF given");
str = endptr + 1;
- MP_TOOM_MUL_CUTOFF = (int)s_strtol(str, &endptr, "[3/4] No value for MP_TOOM_MUL_CUTOFF given");
+ MP_MUL_TOOM_CUTOFF = (int)s_strtol(str, &endptr, "[3/4] No value for MP_MUL_TOOM_CUTOFF given");
str = endptr + 1;
- MP_TOOM_SQR_CUTOFF = (int)s_strtol(str, &endptr, "[4/4] No value for MP_TOOM_SQR_CUTOFF given");
+ MP_SQR_TOOM_CUTOFF = (int)s_strtol(str, &endptr, "[4/4] No value for MP_SQR_TOOM_CUTOFF given");
break;
case 'h':
s_exit_code = EXIT_SUCCESS;
@@ -455,10 +455,10 @@ int main(int argc, char **argv)
of the macro MP_WPARRAY in tommath.h which needs to
be changed manually (to 0 (zero)).
*/
- T_MUL_SQR("Karatsuba multiplication", KARATSUBA_MUL, s_time_mul),
- T_MUL_SQR("Karatsuba squaring", KARATSUBA_SQR, s_time_sqr),
- T_MUL_SQR("Toom-Cook 3-way multiplying", TOOM_MUL, s_time_mul),
- T_MUL_SQR("Toom-Cook 3-way squaring", TOOM_SQR, s_time_sqr),
+ T_MUL_SQR("Karatsuba multiplication", MUL_KARATSUBA, s_time_mul),
+ T_MUL_SQR("Karatsuba squaring", SQR_KARATSUBA, s_time_sqr),
+ T_MUL_SQR("Toom-Cook 3-way multiplying", MUL_TOOM, s_time_mul),
+ T_MUL_SQR("Toom-Cook 3-way squaring", SQR_TOOM, s_time_sqr),
#undef T_MUL_SQR
};
/* Turn all limits from bncore.c to the max */
@@ -473,15 +473,15 @@ int main(int argc, char **argv)
}
if (args.terse == 1) {
printf("%d %d %d %d\n",
- updated.KARATSUBA_MUL,
- updated.KARATSUBA_SQR,
- updated.TOOM_MUL,
- updated.TOOM_SQR);
+ updated.MUL_KARATSUBA,
+ updated.SQR_KARATSUBA,
+ updated.MUL_TOOM,
+ updated.SQR_TOOM);
} else {
- printf("KARATSUBA_MUL_CUTOFF = %d\n", updated.KARATSUBA_MUL);
- printf("KARATSUBA_SQR_CUTOFF = %d\n", updated.KARATSUBA_SQR);
- printf("TOOM_MUL_CUTOFF = %d\n", updated.TOOM_MUL);
- printf("TOOM_SQR_CUTOFF = %d\n", updated.TOOM_SQR);
+ printf("MUL_KARATSUBA_CUTOFF = %d\n", updated.MUL_KARATSUBA);
+ printf("SQR_KARATSUBA_CUTOFF = %d\n", updated.SQR_KARATSUBA);
+ printf("MUL_TOOM_CUTOFF = %d\n", updated.MUL_TOOM);
+ printf("SQR_TOOM_CUTOFF = %d\n", updated.SQR_TOOM);
}
if (args.print == 1) {
@@ -526,15 +526,15 @@ int main(int argc, char **argv)
set_cutoffs(&orig);
if (args.terse == 1) {
printf("%d %d %d %d\n",
- MP_KARATSUBA_MUL_CUTOFF,
- MP_KARATSUBA_SQR_CUTOFF,
- MP_TOOM_MUL_CUTOFF,
- MP_TOOM_SQR_CUTOFF);
+ MP_MUL_KARATSUBA_CUTOFF,
+ MP_SQR_KARATSUBA_CUTOFF,
+ MP_MUL_TOOM_CUTOFF,
+ MP_SQR_TOOM_CUTOFF);
} else {
- printf("KARATSUBA_MUL_CUTOFF = %d\n", MP_KARATSUBA_MUL_CUTOFF);
- printf("KARATSUBA_SQR_CUTOFF = %d\n", MP_KARATSUBA_SQR_CUTOFF);
- printf("TOOM_MUL_CUTOFF = %d\n", MP_TOOM_MUL_CUTOFF);
- printf("TOOM_SQR_CUTOFF = %d\n", MP_TOOM_SQR_CUTOFF);
+ printf("MUL_KARATSUBA_CUTOFF = %d\n", MP_MUL_KARATSUBA_CUTOFF);
+ printf("SQR_KARATSUBA_CUTOFF = %d\n", MP_SQR_KARATSUBA_CUTOFF);
+ printf("MUL_TOOM_CUTOFF = %d\n", MP_MUL_TOOM_CUTOFF);
+ printf("SQR_TOOM_CUTOFF = %d\n", MP_SQR_TOOM_CUTOFF);
}
}
}
diff --git a/etc/tune_it.sh b/etc/tune_it.sh
index 5e0fe7c..dba5b69 100755
--- a/etc/tune_it.sh
+++ b/etc/tune_it.sh
@@ -93,15 +93,14 @@ i=$(tail -n +2 $FILE_NAME | wc -l)
# our median point will be at $i entries
i=$(( (i / 2) + 1 ))
TMP=$(median $FILE_NAME 1 $i)
-echo "#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF $TMP"
-echo "#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(km) Appending to $TOMMATH_CUTOFFS_H" $?
+echo "#define MP_DEFAULT_MUL_KARATSUBA_CUTOFF $TMP"
+echo "#define MP_DEFAULT_MUL_KARATSUBA_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(km) Appending to $TOMMATH_CUTOFFS_H" $?
TMP=$(median $FILE_NAME 2 $i)
-echo "#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF $TMP"
-echo "#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(ks) Appending to $TOMMATH_CUTOFFS_H" $?
+echo "#define MP_DEFAULT_SQR_KARATSUBA_CUTOFF $TMP"
+echo "#define MP_DEFAULT_SQR_KARATSUBA_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(ks) Appending to $TOMMATH_CUTOFFS_H" $?
TMP=$(median $FILE_NAME 3 $i)
-echo "#define MP_DEFAULT_TOOM_MUL_CUTOFF $TMP"
-echo "#define MP_DEFAULT_TOOM_MUL_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3m) Appending to $TOMMATH_CUTOFFS_H" $?
+echo "#define MP_DEFAULT_MUL_TOOM_CUTOFF $TMP"
+echo "#define MP_DEFAULT_MUL_TOOM_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3m) Appending to $TOMMATH_CUTOFFS_H" $?
TMP=$(median $FILE_NAME 4 $i)
-echo "#define MP_DEFAULT_TOOM_SQR_CUTOFF $TMP"
-echo "#define MP_DEFAULT_TOOM_SQR_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3s) Appending to $TOMMATH_CUTOFFS_H" $?
-
+echo "#define MP_DEFAULT_SQR_TOOM_CUTOFF $TMP"
+echo "#define MP_DEFAULT_SQR_TOOM_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3s) Appending to $TOMMATH_CUTOFFS_H" $?
diff --git a/helper.pl b/helper.pl
index 223c7ef..93ca40a 100755
--- a/helper.pl
+++ b/helper.pl
@@ -57,9 +57,8 @@ sub check_source {
push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bcalloc\s*\(/;
push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bfree\s*\(/;
# and we probably want to also avoid the following
- push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcpy\s*\(/;
- push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemset\s*\(/;
- push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcpy\s*\(/;
+ push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcpy\s*\(/ && $file !~ /s_mp_copy_digs.c/;
+ push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemset\s*\(/ && $file !~ /s_mp_zero_buf.c/ && $file !~ /s_mp_zero_digs.c/;
push @{$troubles->{unwanted_memmove}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemmove\s*\(/;
push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcmp\s*\(/;
push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bstrcmp\s*\(/;
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 116275e..07c784b 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -577,10 +577,6 @@
>
</File>
<File
- RelativePath="mp_mod_d.c"
- >
- </File>
- <File
RelativePath="mp_montgomery_calc_normalization.c"
>
</File>
@@ -833,7 +829,7 @@
>
</File>
<File
- RelativePath="s_mp_balance_mul.c"
+ RelativePath="s_mp_copy_digs.c"
>
</File>
<File
@@ -861,51 +857,55 @@
>
</File>
<File
- RelativePath="s_mp_invmod_fast.c"
+ RelativePath="s_mp_invmod.c"
>
</File>
<File
- RelativePath="s_mp_invmod_slow.c"
+ RelativePath="s_mp_invmod_odd.c"
>
</File>
<File
- RelativePath="s_mp_karatsuba_mul.c"
+ RelativePath="s_mp_log.c"
>
</File>
<File
- RelativePath="s_mp_karatsuba_sqr.c"
+ RelativePath="s_mp_log_d.c"
>
</File>
<File
- RelativePath="s_mp_log.c"
+ RelativePath="s_mp_log_pow2.c"
>
</File>
<File
- RelativePath="s_mp_log_d.c"
+ RelativePath="s_mp_montgomery_reduce_comba.c"
>
</File>
<File
- RelativePath="s_mp_log_pow2.c"
+ RelativePath="s_mp_mul.c"
>
</File>
<File
- RelativePath="s_mp_montgomery_reduce_fast.c"
+ RelativePath="s_mp_mul_balance.c"
>
</File>
<File
- RelativePath="s_mp_mul_digs.c"
+ RelativePath="s_mp_mul_comba.c"
>
</File>
<File
- RelativePath="s_mp_mul_digs_fast.c"
+ RelativePath="s_mp_mul_high.c"
>
</File>
<File
- RelativePath="s_mp_mul_high_digs.c"
+ RelativePath="s_mp_mul_high_comba.c"
>
</File>
<File
- RelativePath="s_mp_mul_high_digs_fast.c"
+ RelativePath="s_mp_mul_karatsuba.c"
+ >
+ </File>
+ <File
+ RelativePath="s_mp_mul_toom.c"
>
</File>
<File
@@ -925,7 +925,15 @@
>
</File>
<File
- RelativePath="s_mp_sqr_fast.c"
+ RelativePath="s_mp_sqr_comba.c"
+ >
+ </File>
+ <File
+ RelativePath="s_mp_sqr_karatsuba.c"
+ >
+ </File>
+ <File
+ RelativePath="s_mp_sqr_toom.c"
>
</File>
<File
@@ -933,11 +941,11 @@
>
</File>
<File
- RelativePath="s_mp_toom_mul.c"
+ RelativePath="s_mp_zero_buf.c"
>
</File>
<File
- RelativePath="s_mp_toom_sqr.c"
+ RelativePath="s_mp_zero_digs.c"
>
</File>
<File
diff --git a/makefile b/makefile
index cb7ba21..4045390 100644
--- a/makefile
+++ b/makefile
@@ -34,22 +34,22 @@ mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp
mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_mod_d.o mp_montgomery_calc_normalization.o \
-mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o \
-mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o \
-mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o \
-mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o \
-mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \
-mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o \
-mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o \
-mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o \
-mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o \
-mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_div_recursive.o s_mp_div_school.o \
-s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o \
-s_mp_karatsuba_mul.o s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
-s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o s_mp_mul_high_digs.o \
-s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o \
-s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
+mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
+mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
+mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o mp_rand.o \
+mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o \
+mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o \
+mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o \
+mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o \
+mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o \
+s_mp_add.o s_mp_copy_digs.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o \
+s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
+s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o \
+s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
+s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
+s_mp_zero_buf.o s_mp_zero_digs.o
#END_INS
diff --git a/makefile.mingw b/makefile.mingw
index 08b8e96..c597143 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -37,22 +37,22 @@ mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp
mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_mod_d.o mp_montgomery_calc_normalization.o \
-mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o \
-mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o \
-mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o \
-mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o \
-mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \
-mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o \
-mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o \
-mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o \
-mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o \
-mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_div_recursive.o s_mp_div_school.o \
-s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o \
-s_mp_karatsuba_mul.o s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
-s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o s_mp_mul_high_digs.o \
-s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o \
-s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
+mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
+mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
+mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o mp_rand.o \
+mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o \
+mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o \
+mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o \
+mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o \
+mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o \
+s_mp_add.o s_mp_copy_digs.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o \
+s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
+s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o \
+s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
+s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
+s_mp_zero_buf.o s_mp_zero_digs.o
HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
diff --git a/makefile.msvc b/makefile.msvc
index 058d5ce..0670b2b 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -29,22 +29,22 @@ mp_from_ubin.obj mp_fwrite.obj mp_gcd.obj mp_get_double.obj mp_get_i32.obj mp_ge
mp_get_mag_u32.obj mp_get_mag_u64.obj mp_get_mag_ul.obj mp_get_mag_ull.obj mp_grow.obj mp_init.obj mp_init_copy.obj \
mp_init_i32.obj mp_init_i64.obj mp_init_l.obj mp_init_ll.obj mp_init_multi.obj mp_init_set.obj mp_init_size.obj \
mp_init_u32.obj mp_init_u64.obj mp_init_ul.obj mp_init_ull.obj mp_invmod.obj mp_is_square.obj mp_kronecker.obj mp_lcm.obj \
-mp_log_u32.obj mp_lshd.obj mp_mod.obj mp_mod_2d.obj mp_mod_d.obj mp_montgomery_calc_normalization.obj \
-mp_montgomery_reduce.obj mp_montgomery_setup.obj mp_mul.obj mp_mul_2.obj mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj \
-mp_neg.obj mp_or.obj mp_pack.obj mp_pack_count.obj mp_prime_fermat.obj mp_prime_frobenius_underwood.obj \
-mp_prime_is_prime.obj mp_prime_miller_rabin.obj mp_prime_next_prime.obj mp_prime_rabin_miller_trials.obj \
-mp_prime_rand.obj mp_prime_strong_lucas_selfridge.obj mp_prime_tab.obj mp_radix_size.obj mp_radix_smap.obj \
-mp_rand.obj mp_read_radix.obj mp_reduce.obj mp_reduce_2k.obj mp_reduce_2k_l.obj mp_reduce_2k_setup.obj \
-mp_reduce_2k_setup_l.obj mp_reduce_is_2k.obj mp_reduce_is_2k_l.obj mp_reduce_setup.obj mp_root_u32.obj mp_rshd.obj \
-mp_sbin_size.obj mp_set.obj mp_set_double.obj mp_set_i32.obj mp_set_i64.obj mp_set_l.obj mp_set_ll.obj mp_set_u32.obj \
-mp_set_u64.obj mp_set_ul.obj mp_set_ull.obj mp_shrink.obj mp_signed_rsh.obj mp_sqr.obj mp_sqrmod.obj mp_sqrt.obj \
-mp_sqrtmod_prime.obj mp_sub.obj mp_sub_d.obj mp_submod.obj mp_to_radix.obj mp_to_sbin.obj mp_to_ubin.obj mp_ubin_size.obj \
-mp_unpack.obj mp_xor.obj mp_zero.obj s_mp_add.obj s_mp_balance_mul.obj s_mp_div_recursive.obj s_mp_div_school.obj \
-s_mp_div_small.obj s_mp_exptmod.obj s_mp_exptmod_fast.obj s_mp_get_bit.obj s_mp_invmod_fast.obj s_mp_invmod_slow.obj \
-s_mp_karatsuba_mul.obj s_mp_karatsuba_sqr.obj s_mp_log.obj s_mp_log_d.obj s_mp_log_pow2.obj \
-s_mp_montgomery_reduce_fast.obj s_mp_mul_digs.obj s_mp_mul_digs_fast.obj s_mp_mul_high_digs.obj \
-s_mp_mul_high_digs_fast.obj s_mp_prime_is_divisible.obj s_mp_rand_jenkins.obj s_mp_rand_platform.obj s_mp_sqr.obj \
-s_mp_sqr_fast.obj s_mp_sub.obj s_mp_toom_mul.obj s_mp_toom_sqr.obj
+mp_log_u32.obj mp_lshd.obj mp_mod.obj mp_mod_2d.obj mp_montgomery_calc_normalization.obj mp_montgomery_reduce.obj \
+mp_montgomery_setup.obj mp_mul.obj mp_mul_2.obj mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj mp_neg.obj mp_or.obj mp_pack.obj \
+mp_pack_count.obj mp_prime_fermat.obj mp_prime_frobenius_underwood.obj mp_prime_is_prime.obj \
+mp_prime_miller_rabin.obj mp_prime_next_prime.obj mp_prime_rabin_miller_trials.obj mp_prime_rand.obj \
+mp_prime_strong_lucas_selfridge.obj mp_prime_tab.obj mp_radix_size.obj mp_radix_smap.obj mp_rand.obj \
+mp_read_radix.obj mp_reduce.obj mp_reduce_2k.obj mp_reduce_2k_l.obj mp_reduce_2k_setup.obj mp_reduce_2k_setup_l.obj \
+mp_reduce_is_2k.obj mp_reduce_is_2k_l.obj mp_reduce_setup.obj mp_root_u32.obj mp_rshd.obj mp_sbin_size.obj mp_set.obj \
+mp_set_double.obj mp_set_i32.obj mp_set_i64.obj mp_set_l.obj mp_set_ll.obj mp_set_u32.obj mp_set_u64.obj mp_set_ul.obj \
+mp_set_ull.obj mp_shrink.obj mp_signed_rsh.obj mp_sqr.obj mp_sqrmod.obj mp_sqrt.obj mp_sqrtmod_prime.obj mp_sub.obj \
+mp_sub_d.obj mp_submod.obj mp_to_radix.obj mp_to_sbin.obj mp_to_ubin.obj mp_ubin_size.obj mp_unpack.obj mp_xor.obj mp_zero.obj \
+s_mp_add.obj s_mp_copy_digs.obj s_mp_div_recursive.obj s_mp_div_school.obj s_mp_div_small.obj s_mp_exptmod.obj \
+s_mp_exptmod_fast.obj s_mp_get_bit.obj s_mp_invmod.obj s_mp_invmod_odd.obj s_mp_log.obj s_mp_log_d.obj s_mp_log_pow2.obj \
+s_mp_montgomery_reduce_comba.obj s_mp_mul.obj s_mp_mul_balance.obj s_mp_mul_comba.obj s_mp_mul_high.obj \
+s_mp_mul_high_comba.obj s_mp_mul_karatsuba.obj s_mp_mul_toom.obj s_mp_prime_is_divisible.obj s_mp_rand_jenkins.obj \
+s_mp_rand_platform.obj s_mp_sqr.obj s_mp_sqr_comba.obj s_mp_sqr_karatsuba.obj s_mp_sqr_toom.obj s_mp_sub.obj \
+s_mp_zero_buf.obj s_mp_zero_digs.obj
HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
diff --git a/makefile.shared b/makefile.shared
index 51b0ee4..9dd34bb 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -31,22 +31,22 @@ mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp
mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_mod_d.o mp_montgomery_calc_normalization.o \
-mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o \
-mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o \
-mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o \
-mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o \
-mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \
-mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o \
-mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o \
-mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o \
-mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o \
-mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_div_recursive.o s_mp_div_school.o \
-s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o \
-s_mp_karatsuba_mul.o s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
-s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o s_mp_mul_high_digs.o \
-s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o \
-s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
+mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
+mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
+mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o mp_rand.o \
+mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o \
+mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o \
+mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o \
+mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o \
+mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o \
+s_mp_add.o s_mp_copy_digs.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o \
+s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
+s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o \
+s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
+s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
+s_mp_zero_buf.o s_mp_zero_digs.o
#END_INS
diff --git a/makefile.unix b/makefile.unix
index 5fff6b9..33a3829 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -38,22 +38,22 @@ mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp
mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_mod_d.o mp_montgomery_calc_normalization.o \
-mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o \
-mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o \
-mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o \
-mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o \
-mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \
-mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o \
-mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o \
-mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o \
-mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o \
-mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_balance_mul.o s_mp_div_recursive.o s_mp_div_school.o \
-s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o \
-s_mp_karatsuba_mul.o s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
-s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o s_mp_mul_high_digs.o \
-s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o \
-s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
+mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
+mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
+mp_prime_strong_lucas_selfridge.o mp_prime_tab.o mp_radix_size.o mp_radix_smap.o mp_rand.o \
+mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o \
+mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o \
+mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o \
+mp_set_ull.o mp_shrink.o mp_signed_rsh.o mp_sqr.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o \
+mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o \
+s_mp_add.o s_mp_copy_digs.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o \
+s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o \
+s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o \
+s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
+s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
+s_mp_zero_buf.o s_mp_zero_digs.o
HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
diff --git a/mp_abs.c b/mp_abs.c
index 902279e..a87cc0c 100644
--- a/mp_abs.c
+++ b/mp_abs.c
@@ -9,12 +9,11 @@
*/
mp_err mp_abs(const mp_int *a, mp_int *b)
{
+ mp_err err;
+
/* copy a to b */
- if (a != b) {
- mp_err err;
- if ((err = mp_copy(a, b)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_copy(a, b)) != MP_OKAY) {
+ return err;
}
/* force the sign of b to positive */
diff --git a/mp_add_d.c b/mp_add_d.c
index 4508cc8..de935bb 100644
--- a/mp_add_d.c
+++ b/mp_add_d.c
@@ -6,9 +6,8 @@
/* single digit addition */
mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
{
- mp_err err;
- int ix, oldused;
- mp_digit *tmpa, *tmpc;
+ mp_err err;
+ int oldused;
/* fast path for a == c */
if (a == c) {
@@ -26,10 +25,8 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
}
/* grow c as required */
- if (c->alloc < (a->used + 1)) {
- if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
+ return err;
}
/* if a is negative and |a| >= b, call c = |a| - b */
@@ -53,49 +50,34 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
/* old number of used digits in c */
oldused = c->used;
- /* source alias */
- tmpa = a->dp;
-
- /* destination alias */
- tmpc = c->dp;
-
/* if a is positive */
if (a->sign == MP_ZPOS) {
/* add digits, mu is carry */
+ int i;
mp_digit mu = b;
- for (ix = 0; ix < a->used; ix++) {
- *tmpc = *tmpa++ + mu;
- mu = *tmpc >> MP_DIGIT_BIT;
- *tmpc++ &= MP_MASK;
+ for (i = 0; i < a->used; i++) {
+ c->dp[i] = a->dp[i] + mu;
+ mu = c->dp[i] >> MP_DIGIT_BIT;
+ c->dp[i] &= MP_MASK;
}
/* set final carry */
- ix++;
- *tmpc++ = mu;
+ c->dp[i] = mu;
/* setup size */
c->used = a->used + 1;
} else {
/* a was negative and |a| < b */
- c->used = 1;
+ c->used = 1;
/* the result is a single digit */
- if (a->used == 1) {
- *tmpc++ = b - a->dp[0];
- } else {
- *tmpc++ = b;
- }
-
- /* setup count so the clearing of oldused
- * can fall through correctly
- */
- ix = 1;
+ c->dp[0] = (a->used == 1) ? b - a->dp[0] : b;
}
/* sign always positive */
c->sign = MP_ZPOS;
/* now zero to oldused */
- MP_ZERO_DIGITS(tmpc, oldused - ix);
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
mp_clamp(c);
return MP_OKAY;
diff --git a/mp_and.c b/mp_and.c
index 92e6aed..a865ae0 100644
--- a/mp_and.c
+++ b/mp_and.c
@@ -11,10 +11,8 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
mp_digit ac = 1, bc = 1, cc = 1;
mp_sign csign = ((a->sign == MP_NEG) && (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
- if (c->alloc < used) {
- if ((err = mp_grow(c, used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
for (i = 0; i < used; i++) {
diff --git a/mp_clear.c b/mp_clear.c
index 55d76b2..11094b2 100644
--- a/mp_clear.c
+++ b/mp_clear.c
@@ -9,7 +9,7 @@ void mp_clear(mp_int *a)
/* only do anything if a hasn't been freed previously */
if (a->dp != NULL) {
/* free ram */
- MP_FREE_DIGITS(a->dp, a->alloc);
+ MP_FREE_DIGS(a->dp, a->alloc);
/* reset members to make debugging easier */
a->dp = NULL;
diff --git a/mp_complement.c b/mp_complement.c
index ad6bed3..c16e25f 100644
--- a/mp_complement.c
+++ b/mp_complement.c
@@ -6,7 +6,8 @@
/* b = ~a */
mp_err mp_complement(const mp_int *a, mp_int *b)
{
- mp_err err = mp_neg(a, b);
- return (err == MP_OKAY) ? mp_sub_d(b, 1uL, b) : err;
+ mp_int a_ = *a;
+ a_.sign = ((a_.sign == MP_ZPOS) && !mp_iszero(a)) ? MP_NEG : MP_ZPOS;
+ return mp_sub_d(&a_, 1uL, b);
}
#endif
diff --git a/mp_copy.c b/mp_copy.c
index cf4d5e0..d79e2b8 100644
--- a/mp_copy.c
+++ b/mp_copy.c
@@ -6,7 +6,7 @@
/* copy, b = a */
mp_err mp_copy(const mp_int *a, mp_int *b)
{
- int n;
+ mp_err err;
/* if dst == src do nothing */
if (a == b) {
@@ -14,26 +14,16 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
}
/* grow dest */
- if (b->alloc < a->used) {
- mp_err err;
- if ((err = mp_grow(b, a->used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, a->used)) != MP_OKAY) {
+ return err;
}
- /* zero b and copy the parameters over */
-
- /* copy all the digits */
- for (n = 0; n < a->used; n++) {
- b->dp[n] = a->dp[n];
- }
-
- /* clear high digits */
- MP_ZERO_DIGITS(b->dp + a->used, b->used - a->used);
-
- /* copy used count and sign */
+ /* copy everything over and zero high digits */
+ s_mp_copy_digs(b->dp, a->dp, a->used);
+ s_mp_zero_digs(b->dp + a->used, b->used - a->used);
b->used = a->used;
b->sign = a->sign;
+
return MP_OKAY;
}
#endif
diff --git a/mp_cutoffs.c b/mp_cutoffs.c
index 46b04ef..45b0bee 100644
--- a/mp_cutoffs.c
+++ b/mp_cutoffs.c
@@ -5,10 +5,10 @@
#ifndef MP_FIXED_CUTOFFS
#include "tommath_cutoffs.h"
-int MP_KARATSUBA_MUL_CUTOFF = MP_DEFAULT_KARATSUBA_MUL_CUTOFF,
- MP_KARATSUBA_SQR_CUTOFF = MP_DEFAULT_KARATSUBA_SQR_CUTOFF,
- MP_TOOM_MUL_CUTOFF = MP_DEFAULT_TOOM_MUL_CUTOFF,
- MP_TOOM_SQR_CUTOFF = MP_DEFAULT_TOOM_SQR_CUTOFF;
+int MP_MUL_KARATSUBA_CUTOFF = MP_DEFAULT_MUL_KARATSUBA_CUTOFF,
+ MP_SQR_KARATSUBA_CUTOFF = MP_DEFAULT_SQR_KARATSUBA_CUTOFF,
+ MP_MUL_TOOM_CUTOFF = MP_DEFAULT_MUL_TOOM_CUTOFF,
+ MP_SQR_TOOM_CUTOFF = MP_DEFAULT_SQR_TOOM_CUTOFF;
#endif
#endif
diff --git a/mp_div.c b/mp_div.c
index 05b96dd..cbc52e8 100644
--- a/mp_div.c
+++ b/mp_div.c
@@ -26,7 +26,7 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
}
if (MP_HAS(S_MP_DIV_RECURSIVE)
- && (b->used > MP_KARATSUBA_MUL_CUTOFF)
+ && (b->used > MP_MUL_KARATSUBA_CUTOFF)
&& (b->used <= ((a->used/3)*2))) {
err = s_mp_div_recursive(a, b, c, d);
} else if (MP_HAS(S_MP_DIV_SCHOOL)) {
diff --git a/mp_div_2.c b/mp_div_2.c
index 60bd63d..8ab9bcb 100644
--- a/mp_div_2.c
+++ b/mp_div_2.c
@@ -6,41 +6,32 @@
/* b = a/2 */
mp_err mp_div_2(const mp_int *a, mp_int *b)
{
- int x, oldused;
- mp_digit r, rr, *tmpa, *tmpb;
mp_err err;
+ int x, oldused;
+ mp_digit r;
- /* copy */
- if (b->alloc < a->used) {
- if ((err = mp_grow(b, a->used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, a->used)) != MP_OKAY) {
+ return err;
}
oldused = b->used;
b->used = a->used;
- /* source alias */
- tmpa = a->dp + b->used - 1;
-
- /* dest alias */
- tmpb = b->dp + b->used - 1;
-
/* carry */
r = 0;
- for (x = b->used - 1; x >= 0; x--) {
+ for (x = b->used; x --> 0;) {
/* get the carry for the next iteration */
- rr = *tmpa & 1u;
+ mp_digit rr = a->dp[x] & 1u;
/* shift the current digit, add in carry and store */
- *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
+ b->dp[x] = (a->dp[x] >> 1) | (r << (MP_DIGIT_BIT - 1));
/* forward carry to next iteration */
r = rr;
}
/* zero excess digits */
- MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
+ s_mp_zero_digs(b->dp + b->used, oldused - b->used);
b->sign = a->sign;
mp_clamp(b);
diff --git a/mp_div_2d.c b/mp_div_2d.c
index 9b396ac..e523465 100644
--- a/mp_div_2d.c
+++ b/mp_div_2d.c
@@ -6,23 +6,16 @@
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
{
- mp_digit D, r, rr;
- int x;
mp_err err;
- /* if the shift count is <= 0 then we do no work */
- if (b <= 0) {
- err = mp_copy(a, c);
- if (d != NULL) {
- mp_zero(d);
- }
- return err;
+ if (b < 0) {
+ return MP_VAL;
}
- /* copy */
if ((err = mp_copy(a, c)) != MP_OKAY) {
return err;
}
+
/* 'a' should not be used after here - it might be the same as d */
/* get the remainder */
@@ -38,28 +31,25 @@ mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
}
/* shift any bit count < MP_DIGIT_BIT */
- D = (mp_digit)(b % MP_DIGIT_BIT);
- if (D != 0u) {
- mp_digit *tmpc, mask, shift;
+ b %= MP_DIGIT_BIT;
+ if (b != 0u) {
+ int x;
+ mp_digit r, mask, shift;
/* mask */
- mask = ((mp_digit)1 << D) - 1uL;
+ mask = ((mp_digit)1 << b) - 1uL;
/* shift for lsb */
- shift = (mp_digit)MP_DIGIT_BIT - D;
-
- /* alias */
- tmpc = c->dp + (c->used - 1);
+ shift = (mp_digit)(MP_DIGIT_BIT - b);
/* carry */
r = 0;
- for (x = c->used - 1; x >= 0; x--) {
+ for (x = c->used; x --> 0;) {
/* get the lower bits of this word in a temp */
- rr = *tmpc & mask;
+ mp_digit rr = c->dp[x] & mask;
/* shift the current word and mix in the carry bits from the previous word */
- *tmpc = (*tmpc >> D) | (r << shift);
- --tmpc;
+ c->dp[x] = (c->dp[x] >> b) | (r << shift);
/* set the carry to the carry bits of the current word found above */
r = rr;
diff --git a/mp_div_d.c b/mp_div_d.c
index 98b6b24..472ab27 100644
--- a/mp_div_d.c
+++ b/mp_div_d.c
@@ -8,7 +8,6 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
{
mp_int q;
mp_word w;
- mp_digit t;
mp_err err;
int ix;
@@ -56,14 +55,12 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
q.used = a->used;
q.sign = a->sign;
w = 0;
- for (ix = a->used - 1; ix >= 0; ix--) {
+ for (ix = a->used; ix --> 0;) {
+ mp_digit t = 0;
w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix];
-
if (w >= b) {
t = (mp_digit)(w / b);
w -= (mp_word)t * (mp_word)b;
- } else {
- t = 0;
}
q.dp[ix] = t;
}
@@ -78,7 +75,7 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
}
mp_clear(&q);
- return err;
+ return MP_OKAY;
}
#endif
diff --git a/mp_dr_reduce.c b/mp_dr_reduce.c
index fba0e21..f0f6f35 100644
--- a/mp_dr_reduce.c
+++ b/mp_dr_reduce.c
@@ -19,59 +19,49 @@
*/
mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
{
- mp_err err;
- int i, m;
- mp_word r;
- mp_digit mu, *tmpx1, *tmpx2;
+ mp_err err;
/* m = digits in modulus */
- m = n->used;
+ int m = n->used;
/* ensure that "x" has at least 2m digits */
- if (x->alloc < (m + m)) {
- if ((err = mp_grow(x, m + m)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(x, m + m)) != MP_OKAY) {
+ return err;
}
/* top of loop, this is where the code resumes if
* another reduction pass is required.
*/
-top:
- /* aliases for digits */
- /* alias for lower half of x */
- tmpx1 = x->dp;
+ for (;;) {
+ int i;
+ mp_digit mu = 0;
- /* alias for upper half of x, or x/B**m */
- tmpx2 = x->dp + m;
+ /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
+ for (i = 0; i < m; i++) {
+ mp_word r = ((mp_word)x->dp[i + m] * (mp_word)k) + x->dp[i] + mu;
+ x->dp[i] = (mp_digit)(r & MP_MASK);
+ mu = (mp_digit)(r >> ((mp_word)MP_DIGIT_BIT));
+ }
- /* set carry to zero */
- mu = 0;
+ /* set final carry */
+ x->dp[i] = mu;
- /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
- for (i = 0; i < m; i++) {
- r = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu;
- *tmpx1++ = (mp_digit)(r & MP_MASK);
- mu = (mp_digit)(r >> ((mp_word)MP_DIGIT_BIT));
- }
+ /* zero words above m */
+ s_mp_zero_digs(x->dp + m + 1, (x->used - m) - 1);
- /* set final carry */
- *tmpx1++ = mu;
+ /* clamp, sub and return */
+ mp_clamp(x);
- /* zero words above m */
- MP_ZERO_DIGITS(tmpx1, (x->used - m) - 1);
-
- /* clamp, sub and return */
- mp_clamp(x);
+ /* if x >= n then subtract and reduce again
+ * Each successive "recursion" makes the input smaller and smaller.
+ */
+ if (mp_cmp_mag(x, n) == MP_LT) {
+ break;
+ }
- /* if x >= n then subtract and reduce again
- * Each successive "recursion" makes the input smaller and smaller.
- */
- if (mp_cmp_mag(x, n) != MP_LT) {
if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
return err;
}
- goto top;
}
return MP_OKAY;
}
diff --git a/mp_from_ubin.c b/mp_from_ubin.c
index ae79be3..8272185 100644
--- a/mp_from_ubin.c
+++ b/mp_from_ubin.c
@@ -9,10 +9,8 @@ mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size)
mp_err err;
/* make sure there are at least two digits */
- if (a->alloc < 2) {
- if ((err = mp_grow(a, 2)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(a, 2)) != MP_OKAY) {
+ return err;
}
/* zero the int */
diff --git a/mp_fwrite.c b/mp_fwrite.c
index 42d7287..6b8ea13 100644
--- a/mp_fwrite.c
+++ b/mp_fwrite.c
@@ -25,7 +25,7 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
}
}
- MP_FREE_BUFFER(buf, size);
+ MP_FREE_BUF(buf, size);
return err;
}
#endif
diff --git a/mp_grow.c b/mp_grow.c
index 25be5ed..0de6679 100644
--- a/mp_grow.c
+++ b/mp_grow.c
@@ -26,7 +26,7 @@ mp_err mp_grow(mp_int *a, int size)
a->dp = dp;
/* zero excess digits */
- MP_ZERO_DIGITS(a->dp + a->alloc, size - a->alloc);
+ s_mp_zero_digs(a->dp + a->alloc, size - a->alloc);
a->alloc = size;
}
return MP_OKAY;
diff --git a/mp_invmod.c b/mp_invmod.c
index e43eb3e..94929cc 100644
--- a/mp_invmod.c
+++ b/mp_invmod.c
@@ -12,12 +12,12 @@ mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
}
/* if the modulus is odd we can use a faster routine instead */
- if (MP_HAS(S_MP_INVMOD_FAST) && mp_isodd(b)) {
- return s_mp_invmod_fast(a, b, c);
+ if (MP_HAS(S_MP_INVMOD_ODD) && mp_isodd(b)) {
+ return s_mp_invmod_odd(a, b, c);
}
- return MP_HAS(S_MP_INVMOD_SLOW)
- ? s_mp_invmod_slow(a, b, c)
+ return MP_HAS(S_MP_INVMOD)
+ ? s_mp_invmod(a, b, c)
: MP_VAL;
}
#endif
diff --git a/mp_lshd.c b/mp_lshd.c
index 6c14402..bfa8af8 100644
--- a/mp_lshd.c
+++ b/mp_lshd.c
@@ -6,6 +6,7 @@
/* shift left a certain amount of digits */
mp_err mp_lshd(mp_int *a, int b)
{
+ mp_err err;
int x;
/* if its less than zero return */
@@ -18,11 +19,8 @@ mp_err mp_lshd(mp_int *a, int b)
}
/* grow to fit the new digits */
- if (a->alloc < (a->used + b)) {
- mp_err err;
- if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
+ return err;
}
/* increment the used by the shift amount then copy upwards */
@@ -37,7 +35,7 @@ mp_err mp_lshd(mp_int *a, int b)
}
/* zero the lower digits */
- MP_ZERO_DIGITS(a->dp, b);
+ s_mp_zero_digs(a->dp, b);
return MP_OKAY;
}
diff --git a/mp_mod_2d.c b/mp_mod_2d.c
index a94a314..82c64f0 100644
--- a/mp_mod_2d.c
+++ b/mp_mod_2d.c
@@ -29,7 +29,7 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
/* zero digits above the last digit of the modulus */
x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1);
- MP_ZERO_DIGITS(c->dp + x, c->used - x);
+ s_mp_zero_digs(c->dp + x, c->used - x);
/* clear the digit that is not completely outside/inside the modulus */
c->dp[b / MP_DIGIT_BIT] &=
diff --git a/mp_mod_d.c b/mp_mod_d.c
deleted file mode 100644
index 3f7e191..0000000
--- a/mp_mod_d.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "tommath_private.h"
-#ifdef MP_MOD_D_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c)
-{
- return mp_div_d(a, b, NULL, c);
-}
-#endif
diff --git a/mp_montgomery_reduce.c b/mp_montgomery_reduce.c
index a872aba..de6a900 100644
--- a/mp_montgomery_reduce.c
+++ b/mp_montgomery_reduce.c
@@ -6,9 +6,8 @@
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
{
- int ix, digs;
- mp_err err;
- mp_digit mu;
+ mp_err err;
+ int ix, digs;
/* can the fast reduction [comba] method be used?
*
@@ -20,18 +19,19 @@ mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
if ((digs < MP_WARRAY) &&
(x->used <= MP_WARRAY) &&
(n->used < MP_MAXFAST)) {
- return s_mp_montgomery_reduce_fast(x, n, rho);
+ return s_mp_montgomery_reduce_comba(x, n, rho);
}
/* grow the input as required */
- if (x->alloc < digs) {
- if ((err = mp_grow(x, digs)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(x, digs)) != MP_OKAY) {
+ return err;
}
x->used = digs;
for (ix = 0; ix < n->used; ix++) {
+ int iy;
+ mp_digit u, mu;
+
/* mu = ai * rho mod b
*
* The value of rho must be precalculated via
@@ -43,41 +43,28 @@ mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
mu = (mp_digit)(((mp_word)x->dp[ix] * (mp_word)rho) & MP_MASK);
/* a = a + mu * m * b**i */
- {
- int iy;
- mp_digit *tmpn, *tmpx, u;
- mp_word r;
-
- /* alias for digits of the modulus */
- tmpn = n->dp;
-
- /* alias for the digits of x [the input] */
- tmpx = x->dp + ix;
- /* set the carry to zero */
- u = 0;
+ /* Multiply and add in place */
+ u = 0;
+ for (iy = 0; iy < n->used; iy++) {
+ /* compute product and sum */
+ mp_word r = ((mp_word)mu * (mp_word)n->dp[iy]) +
+ (mp_word)u + (mp_word)x->dp[ix + iy];
- /* Multiply and add in place */
- for (iy = 0; iy < n->used; iy++) {
- /* compute product and sum */
- r = ((mp_word)mu * (mp_word)*tmpn++) +
- (mp_word)u + (mp_word)*tmpx;
+ /* get carry */
+ u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
- /* get carry */
- u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
-
- /* fix digit */
- *tmpx++ = (mp_digit)(r & (mp_word)MP_MASK);
- }
- /* At this point the ix'th digit of x should be zero */
-
-
- /* propagate carries upwards as required*/
- while (u != 0u) {
- *tmpx += u;
- u = *tmpx >> MP_DIGIT_BIT;
- *tmpx++ &= MP_MASK;
- }
+ /* fix digit */
+ x->dp[ix + iy] = (mp_digit)(r & (mp_word)MP_MASK);
+ }
+ /* At this point the ix'th digit of x should be zero */
+
+ /* propagate carries upwards as required*/
+ while (u != 0u) {
+ x->dp[ix + iy] += u;
+ u = x->dp[ix + iy] >> MP_DIGIT_BIT;
+ x->dp[ix + iy] &= MP_MASK;
+ ++iy;
}
}
diff --git a/mp_mul.c b/mp_mul.c
index 9c8f8ae..4103535 100644
--- a/mp_mul.c
+++ b/mp_mul.c
@@ -7,31 +7,31 @@
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
{
mp_err err;
- int min_len = MP_MIN(a->used, b->used),
- max_len = MP_MAX(a->used, b->used),
+ int min = MP_MIN(a->used, b->used),
+ max = MP_MAX(a->used, b->used),
digs = a->used + b->used + 1;
mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
- if (MP_HAS(S_MP_BALANCE_MUL) &&
+ if (MP_HAS(S_MP_MUL_BALANCE) &&
/* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off.
- * The bigger one needs to be at least about one MP_KARATSUBA_MUL_CUTOFF bigger
+ * The bigger one needs to be at least about one MP_MUL_KARATSUBA_CUTOFF bigger
* to make some sense, but it depends on architecture, OS, position of the
* stars... so YMMV.
- * Using it to cut the input into slices small enough for s_mp_mul_digs_fast
+ * Using it to cut the input into slices small enough for s_mp_mul_comba
* was actually slower on the author's machine, but YMMV.
*/
- (min_len >= MP_KARATSUBA_MUL_CUTOFF) &&
- ((max_len / 2) >= MP_KARATSUBA_MUL_CUTOFF) &&
+ (min >= MP_MUL_KARATSUBA_CUTOFF) &&
+ ((max / 2) >= MP_MUL_KARATSUBA_CUTOFF) &&
/* Not much effect was observed below a ratio of 1:2, but again: YMMV. */
- (max_len >= (2 * min_len))) {
- err = s_mp_balance_mul(a,b,c);
- } else if (MP_HAS(S_MP_TOOM_MUL) &&
- (min_len >= MP_TOOM_MUL_CUTOFF)) {
- err = s_mp_toom_mul(a, b, c);
- } else if (MP_HAS(S_MP_KARATSUBA_MUL) &&
- (min_len >= MP_KARATSUBA_MUL_CUTOFF)) {
- err = s_mp_karatsuba_mul(a, b, c);
- } else if (MP_HAS(S_MP_MUL_DIGS_FAST) &&
+ (max >= (2 * min))) {
+ err = s_mp_mul_balance(a,b,c);
+ } else if (MP_HAS(S_MP_MUL_TOOM) &&
+ (min >= MP_MUL_TOOM_CUTOFF)) {
+ err = s_mp_mul_toom(a, b, c);
+ } else if (MP_HAS(S_MP_MUL_KARATSUBA) &&
+ (min >= MP_MUL_KARATSUBA_CUTOFF)) {
+ err = s_mp_mul_karatsuba(a, b, c);
+ } else if (MP_HAS(S_MP_MUL_COMBA) &&
/* can we use the fast multiplier?
*
* The fast multiplier can be used if the output will
@@ -39,10 +39,10 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
* digits won't affect carry propagation
*/
(digs < MP_WARRAY) &&
- (min_len <= MP_MAXFAST)) {
- err = s_mp_mul_digs_fast(a, b, c, digs);
- } else if (MP_HAS(S_MP_MUL_DIGS)) {
- err = s_mp_mul_digs(a, b, c, digs);
+ (min <= MP_MAXFAST)) {
+ err = s_mp_mul_comba(a, b, c, digs);
+ } else if (MP_HAS(S_MP_MUL)) {
+ err = s_mp_mul(a, b, c, digs);
} else {
err = MP_VAL;
}
diff --git a/mp_mul_2.c b/mp_mul_2.c
index cd5589d..7d7084b 100644
--- a/mp_mul_2.c
+++ b/mp_mul_2.c
@@ -6,58 +6,47 @@
/* b = a*2 */
mp_err mp_mul_2(const mp_int *a, mp_int *b)
{
- int x, oldused;
mp_err err;
+ int x, oldused;
+ mp_digit r;
/* grow to accomodate result */
- if (b->alloc < (a->used + 1)) {
- if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
+ return err;
}
oldused = b->used;
b->used = a->used;
- {
- mp_digit r, rr, *tmpa, *tmpb;
+ /* carry */
+ r = 0;
+ for (x = 0; x < a->used; x++) {
- /* alias for source */
- tmpa = a->dp;
-
- /* alias for dest */
- tmpb = b->dp;
-
- /* carry */
- r = 0;
- for (x = 0; x < a->used; x++) {
+ /* get what will be the *next* carry bit from the
+ * MSB of the current digit
+ */
+ mp_digit rr = a->dp[x] >> (mp_digit)(MP_DIGIT_BIT - 1);
- /* get what will be the *next* carry bit from the
- * MSB of the current digit
- */
- rr = *tmpa >> (mp_digit)(MP_DIGIT_BIT - 1);
+ /* now shift up this digit, add in the carry [from the previous] */
+ b->dp[x] = ((a->dp[x] << 1uL) | r) & MP_MASK;
- /* now shift up this digit, add in the carry [from the previous] */
- *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
+ /* copy the carry that would be from the source
+ * digit into the next iteration
+ */
+ r = rr;
+ }
- /* copy the carry that would be from the source
- * digit into the next iteration
- */
- r = rr;
- }
+ /* new leading digit? */
+ if (r != 0u) {
+ /* add a MSB which is always 1 at this point */
+ b->dp[b->used++] = 1;
+ }
- /* new leading digit? */
- if (r != 0u) {
- /* add a MSB which is always 1 at this point */
- *tmpb = 1;
- ++(b->used);
- }
+ /* now zero any excess digits on the destination
+ * that we didn't write to
+ */
+ s_mp_zero_digs(b->dp + b->used, oldused - b->used);
- /* now zero any excess digits on the destination
- * that we didn't write to
- */
- MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
- }
b->sign = a->sign;
return MP_OKAY;
}
diff --git a/mp_mul_2d.c b/mp_mul_2d.c
index 1ba53a0..e458137 100644
--- a/mp_mul_2d.c
+++ b/mp_mul_2d.c
@@ -6,20 +6,18 @@
/* shift left by a certain bit count */
mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c)
{
- mp_digit d;
- mp_err err;
+ mp_err err;
- /* copy */
- if (a != c) {
- if ((err = mp_copy(a, c)) != MP_OKAY) {
- return err;
- }
+ if (b < 0) {
+ return MP_VAL;
}
- if (c->alloc < (c->used + (b / MP_DIGIT_BIT) + 1)) {
- if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_copy(a, c)) != MP_OKAY) {
+ return err;
+ }
+
+ if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
+ return err;
}
/* shift by as many digits in the bit count */
@@ -30,29 +28,25 @@ mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c)
}
/* shift any bit count < MP_DIGIT_BIT */
- d = (mp_digit)(b % MP_DIGIT_BIT);
- if (d != 0u) {
- mp_digit *tmpc, shift, mask, r, rr;
+ b %= MP_DIGIT_BIT;
+ if (b != 0u) {
+ mp_digit shift, mask, r;
int x;
/* bitmask for carries */
- mask = ((mp_digit)1 << d) - (mp_digit)1;
+ mask = ((mp_digit)1 << b) - (mp_digit)1;
/* shift for msbs */
- shift = (mp_digit)MP_DIGIT_BIT - d;
-
- /* alias */
- tmpc = c->dp;
+ shift = (mp_digit)(MP_DIGIT_BIT - b);
/* carry */
r = 0;
for (x = 0; x < c->used; x++) {
/* get the higher bits of the current word */
- rr = (*tmpc >> shift) & mask;
+ mp_digit rr = (c->dp[x] >> shift) & mask;
/* shift the current word and OR in the carry */
- *tmpc = ((*tmpc << d) | r) & MP_MASK;
- ++tmpc;
+ c->dp[x] = ((c->dp[x] << b) | r) & MP_MASK;
/* set the carry to the carry bits of the current word */
r = rr;
diff --git a/mp_mul_d.c b/mp_mul_d.c
index 399dc7b..30d6c93 100644
--- a/mp_mul_d.c
+++ b/mp_mul_d.c
@@ -6,54 +6,45 @@
/* multiply by a digit */
mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
{
- mp_digit u, *tmpa, *tmpc;
- mp_word r;
+ mp_digit u;
mp_err err;
- int ix, olduse;
+ int ix, oldused;
/* make sure c is big enough to hold a*b */
- if (c->alloc < (a->used + 1)) {
- if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
+ return err;
}
/* get the original destinations used count */
- olduse = c->used;
+ oldused = c->used;
/* set the sign */
c->sign = a->sign;
- /* alias for a->dp [source] */
- tmpa = a->dp;
-
- /* alias for c->dp [dest] */
- tmpc = c->dp;
-
/* zero carry */
u = 0;
/* compute columns */
for (ix = 0; ix < a->used; ix++) {
/* compute product and carry sum for this term */
- r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
+ mp_word r = (mp_word)u + ((mp_word)a->dp[ix] * (mp_word)b);
/* mask off higher bits to get a single digit */
- *tmpc++ = (mp_digit)(r & (mp_word)MP_MASK);
+ c->dp[ix] = (mp_digit)(r & (mp_word)MP_MASK);
/* send carry into next iteration */
u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
}
/* store final carry [if any] and increment ix offset */
- *tmpc++ = u;
- ++ix;
-
- /* now zero digits above the top */
- MP_ZERO_DIGITS(tmpc, olduse - ix);
+ c->dp[ix] = u;
/* set used count */
c->used = a->used + 1;
+
+ /* now zero digits above the top */
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
+
mp_clamp(c);
return MP_OKAY;
diff --git a/mp_neg.c b/mp_neg.c
index f54ef3e..bfb6eb9 100644
--- a/mp_neg.c
+++ b/mp_neg.c
@@ -6,11 +6,9 @@
/* b = -a */
mp_err mp_neg(const mp_int *a, mp_int *b)
{
- if (a != b) {
- mp_err err;
- if ((err = mp_copy(a, b)) != MP_OKAY) {
- return err;
- }
+ mp_err err;
+ if ((err = mp_copy(a, b)) != MP_OKAY) {
+ return err;
}
b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG;
diff --git a/mp_or.c b/mp_or.c
index 7fa1375..5cf5255 100644
--- a/mp_or.c
+++ b/mp_or.c
@@ -11,10 +11,8 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
mp_digit ac = 1, bc = 1, cc = 1;
mp_sign csign = ((a->sign == MP_NEG) || (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
- if (c->alloc < used) {
- if ((err = mp_grow(c, used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
for (i = 0; i < used; i++) {
diff --git a/mp_prime_fermat.c b/mp_prime_fermat.c
index 50d2e5e..ac8116f 100644
--- a/mp_prime_fermat.c
+++ b/mp_prime_fermat.c
@@ -16,9 +16,6 @@ mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, bool *result)
mp_int t;
mp_err err;
- /* default to composite */
- *result = false;
-
/* ensure b > 1 */
if (mp_cmp_d(b, 1uL) != MP_GT) {
return MP_VAL;
@@ -31,16 +28,13 @@ mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, bool *result)
/* compute t = b**a mod a */
if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) {
- goto LBL_T;
+ goto LBL_ERR;
}
/* is it equal to b? */
- if (mp_cmp(&t, b) == MP_EQ) {
- *result = true;
- }
+ *result = mp_cmp(&t, b) == MP_EQ;
- err = MP_OKAY;
-LBL_T:
+LBL_ERR:
mp_clear(&t);
return err;
}
diff --git a/mp_prime_frobenius_underwood.c b/mp_prime_frobenius_underwood.c
index 543b8b4..62d3476 100644
--- a/mp_prime_frobenius_underwood.c
+++ b/mp_prime_frobenius_underwood.c
@@ -23,17 +23,16 @@
mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result)
{
mp_int T1z, T2z, Np1z, sz, tz;
-
- int a, ap2, length, i, j;
+ int a, ap2, i;
mp_err err;
- *result = false;
-
if ((err = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) {
return err;
}
for (a = 0; a < LTM_FROBENIUS_UNDERWOOD_A; a++) {
+ int j;
+
/* TODO: That's ugly! No, really, it is! */
if ((a==2) || (a==4) || (a==7) || (a==8) || (a==10) ||
(a==14) || (a==18) || (a==23) || (a==26) || (a==28)) {
@@ -42,7 +41,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result)
mp_set_i32(&T1z, (int32_t)((a * a) - 4));
- if ((err = mp_kronecker(&T1z, N, &j)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_kronecker(&T1z, N, &j)) != MP_OKAY) goto LBL_END;
if (j == -1) {
break;
@@ -50,73 +49,76 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result)
if (j == 0) {
/* composite */
- goto LBL_FU_ERR;
+ *result = false;
+ goto LBL_END;
}
}
/* Tell it a composite and set return value accordingly */
if (a >= LTM_FROBENIUS_UNDERWOOD_A) {
err = MP_ITER;
- goto LBL_FU_ERR;
+ goto LBL_END;
}
/* Composite if N and (a+4)*(2*a+5) are not coprime */
mp_set_u32(&T1z, (uint32_t)((a+4)*((2*a)+5)));
- if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) goto LBL_END;
- if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) goto LBL_FU_ERR;
+ if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) {
+ /* composite */
+ *result = false;
+ goto LBL_END;
+ }
ap2 = a + 2;
- if ((err = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) goto LBL_END;
mp_set(&sz, 1uL);
mp_set(&tz, 2uL);
- length = mp_count_bits(&Np1z);
- for (i = length - 2; i >= 0; i--) {
+ for (i = mp_count_bits(&Np1z) - 2; i >= 0; i--) {
/*
* temp = (sz*(a*sz+2*tz))%N;
* tz = ((tz-sz)*(tz+sz))%N;
* sz = temp;
*/
- if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_END;
/* a = 0 at about 50% of the cases (non-square and odd input) */
if (a != 0) {
- if ((err = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) goto LBL_END;
}
- if ((err = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_add(&sz, &tz, &sz)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY) goto LBL_FU_ERR;
- if (s_mp_get_bit(&Np1z, (unsigned int)i)) {
+ if ((err = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_add(&sz, &tz, &sz)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY) goto LBL_END;
+ if (s_mp_get_bit(&Np1z, i)) {
/*
* temp = (a+2) * sz + tz
* tz = 2 * tz - sz
* sz = temp
*/
if (a == 0) {
- if ((err = mp_mul_2(&sz, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_mul_2(&sz, &T1z)) != MP_OKAY) goto LBL_END;
} else {
- if ((err = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) goto LBL_END;
}
- if ((err = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_FU_ERR;
- if ((err = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) goto LBL_FU_ERR;
+ if ((err = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_END;
+ if ((err = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) goto LBL_END;
mp_exch(&sz, &T1z);
}
}
mp_set_u32(&T1z, (uint32_t)((2 * a) + 5));
- if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) goto LBL_FU_ERR;
- if (mp_iszero(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
- *result = true;
- }
+ if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) goto LBL_END;
+
+ *result = mp_iszero(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ);
-LBL_FU_ERR:
+LBL_END:
mp_clear_multi(&tz, &sz, &Np1z, &T2z, &T1z, NULL);
return err;
}
diff --git a/mp_prime_is_prime.c b/mp_prime_is_prime.c
index d0eca2c..7d73864 100644
--- a/mp_prime_is_prime.c
+++ b/mp_prime_is_prime.c
@@ -13,14 +13,12 @@ static unsigned int s_floor_ilog2(int value)
return r;
}
-
mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
{
mp_int b;
- int ix, p_max = 0, size_a, len;
- bool res;
+ int ix;
+ bool res;
mp_err err;
- unsigned int fips_rand, mask;
/* default to no */
*result = false;
@@ -133,6 +131,8 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
TODO: can be made a bit finer grained but comparing is not free.
*/
if (t < 0) {
+ int p_max = 0;
+
/*
Sorenson, Jonathan; Webster, Jonathan (2015).
"Strong Pseudoprimes to Twelve Prime Bases".
@@ -174,6 +174,9 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
See Fips 186.4 p. 126ff
*/
else if (t > 0) {
+ unsigned int mask;
+ int size_a;
+
/*
* The mp_digit's have a defined bit-size but the size of the
* array a.dp is a simple 'int' and this library can not assume full
@@ -219,6 +222,9 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
need to be prime.
*/
for (ix = 0; ix < t; ix++) {
+ unsigned int fips_rand;
+ int len;
+
/* mp_rand() guarantees the first digit to be non-zero */
if ((err = mp_rand(&b, 1)) != MP_OKAY) {
goto LBL_B;
diff --git a/mp_prime_miller_rabin.c b/mp_prime_miller_rabin.c
index a3af8bc..4c23a9f 100644
--- a/mp_prime_miller_rabin.c
+++ b/mp_prime_miller_rabin.c
@@ -16,9 +16,6 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
mp_err err;
int s, j;
- /* default */
- *result = false;
-
/* ensure b > 1 */
if (mp_cmp_d(b, 1uL) != MP_GT) {
return MP_VAL;
@@ -29,12 +26,12 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
return err;
}
if ((err = mp_sub_d(&n1, 1uL, &n1)) != MP_OKAY) {
- goto LBL_N1;
+ goto LBL_ERR1;
}
/* set 2**s * r = n1 */
if ((err = mp_init_copy(&r, &n1)) != MP_OKAY) {
- goto LBL_N1;
+ goto LBL_ERR1;
}
/* count the number of least significant bits
@@ -44,15 +41,15 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
/* now divide n - 1 by 2**s */
if ((err = mp_div_2d(&r, s, &r, NULL)) != MP_OKAY) {
- goto LBL_R;
+ goto LBL_ERR2;
}
/* compute y = b**r mod a */
if ((err = mp_init(&y)) != MP_OKAY) {
- goto LBL_R;
+ goto LBL_ERR2;
}
if ((err = mp_exptmod(b, &r, a, &y)) != MP_OKAY) {
- goto LBL_Y;
+ goto LBL_END;
}
/* if y != 1 and y != n1 do */
@@ -61,12 +58,13 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
/* while j <= s-1 and y != n1 */
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
if ((err = mp_sqrmod(&y, a, &y)) != MP_OKAY) {
- goto LBL_Y;
+ goto LBL_END;
}
/* if y == 1 then composite */
if (mp_cmp_d(&y, 1uL) == MP_EQ) {
- goto LBL_Y;
+ *result = false;
+ goto LBL_END;
}
++j;
@@ -74,17 +72,19 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
/* if y != n1 then composite */
if (mp_cmp(&y, &n1) != MP_EQ) {
- goto LBL_Y;
+ *result = false;
+ goto LBL_END;
}
}
/* probably prime now */
*result = true;
-LBL_Y:
+
+LBL_END:
mp_clear(&y);
-LBL_R:
+LBL_ERR2:
mp_clear(&r);
-LBL_N1:
+LBL_ERR1:
mp_clear(&n1);
return err;
}
diff --git a/mp_prime_next_prime.c b/mp_prime_next_prime.c
index 40c94a4..6faa08d 100644
--- a/mp_prime_next_prime.c
+++ b/mp_prime_next_prime.c
@@ -10,11 +10,10 @@
*/
mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
{
- int x, y;
- mp_ord cmp;
+ int x;
mp_err err;
bool res = false;
- mp_digit res_tab[MP_PRIME_TAB_SIZE], step, kstep;
+ mp_digit res_tab[MP_PRIME_TAB_SIZE], kstep;
mp_int b;
/* force positive */
@@ -24,7 +23,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
if (mp_cmp_d(a, s_mp_prime_tab[MP_PRIME_TAB_SIZE-1]) == MP_LT) {
/* find which prime it is bigger than "a" */
for (x = 0; x < MP_PRIME_TAB_SIZE; x++) {
- cmp = mp_cmp_d(a, s_mp_prime_tab[x]);
+ mp_ord cmp = mp_cmp_d(a, s_mp_prime_tab[x]);
if (cmp == MP_EQ) {
continue;
}
@@ -42,11 +41,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
}
/* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */
- if (bbs_style) {
- kstep = 4;
- } else {
- kstep = 2;
- }
+ kstep = bbs_style ? 4 : 2;
/* at this point we will use a combination of a sieve and Miller-Rabin */
@@ -79,11 +74,12 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
}
for (;;) {
+ mp_digit step = 0;
+ bool y;
/* skip to the next non-trivially divisible candidate */
- step = 0;
do {
- /* y == 1 if any residue was zero [e.g. cannot be prime] */
- y = 0;
+ /* y == true if any residue was zero [e.g. cannot be prime] */
+ y = false;
/* increase step to next candidate */
step += kstep;
@@ -100,10 +96,10 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
/* set flag if zero */
if (res_tab[x] == 0u) {
- y = 1;
+ y = true;
}
}
- } while ((y == 1) && (step < (((mp_digit)1 << MP_DIGIT_BIT) - kstep)));
+ } while (y && (step < (((mp_digit)1 << MP_DIGIT_BIT) - kstep)));
/* add the step */
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
@@ -111,7 +107,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
}
/* if didn't pass sieve and step == MP_MAX then skip test */
- if ((y == 1) && (step >= (((mp_digit)1 << MP_DIGIT_BIT) - kstep))) {
+ if (y && (step >= (((mp_digit)1 << MP_DIGIT_BIT) - kstep))) {
continue;
}
@@ -123,7 +119,6 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
}
}
- err = MP_OKAY;
LBL_ERR:
mp_clear(&b);
return err;
diff --git a/mp_prime_rand.c b/mp_prime_rand.c
index 8476b4f..c5cebbd 100644
--- a/mp_prime_rand.c
+++ b/mp_prime_rand.c
@@ -116,7 +116,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
err = MP_OKAY;
LBL_ERR:
- MP_FREE_BUFFER(tmp, (size_t)bsize);
+ MP_FREE_BUF(tmp, (size_t)bsize);
return err;
}
diff --git a/mp_prime_strong_lucas_selfridge.c b/mp_prime_strong_lucas_selfridge.c
index df5de96..6262e07 100644
--- a/mp_prime_strong_lucas_selfridge.c
+++ b/mp_prime_strong_lucas_selfridge.c
@@ -192,7 +192,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
if ((err = mp_mod(&Qmz, a, &Qmz)) != MP_OKAY) goto LBL_LS_ERR;
if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) goto LBL_LS_ERR;
- if (s_mp_get_bit(&Dz, (unsigned int)u)) {
+ if (s_mp_get_bit(&Dz, u)) {
/* Formulas for addition of indices (carried out mod N);
*
* U_(m+n) = (U_m*V_n + U_n*V_m)/2
diff --git a/mp_reduce.c b/mp_reduce.c
index 5226fe7..b6fae55 100644
--- a/mp_reduce.c
+++ b/mp_reduce.c
@@ -26,12 +26,12 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) {
goto LBL_ERR;
}
- } else if (MP_HAS(S_MP_MUL_HIGH_DIGS)) {
- if ((err = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
+ } else if (MP_HAS(S_MP_MUL_HIGH)) {
+ if ((err = s_mp_mul_high(&q, mu, &q, um)) != MP_OKAY) {
goto LBL_ERR;
}
- } else if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)) {
- if ((err = s_mp_mul_high_digs_fast(&q, mu, &q, um)) != MP_OKAY) {
+ } else if (MP_HAS(S_MP_MUL_HIGH_COMBA)) {
+ if ((err = s_mp_mul_high_comba(&q, mu, &q, um)) != MP_OKAY) {
goto LBL_ERR;
}
} else {
@@ -48,7 +48,7 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
}
/* q = q * m mod b**(k+1), quick (no division) */
- if ((err = s_mp_mul_digs(&q, m, &q, um + 1)) != MP_OKAY) {
+ if ((err = s_mp_mul(&q, m, &q, um + 1)) != MP_OKAY) {
goto LBL_ERR;
}
diff --git a/mp_reduce_2k.c b/mp_reduce_2k.c
index 5d3c7f9..e635f5b 100644
--- a/mp_reduce_2k.c
+++ b/mp_reduce_2k.c
@@ -8,36 +8,37 @@ mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d)
{
mp_int q;
mp_err err;
- int p;
+ int p;
if ((err = mp_init(&q)) != MP_OKAY) {
return err;
}
p = mp_count_bits(n);
-top:
- /* q = a/2**p, a = a mod 2**p */
- if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
- goto LBL_ERR;
- }
-
- if (d != 1u) {
- /* q = q * d */
- if ((err = mp_mul_d(&q, d, &q)) != MP_OKAY) {
+ for (;;) {
+ /* q = a/2**p, a = a mod 2**p */
+ if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto LBL_ERR;
}
- }
- /* a = a + q */
- if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ if (d != 1u) {
+ /* q = q * d */
+ if ((err = mp_mul_d(&q, d, &q)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ }
- if (mp_cmp_mag(a, n) != MP_LT) {
+ /* a = a + q */
+ if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+
+ if (mp_cmp_mag(a, n) == MP_LT) {
+ break;
+ }
if ((err = s_mp_sub(a, n, a)) != MP_OKAY) {
goto LBL_ERR;
}
- goto top;
}
LBL_ERR:
diff --git a/mp_reduce_2k_l.c b/mp_reduce_2k_l.c
index 6328cbc..31d9a18 100644
--- a/mp_reduce_2k_l.c
+++ b/mp_reduce_2k_l.c
@@ -18,27 +18,30 @@ mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d)
}
p = mp_count_bits(n);
-top:
- /* q = a/2**p, a = a mod 2**p */
- if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
- goto LBL_ERR;
- }
- /* q = q * d */
- if ((err = mp_mul(&q, d, &q)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ for (;;) {
+ /* q = a/2**p, a = a mod 2**p */
+ if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
- /* a = a + q */
- if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
- goto LBL_ERR;
- }
+ /* q = q * d */
+ if ((err = mp_mul(&q, d, &q)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+
+ /* a = a + q */
+ if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
- if (mp_cmp_mag(a, n) != MP_LT) {
+ if (mp_cmp_mag(a, n) == MP_LT) {
+ break;
+ }
if ((err = s_mp_sub(a, n, a)) != MP_OKAY) {
goto LBL_ERR;
}
- goto top;
+
}
LBL_ERR:
diff --git a/mp_reduce_2k_setup.c b/mp_reduce_2k_setup.c
index 0f3fd29..51f8841 100644
--- a/mp_reduce_2k_setup.c
+++ b/mp_reduce_2k_setup.c
@@ -8,25 +8,23 @@ mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d)
{
mp_err err;
mp_int tmp;
- int p;
if ((err = mp_init(&tmp)) != MP_OKAY) {
return err;
}
- p = mp_count_bits(a);
- if ((err = mp_2expt(&tmp, p)) != MP_OKAY) {
- mp_clear(&tmp);
- return err;
+ if ((err = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
+ goto LBL_ERR;
}
if ((err = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) {
- mp_clear(&tmp);
- return err;
+ goto LBL_ERR;
}
*d = tmp.dp[0];
+
+LBL_ERR:
mp_clear(&tmp);
- return MP_OKAY;
+ return err;
}
#endif
diff --git a/mp_rshd.c b/mp_rshd.c
index d798907..3f0a941 100644
--- a/mp_rshd.c
+++ b/mp_rshd.c
@@ -35,7 +35,7 @@ void mp_rshd(mp_int *a, int b)
}
/* zero the top digits */
- MP_ZERO_DIGITS(a->dp + a->used - b, b);
+ s_mp_zero_digs(a->dp + a->used - b, b);
/* remove excess digits */
a->used -= b;
diff --git a/mp_set.c b/mp_set.c
index 3ee5f81..bc0c4da 100644
--- a/mp_set.c
+++ b/mp_set.c
@@ -10,6 +10,6 @@ void mp_set(mp_int *a, mp_digit b)
a->dp[0] = b & MP_MASK;
a->sign = MP_ZPOS;
a->used = (a->dp[0] != 0u) ? 1 : 0;
- MP_ZERO_DIGITS(a->dp + a->used, oldused - a->used);
+ s_mp_zero_digs(a->dp + a->used, oldused - a->used);
}
#endif
diff --git a/mp_sqr.c b/mp_sqr.c
index e38130b..b0da0ed 100644
--- a/mp_sqr.c
+++ b/mp_sqr.c
@@ -7,16 +7,16 @@
mp_err mp_sqr(const mp_int *a, mp_int *b)
{
mp_err err;
- if (MP_HAS(S_MP_TOOM_SQR) && /* use Toom-Cook? */
- (a->used >= MP_TOOM_SQR_CUTOFF)) {
- err = s_mp_toom_sqr(a, b);
- } else if (MP_HAS(S_MP_KARATSUBA_SQR) && /* Karatsuba? */
- (a->used >= MP_KARATSUBA_SQR_CUTOFF)) {
- err = s_mp_karatsuba_sqr(a, b);
- } else if (MP_HAS(S_MP_SQR_FAST) && /* can we use the fast comba multiplier? */
+ if (MP_HAS(S_MP_SQR_TOOM) && /* use Toom-Cook? */
+ (a->used >= MP_SQR_TOOM_CUTOFF)) {
+ err = s_mp_sqr_toom(a, b);
+ } else if (MP_HAS(S_MP_SQR_KARATSUBA) && /* Karatsuba? */
+ (a->used >= MP_SQR_KARATSUBA_CUTOFF)) {
+ err = s_mp_sqr_karatsuba(a, b);
+ } else if (MP_HAS(S_MP_SQR_COMBA) && /* can we use the fast comba multiplier? */
(((a->used * 2) + 1) < MP_WARRAY) &&
(a->used < (MP_MAXFAST / 2))) {
- err = s_mp_sqr_fast(a, b);
+ err = s_mp_sqr_comba(a, b);
} else if (MP_HAS(S_MP_SQR)) {
err = s_mp_sqr(a, b);
} else {
diff --git a/mp_sub_d.c b/mp_sub_d.c
index 96a747c..e80df3d 100644
--- a/mp_sub_d.c
+++ b/mp_sub_d.c
@@ -6,9 +6,8 @@
/* single digit subtraction */
mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
{
- mp_digit *tmpa, *tmpc;
- mp_err err;
- int ix, oldused;
+ mp_err err;
+ int oldused;
/* fast path for a == c */
if (a == c) {
@@ -25,10 +24,8 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
}
/* grow c as required */
- if (c->alloc < (a->used + 1)) {
- if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
+ return err;
}
/* if a is negative just do an unsigned
@@ -46,24 +43,17 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
return err;
}
- /* setup regs */
oldused = c->used;
- tmpa = a->dp;
- tmpc = c->dp;
/* if a <= b simply fix the single digit */
if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) {
- if (a->used == 1) {
- *tmpc++ = b - *tmpa;
- } else {
- *tmpc++ = b;
- }
- ix = 1;
+ c->dp[0] = (a->used == 1) ? b - a->dp[0] : b;
/* negative/1digit */
c->sign = MP_NEG;
c->used = 1;
} else {
+ int i;
mp_digit mu = b;
/* positive/size */
@@ -71,15 +61,15 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
c->used = a->used;
/* subtract digits, mu is carry */
- for (ix = 0; ix < a->used; ix++) {
- *tmpc = *tmpa++ - mu;
- mu = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
- *tmpc++ &= MP_MASK;
+ for (i = 0; i < a->used; i++) {
+ c->dp[i] = a->dp[i] - mu;
+ mu = c->dp[i] >> (MP_SIZEOF_BITS(mp_digit) - 1u);
+ c->dp[i] &= MP_MASK;
}
}
/* zero excess digits */
- MP_ZERO_DIGITS(tmpc, oldused - ix);
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
mp_clamp(c);
return MP_OKAY;
diff --git a/mp_xor.c b/mp_xor.c
index ca2c2f1..2fe8618 100644
--- a/mp_xor.c
+++ b/mp_xor.c
@@ -11,10 +11,8 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
mp_digit ac = 1, bc = 1, cc = 1;
mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
- if (c->alloc < used) {
- if ((err = mp_grow(c, used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
for (i = 0; i < used; i++) {
diff --git a/mp_zero.c b/mp_zero.c
index b7dddd2..48b60e4 100644
--- a/mp_zero.c
+++ b/mp_zero.c
@@ -7,7 +7,7 @@
void mp_zero(mp_int *a)
{
a->sign = MP_ZPOS;
- MP_ZERO_DIGITS(a->dp, a->used);
+ s_mp_zero_digs(a->dp, a->used);
a->used = 0;
}
#endif
diff --git a/s_mp_add.c b/s_mp_add.c
index 9220719..2bda2fe 100644
--- a/s_mp_add.c
+++ b/s_mp_add.c
@@ -6,84 +6,63 @@
/* low level addition, based on HAC pp.594, Algorithm 14.7 */
mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
{
- const mp_int *x;
+ int oldused, min, max, i;
+ mp_digit u;
mp_err err;
- int olduse, min, max;
/* find sizes, we let |a| <= |b| which means we have to sort
* them. "x" will point to the input with the most digits
*/
- if (a->used > b->used) {
- min = b->used;
- max = a->used;
- x = a;
- } else {
- min = a->used;
- max = b->used;
- x = b;
+ if (a->used < b->used) {
+ MP_EXCH(const mp_int *, a, b);
}
+ min = b->used;
+ max = a->used;
+
/* init result */
- if (c->alloc < (max + 1)) {
- if ((err = mp_grow(c, max + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, max + 1)) != MP_OKAY) {
+ return err;
}
/* get old used digit count and set new one */
- olduse = c->used;
+ oldused = c->used;
c->used = max + 1;
- {
- mp_digit u, *tmpa, *tmpb, *tmpc;
- int i;
-
- /* alias for digit pointers */
+ /* zero the carry */
+ u = 0;
+ for (i = 0; i < min; i++) {
+ /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
+ c->dp[i] = a->dp[i] + b->dp[i] + u;
- /* first input */
- tmpa = a->dp;
+ /* U = carry bit of T[i] */
+ u = c->dp[i] >> (mp_digit)MP_DIGIT_BIT;
- /* second input */
- tmpb = b->dp;
-
- /* destination */
- tmpc = c->dp;
+ /* take away carry bit from T[i] */
+ c->dp[i] &= MP_MASK;
+ }
- /* zero the carry */
- u = 0;
- for (i = 0; i < min; i++) {
- /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
- *tmpc = *tmpa++ + *tmpb++ + u;
+ /* now copy higher words if any, that is in A+B
+ * if A or B has more digits add those in
+ */
+ if (min != max) {
+ for (; i < max; i++) {
+ /* T[i] = A[i] + U */
+ c->dp[i] = a->dp[i] + u;
/* U = carry bit of T[i] */
- u = *tmpc >> (mp_digit)MP_DIGIT_BIT;
+ u = c->dp[i] >> (mp_digit)MP_DIGIT_BIT;
/* take away carry bit from T[i] */
- *tmpc++ &= MP_MASK;
- }
-
- /* now copy higher words if any, that is in A+B
- * if A or B has more digits add those in
- */
- if (min != max) {
- for (; i < max; i++) {
- /* T[i] = X[i] + U */
- *tmpc = x->dp[i] + u;
-
- /* U = carry bit of T[i] */
- u = *tmpc >> (mp_digit)MP_DIGIT_BIT;
-
- /* take away carry bit from T[i] */
- *tmpc++ &= MP_MASK;
- }
+ c->dp[i] &= MP_MASK;
}
+ }
- /* add carry */
- *tmpc++ = u;
+ /* add carry */
+ c->dp[i] = u;
- /* clear digits above oldused */
- MP_ZERO_DIGITS(tmpc, olduse - c->used);
- }
+ /* clear digits above oldused */
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
mp_clamp(c);
return MP_OKAY;
diff --git a/s_mp_balance_mul.c b/s_mp_balance_mul.c
deleted file mode 100644
index 4108830..0000000
--- a/s_mp_balance_mul.c
+++ /dev/null
@@ -1,81 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_BALANCE_MUL_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* single-digit multiplication with the smaller number as the single-digit */
-mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
-{
- int count, len_a, len_b, nblocks, i, j, bsize;
- mp_int a0, tmp, A, B, r;
- mp_err err;
-
- len_a = a->used;
- len_b = b->used;
-
- nblocks = MP_MAX(a->used, b->used) / MP_MIN(a->used, b->used);
- bsize = MP_MIN(a->used, b->used) ;
-
- if ((err = mp_init_size(&a0, bsize + 2)) != MP_OKAY) {
- return err;
- }
- if ((err = mp_init_multi(&tmp, &r, NULL)) != MP_OKAY) {
- mp_clear(&a0);
- return err;
- }
-
- /* Make sure that A is the larger one*/
- if (len_a < len_b) {
- B = *a;
- A = *b;
- } else {
- A = *a;
- B = *b;
- }
-
- for (i = 0, j=0; i < nblocks; i++) {
- /* Cut a slice off of a */
- a0.used = 0;
- for (count = 0; count < bsize; count++) {
- a0.dp[count] = A.dp[ j++ ];
- a0.used++;
- }
- mp_clamp(&a0);
- /* Multiply with b */
- if ((err = mp_mul(&a0, &B, &tmp)) != MP_OKAY) {
- goto LBL_ERR;
- }
- /* Shift tmp to the correct position */
- if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
- goto LBL_ERR;
- }
- /* Add to output. No carry needed */
- if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
- goto LBL_ERR;
- }
- }
- /* The left-overs; there are always left-overs */
- if (j < A.used) {
- a0.used = 0;
- for (count = 0; j < A.used; count++) {
- a0.dp[count] = A.dp[ j++ ];
- a0.used++;
- }
- mp_clamp(&a0);
- if ((err = mp_mul(&a0, &B, &tmp)) != MP_OKAY) {
- goto LBL_ERR;
- }
- if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
- goto LBL_ERR;
- }
- if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
- goto LBL_ERR;
- }
- }
-
- mp_exch(&r,c);
-LBL_ERR:
- mp_clear_multi(&a0, &tmp, &r,NULL);
- return err;
-}
-#endif
diff --git a/s_mp_copy_digs.c b/s_mp_copy_digs.c
new file mode 100644
index 0000000..4079c33
--- /dev/null
+++ b/s_mp_copy_digs.c
@@ -0,0 +1,23 @@
+#include "tommath_private.h"
+#ifdef S_MP_COPY_DIGS_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+#ifdef MP_USE_MEMOPS
+# include <string.h>
+#endif
+
+void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits)
+{
+#ifdef MP_USE_MEMOPS
+ if (digits > 0) {
+ memcpy(d, s, (size_t)digits * sizeof(mp_digit));
+ }
+#else
+ while (digits-- > 0) {
+ *d++ = *s++;
+ }
+#endif
+}
+
+#endif
diff --git a/s_mp_div_recursive.c b/s_mp_div_recursive.c
index 7007aef..2fbc312 100644
--- a/s_mp_div_recursive.c
+++ b/s_mp_div_recursive.c
@@ -20,7 +20,7 @@ static mp_err s_mp_recursion(const mp_int *a, const mp_int *b, mp_int *q, mp_int
mp_int A1, A2, B1, B0, Q1, Q0, R1, R0, t;
int m = a->used - b->used, k = m/2;
- if (m < MP_KARATSUBA_MUL_CUTOFF) {
+ if (m < MP_MUL_KARATSUBA_CUTOFF) {
return s_mp_div_school(a, b, q, r);
}
@@ -104,7 +104,7 @@ mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r
Vid. section 2.3.
*/
- m = MP_KARATSUBA_MUL_CUTOFF;
+ m = MP_MUL_KARATSUBA_CUTOFF;
while (m <= b->used) {
m <<= 1;
}
diff --git a/s_mp_exptmod_fast.c b/s_mp_exptmod_fast.c
index d581129..813eef2 100644
--- a/s_mp_exptmod_fast.c
+++ b/s_mp_exptmod_fast.c
@@ -80,10 +80,10 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i
}
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
- if (MP_HAS(S_MP_MONTGOMERY_REDUCE_FAST) &&
+ if (MP_HAS(S_MP_MONTGOMERY_REDUCE_COMBA) &&
(((P->used * 2) + 1) < MP_WARRAY) &&
(P->used < MP_MAXFAST)) {
- redux = s_mp_montgomery_reduce_fast;
+ redux = s_mp_montgomery_reduce_comba;
} else if (MP_HAS(MP_MONTGOMERY_REDUCE)) {
/* use slower baseline Montgomery method */
redux = mp_montgomery_reduce;
diff --git a/s_mp_get_bit.c b/s_mp_get_bit.c
index f077f61..a509bce 100644
--- a/s_mp_get_bit.c
+++ b/s_mp_get_bit.c
@@ -5,12 +5,12 @@
/* SPDX-License-Identifier: Unlicense */
/* Get bit at position b and return true if the bit is 1, false if it is 0 */
-bool s_mp_get_bit(const mp_int *a, unsigned int b)
+bool s_mp_get_bit(const mp_int *a, int b)
{
mp_digit bit;
- int limb = (int)(b / MP_DIGIT_BIT);
+ int limb = b / MP_DIGIT_BIT;
- if (limb >= a->used) {
+ if (limb < 0 || limb >= a->used) {
return false;
}
diff --git a/s_mp_invmod.c b/s_mp_invmod.c
new file mode 100644
index 0000000..f3b3f43
--- /dev/null
+++ b/s_mp_invmod.c
@@ -0,0 +1,117 @@
+#include "tommath_private.h"
+#ifdef S_MP_INVMOD_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* hac 14.61, pp608 */
+mp_err s_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
+{
+ mp_int x, y, u, v, A, B, C, D;
+ mp_err err;
+
+ /* b cannot be negative */
+ if ((b->sign == MP_NEG) || mp_iszero(b)) {
+ return MP_VAL;
+ }
+
+ /* init temps */
+ if ((err = mp_init_multi(&x, &y, &u, &v,
+ &A, &B, &C, &D, NULL)) != MP_OKAY) {
+ return err;
+ }
+
+ /* x = a, y = b */
+ if ((err = mp_mod(a, b, &x)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_copy(b, &y)) != MP_OKAY) goto LBL_ERR;
+
+ /* 2. [modified] if x,y are both even then return an error! */
+ if (mp_iseven(&x) && mp_iseven(&y)) {
+ err = MP_VAL;
+ goto LBL_ERR;
+ }
+
+ /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
+ if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
+ mp_set(&A, 1uL);
+ mp_set(&D, 1uL);
+
+ do {
+ /* 4. while u is even do */
+ while (mp_iseven(&u)) {
+ /* 4.1 u = u/2 */
+ if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
+
+ /* 4.2 if A or B is odd then */
+ if (mp_isodd(&A) || mp_isodd(&B)) {
+ /* A = (A+y)/2, B = (B-x)/2 */
+ if ((err = mp_add(&A, &y, &A)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
+ }
+ /* A = A/2, B = B/2 */
+ if ((err = mp_div_2(&A, &A)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* 5. while v is even do */
+ while (mp_iseven(&v)) {
+ /* 5.1 v = v/2 */
+ if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
+
+ /* 5.2 if C or D is odd then */
+ if (mp_isodd(&C) || mp_isodd(&D)) {
+ /* C = (C+y)/2, D = (D-x)/2 */
+ if ((err = mp_add(&C, &y, &C)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+ /* C = C/2, D = D/2 */
+ if ((err = mp_div_2(&C, &C)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* 6. if u >= v then */
+ if (mp_cmp(&u, &v) != MP_LT) {
+ /* u = u - v, A = A - C, B = B - D */
+ if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
+
+ if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) goto LBL_ERR;
+
+ if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
+ } else {
+ /* v - v - u, C = C - A, D = D - B */
+ if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
+
+ if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) goto LBL_ERR;
+
+ if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* if not zero goto step 4 */
+ } while (!mp_iszero(&u));
+
+ /* now a = C, b = D, gcd == g*v */
+
+ /* if v != 1 then there is no inverse */
+ if (mp_cmp_d(&v, 1uL) != MP_EQ) {
+ err = MP_VAL;
+ goto LBL_ERR;
+ }
+
+ /* if its too low */
+ while (mp_cmp_d(&C, 0uL) == MP_LT) {
+ if ((err = mp_add(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* too big */
+ while (mp_cmp_mag(&C, b) != MP_LT) {
+ if ((err = mp_sub(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* C is now the inverse */
+ mp_exch(&C, c);
+
+LBL_ERR:
+ mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
+ return err;
+}
+#endif
diff --git a/s_mp_invmod_fast.c b/s_mp_invmod_fast.c
deleted file mode 100644
index ed1fc4a..0000000
--- a/s_mp_invmod_fast.c
+++ /dev/null
@@ -1,118 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_INVMOD_FAST_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* computes the modular inverse via binary extended euclidean algorithm,
- * that is c = 1/a mod b
- *
- * Based on slow invmod except this is optimized for the case where b is
- * odd as per HAC Note 14.64 on pp. 610
- */
-mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c)
-{
- mp_int x, y, u, v, B, D;
- mp_sign neg;
- mp_err err;
-
- /* 2. [modified] b must be odd */
- if (mp_iseven(b)) {
- return MP_VAL;
- }
-
- /* init all our temps */
- if ((err = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
- return err;
- }
-
- /* x == modulus, y == value to invert */
- if ((err = mp_copy(b, &x)) != MP_OKAY) goto LBL_ERR;
-
- /* we need y = |a| */
- if ((err = mp_mod(a, b, &y)) != MP_OKAY) goto LBL_ERR;
-
- /* if one of x,y is zero return an error! */
- if (mp_iszero(&x) || mp_iszero(&y)) {
- err = MP_VAL;
- goto LBL_ERR;
- }
-
- /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
- if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
- mp_set(&D, 1uL);
-
-top:
- /* 4. while u is even do */
- while (mp_iseven(&u)) {
- /* 4.1 u = u/2 */
- if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
-
- /* 4.2 if B is odd then */
- if (mp_isodd(&B)) {
- if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
- }
- /* B = B/2 */
- if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* 5. while v is even do */
- while (mp_iseven(&v)) {
- /* 5.1 v = v/2 */
- if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
-
- /* 5.2 if D is odd then */
- if (mp_isodd(&D)) {
- /* D = (D-x)/2 */
- if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
- }
- /* D = D/2 */
- if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* 6. if u >= v then */
- if (mp_cmp(&u, &v) != MP_LT) {
- /* u = u - v, B = B - D */
- if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
-
- if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
- } else {
- /* v - v - u, D = D - B */
- if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
-
- if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* if not zero goto step 4 */
- if (!mp_iszero(&u)) {
- goto top;
- }
-
- /* now a = C, b = D, gcd == g*v */
-
- /* if v != 1 then there is no inverse */
- if (mp_cmp_d(&v, 1uL) != MP_EQ) {
- err = MP_VAL;
- goto LBL_ERR;
- }
-
- /* b is now the inverse */
- neg = a->sign;
- while (D.sign == MP_NEG) {
- if ((err = mp_add(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* too big */
- while (mp_cmp_mag(&D, b) != MP_LT) {
- if ((err = mp_sub(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
- }
-
- mp_exch(&D, c);
- c->sign = neg;
- err = MP_OKAY;
-
-LBL_ERR:
- mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
- return err;
-}
-#endif
diff --git a/s_mp_invmod_odd.c b/s_mp_invmod_odd.c
new file mode 100644
index 0000000..8b55d5b
--- /dev/null
+++ b/s_mp_invmod_odd.c
@@ -0,0 +1,116 @@
+#include "tommath_private.h"
+#ifdef S_MP_INVMOD_ODD_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* computes the modular inverse via binary extended euclidean algorithm,
+ * that is c = 1/a mod b
+ *
+ * Based on slow invmod except this is optimized for the case where b is
+ * odd as per HAC Note 14.64 on pp. 610
+ */
+mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
+{
+ mp_int x, y, u, v, B, D;
+ mp_sign neg;
+ mp_err err;
+
+ /* 2. [modified] b must be odd */
+ if (mp_iseven(b)) {
+ return MP_VAL;
+ }
+
+ /* init all our temps */
+ if ((err = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
+ return err;
+ }
+
+ /* x == modulus, y == value to invert */
+ if ((err = mp_copy(b, &x)) != MP_OKAY) goto LBL_ERR;
+
+ /* we need y = |a| */
+ if ((err = mp_mod(a, b, &y)) != MP_OKAY) goto LBL_ERR;
+
+ /* if one of x,y is zero return an error! */
+ if (mp_iszero(&x) || mp_iszero(&y)) {
+ err = MP_VAL;
+ goto LBL_ERR;
+ }
+
+ /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
+ if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
+ mp_set(&D, 1uL);
+
+ do {
+ /* 4. while u is even do */
+ while (mp_iseven(&u)) {
+ /* 4.1 u = u/2 */
+ if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
+
+ /* 4.2 if B is odd then */
+ if (mp_isodd(&B)) {
+ if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
+ }
+ /* B = B/2 */
+ if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* 5. while v is even do */
+ while (mp_iseven(&v)) {
+ /* 5.1 v = v/2 */
+ if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
+
+ /* 5.2 if D is odd then */
+ if (mp_isodd(&D)) {
+ /* D = (D-x)/2 */
+ if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+ /* D = D/2 */
+ if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* 6. if u >= v then */
+ if (mp_cmp(&u, &v) != MP_LT) {
+ /* u = u - v, B = B - D */
+ if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
+
+ if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
+ } else {
+ /* v - v - u, D = D - B */
+ if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
+
+ if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* if not zero goto step 4 */
+ } while (!mp_iszero(&u));
+
+ /* now a = C, b = D, gcd == g*v */
+
+ /* if v != 1 then there is no inverse */
+ if (mp_cmp_d(&v, 1uL) != MP_EQ) {
+ err = MP_VAL;
+ goto LBL_ERR;
+ }
+
+ /* b is now the inverse */
+ neg = a->sign;
+ while (D.sign == MP_NEG) {
+ if ((err = mp_add(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ /* too big */
+ while (mp_cmp_mag(&D, b) != MP_LT) {
+ if ((err = mp_sub(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
+ }
+
+ mp_exch(&D, c);
+ c->sign = neg;
+ err = MP_OKAY;
+
+LBL_ERR:
+ mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
+ return err;
+}
+#endif
diff --git a/s_mp_invmod_slow.c b/s_mp_invmod_slow.c
deleted file mode 100644
index 28cd6cd..0000000
--- a/s_mp_invmod_slow.c
+++ /dev/null
@@ -1,119 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_INVMOD_SLOW_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* hac 14.61, pp608 */
-mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
-{
- mp_int x, y, u, v, A, B, C, D;
- mp_err err;
-
- /* b cannot be negative */
- if ((b->sign == MP_NEG) || mp_iszero(b)) {
- return MP_VAL;
- }
-
- /* init temps */
- if ((err = mp_init_multi(&x, &y, &u, &v,
- &A, &B, &C, &D, NULL)) != MP_OKAY) {
- return err;
- }
-
- /* x = a, y = b */
- if ((err = mp_mod(a, b, &x)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_copy(b, &y)) != MP_OKAY) goto LBL_ERR;
-
- /* 2. [modified] if x,y are both even then return an error! */
- if (mp_iseven(&x) && mp_iseven(&y)) {
- err = MP_VAL;
- goto LBL_ERR;
- }
-
- /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
- if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
- mp_set(&A, 1uL);
- mp_set(&D, 1uL);
-
-top:
- /* 4. while u is even do */
- while (mp_iseven(&u)) {
- /* 4.1 u = u/2 */
- if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
-
- /* 4.2 if A or B is odd then */
- if (mp_isodd(&A) || mp_isodd(&B)) {
- /* A = (A+y)/2, B = (B-x)/2 */
- if ((err = mp_add(&A, &y, &A)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
- }
- /* A = A/2, B = B/2 */
- if ((err = mp_div_2(&A, &A)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* 5. while v is even do */
- while (mp_iseven(&v)) {
- /* 5.1 v = v/2 */
- if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
-
- /* 5.2 if C or D is odd then */
- if (mp_isodd(&C) || mp_isodd(&D)) {
- /* C = (C+y)/2, D = (D-x)/2 */
- if ((err = mp_add(&C, &y, &C)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
- }
- /* C = C/2, D = D/2 */
- if ((err = mp_div_2(&C, &C)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* 6. if u >= v then */
- if (mp_cmp(&u, &v) != MP_LT) {
- /* u = u - v, A = A - C, B = B - D */
- if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
-
- if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) goto LBL_ERR;
-
- if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
- } else {
- /* v - v - u, C = C - A, D = D - B */
- if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
-
- if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) goto LBL_ERR;
-
- if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* if not zero goto step 4 */
- if (!mp_iszero(&u)) {
- goto top;
- }
-
- /* now a = C, b = D, gcd == g*v */
-
- /* if v != 1 then there is no inverse */
- if (mp_cmp_d(&v, 1uL) != MP_EQ) {
- err = MP_VAL;
- goto LBL_ERR;
- }
-
- /* if its too low */
- while (mp_cmp_d(&C, 0uL) == MP_LT) {
- if ((err = mp_add(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* too big */
- while (mp_cmp_mag(&C, b) != MP_LT) {
- if ((err = mp_sub(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
- }
-
- /* C is now the inverse */
- mp_exch(&C, c);
- err = MP_OKAY;
-LBL_ERR:
- mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
- return err;
-}
-#endif
diff --git a/s_mp_karatsuba_mul.c b/s_mp_karatsuba_mul.c
deleted file mode 100644
index df3daa7..0000000
--- a/s_mp_karatsuba_mul.c
+++ /dev/null
@@ -1,174 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_KARATSUBA_MUL_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* c = |a| * |b| using Karatsuba Multiplication using
- * three half size multiplications
- *
- * Let B represent the radix [e.g. 2**MP_DIGIT_BIT] and
- * let n represent half of the number of digits in
- * the min(a,b)
- *
- * a = a1 * B**n + a0
- * b = b1 * B**n + b0
- *
- * Then, a * b =>
- a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
- *
- * Note that a1b1 and a0b0 are used twice and only need to be
- * computed once. So in total three half size (half # of
- * digit) multiplications are performed, a0b0, a1b1 and
- * (a1+b1)(a0+b0)
- *
- * Note that a multiplication of half the digits requires
- * 1/4th the number of single precision multiplications so in
- * total after one call 25% of the single precision multiplications
- * are saved. Note also that the call to mp_mul can end up back
- * in this function if the a0, a1, b0, or b1 are above the threshold.
- * This is known as divide-and-conquer and leads to the famous
- * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
- * the standard O(N**2) that the baseline/comba methods use.
- * Generally though the overhead of this method doesn't pay off
- * until a certain size (N ~ 80) is reached.
- */
-mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
-{
- mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
- int B;
- mp_err err = MP_MEM; /* default the return code to an error */
-
- /* min # of digits */
- B = MP_MIN(a->used, b->used);
-
- /* now divide in two */
- B = B >> 1;
-
- /* init copy all the temps */
- if (mp_init_size(&x0, B) != MP_OKAY) {
- goto LBL_ERR;
- }
- if (mp_init_size(&x1, a->used - B) != MP_OKAY) {
- goto X0;
- }
- if (mp_init_size(&y0, B) != MP_OKAY) {
- goto X1;
- }
- if (mp_init_size(&y1, b->used - B) != MP_OKAY) {
- goto Y0;
- }
-
- /* init temps */
- if (mp_init_size(&t1, B * 2) != MP_OKAY) {
- goto Y1;
- }
- if (mp_init_size(&x0y0, B * 2) != MP_OKAY) {
- goto T1;
- }
- if (mp_init_size(&x1y1, B * 2) != MP_OKAY) {
- goto X0Y0;
- }
-
- /* now shift the digits */
- x0.used = y0.used = B;
- x1.used = a->used - B;
- y1.used = b->used - B;
-
- {
- int x;
- mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
-
- /* we copy the digits directly instead of using higher level functions
- * since we also need to shift the digits
- */
- tmpa = a->dp;
- tmpb = b->dp;
-
- tmpx = x0.dp;
- tmpy = y0.dp;
- for (x = 0; x < B; x++) {
- *tmpx++ = *tmpa++;
- *tmpy++ = *tmpb++;
- }
-
- tmpx = x1.dp;
- for (x = B; x < a->used; x++) {
- *tmpx++ = *tmpa++;
- }
-
- tmpy = y1.dp;
- for (x = B; x < b->used; x++) {
- *tmpy++ = *tmpb++;
- }
- }
-
- /* only need to clamp the lower words since by definition the
- * upper words x1/y1 must have a known number of digits
- */
- mp_clamp(&x0);
- mp_clamp(&y0);
-
- /* now calc the products x0y0 and x1y1 */
- /* after this x0 is no longer required, free temp [x0==t2]! */
- if (mp_mul(&x0, &y0, &x0y0) != MP_OKAY) {
- goto X1Y1; /* x0y0 = x0*y0 */
- }
- if (mp_mul(&x1, &y1, &x1y1) != MP_OKAY) {
- goto X1Y1; /* x1y1 = x1*y1 */
- }
-
- /* now calc x1+x0 and y1+y0 */
- if (s_mp_add(&x1, &x0, &t1) != MP_OKAY) {
- goto X1Y1; /* t1 = x1 - x0 */
- }
- if (s_mp_add(&y1, &y0, &x0) != MP_OKAY) {
- goto X1Y1; /* t2 = y1 - y0 */
- }
- if (mp_mul(&t1, &x0, &t1) != MP_OKAY) {
- goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
- }
-
- /* add x0y0 */
- if (mp_add(&x0y0, &x1y1, &x0) != MP_OKAY) {
- goto X1Y1; /* t2 = x0y0 + x1y1 */
- }
- if (s_mp_sub(&t1, &x0, &t1) != MP_OKAY) {
- goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
- }
-
- /* shift by B */
- if (mp_lshd(&t1, B) != MP_OKAY) {
- goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
- }
- if (mp_lshd(&x1y1, B * 2) != MP_OKAY) {
- goto X1Y1; /* x1y1 = x1y1 << 2*B */
- }
-
- if (mp_add(&x0y0, &t1, &t1) != MP_OKAY) {
- goto X1Y1; /* t1 = x0y0 + t1 */
- }
- if (mp_add(&t1, &x1y1, c) != MP_OKAY) {
- goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
- }
-
- /* Algorithm succeeded set the return code to MP_OKAY */
- err = MP_OKAY;
-
-X1Y1:
- mp_clear(&x1y1);
-X0Y0:
- mp_clear(&x0y0);
-T1:
- mp_clear(&t1);
-Y1:
- mp_clear(&y1);
-Y0:
- mp_clear(&y0);
-X1:
- mp_clear(&x1);
-X0:
- mp_clear(&x0);
-LBL_ERR:
- return err;
-}
-#endif
diff --git a/s_mp_karatsuba_sqr.c b/s_mp_karatsuba_sqr.c
deleted file mode 100644
index 7f22842..0000000
--- a/s_mp_karatsuba_sqr.c
+++ /dev/null
@@ -1,110 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_KARATSUBA_SQR_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* Karatsuba squaring, computes b = a*a using three
- * half size squarings
- *
- * See comments of karatsuba_mul for details. It
- * is essentially the same algorithm but merely
- * tuned to perform recursive squarings.
- */
-mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b)
-{
- mp_int x0, x1, t1, t2, x0x0, x1x1;
- int B;
- mp_err err = MP_MEM;
-
- /* min # of digits */
- B = a->used;
-
- /* now divide in two */
- B = B >> 1;
-
- /* init copy all the temps */
- if (mp_init_size(&x0, B) != MP_OKAY)
- goto LBL_ERR;
- if (mp_init_size(&x1, a->used - B) != MP_OKAY)
- goto X0;
-
- /* init temps */
- if (mp_init_size(&t1, a->used * 2) != MP_OKAY)
- goto X1;
- if (mp_init_size(&t2, a->used * 2) != MP_OKAY)
- goto T1;
- if (mp_init_size(&x0x0, B * 2) != MP_OKAY)
- goto T2;
- if (mp_init_size(&x1x1, (a->used - B) * 2) != MP_OKAY)
- goto X0X0;
-
- {
- int x;
- mp_digit *dst, *src;
-
- src = a->dp;
-
- /* now shift the digits */
- dst = x0.dp;
- for (x = 0; x < B; x++) {
- *dst++ = *src++;
- }
-
- dst = x1.dp;
- for (x = B; x < a->used; x++) {
- *dst++ = *src++;
- }
- }
-
- x0.used = B;
- x1.used = a->used - B;
-
- mp_clamp(&x0);
-
- /* now calc the products x0*x0 and x1*x1 */
- if (mp_sqr(&x0, &x0x0) != MP_OKAY)
- goto X1X1; /* x0x0 = x0*x0 */
- if (mp_sqr(&x1, &x1x1) != MP_OKAY)
- goto X1X1; /* x1x1 = x1*x1 */
-
- /* now calc (x1+x0)**2 */
- if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
- goto X1X1; /* t1 = x1 - x0 */
- if (mp_sqr(&t1, &t1) != MP_OKAY)
- goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
-
- /* add x0y0 */
- if (s_mp_add(&x0x0, &x1x1, &t2) != MP_OKAY)
- goto X1X1; /* t2 = x0x0 + x1x1 */
- if (s_mp_sub(&t1, &t2, &t1) != MP_OKAY)
- goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
-
- /* shift by B */
- if (mp_lshd(&t1, B) != MP_OKAY)
- goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
- if (mp_lshd(&x1x1, B * 2) != MP_OKAY)
- goto X1X1; /* x1x1 = x1x1 << 2*B */
-
- if (mp_add(&x0x0, &t1, &t1) != MP_OKAY)
- goto X1X1; /* t1 = x0x0 + t1 */
- if (mp_add(&t1, &x1x1, b) != MP_OKAY)
- goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
-
- err = MP_OKAY;
-
-X1X1:
- mp_clear(&x1x1);
-X0X0:
- mp_clear(&x0x0);
-T2:
- mp_clear(&t2);
-T1:
- mp_clear(&t1);
-X1:
- mp_clear(&x1);
-X0:
- mp_clear(&x0);
-LBL_ERR:
- return err;
-}
-#endif
diff --git a/s_mp_montgomery_reduce_comba.c b/s_mp_montgomery_reduce_comba.c
new file mode 100644
index 0000000..6f249c4
--- /dev/null
+++ b/s_mp_montgomery_reduce_comba.c
@@ -0,0 +1,119 @@
+#include "tommath_private.h"
+#ifdef S_MP_MONTGOMERY_REDUCE_COMBA_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* computes xR**-1 == x (mod N) via Montgomery Reduction
+ *
+ * This is an optimized implementation of montgomery_reduce
+ * which uses the comba method to quickly calculate the columns of the
+ * reduction.
+ *
+ * Based on Algorithm 14.32 on pp.601 of HAC.
+*/
+mp_err s_mp_montgomery_reduce_comba(mp_int *x, const mp_int *n, mp_digit rho)
+{
+ int ix, oldused;
+ mp_err err;
+ mp_word W[MP_WARRAY];
+
+ if (x->used > MP_WARRAY) {
+ return MP_VAL;
+ }
+
+ /* get old used count */
+ oldused = x->used;
+
+ /* grow a as required */
+ if ((err = mp_grow(x, n->used + 1)) != MP_OKAY) {
+ return err;
+ }
+
+ /* first we have to get the digits of the input into
+ * an array of double precision words W[...]
+ */
+
+ /* copy the digits of a into W[0..a->used-1] */
+ for (ix = 0; ix < x->used; ix++) {
+ W[ix] = x->dp[ix];
+ }
+
+ /* zero the high words of W[a->used..m->used*2] */
+ if (ix < ((n->used * 2) + 1)) {
+ s_mp_zero_buf(W + x->used, sizeof(mp_word) * (size_t)(((n->used * 2) + 1) - ix));
+ }
+
+ /* now we proceed to zero successive digits
+ * from the least significant upwards
+ */
+ for (ix = 0; ix < n->used; ix++) {
+ int iy;
+ mp_digit mu;
+
+ /* mu = ai * m' mod b
+ *
+ * We avoid a double precision multiplication (which isn't required)
+ * by casting the value down to a mp_digit. Note this requires
+ * that W[ix-1] have the carry cleared (see after the inner loop)
+ */
+ mu = ((W[ix] & MP_MASK) * rho) & MP_MASK;
+
+ /* a = a + mu * m * b**i
+ *
+ * This is computed in place and on the fly. The multiplication
+ * by b**i is handled by offseting which columns the results
+ * are added to.
+ *
+ * Note the comba method normally doesn't handle carries in the
+ * inner loop In this case we fix the carry from the previous
+ * column since the Montgomery reduction requires digits of the
+ * result (so far) [see above] to work. This is
+ * handled by fixing up one carry after the inner loop. The
+ * carry fixups are done in order so after these loops the
+ * first m->used words of W[] have the carries fixed
+ */
+ for (iy = 0; iy < n->used; iy++) {
+ W[ix + iy] += (mp_word)mu * (mp_word)n->dp[iy];
+ }
+
+ /* now fix carry for next digit, W[ix+1] */
+ W[ix + 1] += W[ix] >> (mp_word)MP_DIGIT_BIT;
+ }
+
+ /* now we have to propagate the carries and
+ * shift the words downward [all those least
+ * significant digits we zeroed].
+ */
+
+ for (; ix < (n->used * 2); ix++) {
+ W[ix + 1] += W[ix] >> (mp_word)MP_DIGIT_BIT;
+ }
+
+ /* copy out, A = A/b**n
+ *
+ * The result is A/b**n but instead of converting from an
+ * array of mp_word to mp_digit than calling mp_rshd
+ * we just copy them in the right order
+ */
+
+ for (ix = 0; ix < (n->used + 1); ix++) {
+ x->dp[ix] = W[n->used + ix] & (mp_word)MP_MASK;
+ }
+
+ /* set the max used */
+ x->used = n->used + 1;
+
+ /* zero oldused digits, if the input a was larger than
+ * m->used+1 we'll have to clear the digits
+ */
+ s_mp_zero_digs(x->dp + x->used, oldused - x->used);
+
+ mp_clamp(x);
+
+ /* if A >= m then A = A - m */
+ if (mp_cmp_mag(x, n) != MP_LT) {
+ return s_mp_sub(x, n, x);
+ }
+ return MP_OKAY;
+}
+#endif
diff --git a/s_mp_montgomery_reduce_fast.c b/s_mp_montgomery_reduce_fast.c
deleted file mode 100644
index 083e7a4..0000000
--- a/s_mp_montgomery_reduce_fast.c
+++ /dev/null
@@ -1,159 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_MONTGOMERY_REDUCE_FAST_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* computes xR**-1 == x (mod N) via Montgomery Reduction
- *
- * This is an optimized implementation of montgomery_reduce
- * which uses the comba method to quickly calculate the columns of the
- * reduction.
- *
- * Based on Algorithm 14.32 on pp.601 of HAC.
-*/
-mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
-{
- int ix, olduse;
- mp_err err;
- mp_word W[MP_WARRAY];
-
- if (x->used > MP_WARRAY) {
- return MP_VAL;
- }
-
- /* get old used count */
- olduse = x->used;
-
- /* grow a as required */
- if (x->alloc < (n->used + 1)) {
- if ((err = mp_grow(x, n->used + 1)) != MP_OKAY) {
- return err;
- }
- }
-
- /* first we have to get the digits of the input into
- * an array of double precision words W[...]
- */
- {
- mp_word *_W;
- mp_digit *tmpx;
-
- /* alias for the W[] array */
- _W = W;
-
- /* alias for the digits of x*/
- tmpx = x->dp;
-
- /* copy the digits of a into W[0..a->used-1] */
- for (ix = 0; ix < x->used; ix++) {
- *_W++ = *tmpx++;
- }
-
- /* zero the high words of W[a->used..m->used*2] */
- if (ix < ((n->used * 2) + 1)) {
- MP_ZERO_BUFFER(_W, sizeof(mp_word) * (size_t)(((n->used * 2) + 1) - ix));
- }
- }
-
- /* now we proceed to zero successive digits
- * from the least significant upwards
- */
- for (ix = 0; ix < n->used; ix++) {
- /* mu = ai * m' mod b
- *
- * We avoid a double precision multiplication (which isn't required)
- * by casting the value down to a mp_digit. Note this requires
- * that W[ix-1] have the carry cleared (see after the inner loop)
- */
- mp_digit mu;
- mu = ((W[ix] & MP_MASK) * rho) & MP_MASK;
-
- /* a = a + mu * m * b**i
- *
- * This is computed in place and on the fly. The multiplication
- * by b**i is handled by offseting which columns the results
- * are added to.
- *
- * Note the comba method normally doesn't handle carries in the
- * inner loop In this case we fix the carry from the previous
- * column since the Montgomery reduction requires digits of the
- * result (so far) [see above] to work. This is
- * handled by fixing up one carry after the inner loop. The
- * carry fixups are done in order so after these loops the
- * first m->used words of W[] have the carries fixed
- */
- {
- int iy;
- mp_digit *tmpn;
- mp_word *_W;
-
- /* alias for the digits of the modulus */
- tmpn = n->dp;
-
- /* Alias for the columns set by an offset of ix */
- _W = W + ix;
-
- /* inner loop */
- for (iy = 0; iy < n->used; iy++) {
- *_W++ += (mp_word)mu * (mp_word)*tmpn++;
- }
- }
-
- /* now fix carry for next digit, W[ix+1] */
- W[ix + 1] += W[ix] >> (mp_word)MP_DIGIT_BIT;
- }
-
- /* now we have to propagate the carries and
- * shift the words downward [all those least
- * significant digits we zeroed].
- */
- {
- mp_digit *tmpx;
- mp_word *_W, *_W1;
-
- /* nox fix rest of carries */
-
- /* alias for current word */
- _W1 = W + ix;
-
- /* alias for next word, where the carry goes */
- _W = W + ++ix;
-
- for (; ix < ((n->used * 2) + 1); ix++) {
- *_W++ += *_W1++ >> (mp_word)MP_DIGIT_BIT;
- }
-
- /* copy out, A = A/b**n
- *
- * The result is A/b**n but instead of converting from an
- * array of mp_word to mp_digit than calling mp_rshd
- * we just copy them in the right order
- */
-
- /* alias for destination word */
- tmpx = x->dp;
-
- /* alias for shifted double precision result */
- _W = W + n->used;
-
- for (ix = 0; ix < (n->used + 1); ix++) {
- *tmpx++ = *_W++ & (mp_word)MP_MASK;
- }
-
- /* zero oldused digits, if the input a was larger than
- * m->used+1 we'll have to clear the digits
- */
- MP_ZERO_DIGITS(tmpx, olduse - ix);
- }
-
- /* set the max used and clamp */
- x->used = n->used + 1;
- mp_clamp(x);
-
- /* if A >= m then A = A - m */
- if (mp_cmp_mag(x, n) != MP_LT) {
- return s_mp_sub(x, n, x);
- }
- return MP_OKAY;
-}
-#endif
diff --git a/s_mp_mul.c b/s_mp_mul.c
new file mode 100644
index 0000000..cd17b99
--- /dev/null
+++ b/s_mp_mul.c
@@ -0,0 +1,61 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* multiplies |a| * |b| and only computes upto digs digits of result
+ * HAC pp. 595, Algorithm 14.12 Modified so you can control how
+ * many digits of output are created.
+ */
+mp_err s_mp_mul(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+{
+ mp_int t;
+ mp_err err;
+ int pa, ix;
+
+ /* can we use the fast multiplier? */
+ if ((digs < MP_WARRAY) &&
+ (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
+ return s_mp_mul_comba(a, b, c, digs);
+ }
+
+ if ((err = mp_init_size(&t, digs)) != MP_OKAY) {
+ return err;
+ }
+ t.used = digs;
+
+ /* compute the digits of the product directly */
+ pa = a->used;
+ for (ix = 0; ix < pa; ix++) {
+ int iy, pb;
+ mp_digit u = 0;
+
+ /* limit ourselves to making digs digits of output */
+ pb = MP_MIN(b->used, digs - ix);
+
+ /* compute the columns of the output and propagate the carry */
+ for (iy = 0; iy < pb; iy++) {
+ /* compute the column as a mp_word */
+ mp_word r = (mp_word)t.dp[ix + iy] +
+ ((mp_word)a->dp[ix] * (mp_word)b->dp[iy]) +
+ (mp_word)u;
+
+ /* the new column is the lower part of the result */
+ t.dp[ix + iy] = (mp_digit)(r & (mp_word)MP_MASK);
+
+ /* get the carry word from the result */
+ u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
+ }
+ /* set carry if it is placed below digs */
+ if ((ix + iy) < digs) {
+ t.dp[ix + pb] = u;
+ }
+ }
+
+ mp_clamp(&t);
+ mp_exch(&t, c);
+
+ mp_clear(&t);
+ return MP_OKAY;
+}
+#endif
diff --git a/s_mp_mul_balance.c b/s_mp_mul_balance.c
new file mode 100644
index 0000000..f36f0d3
--- /dev/null
+++ b/s_mp_mul_balance.c
@@ -0,0 +1,71 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_BALANCE_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* single-digit multiplication with the smaller number as the single-digit */
+mp_err s_mp_mul_balance(const mp_int *a, const mp_int *b, mp_int *c)
+{
+ mp_int a0, tmp, r;
+ mp_err err;
+ int i, j,
+ nblocks = MP_MAX(a->used, b->used) / MP_MIN(a->used, b->used),
+ bsize = MP_MIN(a->used, b->used);
+
+ if ((err = mp_init_size(&a0, bsize + 2)) != MP_OKAY) {
+ return err;
+ }
+ if ((err = mp_init_multi(&tmp, &r, NULL)) != MP_OKAY) {
+ mp_clear(&a0);
+ return err;
+ }
+
+ /* Make sure that A is the larger one*/
+ if (a->used < b->used) {
+ MP_EXCH(const mp_int *, a, b);
+ }
+
+ for (i = 0, j=0; i < nblocks; i++) {
+ /* Cut a slice off of a */
+ a0.used = bsize;
+ s_mp_copy_digs(a0.dp, a->dp + j, a0.used);
+ j += a0.used;
+ mp_clamp(&a0);
+
+ /* Multiply with b */
+ if ((err = mp_mul(&a0, b, &tmp)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ /* Shift tmp to the correct position */
+ if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ /* Add to output. No carry needed */
+ if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ }
+ /* The left-overs; there are always left-overs */
+ if (j < a->used) {
+ a0.used = a->used - j;
+ s_mp_copy_digs(a0.dp, a->dp + j, a0.used);
+ j += a0.used;
+ mp_clamp(&a0);
+
+ if ((err = mp_mul(&a0, b, &tmp)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ }
+
+ mp_exch(&r,c);
+LBL_ERR:
+ mp_clear_multi(&a0, &tmp, &r,NULL);
+ return err;
+}
+#endif
diff --git a/s_mp_mul_comba.c b/s_mp_mul_comba.c
new file mode 100644
index 0000000..07dd791
--- /dev/null
+++ b/s_mp_mul_comba.c
@@ -0,0 +1,78 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_COMBA_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* Fast (comba) multiplier
+ *
+ * This is the fast column-array [comba] multiplier. It is
+ * designed to compute the columns of the product first
+ * then handle the carries afterwards. This has the effect
+ * of making the nested loops that compute the columns very
+ * simple and schedulable on super-scalar processors.
+ *
+ * This has been modified to produce a variable number of
+ * digits of output so if say only a half-product is required
+ * you don't have to compute the upper half (a feature
+ * required for fast Barrett reduction).
+ *
+ * Based on Algorithm 14.12 on pp.595 of HAC.
+ *
+ */
+mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+{
+ int oldused, pa, ix;
+ mp_err err;
+ mp_digit W[MP_WARRAY];
+ mp_word _W;
+
+ /* grow the destination as required */
+ if ((err = mp_grow(c, digs)) != MP_OKAY) {
+ return err;
+ }
+
+ /* number of output digits to produce */
+ pa = MP_MIN(digs, a->used + b->used);
+
+ /* clear the carry */
+ _W = 0;
+ for (ix = 0; ix < pa; ix++) {
+ int tx, ty, iy, iz;
+
+ /* get offsets into the two bignums */
+ ty = MP_MIN(b->used-1, ix);
+ tx = ix - ty;
+
+ /* this is the number of times the loop will iterrate, essentially
+ while (tx++ < a->used && ty-- >= 0) { ... }
+ */
+ iy = MP_MIN(a->used-tx, ty+1);
+
+ /* execute loop */
+ for (iz = 0; iz < iy; ++iz) {
+ _W += (mp_word)a->dp[tx + iz] * (mp_word)b->dp[ty - iz];
+ }
+
+ /* store term */
+ W[ix] = (mp_digit)_W & MP_MASK;
+
+ /* make next carry */
+ _W = _W >> (mp_word)MP_DIGIT_BIT;
+ }
+
+ /* setup dest */
+ oldused = c->used;
+ c->used = pa;
+
+ for (ix = 0; ix < pa; ix++) {
+ /* now extract the previous digit [below the carry] */
+ c->dp[ix] = W[ix];
+ }
+
+ /* clear unused digits [that existed in the old copy of c] */
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
+
+ mp_clamp(c);
+ return MP_OKAY;
+}
+#endif
diff --git a/s_mp_mul_digs.c b/s_mp_mul_digs.c
deleted file mode 100644
index ea0985b..0000000
--- a/s_mp_mul_digs.c
+++ /dev/null
@@ -1,74 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_MUL_DIGS_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* multiplies |a| * |b| and only computes upto digs digits of result
- * HAC pp. 595, Algorithm 14.12 Modified so you can control how
- * many digits of output are created.
- */
-mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
-{
- mp_int t;
- mp_err err;
- int pa, pb, ix, iy;
- mp_digit u;
- mp_word r;
- mp_digit tmpx, *tmpt, *tmpy;
-
- /* can we use the fast multiplier? */
- if ((digs < MP_WARRAY) &&
- (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
- return s_mp_mul_digs_fast(a, b, c, digs);
- }
-
- if ((err = mp_init_size(&t, digs)) != MP_OKAY) {
- return err;
- }
- t.used = digs;
-
- /* compute the digits of the product directly */
- pa = a->used;
- for (ix = 0; ix < pa; ix++) {
- /* set the carry to zero */
- u = 0;
-
- /* limit ourselves to making digs digits of output */
- pb = MP_MIN(b->used, digs - ix);
-
- /* setup some aliases */
- /* copy of the digit from a used within the nested loop */
- tmpx = a->dp[ix];
-
- /* an alias for the destination shifted ix places */
- tmpt = t.dp + ix;
-
- /* an alias for the digits of b */
- tmpy = b->dp;
-
- /* compute the columns of the output and propagate the carry */
- for (iy = 0; iy < pb; iy++) {
- /* compute the column as a mp_word */
- r = (mp_word)*tmpt +
- ((mp_word)tmpx * (mp_word)*tmpy++) +
- (mp_word)u;
-
- /* the new column is the lower part of the result */
- *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
-
- /* get the carry word from the result */
- u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
- }
- /* set carry if it is placed below digs */
- if ((ix + iy) < digs) {
- *tmpt = u;
- }
- }
-
- mp_clamp(&t);
- mp_exch(&t, c);
-
- mp_clear(&t);
- return MP_OKAY;
-}
-#endif
diff --git a/s_mp_mul_digs_fast.c b/s_mp_mul_digs_fast.c
deleted file mode 100644
index 8988838..0000000
--- a/s_mp_mul_digs_fast.c
+++ /dev/null
@@ -1,90 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_MUL_DIGS_FAST_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* Fast (comba) multiplier
- *
- * This is the fast column-array [comba] multiplier. It is
- * designed to compute the columns of the product first
- * then handle the carries afterwards. This has the effect
- * of making the nested loops that compute the columns very
- * simple and schedulable on super-scalar processors.
- *
- * This has been modified to produce a variable number of
- * digits of output so if say only a half-product is required
- * you don't have to compute the upper half (a feature
- * required for fast Barrett reduction).
- *
- * Based on Algorithm 14.12 on pp.595 of HAC.
- *
- */
-mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
-{
- int olduse, pa, ix, iz;
- mp_err err;
- mp_digit W[MP_WARRAY];
- mp_word _W;
-
- /* grow the destination as required */
- if (c->alloc < digs) {
- if ((err = mp_grow(c, digs)) != MP_OKAY) {
- return err;
- }
- }
-
- /* number of output digits to produce */
- pa = MP_MIN(digs, a->used + b->used);
-
- /* clear the carry */
- _W = 0;
- for (ix = 0; ix < pa; ix++) {
- int tx, ty;
- int iy;
- mp_digit *tmpx, *tmpy;
-
- /* get offsets into the two bignums */
- ty = MP_MIN(b->used-1, ix);
- tx = ix - ty;
-
- /* setup temp aliases */
- tmpx = a->dp + tx;
- tmpy = b->dp + ty;
-
- /* this is the number of times the loop will iterrate, essentially
- while (tx++ < a->used && ty-- >= 0) { ... }
- */
- iy = MP_MIN(a->used-tx, ty+1);
-
- /* execute loop */
- for (iz = 0; iz < iy; ++iz) {
- _W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
-
- }
-
- /* store term */
- W[ix] = (mp_digit)_W & MP_MASK;
-
- /* make next carry */
- _W = _W >> (mp_word)MP_DIGIT_BIT;
- }
-
- /* setup dest */
- olduse = c->used;
- c->used = pa;
-
- {
- mp_digit *tmpc;
- tmpc = c->dp;
- for (ix = 0; ix < pa; ix++) {
- /* now extract the previous digit [below the carry] */
- *tmpc++ = W[ix];
- }
-
- /* clear unused digits [that existed in the old copy of c] */
- MP_ZERO_DIGITS(tmpc, olduse - ix);
- }
- mp_clamp(c);
- return MP_OKAY;
-}
-#endif
diff --git a/s_mp_mul_high.c b/s_mp_mul_high.c
new file mode 100644
index 0000000..d1d1806
--- /dev/null
+++ b/s_mp_mul_high.c
@@ -0,0 +1,52 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_HIGH_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* multiplies |a| * |b| and does not compute the lower digs digits
+ * [meant to get the higher part of the product]
+ */
+mp_err s_mp_mul_high(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+{
+ mp_int t;
+ int pa, pb, ix;
+ mp_err err;
+
+ /* can we use the fast multiplier? */
+ if (MP_HAS(S_MP_MUL_HIGH_COMBA)
+ && ((a->used + b->used + 1) < MP_WARRAY)
+ && (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
+ return s_mp_mul_high_comba(a, b, c, digs);
+ }
+
+ if ((err = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) {
+ return err;
+ }
+ t.used = a->used + b->used + 1;
+
+ pa = a->used;
+ pb = b->used;
+ for (ix = 0; ix < pa; ix++) {
+ int iy;
+ mp_digit u = 0;
+
+ for (iy = digs - ix; iy < pb; iy++) {
+ /* calculate the double precision result */
+ mp_word r = (mp_word)t.dp[ix + iy] +
+ ((mp_word)a->dp[ix] * (mp_word)b->dp[iy]) +
+ (mp_word)u;
+
+ /* get the lower part */
+ t.dp[ix + iy] = (mp_digit)(r & (mp_word)MP_MASK);
+
+ /* carry the carry */
+ u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
+ }
+ t.dp[ix + pb] = u;
+ }
+ mp_clamp(&t);
+ mp_exch(&t, c);
+ mp_clear(&t);
+ return MP_OKAY;
+}
+#endif
diff --git a/s_mp_mul_high_comba.c b/s_mp_mul_high_comba.c
new file mode 100644
index 0000000..317346d
--- /dev/null
+++ b/s_mp_mul_high_comba.c
@@ -0,0 +1,70 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_HIGH_COMBA_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* this is a modified version of s_mp_mul_comba that only produces
+ * output digits *above* digs. See the comments for s_mp_mul_comba
+ * to see how it works.
+ *
+ * This is used in the Barrett reduction since for one of the multiplications
+ * only the higher digits were needed. This essentially halves the work.
+ *
+ * Based on Algorithm 14.12 on pp.595 of HAC.
+ */
+mp_err s_mp_mul_high_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+{
+ int oldused, pa, ix;
+ mp_err err;
+ mp_digit W[MP_WARRAY];
+ mp_word _W;
+
+ /* grow the destination as required */
+ pa = a->used + b->used;
+ if ((err = mp_grow(c, pa)) != MP_OKAY) {
+ return err;
+ }
+
+ /* number of output digits to produce */
+ pa = a->used + b->used;
+ _W = 0;
+ for (ix = digs; ix < pa; ix++) {
+ int tx, ty, iy, iz;
+
+ /* get offsets into the two bignums */
+ ty = MP_MIN(b->used-1, ix);
+ tx = ix - ty;
+
+ /* this is the number of times the loop will iterrate, essentially its
+ while (tx++ < a->used && ty-- >= 0) { ... }
+ */
+ iy = MP_MIN(a->used-tx, ty+1);
+
+ /* execute loop */
+ for (iz = 0; iz < iy; iz++) {
+ _W += (mp_word)a->dp[tx + iz] * (mp_word)b->dp[ty - iz];
+ }
+
+ /* store term */
+ W[ix] = (mp_digit)_W & MP_MASK;
+
+ /* make next carry */
+ _W = _W >> (mp_word)MP_DIGIT_BIT;
+ }
+
+ /* setup dest */
+ oldused = c->used;
+ c->used = pa;
+
+ for (ix = digs; ix < pa; ix++) {
+ /* now extract the previous digit [below the carry] */
+ c->dp[ix] = W[ix];
+ }
+
+ /* clear unused digits [that existed in the old copy of c] */
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
+
+ mp_clamp(c);
+ return MP_OKAY;
+}
+#endif
diff --git a/s_mp_mul_high_digs.c b/s_mp_mul_high_digs.c
deleted file mode 100644
index 87cfbe5..0000000
--- a/s_mp_mul_high_digs.c
+++ /dev/null
@@ -1,64 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_MUL_HIGH_DIGS_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* multiplies |a| * |b| and does not compute the lower digs digits
- * [meant to get the higher part of the product]
- */
-mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
-{
- mp_int t;
- int pa, pb, ix, iy;
- mp_err err;
- mp_digit u;
- mp_word r;
- mp_digit tmpx, *tmpt, *tmpy;
-
- /* can we use the fast multiplier? */
- if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)
- && ((a->used + b->used + 1) < MP_WARRAY)
- && (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
- return s_mp_mul_high_digs_fast(a, b, c, digs);
- }
-
- if ((err = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) {
- return err;
- }
- t.used = a->used + b->used + 1;
-
- pa = a->used;
- pb = b->used;
- for (ix = 0; ix < pa; ix++) {
- /* clear the carry */
- u = 0;
-
- /* left hand side of A[ix] * B[iy] */
- tmpx = a->dp[ix];
-
- /* alias to the address of where the digits will be stored */
- tmpt = &(t.dp[digs]);
-
- /* alias for where to read the right hand side from */
- tmpy = b->dp + (digs - ix);
-
- for (iy = digs - ix; iy < pb; iy++) {
- /* calculate the double precision result */
- r = (mp_word)*tmpt +
- ((mp_word)tmpx * (mp_word)*tmpy++) +
- (mp_word)u;
-
- /* get the lower part */
- *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
-
- /* carry the carry */
- u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
- }
- *tmpt = u;
- }
- mp_clamp(&t);
- mp_exch(&t, c);
- mp_clear(&t);
- return MP_OKAY;
-}
-#endif
diff --git a/s_mp_mul_high_digs_fast.c b/s_mp_mul_high_digs_fast.c
deleted file mode 100644
index 1559ebc..0000000
--- a/s_mp_mul_high_digs_fast.c
+++ /dev/null
@@ -1,81 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_MUL_HIGH_DIGS_FAST_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* this is a modified version of s_mp_mul_digs_fast that only produces
- * output digits *above* digs. See the comments for s_mp_mul_digs_fast
- * to see how it works.
- *
- * This is used in the Barrett reduction since for one of the multiplications
- * only the higher digits were needed. This essentially halves the work.
- *
- * Based on Algorithm 14.12 on pp.595 of HAC.
- */
-mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
-{
- int olduse, pa, ix, iz;
- mp_err err;
- mp_digit W[MP_WARRAY];
- mp_word _W;
-
- /* grow the destination as required */
- pa = a->used + b->used;
- if (c->alloc < pa) {
- if ((err = mp_grow(c, pa)) != MP_OKAY) {
- return err;
- }
- }
-
- /* number of output digits to produce */
- pa = a->used + b->used;
- _W = 0;
- for (ix = digs; ix < pa; ix++) {
- int tx, ty, iy;
- mp_digit *tmpx, *tmpy;
-
- /* get offsets into the two bignums */
- ty = MP_MIN(b->used-1, ix);
- tx = ix - ty;
-
- /* setup temp aliases */
- tmpx = a->dp + tx;
- tmpy = b->dp + ty;
-
- /* this is the number of times the loop will iterrate, essentially its
- while (tx++ < a->used && ty-- >= 0) { ... }
- */
- iy = MP_MIN(a->used-tx, ty+1);
-
- /* execute loop */
- for (iz = 0; iz < iy; iz++) {
- _W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
- }
-
- /* store term */
- W[ix] = (mp_digit)_W & MP_MASK;
-
- /* make next carry */
- _W = _W >> (mp_word)MP_DIGIT_BIT;
- }
-
- /* setup dest */
- olduse = c->used;
- c->used = pa;
-
- {
- mp_digit *tmpc;
-
- tmpc = c->dp + digs;
- for (ix = digs; ix < pa; ix++) {
- /* now extract the previous digit [below the carry] */
- *tmpc++ = W[ix];
- }
-
- /* clear unused digits [that existed in the old copy of c] */
- MP_ZERO_DIGITS(tmpc, olduse - ix);
- }
- mp_clamp(c);
- return MP_OKAY;
-}
-#endif
diff --git a/s_mp_mul_karatsuba.c b/s_mp_mul_karatsuba.c
new file mode 100644
index 0000000..bf9271f
--- /dev/null
+++ b/s_mp_mul_karatsuba.c
@@ -0,0 +1,151 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_KARATSUBA_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* c = |a| * |b| using Karatsuba Multiplication using
+ * three half size multiplications
+ *
+ * Let B represent the radix [e.g. 2**MP_DIGIT_BIT] and
+ * let n represent half of the number of digits in
+ * the min(a,b)
+ *
+ * a = a1 * B**n + a0
+ * b = b1 * B**n + b0
+ *
+ * Then, a * b =>
+ a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
+ *
+ * Note that a1b1 and a0b0 are used twice and only need to be
+ * computed once. So in total three half size (half # of
+ * digit) multiplications are performed, a0b0, a1b1 and
+ * (a1+b1)(a0+b0)
+ *
+ * Note that a multiplication of half the digits requires
+ * 1/4th the number of single precision multiplications so in
+ * total after one call 25% of the single precision multiplications
+ * are saved. Note also that the call to mp_mul can end up back
+ * in this function if the a0, a1, b0, or b1 are above the threshold.
+ * This is known as divide-and-conquer and leads to the famous
+ * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
+ * the standard O(N**2) that the baseline/comba methods use.
+ * Generally though the overhead of this method doesn't pay off
+ * until a certain size (N ~ 80) is reached.
+ */
+mp_err s_mp_mul_karatsuba(const mp_int *a, const mp_int *b, mp_int *c)
+{
+ mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
+ int B;
+ mp_err err;
+
+ /* min # of digits */
+ B = MP_MIN(a->used, b->used);
+
+ /* now divide in two */
+ B = B >> 1;
+
+ /* init copy all the temps */
+ if ((err = mp_init_size(&x0, B)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ if ((err = mp_init_size(&x1, a->used - B)) != MP_OKAY) {
+ goto X0;
+ }
+ if ((err = mp_init_size(&y0, B)) != MP_OKAY) {
+ goto X1;
+ }
+ if ((err = mp_init_size(&y1, b->used - B)) != MP_OKAY) {
+ goto Y0;
+ }
+
+ /* init temps */
+ if ((err = mp_init_size(&t1, B * 2)) != MP_OKAY) {
+ goto Y1;
+ }
+ if ((err = mp_init_size(&x0y0, B * 2)) != MP_OKAY) {
+ goto T1;
+ }
+ if ((err = mp_init_size(&x1y1, B * 2)) != MP_OKAY) {
+ goto X0Y0;
+ }
+
+ /* now shift the digits */
+ x0.used = y0.used = B;
+ x1.used = a->used - B;
+ y1.used = b->used - B;
+
+ /* we copy the digits directly instead of using higher level functions
+ * since we also need to shift the digits
+ */
+ s_mp_copy_digs(x0.dp, a->dp, x0.used);
+ s_mp_copy_digs(y0.dp, b->dp, y0.used);
+ s_mp_copy_digs(x1.dp, a->dp + B, x1.used);
+ s_mp_copy_digs(y1.dp, b->dp + B, y1.used);
+
+ /* only need to clamp the lower words since by definition the
+ * upper words x1/y1 must have a known number of digits
+ */
+ mp_clamp(&x0);
+ mp_clamp(&y0);
+
+ /* now calc the products x0y0 and x1y1 */
+ /* after this x0 is no longer required, free temp [x0==t2]! */
+ if ((err = mp_mul(&x0, &y0, &x0y0)) != MP_OKAY) {
+ goto X1Y1; /* x0y0 = x0*y0 */
+ }
+ if ((err = mp_mul(&x1, &y1, &x1y1)) != MP_OKAY) {
+ goto X1Y1; /* x1y1 = x1*y1 */
+ }
+
+ /* now calc x1+x0 and y1+y0 */
+ if ((err = s_mp_add(&x1, &x0, &t1)) != MP_OKAY) {
+ goto X1Y1; /* t1 = x1 - x0 */
+ }
+ if ((err = s_mp_add(&y1, &y0, &x0)) != MP_OKAY) {
+ goto X1Y1; /* t2 = y1 - y0 */
+ }
+ if ((err = mp_mul(&t1, &x0, &t1)) != MP_OKAY) {
+ goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
+ }
+
+ /* add x0y0 */
+ if ((err = mp_add(&x0y0, &x1y1, &x0)) != MP_OKAY) {
+ goto X1Y1; /* t2 = x0y0 + x1y1 */
+ }
+ if ((err = s_mp_sub(&t1, &x0, &t1)) != MP_OKAY) {
+ goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
+ }
+
+ /* shift by B */
+ if ((err = mp_lshd(&t1, B)) != MP_OKAY) {
+ goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
+ }
+ if ((err = mp_lshd(&x1y1, B * 2)) != MP_OKAY) {
+ goto X1Y1; /* x1y1 = x1y1 << 2*B */
+ }
+
+ if ((err = mp_add(&x0y0, &t1, &t1)) != MP_OKAY) {
+ goto X1Y1; /* t1 = x0y0 + t1 */
+ }
+ if ((err = mp_add(&t1, &x1y1, c)) != MP_OKAY) {
+ goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
+ }
+
+X1Y1:
+ mp_clear(&x1y1);
+X0Y0:
+ mp_clear(&x0y0);
+T1:
+ mp_clear(&t1);
+Y1:
+ mp_clear(&y1);
+Y0:
+ mp_clear(&y0);
+X1:
+ mp_clear(&x1);
+X0:
+ mp_clear(&x0);
+LBL_ERR:
+ return err;
+}
+#endif
diff --git a/s_mp_mul_toom.c b/s_mp_mul_toom.c
new file mode 100644
index 0000000..1ed03c8
--- /dev/null
+++ b/s_mp_mul_toom.c
@@ -0,0 +1,202 @@
+#include "tommath_private.h"
+#ifdef S_MP_MUL_TOOM_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* multiplication using the Toom-Cook 3-way algorithm
+ *
+ * Much more complicated than Karatsuba but has a lower
+ * asymptotic running time of O(N**1.464). This algorithm is
+ * only particularly useful on VERY large inputs
+ * (we're talking 1000s of digits here...).
+*/
+
+/*
+ This file contains code from J. Arndt's book "Matters Computational"
+ and the accompanying FXT-library with permission of the author.
+*/
+
+/*
+ Setup from
+
+ Chung, Jaewook, and M. Anwar Hasan. "Asymmetric squaring formulae."
+ 18th IEEE Symposium on Computer Arithmetic (ARITH'07). IEEE, 2007.
+
+ The interpolation from above needed one temporary variable more
+ than the interpolation here:
+
+ Bodrato, Marco, and Alberto Zanoni. "What about Toom-Cook matrices optimality."
+ Centro Vito Volterra Universita di Roma Tor Vergata (2006)
+*/
+
+mp_err s_mp_mul_toom(const mp_int *a, const mp_int *b, mp_int *c)
+{
+ mp_int S1, S2, T1, a0, a1, a2, b0, b1, b2;
+ int B;
+ mp_err err;
+
+ /* init temps */
+ if ((err = mp_init_multi(&S1, &S2, &T1, NULL)) != MP_OKAY) {
+ return err;
+ }
+
+ /* B */
+ B = MP_MIN(a->used, b->used) / 3;
+
+ /** a = a2 * x^2 + a1 * x + a0; */
+ if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0;
+ if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1;
+ if ((err = mp_init_size(&a2, a->used - 2 * B)) != MP_OKAY) goto LBL_ERRa2;
+
+ a0.used = a1.used = B;
+ a2.used = a->used - 2 * B;
+ s_mp_copy_digs(a0.dp, a->dp, a0.used);
+ s_mp_copy_digs(a1.dp, a->dp + B, a1.used);
+ s_mp_copy_digs(a2.dp, a->dp + 2 * B, a2.used);
+ mp_clamp(&a0);
+ mp_clamp(&a1);
+ mp_clamp(&a2);
+
+ /** b = b2 * x^2 + b1 * x + b0; */
+ if ((err = mp_init_size(&b0, B)) != MP_OKAY) goto LBL_ERRb0;
+ if ((err = mp_init_size(&b1, B)) != MP_OKAY) goto LBL_ERRb1;
+ if ((err = mp_init_size(&b2, b->used - 2 * B)) != MP_OKAY) goto LBL_ERRb2;
+
+ b0.used = b1.used = B;
+ b2.used = b->used - 2 * B;
+ s_mp_copy_digs(b0.dp, b->dp, b0.used);
+ s_mp_copy_digs(b1.dp, b->dp + B, b1.used);
+ s_mp_copy_digs(b2.dp, b->dp + 2 * B, b2.used);
+ mp_clamp(&b0);
+ mp_clamp(&b1);
+ mp_clamp(&b2);
+
+ /** \\ S1 = (a2+a1+a0) * (b2+b1+b0); */
+ /** T1 = a2 + a1; */
+ if ((err = mp_add(&a2, &a1, &T1)) != MP_OKAY) goto LBL_ERR;
+
+ /** S2 = T1 + a0; */
+ if ((err = mp_add(&T1, &a0, &S2)) != MP_OKAY) goto LBL_ERR;
+
+ /** c = b2 + b1; */
+ if ((err = mp_add(&b2, &b1, c)) != MP_OKAY) goto LBL_ERR;
+
+ /** S1 = c + b0; */
+ if ((err = mp_add(c, &b0, &S1)) != MP_OKAY) goto LBL_ERR;
+
+ /** S1 = S1 * S2; */
+ if ((err = mp_mul(&S1, &S2, &S1)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\S2 = (4*a2+2*a1+a0) * (4*b2+2*b1+b0); */
+ /** T1 = T1 + a2; */
+ if ((err = mp_add(&T1, &a2, &T1)) != MP_OKAY) goto LBL_ERR;
+
+ /** T1 = T1 << 1; */
+ if ((err = mp_mul_2(&T1, &T1)) != MP_OKAY) goto LBL_ERR;
+
+ /** T1 = T1 + a0; */
+ if ((err = mp_add(&T1, &a0, &T1)) != MP_OKAY) goto LBL_ERR;
+
+ /** c = c + b2; */
+ if ((err = mp_add(c, &b2, c)) != MP_OKAY) goto LBL_ERR;
+
+ /** c = c << 1; */
+ if ((err = mp_mul_2(c, c)) != MP_OKAY) goto LBL_ERR;
+
+ /** c = c + b0; */
+ if ((err = mp_add(c, &b0, c)) != MP_OKAY) goto LBL_ERR;
+
+ /** S2 = T1 * c; */
+ if ((err = mp_mul(&T1, c, &S2)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\S3 = (a2-a1+a0) * (b2-b1+b0); */
+ /** a1 = a2 - a1; */
+ if ((err = mp_sub(&a2, &a1, &a1)) != MP_OKAY) goto LBL_ERR;
+
+ /** a1 = a1 + a0; */
+ if ((err = mp_add(&a1, &a0, &a1)) != MP_OKAY) goto LBL_ERR;
+
+ /** b1 = b2 - b1; */
+ if ((err = mp_sub(&b2, &b1, &b1)) != MP_OKAY) goto LBL_ERR;
+
+ /** b1 = b1 + b0; */
+ if ((err = mp_add(&b1, &b0, &b1)) != MP_OKAY) goto LBL_ERR;
+
+ /** a1 = a1 * b1; */
+ if ((err = mp_mul(&a1, &b1, &a1)) != MP_OKAY) goto LBL_ERR;
+
+ /** b1 = a2 * b2; */
+ if ((err = mp_mul(&a2, &b2, &b1)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\S2 = (S2 - S3)/3; */
+ /** S2 = S2 - a1; */
+ if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) goto LBL_ERR;
+
+ /** S2 = S2 / 3; \\ this is an exact division */
+ if ((err = mp_div_3(&S2, &S2, NULL)) != MP_OKAY) goto LBL_ERR;
+
+ /** a1 = S1 - a1; */
+ if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) goto LBL_ERR;
+
+ /** a1 = a1 >> 1; */
+ if ((err = mp_div_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR;
+
+ /** a0 = a0 * b0; */
+ if ((err = mp_mul(&a0, &b0, &a0)) != MP_OKAY) goto LBL_ERR;
+
+ /** S1 = S1 - a0; */
+ if ((err = mp_sub(&S1, &a0, &S1)) != MP_OKAY) goto LBL_ERR;
+
+ /** S2 = S2 - S1; */
+ if ((err = mp_sub(&S2, &S1, &S2)) != MP_OKAY) goto LBL_ERR;
+
+ /** S2 = S2 >> 1; */
+ if ((err = mp_div_2(&S2, &S2)) != MP_OKAY) goto LBL_ERR;
+
+ /** S1 = S1 - a1; */
+ if ((err = mp_sub(&S1, &a1, &S1)) != MP_OKAY) goto LBL_ERR;
+
+ /** S1 = S1 - b1; */
+ if ((err = mp_sub(&S1, &b1, &S1)) != MP_OKAY) goto LBL_ERR;
+
+ /** T1 = b1 << 1; */
+ if ((err = mp_mul_2(&b1, &T1)) != MP_OKAY) goto LBL_ERR;
+
+ /** S2 = S2 - T1; */
+ if ((err = mp_sub(&S2, &T1, &S2)) != MP_OKAY) goto LBL_ERR;
+
+ /** a1 = a1 - S2; */
+ if ((err = mp_sub(&a1, &S2, &a1)) != MP_OKAY) goto LBL_ERR;
+
+
+ /** P = b1*x^4+ S2*x^3+ S1*x^2+ a1*x + a0; */
+ if ((err = mp_lshd(&b1, 4 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_lshd(&S2, 3 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(&b1, &S2, &b1)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_lshd(&S1, 2 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(&b1, &S1, &b1)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_lshd(&a1, 1 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(&b1, &a1, &b1)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(&b1, &a0, c)) != MP_OKAY) goto LBL_ERR;
+
+ /** a * b - P */
+
+
+LBL_ERR:
+ mp_clear(&b2);
+LBL_ERRb2:
+ mp_clear(&b1);
+LBL_ERRb1:
+ mp_clear(&b0);
+LBL_ERRb0:
+ mp_clear(&a2);
+LBL_ERRa2:
+ mp_clear(&a1);
+LBL_ERRa1:
+ mp_clear(&a0);
+LBL_ERRa0:
+ mp_clear_multi(&S1, &S2, &T1, NULL);
+ return err;
+}
+
+#endif
diff --git a/s_mp_prime_is_divisible.c b/s_mp_prime_is_divisible.c
index 0cca5a6..63b2405 100644
--- a/s_mp_prime_is_divisible.c
+++ b/s_mp_prime_is_divisible.c
@@ -10,16 +10,12 @@
*/
mp_err s_mp_prime_is_divisible(const mp_int *a, bool *result)
{
- int ix;
- mp_err err;
- mp_digit res;
-
- /* default to not */
- *result = false;
-
- for (ix = 0; ix < MP_PRIME_TAB_SIZE; ix++) {
- /* what is a mod LBL_prime_tab[ix] */
- if ((err = mp_mod_d(a, s_mp_prime_tab[ix], &res)) != MP_OKAY) {
+ int i;
+ for (i = 0; i < MP_PRIME_TAB_SIZE; i++) {
+ /* what is a mod LBL_prime_tab[i] */
+ mp_err err;
+ mp_digit res;
+ if ((err = mp_mod_d(a, s_mp_prime_tab[i], &res)) != MP_OKAY) {
return err;
}
@@ -30,6 +26,8 @@ mp_err s_mp_prime_is_divisible(const mp_int *a, bool *result)
}
}
+ /* default to not */
+ *result = false;
return MP_OKAY;
}
#endif
diff --git a/s_mp_sqr.c b/s_mp_sqr.c
index 61106ed..4a20306 100644
--- a/s_mp_sqr.c
+++ b/s_mp_sqr.c
@@ -7,10 +7,8 @@
mp_err s_mp_sqr(const mp_int *a, mp_int *b)
{
mp_int t;
- int ix, iy, pa;
+ int ix, pa;
mp_err err;
- mp_word r;
- mp_digit u, tmpx, *tmpt;
pa = a->used;
if ((err = mp_init_size(&t, (2 * pa) + 1)) != MP_OKAY) {
@@ -21,10 +19,13 @@ mp_err s_mp_sqr(const mp_int *a, mp_int *b)
t.used = (2 * pa) + 1;
for (ix = 0; ix < pa; ix++) {
+ mp_digit u;
+ int iy;
+
/* first calculate the digit at 2*ix */
/* calculate double precision result */
- r = (mp_word)t.dp[2*ix] +
- ((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
+ mp_word r = (mp_word)t.dp[2*ix] +
+ ((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
/* store lower part in result */
t.dp[ix+ix] = (mp_digit)(r & (mp_word)MP_MASK);
@@ -32,32 +33,27 @@ mp_err s_mp_sqr(const mp_int *a, mp_int *b)
/* get the carry */
u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
- /* left hand side of A[ix] * A[iy] */
- tmpx = a->dp[ix];
-
- /* alias for where to store the results */
- tmpt = t.dp + ((2 * ix) + 1);
-
for (iy = ix + 1; iy < pa; iy++) {
/* first calculate the product */
- r = (mp_word)tmpx * (mp_word)a->dp[iy];
+ r = (mp_word)a->dp[ix] * (mp_word)a->dp[iy];
/* now calculate the double precision result, note we use
* addition instead of *2 since it's easier to optimize
*/
- r = (mp_word)*tmpt + r + r + (mp_word)u;
+ r = (mp_word)t.dp[ix + iy] + r + r + (mp_word)u;
/* store lower part */
- *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
+ t.dp[ix + iy] = (mp_digit)(r & (mp_word)MP_MASK);
/* get carry */
u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
}
/* propagate upwards */
while (u != 0uL) {
- r = (mp_word)*tmpt + (mp_word)u;
- *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
+ r = (mp_word)t.dp[ix + iy] + (mp_word)u;
+ t.dp[ix + iy] = (mp_digit)(r & (mp_word)MP_MASK);
u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
+ ++iy;
}
}
diff --git a/s_mp_sqr_comba.c b/s_mp_sqr_comba.c
new file mode 100644
index 0000000..cb88dcc
--- /dev/null
+++ b/s_mp_sqr_comba.c
@@ -0,0 +1,87 @@
+#include "tommath_private.h"
+#ifdef S_MP_SQR_COMBA_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* the jist of squaring...
+ * you do like mult except the offset of the tmpx [one that
+ * starts closer to zero] can't equal the offset of tmpy.
+ * So basically you set up iy like before then you min it with
+ * (ty-tx) so that it never happens. You double all those
+ * you add in the inner loop
+
+After that loop you do the squares and add them in.
+*/
+
+mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b)
+{
+ int oldused, pa, ix;
+ mp_digit W[MP_WARRAY];
+ mp_word W1;
+ mp_err err;
+
+ /* grow the destination as required */
+ pa = a->used + a->used;
+ if ((err = mp_grow(b, pa)) != MP_OKAY) {
+ return err;
+ }
+
+ /* number of output digits to produce */
+ W1 = 0;
+ for (ix = 0; ix < pa; ix++) {
+ int tx, ty, iy, iz;
+ mp_word _W;
+
+ /* clear counter */
+ _W = 0;
+
+ /* get offsets into the two bignums */
+ ty = MP_MIN(a->used-1, ix);
+ tx = ix - ty;
+
+ /* this is the number of times the loop will iterrate, essentially
+ while (tx++ < a->used && ty-- >= 0) { ... }
+ */
+ iy = MP_MIN(a->used-tx, ty+1);
+
+ /* now for squaring tx can never equal ty
+ * we halve the distance since they approach at a rate of 2x
+ * and we have to round because odd cases need to be executed
+ */
+ iy = MP_MIN(iy, ((ty-tx)+1)>>1);
+
+ /* execute loop */
+ for (iz = 0; iz < iy; iz++) {
+ _W += (mp_word)a->dp[tx + iz] * (mp_word)a->dp[ty - iz];
+ }
+
+ /* double the inner product and add carry */
+ _W = _W + _W + W1;
+
+ /* even columns have the square term in them */
+ if (((unsigned)ix & 1u) == 0u) {
+ _W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
+ }
+
+ /* store it */
+ W[ix] = (mp_digit)_W & MP_MASK;
+
+ /* make next carry */
+ W1 = _W >> (mp_word)MP_DIGIT_BIT;
+ }
+
+ /* setup dest */
+ oldused = b->used;
+ b->used = a->used+a->used;
+
+ for (ix = 0; ix < pa; ix++) {
+ b->dp[ix] = W[ix] & MP_MASK;
+ }
+
+ /* clear unused digits [that existed in the old copy of c] */
+ s_mp_zero_digs(b->dp + b->used, oldused - b->used);
+
+ mp_clamp(b);
+ return MP_OKAY;
+}
+#endif
diff --git a/s_mp_sqr_fast.c b/s_mp_sqr_fast.c
deleted file mode 100644
index bcb1f5e..0000000
--- a/s_mp_sqr_fast.c
+++ /dev/null
@@ -1,97 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_SQR_FAST_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* the jist of squaring...
- * you do like mult except the offset of the tmpx [one that
- * starts closer to zero] can't equal the offset of tmpy.
- * So basically you set up iy like before then you min it with
- * (ty-tx) so that it never happens. You double all those
- * you add in the inner loop
-
-After that loop you do the squares and add them in.
-*/
-
-mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b)
-{
- int olduse, pa, ix, iz;
- mp_digit W[MP_WARRAY], *tmpx;
- mp_word W1;
- mp_err err;
-
- /* grow the destination as required */
- pa = a->used + a->used;
- if (b->alloc < pa) {
- if ((err = mp_grow(b, pa)) != MP_OKAY) {
- return err;
- }
- }
-
- /* number of output digits to produce */
- W1 = 0;
- for (ix = 0; ix < pa; ix++) {
- int tx, ty, iy;
- mp_word _W;
- mp_digit *tmpy;
-
- /* clear counter */
- _W = 0;
-
- /* get offsets into the two bignums */
- ty = MP_MIN(a->used-1, ix);
- tx = ix - ty;
-
- /* setup temp aliases */
- tmpx = a->dp + tx;
- tmpy = a->dp + ty;
-
- /* this is the number of times the loop will iterrate, essentially
- while (tx++ < a->used && ty-- >= 0) { ... }
- */
- iy = MP_MIN(a->used-tx, ty+1);
-
- /* now for squaring tx can never equal ty
- * we halve the distance since they approach at a rate of 2x
- * and we have to round because odd cases need to be executed
- */
- iy = MP_MIN(iy, ((ty-tx)+1)>>1);
-
- /* execute loop */
- for (iz = 0; iz < iy; iz++) {
- _W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
- }
-
- /* double the inner product and add carry */
- _W = _W + _W + W1;
-
- /* even columns have the square term in them */
- if (((unsigned)ix & 1u) == 0u) {
- _W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
- }
-
- /* store it */
- W[ix] = (mp_digit)_W & MP_MASK;
-
- /* make next carry */
- W1 = _W >> (mp_word)MP_DIGIT_BIT;
- }
-
- /* setup dest */
- olduse = b->used;
- b->used = a->used+a->used;
-
- {
- mp_digit *tmpb;
- tmpb = b->dp;
- for (ix = 0; ix < pa; ix++) {
- *tmpb++ = W[ix] & MP_MASK;
- }
-
- /* clear unused digits [that existed in the old copy of c] */
- MP_ZERO_DIGITS(tmpb, olduse - ix);
- }
- mp_clamp(b);
- return MP_OKAY;
-}
-#endif
diff --git a/s_mp_sqr_karatsuba.c b/s_mp_sqr_karatsuba.c
new file mode 100644
index 0000000..f064b46
--- /dev/null
+++ b/s_mp_sqr_karatsuba.c
@@ -0,0 +1,92 @@
+#include "tommath_private.h"
+#ifdef S_MP_SQR_KARATSUBA_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* Karatsuba squaring, computes b = a*a using three
+ * half size squarings
+ *
+ * See comments of mul_karatsuba for details. It
+ * is essentially the same algorithm but merely
+ * tuned to perform recursive squarings.
+ */
+mp_err s_mp_sqr_karatsuba(const mp_int *a, mp_int *b)
+{
+ mp_int x0, x1, t1, t2, x0x0, x1x1;
+ int B;
+ mp_err err;
+
+ /* min # of digits */
+ B = a->used;
+
+ /* now divide in two */
+ B = B >> 1;
+
+ /* init copy all the temps */
+ if ((err = mp_init_size(&x0, B)) != MP_OKAY)
+ goto LBL_ERR;
+ if ((err = mp_init_size(&x1, a->used - B)) != MP_OKAY)
+ goto X0;
+
+ /* init temps */
+ if ((err = mp_init_size(&t1, a->used * 2)) != MP_OKAY)
+ goto X1;
+ if ((err = mp_init_size(&t2, a->used * 2)) != MP_OKAY)
+ goto T1;
+ if ((err = mp_init_size(&x0x0, B * 2)) != MP_OKAY)
+ goto T2;
+ if ((err = mp_init_size(&x1x1, (a->used - B) * 2)) != MP_OKAY)
+ goto X0X0;
+
+ /* now shift the digits */
+ x0.used = B;
+ x1.used = a->used - B;
+ s_mp_copy_digs(x0.dp, a->dp, x0.used);
+ s_mp_copy_digs(x1.dp, a->dp + B, x1.used);
+ mp_clamp(&x0);
+
+ /* now calc the products x0*x0 and x1*x1 */
+ if ((err = mp_sqr(&x0, &x0x0)) != MP_OKAY)
+ goto X1X1; /* x0x0 = x0*x0 */
+ if ((err = mp_sqr(&x1, &x1x1)) != MP_OKAY)
+ goto X1X1; /* x1x1 = x1*x1 */
+
+ /* now calc (x1+x0)**2 */
+ if ((err = s_mp_add(&x1, &x0, &t1)) != MP_OKAY)
+ goto X1X1; /* t1 = x1 - x0 */
+ if ((err = mp_sqr(&t1, &t1)) != MP_OKAY)
+ goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
+
+ /* add x0y0 */
+ if ((err = s_mp_add(&x0x0, &x1x1, &t2)) != MP_OKAY)
+ goto X1X1; /* t2 = x0x0 + x1x1 */
+ if ((err = s_mp_sub(&t1, &t2, &t1)) != MP_OKAY)
+ goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
+
+ /* shift by B */
+ if ((err = mp_lshd(&t1, B)) != MP_OKAY)
+ goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
+ if ((err = mp_lshd(&x1x1, B * 2)) != MP_OKAY)
+ goto X1X1; /* x1x1 = x1x1 << 2*B */
+
+ if ((err = mp_add(&x0x0, &t1, &t1)) != MP_OKAY)
+ goto X1X1; /* t1 = x0x0 + t1 */
+ if ((err = mp_add(&t1, &x1x1, b)) != MP_OKAY)
+ goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
+
+X1X1:
+ mp_clear(&x1x1);
+X0X0:
+ mp_clear(&x0x0);
+T2:
+ mp_clear(&t2);
+T1:
+ mp_clear(&t1);
+X1:
+ mp_clear(&x1);
+X0:
+ mp_clear(&x0);
+LBL_ERR:
+ return err;
+}
+#endif
diff --git a/s_mp_sqr_toom.c b/s_mp_sqr_toom.c
new file mode 100644
index 0000000..fd70a3d
--- /dev/null
+++ b/s_mp_sqr_toom.c
@@ -0,0 +1,133 @@
+#include "tommath_private.h"
+#ifdef S_MP_SQR_TOOM_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* squaring using Toom-Cook 3-way algorithm */
+
+/*
+ This file contains code from J. Arndt's book "Matters Computational"
+ and the accompanying FXT-library with permission of the author.
+*/
+
+/* squaring using Toom-Cook 3-way algorithm */
+/*
+ Setup and interpolation from algorithm SQR_3 in
+
+ Chung, Jaewook, and M. Anwar Hasan. "Asymmetric squaring formulae."
+ 18th IEEE Symposium on Computer Arithmetic (ARITH'07). IEEE, 2007.
+
+*/
+mp_err s_mp_sqr_toom(const mp_int *a, mp_int *b)
+{
+ mp_int S0, a0, a1, a2;
+ int B;
+ mp_err err;
+
+ /* init temps */
+ if ((err = mp_init(&S0)) != MP_OKAY) {
+ return err;
+ }
+
+ /* B */
+ B = a->used / 3;
+
+ /** a = a2 * x^2 + a1 * x + a0; */
+ if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0;
+ if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1;
+ if ((err = mp_init_size(&a2, a->used - (2 * B))) != MP_OKAY) goto LBL_ERRa2;
+
+ a0.used = a1.used = B;
+ a2.used = a->used - 2 * B;
+ s_mp_copy_digs(a0.dp, a->dp, a0.used);
+ s_mp_copy_digs(a1.dp, a->dp + B, a1.used);
+ s_mp_copy_digs(a2.dp, a->dp + 2 * B, a2.used);
+ mp_clamp(&a0);
+ mp_clamp(&a1);
+ mp_clamp(&a2);
+
+ /** S0 = a0^2; */
+ if ((err = mp_sqr(&a0, &S0)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\S1 = (a2 + a1 + a0)^2 */
+ /** \\S2 = (a2 - a1 + a0)^2 */
+ /** \\S1 = a0 + a2; */
+ /** a0 = a0 + a2; */
+ if ((err = mp_add(&a0, &a2, &a0)) != MP_OKAY) goto LBL_ERR;
+ /** \\S2 = S1 - a1; */
+ /** b = a0 - a1; */
+ if ((err = mp_sub(&a0, &a1, b)) != MP_OKAY) goto LBL_ERR;
+ /** \\S1 = S1 + a1; */
+ /** a0 = a0 + a1; */
+ if ((err = mp_add(&a0, &a1, &a0)) != MP_OKAY) goto LBL_ERR;
+ /** \\S1 = S1^2; */
+ /** a0 = a0^2; */
+ if ((err = mp_sqr(&a0, &a0)) != MP_OKAY) goto LBL_ERR;
+ /** \\S2 = S2^2; */
+ /** b = b^2; */
+ if ((err = mp_sqr(b, b)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\ S3 = 2 * a1 * a2 */
+ /** \\S3 = a1 * a2; */
+ /** a1 = a1 * a2; */
+ if ((err = mp_mul(&a1, &a2, &a1)) != MP_OKAY) goto LBL_ERR;
+ /** \\S3 = S3 << 1; */
+ /** a1 = a1 << 1; */
+ if ((err = mp_mul_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\S4 = a2^2; */
+ /** a2 = a2^2; */
+ if ((err = mp_sqr(&a2, &a2)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\ tmp = (S1 + S2)/2 */
+ /** \\tmp = S1 + S2; */
+ /** b = a0 + b; */
+ if ((err = mp_add(&a0, b, b)) != MP_OKAY) goto LBL_ERR;
+ /** \\tmp = tmp >> 1; */
+ /** b = b >> 1; */
+ if ((err = mp_div_2(b, b)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\ S1 = S1 - tmp - S3 */
+ /** \\S1 = S1 - tmp; */
+ /** a0 = a0 - b; */
+ if ((err = mp_sub(&a0, b, &a0)) != MP_OKAY) goto LBL_ERR;
+ /** \\S1 = S1 - S3; */
+ /** a0 = a0 - a1; */
+ if ((err = mp_sub(&a0, &a1, &a0)) != MP_OKAY) goto LBL_ERR;
+
+ /** \\S2 = tmp - S4 -S0 */
+ /** \\S2 = tmp - S4; */
+ /** b = b - a2; */
+ if ((err = mp_sub(b, &a2, b)) != MP_OKAY) goto LBL_ERR;
+ /** \\S2 = S2 - S0; */
+ /** b = b - S0; */
+ if ((err = mp_sub(b, &S0, b)) != MP_OKAY) goto LBL_ERR;
+
+
+ /** \\P = S4*x^4 + S3*x^3 + S2*x^2 + S1*x + S0; */
+ /** P = a2*x^4 + a1*x^3 + b*x^2 + a0*x + S0; */
+
+ if ((err = mp_lshd(&a2, 4 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_lshd(&a1, 3 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_lshd(b, 2 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_lshd(&a0, 1 * B)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(&a2, &a1, &a2)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(&a2, b, b)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(b, &a0, b)) != MP_OKAY) goto LBL_ERR;
+ if ((err = mp_add(b, &S0, b)) != MP_OKAY) goto LBL_ERR;
+ /** a^2 - P */
+
+
+LBL_ERR:
+ mp_clear(&a2);
+LBL_ERRa2:
+ mp_clear(&a1);
+LBL_ERRa1:
+ mp_clear(&a0);
+LBL_ERRa0:
+ mp_clear(&S0);
+
+ return err;
+}
+
+#endif
diff --git a/s_mp_sub.c b/s_mp_sub.c
index bef1fce..b1a749e 100644
--- a/s_mp_sub.c
+++ b/s_mp_sub.c
@@ -6,64 +6,49 @@
/* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
{
- int olduse, min, max;
+ int oldused = c->used, min = b->used, max = a->used, i;
+ mp_digit u;
mp_err err;
- /* find sizes */
- min = b->used;
- max = a->used;
-
/* init result */
- if (c->alloc < max) {
- if ((err = mp_grow(c, max)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, max)) != MP_OKAY) {
+ return err;
}
- olduse = c->used;
- c->used = max;
-
- {
- mp_digit u, *tmpa, *tmpb, *tmpc;
- int i;
-
- /* alias for digit pointers */
- tmpa = a->dp;
- tmpb = b->dp;
- tmpc = c->dp;
-
- /* set carry to zero */
- u = 0;
- for (i = 0; i < min; i++) {
- /* T[i] = A[i] - B[i] - U */
- *tmpc = (*tmpa++ - *tmpb++) - u;
- /* U = carry bit of T[i]
- * Note this saves performing an AND operation since
- * if a carry does occur it will propagate all the way to the
- * MSB. As a result a single shift is enough to get the carry
- */
- u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
-
- /* Clear carry from T[i] */
- *tmpc++ &= MP_MASK;
- }
+ c->used = max;
- /* now copy higher words if any, e.g. if A has more digits than B */
- for (; i < max; i++) {
- /* T[i] = A[i] - U */
- *tmpc = *tmpa++ - u;
+ /* set carry to zero */
+ u = 0;
+ for (i = 0; i < min; i++) {
+ /* T[i] = A[i] - B[i] - U */
+ c->dp[i] = (a->dp[i] - b->dp[i]) - u;
+
+ /* U = carry bit of T[i]
+ * Note this saves performing an AND operation since
+ * if a carry does occur it will propagate all the way to the
+ * MSB. As a result a single shift is enough to get the carry
+ */
+ u = c->dp[i] >> (MP_SIZEOF_BITS(mp_digit) - 1u);
+
+ /* Clear carry from T[i] */
+ c->dp[i] &= MP_MASK;
+ }
- /* U = carry bit of T[i] */
- u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
+ /* now copy higher words if any, e.g. if A has more digits than B */
+ for (; i < max; i++) {
+ /* T[i] = A[i] - U */
+ c->dp[i] = a->dp[i] - u;
- /* Clear carry from T[i] */
- *tmpc++ &= MP_MASK;
- }
+ /* U = carry bit of T[i] */
+ u = c->dp[i] >> (MP_SIZEOF_BITS(mp_digit) - 1u);
- /* clear digits above used (since we may not have grown result above) */
- MP_ZERO_DIGITS(tmpc, olduse - c->used);
+ /* Clear carry from T[i] */
+ c->dp[i] &= MP_MASK;
}
+ /* clear digits above used (since we may not have grown result above) */
+ s_mp_zero_digs(c->dp + c->used, oldused - c->used);
+
mp_clamp(c);
return MP_OKAY;
}
diff --git a/s_mp_toom_mul.c b/s_mp_toom_mul.c
deleted file mode 100644
index 93113b8..0000000
--- a/s_mp_toom_mul.c
+++ /dev/null
@@ -1,215 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_TOOM_MUL_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* multiplication using the Toom-Cook 3-way algorithm
- *
- * Much more complicated than Karatsuba but has a lower
- * asymptotic running time of O(N**1.464). This algorithm is
- * only particularly useful on VERY large inputs
- * (we're talking 1000s of digits here...).
-*/
-
-/*
- This file contains code from J. Arndt's book "Matters Computational"
- and the accompanying FXT-library with permission of the author.
-*/
-
-/*
- Setup from
-
- Chung, Jaewook, and M. Anwar Hasan. "Asymmetric squaring formulae."
- 18th IEEE Symposium on Computer Arithmetic (ARITH'07). IEEE, 2007.
-
- The interpolation from above needed one temporary variable more
- than the interpolation here:
-
- Bodrato, Marco, and Alberto Zanoni. "What about Toom-Cook matrices optimality."
- Centro Vito Volterra Universita di Roma Tor Vergata (2006)
-*/
-
-mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
-{
- mp_int S1, S2, T1, a0, a1, a2, b0, b1, b2;
- int B, count;
- mp_err err;
-
- /* init temps */
- if ((err = mp_init_multi(&S1, &S2, &T1, NULL)) != MP_OKAY) {
- return err;
- }
-
- /* B */
- B = MP_MIN(a->used, b->used) / 3;
-
- /** a = a2 * x^2 + a1 * x + a0; */
- if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0;
-
- for (count = 0; count < B; count++) {
- a0.dp[count] = a->dp[count];
- a0.used++;
- }
- mp_clamp(&a0);
- if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1;
- for (; count < (2 * B); count++) {
- a1.dp[count - B] = a->dp[count];
- a1.used++;
- }
- mp_clamp(&a1);
- if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) goto LBL_ERRa2;
- for (; count < a->used; count++) {
- a2.dp[count - (2 * B)] = a->dp[count];
- a2.used++;
- }
- mp_clamp(&a2);
-
- /** b = b2 * x^2 + b1 * x + b0; */
- if ((err = mp_init_size(&b0, B)) != MP_OKAY) goto LBL_ERRb0;
- for (count = 0; count < B; count++) {
- b0.dp[count] = b->dp[count];
- b0.used++;
- }
- mp_clamp(&b0);
- if ((err = mp_init_size(&b1, B)) != MP_OKAY) goto LBL_ERRb1;
- for (; count < (2 * B); count++) {
- b1.dp[count - B] = b->dp[count];
- b1.used++;
- }
- mp_clamp(&b1);
- if ((err = mp_init_size(&b2, B + (b->used - (3 * B)))) != MP_OKAY) goto LBL_ERRb2;
- for (; count < b->used; count++) {
- b2.dp[count - (2 * B)] = b->dp[count];
- b2.used++;
- }
- mp_clamp(&b2);
-
- /** \\ S1 = (a2+a1+a0) * (b2+b1+b0); */
- /** T1 = a2 + a1; */
- if ((err = mp_add(&a2, &a1, &T1)) != MP_OKAY) goto LBL_ERR;
-
- /** S2 = T1 + a0; */
- if ((err = mp_add(&T1, &a0, &S2)) != MP_OKAY) goto LBL_ERR;
-
- /** c = b2 + b1; */
- if ((err = mp_add(&b2, &b1, c)) != MP_OKAY) goto LBL_ERR;
-
- /** S1 = c + b0; */
- if ((err = mp_add(c, &b0, &S1)) != MP_OKAY) goto LBL_ERR;
-
- /** S1 = S1 * S2; */
- if ((err = mp_mul(&S1, &S2, &S1)) != MP_OKAY) goto LBL_ERR;
-
- /** \\S2 = (4*a2+2*a1+a0) * (4*b2+2*b1+b0); */
- /** T1 = T1 + a2; */
- if ((err = mp_add(&T1, &a2, &T1)) != MP_OKAY) goto LBL_ERR;
-
- /** T1 = T1 << 1; */
- if ((err = mp_mul_2(&T1, &T1)) != MP_OKAY) goto LBL_ERR;
-
- /** T1 = T1 + a0; */
- if ((err = mp_add(&T1, &a0, &T1)) != MP_OKAY) goto LBL_ERR;
-
- /** c = c + b2; */
- if ((err = mp_add(c, &b2, c)) != MP_OKAY) goto LBL_ERR;
-
- /** c = c << 1; */
- if ((err = mp_mul_2(c, c)) != MP_OKAY) goto LBL_ERR;
-
- /** c = c + b0; */
- if ((err = mp_add(c, &b0, c)) != MP_OKAY) goto LBL_ERR;
-
- /** S2 = T1 * c; */
- if ((err = mp_mul(&T1, c, &S2)) != MP_OKAY) goto LBL_ERR;
-
- /** \\S3 = (a2-a1+a0) * (b2-b1+b0); */
- /** a1 = a2 - a1; */
- if ((err = mp_sub(&a2, &a1, &a1)) != MP_OKAY) goto LBL_ERR;
-
- /** a1 = a1 + a0; */
- if ((err = mp_add(&a1, &a0, &a1)) != MP_OKAY) goto LBL_ERR;
-
- /** b1 = b2 - b1; */
- if ((err = mp_sub(&b2, &b1, &b1)) != MP_OKAY) goto LBL_ERR;
-
- /** b1 = b1 + b0; */
- if ((err = mp_add(&b1, &b0, &b1)) != MP_OKAY) goto LBL_ERR;
-
- /** a1 = a1 * b1; */
- if ((err = mp_mul(&a1, &b1, &a1)) != MP_OKAY) goto LBL_ERR;
-
- /** b1 = a2 * b2; */
- if ((err = mp_mul(&a2, &b2, &b1)) != MP_OKAY) goto LBL_ERR;
-
- /** \\S2 = (S2 - S3)/3; */
- /** S2 = S2 - a1; */
- if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) goto LBL_ERR;
-
- /** S2 = S2 / 3; \\ this is an exact division */
- if ((err = mp_div_3(&S2, &S2, NULL)) != MP_OKAY) goto LBL_ERR;
-
- /** a1 = S1 - a1; */
- if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) goto LBL_ERR;
-
- /** a1 = a1 >> 1; */
- if ((err = mp_div_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR;
-
- /** a0 = a0 * b0; */
- if ((err = mp_mul(&a0, &b0, &a0)) != MP_OKAY) goto LBL_ERR;
-
- /** S1 = S1 - a0; */
- if ((err = mp_sub(&S1, &a0, &S1)) != MP_OKAY) goto LBL_ERR;
-
- /** S2 = S2 - S1; */
- if ((err = mp_sub(&S2, &S1, &S2)) != MP_OKAY) goto LBL_ERR;
-
- /** S2 = S2 >> 1; */
- if ((err = mp_div_2(&S2, &S2)) != MP_OKAY) goto LBL_ERR;
-
- /** S1 = S1 - a1; */
- if ((err = mp_sub(&S1, &a1, &S1)) != MP_OKAY) goto LBL_ERR;
-
- /** S1 = S1 - b1; */
- if ((err = mp_sub(&S1, &b1, &S1)) != MP_OKAY) goto LBL_ERR;
-
- /** T1 = b1 << 1; */
- if ((err = mp_mul_2(&b1, &T1)) != MP_OKAY) goto LBL_ERR;
-
- /** S2 = S2 - T1; */
- if ((err = mp_sub(&S2, &T1, &S2)) != MP_OKAY) goto LBL_ERR;
-
- /** a1 = a1 - S2; */
- if ((err = mp_sub(&a1, &S2, &a1)) != MP_OKAY) goto LBL_ERR;
-
-
- /** P = b1*x^4+ S2*x^3+ S1*x^2+ a1*x + a0; */
- if ((err = mp_lshd(&b1, 4 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_lshd(&S2, 3 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(&b1, &S2, &b1)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_lshd(&S1, 2 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(&b1, &S1, &b1)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_lshd(&a1, 1 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(&b1, &a1, &b1)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(&b1, &a0, c)) != MP_OKAY) goto LBL_ERR;
-
- /** a * b - P */
-
-
-LBL_ERR:
- mp_clear(&b2);
-LBL_ERRb2:
- mp_clear(&b1);
-LBL_ERRb1:
- mp_clear(&b0);
-LBL_ERRb0:
- mp_clear(&a2);
-LBL_ERRa2:
- mp_clear(&a1);
-LBL_ERRa1:
- mp_clear(&a0);
-LBL_ERRa0:
- mp_clear_multi(&S1, &S2, &T1, NULL);
- return err;
-}
-
-#endif
diff --git a/s_mp_toom_sqr.c b/s_mp_toom_sqr.c
deleted file mode 100644
index 67c465c..0000000
--- a/s_mp_toom_sqr.c
+++ /dev/null
@@ -1,147 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_TOOM_SQR_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* squaring using Toom-Cook 3-way algorithm */
-
-/*
- This file contains code from J. Arndt's book "Matters Computational"
- and the accompanying FXT-library with permission of the author.
-*/
-
-/* squaring using Toom-Cook 3-way algorithm */
-/*
- Setup and interpolation from algorithm SQR_3 in
-
- Chung, Jaewook, and M. Anwar Hasan. "Asymmetric squaring formulae."
- 18th IEEE Symposium on Computer Arithmetic (ARITH'07). IEEE, 2007.
-
-*/
-mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b)
-{
- mp_int S0, a0, a1, a2;
- mp_digit *tmpa, *tmpc;
- int B, count;
- mp_err err;
-
-
- /* init temps */
- if ((err = mp_init(&S0)) != MP_OKAY) {
- return err;
- }
-
- /* B */
- B = a->used / 3;
-
- /** a = a2 * x^2 + a1 * x + a0; */
- if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0;
-
- a0.used = B;
- if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1;
- a1.used = B;
- if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) goto LBL_ERRa2;
-
- tmpa = a->dp;
- tmpc = a0.dp;
- for (count = 0; count < B; count++) {
- *tmpc++ = *tmpa++;
- }
- tmpc = a1.dp;
- for (; count < (2 * B); count++) {
- *tmpc++ = *tmpa++;
- }
- tmpc = a2.dp;
- for (; count < a->used; count++) {
- *tmpc++ = *tmpa++;
- a2.used++;
- }
- mp_clamp(&a0);
- mp_clamp(&a1);
- mp_clamp(&a2);
-
- /** S0 = a0^2; */
- if ((err = mp_sqr(&a0, &S0)) != MP_OKAY) goto LBL_ERR;
-
- /** \\S1 = (a2 + a1 + a0)^2 */
- /** \\S2 = (a2 - a1 + a0)^2 */
- /** \\S1 = a0 + a2; */
- /** a0 = a0 + a2; */
- if ((err = mp_add(&a0, &a2, &a0)) != MP_OKAY) goto LBL_ERR;
- /** \\S2 = S1 - a1; */
- /** b = a0 - a1; */
- if ((err = mp_sub(&a0, &a1, b)) != MP_OKAY) goto LBL_ERR;
- /** \\S1 = S1 + a1; */
- /** a0 = a0 + a1; */
- if ((err = mp_add(&a0, &a1, &a0)) != MP_OKAY) goto LBL_ERR;
- /** \\S1 = S1^2; */
- /** a0 = a0^2; */
- if ((err = mp_sqr(&a0, &a0)) != MP_OKAY) goto LBL_ERR;
- /** \\S2 = S2^2; */
- /** b = b^2; */
- if ((err = mp_sqr(b, b)) != MP_OKAY) goto LBL_ERR;
-
- /** \\ S3 = 2 * a1 * a2 */
- /** \\S3 = a1 * a2; */
- /** a1 = a1 * a2; */
- if ((err = mp_mul(&a1, &a2, &a1)) != MP_OKAY) goto LBL_ERR;
- /** \\S3 = S3 << 1; */
- /** a1 = a1 << 1; */
- if ((err = mp_mul_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR;
-
- /** \\S4 = a2^2; */
- /** a2 = a2^2; */
- if ((err = mp_sqr(&a2, &a2)) != MP_OKAY) goto LBL_ERR;
-
- /** \\ tmp = (S1 + S2)/2 */
- /** \\tmp = S1 + S2; */
- /** b = a0 + b; */
- if ((err = mp_add(&a0, b, b)) != MP_OKAY) goto LBL_ERR;
- /** \\tmp = tmp >> 1; */
- /** b = b >> 1; */
- if ((err = mp_div_2(b, b)) != MP_OKAY) goto LBL_ERR;
-
- /** \\ S1 = S1 - tmp - S3 */
- /** \\S1 = S1 - tmp; */
- /** a0 = a0 - b; */
- if ((err = mp_sub(&a0, b, &a0)) != MP_OKAY) goto LBL_ERR;
- /** \\S1 = S1 - S3; */
- /** a0 = a0 - a1; */
- if ((err = mp_sub(&a0, &a1, &a0)) != MP_OKAY) goto LBL_ERR;
-
- /** \\S2 = tmp - S4 -S0 */
- /** \\S2 = tmp - S4; */
- /** b = b - a2; */
- if ((err = mp_sub(b, &a2, b)) != MP_OKAY) goto LBL_ERR;
- /** \\S2 = S2 - S0; */
- /** b = b - S0; */
- if ((err = mp_sub(b, &S0, b)) != MP_OKAY) goto LBL_ERR;
-
-
- /** \\P = S4*x^4 + S3*x^3 + S2*x^2 + S1*x + S0; */
- /** P = a2*x^4 + a1*x^3 + b*x^2 + a0*x + S0; */
-
- if ((err = mp_lshd(&a2, 4 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_lshd(&a1, 3 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_lshd(b, 2 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_lshd(&a0, 1 * B)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(&a2, &a1, &a2)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(&a2, b, b)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(b, &a0, b)) != MP_OKAY) goto LBL_ERR;
- if ((err = mp_add(b, &S0, b)) != MP_OKAY) goto LBL_ERR;
- /** a^2 - P */
-
-
-LBL_ERR:
- mp_clear(&a2);
-LBL_ERRa2:
- mp_clear(&a1);
-LBL_ERRa1:
- mp_clear(&a0);
-LBL_ERRa0:
- mp_clear(&S0);
-
- return err;
-}
-
-#endif
diff --git a/s_mp_zero_buf.c b/s_mp_zero_buf.c
new file mode 100644
index 0000000..23a458d
--- /dev/null
+++ b/s_mp_zero_buf.c
@@ -0,0 +1,22 @@
+#include "tommath_private.h"
+#ifdef S_MP_ZERO_BUF_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+#ifdef MP_USE_MEMOPS
+# include <string.h>
+#endif
+
+void s_mp_zero_buf(void *mem, size_t size)
+{
+#ifdef MP_USE_MEMOPS
+ memset(mem, 0, size);
+#else
+ char *m = (char *)mem;
+ while (size-- > 0u) {
+ *m++ = '\0';
+ }
+#endif
+}
+
+#endif
diff --git a/s_mp_zero_digs.c b/s_mp_zero_digs.c
new file mode 100644
index 0000000..79e8377
--- /dev/null
+++ b/s_mp_zero_digs.c
@@ -0,0 +1,23 @@
+#include "tommath_private.h"
+#ifdef S_MP_ZERO_DIGS_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+#ifdef MP_USE_MEMOPS
+# include <string.h>
+#endif
+
+void s_mp_zero_digs(mp_digit *d, int digits)
+{
+#ifdef MP_USE_MEMOPS
+ if (digits > 0) {
+ memset(d, 0, (size_t)digits * sizeof(mp_digit));
+ }
+#else
+ while (digits-- > 0) {
+ *d++ = 0;
+ }
+#endif
+}
+
+#endif
diff --git a/tommath.def b/tommath.def
index db0aba4..4d81e34 100644
--- a/tommath.def
+++ b/tommath.def
@@ -72,7 +72,6 @@ EXPORTS
mp_lshd
mp_mod
mp_mod_2d
- mp_mod_d
mp_montgomery_calc_normalization
mp_montgomery_reduce
mp_montgomery_setup
diff --git a/tommath.h b/tommath.h
index 86b19d3..d024a33 100644
--- a/tommath.h
+++ b/tommath.h
@@ -63,7 +63,7 @@ typedef uint32_t mp_digit;
# ifdef MP_31BIT
/*
* This is an extension that uses 31-bit digits.
- * Please be aware that not all functions support this size, especially s_mp_mul_digs_fast
+ * Please be aware that not all functions support this size, especially s_mp_mul_comba
* will be reduced to work on small numbers only:
* Up to 8 limbs, 248 bits instead of up to 512 limbs, 15872 bits with MP_28BIT.
*/
@@ -117,10 +117,10 @@ typedef enum {
/* tunable cutoffs */
#ifndef MP_FIXED_CUTOFFS
extern int
-MP_KARATSUBA_MUL_CUTOFF,
-MP_KARATSUBA_SQR_CUTOFF,
-MP_TOOM_MUL_CUTOFF,
-MP_TOOM_SQR_CUTOFF;
+MP_MUL_KARATSUBA_CUTOFF,
+MP_SQR_KARATSUBA_CUTOFF,
+MP_MUL_TOOM_CUTOFF,
+MP_SQR_TOOM_CUTOFF;
#endif
/* define this to use lower memory usage routines (exptmods mostly) */
@@ -398,7 +398,7 @@ mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR;
/* c = a mod b, 0 <= c < b */
-mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
+#define mp_mod_d(a, b, c) mp_div_d((a), (b), NULL, (c))
/* ---> number theory <--- */
diff --git a/tommath_class.h b/tommath_class.h
index 3a3d549..2a5380a 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -77,7 +77,6 @@
# define MP_LSHD_C
# define MP_MOD_C
# define MP_MOD_2D_C
-# define MP_MOD_D_C
# define MP_MONTGOMERY_CALC_NORMALIZATION_C
# define MP_MONTGOMERY_REDUCE_C
# define MP_MONTGOMERY_SETUP_C
@@ -141,33 +140,36 @@
# define MP_XOR_C
# define MP_ZERO_C
# define S_MP_ADD_C
-# define S_MP_BALANCE_MUL_C
+# define S_MP_COPY_DIGS_C
# define S_MP_DIV_RECURSIVE_C
# define S_MP_DIV_SCHOOL_C
# define S_MP_DIV_SMALL_C
# define S_MP_EXPTMOD_C
# define S_MP_EXPTMOD_FAST_C
# define S_MP_GET_BIT_C
-# define S_MP_INVMOD_FAST_C
-# define S_MP_INVMOD_SLOW_C
-# define S_MP_KARATSUBA_MUL_C
-# define S_MP_KARATSUBA_SQR_C
+# define S_MP_INVMOD_C
+# define S_MP_INVMOD_ODD_C
# define S_MP_LOG_C
# define S_MP_LOG_D_C
# define S_MP_LOG_POW2_C
-# define S_MP_MONTGOMERY_REDUCE_FAST_C
-# define S_MP_MUL_DIGS_C
-# define S_MP_MUL_DIGS_FAST_C
-# define S_MP_MUL_HIGH_DIGS_C
-# define S_MP_MUL_HIGH_DIGS_FAST_C
+# define S_MP_MONTGOMERY_REDUCE_COMBA_C
+# define S_MP_MUL_C
+# define S_MP_MUL_BALANCE_C
+# define S_MP_MUL_COMBA_C
+# define S_MP_MUL_HIGH_C
+# define S_MP_MUL_HIGH_COMBA_C
+# define S_MP_MUL_KARATSUBA_C
+# define S_MP_MUL_TOOM_C
# define S_MP_PRIME_IS_DIVISIBLE_C
# define S_MP_RAND_JENKINS_C
# define S_MP_RAND_PLATFORM_C
# define S_MP_SQR_C
-# define S_MP_SQR_FAST_C
+# define S_MP_SQR_COMBA_C
+# define S_MP_SQR_KARATSUBA_C
+# define S_MP_SQR_TOOM_C
# define S_MP_SUB_C
-# define S_MP_TOOM_MUL_C
-# define S_MP_TOOM_SQR_C
+# define S_MP_ZERO_BUF_C
+# define S_MP_ZERO_DIGS_C
#endif
#endif
#if defined(MP_2EXPT_C)
@@ -189,6 +191,7 @@
# define MP_CLAMP_C
# define MP_GROW_C
# define MP_SUB_D_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_ADDMOD_C)
@@ -207,6 +210,7 @@
#endif
#if defined(MP_CLEAR_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_CLEAR_MULTI_C)
@@ -227,12 +231,13 @@
#endif
#if defined(MP_COMPLEMENT_C)
-# define MP_NEG_C
# define MP_SUB_D_C
#endif
#if defined(MP_COPY_C)
# define MP_GROW_C
+# define S_MP_COPY_DIGS_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_COUNT_BITS_C)
@@ -253,6 +258,7 @@
#if defined(MP_DIV_2_C)
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_DIV_2D_C)
@@ -260,7 +266,6 @@
# define MP_COPY_C
# define MP_MOD_2D_C
# define MP_RSHD_C
-# define MP_ZERO_C
#endif
#if defined(MP_DIV_3_C)
@@ -288,6 +293,7 @@
# define MP_CMP_MAG_C
# define MP_GROW_C
# define S_MP_SUB_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_DR_SETUP_C)
@@ -351,6 +357,7 @@
#if defined(MP_FWRITE_C)
# define MP_RADIX_SIZE_C
# define MP_TO_RADIX_C
+# define S_MP_ZERO_BUF_C
#endif
#if defined(MP_GCD_C)
@@ -397,6 +404,7 @@
#endif
#if defined(MP_GROW_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_INIT_C)
@@ -463,17 +471,17 @@
#if defined(MP_INVMOD_C)
# define MP_CMP_D_C
-# define S_MP_INVMOD_FAST_C
-# define S_MP_INVMOD_SLOW_C
+# define S_MP_INVMOD_C
+# define S_MP_INVMOD_ODD_C
#endif
#if defined(MP_IS_SQUARE_C)
# define MP_CLEAR_C
# define MP_CMP_MAG_C
+# define MP_DIV_D_C
# define MP_GET_I32_C
# define MP_INIT_U32_C
# define MP_MOD_C
-# define MP_MOD_D_C
# define MP_SQRT_C
# define MP_SQR_C
#endif
@@ -506,6 +514,7 @@
#if defined(MP_LSHD_C)
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_MOD_C)
@@ -520,10 +529,7 @@
# define MP_CLAMP_C
# define MP_COPY_C
# define MP_ZERO_C
-#endif
-
-#if defined(MP_MOD_D_C)
-# define MP_DIV_D_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_MONTGOMERY_CALC_NORMALIZATION_C)
@@ -540,7 +546,7 @@
# define MP_CMP_MAG_C
# define MP_GROW_C
# define MP_RSHD_C
-# define S_MP_MONTGOMERY_REDUCE_FAST_C
+# define S_MP_MONTGOMERY_REDUCE_COMBA_C
# define S_MP_SUB_C
#endif
@@ -548,15 +554,16 @@
#endif
#if defined(MP_MUL_C)
-# define S_MP_BALANCE_MUL_C
-# define S_MP_KARATSUBA_MUL_C
-# define S_MP_MUL_DIGS_C
-# define S_MP_MUL_DIGS_FAST_C
-# define S_MP_TOOM_MUL_C
+# define S_MP_MUL_BALANCE_C
+# define S_MP_MUL_C
+# define S_MP_MUL_COMBA_C
+# define S_MP_MUL_KARATSUBA_C
+# define S_MP_MUL_TOOM_C
#endif
#if defined(MP_MUL_2_C)
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_MUL_2D_C)
@@ -569,6 +576,7 @@
#if defined(MP_MUL_D_C)
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_MULMOD_C)
@@ -660,8 +668,8 @@
# define MP_ADD_D_C
# define MP_CLEAR_C
# define MP_CMP_D_C
+# define MP_DIV_D_C
# define MP_INIT_C
-# define MP_MOD_D_C
# define MP_PRIME_IS_PRIME_C
# define MP_SET_C
# define MP_SUB_D_C
@@ -678,6 +686,7 @@
# define MP_PRIME_IS_PRIME_C
# define MP_SUB_D_C
# define S_MP_RAND_SOURCE_C
+# define S_MP_ZERO_BUF_C
#endif
#if defined(MP_PRIME_STRONG_LUCAS_SELFRIDGE_C)
@@ -744,9 +753,9 @@
# define MP_RSHD_C
# define MP_SET_C
# define MP_SUB_C
-# define S_MP_MUL_DIGS_C
-# define S_MP_MUL_HIGH_DIGS_C
-# define S_MP_MUL_HIGH_DIGS_FAST_C
+# define S_MP_MUL_C
+# define S_MP_MUL_HIGH_C
+# define S_MP_MUL_HIGH_COMBA_C
# define S_MP_SUB_C
#endif
@@ -820,6 +829,7 @@
#if defined(MP_RSHD_C)
# define MP_ZERO_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SBIN_SIZE_C)
@@ -827,6 +837,7 @@
#endif
#if defined(MP_SET_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SET_DOUBLE_C)
@@ -852,15 +863,19 @@
#endif
#if defined(MP_SET_U32_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SET_U64_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SET_UL_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SET_ULL_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SHRINK_C)
@@ -873,10 +888,10 @@
#endif
#if defined(MP_SQR_C)
-# define S_MP_KARATSUBA_SQR_C
# define S_MP_SQR_C
-# define S_MP_SQR_FAST_C
-# define S_MP_TOOM_SQR_C
+# define S_MP_SQR_COMBA_C
+# define S_MP_SQR_KARATSUBA_C
+# define S_MP_SQR_TOOM_C
#endif
#if defined(MP_SQRMOD_C)
@@ -905,10 +920,10 @@
# define MP_CMP_D_C
# define MP_COPY_C
# define MP_DIV_2_C
+# define MP_DIV_D_C
# define MP_EXPTMOD_C
# define MP_INIT_MULTI_C
# define MP_KRONECKER_C
-# define MP_MOD_D_C
# define MP_MULMOD_C
# define MP_SET_C
# define MP_SQRMOD_C
@@ -926,6 +941,7 @@
# define MP_ADD_D_C
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(MP_SUBMOD_C)
@@ -969,23 +985,16 @@
#endif
#if defined(MP_ZERO_C)
+# define S_MP_ZERO_DIGS_C
#endif
#if defined(S_MP_ADD_C)
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
-#if defined(S_MP_BALANCE_MUL_C)
-# define MP_ADD_C
-# define MP_CLAMP_C
-# define MP_CLEAR_C
-# define MP_CLEAR_MULTI_C
-# define MP_EXCH_C
-# define MP_INIT_MULTI_C
-# define MP_INIT_SIZE_C
-# define MP_LSHD_C
-# define MP_MUL_C
+#if defined(S_MP_COPY_DIGS_C)
#endif
#if defined(S_MP_DIV_RECURSIVE_C)
@@ -1075,13 +1084,13 @@
# define MP_REDUCE_2K_SETUP_C
# define MP_SET_C
# define MP_SQR_C
-# define S_MP_MONTGOMERY_REDUCE_FAST_C
+# define S_MP_MONTGOMERY_REDUCE_COMBA_C
#endif
#if defined(S_MP_GET_BIT_C)
#endif
-#if defined(S_MP_INVMOD_FAST_C)
+#if defined(S_MP_INVMOD_C)
# define MP_ADD_C
# define MP_CLEAR_MULTI_C
# define MP_CMP_C
@@ -1096,7 +1105,7 @@
# define MP_SUB_C
#endif
-#if defined(S_MP_INVMOD_SLOW_C)
+#if defined(S_MP_INVMOD_ODD_C)
# define MP_ADD_C
# define MP_CLEAR_MULTI_C
# define MP_CMP_C
@@ -1111,28 +1120,6 @@
# define MP_SUB_C
#endif
-#if defined(S_MP_KARATSUBA_MUL_C)
-# define MP_ADD_C
-# define MP_CLAMP_C
-# define MP_CLEAR_C
-# define MP_INIT_SIZE_C
-# define MP_LSHD_C
-# define MP_MUL_C
-# define S_MP_ADD_C
-# define S_MP_SUB_C
-#endif
-
-#if defined(S_MP_KARATSUBA_SQR_C)
-# define MP_ADD_C
-# define MP_CLAMP_C
-# define MP_CLEAR_C
-# define MP_INIT_SIZE_C
-# define MP_LSHD_C
-# define MP_SQR_C
-# define S_MP_ADD_C
-# define S_MP_SUB_C
-#endif
-
#if defined(S_MP_LOG_C)
# define MP_CLEAR_MULTI_C
# define MP_CMP_C
@@ -1153,41 +1140,86 @@
# define MP_COUNT_BITS_C
#endif
-#if defined(S_MP_MONTGOMERY_REDUCE_FAST_C)
+#if defined(S_MP_MONTGOMERY_REDUCE_COMBA_C)
# define MP_CLAMP_C
# define MP_CMP_MAG_C
# define MP_GROW_C
# define S_MP_SUB_C
+# define S_MP_ZERO_BUF_C
+# define S_MP_ZERO_DIGS_C
#endif
-#if defined(S_MP_MUL_DIGS_C)
+#if defined(S_MP_MUL_C)
# define MP_CLAMP_C
# define MP_CLEAR_C
# define MP_EXCH_C
# define MP_INIT_SIZE_C
-# define S_MP_MUL_DIGS_FAST_C
+# define S_MP_MUL_COMBA_C
#endif
-#if defined(S_MP_MUL_DIGS_FAST_C)
+#if defined(S_MP_MUL_BALANCE_C)
+# define MP_ADD_C
+# define MP_CLAMP_C
+# define MP_CLEAR_C
+# define MP_CLEAR_MULTI_C
+# define MP_EXCH_C
+# define MP_INIT_MULTI_C
+# define MP_INIT_SIZE_C
+# define MP_LSHD_C
+# define MP_MUL_C
+# define S_MP_COPY_DIGS_C
+#endif
+
+#if defined(S_MP_MUL_COMBA_C)
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
-#if defined(S_MP_MUL_HIGH_DIGS_C)
+#if defined(S_MP_MUL_HIGH_C)
# define MP_CLAMP_C
# define MP_CLEAR_C
# define MP_EXCH_C
# define MP_INIT_SIZE_C
-# define S_MP_MUL_HIGH_DIGS_FAST_C
+# define S_MP_MUL_HIGH_COMBA_C
#endif
-#if defined(S_MP_MUL_HIGH_DIGS_FAST_C)
+#if defined(S_MP_MUL_HIGH_COMBA_C)
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
+#endif
+
+#if defined(S_MP_MUL_KARATSUBA_C)
+# define MP_ADD_C
+# define MP_CLAMP_C
+# define MP_CLEAR_C
+# define MP_INIT_SIZE_C
+# define MP_LSHD_C
+# define MP_MUL_C
+# define S_MP_ADD_C
+# define S_MP_COPY_DIGS_C
+# define S_MP_SUB_C
+#endif
+
+#if defined(S_MP_MUL_TOOM_C)
+# define MP_ADD_C
+# define MP_CLAMP_C
+# define MP_CLEAR_C
+# define MP_CLEAR_MULTI_C
+# define MP_DIV_2_C
+# define MP_DIV_3_C
+# define MP_INIT_MULTI_C
+# define MP_INIT_SIZE_C
+# define MP_LSHD_C
+# define MP_MUL_2_C
+# define MP_MUL_C
+# define MP_SUB_C
+# define S_MP_COPY_DIGS_C
#endif
#if defined(S_MP_PRIME_IS_DIVISIBLE_C)
-# define MP_MOD_D_C
+# define MP_DIV_D_C
#endif
#if defined(S_MP_RAND_JENKINS_C)
@@ -1204,32 +1236,25 @@
# define MP_INIT_SIZE_C
#endif
-#if defined(S_MP_SQR_FAST_C)
+#if defined(S_MP_SQR_COMBA_C)
# define MP_CLAMP_C
# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
#endif
-#if defined(S_MP_SUB_C)
-# define MP_CLAMP_C
-# define MP_GROW_C
-#endif
-
-#if defined(S_MP_TOOM_MUL_C)
+#if defined(S_MP_SQR_KARATSUBA_C)
# define MP_ADD_C
# define MP_CLAMP_C
# define MP_CLEAR_C
-# define MP_CLEAR_MULTI_C
-# define MP_DIV_2_C
-# define MP_DIV_3_C
-# define MP_INIT_MULTI_C
# define MP_INIT_SIZE_C
# define MP_LSHD_C
-# define MP_MUL_2_C
-# define MP_MUL_C
-# define MP_SUB_C
+# define MP_SQR_C
+# define S_MP_ADD_C
+# define S_MP_COPY_DIGS_C
+# define S_MP_SUB_C
#endif
-#if defined(S_MP_TOOM_SQR_C)
+#if defined(S_MP_SQR_TOOM_C)
# define MP_ADD_C
# define MP_CLAMP_C
# define MP_CLEAR_C
@@ -1241,6 +1266,19 @@
# define MP_MUL_C
# define MP_SQR_C
# define MP_SUB_C
+# define S_MP_COPY_DIGS_C
+#endif
+
+#if defined(S_MP_SUB_C)
+# define MP_CLAMP_C
+# define MP_GROW_C
+# define S_MP_ZERO_DIGS_C
+#endif
+
+#if defined(S_MP_ZERO_BUF_C)
+#endif
+
+#if defined(S_MP_ZERO_DIGS_C)
#endif
#ifdef LTM_INSIDE
diff --git a/tommath_cutoffs.h b/tommath_cutoffs.h
index a65a9b3..fb84160 100644
--- a/tommath_cutoffs.h
+++ b/tommath_cutoffs.h
@@ -7,7 +7,7 @@
on the aforementioned machine for example.
*/
-#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF 80
-#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF 120
-#define MP_DEFAULT_TOOM_MUL_CUTOFF 350
-#define MP_DEFAULT_TOOM_SQR_CUTOFF 400
+#define MP_DEFAULT_MUL_KARATSUBA_CUTOFF 80
+#define MP_DEFAULT_SQR_KARATSUBA_CUTOFF 120
+#define MP_DEFAULT_MUL_TOOM_CUTOFF 350
+#define MP_DEFAULT_SQR_TOOM_CUTOFF 400
diff --git a/tommath_private.h b/tommath_private.h
index f2989d4..0969796 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -42,55 +42,25 @@
* define MP_NO_ZERO_ON_FREE during compilation.
*/
#ifdef MP_NO_ZERO_ON_FREE
-# define MP_FREE_BUFFER(mem, size) MP_FREE((mem), (size))
-# define MP_FREE_DIGITS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (size_t)(digits))
+# define MP_FREE_BUF(mem, size) MP_FREE((mem), (size))
+# define MP_FREE_DIGS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (size_t)(digits))
#else
-# define MP_FREE_BUFFER(mem, size) \
+# define MP_FREE_BUF(mem, size) \
do { \
size_t fs_ = (size); \
void* fm_ = (mem); \
if (fm_ != NULL) { \
- MP_ZERO_BUFFER(fm_, fs_); \
+ s_mp_zero_buf(fm_, fs_); \
MP_FREE(fm_, fs_); \
} \
} while (0)
-# define MP_FREE_DIGITS(mem, digits) \
+# define MP_FREE_DIGS(mem, digits) \
do { \
int fd_ = (digits); \
- void* fm_ = (mem); \
+ mp_digit* fm_ = (mem); \
if (fm_ != NULL) { \
- size_t fs_ = sizeof (mp_digit) * (size_t)fd_; \
- MP_ZERO_BUFFER(fm_, fs_); \
- MP_FREE(fm_, fs_); \
- } \
-} while (0)
-#endif
-
-#ifdef MP_USE_MEMSET
-# include <string.h>
-# define MP_ZERO_BUFFER(mem, size) memset((mem), 0, (size))
-# define MP_ZERO_DIGITS(mem, digits) \
-do { \
- int zd_ = (digits); \
- if (zd_ > 0) { \
- memset((mem), 0, sizeof(mp_digit) * (size_t)zd_); \
- } \
-} while (0)
-#else
-# define MP_ZERO_BUFFER(mem, size) \
-do { \
- size_t zs_ = (size); \
- char* zm_ = (char*)(mem); \
- while (zs_-- > 0u) { \
- *zm_++ = '\0'; \
- } \
-} while (0)
-# define MP_ZERO_DIGITS(mem, digits) \
-do { \
- int zd_ = (digits); \
- mp_digit* zm_ = (mem); \
- while (zd_-- > 0) { \
- *zm_++ = 0; \
+ s_mp_zero_digs(fm_, fd_); \
+ MP_FREE(fm_, sizeof (mp_digit) * (size_t)fd_); \
} \
} while (0)
#endif
@@ -112,10 +82,10 @@ do { \
#ifdef MP_FIXED_CUTOFFS
# include "tommath_cutoffs.h"
-# define MP_KARATSUBA_MUL_CUTOFF MP_DEFAULT_KARATSUBA_MUL_CUTOFF
-# define MP_KARATSUBA_SQR_CUTOFF MP_DEFAULT_KARATSUBA_SQR_CUTOFF
-# define MP_TOOM_MUL_CUTOFF MP_DEFAULT_TOOM_MUL_CUTOFF
-# define MP_TOOM_SQR_CUTOFF MP_DEFAULT_TOOM_SQR_CUTOFF
+# define MP_MUL_KARATSUBA_CUTOFF MP_DEFAULT_MUL_KARATSUBA_CUTOFF
+# define MP_SQR_KARATSUBA_CUTOFF MP_DEFAULT_SQR_KARATSUBA_CUTOFF
+# define MP_MUL_TOOM_CUTOFF MP_DEFAULT_MUL_TOOM_CUTOFF
+# define MP_SQR_TOOM_CUTOFF MP_DEFAULT_SQR_TOOM_CUTOFF
#endif
/* define heap macros */
@@ -188,23 +158,23 @@ MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
/* lowlevel functions, do not call! */
-MP_PRIVATE bool s_mp_get_bit(const mp_int *a, unsigned int b);
+MP_PRIVATE bool s_mp_get_bit(const mp_int *a, int b);
MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
-MP_PRIVATE mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
-MP_PRIVATE mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
-MP_PRIVATE mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
-MP_PRIVATE mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul_high_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul_high(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+MP_PRIVATE mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
-MP_PRIVATE mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
-MP_PRIVATE mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
-MP_PRIVATE mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul_balance(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul_karatsuba(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_PRIVATE mp_err s_mp_mul_toom(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_PRIVATE mp_err s_mp_sqr_karatsuba(const mp_int *a, mp_int *b) MP_WUR;
+MP_PRIVATE mp_err s_mp_sqr_toom(const mp_int *a, mp_int *b) MP_WUR;
+MP_PRIVATE mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_PRIVATE mp_err s_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+MP_PRIVATE mp_err s_mp_montgomery_reduce_comba(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
@@ -215,6 +185,9 @@ MP_PRIVATE uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base);
MP_PRIVATE mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);
MP_PRIVATE mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
MP_PRIVATE mp_err s_mp_div_small(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
+MP_PRIVATE void s_mp_zero_buf(void *mem, size_t size);
+MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits);
+MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
/* TODO: jenkins prng is not thread safe as of now */
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
@@ -247,7 +220,7 @@ extern MP_PRIVATE const mp_digit s_mp_prime_tab[];
} \
a->used = i; \
a->sign = MP_ZPOS; \
- MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
+ s_mp_zero_digs(a->dp + a->used, a->alloc - a->used); \
}
#define MP_SET_SIGNED(name, uname, type, utype) \
diff --git a/tommath_superclass.h b/tommath_superclass.h
index 6961a59..db927d6 100644
--- a/tommath_superclass.h
+++ b/tommath_superclass.h
@@ -76,23 +76,23 @@
* like removing support for even moduli, etc...
*/
# ifdef LTM_LAST
+# undef MP_DIV_3_C
# undef MP_DR_IS_MODULUS_C
-# undef MP_DR_SETUP_C
# undef MP_DR_REDUCE_C
-# undef MP_DIV_3_C
-# undef MP_REDUCE_2K_SETUP_C
+# undef MP_DR_SETUP_C
# undef MP_REDUCE_2K_C
+# undef MP_REDUCE_2K_SETUP_C
# undef MP_REDUCE_IS_2K_C
# undef MP_REDUCE_SETUP_C
-# undef S_MP_BALANCE_MUL_C
# undef S_MP_EXPTMOD_C
-# undef S_MP_INVMOD_FAST_C
-# undef S_MP_KARATSUBA_MUL_C
-# undef S_MP_KARATSUBA_SQR_C
-# undef S_MP_MUL_HIGH_DIGS_C
-# undef S_MP_MUL_HIGH_DIGS_FAST_C
-# undef S_MP_TOOM_MUL_C
-# undef S_MP_TOOM_SQR_C
+# undef S_MP_INVMOD_ODD_C
+# undef S_MP_MUL_BALANCE_C
+# undef S_MP_MUL_HIGH_C
+# undef S_MP_MUL_HIGH_COMBA_C
+# undef S_MP_MUL_KARATSUBA_C
+# undef S_MP_MUL_TOOM_C
+# undef S_MP_SQR_KARATSUBA_C
+# undef S_MP_SQR_TOOM_C
# ifndef SC_RSA_1_WITH_TESTS
# undef MP_REDUCE_C
@@ -104,7 +104,7 @@
* trouble.
*/
# undef MP_MONTGOMERY_REDUCE_C
-# undef S_MP_MUL_DIGS_C
+# undef S_MP_MUL_C
# undef S_MP_SQR_C
# endif