Commit 4534056c28b3458fa167087899bd195c30318222

Daniel Mendler 2019-05-13T00:22:18

use enums mp_err, mp_ord, mp_bool, mp_sign * MP_USE_ENUMS enables enums * Wc++-compat catches some implicit conversions if MP_USE_ENUMS is defined * 100% backwards compatible API/ABI if MP_USE_ENUMS is not defined

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
diff --git a/.travis.yml b/.travis.yml
index a6a097d..358f03c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -112,7 +112,8 @@ matrix:
             - gcc-4.9
 
     # clang for x86-64 architecture (64-bit longs and 64-bit pointers)
-    - env: SANITIZER=1 CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 CONV_WARNINGS=relaxed BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
     - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
       addons:
         apt:
diff --git a/bn_deprecated.c b/bn_deprecated.c
index 7a20583..5dc3da9 100644
--- a/bn_deprecated.c
+++ b/bn_deprecated.c
@@ -7,73 +7,73 @@
 /* SPDX-License-Identifier: Unlicense */
 #include <tommath_private.h>
 #ifdef BN_FAST_MP_INVMOD_C
-int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
 {
    return s_mp_invmod_fast(a, b, c);
 }
 #endif
 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
-int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
+mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
 {
    return s_mp_montgomery_reduce_fast(x, n, rho);
 }
 #endif
 #ifdef BN_FAST_S_MP_MUL_DIGS_C
-int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 {
    return s_mp_mul_digs_fast(a, b, c, digs);
 }
 #endif
 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
-int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 {
    return s_mp_mul_high_digs_fast(a, b, c, digs);
 }
 #endif
 #ifdef BN_FAST_S_MP_SQR_C
-int fast_s_mp_sqr(const mp_int *a, mp_int *b)
+mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b)
 {
    return s_mp_sqr_fast(a, b);
 }
 #endif
 #ifdef BN_MP_BALANCE_MUL_C
-int mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
 {
    return s_mp_balance_mul(a, b, c);
 }
 #endif
 #ifdef BN_MP_EXPTMOD_FAST_C
-int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
+mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
 {
    return s_mp_exptmod_fast(G, X, P, Y, redmode);
 }
 #endif
 #ifdef BN_MP_INVMOD_SLOW_C
-int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
 {
    return s_mp_invmod_slow(a, b, c);
 }
 #endif
 #ifdef BN_MP_KARATSUBA_MUL_C
-int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
 {
    return s_mp_karatsuba_mul(a, b, c);
 }
 #endif
 #ifdef BN_MP_KARATSUBA_SQR_C
-int mp_karatsuba_sqr(const mp_int *a, mp_int *b)
+mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b)
 {
    return s_mp_karatsuba_sqr(a, b);
 }
 #endif
 #ifdef BN_MP_TOOM_MUL_C
-int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
 {
    return s_mp_toom_mul(a, b, c);
 }
 #endif
 #ifdef BN_MP_TOOM_SQR_C
-int mp_toom_sqr(const mp_int *a, mp_int *b)
+mp_err mp_toom_sqr(const mp_int *a, mp_int *b)
 {
    return s_mp_toom_sqr(a, b);
 }
diff --git a/bn_mp_2expt.c b/bn_mp_2expt.c
index 2b669da..993b4e3 100644
--- a/bn_mp_2expt.c
+++ b/bn_mp_2expt.c
@@ -8,9 +8,9 @@
  * Simple algorithm which zeroes the int, grows it then just sets one bit
  * as required.
  */
-int mp_2expt(mp_int *a, int b)
+mp_err mp_2expt(mp_int *a, int b)
 {
-   int     res;
+   mp_err    res;
 
    /* zero a as per default */
    mp_zero(a);
diff --git a/bn_mp_abs.c b/bn_mp_abs.c
index 8e6855f..f2b0526 100644
--- a/bn_mp_abs.c
+++ b/bn_mp_abs.c
@@ -7,9 +7,9 @@
  *
  * Simple function copies the input and fixes the sign to positive
  */
-int mp_abs(const mp_int *a, mp_int *b)
+mp_err mp_abs(const mp_int *a, mp_int *b)
 {
-   int     res;
+   mp_err     res;
 
    /* copy a to b */
    if (a != b) {
diff --git a/bn_mp_add.c b/bn_mp_add.c
index dab0820..1ded200 100644
--- a/bn_mp_add.c
+++ b/bn_mp_add.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* high level addition (handles signs) */
-int mp_add(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     sa, sb, res;
+   mp_sign sa, sb;
+   mp_err res;
 
    /* get sign of both inputs */
    sa = a->sign;
diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c
index c02cb4e..f96674b 100644
--- a/bn_mp_add_d.c
+++ b/bn_mp_add_d.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* single digit addition */
-int mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
+mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
 {
-   int     res, ix, oldused;
+   mp_err     res;
+   int ix, oldused;
    mp_digit *tmpa, *tmpc, mu;
 
    /* grow c as required */
diff --git a/bn_mp_addmod.c b/bn_mp_addmod.c
index 72b5720..9205b1c 100644
--- a/bn_mp_addmod.c
+++ b/bn_mp_addmod.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* d = a + b (mod c) */
-int mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
+mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
 {
-   int     res;
+   mp_err  res;
    mp_int  t;
 
    if ((res = mp_init(&t)) != MP_OKAY) {
diff --git a/bn_mp_and.c b/bn_mp_and.c
index 78e42d9..cca5ca8 100644
--- a/bn_mp_and.c
+++ b/bn_mp_and.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* AND two ints together */
-int mp_and(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     res, ix, px;
+   int ix, px;
+   mp_err res;
    mp_int  t;
    const mp_int *x;
 
diff --git a/bn_mp_cmp.c b/bn_mp_cmp.c
index 6fd9371..ced4840 100644
--- a/bn_mp_cmp.c
+++ b/bn_mp_cmp.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* compare two ints (signed)*/
-int mp_cmp(const mp_int *a, const mp_int *b)
+mp_ord mp_cmp(const mp_int *a, const mp_int *b)
 {
    /* compare based on sign */
    if (a->sign != b->sign) {
diff --git a/bn_mp_cmp_d.c b/bn_mp_cmp_d.c
index f0aa507..5a8337b 100644
--- a/bn_mp_cmp_d.c
+++ b/bn_mp_cmp_d.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* compare a digit */
-int mp_cmp_d(const mp_int *a, mp_digit b)
+mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
 {
    /* compare based on sign */
    if (a->sign == MP_NEG) {
diff --git a/bn_mp_cmp_mag.c b/bn_mp_cmp_mag.c
index 482cef5..315a826 100644
--- a/bn_mp_cmp_mag.c
+++ b/bn_mp_cmp_mag.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* compare maginitude of two ints (unsigned) */
-int mp_cmp_mag(const mp_int *a, const mp_int *b)
+mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
 {
    int     n;
    mp_digit *tmpa, *tmpb;
diff --git a/bn_mp_complement.c b/bn_mp_complement.c
index 7fab918..3a71adb 100644
--- a/bn_mp_complement.c
+++ b/bn_mp_complement.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* b = ~a */
-int mp_complement(const mp_int *a, mp_int *b)
+mp_err mp_complement(const mp_int *a, mp_int *b)
 {
-   int res = mp_neg(a, b);
+   mp_err res = mp_neg(a, b);
    return (res == MP_OKAY) ? mp_sub_d(b, 1uL, b) : res;
 }
 #endif
diff --git a/bn_mp_copy.c b/bn_mp_copy.c
index 19fa9dc..9e023bb 100644
--- a/bn_mp_copy.c
+++ b/bn_mp_copy.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* copy, b = a */
-int mp_copy(const mp_int *a, mp_int *b)
+mp_err mp_copy(const mp_int *a, mp_int *b)
 {
-   int     res, n;
+   int n;
+   mp_err res;
 
    /* if dst == src do nothing */
    if (a == b) {
diff --git a/bn_mp_decr.c b/bn_mp_decr.c
index 7bd248d..2e85ecb 100644
--- a/bn_mp_decr.c
+++ b/bn_mp_decr.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* Decrement "a" by one like "a--". Changes input! */
-int mp_decr(mp_int *a)
+mp_err mp_decr(mp_int *a)
 {
-   int e = MP_OKAY;
+   mp_err e = MP_OKAY;
    if (MP_IS_ZERO(a)) {
       mp_set(a,1uL);
       a->sign = MP_NEG;
diff --git a/bn_mp_div.c b/bn_mp_div.c
index 53a342e..9bd80ea 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -6,10 +6,11 @@
 #ifdef BN_MP_DIV_SMALL
 
 /* slower bit-bang division... also smaller */
-int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
+mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
 {
    mp_int ta, tb, tq, q;
-   int    res, n, n2;
+   int     n, n2;
+   mp_err res;
 
    /* is divisor zero ? */
    if (MP_IS_ZERO(b)) {
@@ -88,10 +89,12 @@ LBL_ERR:
  * The overall algorithm is as described as
  * 14.20 from HAC but fixed to treat these cases.
 */
-int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
+mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
 {
    mp_int  q, x, y, t1, t2;
-   int     res, n, t, i, norm, neg;
+   int     n, t, i, norm;
+   mp_sign neg;
+   mp_err  res;
 
    /* is divisor zero ? */
    if (MP_IS_ZERO(b)) {
diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c
index 7935235..a51510b 100644
--- a/bn_mp_div_2.c
+++ b/bn_mp_div_2.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* b = a/2 */
-int mp_div_2(const mp_int *a, mp_int *b)
+mp_err mp_div_2(const mp_int *a, mp_int *b)
 {
-   int     x, res, oldused;
+   int     x, oldused;
+   mp_err res;
 
    /* copy */
    if (b->alloc < a->used) {
diff --git a/bn_mp_div_2d.c b/bn_mp_div_2d.c
index e13c471..d8c2377 100644
--- a/bn_mp_div_2d.c
+++ b/bn_mp_div_2d.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
-int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *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, res;
+   int     x;
+   mp_err res;
 
    /* if the shift count is <= 0 then we do no work */
    if (b <= 0) {
diff --git a/bn_mp_div_3.c b/bn_mp_div_3.c
index ccd8e85..bfa5710 100644
--- a/bn_mp_div_3.c
+++ b/bn_mp_div_3.c
@@ -4,12 +4,13 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* divide by three (based on routine from MPI and the GMP manual) */
-int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
+mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
 {
    mp_int   q;
    mp_word  w, t;
    mp_digit b;
-   int      res, ix;
+   mp_err   res;
+   int      ix;
 
    /* b = 2**MP_DIGIT_BIT / 3 */
    b = ((mp_word)1 << (mp_word)MP_DIGIT_BIT) / (mp_word)3;
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index c75e170..fcfe36a 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -22,12 +22,13 @@ static int s_is_power_of_two(mp_digit b, int *p)
 }
 
 /* single digit division (based on routine from MPI) */
-int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
+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;
-   int     res, ix;
+   mp_err res;
+   int ix;
 
    /* cannot divide by zero */
    if (b == 0u) {
diff --git a/bn_mp_dr_is_modulus.c b/bn_mp_dr_is_modulus.c
index d59940f..83760ea 100644
--- a/bn_mp_dr_is_modulus.c
+++ b/bn_mp_dr_is_modulus.c
@@ -4,13 +4,13 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines if a number is a valid DR modulus */
-int mp_dr_is_modulus(const mp_int *a)
+mp_bool mp_dr_is_modulus(const mp_int *a)
 {
    int ix;
 
    /* must be at least two digits */
    if (a->used < 2) {
-      return 0;
+      return MP_NO;
    }
 
    /* must be of the form b**k - a [a <= b] so all
@@ -18,10 +18,10 @@ int mp_dr_is_modulus(const mp_int *a)
     */
    for (ix = 1; ix < a->used; ix++) {
       if (a->dp[ix] != MP_MASK) {
-         return 0;
+         return MP_NO;
       }
    }
-   return 1;
+   return MP_YES;
 }
 
 #endif
diff --git a/bn_mp_dr_reduce.c b/bn_mp_dr_reduce.c
index 25b7b15..ffc33a6 100644
--- a/bn_mp_dr_reduce.c
+++ b/bn_mp_dr_reduce.c
@@ -17,9 +17,10 @@
  *
  * Input x must be in the range 0 <= x <= (n-1)**2
  */
-int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
+mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
 {
-   int      err, i, m;
+   mp_err      err;
+   int i, m;
    mp_word  r;
    mp_digit mu, *tmpx1, *tmpx2;
 
diff --git a/bn_mp_error_to_string.c b/bn_mp_error_to_string.c
index 97b35ae..e936ec1 100644
--- a/bn_mp_error_to_string.c
+++ b/bn_mp_error_to_string.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* return a char * string for a given code */
-const char *mp_error_to_string(int code)
+const char *mp_error_to_string(mp_err code)
 {
    switch (code) {
    case MP_OKAY:
diff --git a/bn_mp_export.c b/bn_mp_export.c
index 2167e89..5c8a492 100644
--- a/bn_mp_export.c
+++ b/bn_mp_export.c
@@ -6,10 +6,10 @@
 /* based on gmp's mpz_export.
  * see http://gmplib.org/manual/Integer-Import-and-Export.html
  */
-int mp_export(void *rop, size_t *countp, int order, size_t size,
-              int endian, size_t nails, const mp_int *op)
+mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
+                 int endian, size_t nails, const mp_int *op)
 {
-   int result;
+   mp_err result;
    size_t odd_nails, nail_bytes, i, j, bits, count;
    unsigned char odd_nail_mask;
 
diff --git a/bn_mp_expt_d.c b/bn_mp_expt_d.c
index b2921ea..41a7cbe 100644
--- a/bn_mp_expt_d.c
+++ b/bn_mp_expt_d.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* wrapper function for mp_expt_d_ex() */
-int mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
+mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
 {
    return mp_expt_d_ex(a, b, c, 0);
 }
diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c
index 9ce2d13..f10e42b 100644
--- a/bn_mp_expt_d_ex.c
+++ b/bn_mp_expt_d_ex.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* calculate c = a**b  using a square-multiply algorithm */
-int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
+mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
 {
-   int     res;
+   mp_err res;
    unsigned int x;
 
    mp_int  g;
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index bcef306..9bf3d8c 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -8,7 +8,7 @@
  * embedded in the normal function but that wasted alot of stack space
  * for nothing (since 99% of the time the Montgomery code would be called)
  */
-int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
+mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
 {
    int dr;
 
@@ -21,7 +21,7 @@ int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
    if (X->sign == MP_NEG) {
 #ifdef BN_MP_INVMOD_C
       mp_int tmpG, tmpX;
-      int err;
+      mp_err err;
 
       /* first compute 1/G mod P */
       if ((err = mp_init(&tmpG)) != MP_OKAY) {
@@ -61,7 +61,7 @@ int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
 
 #ifdef BN_MP_DR_IS_MODULUS_C
    /* is it a DR modulus? */
-   dr = mp_dr_is_modulus(P);
+   dr = mp_dr_is_modulus(P) == MP_YES;
 #else
    /* default to no */
    dr = 0;
@@ -70,7 +70,7 @@ int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
 #ifdef BN_MP_REDUCE_IS_2K_C
    /* if not, is it a unrestricted DR modulus? */
    if (dr == 0) {
-      dr = mp_reduce_is_2k(P) << 1;
+      dr = (mp_reduce_is_2k(P) == MP_YES) << 1;
    }
 #endif
 
diff --git a/bn_mp_exteuclid.c b/bn_mp_exteuclid.c
index baeeede..3af7467 100644
--- a/bn_mp_exteuclid.c
+++ b/bn_mp_exteuclid.c
@@ -6,10 +6,10 @@
 /* Extended euclidean algorithm of (a, b) produces
    a*u1 + b*u2 = u3
  */
-int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
+mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
 {
    mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp;
-   int err;
+   mp_err err;
 
    if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) {
       return err;
diff --git a/bn_mp_fread.c b/bn_mp_fread.c
index d5c5767..bbb59a5 100644
--- a/bn_mp_fread.c
+++ b/bn_mp_fread.c
@@ -5,9 +5,11 @@
 
 #ifndef MP_NO_FILE
 /* read a bigint from a file stream in ASCII */
-int mp_fread(mp_int *a, int radix, FILE *stream)
+mp_err mp_fread(mp_int *a, int radix, FILE *stream)
 {
-   int err, ch, neg, y;
+   mp_err err;
+   mp_sign neg;
+   int ch, y;
    unsigned pos;
 
    /* clear a */
diff --git a/bn_mp_fwrite.c b/bn_mp_fwrite.c
index 89ab29f..744e5ff 100644
--- a/bn_mp_fwrite.c
+++ b/bn_mp_fwrite.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 #ifndef MP_NO_FILE
-int mp_fwrite(const mp_int *a, int radix, FILE *stream)
+mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
 {
    char *buf;
-   int err, len;
+   mp_err err;
+   int len;
 
    if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
       return err;
diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c
index 51cb195..e74372b 100644
--- a/bn_mp_gcd.c
+++ b/bn_mp_gcd.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* Greatest Common Divisor using the binary method */
-int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
 {
    mp_int  u, v;
-   int     k, u_lsb, v_lsb, res;
+   int     k, u_lsb, v_lsb;
+   mp_err res;
 
    /* either zero than gcd is the largest */
    if (MP_IS_ZERO(a)) {
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 7df66f9..9e904c5 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* grow as required */
-int mp_grow(mp_int *a, int size)
+mp_err mp_grow(mp_int *a, int size)
 {
    int     i;
    mp_digit *tmp;
diff --git a/bn_mp_ilogb.c b/bn_mp_ilogb.c
index c0f40f7..d72c952 100644
--- a/bn_mp_ilogb.c
+++ b/bn_mp_ilogb.c
@@ -70,9 +70,10 @@ static mp_digit s_digit_ilogb(mp_digit base, mp_digit n)
          as is the output of mp_bitcount.
          With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only!
 */
-int mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
+mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
 {
-   int err, cmp;
+   mp_err err;
+   int cmp;
    unsigned int high, low, mid;
    mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
    mp_digit tmp;
diff --git a/bn_mp_import.c b/bn_mp_import.c
index 93556ea..37685be 100644
--- a/bn_mp_import.c
+++ b/bn_mp_import.c
@@ -6,10 +6,10 @@
 /* based on gmp's mpz_import.
  * see http://gmplib.org/manual/Integer-Import-and-Export.html
  */
-int mp_import(mp_int *rop, size_t count, int order, size_t size,
-              int endian, size_t nails, const void *op)
+mp_err mp_import(mp_int *rop, size_t count, int order, size_t size,
+                 int endian, size_t nails, const void *op)
 {
-   int result;
+   mp_err result;
    size_t odd_nails, nail_bytes, i, j;
    unsigned char odd_nail_mask;
 
diff --git a/bn_mp_incr.c b/bn_mp_incr.c
index 15a9ef5..5e47d2c 100644
--- a/bn_mp_incr.c
+++ b/bn_mp_incr.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* Increment "a" by one like "a++". Changes input! */
-int mp_incr(mp_int *a)
+mp_err mp_incr(mp_int *a)
 {
-   int e = MP_OKAY;
+   mp_err e = MP_OKAY;
    if (MP_IS_ZERO(a)) {
       mp_set(a,1uL);
       return MP_OKAY;
diff --git a/bn_mp_init.c b/bn_mp_init.c
index 99c74aa..2eb7924 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* init a new mp_int */
-int mp_init(mp_int *a)
+mp_err mp_init(mp_int *a)
 {
    /* allocate memory required and clear it */
    a->dp = (mp_digit *) MP_CALLOC((size_t)MP_PREC, sizeof(mp_digit));
diff --git a/bn_mp_init_copy.c b/bn_mp_init_copy.c
index 12f6ffc..e30d501 100644
--- a/bn_mp_init_copy.c
+++ b/bn_mp_init_copy.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* creates "a" then copies b into it */
-int mp_init_copy(mp_int *a, const mp_int *b)
+mp_err mp_init_copy(mp_int *a, const mp_int *b)
 {
-   int     res;
+   mp_err     res;
 
    if ((res = mp_init_size(a, b->used)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_init_multi.c b/bn_mp_init_multi.c
index cd097ee..09fc25f 100644
--- a/bn_mp_init_multi.c
+++ b/bn_mp_init_multi.c
@@ -5,7 +5,7 @@
 
 #include <stdarg.h>
 
-int mp_init_multi(mp_int *mp, ...)
+mp_err mp_init_multi(mp_int *mp, ...)
 {
    mp_err res = MP_OKAY;      /* Assume ok until proven otherwise */
    int n = 0;                 /* Number of ok inits */
diff --git a/bn_mp_init_set.c b/bn_mp_init_set.c
index a041684..5068f2b 100644
--- a/bn_mp_init_set.c
+++ b/bn_mp_init_set.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* initialize and set a digit */
-int mp_init_set(mp_int *a, mp_digit b)
+mp_err mp_init_set(mp_int *a, mp_digit b)
 {
-   int err;
+   mp_err err;
    if ((err = mp_init(a)) != MP_OKAY) {
       return err;
    }
diff --git a/bn_mp_init_set_int.c b/bn_mp_init_set_int.c
index c364c87..fbb2b6b 100644
--- a/bn_mp_init_set_int.c
+++ b/bn_mp_init_set_int.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* initialize and set a digit */
-int mp_init_set_int(mp_int *a, unsigned long b)
+mp_err mp_init_set_int(mp_int *a, unsigned long b)
 {
-   int err;
+   mp_err err;
    if ((err = mp_init(a)) != MP_OKAY) {
       return err;
    }
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index d97f8be..d622687 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* init an mp_init for a given size */
-int mp_init_size(mp_int *a, int size)
+mp_err mp_init_size(mp_int *a, int size)
 {
    size = MP_MAX(MP_MIN_PREC, size);
 
diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c
index 637f155..3142ac9 100644
--- a/bn_mp_is_square.c
+++ b/bn_mp_is_square.c
@@ -26,9 +26,9 @@ static const char rem_105[105] = {
 };
 
 /* Store non-zero to ret if arg is square, and zero if not */
-int mp_is_square(const mp_int *arg, int *ret)
+mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
 {
-   int           res;
+   mp_err        res;
    mp_digit      c;
    mp_int        t;
    unsigned long r;
diff --git a/bn_mp_iseven.c b/bn_mp_iseven.c
index baea2ad..5cb9622 100644
--- a/bn_mp_iseven.c
+++ b/bn_mp_iseven.c
@@ -3,7 +3,7 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-int mp_iseven(const mp_int *a)
+mp_bool mp_iseven(const mp_int *a)
 {
    return MP_IS_EVEN(a) ? MP_YES : MP_NO;
 }
diff --git a/bn_mp_isodd.c b/bn_mp_isodd.c
index 1f11e2f..bf17646 100644
--- a/bn_mp_isodd.c
+++ b/bn_mp_isodd.c
@@ -3,7 +3,7 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-int mp_isodd(const mp_int *a)
+mp_bool mp_isodd(const mp_int *a)
 {
    return MP_IS_ODD(a) ? MP_YES : MP_NO;
 }
diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c
index 04935ca..f050bdf 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -6,7 +6,7 @@
 /* computes the jacobi c = (a | n) (or Legendre if n is prime)
  * Kept for legacy reasons, please use mp_kronecker() instead
  */
-int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
+mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c)
 {
    /* if a < 0 return MP_VAL */
    if (a->sign == MP_NEG) {
diff --git a/bn_mp_kronecker.c b/bn_mp_kronecker.c
index a40e7c1..2b7fd6e 100644
--- a/bn_mp_kronecker.c
+++ b/bn_mp_kronecker.c
@@ -17,11 +17,10 @@
      publisher={Springer Science \& Business Media}
     }
  */
-int mp_kronecker(const mp_int *a, const mp_int *p, int *c)
+mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
 {
    mp_int a1, p1, r;
-
-   int e = MP_OKAY;
+   mp_err e = MP_OKAY;
    int v, k;
 
    static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1};
diff --git a/bn_mp_lcm.c b/bn_mp_lcm.c
index cf95afe..ef7ed12 100644
--- a/bn_mp_lcm.c
+++ b/bn_mp_lcm.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* computes least common multiple as |a*b|/(a, b) */
-int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     res;
+   mp_err  res;
    mp_int  t1, t2;
 
 
diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c
index 7faec30..b8da2b6 100644
--- a/bn_mp_lshd.c
+++ b/bn_mp_lshd.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* shift left a certain amount of digits */
-int mp_lshd(mp_int *a, int b)
+mp_err mp_lshd(mp_int *a, int b)
 {
-   int     x, res;
+   int x;
+   mp_err res;
 
    /* if its less than zero return */
    if (b <= 0) {
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index 0ef738e..3b0d38f 100644
--- a/bn_mp_mod.c
+++ b/bn_mp_mod.c
@@ -4,10 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
-int mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
 {
    mp_int  t;
-   int     res;
+   mp_err  res;
 
    if ((res = mp_init_size(&t, b->used)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_mod_2d.c b/bn_mp_mod_2d.c
index db58771..a02672f 100644
--- a/bn_mp_mod_2d.c
+++ b/bn_mp_mod_2d.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* calc a value mod 2**b */
-int mp_mod_2d(const mp_int *a, int b, mp_int *c)
+mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
 {
-   int     x, res;
+   int x;
+   mp_err res;
 
    /* if b is <= 0 then zero the int */
    if (b <= 0) {
diff --git a/bn_mp_mod_d.c b/bn_mp_mod_d.c
index 89f05dc..0b6c12a 100644
--- a/bn_mp_mod_d.c
+++ b/bn_mp_mod_d.c
@@ -3,7 +3,7 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c)
+mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c)
 {
    return mp_div_d(a, b, NULL, c);
 }
diff --git a/bn_mp_montgomery_calc_normalization.c b/bn_mp_montgomery_calc_normalization.c
index 1331db1..3648ab3 100644
--- a/bn_mp_montgomery_calc_normalization.c
+++ b/bn_mp_montgomery_calc_normalization.c
@@ -9,9 +9,10 @@
  * The method is slightly modified to shift B unconditionally upto just under
  * the leading bit of b.  This saves alot of multiple precision shifting.
  */
-int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
+mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
 {
-   int     x, bits, res;
+   int    x, bits;
+   mp_err res;
 
    /* how many bits of last digit does b use */
    bits = mp_count_bits(b) % MP_DIGIT_BIT;
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index c379675..2640873 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* computes xR**-1 == x (mod N) via Montgomery Reduction */
-int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
+mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
 {
-   int     ix, res, digs;
+   int      ix, digs;
+   mp_err   res;
    mp_digit mu;
 
    /* can the fast reduction [comba] method be used?
diff --git a/bn_mp_montgomery_setup.c b/bn_mp_montgomery_setup.c
index ac5f532..39f6e9d 100644
--- a/bn_mp_montgomery_setup.c
+++ b/bn_mp_montgomery_setup.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* setups the montgomery reduction stuff */
-int mp_montgomery_setup(const mp_int *n, mp_digit *rho)
+mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho)
 {
    mp_digit x, b;
 
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index 68d8fb8..2fd9df0 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* high level multiplication (handles sign) */
-int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     res, neg;
+   mp_err  res;
+   mp_sign neg;
 #ifdef BN_S_MP_BALANCE_MUL_C
    int len_b, len_a;
 #endif
diff --git a/bn_mp_mul_2.c b/bn_mp_mul_2.c
index 75692d3..eba23bf 100644
--- a/bn_mp_mul_2.c
+++ b/bn_mp_mul_2.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* b = a*2 */
-int mp_mul_2(const mp_int *a, mp_int *b)
+mp_err mp_mul_2(const mp_int *a, mp_int *b)
 {
-   int     x, res, oldused;
+   int     x, oldused;
+   mp_err res;
 
    /* grow to accomodate result */
    if (b->alloc < (a->used + 1)) {
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
index f41618b..9ff0d63 100644
--- a/bn_mp_mul_2d.c
+++ b/bn_mp_mul_2d.c
@@ -4,10 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* shift left by a certain bit count */
-int mp_mul_2d(const mp_int *a, int b, mp_int *c)
+mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c)
 {
    mp_digit d;
-   int      res;
+   mp_err   res;
 
    /* copy */
    if (a != c) {
diff --git a/bn_mp_mul_d.c b/bn_mp_mul_d.c
index 74d69ce..fe7ec7b 100644
--- a/bn_mp_mul_d.c
+++ b/bn_mp_mul_d.c
@@ -4,11 +4,12 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* multiply by a digit */
-int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
+mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
 {
    mp_digit u, *tmpa, *tmpc;
    mp_word  r;
-   int      ix, res, olduse;
+   mp_err   res;
+   int      ix, olduse;
 
    /* make sure c is big enough to hold a*b */
    if (c->alloc < (a->used + 1)) {
diff --git a/bn_mp_mulmod.c b/bn_mp_mulmod.c
index 3c5a63a..10a38db 100644
--- a/bn_mp_mulmod.c
+++ b/bn_mp_mulmod.c
@@ -4,10 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* d = a * b (mod c) */
-int mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
+mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
 {
-   int     res;
-   mp_int  t;
+   mp_err res;
+   mp_int t;
 
    if ((res = mp_init_size(&t, c->used)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_n_root.c b/bn_mp_n_root.c
index 3dfbf38..5b5e743 100644
--- a/bn_mp_n_root.c
+++ b/bn_mp_n_root.c
@@ -6,7 +6,7 @@
 /* wrapper function for mp_n_root_ex()
  * computes c = (a)**(1/b) such that (c)**b <= a and (c+1)**b > a
  */
-int mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
+mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
 {
    return mp_n_root_ex(a, b, c, 0);
 }
diff --git a/bn_mp_n_root_ex.c b/bn_mp_n_root_ex.c
index 42be929..c37f575 100644
--- a/bn_mp_n_root_ex.c
+++ b/bn_mp_n_root_ex.c
@@ -12,11 +12,12 @@
  * which will find the root in log(N) time where
  * each step involves a fair bit.
  */
-int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
+mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
 {
-   mp_int  t1, t2, t3, a_;
-   int   res, cmp;
-   int ilog2;
+   mp_int t1, t2, t3, a_;
+   int    cmp;
+   int    ilog2;
+   mp_err res;
 
    /* input must be positive if b is even */
    if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
diff --git a/bn_mp_neg.c b/bn_mp_neg.c
index 1c5b9a0..3ca20f8 100644
--- a/bn_mp_neg.c
+++ b/bn_mp_neg.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* b = -a */
-int mp_neg(const mp_int *a, mp_int *b)
+mp_err mp_neg(const mp_int *a, mp_int *b)
 {
-   int     res;
+   mp_err res;
    if (a != b) {
       if ((res = mp_copy(a, b)) != MP_OKAY) {
          return res;
diff --git a/bn_mp_or.c b/bn_mp_or.c
index f3704f7..5ef3f5a 100644
--- a/bn_mp_or.c
+++ b/bn_mp_or.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* OR two ints together */
-int mp_or(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     res, ix, px;
+   int     ix, px;
+   mp_err  res;
    mp_int  t;
    const mp_int *x;
 
diff --git a/bn_mp_prime_fermat.c b/bn_mp_prime_fermat.c
index c4f7f3b..af3e884 100644
--- a/bn_mp_prime_fermat.c
+++ b/bn_mp_prime_fermat.c
@@ -11,10 +11,10 @@
  *
  * Sets result to 1 if the congruence holds, or zero otherwise.
  */
-int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result)
+mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result)
 {
    mp_int  t;
-   int     err;
+   mp_err  err;
 
    /* default to composite  */
    *result = MP_NO;
diff --git a/bn_mp_prime_frobenius_underwood.c b/bn_mp_prime_frobenius_underwood.c
index 8855cfb..5b7b099 100644
--- a/bn_mp_prime_frobenius_underwood.c
+++ b/bn_mp_prime_frobenius_underwood.c
@@ -23,12 +23,12 @@
 #else
 #define LTM_FROBENIUS_UNDERWOOD_A 32764
 #endif
-int mp_prime_frobenius_underwood(const mp_int *N, int *result)
+mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
 {
    mp_int T1z, T2z, Np1z, sz, tz;
 
    int a, ap2, length, i, j, isset;
-   int e;
+   mp_err e;
 
    *result = MP_NO;
 
@@ -130,7 +130,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
          goto LBL_FU_ERR;
       }
       if ((isset = mp_get_bit(&Np1z, i)) == MP_VAL) {
-         e = isset;
+         e = MP_VAL;
          goto LBL_FU_ERR;
       }
       if (isset == MP_YES) {
diff --git a/bn_mp_prime_is_divisible.c b/bn_mp_prime_is_divisible.c
index 2697cc7..45c1c96 100644
--- a/bn_mp_prime_is_divisible.c
+++ b/bn_mp_prime_is_divisible.c
@@ -8,9 +8,10 @@
  *
  * sets result to 0 if not, 1 if yes
  */
-int mp_prime_is_divisible(const mp_int *a, int *result)
+mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
 {
-   int     err, ix;
+   int      ix;
+   mp_err   err;
    mp_digit res;
 
    /* default to not */
diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index 7c93020..db94dec 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -14,10 +14,12 @@ static unsigned int s_floor_ilog2(int value)
 }
 
 
-int mp_prime_is_prime(const mp_int *a, int t, int *result)
+mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
 {
    mp_int  b;
-   int     ix, err, res, p_max = 0, size_a, len;
+   int     ix, p_max = 0, size_a, len;
+   mp_bool res;
+   mp_err  err;
    unsigned int fips_rand, mask;
 
    /* default to no */
@@ -32,11 +34,11 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result)
    /* N > 3 */
    if (a->used == 1) {
       if ((a->dp[0] == 0u) || (a->dp[0] == 1u)) {
-         *result = 0;
+         *result = MP_NO;
          return MP_OKAY;
       }
       if (a->dp[0] == 2u) {
-         *result = 1;
+         *result = MP_YES;
          return MP_OKAY;
       }
    }
diff --git a/bn_mp_prime_miller_rabin.c b/bn_mp_prime_miller_rabin.c
index c46dcfb..96470db 100644
--- a/bn_mp_prime_miller_rabin.c
+++ b/bn_mp_prime_miller_rabin.c
@@ -10,10 +10,11 @@
  * Randomly the chance of error is no more than 1/4 and often
  * very much lower.
  */
-int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result)
+mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result)
 {
    mp_int  n1, y, r;
-   int     s, j, err;
+   mp_err  err;
+   int     s, j;
 
    /* default */
    *result = MP_NO;
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index 2e27eed..f5ce9eb 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -8,9 +8,11 @@
  *
  * bbs_style = 1 means the prime must be congruent to 3 mod 4
  */
-int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
+mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style)
 {
-   int      err, res = MP_NO, x, y;
+   int      x, y;
+   mp_err  err;
+   mp_bool res = MP_NO;
    mp_digit res_tab[MP_PRIME_SIZE], step, kstep;
    mp_int   b;
 
diff --git a/bn_mp_prime_rand.c b/bn_mp_prime_rand.c
index cd520ff..b4b08ba 100644
--- a/bn_mp_prime_rand.c
+++ b/bn_mp_prime_rand.c
@@ -18,10 +18,12 @@
  */
 
 /* This is possibly the mother of all prime generation functions, muahahahahaha! */
-static int s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
+static mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
 {
    unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
-   int res, err, bsize, maskOR_msb_offset;
+   int bsize, maskOR_msb_offset;
+   mp_bool res;
+   mp_err err;
 
    /* sanity check the input */
    if ((size <= 1) || (t <= 0)) {
@@ -130,12 +132,12 @@ static int s_mp_rand_cb(unsigned char *dst, int len, void *dat)
    return len;
 }
 
-int mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
+mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
 {
    return s_mp_prime_random_ex(a, t, size, flags, cb, dat);
 }
 
-int mp_prime_rand(mp_int *a, int t, int size, int flags)
+mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
 {
    return s_mp_prime_random_ex(a, t, size, flags, s_mp_rand_cb, NULL);
 }
diff --git a/bn_mp_prime_strong_lucas_selfridge.c b/bn_mp_prime_strong_lucas_selfridge.c
index 44143b0..0316cac 100644
--- a/bn_mp_prime_strong_lucas_selfridge.c
+++ b/bn_mp_prime_strong_lucas_selfridge.c
@@ -19,10 +19,11 @@
  * multiply bigint a with int d and put the result in c
  * Like mp_mul_d() but with a signed long as the small input
  */
-static int s_mp_mul_si(const mp_int *a, long d, mp_int *c)
+static mp_err s_mp_mul_si(const mp_int *a, long d, mp_int *c)
 {
    mp_int t;
-   int err, neg = 0;
+   mp_err err;
+   int neg = 0;
 
    if ((err = mp_init(&t)) != MP_OKAY) {
       return err;
@@ -64,13 +65,13 @@ LBL_MPMULSI_ERR:
     (If that name sounds familiar, he is the guy who found the fdiv bug in the
      Pentium (P5x, I think) Intel processor)
 */
-int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
+mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
 {
    /* CZ TODO: choose better variable names! */
    mp_int Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz;
    /* CZ TODO: Some of them need the full 32 bit, hence the (temporary) exclusion of MP_8BIT */
    int32_t D, Ds, J, sign, P, Q, r, s, u, Nbits;
-   int e;
+   mp_err e;
    int isset, oddness;
 
    *result = MP_NO;
@@ -246,7 +247,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
          goto LBL_LS_ERR;
       }
       if ((isset = mp_get_bit(&Dz, u)) == MP_VAL) {
-         e = isset;
+         e = MP_VAL;
          goto LBL_LS_ERR;
       }
       if (isset == MP_YES) {
diff --git a/bn_mp_radix_size.c b/bn_mp_radix_size.c
index 8895ae2..ffcc3aa 100644
--- a/bn_mp_radix_size.c
+++ b/bn_mp_radix_size.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* returns size of ASCII reprensentation */
-int mp_radix_size(const mp_int *a, int radix, int *size)
+mp_err mp_radix_size(const mp_int *a, int radix, int *size)
 {
-   int     res, digs;
-   mp_int  t;
+   mp_err  res;
+   int     digs;
+   mp_int   t;
    mp_digit d;
 
    *size = 0;
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index f347c97..aad48d9 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -3,24 +3,25 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-int (*s_mp_rand_source)(void *out, size_t size) = s_mp_rand_platform;
+mp_err(*s_mp_rand_source)(void *out, size_t size) = s_mp_rand_platform;
 
-void mp_rand_source(int (*source)(void *out, size_t size))
+void mp_rand_source(mp_err(*source)(void *out, size_t size))
 {
    s_mp_rand_source = (source == NULL) ? s_mp_rand_platform : source;
 }
 
 /* makes a pseudo-random int of a given size */
-int mp_rand_digit(mp_digit *r)
+mp_err mp_rand_digit(mp_digit *r)
 {
-   int ret = s_mp_rand_source(r, sizeof(mp_digit));
+   mp_err ret = s_mp_rand_source(r, sizeof(mp_digit));
    *r &= MP_MASK;
    return ret;
 }
 
-int mp_rand(mp_int *a, int digits)
+mp_err mp_rand(mp_int *a, int digits)
 {
-   int ret, i;
+   int i;
+   mp_err ret;
 
    mp_zero(a);
 
diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c
index 913c217..57a00cc 100644
--- a/bn_mp_read_radix.c
+++ b/bn_mp_read_radix.c
@@ -6,11 +6,13 @@
 #define MP_TOUPPER(c) ((((c) >= 'a') && ((c) <= 'z')) ? (((c) + 'A') - 'a') : (c))
 
 /* read a string [ASCII] in a given radix */
-int mp_read_radix(mp_int *a, const char *str, int radix)
+mp_err mp_read_radix(mp_int *a, const char *str, int radix)
 {
-   int     y, res, neg;
+   mp_err   res;
+   int      y;
+   mp_sign  neg;
    unsigned pos;
-   char    ch;
+   char     ch;
 
    /* zero the digit bignum */
    mp_zero(a);
diff --git a/bn_mp_read_signed_bin.c b/bn_mp_read_signed_bin.c
index 62fdc0e..6d868cf 100644
--- a/bn_mp_read_signed_bin.c
+++ b/bn_mp_read_signed_bin.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* read signed bin, big endian, first byte is 0==positive or 1==negative */
-int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
+mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
 {
-   int     res;
+   mp_err res;
 
    /* read magnitude */
    if ((res = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) {
diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c
index bdd8f30..ae4e875 100644
--- a/bn_mp_read_unsigned_bin.c
+++ b/bn_mp_read_unsigned_bin.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* reads a unsigned char array, assumes the msb is stored first [big endian] */
-int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
+mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
 {
-   int     res;
+   mp_err res;
 
    /* make sure there are at least two digits */
    if (a->alloc < 2) {
diff --git a/bn_mp_reduce.c b/bn_mp_reduce.c
index e6a8bd2..4b7852b 100644
--- a/bn_mp_reduce.c
+++ b/bn_mp_reduce.c
@@ -7,10 +7,11 @@
  * precomputed via mp_reduce_setup.
  * From HAC pp.604 Algorithm 14.42
  */
-int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
+mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
 {
    mp_int  q;
-   int     res, um = m->used;
+   mp_err  res;
+   int     um = m->used;
 
    /* q = x */
    if ((res = mp_init_copy(&q, x)) != MP_OKAY) {
diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c
index d357c28..0bf2ffb 100644
--- a/bn_mp_reduce_2k.c
+++ b/bn_mp_reduce_2k.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* reduces a modulo n where n is of the form 2**p - d */
-int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d)
+mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d)
 {
    mp_int q;
-   int    p, res;
+   mp_err res;
+   int    p;
 
    if ((res = mp_init(&q)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_reduce_2k_l.c b/bn_mp_reduce_2k_l.c
index c6b743a..b2905ed 100644
--- a/bn_mp_reduce_2k_l.c
+++ b/bn_mp_reduce_2k_l.c
@@ -7,10 +7,11 @@
    This differs from reduce_2k since "d" can be larger
    than a single digit.
 */
-int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d)
+mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d)
 {
    mp_int q;
-   int    p, res;
+   mp_err res;
+   int    p;
 
    if ((res = mp_init(&q)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_reduce_2k_setup.c b/bn_mp_reduce_2k_setup.c
index f91de82..bae80d9 100644
--- a/bn_mp_reduce_2k_setup.c
+++ b/bn_mp_reduce_2k_setup.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines the setup value */
-int mp_reduce_2k_setup(const mp_int *a, mp_digit *d)
+mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d)
 {
-   int res, p;
+   mp_err res;
    mp_int tmp;
+   int    p;
 
    if ((res = mp_init(&tmp)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_reduce_2k_setup_l.c b/bn_mp_reduce_2k_setup_l.c
index aef78b6..652942c 100644
--- a/bn_mp_reduce_2k_setup_l.c
+++ b/bn_mp_reduce_2k_setup_l.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines the setup value */
-int mp_reduce_2k_setup_l(const mp_int *a, mp_int *d)
+mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d)
 {
-   int    res;
+   mp_err res;
    mp_int tmp;
 
    if ((res = mp_init(&tmp)) != MP_OKAY) {
diff --git a/bn_mp_reduce_is_2k.c b/bn_mp_reduce_is_2k.c
index ba8958f..fe3aeea 100644
--- a/bn_mp_reduce_is_2k.c
+++ b/bn_mp_reduce_is_2k.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines if mp_reduce_2k can be used */
-int mp_reduce_is_2k(const mp_int *a)
+mp_bool mp_reduce_is_2k(const mp_int *a)
 {
    int ix, iy, iw;
    mp_digit iz;
diff --git a/bn_mp_reduce_is_2k_l.c b/bn_mp_reduce_is_2k_l.c
index 2303dc3..b2c7292 100644
--- a/bn_mp_reduce_is_2k_l.c
+++ b/bn_mp_reduce_is_2k_l.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines if reduce_2k_l can be used */
-int mp_reduce_is_2k_l(const mp_int *a)
+mp_bool mp_reduce_is_2k_l(const mp_int *a)
 {
    int ix, iy;
 
diff --git a/bn_mp_reduce_setup.c b/bn_mp_reduce_setup.c
index 7cd7909..2e4f961 100644
--- a/bn_mp_reduce_setup.c
+++ b/bn_mp_reduce_setup.c
@@ -6,10 +6,9 @@
 /* pre-calculate the value required for Barrett reduction
  * For a given modulus "b" it calulates the value required in "a"
  */
-int mp_reduce_setup(mp_int *a, const mp_int *b)
+mp_err mp_reduce_setup(mp_int *a, const mp_int *b)
 {
-   int     res;
-
+   mp_err res;
    if ((res = mp_2expt(a, b->used * 2 * MP_DIGIT_BIT)) != MP_OKAY) {
       return res;
    }
diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c
index bd2ea37..efb8533 100644
--- a/bn_mp_set_double.c
+++ b/bn_mp_set_double.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559)
-int mp_set_double(mp_int *a, double b)
+mp_err mp_set_double(mp_int *a, double b)
 {
    uint64_t frac;
-   int exp, res;
+   int exp;
+   mp_err res;
    union {
       double   dbl;
       uint64_t bits;
diff --git a/bn_mp_set_int.c b/bn_mp_set_int.c
index 90573bb..a322580 100644
--- a/bn_mp_set_int.c
+++ b/bn_mp_set_int.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* set a 32-bit const */
-int mp_set_int(mp_int *a, unsigned long b)
+mp_err mp_set_int(mp_int *a, unsigned long b)
 {
    return mp_set_long(a, b & 0xFFFFFFFFUL);
 }
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index 28754f5..8b404e0 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* shrink a bignum */
-int mp_shrink(mp_int *a)
+mp_err mp_shrink(mp_int *a)
 {
    static int static_check[-(MP_PREC < MP_MIN_PREC)];
    mp_digit *tmp;
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index 5b93eab..e1e243c 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* computes b = a*a */
-int mp_sqr(const mp_int *a, mp_int *b)
+mp_err mp_sqr(const mp_int *a, mp_int *b)
 {
-   int     res;
+   mp_err res;
 
 #ifdef BN_S_MP_TOOM_SQR_C
    /* use Toom-Cook? */
diff --git a/bn_mp_sqrmod.c b/bn_mp_sqrmod.c
index fd72388..30d7c46 100644
--- a/bn_mp_sqrmod.c
+++ b/bn_mp_sqrmod.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* c = a * a (mod b) */
-int mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     res;
+   mp_err  res;
    mp_int  t;
 
    if ((res = mp_init(&t)) != MP_OKAY) {
diff --git a/bn_mp_sqrt.c b/bn_mp_sqrt.c
index ab9141c..74769f5 100644
--- a/bn_mp_sqrt.c
+++ b/bn_mp_sqrt.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* this function is less generic than mp_n_root, simpler and faster */
-int mp_sqrt(const mp_int *arg, mp_int *ret)
+mp_err mp_sqrt(const mp_int *arg, mp_int *ret)
 {
-   int res;
+   mp_err res;
    mp_int t1, t2;
 
    /* must be positive */
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index 354e9f4..86934cd 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -9,9 +9,10 @@
  *
  */
 
-int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
+mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
 {
-   int res, legendre;
+   mp_err res;
+   int legendre;
    mp_int t1, C, Q, S, Z, M, T, R, two;
    mp_digit i;
 
diff --git a/bn_mp_sub.c b/bn_mp_sub.c
index 48cc06f..00df09e 100644
--- a/bn_mp_sub.c
+++ b/bn_mp_sub.c
@@ -4,12 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* high level subtraction (handles signs) */
-int mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     sa, sb, res;
-
-   sa = a->sign;
-   sb = b->sign;
+   mp_sign sa = a->sign, sb = b->sign;
+   mp_err res;
 
    if (sa != sb) {
       /* subtract a negative from a positive, OR */
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index fc45f97..c953b61 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* single digit subtraction */
-int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
+mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
 {
    mp_digit *tmpa, *tmpc, mu;
-   int       res, ix, oldused;
+   mp_err    res;
+   int       ix, oldused;
 
    /* grow c as required */
    if (c->alloc < (a->used + 1)) {
diff --git a/bn_mp_submod.c b/bn_mp_submod.c
index a2157af..f265eb1 100644
--- a/bn_mp_submod.c
+++ b/bn_mp_submod.c
@@ -4,11 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* d = a - b (mod c) */
-int mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
+mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
 {
-   int     res;
-   mp_int  t;
-
+   mp_err res;
+   mp_int t;
 
    if ((res = mp_init(&t)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c
index 9a6f528..6361f4c 100644
--- a/bn_mp_tc_and.c
+++ b/bn_mp_tc_and.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* two complement and */
-int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int res = MP_OKAY, bits, abits, bbits;
-   int sa = a->sign, sb = b->sign;
+   mp_err res = MP_OKAY;
+   int bits, abits, bbits;
+   mp_sign sa = a->sign, sb = b->sign;
    mp_int *mx = NULL, _mx, acpy, bcpy;
 
    if ((sa == MP_NEG) || (sb == MP_NEG)) {
diff --git a/bn_mp_tc_div_2d.c b/bn_mp_tc_div_2d.c
index 99945dd..fcb66b6 100644
--- a/bn_mp_tc_div_2d.c
+++ b/bn_mp_tc_div_2d.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* two complement right shift */
-int mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
+mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
 {
-   int res;
+   mp_err res;
    if (a->sign == MP_ZPOS) {
       return mp_div_2d(a, b, c, NULL);
    }
diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c
index 93cb520..e4cd756 100644
--- a/bn_mp_tc_or.c
+++ b/bn_mp_tc_or.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* two complement or */
-int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int res = MP_OKAY, bits, abits, bbits;
-   int sa = a->sign, sb = b->sign;
+   mp_err res = MP_OKAY;
+   int bits, abits, bbits;
+   mp_sign sa = a->sign, sb = b->sign;
    mp_int *mx = NULL, _mx, acpy, bcpy;
 
    if ((sa == MP_NEG) || (sb == MP_NEG)) {
diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c
index ff4189c..21d46df 100644
--- a/bn_mp_tc_xor.c
+++ b/bn_mp_tc_xor.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* two complement xor */
-int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int res = MP_OKAY, bits, abits, bbits;
-   int sa = a->sign, sb = b->sign;
+   mp_err res = MP_OKAY;
+   int bits, abits, bbits;
+   mp_sign sa = a->sign, sb = b->sign;
    mp_int *mx = NULL, _mx, acpy, bcpy;
 
    if ((sa == MP_NEG) || (sb == MP_NEG)) {
diff --git a/bn_mp_to_signed_bin.c b/bn_mp_to_signed_bin.c
index c281f3f..6561ea8 100644
--- a/bn_mp_to_signed_bin.c
+++ b/bn_mp_to_signed_bin.c
@@ -4,10 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* store in signed [big endian] format */
-int mp_to_signed_bin(const mp_int *a, unsigned char *b)
+mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b)
 {
-   int     res;
-
+   mp_err res;
    if ((res = mp_to_unsigned_bin(a, b + 1)) != MP_OKAY) {
       return res;
    }
diff --git a/bn_mp_to_signed_bin_n.c b/bn_mp_to_signed_bin_n.c
index 9ee6922..4c36cb5 100644
--- a/bn_mp_to_signed_bin_n.c
+++ b/bn_mp_to_signed_bin_n.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* store in signed [big endian] format */
-int mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
+mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
 {
    if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
       return MP_VAL;
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index 13e7d2e..6a03625 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* store in unsigned [big endian] format */
-int mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
+mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
 {
-   int     x, res;
+   int     x;
+   mp_err  res;
    mp_int  t;
 
    if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
diff --git a/bn_mp_to_unsigned_bin_n.c b/bn_mp_to_unsigned_bin_n.c
index bf47c74..3c08465 100644
--- a/bn_mp_to_unsigned_bin_n.c
+++ b/bn_mp_to_unsigned_bin_n.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* store in unsigned [big endian] format */
-int mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
+mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
 {
    if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
       return MP_VAL;
diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c
index 7a6d544..856ff25 100644
--- a/bn_mp_toradix.c
+++ b/bn_mp_toradix.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* stores a bignum as a ASCII string in a given radix (2..64) */
-int mp_toradix(const mp_int *a, char *str, int radix)
+mp_err mp_toradix(const mp_int *a, char *str, int radix)
 {
-   int     res, digs;
+   mp_err  res;
+   int digs;
    mp_int  t;
    mp_digit d;
    char   *_s = str;
diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c
index 2f0018a..755952e 100644
--- a/bn_mp_toradix_n.c
+++ b/bn_mp_toradix_n.c
@@ -7,9 +7,10 @@
  *
  * Stores upto maxlen-1 chars and always a NULL byte
  */
-int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
+mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
 {
-   int     res, digs;
+   int     digs;
+   mp_err  res;
    mp_int  t;
    mp_digit d;
    char   *_s = str;
diff --git a/bn_mp_xor.c b/bn_mp_xor.c
index a61c8fe..a8a7778 100644
--- a/bn_mp_xor.c
+++ b/bn_mp_xor.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* XOR two ints together */
-int mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     res, ix, px;
+   int     ix, px;
+   mp_err  res;
    mp_int  t;
    const mp_int *x;
 
diff --git a/bn_s_mp_add.c b/bn_s_mp_add.c
index e87dc9f..83df1d3 100644
--- a/bn_s_mp_add.c
+++ b/bn_s_mp_add.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
-int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
 {
    const mp_int *x;
-   int     olduse, res, min, max;
+   mp_err res;
+   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
diff --git a/bn_s_mp_balance_mul.c b/bn_s_mp_balance_mul.c
index ed17a3e..f8de2ec 100644
--- a/bn_s_mp_balance_mul.c
+++ b/bn_s_mp_balance_mul.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* single-digit multiplication with the smaller number as the single-digit */
-int s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int e, count, len_a, len_b, nblocks, i, j, bsize;
+   int count, len_a, len_b, nblocks, i, j, bsize;
    mp_int a0, tmp, A, B, r;
+   mp_err e;
 
    len_a = a->used;
    len_b = b->used;
diff --git a/bn_s_mp_exptmod.c b/bn_s_mp_exptmod.c
index 20572d4..b1cc0e9 100644
--- a/bn_s_mp_exptmod.c
+++ b/bn_s_mp_exptmod.c
@@ -9,12 +9,13 @@
 #   define TAB_SIZE 256
 #endif
 
-int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
+mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
 {
    mp_int  M[TAB_SIZE], res, mu;
    mp_digit buf;
-   int     err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
-   int (*redux)(mp_int *x, const mp_int *m, const mp_int *mu);
+   mp_err   err;
+   int      bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
+   mp_err(*redux)(mp_int *x, const mp_int *m, const mp_int *mu);
 
    /* find window size */
    x = mp_count_bits(X);
diff --git a/bn_s_mp_exptmod_fast.c b/bn_s_mp_exptmod_fast.c
index d6373ef..6b4483c 100644
--- a/bn_s_mp_exptmod_fast.c
+++ b/bn_s_mp_exptmod_fast.c
@@ -17,17 +17,18 @@
 #   define TAB_SIZE 256
 #endif
 
-int s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
+mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
 {
    mp_int  M[TAB_SIZE], res;
    mp_digit buf, mp;
-   int     err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
+   int     bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
+   mp_err   err;
 
    /* use a pointer to the reduction algorithm.  This allows us to use
     * one of many reduction algorithms without modding the guts of
     * the code with if statements everywhere.
     */
-   int (*redux)(mp_int *x, const mp_int *n, mp_digit rho);
+   mp_err(*redux)(mp_int *x, const mp_int *n, mp_digit rho);
 
    /* find window size */
    x = mp_count_bits(X);
diff --git a/bn_s_mp_invmod_fast.c b/bn_s_mp_invmod_fast.c
index 071b76d..111ecdd 100644
--- a/bn_s_mp_invmod_fast.c
+++ b/bn_s_mp_invmod_fast.c
@@ -9,10 +9,11 @@
  * Based on slow invmod except this is optimized for the case where b is
  * odd as per HAC Note 14.64 on pp. 610
  */
-int s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c)
+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;
-   int     res, neg;
+   mp_sign neg;
+   mp_err  res;
 
    /* 2. [modified] b must be odd   */
    if (MP_IS_EVEN(b)) {
diff --git a/bn_s_mp_invmod_slow.c b/bn_s_mp_invmod_slow.c
index dbafde1..b006aed 100644
--- a/bn_s_mp_invmod_slow.c
+++ b/bn_s_mp_invmod_slow.c
@@ -4,10 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* hac 14.61, pp608 */
-int s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
+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;
-   int     res;
+   mp_err  res;
 
    /* b cannot be negative */
    if ((b->sign == MP_NEG) || MP_IS_ZERO(b)) {
diff --git a/bn_s_mp_karatsuba_mul.c b/bn_s_mp_karatsuba_mul.c
index 15d3b11..6ef96c7 100644
--- a/bn_s_mp_karatsuba_mul.c
+++ b/bn_s_mp_karatsuba_mul.c
@@ -32,13 +32,11 @@
  * Generally though the overhead of this method doesn't pay off
  * until a certain size (N ~ 80) is reached.
  */
-int s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
+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, err;
-
-   /* default the return code to an error */
-   err = MP_MEM;
+   int     B;
+   mp_err  err = MP_MEM; /* default the return code to an error */
 
    /* min # of digits */
    B = MP_MIN(a->used, b->used);
diff --git a/bn_s_mp_karatsuba_sqr.c b/bn_s_mp_karatsuba_sqr.c
index 8a3069b..f132d07 100644
--- a/bn_s_mp_karatsuba_sqr.c
+++ b/bn_s_mp_karatsuba_sqr.c
@@ -10,12 +10,11 @@
  * is essentially the same algorithm but merely
  * tuned to perform recursive squarings.
  */
-int s_mp_karatsuba_sqr(const mp_int *a, mp_int *b)
+mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b)
 {
    mp_int  x0, x1, t1, t2, x0x0, x1x1;
-   int     B, err;
-
-   err = MP_MEM;
+   int     B;
+   mp_err  err = MP_MEM;
 
    /* min # of digits */
    B = a->used;
diff --git a/bn_s_mp_montgomery_reduce_fast.c b/bn_s_mp_montgomery_reduce_fast.c
index 688049c..4e69af6 100644
--- a/bn_s_mp_montgomery_reduce_fast.c
+++ b/bn_s_mp_montgomery_reduce_fast.c
@@ -11,9 +11,10 @@
  *
  * Based on Algorithm 14.32 on pp.601 of HAC.
 */
-int s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
+mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
 {
-   int     ix, res, olduse;
+   int     ix, olduse;
+   mp_err  res;
    mp_word W[MP_WARRAY];
 
    if (x->used > (int)MP_WARRAY) {
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index 87b785c..109505b 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -7,10 +7,11 @@
  * HAC pp. 595, Algorithm 14.12  Modified so you can control how
  * many digits of output are created.
  */
-int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 {
    mp_int  t;
-   int     res, pa, pb, ix, iy;
+   mp_err  res;
+   int     pa, pb, ix, iy;
    mp_digit u;
    mp_word r;
    mp_digit tmpx, *tmpt, *tmpy;
diff --git a/bn_s_mp_mul_digs_fast.c b/bn_s_mp_mul_digs_fast.c
index 2361433..9c3776c 100644
--- a/bn_s_mp_mul_digs_fast.c
+++ b/bn_s_mp_mul_digs_fast.c
@@ -19,9 +19,10 @@
  * Based on Algorithm 14.12 on pp.595 of HAC.
  *
  */
-int s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 {
-   int     olduse, res, pa, ix, iz;
+   int      olduse, pa, ix, iz;
+   mp_err   res;
    mp_digit W[MP_WARRAY];
    mp_word  _W;
 
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index 14b889e..7a13991 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -6,12 +6,13 @@
 /* multiplies |a| * |b| and does not compute the lower digs digits
  * [meant to get the higher part of the product]
  */
-int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 {
-   mp_int  t;
-   int     res, pa, pb, ix, iy;
+   mp_int   t;
+   int      pa, pb, ix, iy;
+   mp_err   res;
    mp_digit u;
-   mp_word r;
+   mp_word  r;
    mp_digit tmpx, *tmpt, *tmpy;
 
    /* can we use the fast multiplier? */
diff --git a/bn_s_mp_mul_high_digs_fast.c b/bn_s_mp_mul_high_digs_fast.c
index 27242a1..36cb2d1 100644
--- a/bn_s_mp_mul_high_digs_fast.c
+++ b/bn_s_mp_mul_high_digs_fast.c
@@ -12,9 +12,10 @@
  *
  * Based on Algorithm 14.12 on pp.595 of HAC.
  */
-int s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
+mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
 {
-   int     olduse, res, pa, ix, iz;
+   int     olduse, pa, ix, iz;
+   mp_err   res;
    mp_digit W[MP_WARRAY];
    mp_word  _W;
 
diff --git a/bn_s_mp_rand_jenkins.c b/bn_s_mp_rand_jenkins.c
index a310bea..a914458 100644
--- a/bn_s_mp_rand_jenkins.c
+++ b/bn_s_mp_rand_jenkins.c
@@ -35,7 +35,7 @@ void s_mp_rand_jenkins_init(uint64_t seed)
    }
 }
 
-int s_mp_rand_jenkins(void *p, size_t n)
+mp_err s_mp_rand_jenkins(void *p, size_t n)
 {
    char *q = (char *)p;
    while (n > 0u) {
diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c
index ec2f008..a4da912 100644
--- a/bn_s_mp_rand_platform.c
+++ b/bn_s_mp_rand_platform.c
@@ -26,7 +26,7 @@
 #include <windows.h>
 #include <wincrypt.h>
 
-static int s_read_win_csp(void *p, size_t n)
+static mp_err s_read_win_csp(void *p, size_t n)
 {
    static HCRYPTPROV hProv = 0;
    if (hProv == 0) {
@@ -49,7 +49,7 @@ static int s_read_win_csp(void *p, size_t n)
 #include <sys/random.h>
 #include <errno.h>
 
-static int s_read_getrandom(void *p, size_t n)
+static mp_err s_read_getrandom(void *p, size_t n)
 {
    char *q = (char *)p;
    while (n > 0u) {
@@ -79,7 +79,7 @@ static int s_read_getrandom(void *p, size_t n)
 #include <errno.h>
 #include <unistd.h>
 
-static int s_read_dev_urandom(void *p, size_t n)
+static mp_err s_read_dev_urandom(void *p, size_t n)
 {
    int fd;
    char *q = (char *)p;
@@ -111,7 +111,7 @@ static int s_read_dev_urandom(void *p, size_t n)
 unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
 void (*ltm_rng_callback)(void);
 
-static int s_read_ltm_rng(void *p, size_t n)
+static mp_err s_read_ltm_rng(void *p, size_t n)
 {
    unsigned long ret;
    if (ltm_rng == NULL) return MP_ERR;
@@ -121,14 +121,14 @@ static int s_read_ltm_rng(void *p, size_t n)
 }
 #endif
 
-int s_mp_rand_platform(void *p, size_t n)
+mp_err s_mp_rand_platform(void *p, size_t n)
 {
 #if defined(MP_ARC4RANDOM)
    arc4random_buf(p, n);
    return MP_OKAY;
 #else
 
-   int ret = MP_ERR;
+   mp_err ret = MP_ERR;
 
 #if defined(MP_WIN_CSP)
    ret = s_read_win_csp(p, n);
diff --git a/bn_s_mp_sqr.c b/bn_s_mp_sqr.c
index b6f0ea0..650f461 100644
--- a/bn_s_mp_sqr.c
+++ b/bn_s_mp_sqr.c
@@ -4,11 +4,12 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
-int s_mp_sqr(const mp_int *a, mp_int *b)
+mp_err s_mp_sqr(const mp_int *a, mp_int *b)
 {
-   mp_int  t;
-   int     res, ix, iy, pa;
-   mp_word r;
+   mp_int   t;
+   int      ix, iy, pa;
+   mp_err   res;
+   mp_word  r;
    mp_digit u, tmpx, *tmpt;
 
    pa = a->used;
diff --git a/bn_s_mp_sqr_fast.c b/bn_s_mp_sqr_fast.c
index 304500c..95acd7a 100644
--- a/bn_s_mp_sqr_fast.c
+++ b/bn_s_mp_sqr_fast.c
@@ -13,11 +13,12 @@
 After that loop you do the squares and add them in.
 */
 
-int s_mp_sqr_fast(const mp_int *a, mp_int *b)
+mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b)
 {
-   int       olduse, res, pa, ix, iz;
-   mp_digit   W[MP_WARRAY], *tmpx;
+   int       olduse, pa, ix, iz;
+   mp_digit  W[MP_WARRAY], *tmpx;
    mp_word   W1;
+   mp_err    res;
 
    /* grow the destination as required */
    pa = a->used + a->used;
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index ffd8272..d00e0e4 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -4,9 +4,10 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
-int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   int     olduse, res, min, max;
+   int    olduse, min, max;
+   mp_err res;
 
    /* find sizes */
    min = b->used;
diff --git a/bn_s_mp_toom_mul.c b/bn_s_mp_toom_mul.c
index 2156139..9f10761 100644
--- a/bn_s_mp_toom_mul.c
+++ b/bn_s_mp_toom_mul.c
@@ -10,10 +10,11 @@
  * only particularly useful on VERY large inputs
  * (we're talking 1000s of digits here...).
 */
-int s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
+mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
 {
    mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
-   int res, B;
+   int B;
+   mp_err res;
 
    /* init temps */
    if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
diff --git a/bn_s_mp_toom_sqr.c b/bn_s_mp_toom_sqr.c
index 72cbea8..24fb4d9 100644
--- a/bn_s_mp_toom_sqr.c
+++ b/bn_s_mp_toom_sqr.c
@@ -4,10 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* squaring using Toom-Cook 3-way algorithm */
-int s_mp_toom_sqr(const mp_int *a, mp_int *b)
+mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b)
 {
    mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
-   int res, B;
+   int B;
+   mp_err res;
 
    /* init temps */
    if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != MP_OKAY) {
diff --git a/demo/main.c b/demo/main.c
index 068e31a..4cebb12 100644
--- a/demo/main.c
+++ b/demo/main.c
@@ -9,7 +9,7 @@ void ndraw(mp_int *a, const char *name)
    int size;
 
    mp_radix_size(a, 10, &size);
-   buf = malloc((size_t) size);
+   buf = (char *)malloc((size_t) size);
    if (buf == NULL) {
       fprintf(stderr, "\nndraw: malloc(%d) failed\n", size);
       exit(EXIT_FAILURE);
diff --git a/demo/test.c b/demo/test.c
index c4314d0..4e5339e 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -94,7 +94,7 @@ LBL_ERR:
    return EXIT_FAILURE;
 }
 
-static int very_random_source(void *out, size_t size)
+static mp_err very_random_source(void *out, size_t size)
 {
    memset(out, 0xff, size);
    return MP_OKAY;
@@ -103,7 +103,8 @@ static int very_random_source(void *out, size_t size)
 static int test_mp_rand(void)
 {
    mp_int a, b;
-   int err, n;
+   int n;
+   mp_err err;
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
    }
@@ -147,8 +148,8 @@ static int test_mp_jacobi(void)
       { 9, { -1,  1,  0,  1,  1,  0,  1,  1,  0,  1,  1,  0,  1,  1,  0,  1 } },
    };
 
-   int i, n, err, should, cnt;
-
+   int i, n, should, cnt;
+   mp_err err;
    mp_int a, b;
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
@@ -225,8 +226,8 @@ static int test_mp_kronecker(void)
    };
 
    long k, m;
-   int i, err, cnt;
-
+   int i, cnt;
+   mp_err err;
    mp_int a, b;
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
@@ -718,6 +719,8 @@ static int test_mp_is_square(void)
    int i, n;
 
    mp_int a, b;
+   mp_bool res;
+
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
    }
@@ -730,22 +733,22 @@ static int test_mp_is_square(void)
       n = (rand_int() & 7) + 1;
       mp_rand(&a, n);
       mp_sqr(&a, &a);
-      if (mp_is_square(&a, &n) != MP_OKAY) {
+      if (mp_is_square(&a, &res) != MP_OKAY) {
          printf("\nfn:mp_is_square() error!");
          goto LBL_ERR;
       }
-      if (n == 0) {
+      if (res == MP_NO) {
          printf("\nfn:mp_is_square() bad result!");
          goto LBL_ERR;
       }
 
       /* test for false positives */
       mp_add_d(&a, 1uL, &a);
-      if (mp_is_square(&a, &n) != MP_OKAY) {
+      if (mp_is_square(&a, &res) != MP_OKAY) {
          printf("\nfp:mp_is_square() error!");
          goto LBL_ERR;
       }
-      if (n == 1) {
+      if (res == MP_YES) {
          printf("\nfp:mp_is_square() bad result!");
          goto LBL_ERR;
       }
@@ -804,8 +807,8 @@ LBL_ERR:
 
 static int test_mp_prime_rand(void)
 {
-   int ix, err;
-
+   int ix;
+   mp_err err;
    mp_int a, b;
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
@@ -836,7 +839,9 @@ LBL_ERR:
 
 static int test_mp_prime_is_prime(void)
 {
-   int ix, err, cnt;
+   int ix;
+   mp_err err;
+   mp_bool cnt;
 
    mp_int a, b;
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
diff --git a/dep.pl b/dep.pl
index fb610c1..6625cb2 100755
--- a/dep.pl
+++ b/dep.pl
@@ -98,6 +98,7 @@ EOS
    foreach my $line (split /\n/, $content) {
       while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()/g) {
           my $a = $&;
+          next if $a eq "mp_err";
           $a =~ tr/[a-z]/[A-Z]/;
           $a = 'BN_' . $a . '_C';
           if (!($list =~ /$a/)) {
diff --git a/makefile_include.mk b/makefile_include.mk
index d7d4cba..aad3896 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -61,6 +61,9 @@ endif
 
 ifdef CONV_WARNINGS
 CFLAGS += -std=c89 -Wconversion -Wsign-conversion
+ifeq ($(CONV_WARNINGS), strict)
+CFLAGS += -DMP_USE_ENUMS -Wc++-compat
+endif
 else
 CFLAGS += -Wsystem-headers
 endif
diff --git a/tommath.h b/tommath.h
index 9603167..821a13d 100644
--- a/tommath.h
+++ b/tommath.h
@@ -97,24 +97,6 @@ typedef uint64_t             mp_word;
 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)MP_DIGIT_BIT))-((mp_digit)1))
 #define MP_DIGIT_MAX     MP_MASK
 
-/* equalities */
-#define MP_LT        -1   /* less than */
-#define MP_EQ         0   /* equal to */
-#define MP_GT         1   /* greater than */
-
-#define MP_ZPOS       0   /* positive integer */
-#define MP_NEG        1   /* negative */
-
-#define MP_OKAY       0   /* ok result */
-#define MP_ERR        -1  /* unknown error */
-#define MP_MEM        -2  /* out of mem */
-#define MP_VAL        -3  /* invalid input */
-#define MP_RANGE      MP_VAL
-#define MP_ITER       -4  /* Max. iterations reached */
-
-#define MP_YES        1   /* yes response */
-#define MP_NO         0   /* no response */
-
 /* Primality generation flags */
 #define MP_PRIME_BBS      0x0001 /* BBS style prime */
 #define MP_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
@@ -124,7 +106,47 @@ typedef uint64_t             mp_word;
 #define LTM_PRIME_SAFE     (MP_DEPRECATED_PRAGMA("LTM_PRIME_SAFE has been deprecated, use MP_PRIME_SAFE") MP_PRIME_SAFE)
 #define LTM_PRIME_2MSB_ON  (MP_DEPRECATED_PRAGMA("LTM_PRIME_2MSB_ON has been deprecated, use MP_PRIME_2MSB_ON") MP_PRIME_2MSB_ON)
 
-typedef int           mp_err;
+#ifdef MP_USE_ENUMS
+typedef enum {
+   MP_ZPOS = 0,
+   MP_NEG = 1
+} mp_sign;
+typedef enum {
+   MP_LT = -1,
+   MP_EQ = 0,
+   MP_GT = 1
+} mp_ord;
+typedef enum {
+   MP_NO = 0,
+   MP_YES = 1
+} mp_bool;
+typedef enum {
+   MP_OKAY  = 0,
+   MP_ERR   = -1,
+   MP_MEM   = -2,
+   MP_VAL   = -3,
+   MP_RANGE = MP_VAL,
+   MP_ITER  = -4
+} mp_err;
+#else
+typedef int mp_sign;
+#define MP_ZPOS       0   /* positive integer */
+#define MP_NEG        1   /* negative */
+typedef int mp_ord;
+#define MP_LT        -1   /* less than */
+#define MP_EQ         0   /* equal to */
+#define MP_GT         1   /* greater than */
+typedef int mp_bool;
+#define MP_YES        1   /* yes response */
+#define MP_NO         0   /* no response */
+typedef int mp_err;
+#define MP_OKAY       0   /* ok result */
+#define MP_ERR        -1  /* unknown error */
+#define MP_MEM        -2  /* out of mem */
+#define MP_VAL        -3  /* invalid input */
+#define MP_RANGE      MP_VAL
+#define MP_ITER       -4  /* Max. iterations reached */
+#endif
 
 /* tunable cutoffs */
 
@@ -202,7 +224,8 @@ TOOM_SQR_CUTOFF;
 
 /* the infamous mp_int structure */
 typedef struct  {
-   int used, alloc, sign;
+   int used, alloc;
+   mp_sign sign;
    mp_digit *dp;
 } mp_int;
 
@@ -211,17 +234,17 @@ typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat);
 typedef private_mp_prime_callback ltm_prime_callback MP_DEPRECATED(mp_rand_source);
 
 /* error code to char* string */
-const char *mp_error_to_string(int code);
+const char *mp_error_to_string(mp_err code) MP_WUR;
 
 /* ---> init and deinit bignum functions <--- */
 /* init a bignum */
-MP_WUR int mp_init(mp_int *a);
+mp_err mp_init(mp_int *a) MP_WUR;
 
 /* free a bignum */
 void mp_clear(mp_int *a);
 
 /* init a null terminated series of arguments */
-MP_WUR int mp_init_multi(mp_int *mp, ...) MP_NULL_TERMINATED;
+mp_err mp_init_multi(mp_int *mp, ...) MP_NULL_TERMINATED MP_WUR;
 
 /* clear a null terminated series of arguments */
 void mp_clear_multi(mp_int *mp, ...) MP_NULL_TERMINATED;
@@ -230,18 +253,18 @@ void mp_clear_multi(mp_int *mp, ...) MP_NULL_TERMINATED;
 void mp_exch(mp_int *a, mp_int *b);
 
 /* shrink ram required for a bignum */
-MP_WUR int mp_shrink(mp_int *a);
+mp_err mp_shrink(mp_int *a) MP_WUR;
 
 /* grow an int to a given size */
-MP_WUR int mp_grow(mp_int *a, int size);
+mp_err mp_grow(mp_int *a, int size) MP_WUR;
 
 /* init to a given number of digits */
-MP_WUR int mp_init_size(mp_int *a, int size);
+mp_err mp_init_size(mp_int *a, int size) MP_WUR;
 
 /* ---> Basic Manipulations <--- */
 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
-MP_WUR int mp_iseven(const mp_int *a);
-MP_WUR int mp_isodd(const mp_int *a);
+mp_bool mp_iseven(const mp_int *a) MP_WUR;
+mp_bool mp_isodd(const mp_int *a) MP_WUR;
 #define mp_isneg(a)  (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
 
 /* set to zero */
@@ -251,49 +274,49 @@ void mp_zero(mp_int *a);
 void mp_set(mp_int *a, mp_digit b);
 
 /* set a double */
-MP_WUR int mp_set_double(mp_int *a, double b);
+mp_err mp_set_double(mp_int *a, double b) MP_WUR;
 
 /* set a 32-bit const */
-/* TODO void - never fails */ int mp_set_int(mp_int *a, unsigned long b);
+/* TODO void - never fails */ mp_err mp_set_int(mp_int *a, unsigned long b);
 
 /* set a platform dependent unsigned long value */
-/* TODO void - never fails */ int mp_set_long(mp_int *a, unsigned long b);
+/* TODO void - never fails */ mp_err mp_set_long(mp_int *a, unsigned long b);
 
 /* set a platform dependent unsigned long long value */
-/* TODO void - never fails */ int mp_set_long_long(mp_int *a, unsigned long long b);
+/* TODO void - never fails */ mp_err mp_set_long_long(mp_int *a, unsigned long long b);
 
 /* get a double */
-MP_WUR double mp_get_double(const mp_int *a);
+double mp_get_double(const mp_int *a) MP_WUR;
 
 /* get a 32-bit value */
-MP_WUR unsigned long mp_get_int(const mp_int *a);
+unsigned long mp_get_int(const mp_int *a) MP_WUR;
 
 /* get a platform dependent unsigned long value */
-MP_WUR unsigned long mp_get_long(const mp_int *a);
+unsigned long mp_get_long(const mp_int *a) MP_WUR;
 
 /* get a platform dependent unsigned long long value */
-MP_WUR unsigned long long mp_get_long_long(const mp_int *a);
+unsigned long long mp_get_long_long(const mp_int *a) MP_WUR;
 
 /* initialize and set a digit */
-MP_WUR int mp_init_set(mp_int *a, mp_digit b);
+mp_err mp_init_set(mp_int *a, mp_digit b) MP_WUR;
 
 /* initialize and set 32-bit value */
-MP_WUR int mp_init_set_int(mp_int *a, unsigned long b);
+mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;
 
 /* copy, b = a */
-MP_WUR int mp_copy(const mp_int *a, mp_int *b);
+mp_err mp_copy(const mp_int *a, mp_int *b) MP_WUR;
 
 /* inits and copies, a = b */
-MP_WUR int mp_init_copy(mp_int *a, const mp_int *b);
+mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR;
 
 /* trim unused digits */
 void mp_clamp(mp_int *a);
 
 /* import binary data */
-MP_WUR int mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails, const void *op);
+mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails, const void *op) MP_WUR;
 
 /* export binary data */
-MP_WUR int mp_export(void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, const mp_int *op);
+mp_err mp_export(void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, const mp_int *op) MP_WUR;
 
 /* ---> digit manipulation <--- */
 
@@ -301,37 +324,37 @@ MP_WUR int mp_export(void *rop, size_t *countp, int order, size_t size, int endi
 void mp_rshd(mp_int *a, int b);
 
 /* left shift by "b" digits */
-MP_WUR int mp_lshd(mp_int *a, int b);
+mp_err mp_lshd(mp_int *a, int b) MP_WUR;
 
 /* c = a / 2**b, implemented as c = a >> b */
-MP_WUR int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d);
+mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) MP_WUR;
 
 /* b = a/2 */
-MP_WUR int mp_div_2(const mp_int *a, mp_int *b);
+mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR;
 
 /* c = a * 2**b, implemented as c = a << b */
-MP_WUR int mp_mul_2d(const mp_int *a, int b, mp_int *c);
+mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
 
 /* b = a*2 */
-MP_WUR int mp_mul_2(const mp_int *a, mp_int *b);
+mp_err mp_mul_2(const mp_int *a, mp_int *b) MP_WUR;
 
 /* c = a mod 2**b */
-MP_WUR int mp_mod_2d(const mp_int *a, int b, mp_int *c);
+mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
 
 /* computes a = 2**b */
-MP_WUR int mp_2expt(mp_int *a, int b);
+mp_err mp_2expt(mp_int *a, int b) MP_WUR;
 
 /* Counts the number of lsbs which are zero before the first zero bit */
-MP_WUR int mp_cnt_lsb(const mp_int *a);
+int mp_cnt_lsb(const mp_int *a) MP_WUR;
 
 /* I Love Earth! */
 
 /* makes a pseudo-random mp_int of a given size */
-MP_WUR int mp_rand(mp_int *a, int digits);
+mp_err mp_rand(mp_int *a, int digits) MP_WUR;
 /* makes a pseudo-random small int of a given size */
-MP_WUR MP_DEPRECATED(mp_rand) int mp_rand_digit(mp_digit *r);
+MP_DEPRECATED(mp_rand) mp_err mp_rand_digit(mp_digit *r) MP_WUR;
 /* use custom random data source instead of source provided the platform */
-void mp_rand_source(int source(void *out, size_t size));
+void mp_rand_source(mp_err source(void *out, size_t size));
 
 #ifdef MP_PRNG_ENABLE_LTM_RNG
 #  warning MP_PRNG_ENABLE_LTM_RNG has been deprecated, use mp_rand_source instead.
@@ -345,197 +368,197 @@ extern void (*ltm_rng_callback)(void);
 
 /* ---> binary operations <--- */
 /* c = a XOR b  */
-MP_WUR int mp_xor(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a OR b */
-MP_WUR int mp_or(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a AND b */
-MP_WUR int mp_and(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* Checks the bit at position b and returns MP_YES
    if the bit is 1, MP_NO if it is 0 and MP_VAL
    in case of error */
-MP_WUR int mp_get_bit(const mp_int *a, int b);
+/* TODO better return type, mixes mp_bool and mp_err */ int mp_get_bit(const mp_int *a, int b) MP_WUR;
 
 /* c = a XOR b (two complement) */
-MP_WUR int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a OR b (two complement) */
-MP_WUR int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a AND b (two complement) */
-MP_WUR int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* right shift (two complement) */
-MP_WUR int mp_tc_div_2d(const mp_int *a, int b, mp_int *c);
+mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
 
 /* ---> Basic arithmetic <--- */
 
 /* b = ~a */
-MP_WUR int mp_complement(const mp_int *a, mp_int *b);
+mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR;
 
 /* b = -a */
-MP_WUR int mp_neg(const mp_int *a, mp_int *b);
+mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR;
 
 /* b = |a| */
-MP_WUR int mp_abs(const mp_int *a, mp_int *b);
+mp_err mp_abs(const mp_int *a, mp_int *b) MP_WUR;
 
 /* compare a to b */
-MP_WUR int mp_cmp(const mp_int *a, const mp_int *b);
+mp_ord mp_cmp(const mp_int *a, const mp_int *b) MP_WUR;
 
 /* compare |a| to |b| */
-MP_WUR int mp_cmp_mag(const mp_int *a, const mp_int *b);
+mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) MP_WUR;
 
 /* c = a + b */
-MP_WUR int mp_add(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a - b */
-MP_WUR int mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = a * b */
-MP_WUR int mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* b = a*a  */
-MP_WUR int mp_sqr(const mp_int *a, mp_int *b);
+mp_err mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
 
 /* a/b => cb + d == a */
-MP_WUR int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
+mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
 
 /* c = a mod b, 0 <= c < b  */
-MP_WUR int mp_mod(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* ---> single digit functions <--- */
 
 /* compare against a single digit */
-MP_WUR int mp_cmp_d(const mp_int *a, mp_digit b);
+mp_ord mp_cmp_d(const mp_int *a, mp_digit b) MP_WUR;
 
 /* c = a + b */
-MP_WUR int mp_add_d(const mp_int *a, mp_digit b, mp_int *c);
+mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
 
 /* Increment "a" by one like "a++". Changes input! */
-MP_WUR int mp_incr(mp_int *a);
+mp_err mp_incr(mp_int *a) MP_WUR;
 
 /* c = a - b */
-MP_WUR int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c);
+mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
 
 /* Decrement "a" by one like "a--". Changes input! */
-MP_WUR int mp_decr(mp_int *a);
+mp_err mp_decr(mp_int *a) MP_WUR;
 
 /* c = a * b */
-MP_WUR int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c);
+mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
 
 /* a/b => cb + d == a */
-MP_WUR int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
+mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR;
 
 /* a/3 => 3c + d == a */
-MP_WUR int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d);
+mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
 
 /* c = a**b */
-MP_WUR int mp_expt_d(const mp_int *a, mp_digit b, mp_int *c);
-MP_WUR int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
+mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
+mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
 
 /* c = a mod b, 0 <= c < b  */
-MP_WUR int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c);
+mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
 
 /* ---> number theory <--- */
 
 /* d = a + b (mod c) */
-MP_WUR int mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
+mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
 
 /* d = a - b (mod c) */
-MP_WUR int mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
+mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
 
 /* d = a * b (mod c) */
-MP_WUR int mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
+mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
 
 /* c = a * a (mod b) */
-MP_WUR int mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = 1/a (mod b) */
-MP_WUR int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* c = (a, b) */
-MP_WUR int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* produces value such that U1*a + U2*b = U3 */
-MP_WUR int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
+mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) MP_WUR;
 
 /* c = [a, b] or (a*b)/(a, b) */
-MP_WUR int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c);
+mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* finds one of the b'th root of a, such that |c|**b <= |a|
  *
  * returns error if a < 0 and b is even
  */
-MP_WUR int mp_n_root(const mp_int *a, mp_digit b, mp_int *c);
-MP_WUR int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
+mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
+mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
 
 /* special sqrt algo */
-MP_WUR int mp_sqrt(const mp_int *arg, mp_int *ret);
+mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
 
 /* special sqrt (mod prime) */
-MP_WUR int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret);
+mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR;
 
 /* is number a square? */
-MP_WUR int mp_is_square(const mp_int *arg, int *ret);
+mp_err mp_is_square(const mp_int *arg, mp_bool *ret) MP_WUR;
 
 /* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
-MP_WUR int mp_jacobi(const mp_int *a, const mp_int *n, int *c);
+mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c) MP_WUR;
 
 /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
-MP_WUR int mp_kronecker(const mp_int *a, const mp_int *p, int *c);
+mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) MP_WUR;
 
 /* used to setup the Barrett reduction for a given modulus b */
-MP_WUR int mp_reduce_setup(mp_int *a, const mp_int *b);
+mp_err mp_reduce_setup(mp_int *a, const mp_int *b) MP_WUR;
 
 /* Barrett Reduction, computes a (mod b) with a precomputed value c
  *
  * Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
  * compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
  */
-MP_WUR int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);
+mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) MP_WUR;
 
 /* setups the montgomery reduction */
-MP_WUR int mp_montgomery_setup(const mp_int *n, mp_digit *rho);
+mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) MP_WUR;
 
 /* computes a = B**n mod b without division or multiplication useful for
  * normalizing numbers in a Montgomery system.
  */
-MP_WUR int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b);
+mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) MP_WUR;
 
 /* computes x/R == x (mod N) via Montgomery Reduction */
-MP_WUR int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
+mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
 
 /* returns 1 if a is a valid DR modulus */
-MP_WUR int mp_dr_is_modulus(const mp_int *a);
+mp_bool mp_dr_is_modulus(const mp_int *a) MP_WUR;
 
 /* sets the value of "d" required for mp_dr_reduce */
 void mp_dr_setup(const mp_int *a, mp_digit *d);
 
 /* reduces a modulo n using the Diminished Radix method */
-MP_WUR int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k);
+mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) MP_WUR;
 
 /* returns true if a can be reduced with mp_reduce_2k */
-MP_WUR int mp_reduce_is_2k(const mp_int *a);
+mp_bool mp_reduce_is_2k(const mp_int *a) MP_WUR;
 
 /* determines k value for 2k reduction */
-MP_WUR int mp_reduce_2k_setup(const mp_int *a, mp_digit *d);
+mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR;
 
 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
-MP_WUR int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d);
+mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) MP_WUR;
 
 /* returns true if a can be reduced with mp_reduce_2k_l */
-MP_WUR int mp_reduce_is_2k_l(const mp_int *a);
+mp_bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR;
 
 /* determines k value for 2k reduction */
-MP_WUR int mp_reduce_2k_setup_l(const mp_int *a, mp_int *d);
+mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR;
 
 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
-MP_WUR int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d);
+mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) MP_WUR;
 
 /* Y = G**X (mod P) */
-MP_WUR int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y);
+mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) MP_WUR;
 
 /* ---> Primes <--- */
 
@@ -551,32 +574,32 @@ MP_WUR int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int 
 extern const mp_digit ltm_prime_tab[MP_PRIME_SIZE];
 
 /* result=1 if a is divisible by one of the first MP_PRIME_SIZE primes */
-MP_WUR int mp_prime_is_divisible(const mp_int *a, int *result);
+mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result) MP_WUR;
 
 /* performs one Fermat test of "a" using base "b".
  * Sets result to 0 if composite or 1 if probable prime
  */
-MP_WUR int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result);
+mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
 
 /* performs one Miller-Rabin test of "a" using base "b".
  * Sets result to 0 if composite or 1 if probable prime
  */
-MP_WUR int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result);
+mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
 
 /* This gives [for a given bit size] the number of trials required
  * such that Miller-Rabin gives a prob of failure lower than 2^-96
  */
-MP_WUR int mp_prime_rabin_miller_trials(int size);
+int mp_prime_rabin_miller_trials(int size) MP_WUR;
 
 /* performs one strong Lucas-Selfridge test of "a".
  * Sets result to 0 if composite or 1 if probable prime
  */
-MP_WUR int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result);
+mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR;
 
 /* performs one Frobenius test of "a" as described by Paul Underwood.
  * Sets result to 0 if composite or 1 if probable prime
  */
-MP_WUR int mp_prime_frobenius_underwood(const mp_int *N, int *result);
+mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR;
 
 /* performs t random rounds of Miller-Rabin on "a" additional to
  * bases 2 and 3.  Also performs an initial sieve of trial
@@ -592,14 +615,14 @@ MP_WUR int mp_prime_frobenius_underwood(const mp_int *N, int *result);
  *
  * Sets result to 1 if probably prime, 0 otherwise
  */
-MP_WUR int mp_prime_is_prime(const mp_int *a, int t, int *result);
+mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR;
 
 /* finds the next prime after the number "a" using "t" trials
  * of Miller-Rabin.
  *
  * bbs_style = 1 means the prime must be congruent to 3 mod 4
  */
-MP_WUR int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
+mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR;
 
 /* makes a truly random prime of a given size (bytes),
  * call with bbs = 1 if you want it to be congruent to 3 mod 4
@@ -625,35 +648,34 @@ MP_WUR int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
  * so it can be NULL
  *
  */
-MP_WUR MP_DEPRECATED(mp_prime_rand) int mp_prime_random_ex(mp_int *a, int t, int size, int flags,
-      private_mp_prime_callback cb, void *dat);
-MP_WUR int mp_prime_rand(mp_int *a, int t, int size, int flags);
+MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags,
+      private_mp_prime_callback cb, void *dat) MP_WUR;
+mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR;
 
 /* Integer logarithm to integer base */
-MP_WUR int mp_ilogb(const mp_int *a, mp_digit base, mp_int *c);
-
+mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c) MP_WUR;
 
 /* ---> radix conversion <--- */
-MP_WUR int mp_count_bits(const mp_int *a);
+int mp_count_bits(const mp_int *a) MP_WUR;
 
-MP_WUR int mp_unsigned_bin_size(const mp_int *a);
-MP_WUR int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
-MP_WUR int mp_to_unsigned_bin(const mp_int *a, unsigned char *b);
-MP_WUR int mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen);
+int mp_unsigned_bin_size(const mp_int *a) MP_WUR;
+mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
+mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR;
+mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
 
-MP_WUR int mp_signed_bin_size(const mp_int *a);
-MP_WUR int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
-MP_WUR int mp_to_signed_bin(const mp_int *a,  unsigned char *b);
-MP_WUR int mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen);
+int mp_signed_bin_size(const mp_int *a) MP_WUR;
+mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
+mp_err mp_to_signed_bin(const mp_int *a,  unsigned char *b) MP_WUR;
+mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
 
-MP_WUR int mp_read_radix(mp_int *a, const char *str, int radix);
-MP_WUR int mp_toradix(const mp_int *a, char *str, int radix);
-MP_WUR int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen);
-MP_WUR int mp_radix_size(const mp_int *a, int radix, int *size);
+mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
+mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
+mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
+mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
 
 #ifndef MP_NO_FILE
-MP_WUR int mp_fread(mp_int *a, int radix, FILE *stream);
-MP_WUR int mp_fwrite(const mp_int *a, int radix, FILE *stream);
+mp_err mp_fread(mp_int *a, int radix, FILE *stream) MP_WUR;
+mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR;
 #endif
 
 #define mp_read_raw(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_signed_bin") mp_read_signed_bin((mp), (str), (len)))
diff --git a/tommath_private.h b/tommath_private.h
index 7716dce..9e052ce 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -142,31 +142,31 @@ extern void MP_FREE(void *mem, size_t size);
 #define MP_MAXFAST              (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
 
 /* random number source */
-extern int (*s_mp_rand_source)(void *out, size_t size);
+extern mp_err(*s_mp_rand_source)(void *out, size_t size);
 
 /* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
 #define MP_MIN_PREC ((((CHAR_BIT * (int)sizeof(long long)) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
 
 /* lowlevel functions, do not call! */
-MP_WUR int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs);
-MP_WUR int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
-MP_WUR int s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs);
-MP_WUR int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
-MP_WUR int s_mp_sqr_fast(const mp_int *a, mp_int *b);
-MP_WUR int s_mp_sqr(const mp_int *a, mp_int *b);
-MP_WUR int s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_karatsuba_sqr(const mp_int *a, mp_int *b);
-MP_WUR int s_mp_toom_sqr(const mp_int *a, mp_int *b);
-MP_WUR int s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
-MP_WUR int s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho);
-MP_WUR int s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
-MP_WUR int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
-MP_WUR int s_mp_rand_platform(void *p, size_t n);
+mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
+mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
+mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
+mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
+mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
+mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
+mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
+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_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
+mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
 void s_mp_reverse(unsigned char *s, int len);
 
 /* TODO: jenkins prng is not thread safe as of now */
@@ -198,20 +198,20 @@ int func_name (mp_int * a, type b)                       \
 }
 
 /* deprecated functions */
-MP_DEPRECATED(s_mp_invmod_fast) int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
-MP_DEPRECATED(s_mp_montgomery_reduce_fast) int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
-MP_DEPRECATED(s_mp_mul_digs_fast) int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
-MP_DEPRECATED(s_mp_mul_high_digs_fast) int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c,
+MP_DEPRECATED(s_mp_invmod_fast) mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
+MP_DEPRECATED(s_mp_montgomery_reduce_fast) mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
+MP_DEPRECATED(s_mp_mul_digs_fast) mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs);
+MP_DEPRECATED(s_mp_mul_high_digs_fast) mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c,
       int digs);
-MP_DEPRECATED(s_mp_sqr_fast) int fast_s_mp_sqr(const mp_int *a, mp_int *b);
-MP_DEPRECATED(s_mp_balance_mul) int mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
-MP_DEPRECATED(s_mp_exptmod_fast) int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y,
+MP_DEPRECATED(s_mp_sqr_fast) mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b);
+MP_DEPRECATED(s_mp_balance_mul) mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
+MP_DEPRECATED(s_mp_exptmod_fast) mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y,
       int redmode);
-MP_DEPRECATED(s_mp_invmod_slow) int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
-MP_DEPRECATED(s_mp_karatsuba_mul) int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
-MP_DEPRECATED(s_mp_karatsuba_sqr) int mp_karatsuba_sqr(const mp_int *a, mp_int *b);
-MP_DEPRECATED(s_mp_toom_mul) int mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
-MP_DEPRECATED(s_mp_toom_sqr) int mp_toom_sqr(const mp_int *a, mp_int *b);
+MP_DEPRECATED(s_mp_invmod_slow) mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
+MP_DEPRECATED(s_mp_karatsuba_mul) mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
+MP_DEPRECATED(s_mp_karatsuba_sqr) mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b);
+MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
+MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
 MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
 
 #ifdef __cplusplus