Commit 3289c9590bce8f42ffba458262ba1e6d774d99d4

Daniel Mendler 2019-05-20T10:41:06

introduce various mp_set_sint/mp_set_uint functions with precise types

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
diff --git a/bn_conversion.c b/bn_conversion.c
new file mode 100644
index 0000000..388b421
--- /dev/null
+++ b/bn_conversion.c
@@ -0,0 +1,95 @@
+#include "tommath_private.h"
+
+#ifdef BN_CONVERSION_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+#define MP_SET_UNSIGNED(name, w)                                        \
+    void name(mp_int * a, uint##w##_t b)                                \
+    {                                                                   \
+        int i = 0;                                                      \
+        while (b != 0u) {                                               \
+            a->dp[i++] = ((mp_digit)b & MP_MASK);                       \
+            if (w <= MP_DIGIT_BIT) { break; }                           \
+            b >>= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);             \
+        }                                                               \
+        a->used = i;                                                    \
+        a->sign = MP_ZPOS;                                              \
+        MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);            \
+    }
+#define MP_SET_SIGNED(name, uname, w)                                   \
+    void name(mp_int * a, int##w##_t b)                                 \
+    {                                                                   \
+        uname(a, b < 0 ? -(uint##w##_t)b : (uint##w##_t)b);             \
+        if (b < 0) { a->sign = MP_NEG; }                                \
+    }
+#define MP_INIT_INT(name , set, type)                     \
+    mp_err name(mp_int * a, type b)                      \
+    {                                                    \
+        mp_err err;                                      \
+        if ((err = mp_init(a)) != MP_OKAY) {             \
+            return err;                                  \
+        }                                                \
+        set(a, b);                                       \
+        return MP_OKAY;                                  \
+    }
+#define MP_GET_MAG(name, w)                                             \
+    uint##w##_t name(const mp_int* a)                                   \
+    {                                                                   \
+        unsigned i = MP_MIN((unsigned)a->used, (unsigned)((w + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
+        uint##w##_t res = 0;                                            \
+        while (i --> 0) {                                               \
+            res <<= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);           \
+            res |= (uint##w##_t)a->dp[i];                               \
+            if (w <= MP_DIGIT_BIT) { break; }                           \
+        }                                                               \
+        return res;                                                     \
+    }
+#define MP_GET_SIGNED(name, mag, w)                                     \
+    int##w##_t name(const mp_int* a)                                    \
+    {                                                                   \
+        uint64_t res = mag(a);                                          \
+        return a->sign == MP_NEG ? (int##w##_t)-res : (int##w##_t)res;  \
+    }
+
+#ifdef BN_MP_SET_UINT_C
+MP_SET_UNSIGNED(mp_set_uint, 32)
+#endif
+
+#ifdef BN_MP_SET_UINT64_C
+MP_SET_UNSIGNED(mp_set_uint64, 64)
+#endif
+
+#ifdef BN_MP_SET_SINT_C
+MP_SET_SIGNED(mp_set_sint, mp_set_uint, 32)
+#endif
+
+#ifdef BN_MP_SET_SINT64_C
+MP_SET_SIGNED(mp_set_sint64, mp_set_uint64, 64)
+#endif
+
+#if defined(BN_MP_GET_SINT_C) || defined(BN_MP_GET_UINT_C)
+MP_GET_SIGNED(mp_get_sint, mp_get_mag, 32)
+#endif
+
+#if defined(BN_MP_GET_SINT64_C) || defined(BN_MP_GET_UINT64_C)
+MP_GET_SIGNED(mp_get_sint64, mp_get_mag64, 64)
+#endif
+
+#ifdef BN_MP_GET_MAG_C
+MP_GET_MAG(mp_get_mag, 32)
+#endif
+
+#ifdef BN_MP_GET_MAG64_C
+MP_GET_MAG(mp_get_mag64, 64)
+#endif
+
+#ifdef BN_MP_INIT_UINT_C
+MP_INIT_INT(mp_init_uint, mp_set_uint, uint32_t)
+#endif
+
+#ifdef BN_MP_INIT_SINT_C
+MP_INIT_INT(mp_init_sint, mp_set_sint, int32_t)
+#endif
+
+#endif
diff --git a/bn_deprecated.c b/bn_deprecated.c
index ca930d6..4be7cc8 100644
--- a/bn_deprecated.c
+++ b/bn_deprecated.c
@@ -146,4 +146,49 @@ mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
    return mp_signed_rsh(a, b, c);
 }
 #endif
+#ifdef BN_MP_INIT_SET_INT_C
+mp_err mp_init_set_int(mp_int *a, unsigned long b)
+{
+   return mp_init_uint(a, (uint32_t)b);
+}
+#endif
+#ifdef BN_MP_SET_INT_C
+mp_err mp_set_int(mp_int *a, unsigned long b)
+{
+   mp_set_uint(a, (uint32_t)b);
+   return MP_OKAY;
+}
+#endif
+#ifdef BN_MP_SET_LONG_C
+mp_err mp_set_long(mp_int *a, unsigned long b)
+{
+   mp_set_uint64(a, b);
+   return MP_OKAY;
+}
+#endif
+#ifdef BN_MP_SET_LONG_LONG_C
+mp_err mp_set_long_long(mp_int *a, unsigned long long b)
+{
+   mp_set_uint64(a, b);
+   return MP_OKAY;
+}
+#endif
+#ifdef BN_MP_GET_INT_C
+unsigned long mp_get_int(const mp_int *a)
+{
+   return mp_get_mag(a);
+}
+#endif
+#ifdef BN_MP_GET_LONG_C
+unsigned long mp_get_long(const mp_int *a)
+{
+   return sizeof(long) > sizeof(int32_t) ? (unsigned long)mp_get_mag64(a) : (unsigned long)mp_get_mag(a);
+}
+#endif
+#ifdef BN_MP_GET_LONG_LONG_C
+unsigned long long mp_get_long_long(const mp_int *a)
+{
+   return (unsigned long long)mp_get_mag64(a);
+}
+#endif
 #endif
diff --git a/bn_mp_get_int.c b/bn_mp_get_int.c
deleted file mode 100644
index e3571e1..0000000
--- a/bn_mp_get_int.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_GET_INT_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* get the lower 32-bits of an mp_int */
-unsigned long mp_get_int(const mp_int *a)
-{
-   /* force result to 32-bits always so it is consistent on non 32-bit platforms */
-   return mp_get_long(a) & 0xFFFFFFFFuL;
-}
-#endif
diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
deleted file mode 100644
index a800efb..0000000
--- a/bn_mp_get_long.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_GET_LONG_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* get the lower unsigned long of an mp_int, platform dependent */
-unsigned long mp_get_long(const mp_int *a)
-{
-   int i;
-   unsigned long res;
-
-   if (MP_IS_ZERO(a)) {
-      return 0;
-   }
-
-   /* get number of digits of the lsb we have to read */
-   i = MP_MIN(a->used, (((int)MP_SIZEOF_BITS(unsigned long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)) - 1;
-
-   /* get most significant digit of result */
-   res = (unsigned long)a->dp[i];
-
-#if (ULONG_MAX != 0xFFFFFFFFuL) || (MP_DIGIT_BIT < 32)
-   while (--i >= 0) {
-      res = (res << MP_DIGIT_BIT) | (unsigned long)a->dp[i];
-   }
-#endif
-   return res;
-}
-#endif
diff --git a/bn_mp_get_long_long.c b/bn_mp_get_long_long.c
deleted file mode 100644
index 91dde4c..0000000
--- a/bn_mp_get_long_long.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_GET_LONG_LONG_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* get the lower unsigned long long of an mp_int, platform dependent */
-unsigned long long mp_get_long_long(const mp_int *a)
-{
-   int i;
-   unsigned long long res;
-
-   if (MP_IS_ZERO(a)) {
-      return 0;
-   }
-
-   /* get number of digits of the lsb we have to read */
-   i = MP_MIN(a->used, (((int)MP_SIZEOF_BITS(unsigned long long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)) - 1;
-
-   /* get most significant digit of result */
-   res = (unsigned long long)a->dp[i];
-
-#if MP_DIGIT_BIT < 64
-   while (--i >= 0) {
-      res = (res << MP_DIGIT_BIT) | (unsigned long long)a->dp[i];
-   }
-#endif
-   return res;
-}
-#endif
diff --git a/bn_mp_ilogb.c b/bn_mp_ilogb.c
index 0ca0193..6e189fc 100644
--- a/bn_mp_ilogb.c
+++ b/bn_mp_ilogb.c
@@ -76,7 +76,6 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
    mp_ord cmp;
    unsigned int high, low, mid;
    mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
-   mp_digit tmp;
 
    err = MP_OKAY;
    if (a->sign == MP_NEG) {
@@ -90,12 +89,11 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
       return MP_VAL;
    }
    if (base == 2u) {
-      mp_set_int(c, (unsigned long)(mp_count_bits(a) - 1));
+      mp_set_uint(c, (uint32_t)(mp_count_bits(a) - 1));
       return err;
    }
    if (a->used == 1) {
-      tmp = s_digit_ilogb(base, a->dp[0]);
-      mp_set(c, tmp);
+      mp_set(c, s_digit_ilogb(base, a->dp[0]));
       return err;
    }
 
@@ -106,7 +104,7 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
       return err;
    }
    if (cmp == MP_EQ) {
-      mp_set(c, (mp_digit)1uL);
+      mp_set(c, 1u);
       return err;
    }
 
@@ -163,15 +161,15 @@ mp_err mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
          mp_exch(&bracket_mid, &bracket_low);
       }
       if (cmp == MP_EQ) {
-         mp_set_int(c, (unsigned long)mid);
+         mp_set_uint(c, mid);
          goto LBL_END;
       }
    }
 
    if (mp_cmp(&bracket_high, a) == MP_EQ) {
-      mp_set_int(c, (unsigned long)high);
+      mp_set_uint(c, high);
    } else {
-      mp_set_int(c, (unsigned long)low);
+      mp_set_uint(c, low);
    }
 
 LBL_END:
diff --git a/bn_mp_init_set_int.c b/bn_mp_init_set_int.c
deleted file mode 100644
index fbb2b6b..0000000
--- a/bn_mp_init_set_int.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_INIT_SET_INT_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* initialize and set a digit */
-mp_err mp_init_set_int(mp_int *a, unsigned long b)
-{
-   mp_err err;
-   if ((err = mp_init(a)) != MP_OKAY) {
-      return err;
-   }
-   return mp_set_int(a, b);
-}
-#endif
diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c
index 491b576..f60f8da 100644
--- a/bn_mp_is_square.c
+++ b/bn_mp_is_square.c
@@ -58,13 +58,13 @@ mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
    }
 
 
-   if ((err = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
+   if ((err = mp_init_uint(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
       return err;
    }
    if ((err = mp_mod(arg, &t, &t)) != MP_OKAY) {
       goto LBL_ERR;
    }
-   r = mp_get_int(&t);
+   r = mp_get_uint(&t);
    /* Check for other prime modules, note it's not an ERROR but we must
     * free "t" so the easiest way is to goto LBL_ERR.  We know that err
     * is already equal to MP_OKAY from the mp_mod call
diff --git a/bn_mp_prime_frobenius_underwood.c b/bn_mp_prime_frobenius_underwood.c
index c01e280..d3e325d 100644
--- a/bn_mp_prime_frobenius_underwood.c
+++ b/bn_mp_prime_frobenius_underwood.c
@@ -43,7 +43,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
          continue;
       }
       /* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */
-      mp_set_long(&T1z, (unsigned long)a);
+      mp_set_uint(&T1z, (uint32_t)a);
 
       if ((err = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
          goto LBL_FU_ERR;
@@ -72,7 +72,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
       goto LBL_FU_ERR;
    }
    /* Composite if N and (a+4)*(2*a+5) are not coprime */
-   mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)));
+   mp_set_uint(&T1z, (uint32_t)((a+4)*((2*a)+5)));
 
    if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
       goto LBL_FU_ERR;
@@ -157,7 +157,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
       }
    }
 
-   mp_set_long(&T1z, (unsigned long)((2 * a) + 5));
+   mp_set_uint(&T1z, (uint32_t)((2 * a) + 5));
    if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
       goto LBL_FU_ERR;
    }
diff --git a/bn_mp_prime_strong_lucas_selfridge.c b/bn_mp_prime_strong_lucas_selfridge.c
index 5f288c8..5ec39e7 100644
--- a/bn_mp_prime_strong_lucas_selfridge.c
+++ b/bn_mp_prime_strong_lucas_selfridge.c
@@ -19,32 +19,21 @@
  * 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 mp_err s_mp_mul_si(const mp_int *a, long d, mp_int *c)
+static mp_err s_mp_mul_si(const mp_int *a, int32_t d, mp_int *c)
 {
    mp_int t;
    mp_err err;
-   int neg = 0;
 
    if ((err = mp_init(&t)) != MP_OKAY) {
       return err;
    }
-   if (d < 0) {
-      neg = 1;
-      d = -d;
-   }
 
    /*
     * mp_digit might be smaller than a long, which excludes
     * the use of mp_mul_d() here.
     */
-   mp_set_long(&t, (unsigned long) d);
-   if ((err = mp_mul(a, &t, c)) != MP_OKAY) {
-      goto LBL_MPMULSI_ERR;
-   }
-   if (neg ==  1) {
-      c->sign = (a->sign == MP_NEG) ? MP_ZPOS: MP_NEG;
-   }
-LBL_MPMULSI_ERR:
+   mp_set_sint(&t, d);
+   err = mp_mul(a, &t, c);
    mp_clear(&t);
    return err;
 }
@@ -94,7 +83,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
    for (;;) {
       Ds   = sign * D;
       sign = -sign;
-      mp_set_long(&Dz, (unsigned long)D);
+      mp_set_uint(&Dz, (uint32_t)D);
       if ((err = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) {
          goto LBL_LS_ERR;
       }
@@ -183,30 +172,30 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
       combined with the previous totals for U and V, using the
       composition formulas for addition of indices. */
 
-   mp_set(&Uz, 1uL);    /* U=U_1 */
+   mp_set(&Uz, 1u);    /* U=U_1 */
    mp_set(&Vz, (mp_digit)P);    /* V=V_1 */
-   mp_set(&U2mz, 1uL);  /* U_1 */
+   mp_set(&U2mz, 1u);  /* U_1 */
    mp_set(&V2mz, (mp_digit)P);  /* V_1 */
 
    if (Q < 0) {
       Q = -Q;
-      mp_set_long(&Qmz, (unsigned long)Q);
+      mp_set_uint(&Qmz, (uint32_t)Q);
       if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
          goto LBL_LS_ERR;
       }
       /* Initializes calculation of Q^d */
-      mp_set_long(&Qkdz, (unsigned long)Q);
+      mp_set_uint(&Qkdz, (uint32_t)Q);
       Qmz.sign = MP_NEG;
       Q2mz.sign = MP_NEG;
       Qkdz.sign = MP_NEG;
       Q = -Q;
    } else {
-      mp_set_long(&Qmz, (unsigned long)Q);
+      mp_set_uint(&Qmz, (uint32_t)Q);
       if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
          goto LBL_LS_ERR;
       }
       /* Initializes calculation of Q^d */
-      mp_set_long(&Qkdz, (unsigned long)Q);
+      mp_set_uint(&Qkdz, (uint32_t)Q);
    }
 
    Nbits = mp_count_bits(&Dz);
@@ -266,7 +255,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
          if ((err = mp_mul(&U2mz, &Uz, &T4z)) != MP_OKAY) {
             goto LBL_LS_ERR;
          }
-         if ((err = s_mp_mul_si(&T4z, (long)Ds, &T4z)) != MP_OKAY) {
+         if ((err = s_mp_mul_si(&T4z, Ds, &T4z)) != MP_OKAY) {
             goto LBL_LS_ERR;
          }
          if ((err = mp_add(&T1z, &T2z, &Uz)) != MP_OKAY) {
diff --git a/bn_mp_set.c b/bn_mp_set.c
index a41b711..44ac6df 100644
--- a/bn_mp_set.c
+++ b/bn_mp_set.c
@@ -6,8 +6,9 @@
 /* set to a digit */
 void mp_set(mp_int *a, mp_digit b)
 {
-   mp_zero(a);
    a->dp[0] = b & MP_MASK;
+   a->sign  = MP_ZPOS;
    a->used  = (a->dp[0] != 0u) ? 1 : 0;
+   MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);
 }
 #endif
diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c
index 9e00362..9583b06 100644
--- a/bn_mp_set_double.c
+++ b/bn_mp_set_double.c
@@ -23,7 +23,7 @@ mp_err mp_set_double(mp_int *a, double b)
    }
    exp -= 1023 + 52;
 
-   mp_set_long_long(a, frac);
+   mp_set_uint64(a, frac);
 
    err = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a);
    if (err != MP_OKAY) {
diff --git a/bn_mp_set_int.c b/bn_mp_set_int.c
deleted file mode 100644
index f0c7434..0000000
--- a/bn_mp_set_int.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_SET_INT_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* set a 32-bit const */
-mp_err mp_set_int(mp_int *a, unsigned long b)
-{
-   return mp_set_long(a, b & 0xFFFFFFFFuL);
-}
-#endif
diff --git a/bn_mp_set_long.c b/bn_mp_set_long.c
deleted file mode 100644
index e5d45d8..0000000
--- a/bn_mp_set_long.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_SET_LONG_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-MP_SET_XLONG(mp_set_long, unsigned long)
-#endif
diff --git a/bn_mp_set_long_long.c b/bn_mp_set_long_long.c
deleted file mode 100644
index 6beecd8..0000000
--- a/bn_mp_set_long_long.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_SET_LONG_LONG_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* set a platform dependent unsigned long long int */
-MP_SET_XLONG(mp_set_long_long, unsigned long long)
-#endif
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index 26770ae..bd37813 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -59,7 +59,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    }
 
    /* find a Z such that the Legendre symbol (Z|prime) == -1 */
-   mp_set_int(&Z, 2uL);
+   mp_set_uint(&Z, 2uL);
    /* Z = 2 */
    while (1) {
       if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup;
@@ -79,7 +79,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    /* T = n ^ Q mod prime */
    if ((err = mp_copy(&S, &M)) != MP_OKAY)                       goto cleanup;
    /* M = S */
-   mp_set_int(&two, 2uL);
+   mp_set_uint(&two, 2uL);
 
    while (1) {
       if ((err = mp_copy(&T, &t1)) != MP_OKAY)                    goto cleanup;
diff --git a/demo/test.c b/demo/test.c
index 12d2078..f6ff0c5 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -20,11 +20,41 @@ static int rand_int(void)
    return x;
 }
 
+static int32_t rand_int32(void)
+{
+   int32_t x;
+   if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) {
+      fprintf(stderr, "s_mp_rand_source failed\n");
+      exit(EXIT_FAILURE);
+   }
+   return x;
+}
+
+static int64_t rand_int64(void)
+{
+   int64_t x;
+   if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) {
+      fprintf(stderr, "s_mp_rand_source failed\n");
+      exit(EXIT_FAILURE);
+   }
+   return x;
+}
+
 static unsigned long ulabs(long x)
 {
    return x > 0 ? (unsigned long)x : -(unsigned long)x;
 }
 
+static uint32_t uabs32(int32_t x)
+{
+   return x > 0 ? (uint32_t)x : -(uint32_t)x;
+}
+
+static uint64_t uabs64(int64_t x)
+{
+   return x > 0 ? (uint64_t)x : -(uint64_t)x;
+}
+
 static int test_trivial_stuff(void)
 {
    mp_int a, b, c, d;
@@ -35,7 +65,7 @@ static int test_trivial_stuff(void)
    (void)mp_error_to_string(e);
 
    /* a: 0->5 */
-   mp_set_int(&a, 5uL);
+   mp_set(&a, 5u);
    /* a: 5-> b: -5 */
    mp_neg(&a, &b);
    if (mp_cmp(&a, &b) != MP_GT) {
@@ -59,33 +89,39 @@ static int test_trivial_stuff(void)
    if (mp_isneg(&b) != MP_YES) {
       goto LBL_ERR;
    }
-   if (mp_get_int(&b) != 4) {
+   if (mp_get_sint(&b) != -4) {
+      goto LBL_ERR;
+   }
+   if (mp_get_uint(&b) != (uint32_t)-4) {
+      goto LBL_ERR;
+   }
+   if (mp_get_mag(&b) != 4) {
       goto LBL_ERR;
    }
    /* a: -5-> b: 1 */
    mp_add_d(&a, 6uL, &b);
-   if (mp_get_int(&b) != 1) {
+   if (mp_get_uint(&b) != 1) {
       goto LBL_ERR;
    }
    /* a: -5-> a: 1 */
    mp_add_d(&a, 6uL, &a);
-   if (mp_get_int(&a) != 1) {
+   if (mp_get_uint(&a) != 1) {
       goto LBL_ERR;
    }
    mp_zero(&a);
    /* a: 0-> a: 6 */
    mp_add_d(&a, 6uL, &a);
-   if (mp_get_int(&a) != 6) {
+   if (mp_get_uint(&a) != 6) {
       goto LBL_ERR;
    }
 
-   mp_set_int(&a, 42uL);
-   mp_set_int(&b, 1uL);
+   mp_set(&a, 42u);
+   mp_set(&b, 1u);
    mp_neg(&b, &b);
-   mp_set_int(&c, 1uL);
+   mp_set(&c, 1u);
    mp_exptmod(&a, &b, &c, &d);
 
-   mp_set_int(&c, 7uL);
+   mp_set(&c, 7u);
    mp_exptmod(&a, &b, &c, &d);
 
    if (mp_iseven(&a) == mp_isodd(&a)) {
@@ -99,6 +135,92 @@ LBL_ERR:
    return EXIT_FAILURE;
 }
 
+static int check_get_set_int32(mp_int *a, int32_t b)
+{
+   mp_set_sint(a, b);
+   if (mp_get_sint(a) != b) return EXIT_FAILURE;
+   if (mp_get_uint(a) != (uint32_t)b) return EXIT_FAILURE;
+   if (mp_get_mag(a) != uabs32(b)) return EXIT_FAILURE;
+
+   mp_set_uint(a, (uint32_t)b);
+   if (mp_get_uint(a) != (uint32_t)b) return EXIT_FAILURE;
+   if (mp_get_sint(a) != (int32_t)(uint32_t)b) return EXIT_FAILURE;
+
+   return EXIT_SUCCESS;
+}
+
+static int test_mp_get_set_int(void)
+{
+   int i;
+   mp_int a;
+
+   if (mp_init(&a) != MP_OKAY) {
+      return EXIT_FAILURE;
+   }
+
+   check_get_set_int32(&a, 0);
+   check_get_set_int32(&a, -1);
+   check_get_set_int32(&a, 1);
+   check_get_set_int32(&a, INT32_MIN);
+   check_get_set_int32(&a, INT32_MAX);
+
+   for (i = 0; i < 1000; ++i) {
+      int32_t b = rand_int32();
+      if (check_get_set_int32(&a, b) != EXIT_SUCCESS) {
+         goto LBL_ERR;
+      }
+   }
+
+   mp_clear(&a);
+   return EXIT_SUCCESS;
+LBL_ERR:
+   mp_clear(&a);
+   return EXIT_FAILURE;
+}
+
+static int check_get_set_int64(mp_int *a, int64_t b)
+{
+   mp_set_sint64(a, b);
+   if (mp_get_sint64(a) != b) return EXIT_FAILURE;
+   if (mp_get_uint64(a) != (uint64_t)b) return EXIT_FAILURE;
+   if (mp_get_mag64(a) != uabs64(b)) return EXIT_FAILURE;
+
+   mp_set_uint64(a, (uint64_t)b);
+   if (mp_get_uint64(a) != (uint64_t)b) return EXIT_FAILURE;
+   if (mp_get_sint64(a) != (int64_t)(uint64_t)b) return EXIT_FAILURE;
+
+   return EXIT_SUCCESS;
+}
+
+static int test_mp_get_set_int64(void)
+{
+   int i;
+   mp_int a;
+
+   if (mp_init(&a) != MP_OKAY) {
+      return EXIT_FAILURE;
+   }
+
+   check_get_set_int64(&a, 0);
+   check_get_set_int64(&a, -1);
+   check_get_set_int64(&a, 1);
+   check_get_set_int64(&a, INT64_MIN);
+   check_get_set_int64(&a, INT64_MAX);
+
+   for (i = 0; i < 1000; ++i) {
+      int64_t b = rand_int64();
+      if (check_get_set_int64(&a, b) != EXIT_SUCCESS) {
+         goto LBL_ERR;
+      }
+   }
+
+   mp_clear(&a);
+   return EXIT_SUCCESS;
+LBL_ERR:
+   mp_clear(&a);
+   return EXIT_FAILURE;
+}
+
 static int test_mp_fread_fwrite(void)
 {
    mp_int a, b;
@@ -108,7 +230,7 @@ static int test_mp_fread_fwrite(void)
       return EXIT_FAILURE;
    }
 
-   mp_set_int(&a, 123456uL);
+   mp_set_ulong(&a, 123456uL);
    tmp = tmpfile();
    if ((e = mp_fwrite(&a, 64, tmp)) != MP_OKAY) {
       goto LBL_ERR;
@@ -117,7 +239,7 @@ static int test_mp_fread_fwrite(void)
    if ((e = mp_fread(&b, 64, tmp)) != MP_OKAY) {
       goto LBL_ERR;
    }
-   if (mp_get_int(&b) != 123456uL) {
+   if (mp_get_uint(&b) != 123456uL) {
       goto LBL_ERR;
    }
    fclose(tmp);
@@ -191,8 +313,8 @@ static int test_s_mp_jacobi(void)
       return EXIT_FAILURE;
    }
 
-   mp_set_int(&a, 0uL);
-   mp_set_int(&b, 1uL);
+   mp_set_ulong(&a, 0uL);
+   mp_set_ulong(&b, 1uL);
    if ((err = s_mp_jacobi(&a, &b, &i)) != MP_OKAY) {
       printf("Failed executing s_mp_jacobi(0 | 1) %s.\n", mp_error_to_string(err));
       goto LBL_ERR;
@@ -202,10 +324,10 @@ static int test_s_mp_jacobi(void)
       goto LBL_ERR;
    }
    for (cnt = 0; cnt < (int)(sizeof(jacobi)/sizeof(jacobi[0])); ++cnt) {
-      mp_set_int(&b, jacobi[cnt].n);
+      mp_set_ulong(&b, jacobi[cnt].n);
       /* only test positive values of a */
       for (n = -5; n <= 10; ++n) {
-         mp_set_int(&a, (unsigned int)abs(n));
+         mp_set_ulong(&a, (unsigned int)abs(n));
          should = MP_OKAY;
          if (n < 0) {
             mp_neg(&a, &a);
@@ -269,8 +391,8 @@ static int test_mp_kronecker(void)
       return EXIT_FAILURE;
    }
 
-   mp_set_int(&a, 0uL);
-   mp_set_int(&b, 1uL);
+   mp_set_ulong(&a, 0uL);
+   mp_set_ulong(&b, 1uL);
    if ((err = mp_kronecker(&a, &b, &i)) != MP_OKAY) {
       printf("Failed executing mp_kronecker(0 | 1) %s.\n", mp_error_to_string(err));
       goto LBL_ERR;
@@ -282,18 +404,18 @@ static int test_mp_kronecker(void)
    for (cnt = 0; cnt < (int)(sizeof(kronecker)/sizeof(kronecker[0])); ++cnt) {
       k = kronecker[cnt].n;
       if (k < 0) {
-         mp_set_int(&a, (unsigned long)(-k));
+         mp_set_ulong(&a, (unsigned long)(-k));
          mp_neg(&a, &a);
       } else {
-         mp_set_int(&a, (unsigned long) k);
+         mp_set_ulong(&a, (unsigned long) k);
       }
       /* only test positive values of a */
       for (m = -10; m <= 10; m++) {
          if (m < 0) {
-            mp_set_int(&b,(unsigned long)(-m));
+            mp_set_ulong(&b,(unsigned long)(-m));
             mp_neg(&b, &b);
          } else {
-            mp_set_int(&b, (unsigned long) m);
+            mp_set_ulong(&b, (unsigned long) m);
          }
          if ((err = mp_kronecker(&a, &b, &i)) != MP_OKAY) {
             printf("Failed executing mp_kronecker(%ld | %ld) %s.\n", kronecker[cnt].n, m, mp_error_to_string(err));
@@ -324,13 +446,13 @@ static int test_mp_complement(void)
 
    for (i = 0; i < 1000; ++i) {
       long l = rand_long();
-      mp_set_long(&a, ulabs(l));
+      mp_set_ulong(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
       mp_complement(&a, &b);
 
       l = ~l;
-      mp_set_long(&c, ulabs(l));
+      mp_set_ulong(&c, ulabs(l));
       if (l < 0)
          mp_neg(&c, &c);
 
@@ -361,13 +483,13 @@ static int test_mp_signed_rsh(void)
       int em;
 
       l = rand_long();
-      mp_set_long(&a, ulabs(l));
+      mp_set_ulong(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
       em = abs(rand_int()) % 32;
 
-      mp_set_long(&d, ulabs(l >> em));
+      mp_set_ulong(&d, ulabs(l >> em));
       if ((l >> em) < 0)
          mp_neg(&d, &d);
 
@@ -399,16 +521,16 @@ static int test_mp_xor(void)
       long l, em;
 
       l = rand_long();
-      mp_set_int(&a, ulabs(l));
+      mp_set_ulong(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
       em = rand_long();
-      mp_set_int(&b, ulabs(em));
+      mp_set_ulong(&b, ulabs(em));
       if (em < 0)
          mp_neg(&b, &b);
 
-      mp_set_int(&d, ulabs(l ^ em));
+      mp_set_ulong(&d, ulabs(l ^ em));
       if ((l ^ em) < 0)
          mp_neg(&d, &d);
 
@@ -440,16 +562,16 @@ static int test_mp_or(void)
       long l, em;
 
       l = rand_long();
-      mp_set_long(&a, ulabs(l));
+      mp_set_ulong(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
       em = rand_long();
-      mp_set_long(&b, ulabs(em));
+      mp_set_ulong(&b, ulabs(em));
       if (em < 0)
          mp_neg(&b, &b);
 
-      mp_set_long(&d, ulabs(l | em));
+      mp_set_ulong(&d, ulabs(l | em));
       if ((l | em) < 0)
          mp_neg(&d, &d);
 
@@ -480,16 +602,16 @@ static int test_mp_and(void)
       long l, em;
 
       l = rand_long();
-      mp_set_long(&a, ulabs(l));
+      mp_set_ulong(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
       em = rand_long();
-      mp_set_long(&b, ulabs(em));
+      mp_set_ulong(&b, ulabs(em));
       if (em < 0)
          mp_neg(&b, &b);
 
-      mp_set_long(&d, ulabs(l & em));
+      mp_set_ulong(&d, ulabs(l & em));
       if ((l & em) < 0)
          mp_neg(&d, &d);
 
@@ -610,7 +732,7 @@ LBL_ERR:
 
 }
 
-static int test_mp_get_int(void)
+static int test_mp_get_uint(void)
 {
    unsigned long t;
    int i;
@@ -622,20 +744,20 @@ static int test_mp_get_int(void)
 
    for (i = 0; i < 1000; ++i) {
       t = (unsigned long)rand_long() & 0xFFFFFFFFuL;
-      mp_set_int(&a, t);
-      if (t != mp_get_int(&a)) {
-         printf("\nmp_get_int() bad result!");
+      mp_set_ulong(&a, t);
+      if (t != mp_get_uint(&a)) {
+         printf("\nmp_get_uint() bad result!");
          goto LBL_ERR;
       }
    }
-   mp_set_int(&a, 0uL);
-   if (mp_get_int(&a) != 0) {
-      printf("\nmp_get_int() bad result!");
+   mp_set_ulong(&a, 0uL);
+   if (mp_get_uint(&a) != 0) {
+      printf("\nmp_get_uint() bad result!");
       goto LBL_ERR;
    }
-   mp_set_int(&a, 0xFFFFFFFFuL);
-   if (mp_get_int(&a) != 0xFFFFFFFFuL) {
-      printf("\nmp_get_int() bad result!");
+   mp_set_ulong(&a, 0xFFFFFFFFuL);
+   if (mp_get_uint(&a) != 0xFFFFFFFFuL) {
+      printf("\nmp_get_uint() bad result!");
       goto LBL_ERR;
    }
 
@@ -646,7 +768,7 @@ LBL_ERR:
    return EXIT_FAILURE;
 }
 
-static int test_mp_get_long(void)
+static int test_mp_get_ulong(void)
 {
    unsigned long s, t;
    int i;
@@ -662,10 +784,10 @@ static int test_mp_get_long(void)
          t = ~0UL;
       printf(" t = 0x%lx i = %d\r", t, i);
       do {
-         mp_set_long(&a, t);
-         s = mp_get_long(&a);
+         mp_set_ulong(&a, t);
+         s = mp_get_ulong(&a);
          if (s != t) {
-            printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t);
+            printf("\nmp_get_ulong() bad result! 0x%lx != 0x%lx", s, t);
             goto LBL_ERR;
          }
          t <<= 1;
@@ -679,7 +801,7 @@ LBL_ERR:
    return EXIT_FAILURE;
 }
 
-static int test_mp_get_long_long(void)
+static int test_mp_get_uint64(void)
 {
    unsigned long long q, r;
    int i;
@@ -695,10 +817,10 @@ static int test_mp_get_long_long(void)
          r = ~0ULL;
       printf(" r = 0x%llx i = %d\r", r, i);
       do {
-         mp_set_long_long(&a, r);
-         q = mp_get_long_long(&a);
+         mp_set_uint64(&a, r);
+         q = mp_get_uint64(&a);
          if (q != r) {
-            printf("\nmp_get_long_long() bad result! 0x%llx != 0x%llx", q, r);
+            printf("\nmp_get_uint64() bad result! 0x%llx != 0x%llx", q, r);
             goto LBL_ERR;
          }
          r <<= 1;
@@ -821,8 +943,8 @@ static int test_mp_sqrtmod_prime(void)
 
    /* r^2 = n (mod p) */
    for (i = 0; i < (int)(sizeof(sqrtmod_prime)/sizeof(sqrtmod_prime[0])); ++i) {
-      mp_set_int(&a, sqrtmod_prime[i].p);
-      mp_set_int(&b, sqrtmod_prime[i].n);
+      mp_set_ulong(&a, sqrtmod_prime[i].p);
+      mp_set_ulong(&b, sqrtmod_prime[i].n);
       if (mp_sqrtmod_prime(&b, &a, &c) != MP_OKAY) {
          printf("Failed executing %d. mp_sqrtmod_prime\n", (i+1));
          goto LBL_ERR;
@@ -2021,6 +2143,8 @@ int unit_tests(int argc, char **argv)
    } test[] = {
 #define T(n) { #n, test_##n }
       T(trivial_stuff),
+      T(mp_get_set_int),
+      T(mp_get_set_int64),
       T(mp_and),
       T(mp_cnt_lsb),
       T(mp_complement),
@@ -2028,9 +2152,9 @@ int unit_tests(int argc, char **argv)
       T(mp_div_3),
       T(mp_dr_reduce),
       T(mp_fread_fwrite),
-      T(mp_get_int),
-      T(mp_get_long),
-      T(mp_get_long_long),
+      T(mp_get_uint),
+      T(mp_get_uint64),
+      T(mp_get_ulong),
       T(mp_ilogb),
       T(mp_incr),
       T(mp_invmod),
diff --git a/dep.pl b/dep.pl
index 6625cb2..ceef32d 100755
--- a/dep.pl
+++ b/dep.pl
@@ -96,7 +96,7 @@ EOS
 
    # scan for mp_* and make classes
    foreach my $line (split /\n/, $content) {
-      while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()/g) {
+      while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()|(?<=\()mp\_[a-z_0-9]*(?=,)/g) {
           my $a = $&;
           next if $a eq "mp_err";
           $a =~ tr/[a-z]/[A-Z]/;
@@ -167,4 +167,3 @@ sub draw_func
    $list = $temp;
    return;
 }
-
diff --git a/doc/bn.tex b/doc/bn.tex
index f9f8f6b..5b0584a 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -820,28 +820,33 @@ int main(void)
 \}
 \end{alltt} \end{small}
 
-\subsection{Long Constants}
+\subsection{Int32 and Int64 Constants}
 
-To set a constant that is the size of an ISO C ``unsigned long'' and larger than a single digit the following function
-can be used.
+These functions can be used to set a constant with 32 or 64 bits.
 
-\index{mp\_set\_int}
+\index{mp\_set\_sint}
 \begin{alltt}
-int mp_set_int (mp_int * a, unsigned long b);
+void mp_set_sint (mp_int * a, int32_t b);
+void mp_set_uint (mp_int * a, uint32_t b);
+void mp_set_sint64 (mp_int * a, int64_t b);
+void mp_set_uint64 (mp_int * a, uint64_t b);
 \end{alltt}
 
-This will assign the value of the 32-bit variable $b$ to the mp\_int $a$.  Unlike mp\_set() this function will always
-accept a 32-bit input regardless of the size of a single digit.  However, since the value may span several digits
-this function can fail if it runs out of heap memory.
-
-To get the ``unsigned long'' copy of an mp\_int the following function can be used.
+These functions assign the sign and value of the input \texttt{b} to \texttt{mp_int a}.
+The value can be obtained again by calling the following functions.
 
 \index{mp\_get\_int}
 \begin{alltt}
-unsigned long mp_get_int (mp_int * a);
+int32_t mp_get_sint (mp_int * a);
+uint32_t mp_get_uint (mp_int * a);
+uint32_t mp_get_mag (mp_int * a);
+int64_t mp_get_sint64 (mp_int * a);
+uint64_t mp_get_uint64 (mp_int * a);
+uint64_t mp_get_mag64 (mp_int * a);
 \end{alltt}
 
-This will return the 32 least significant bits of the mp\_int $a$.
+These functions return the 32 or 64 least significant bits of $a$ respectively. The unsigned functions
+return negative values in a twos complement representation. The absolute value or magnitude can be obtained using the mp\_get\_mag functions.
 
 \begin{small} \begin{alltt}
 int main(void)
@@ -856,7 +861,7 @@ int main(void)
    \}
 
    /* set the number to 654321 (note this is bigger than 127) */
-   mp_set_int(&number, 654321);
+   mp_set_uint(&number, 654321);
 
    printf("number == \%lu", mp_get_int(&number));
 
@@ -875,46 +880,31 @@ number == 654321
 
 \subsection{Long Constants - platform dependant}
 
-\index{mp\_set\_long}
+\index{mp\_set\_ulong}
 \begin{alltt}
-int mp_set_long (mp_int * a, unsigned long b);
+void mp_set_slong (mp_int * a, long b);
+void mp_set_ulong (mp_int * a, unsigned long b);
 \end{alltt}
 
 This will assign the value of the platform-dependent sized variable $b$ to the mp\_int $a$.
 
-To get the ``unsigned long'' copy of an mp\_int the following function can be used.
-
-\index{mp\_get\_long}
-\begin{alltt}
-unsigned long mp_get_long (mp_int * a);
-\end{alltt}
-
-This will return the least significant bits of the mp\_int $a$ that fit into an ``unsigned long''.
-
-\subsection{Long Long Constants}
-
-\index{mp\_set\_long\_long}
-\begin{alltt}
-int mp_set_long_long (mp_int * a, unsigned long long b);
-\end{alltt}
-
-This will assign the value of the 64-bit variable $b$ to the mp\_int $a$.
-
-To get the ``unsigned long long'' copy of an mp\_int the following function can be used.
+To retrieve the value, the following functions can be used.
 
-\index{mp\_get\_long\_long}
+\index{mp\_get\_ulong}
 \begin{alltt}
-unsigned long long mp_get_long_long (mp_int * a);
+long mp_get_slong (mp_int * a);
+unsigned long mp_get_ulong (mp_int * a);
 \end{alltt}
 
-This will return the 64 least significant bits of the mp\_int $a$.
+This will return the least significant bits of the mp\_int $a$ that fit into a ``long''.
 
 \subsection{Initialize and Setting Constants}
 To both initialize and set small constants the following two functions are available.
 \index{mp\_init\_set} \index{mp\_init\_set\_int}
 \begin{alltt}
 int mp_init_set (mp_int * a, mp_digit b);
-int mp_init_set_int (mp_int * a, unsigned long b);
+int mp_init_set_sint (mp_int * a, int32_t b);
+int mp_init_set_uint (mp_int * a, uint32_t b);
 \end{alltt}
 
 Both functions work like the previous counterparts except they first mp\_init $a$ before setting the values.
@@ -933,7 +923,7 @@ int main(void)
    \}
 
    /* initialize and set a long */
-   if ((result = mp_init_set_int(&number2, 1023)) != MP_OKAY) \{
+   if ((result = mp_init_set_uint(&number2, 1023)) != MP_OKAY) \{
       printf("Error setting number2: \%s",
              mp_error_to_string(result));
       return EXIT_FAILURE;
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 84e0b16..0389a25 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -313,6 +313,10 @@
 	</References>
 	<Files>
 		<File
+			RelativePath="bn_conversion.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_cutoffs.c"
 			>
 		</File>
@@ -465,18 +469,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_get_int.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_get_long.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_get_long_long.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_grow.c"
 			>
 		</File>
@@ -509,10 +501,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_init_set_int.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_init_size.c"
 			>
 		</File>
@@ -709,18 +697,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_set_int.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_set_long.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_set_long_long.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_shrink.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index 6dbe1ab..f09c94a 100644
--- a/makefile
+++ b/makefile
@@ -26,34 +26,32 @@ endif
 LCOV_ARGS=--directory .
 
 #START_INS
-OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o bn_mp_addmod.o \
-bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \
-bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \
-bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \
-bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o bn_mp_expt_d_ex.o bn_mp_exptmod.o \
-bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_int.o \
-bn_mp_get_long.o bn_mp_get_long_long.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \
-bn_mp_init_copy.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_set_int.o bn_mp_init_size.o \
-bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \
-bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \
-bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o \
-bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o bn_mp_or.o bn_mp_prime_fermat.o \
-bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o bn_mp_prime_is_prime.o \
-bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
-bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
-bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
-bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
-bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
-bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
-bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
-bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
-bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \
-bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \
-bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \
-bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \
-bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \
-bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
-bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+OBJECTS=bn_conversion.o bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o \
+bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o \
+bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
+bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
+bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
+bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
+bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
+bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
+bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
+bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
+bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
+bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
+bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
+bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \
+bn_mp_set.o bn_mp_set_double.o bn_mp_shrink.o bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o \
+bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o \
+bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
+bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
+bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
+bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
+bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
+bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
+bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.mingw b/makefile.mingw
index 2ed79b3..3d290b6 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -29,24 +29,32 @@ LIBMAIN_I =libtommath.dll.a
 LIBMAIN_D =libtommath.dll
 
 #List of objects to compile (all goes to libtommath.a)
-OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o bn_mp_addmod.o \
-bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \
-bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \
-bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \
-bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o bn_mp_expt_d_ex.o bn_mp_exptmod.o \
-bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_int.o \
-bn_mp_get_long.o bn_mp_get_long_long.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \
-bn_mp_init_copy.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_set_int.o bn_mp_init_size.o \
-bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \
-bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \
-bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o \
-bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o bn_mp_or.o bn_mp_prime_fermat.o \
-bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o bn_mp_prime_is_prime.o \
-bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
-bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
-bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
-bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
-bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
+OBJECTS=bn_conversion.o bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o \
+bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o \
+bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
+bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
+bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
+bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
+bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
+bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
+bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
+bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
+bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
+bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
+bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
+bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \
+bn_mp_set.o bn_mp_set_double.o bn_mp_shrink.o bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o \
+bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o \
+bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
+bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
+bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
+bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
+bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
+bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
+bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
 bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
 bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
diff --git a/makefile.msvc b/makefile.msvc
index e957530..da87c6e 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -21,24 +21,32 @@ LTM_LDFLAGS = advapi32.lib
 LIBMAIN_S =tommath.lib
 
 #List of objects to compile (all goes to tommath.lib)
-OBJECTS=bn_cutoffs.obj bn_deprecated.obj bn_mp_2expt.obj bn_mp_abs.obj bn_mp_add.obj bn_mp_add_d.obj bn_mp_addmod.obj \
-bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi.obj bn_mp_cmp.obj bn_mp_cmp_d.obj bn_mp_cmp_mag.obj \
-bn_mp_cnt_lsb.obj bn_mp_complement.obj bn_mp_copy.obj bn_mp_count_bits.obj bn_mp_decr.obj bn_mp_div.obj bn_mp_div_2.obj \
-bn_mp_div_2d.obj bn_mp_div_3.obj bn_mp_div_d.obj bn_mp_dr_is_modulus.obj bn_mp_dr_reduce.obj bn_mp_dr_setup.obj \
-bn_mp_error_to_string.obj bn_mp_exch.obj bn_mp_export.obj bn_mp_expt_d.obj bn_mp_expt_d_ex.obj bn_mp_exptmod.obj \
-bn_mp_exteuclid.obj bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_gcd.obj bn_mp_get_double.obj bn_mp_get_int.obj \
-bn_mp_get_long.obj bn_mp_get_long_long.obj bn_mp_grow.obj bn_mp_ilogb.obj bn_mp_import.obj bn_mp_incr.obj bn_mp_init.obj \
-bn_mp_init_copy.obj bn_mp_init_multi.obj bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_init_size.obj \
-bn_mp_invmod.obj bn_mp_is_square.obj bn_mp_iseven.obj bn_mp_isodd.obj bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_lshd.obj \
-bn_mp_mod.obj bn_mp_mod_2d.obj bn_mp_mod_d.obj bn_mp_montgomery_calc_normalization.obj bn_mp_montgomery_reduce.obj \
-bn_mp_montgomery_setup.obj bn_mp_mul.obj bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj \
-bn_mp_n_root.obj bn_mp_n_root_ex.obj bn_mp_neg.obj bn_mp_or.obj bn_mp_prime_fermat.obj \
-bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_divisible.obj bn_mp_prime_is_prime.obj \
-bn_mp_prime_miller_rabin.obj bn_mp_prime_next_prime.obj bn_mp_prime_rabin_miller_trials.obj \
-bn_mp_prime_rand.obj bn_mp_prime_strong_lucas_selfridge.obj bn_mp_radix_size.obj bn_mp_radix_smap.obj \
-bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj bn_mp_read_unsigned_bin.obj bn_mp_reduce.obj \
-bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj bn_mp_reduce_2k_setup.obj bn_mp_reduce_2k_setup_l.obj \
-bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_rshd.obj bn_mp_set.obj \
+OBJECTS=bn_conversion.obj bn_cutoffs.obj bn_deprecated.obj bn_mp_2expt.obj bn_mp_abs.obj bn_mp_add.obj bn_mp_add_d.obj \
+bn_mp_addmod.obj bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi.obj bn_mp_cmp.obj bn_mp_cmp_d.obj \
+bn_mp_cmp_mag.obj bn_mp_cnt_lsb.obj bn_mp_complement.obj bn_mp_copy.obj bn_mp_count_bits.obj bn_mp_decr.obj \
+bn_mp_div.obj bn_mp_div_2.obj bn_mp_div_2d.obj bn_mp_div_3.obj bn_mp_div_d.obj bn_mp_dr_is_modulus.obj \
+bn_mp_dr_reduce.obj bn_mp_dr_setup.obj bn_mp_error_to_string.obj bn_mp_exch.obj bn_mp_export.obj bn_mp_expt_d.obj \
+bn_mp_expt_d_ex.obj bn_mp_exptmod.obj bn_mp_exteuclid.obj bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_gcd.obj \
+bn_mp_get_double.obj bn_mp_grow.obj bn_mp_ilogb.obj bn_mp_import.obj bn_mp_incr.obj bn_mp_init.obj bn_mp_init_copy.obj \
+bn_mp_init_multi.obj bn_mp_init_set.obj bn_mp_init_size.obj bn_mp_invmod.obj bn_mp_is_square.obj bn_mp_iseven.obj \
+bn_mp_isodd.obj bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_lshd.obj bn_mp_mod.obj bn_mp_mod_2d.obj bn_mp_mod_d.obj \
+bn_mp_montgomery_calc_normalization.obj bn_mp_montgomery_reduce.obj bn_mp_montgomery_setup.obj bn_mp_mul.obj \
+bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj bn_mp_n_root.obj bn_mp_n_root_ex.obj bn_mp_neg.obj \
+bn_mp_or.obj bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_divisible.obj \
+bn_mp_prime_is_prime.obj bn_mp_prime_miller_rabin.obj bn_mp_prime_next_prime.obj \
+bn_mp_prime_rabin_miller_trials.obj bn_mp_prime_rand.obj bn_mp_prime_strong_lucas_selfridge.obj \
+bn_mp_radix_size.obj bn_mp_radix_smap.obj bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj \
+bn_mp_read_unsigned_bin.obj bn_mp_reduce.obj bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj bn_mp_reduce_2k_setup.obj \
+bn_mp_reduce_2k_setup_l.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_rshd.obj \
+bn_mp_set.obj bn_mp_set_double.obj bn_mp_shrink.obj bn_mp_signed_bin_size.obj bn_mp_signed_rsh.obj bn_mp_sqr.obj \
+bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj bn_mp_sub.obj bn_mp_sub_d.obj bn_mp_submod.obj \
+bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin.obj bn_mp_to_unsigned_bin_n.obj \
+bn_mp_toradix.obj bn_mp_toradix_n.obj bn_mp_unsigned_bin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj \
+bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_get_bit.obj \
+bn_s_mp_invmod_fast.obj bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj \
+bn_s_mp_montgomery_reduce_fast.obj bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj \
+bn_s_mp_mul_high_digs_fast.obj bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj \
+bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
 bn_mp_set_double.obj bn_mp_set_int.obj bn_mp_set_long.obj bn_mp_set_long_long.obj bn_mp_shrink.obj \
 bn_mp_signed_bin_size.obj bn_mp_signed_rsh.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj \
 bn_mp_sub.obj bn_mp_sub_d.obj bn_mp_submod.obj bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj \
diff --git a/makefile.shared b/makefile.shared
index 371291b..059a0f2 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -23,24 +23,32 @@ LTLINK = $(LIBTOOL) --mode=link --tag=CC $(CC)
 LCOV_ARGS=--directory .libs --directory .
 
 #START_INS
-OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o bn_mp_addmod.o \
-bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \
-bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \
-bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \
-bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o bn_mp_expt_d_ex.o bn_mp_exptmod.o \
-bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_int.o \
-bn_mp_get_long.o bn_mp_get_long_long.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \
-bn_mp_init_copy.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_set_int.o bn_mp_init_size.o \
-bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \
-bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \
-bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o \
-bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o bn_mp_or.o bn_mp_prime_fermat.o \
-bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o bn_mp_prime_is_prime.o \
-bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
-bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
-bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
-bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
-bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
+OBJECTS=bn_conversion.o bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o \
+bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o \
+bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
+bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
+bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
+bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
+bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
+bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
+bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
+bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
+bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
+bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
+bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
+bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \
+bn_mp_set.o bn_mp_set_double.o bn_mp_shrink.o bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o \
+bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o \
+bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
+bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
+bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
+bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
+bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
+bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
+bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
 bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
 bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
@@ -102,7 +110,3 @@ tune: $(LIBNAME)
 	$(LTLINK) $(LDFLAGS) -o etc/tune etc/tune.o $(LIBNAME)
 	cd etc/; /bin/sh tune_it.sh; cd ..
 	$(MAKE) -f makefile.shared
-
-
-
-
diff --git a/makefile.unix b/makefile.unix
index 2831040..a5559d0 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -30,24 +30,32 @@ LTM_LDFLAGS = $(LDFLAGS)
 #Library to be created (this makefile builds only static library)
 LIBMAIN_S = libtommath.a
 
-OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o bn_mp_addmod.o \
-bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \
-bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \
-bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \
-bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o bn_mp_expt_d_ex.o bn_mp_exptmod.o \
-bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_int.o \
-bn_mp_get_long.o bn_mp_get_long_long.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \
-bn_mp_init_copy.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_set_int.o bn_mp_init_size.o \
-bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \
-bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \
-bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o \
-bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o bn_mp_or.o bn_mp_prime_fermat.o \
-bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o bn_mp_prime_is_prime.o \
-bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
-bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
-bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
-bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \
-bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \
+OBJECTS=bn_conversion.o bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp_add_d.o \
+bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o \
+bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
+bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
+bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
+bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
+bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
+bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
+bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
+bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
+bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
+bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
+bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
+bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \
+bn_mp_set.o bn_mp_set_double.o bn_mp_shrink.o bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o \
+bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o \
+bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \
+bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \
+bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \
+bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \
+bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \
+bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \
+bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \
 bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \
 bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \
@@ -102,4 +110,3 @@ install: $(LIBMAIN_S)
 	@cp $(LIBMAIN_S) $(DESTDIR)$(LIBPATH)/
 	@cp $(HEADERS_PUB) $(DESTDIR)$(INCPATH)/
 	@sed -e 's,^prefix=.*,prefix=$(PREFIX),' -e 's,^Version:.*,Version: $(VERSION),' libtommath.pc.in > $(DESTDIR)$(LIBPATH)/pkgconfig/libtommath.pc
-
diff --git a/tommath.h b/tommath.h
index 2349306..b74ba1f 100644
--- a/tommath.h
+++ b/tommath.h
@@ -270,38 +270,53 @@ mp_bool mp_isodd(const mp_int *a) MP_WUR;
 /* set to zero */
 void mp_zero(mp_int *a);
 
-/* set to a digit */
-void mp_set(mp_int *a, mp_digit b);
-
-/* set a double */
+/* get and set doubles */
+double mp_get_double(const mp_int *a) MP_WUR;
 mp_err mp_set_double(mp_int *a, double b) MP_WUR;
 
-/* set a 32-bit const */
-/* TODO void - never fails */ mp_err mp_set_int(mp_int *a, unsigned long b);
+/* get integer, set integer and init with integer (int32_t) */
+int32_t mp_get_sint(const mp_int *a) MP_WUR;
+void mp_set_sint(mp_int *a, int32_t b);
+mp_err mp_init_sint(mp_int *a, int32_t b) MP_WUR;
 
-/* set a platform dependent unsigned long value */
-/* TODO void - never fails */ mp_err mp_set_long(mp_int *a, unsigned long b);
+/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint32_t) */
+#define mp_get_uint(a) ((uint32_t)mp_get_sint(a))
+void mp_set_uint(mp_int *a, uint32_t b);
+mp_err mp_init_uint(mp_int *a, uint32_t b) MP_WUR;
 
-/* set a platform dependent unsigned long long value */
-/* TODO void - never fails */ mp_err mp_set_long_long(mp_int *a, unsigned long long b);
+/* get integer, set integer and init with integer (int64_t) */
+int64_t mp_get_sint64(const mp_int *a) MP_WUR;
+void mp_set_sint64(mp_int *a, int64_t b);
 
-/* get a double */
-double mp_get_double(const mp_int *a) MP_WUR;
+/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint64_t) */
+#define mp_get_uint64(a) ((uint64_t)mp_get_sint64(a))
+void mp_set_uint64(mp_int *a, uint64_t b);
 
-/* get a 32-bit value */
-unsigned long mp_get_int(const mp_int *a) MP_WUR;
+/* get magnitude */
+uint32_t mp_get_mag(const mp_int *a) MP_WUR;
+uint64_t mp_get_mag64(const mp_int *a) MP_WUR;
 
-/* get a platform dependent unsigned long value */
-unsigned long mp_get_long(const mp_int *a) MP_WUR;
+/* get integer, set integer (long) */
+#define mp_get_slong(a)        (sizeof (long) == 8 ? (long)mp_get_sint64(a) : (long)mp_get_sint(a))
+#define mp_set_llong(a, b)     (sizeof (long) == 8 ? mp_set_sint64((a), (b)) : mp_set_sint((a), (int32_t)(b)))
 
-/* get a platform dependent unsigned long long value */
-unsigned long long mp_get_long_long(const mp_int *a) MP_WUR;
+/* get integer, set integer (unsigned long) */
+#define mp_get_ulong(a)       (sizeof (long) == 8 ? (unsigned long)mp_get_uint64(a) : (unsigned long)mp_get_uint(a))
+#define mp_get_maglong(a)     (sizeof (long) == 8 ? (unsigned long)mp_get_mag64(a) : (unsigned long)mp_get_mag(a))
+#define mp_set_ulong(a, b)    (sizeof (long) == 8 ? mp_set_uint64((a), (b)) : mp_set_uint((a), (uint32_t)(b)))
 
-/* initialize and set a digit */
+/* set to single unsigned digit, only 8 bit guaranteed */
+void mp_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_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;
+/* get integer, set integer and init with integer (deprecated) */
+MP_DEPRECATED(mp_get_mag/mp_get_uint) unsigned long mp_get_int(const mp_int *a) MP_WUR;
+MP_DEPRECATED(mp_get_magl/mp_get_ulong) unsigned long mp_get_long(const mp_int *a) MP_WUR;
+MP_DEPRECATED(mp_get_mag64/mp_get_uint64) unsigned long long mp_get_long_long(const mp_int *a) MP_WUR;
+MP_DEPRECATED(mp_set_uint) mp_err mp_set_int(mp_int *a, unsigned long b);
+MP_DEPRECATED(mp_set_ulong) mp_err mp_set_long(mp_int *a, unsigned long b);
+MP_DEPRECATED(mp_set_uint64) mp_err mp_set_long_long(mp_int *a, unsigned long long b);
+MP_DEPRECATED(mp_init_uint) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;
 
 /* copy, b = a */
 mp_err mp_copy(const mp_int *a, mp_int *b) MP_WUR;
diff --git a/tommath_class.h b/tommath_class.h
index b72f545..ce74471 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -10,6 +10,7 @@
 #endif
 #define LTM1
 #if defined(LTM_ALL)
+#   define BN_CONVERSION_C
 #   define BN_CUTOFFS_C
 #   define BN_DEPRECATED_C
 #   define BN_MP_2EXPT_C
@@ -48,9 +49,6 @@
 #   define BN_MP_FWRITE_C
 #   define BN_MP_GCD_C
 #   define BN_MP_GET_DOUBLE_C
-#   define BN_MP_GET_INT_C
-#   define BN_MP_GET_LONG_C
-#   define BN_MP_GET_LONG_LONG_C
 #   define BN_MP_GROW_C
 #   define BN_MP_ILOGB_C
 #   define BN_MP_IMPORT_C
@@ -59,7 +57,6 @@
 #   define BN_MP_INIT_COPY_C
 #   define BN_MP_INIT_MULTI_C
 #   define BN_MP_INIT_SET_C
-#   define BN_MP_INIT_SET_INT_C
 #   define BN_MP_INIT_SIZE_C
 #   define BN_MP_INVMOD_C
 #   define BN_MP_IS_SQUARE_C
@@ -109,9 +106,6 @@
 #   define BN_MP_RSHD_C
 #   define BN_MP_SET_C
 #   define BN_MP_SET_DOUBLE_C
-#   define BN_MP_SET_INT_C
-#   define BN_MP_SET_LONG_C
-#   define BN_MP_SET_LONG_LONG_C
 #   define BN_MP_SHRINK_C
 #   define BN_MP_SIGNED_BIN_SIZE_C
 #   define BN_MP_SIGNED_RSH_C
@@ -155,6 +149,20 @@
 #   define BN_S_MP_TOOM_MUL_C
 #   define BN_S_MP_TOOM_SQR_C
 #endif
+#if defined(BN_CONVERSION_C)
+#   define BN_MP_INIT_C
+#   define BN_MP_SET_UINT_C
+#   define BN_MP_SET_UINT64_C
+#   define BN_MP_SET_SINT_C
+#   define BN_MP_SET_SINT64_C
+#   define BN_MP_GET_SINT_C
+#   define BN_MP_GET_SINT64_C
+#   define BN_MP_GET_MAG_C
+#   define BN_MP_GET_MAG64_C
+#   define BN_MP_INIT_UINT_C
+#   define BN_MP_INIT_SINT_C
+#endif
+
 #if defined(BN_CUTOFFS_C)
 #endif
 
@@ -202,6 +210,18 @@
 #   define BN_MP_XOR_C
 #   define BN_MP_TC_DIV_2D_C
 #   define BN_MP_SIGNED_RSH_C
+#   define BN_MP_INIT_SET_INT_C
+#   define BN_MP_INIT_UINT_C
+#   define BN_MP_SET_INT_C
+#   define BN_MP_SET_UINT_C
+#   define BN_MP_SET_LONG_C
+#   define BN_MP_SET_UINT64_C
+#   define BN_MP_SET_LONG_LONG_C
+#   define BN_MP_GET_INT_C
+#   define BN_MP_GET_MAG_C
+#   define BN_MP_GET_LONG_C
+#   define BN_MP_GET_MAG64_C
+#   define BN_MP_GET_LONG_LONG_C
 #endif
 
 #if defined(BN_MP_2EXPT_C)
@@ -423,21 +443,11 @@
 #if defined(BN_MP_GET_DOUBLE_C)
 #endif
 
-#if defined(BN_MP_GET_INT_C)
-#   define BN_MP_GET_LONG_C
-#endif
-
-#if defined(BN_MP_GET_LONG_C)
-#endif
-
-#if defined(BN_MP_GET_LONG_LONG_C)
-#endif
-
 #if defined(BN_MP_GROW_C)
 #endif
 
 #if defined(BN_MP_ILOGB_C)
-#   define BN_MP_SET_INT_C
+#   define BN_MP_SET_UINT_C
 #   define BN_MP_COUNT_BITS_C
 #   define BN_MP_SET_C
 #   define BN_MP_CMP_D_C
@@ -483,11 +493,6 @@
 #   define BN_MP_SET_C
 #endif
 
-#if defined(BN_MP_INIT_SET_INT_C)
-#   define BN_MP_INIT_C
-#   define BN_MP_SET_INT_C
-#endif
-
 #if defined(BN_MP_INIT_SIZE_C)
 #endif
 
@@ -499,9 +504,9 @@
 
 #if defined(BN_MP_IS_SQUARE_C)
 #   define BN_MP_MOD_D_C
-#   define BN_MP_INIT_SET_INT_C
+#   define BN_MP_INIT_UINT_C
 #   define BN_MP_MOD_C
-#   define BN_MP_GET_INT_C
+#   define BN_MP_GET_UINT_C
 #   define BN_MP_SQRT_C
 #   define BN_MP_SQR_C
 #   define BN_MP_CMP_MAG_C
@@ -648,7 +653,7 @@
 
 #if defined(BN_MP_PRIME_FROBENIUS_UNDERWOOD_C)
 #   define BN_MP_INIT_MULTI_C
-#   define BN_MP_SET_LONG_C
+#   define BN_MP_SET_UINT_C
 #   define BN_MP_SQR_C
 #   define BN_MP_SUB_D_C
 #   define BN_MP_KRONECKER_C
@@ -730,10 +735,11 @@
 #if defined(BN_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C)
 #   define BN_S_MP_MUL_SI_C
 #   define BN_MP_INIT_C
-#   define BN_MP_SET_LONG_C
+#   define BN_MP_SET_SINT_C
 #   define BN_MP_MUL_C
 #   define BN_MP_CLEAR_C
 #   define BN_MP_INIT_MULTI_C
+#   define BN_MP_SET_UINT_C
 #   define BN_MP_GCD_C
 #   define BN_MP_CMP_D_C
 #   define BN_MP_CMP_C
@@ -861,25 +867,14 @@
 #endif
 
 #if defined(BN_MP_SET_C)
-#   define BN_MP_ZERO_C
 #endif
 
 #if defined(BN_MP_SET_DOUBLE_C)
-#   define BN_MP_SET_LONG_LONG_C
+#   define BN_MP_SET_UINT64_C
 #   define BN_MP_DIV_2D_C
 #   define BN_MP_MUL_2D_C
 #endif
 
-#if defined(BN_MP_SET_INT_C)
-#   define BN_MP_SET_LONG_C
-#endif
-
-#if defined(BN_MP_SET_LONG_C)
-#endif
-
-#if defined(BN_MP_SET_LONG_LONG_C)
-#endif
-
 #if defined(BN_MP_SHRINK_C)
 #endif
 
@@ -930,7 +925,7 @@
 #   define BN_MP_EXPTMOD_C
 #   define BN_MP_COPY_C
 #   define BN_MP_SUB_D_C
-#   define BN_MP_SET_INT_C
+#   define BN_MP_SET_UINT_C
 #   define BN_MP_SQRMOD_C
 #   define BN_MP_MULMOD_C
 #   define BN_MP_SET_C
diff --git a/tommath_private.h b/tommath_private.h
index 729392e..4df0a32 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -203,26 +203,6 @@ extern MP_PRIVATE const char *const mp_s_rmap;
 extern MP_PRIVATE const uint8_t mp_s_rmap_reverse[];
 extern MP_PRIVATE const size_t mp_s_rmap_reverse_sz;
 
-/* Fancy macro to set an MPI from another type.
- * There are several things assumed:
- *  x is the counter
- *  a is the pointer to the MPI
- *  b is the original value that should be set in the MPI.
- */
-#define MP_SET_XLONG(func_name, type)                    \
-mp_err func_name (mp_int * a, type b)                    \
-{                                                        \
-   int x = 0;                                            \
-   mp_zero(a);                                           \
-   while (b != 0u) {                                     \
-      a->dp[x++] = ((mp_digit)b & MP_MASK);              \
-      if (MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) { break; } \
-      b >>= ((MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
-   }                                                     \
-   a->used = x;                                          \
-   return MP_OKAY;                                       \
-}
-
 /* deprecated functions */
 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,