Commit abdb03340255ff6759f2da2d400c50e4601156d6

czurnieden 2019-09-25T00:29:19

Refactored functions to read and write binaries and added "maxlen"

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
diff --git a/bn_deprecated.c b/bn_deprecated.c
index 6a0453a..19b0e49 100644
--- a/bn_deprecated.c
+++ b/bn_deprecated.c
@@ -229,6 +229,75 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
    return mp_root_u32(a, (uint32_t)b, c);
 }
 #endif
+
+#ifdef BN_MP_UNSIGNED_BIN_SIZE_C
+int mp_unsigned_bin_size(const mp_int *a)
+{
+   return (int)mp_ubin_size(a);
+}
+#endif
+
+#ifdef BN_MP_READ_UNSIGNED_BIN_C
+mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
+{
+   return mp_from_ubin(a, b, (size_t) c);
+}
+#endif
+
+#ifdef BN_MP_TO_UNSIGNED_BIN_C
+mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
+{
+   return mp_to_ubin(a, b, SIZE_MAX, NULL);
+}
+#endif
+
+#ifdef BN_MP_TO_UNSIGNED_BIN_N_C
+mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
+{
+   if (*outlen < (unsigned long)mp_ubin_size(a)) {
+      return MP_VAL;
+   }
+   /* TODO: or use "outlen" instead of NULL? */
+   *outlen = (unsigned long)mp_ubin_size(a);
+   return mp_to_ubin(a, b, (size_t)(*outlen), NULL);
+}
+#endif
+
+#ifdef BN_MP_SIGNED_BIN_SIZE_C
+int mp_signed_bin_size(const mp_int *a)
+{
+   return (int)mp_sbin_size(a);
+}
+#endif
+
+#ifdef BN_MP_READ_SIGNED_BIN_C
+mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
+{
+   return mp_from_sbin(a, b, (size_t) c);
+}
+#endif
+
+#ifdef BN_MP_TO_SIGNED_BIN_C
+mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
+{
+   return mp_to_sbin(a, b, SIZE_MAX, NULL);
+}
+#endif
+
+#ifdef BN_MP_TO_SIGNED_BIN_N_C
+mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
+{
+   if (*outlen < (unsigned long)mp_sbin_size(a)) {
+      return MP_VAL;
+   }
+   *outlen = (unsigned long)mp_sbin_size(a);
+   return mp_to_sbin(a, b, (size_t)(*outlen), NULL);
+}
+#endif
+
+
+
+
 #ifdef BN_MP_TORADIX_N_C
 mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
 {
diff --git a/bn_mp_from_sbin.c b/bn_mp_from_sbin.c
new file mode 100644
index 0000000..780639a
--- /dev/null
+++ b/bn_mp_from_sbin.c
@@ -0,0 +1,25 @@
+#include "tommath_private.h"
+#ifdef BN_MP_FROM_SBIN_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* read signed bin, big endian, first byte is 0==positive or 1==negative */
+mp_err mp_from_sbin(mp_int *a, const unsigned char *b, size_t c)
+{
+   mp_err err;
+
+   /* read magnitude */
+   if ((err = mp_from_ubin(a, b + 1, c - 1u)) != MP_OKAY) {
+      return err;
+   }
+
+   /* first byte is 0 for positive, non-zero for negative */
+   if (b[0] == (unsigned char)0) {
+      a->sign = MP_ZPOS;
+   } else {
+      a->sign = MP_NEG;
+   }
+
+   return MP_OKAY;
+}
+#endif
diff --git a/bn_mp_from_ubin.c b/bn_mp_from_ubin.c
new file mode 100644
index 0000000..78a4cf1
--- /dev/null
+++ b/bn_mp_from_ubin.c
@@ -0,0 +1,39 @@
+#include "tommath_private.h"
+#ifdef BN_MP_FROM_UBIN_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* reads a unsigned char array, assumes the msb is stored first [big endian] */
+mp_err mp_from_ubin(mp_int *a, const unsigned char *b, size_t size)
+{
+   mp_err err;
+
+   /* make sure there are at least two digits */
+   if (a->alloc < 2) {
+      if ((err = mp_grow(a, 2)) != MP_OKAY) {
+         return err;
+      }
+   }
+
+   /* zero the int */
+   mp_zero(a);
+
+   /* read the bytes in */
+   while (size-- > 0u) {
+      if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) {
+         return err;
+      }
+
+#ifndef MP_8BIT
+      a->dp[0] |= *b++;
+      a->used += 1;
+#else
+      a->dp[0] = (*b & MP_MASK);
+      a->dp[1] |= ((*b++ >> 7) & 1u);
+      a->used += 2;
+#endif
+   }
+   mp_clamp(a);
+   return MP_OKAY;
+}
+#endif
diff --git a/bn_mp_prime_rand.c b/bn_mp_prime_rand.c
index 1cfe514..4530e9a 100644
--- a/bn_mp_prime_rand.c
+++ b/bn_mp_prime_rand.c
@@ -76,7 +76,8 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_pr
       tmp[bsize-1]             |= maskOR_lsb;
 
       /* read it in */
-      if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) {
+      /* TODO: casting only for now until all lengths have been changed to the type "size_t"*/
+      if ((err = mp_from_ubin(a, tmp, (size_t)bsize)) != MP_OKAY) {
          goto error;
       }
 
diff --git a/bn_mp_read_signed_bin.c b/bn_mp_read_signed_bin.c
deleted file mode 100644
index 2e16399..0000000
--- a/bn_mp_read_signed_bin.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_READ_SIGNED_BIN_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* read signed bin, big endian, first byte is 0==positive or 1==negative */
-mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
-{
-   mp_err err;
-
-   /* read magnitude */
-   if ((err = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) {
-      return err;
-   }
-
-   /* first byte is 0 for positive, non-zero for negative */
-   if (b[0] == (unsigned char)0) {
-      a->sign = MP_ZPOS;
-   } else {
-      a->sign = MP_NEG;
-   }
-
-   return MP_OKAY;
-}
-#endif
diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c
deleted file mode 100644
index 17b273e..0000000
--- a/bn_mp_read_unsigned_bin.c
+++ /dev/null
@@ -1,39 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_READ_UNSIGNED_BIN_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* reads a unsigned char array, assumes the msb is stored first [big endian] */
-mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
-{
-   mp_err err;
-
-   /* make sure there are at least two digits */
-   if (a->alloc < 2) {
-      if ((err = mp_grow(a, 2)) != MP_OKAY) {
-         return err;
-      }
-   }
-
-   /* zero the int */
-   mp_zero(a);
-
-   /* read the bytes in */
-   while (c-- > 0) {
-      if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) {
-         return err;
-      }
-
-#ifndef MP_8BIT
-      a->dp[0] |= *b++;
-      a->used += 1;
-#else
-      a->dp[0] = (*b & MP_MASK);
-      a->dp[1] |= ((*b++ >> 7) & 1u);
-      a->used += 2;
-#endif
-   }
-   mp_clamp(a);
-   return MP_OKAY;
-}
-#endif
diff --git a/bn_mp_sbin_size.c b/bn_mp_sbin_size.c
new file mode 100644
index 0000000..e0993d6
--- /dev/null
+++ b/bn_mp_sbin_size.c
@@ -0,0 +1,11 @@
+#include "tommath_private.h"
+#ifdef BN_MP_SBIN_SIZE_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* get the size for an signed equivalent */
+size_t mp_sbin_size(const mp_int *a)
+{
+   return 1u + mp_ubin_size(a);
+}
+#endif
diff --git a/bn_mp_signed_bin_size.c b/bn_mp_signed_bin_size.c
deleted file mode 100644
index 1a7f49c..0000000
--- a/bn_mp_signed_bin_size.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_SIGNED_BIN_SIZE_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* get the size for an signed equivalent */
-int mp_signed_bin_size(const mp_int *a)
-{
-   return 1 + mp_unsigned_bin_size(a);
-}
-#endif
diff --git a/bn_mp_to_radix.c b/bn_mp_to_radix.c
index ca9c0fa..0af0bb7 100644
--- a/bn_mp_to_radix.c
+++ b/bn_mp_to_radix.c
@@ -9,7 +9,7 @@
  */
 mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
 {
-   int     digs;
+   size_t  digs;
    mp_err  err;
    mp_int  t;
    mp_digit d;
@@ -44,7 +44,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
       --maxlen;
    }
 
-   digs = 0;
+   digs = 0u;
    while (!MP_IS_ZERO(&t)) {
       if (--maxlen < 1u) {
          /* no more room */
diff --git a/bn_mp_to_sbin.c b/bn_mp_to_sbin.c
new file mode 100644
index 0000000..b736f98
--- /dev/null
+++ b/bn_mp_to_sbin.c
@@ -0,0 +1,23 @@
+#include "tommath_private.h"
+#ifdef BN_MP_TO_SBIN_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* store in signed [big endian] format */
+mp_err mp_to_sbin(const mp_int *a, unsigned char *b, size_t maxlen, size_t *written)
+{
+   mp_err err;
+   if (maxlen == 0u) {
+      return MP_VAL;
+   }
+
+   if ((err = mp_to_ubin(a, b + 1, maxlen - 1u, written)) != MP_OKAY) {
+      return err;
+   }
+   if (written != NULL) {
+      (*written)++;
+   }
+   b[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
+   return MP_OKAY;
+}
+#endif
diff --git a/bn_mp_to_signed_bin.c b/bn_mp_to_signed_bin.c
deleted file mode 100644
index 73f37ca..0000000
--- a/bn_mp_to_signed_bin.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TO_SIGNED_BIN_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* store in signed [big endian] format */
-mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b)
-{
-   mp_err err;
-   if ((err = mp_to_unsigned_bin(a, b + 1)) != MP_OKAY) {
-      return err;
-   }
-   b[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
-   return MP_OKAY;
-}
-#endif
diff --git a/bn_mp_to_signed_bin_n.c b/bn_mp_to_signed_bin_n.c
deleted file mode 100644
index 4c36cb5..0000000
--- a/bn_mp_to_signed_bin_n.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TO_SIGNED_BIN_N_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* store in signed [big endian] format */
-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;
-   }
-   *outlen = (unsigned long)mp_signed_bin_size(a);
-   return mp_to_signed_bin(a, b);
-}
-#endif
diff --git a/bn_mp_to_ubin.c b/bn_mp_to_ubin.c
new file mode 100644
index 0000000..80c14ab
--- /dev/null
+++ b/bn_mp_to_ubin.c
@@ -0,0 +1,51 @@
+#include "tommath_private.h"
+#ifdef BN_MP_TO_UBIN_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* store in unsigned [big endian] format */
+mp_err mp_to_ubin(const mp_int *a, unsigned char *b, size_t maxlen, size_t *written)
+{
+   size_t    x;
+   mp_err  err;
+   mp_int  t;
+
+   if (b == NULL) {
+      return MP_MEM;
+   }
+
+   if (maxlen == 0u) {
+      return MP_VAL;
+   }
+
+   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
+      return err;
+   }
+
+   x = 0u;
+   while (!MP_IS_ZERO(&t)) {
+      if (maxlen == 0u) {
+         err = MP_VAL;
+         break;
+      }
+      maxlen--;
+#ifndef MP_8BIT
+      b[x++] = (unsigned char)(t.dp[0] & 255u);
+#else
+      b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
+#endif
+      if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
+         goto LBL_ERR;
+      }
+   }
+   s_mp_reverse(b, x);
+   err = MP_OKAY;
+
+   if (written != NULL) {
+      *written = x;
+   }
+LBL_ERR:
+   mp_clear(&t);
+   return err;
+}
+#endif
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
deleted file mode 100644
index bc6372c..0000000
--- a/bn_mp_to_unsigned_bin.c
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TO_UNSIGNED_BIN_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* store in unsigned [big endian] format */
-mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
-{
-   int     x;
-   mp_err  err;
-   mp_int  t;
-
-   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
-      return err;
-   }
-
-   x = 0;
-   while (!MP_IS_ZERO(&t)) {
-#ifndef MP_8BIT
-      b[x++] = (unsigned char)(t.dp[0] & 255u);
-#else
-      b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
-#endif
-      if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
-         goto LBL_ERR;
-      }
-   }
-   s_mp_reverse(b, x);
-   err = MP_OKAY;
-
-LBL_ERR:
-   mp_clear(&t);
-   return err;
-}
-#endif
diff --git a/bn_mp_to_unsigned_bin_n.c b/bn_mp_to_unsigned_bin_n.c
deleted file mode 100644
index 3c08465..0000000
--- a/bn_mp_to_unsigned_bin_n.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TO_UNSIGNED_BIN_N_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* store in unsigned [big endian] format */
-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;
-   }
-   *outlen = (unsigned long)mp_unsigned_bin_size(a);
-   return mp_to_unsigned_bin(a, b);
-}
-#endif
diff --git a/bn_mp_ubin_size.c b/bn_mp_ubin_size.c
new file mode 100644
index 0000000..f32e190
--- /dev/null
+++ b/bn_mp_ubin_size.c
@@ -0,0 +1,13 @@
+#include "tommath_private.h"
+#ifdef BN_MP_UBIN_SIZE_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* get the size for an unsigned equivalent */
+size_t mp_ubin_size(const mp_int *a)
+{
+   int     size = mp_count_bits(a);
+   size = (size / 8) + ((((unsigned)size & 7u) != 0u) ? 1 : 0);
+   return (size_t)size;
+}
+#endif
diff --git a/bn_mp_unsigned_bin_size.c b/bn_mp_unsigned_bin_size.c
deleted file mode 100644
index 297de92..0000000
--- a/bn_mp_unsigned_bin_size.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_UNSIGNED_BIN_SIZE_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* get the size for an unsigned equivalent */
-int mp_unsigned_bin_size(const mp_int *a)
-{
-   int     size = mp_count_bits(a);
-   return (size / 8) + ((((unsigned)size & 7u) != 0u) ? 1 : 0);
-}
-#endif
diff --git a/bn_s_mp_reverse.c b/bn_s_mp_reverse.c
index 92b0f1f..623d51e 100644
--- a/bn_s_mp_reverse.c
+++ b/bn_s_mp_reverse.c
@@ -4,9 +4,9 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* reverse an array, used for radix code */
-void s_mp_reverse(unsigned char *s, int len)
+void s_mp_reverse(unsigned char *s, size_t len)
 {
-   int     ix, iy;
+   size_t   ix, iy;
    unsigned char t;
 
    ix = 0;
diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 5e8d770..7fbd35e 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -138,10 +138,10 @@ static int mtest_opponent(void)
 
          /* test the sign/unsigned storage functions */
 
-         rr = (unsigned)mp_signed_bin_size(&c);
-         mp_to_signed_bin(&c, (unsigned char *) cmd);
+         rr = (unsigned)mp_sbin_size(&c);
+         mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
          memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
-         mp_read_signed_bin(&d, (unsigned char *) cmd, (int)rr);
+         mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr);
          if (mp_cmp(&c, &d) != MP_EQ) {
             printf("mp_signed_bin failure!\n");
             draw(&c);
@@ -149,11 +149,10 @@ static int mtest_opponent(void)
             goto LBL_ERR;
          }
 
-
-         rr = (unsigned)mp_unsigned_bin_size(&c);
-         mp_to_unsigned_bin(&c, (unsigned char *) cmd);
+         rr = (unsigned)mp_ubin_size(&c);
+         mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
          memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
-         mp_read_unsigned_bin(&d, (unsigned char *) cmd, (int)rr);
+         mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr);
          if (mp_cmp_mag(&c, &d) != MP_EQ) {
             printf("mp_unsigned_bin failure!\n");
             draw(&c);
diff --git a/demo/test.c b/demo/test.c
index 99a5fa7..cfabf37 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -2214,6 +2214,93 @@ LTM_ERR:
    return EXIT_FAILURE;
 }
 
+static int test_mp_read_write_ubin(void)
+{
+   mp_int a, b, c;
+   int err;
+   size_t size, len;
+   unsigned char *buf = NULL;
+
+   if ((err = mp_init_multi(&a, &b, &c, NULL)) != MP_OKAY) {
+      goto LTM_ERR;
+   }
+
+   if ((err = mp_rand(&a, 15)) != MP_OKAY)                   goto LTM_ERR;
+   if ((err = mp_neg(&a, &b)) != MP_OKAY)                    goto LTM_ERR;
+
+   size = mp_ubin_size(&a);
+   printf("mp_to_ubin_size  %zu\n", size);
+   buf = MP_MALLOC(sizeof(*buf) * size);
+   if (buf == NULL) {
+      fprintf(stderr, "test_read_write_binaries (u) failed to allocate %zu bytes\n",
+              sizeof(*buf) * size);
+      goto LTM_ERR;
+   }
+
+   if ((err = mp_to_ubin(&a, buf, size, &len)) != MP_OKAY)   goto LTM_ERR;
+   printf("mp_to_ubin len = %zu\n", len);
+
+   if ((err = mp_from_ubin(&c, buf, len)) != MP_OKAY)        goto LTM_ERR;
+
+   if (mp_cmp(&a, &c) != MP_EQ) {
+      fprintf(stderr, "to/from ubin cycle failed\n");
+      goto LTM_ERR;
+   }
+   free(buf);
+   mp_clear_multi(&a, &b, &c, NULL);
+   return EXIT_SUCCESS;
+LTM_ERR:
+   if (buf != NULL) {
+      free(buf);
+   }
+   mp_clear_multi(&a, &b, &c, NULL);
+   return EXIT_FAILURE;
+}
+
+static int test_mp_read_write_sbin(void)
+{
+   mp_int a, b, c;
+   int err;
+   size_t size, len;
+   unsigned char *buf = NULL;
+
+   if ((err = mp_init_multi(&a, &b, &c, NULL)) != MP_OKAY) {
+      goto LTM_ERR;
+   }
+
+   if ((err = mp_rand(&a, 15)) != MP_OKAY)                   goto LTM_ERR;
+   if ((err = mp_neg(&a, &b)) != MP_OKAY)                    goto LTM_ERR;
+
+   size = mp_sbin_size(&a);
+   printf("mp_to_sbin_size  %zu\n", size);
+   buf = MP_MALLOC(sizeof(*buf) * size);
+   if (buf == NULL) {
+      fprintf(stderr, "test_read_write_binaries (s) failed to allocate %zu bytes\n",
+              sizeof(*buf) * size);
+      goto LTM_ERR;
+   }
+
+   if ((err = mp_to_sbin(&b, buf, size, &len)) != MP_OKAY)   goto LTM_ERR;
+   printf("mp_to_sbin len = %zu\n", len);
+
+   if ((err = mp_from_sbin(&c, buf, len)) != MP_OKAY)        goto LTM_ERR;
+
+   if (mp_cmp(&b, &c) != MP_EQ) {
+      fprintf(stderr, "to/from ubin cycle failed\n");
+      goto LTM_ERR;
+   }
+
+   free(buf);
+   mp_clear_multi(&a, &b, &c, NULL);
+   return EXIT_SUCCESS;
+LTM_ERR:
+   if (buf != NULL) {
+      free(buf);
+   }
+   mp_clear_multi(&a, &b, &c, NULL);
+   return EXIT_FAILURE;
+}
+
 static int unit_tests(int argc, char **argv)
 {
    static const struct {
@@ -2250,6 +2337,8 @@ static int unit_tests(int argc, char **argv)
       T1(mp_prime_rand, MP_PRIME_RAND),
       T1(mp_rand, MP_RAND),
       T1(mp_read_radix, MP_READ_RADIX),
+      T1(mp_read_write_ubin, MP_TO_UBIN),
+      T1(mp_read_write_sbin, MP_TO_SBIN),
       T1(mp_reduce_2k, MP_REDUCE_2K),
       T1(mp_reduce_2k_l, MP_REDUCE_2K_L),
 #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559)
@@ -2263,7 +2352,7 @@ static int unit_tests(int argc, char **argv)
       T1(s_mp_karatsuba_mul, S_MP_KARATSUBA_MUL),
       T1(s_mp_karatsuba_sqr, S_MP_KARATSUBA_SQR),
       T1(s_mp_toom_mul, S_MP_TOOM_MUL),
-      T1(s_mp_toom_sqr, S_MP_TOOM_SQR),
+      T1(s_mp_toom_sqr, S_MP_TOOM_SQR)
 #undef T2
 #undef T1
    };
diff --git a/doc/bn.tex b/doc/bn.tex
index e1fa1e5..850e1ce 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -2145,41 +2145,32 @@ int mp_fread(mp_int *a, int radix, FILE *stream);
 
 Converting an mp\_int to and from binary is another keen idea.
 
-\index{mp\_unsigned\_bin\_size}
+\index{mp\_ubin\_size}
 \begin{alltt}
-int mp_unsigned_bin_size(mp_int *a);
+size_t mp_ubin_size(mp_int *a);
 \end{alltt}
 
 This will return the number of bytes (octets) required to store the unsigned copy of the integer $a$.
 
-\index{mp\_to\_unsigned\_bin}
+\index{mp\_to\_ubin}
 \begin{alltt}
-int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
+int mp_to_unsigned_bin(mp_int *a, unsigned char *b, size_t maxlen, size_t *len);
 \end{alltt}
-This will store $a$ into the buffer $b$ in big--endian format.  Fortunately this is exactly what DER (or is it ASN?)
-requires.  It does not store the sign of the integer.
+This will store $a$ into the buffer $b$ of size \texttt{maxlen} in big--endian format storing the number of bytes written in \texttt{len}.  Fortunately this is exactly what DER (or is it ASN?) requires.  It does not store the sign of the integer.
 
-\index{mp\_to\_unsigned\_bin\_n}
+\index{mp\_from\_ubin}
 \begin{alltt}
-int mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
+int mp_from_ubin(mp_int *a, unsigned char *b, size_t size);
 \end{alltt}
-Like \texttt{mp\_to\_unsigned\_bin} but checks if the value at \texttt{*outlen} is larger than or equal to the output of \texttt{mp\_unsigned\_bin\_size(a)} and sets \texttt{*outlen} to the output of \texttt{mp\_unsigned\_bin\_size(a)} or returns \texttt{MP\_VAL} if the test failed.
-
-
-\index{mp\_read\_unsigned\_bin}
-\begin{alltt}
-int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
-\end{alltt}
-This will read in an unsigned big--endian array of bytes (octets) from $b$ of length $c$ into $a$.  The resulting
-integer $a$ will always be positive.
+This will read in an unsigned big--endian array of bytes (octets) from $b$ of length \texttt{size} into $a$.  The resulting big-integer $a$ will always be positive.
 
 For those who acknowledge the existence of negative numbers (heretic!) there are ``signed'' versions of the
 previous functions.
 \index{mp\_signed\_bin\_size} \index{mp\_to\_signed\_bin} \index{mp\_read\_signed\_bin}
 \begin{alltt}
-int mp_signed_bin_size(mp_int *a);
-int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
-int mp_to_signed_bin(mp_int *a, unsigned char *b);
+int mp_sbin_size(mp_int *a);
+int mp_from_sbin(mp_int *a, unsigned char *b, size_t size);
+int mp_to_sbin(mp_int *a, unsigned char *b, size_t maxsize, size_t *len);
 \end{alltt}
 They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero
 byte depending on the sign.  If the sign is zpos (e.g. not negative) the prefix is zero, otherwise the prefix
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index f61123e..6a1a294 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -449,6 +449,14 @@
 			>
 		</File>
 		<File
+			RelativePath="bn_mp_from_sbin.c"
+			>
+		</File>
+		<File
+			RelativePath="bn_mp_from_ubin.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_mp_fwrite.c"
 			>
 		</File>
@@ -689,14 +697,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_read_signed_bin.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_read_unsigned_bin.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_reduce.c"
 			>
 		</File>
@@ -737,6 +737,10 @@
 			>
 		</File>
 		<File
+			RelativePath="bn_mp_sbin_size.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_mp_set.c"
 			>
 		</File>
@@ -781,10 +785,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_signed_bin_size.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_signed_rsh.c"
 			>
 		</File>
@@ -821,23 +821,15 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_to_signed_bin.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_to_signed_bin_n.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_to_unsigned_bin.c"
+			RelativePath="bn_mp_to_sbin.c"
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_to_unsigned_bin_n.c"
+			RelativePath="bn_mp_to_ubin.c"
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_unsigned_bin_size.c"
+			RelativePath="bn_mp_ubin_size.c"
 			>
 		</File>
 		<File
diff --git a/makefile b/makefile
index 4c6997a..a68d22c 100644
--- a/makefile
+++ b/makefile
@@ -31,25 +31,24 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm
 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_u32.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_i32.o bn_mp_get_i64.o bn_mp_get_l.o \
-bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o \
-bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \
-bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o bn_mp_or.o \
-bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \
+bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \
+bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.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_root_u32.o bn_mp_rshd.o \
-bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \
-bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.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_radix.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_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \
+bn_mp_rand.o bn_mp_read_radix.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_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \
+bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \
+bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.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_radix.o bn_mp_to_sbin.o \
+bn_mp_to_ubin.o bn_mp_ubin_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 \
diff --git a/makefile.mingw b/makefile.mingw
index e335d48..2d334d0 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -34,25 +34,24 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm
 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_u32.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_i32.o bn_mp_get_i64.o bn_mp_get_l.o \
-bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o \
-bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \
-bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o bn_mp_or.o \
-bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \
+bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \
+bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.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_root_u32.o bn_mp_rshd.o \
-bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \
-bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.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_radix.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_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \
+bn_mp_rand.o bn_mp_read_radix.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_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \
+bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \
+bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.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_radix.o bn_mp_to_sbin.o \
+bn_mp_to_ubin.o bn_mp_ubin_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 \
diff --git a/makefile.msvc b/makefile.msvc
index aeac2e7..8419b53 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -26,25 +26,24 @@ bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi.obj bn_mp_cmp.ob
 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_u32.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_i32.obj bn_mp_get_i64.obj bn_mp_get_l.obj \
-bn_mp_get_ll.obj bn_mp_get_mag_u32.obj bn_mp_get_mag_u64.obj bn_mp_get_mag_ul.obj bn_mp_get_mag_ull.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_i32.obj \
-bn_mp_init_i64.obj bn_mp_init_l.obj bn_mp_init_ll.obj bn_mp_init_multi.obj bn_mp_init_set.obj bn_mp_init_size.obj \
-bn_mp_init_u32.obj bn_mp_init_u64.obj bn_mp_init_ul.obj bn_mp_init_ull.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_neg.obj bn_mp_or.obj \
-bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_prime.obj \
+bn_mp_fread.obj bn_mp_from_sbin.obj bn_mp_from_ubin.obj bn_mp_fwrite.obj bn_mp_gcd.obj bn_mp_get_double.obj \
+bn_mp_get_i32.obj bn_mp_get_i64.obj bn_mp_get_l.obj bn_mp_get_ll.obj bn_mp_get_mag_u32.obj bn_mp_get_mag_u64.obj \
+bn_mp_get_mag_ul.obj bn_mp_get_mag_ull.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_i32.obj bn_mp_init_i64.obj bn_mp_init_l.obj bn_mp_init_ll.obj bn_mp_init_multi.obj \
+bn_mp_init_set.obj bn_mp_init_size.obj bn_mp_init_u32.obj bn_mp_init_u64.obj bn_mp_init_ul.obj bn_mp_init_ull.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_neg.obj \
+bn_mp_or.obj bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.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_root_u32.obj bn_mp_rshd.obj \
-bn_mp_set.obj bn_mp_set_double.obj bn_mp_set_i32.obj bn_mp_set_i64.obj bn_mp_set_l.obj bn_mp_set_ll.obj \
-bn_mp_set_u32.obj bn_mp_set_u64.obj bn_mp_set_ul.obj bn_mp_set_ull.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_radix.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_unsigned_bin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj bn_s_mp_add.obj \
+bn_mp_rand.obj bn_mp_read_radix.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_root_u32.obj bn_mp_rshd.obj bn_mp_sbin_size.obj bn_mp_set.obj bn_mp_set_double.obj \
+bn_mp_set_i32.obj bn_mp_set_i64.obj bn_mp_set_l.obj bn_mp_set_ll.obj bn_mp_set_u32.obj bn_mp_set_u64.obj \
+bn_mp_set_ul.obj bn_mp_set_ull.obj bn_mp_shrink.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_radix.obj bn_mp_to_sbin.obj \
+bn_mp_to_ubin.obj bn_mp_ubin_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 \
diff --git a/makefile.shared b/makefile.shared
index ae3c850..a6dc2bb 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -28,25 +28,24 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm
 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_u32.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_i32.o bn_mp_get_i64.o bn_mp_get_l.o \
-bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o \
-bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \
-bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o bn_mp_or.o \
-bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \
+bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \
+bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.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_root_u32.o bn_mp_rshd.o \
-bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \
-bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.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_radix.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_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \
+bn_mp_rand.o bn_mp_read_radix.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_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \
+bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \
+bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.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_radix.o bn_mp_to_sbin.o \
+bn_mp_to_ubin.o bn_mp_ubin_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 \
diff --git a/makefile.unix b/makefile.unix
index bd83d85..be008b0 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -35,25 +35,24 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm
 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_u32.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_i32.o bn_mp_get_i64.o bn_mp_get_l.o \
-bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o \
-bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \
-bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o bn_mp_or.o \
-bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \
+bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \
+bn_mp_get_mag_ul.o bn_mp_get_mag_ull.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_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.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_neg.o \
+bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.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_root_u32.o bn_mp_rshd.o \
-bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \
-bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.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_radix.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_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \
+bn_mp_rand.o bn_mp_read_radix.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_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \
+bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \
+bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.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_radix.o bn_mp_to_sbin.o \
+bn_mp_to_ubin.o bn_mp_ubin_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 \
diff --git a/tommath.def b/tommath.def
index 1deeb94..6ad2b89 100644
--- a/tommath.def
+++ b/tommath.def
@@ -38,6 +38,8 @@ EXPORTS
     mp_exptmod
     mp_exteuclid
     mp_fread
+    mp_from_sbin
+    mp_from_ubin
     mp_fwrite
     mp_gcd
     mp_get_double
@@ -101,8 +103,6 @@ EXPORTS
     mp_radix_size
     mp_rand
     mp_read_radix
-    mp_read_signed_bin
-    mp_read_unsigned_bin
     mp_reduce
     mp_reduce_2k
     mp_reduce_2k_l
@@ -113,6 +113,7 @@ EXPORTS
     mp_reduce_setup
     mp_root_u32
     mp_rshd
+    mp_sbin_size
     mp_set
     mp_set_double
     mp_set_i32
@@ -127,7 +128,6 @@ EXPORTS
     mp_set_ul
     mp_set_ull
     mp_shrink
-    mp_signed_bin_size
     mp_signed_rsh
     mp_sqr
     mp_sqrmod
@@ -137,10 +137,8 @@ EXPORTS
     mp_sub_d
     mp_submod
     mp_to_radix
-    mp_to_signed_bin
-    mp_to_signed_bin_n
-    mp_to_unsigned_bin
-    mp_to_unsigned_bin_n
-    mp_unsigned_bin_size
+    mp_to_sbin
+    mp_to_ubin
+    mp_ubin_size
     mp_xor
     mp_zero
diff --git a/tommath.h b/tommath.h
index 58d099f..8c67c0e 100644
--- a/tommath.h
+++ b/tommath.h
@@ -696,15 +696,24 @@ MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_i
 /* ---> radix conversion <--- */
 int mp_count_bits(const mp_int *a) MP_WUR;
 
-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;
-
-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_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR;
+MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
+MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR;
+MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
+
+MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int *a) MP_WUR;
+MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
+MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a,  unsigned char *b) MP_WUR;
+MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
+
+size_t mp_ubin_size(const mp_int *a) MP_WUR;
+mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
+mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
+
+size_t mp_sbin_size(const mp_int *a) MP_WUR;
+mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
+mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
 
 mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
 MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
diff --git a/tommath_class.h b/tommath_class.h
index 32d1b2d..2eefad7 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -45,6 +45,8 @@
 #   define BN_MP_EXPTMOD_C
 #   define BN_MP_EXTEUCLID_C
 #   define BN_MP_FREAD_C
+#   define BN_MP_FROM_SBIN_C
+#   define BN_MP_FROM_UBIN_C
 #   define BN_MP_FWRITE_C
 #   define BN_MP_GCD_C
 #   define BN_MP_GET_DOUBLE_C
@@ -105,8 +107,6 @@
 #   define BN_MP_RADIX_SMAP_C
 #   define BN_MP_RAND_C
 #   define BN_MP_READ_RADIX_C
-#   define BN_MP_READ_SIGNED_BIN_C
-#   define BN_MP_READ_UNSIGNED_BIN_C
 #   define BN_MP_REDUCE_C
 #   define BN_MP_REDUCE_2K_C
 #   define BN_MP_REDUCE_2K_L_C
@@ -117,6 +117,7 @@
 #   define BN_MP_REDUCE_SETUP_C
 #   define BN_MP_ROOT_U32_C
 #   define BN_MP_RSHD_C
+#   define BN_MP_SBIN_SIZE_C
 #   define BN_MP_SET_C
 #   define BN_MP_SET_DOUBLE_C
 #   define BN_MP_SET_I32_C
@@ -128,7 +129,6 @@
 #   define BN_MP_SET_UL_C
 #   define BN_MP_SET_ULL_C
 #   define BN_MP_SHRINK_C
-#   define BN_MP_SIGNED_BIN_SIZE_C
 #   define BN_MP_SIGNED_RSH_C
 #   define BN_MP_SQR_C
 #   define BN_MP_SQRMOD_C
@@ -138,11 +138,9 @@
 #   define BN_MP_SUB_D_C
 #   define BN_MP_SUBMOD_C
 #   define BN_MP_TO_RADIX_C
-#   define BN_MP_TO_SIGNED_BIN_C
-#   define BN_MP_TO_SIGNED_BIN_N_C
-#   define BN_MP_TO_UNSIGNED_BIN_C
-#   define BN_MP_TO_UNSIGNED_BIN_N_C
-#   define BN_MP_UNSIGNED_BIN_SIZE_C
+#   define BN_MP_TO_SBIN_C
+#   define BN_MP_TO_UBIN_C
+#   define BN_MP_UBIN_SIZE_C
 #   define BN_MP_XOR_C
 #   define BN_MP_ZERO_C
 #   define BN_PRIME_TAB_C
@@ -187,6 +185,8 @@
 #   define BN_MP_EXPT_D_C
 #   define BN_MP_EXPT_D_EX_C
 #   define BN_MP_EXPT_U32_C
+#   define BN_MP_FROM_SBIN_C
+#   define BN_MP_FROM_UBIN_C
 #   define BN_MP_GET_BIT_C
 #   define BN_MP_GET_INT_C
 #   define BN_MP_GET_LONG_C
@@ -207,12 +207,16 @@
 #   define BN_MP_PRIME_IS_DIVISIBLE_C
 #   define BN_MP_PRIME_RANDOM_EX_C
 #   define BN_MP_RAND_DIGIT_C
+#   define BN_MP_READ_SIGNED_BIN_C
+#   define BN_MP_READ_UNSIGNED_BIN_C
 #   define BN_MP_ROOT_U32_C
+#   define BN_MP_SBIN_SIZE_C
 #   define BN_MP_SET_INT_C
 #   define BN_MP_SET_LONG_C
 #   define BN_MP_SET_LONG_LONG_C
 #   define BN_MP_SET_U32_C
 #   define BN_MP_SET_U64_C
+#   define BN_MP_SIGNED_BIN_SIZE_C
 #   define BN_MP_SIGNED_RSH_C
 #   define BN_MP_TC_AND_C
 #   define BN_MP_TC_DIV_2D_C
@@ -223,6 +227,12 @@
 #   define BN_MP_TORADIX_C
 #   define BN_MP_TORADIX_N_C
 #   define BN_MP_TO_RADIX_C
+#   define BN_MP_TO_SBIN_C
+#   define BN_MP_TO_UBIN_C
+#   define BN_MP_TO_UNSIGNED_BIN_C
+#   define BN_MP_TO_UNSIGNED_BIN_N_C
+#   define BN_MP_UBIN_SIZE_C
+#   define BN_MP_UNSIGNED_BIN_SIZE_C
 #   define BN_MP_XOR_C
 #   define BN_S_MP_BALANCE_MUL_C
 #   define BN_S_MP_EXPTMOD_FAST_C
@@ -433,6 +443,17 @@
 #   define BN_MP_ZERO_C
 #endif
 
+#if defined(BN_MP_FROM_SBIN_C)
+#   define BN_MP_FROM_UBIN_C
+#endif
+
+#if defined(BN_MP_FROM_UBIN_C)
+#   define BN_MP_CLAMP_C
+#   define BN_MP_GROW_C
+#   define BN_MP_MUL_2D_C
+#   define BN_MP_ZERO_C
+#endif
+
 #if defined(BN_MP_FWRITE_C)
 #   define BN_MP_RADIX_SIZE_C
 #   define BN_MP_TO_RADIX_C
@@ -776,9 +797,9 @@
 #if defined(BN_MP_PRIME_RAND_C)
 #   define BN_MP_ADD_D_C
 #   define BN_MP_DIV_2_C
+#   define BN_MP_FROM_UBIN_C
 #   define BN_MP_MUL_2_C
 #   define BN_MP_PRIME_IS_PRIME_C
-#   define BN_MP_READ_UNSIGNED_BIN_C
 #   define BN_MP_SUB_D_C
 #   define BN_S_MP_PRIME_RANDOM_EX_C
 #   define BN_S_MP_RAND_CB_C
@@ -837,17 +858,6 @@
 #   define BN_MP_ZERO_C
 #endif
 
-#if defined(BN_MP_READ_SIGNED_BIN_C)
-#   define BN_MP_READ_UNSIGNED_BIN_C
-#endif
-
-#if defined(BN_MP_READ_UNSIGNED_BIN_C)
-#   define BN_MP_CLAMP_C
-#   define BN_MP_GROW_C
-#   define BN_MP_MUL_2D_C
-#   define BN_MP_ZERO_C
-#endif
-
 #if defined(BN_MP_REDUCE_C)
 #   define BN_MP_ADD_C
 #   define BN_MP_CLEAR_C
@@ -938,6 +948,10 @@
 #   define BN_MP_ZERO_C
 #endif
 
+#if defined(BN_MP_SBIN_SIZE_C)
+#   define BN_MP_UBIN_SIZE_C
+#endif
+
 #if defined(BN_MP_SET_C)
 #endif
 
@@ -978,10 +992,6 @@
 #if defined(BN_MP_SHRINK_C)
 #endif
 
-#if defined(BN_MP_SIGNED_BIN_SIZE_C)
-#   define BN_MP_UNSIGNED_BIN_SIZE_C
-#endif
-
 #if defined(BN_MP_SIGNED_RSH_C)
 #   define BN_MP_ADD_D_C
 #   define BN_MP_DIV_2D_C
@@ -1059,28 +1069,18 @@
 #   define BN_S_MP_REVERSE_C
 #endif
 
-#if defined(BN_MP_TO_SIGNED_BIN_C)
-#   define BN_MP_TO_UNSIGNED_BIN_C
-#endif
-
-#if defined(BN_MP_TO_SIGNED_BIN_N_C)
-#   define BN_MP_SIGNED_BIN_SIZE_C
-#   define BN_MP_TO_SIGNED_BIN_C
+#if defined(BN_MP_TO_SBIN_C)
+#   define BN_MP_TO_UBIN_C
 #endif
 
-#if defined(BN_MP_TO_UNSIGNED_BIN_C)
+#if defined(BN_MP_TO_UBIN_C)
 #   define BN_MP_CLEAR_C
 #   define BN_MP_DIV_2D_C
 #   define BN_MP_INIT_COPY_C
 #   define BN_S_MP_REVERSE_C
 #endif
 
-#if defined(BN_MP_TO_UNSIGNED_BIN_N_C)
-#   define BN_MP_TO_UNSIGNED_BIN_C
-#   define BN_MP_UNSIGNED_BIN_SIZE_C
-#endif
-
-#if defined(BN_MP_UNSIGNED_BIN_SIZE_C)
+#if defined(BN_MP_UBIN_SIZE_C)
 #   define BN_MP_COUNT_BITS_C
 #endif
 
diff --git a/tommath_private.h b/tommath_private.h
index b565042..ff6f7cf 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -203,7 +203,7 @@ MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_i
 MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
 MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
 MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat);
-MP_PRIVATE void s_mp_reverse(unsigned char *s, int len);
+MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len);
 MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
 
 /* TODO: jenkins prng is not thread safe as of now */
diff --git a/tommath_superclass.h b/tommath_superclass.h
index b007c24..d88bce9 100644
--- a/tommath_superclass.h
+++ b/tommath_superclass.h
@@ -31,13 +31,13 @@
 #   define BN_MP_RAND_C
 #   define BN_MP_REDUCE_C
 #   define BN_MP_REDUCE_2K_L_C
-#   define BN_MP_READ_SIGNED_BIN_C
+#   define BN_MP_FROM_SBIN_C
 #   define BN_MP_ROOT_U32_C
 #   define BN_MP_SET_L_C
 #   define BN_MP_SET_UL_C
-#   define BN_MP_SIGNED_BIN_SIZE_C
+#   define BN_MP_SBIN_SIZE_C
 #   define BN_MP_TO_RADIX_C
-#   define BN_MP_TO_SIGNED_BIN_C
+#   define BN_MP_TO_SBIN_C
 #   define BN_S_MP_RAND_JENKINS_C
 #   define BN_S_MP_RAND_PLATFORM_C
 #endif