Commit 9f38acbcbffe67510c070cf5cc71018961e44a7f

Steffen Jaeckel 2019-09-02T18:52:45

Merge pull request #332 from libtom/add-mp_to_radix Add `mp_to_radix()`

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
diff --git a/bn_deprecated.c b/bn_deprecated.c
index 4beafe6..6a0453a 100644
--- a/bn_deprecated.c
+++ b/bn_deprecated.c
@@ -229,4 +229,19 @@ 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_TORADIX_N_C
+mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
+{
+   if (maxlen < 0) {
+      return MP_VAL;
+   }
+   return mp_to_radix(a, str, (size_t)maxlen, radix);
+}
+#endif
+#ifdef BN_MP_TORADIX_C
+mp_err mp_toradix(const mp_int *a, char *str, int radix)
+{
+   return mp_to_radix(a, str, SIZE_MAX, radix);
+}
+#endif
 #endif
diff --git a/bn_mp_fwrite.c b/bn_mp_fwrite.c
index 2a59755..5b4719f 100644
--- a/bn_mp_fwrite.c
+++ b/bn_mp_fwrite.c
@@ -19,7 +19,7 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
       return MP_MEM;
    }
 
-   if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
+   if ((err = mp_to_radix(a, buf, (size_t)len, radix)) != MP_OKAY) {
       MP_FREE_BUFFER(buf, (size_t)len);
       return err;
    }
diff --git a/bn_mp_to_radix.c b/bn_mp_to_radix.c
new file mode 100644
index 0000000..c75ee5b
--- /dev/null
+++ b/bn_mp_to_radix.c
@@ -0,0 +1,74 @@
+#include "tommath_private.h"
+#ifdef BN_MP_TORADIX_N_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* stores a bignum as a ASCII string in a given radix (2..64)
+ *
+ * Stores upto maxlen-1 chars and always a NULL byte
+ */
+mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
+{
+   int     digs;
+   mp_err  err;
+   mp_int  t;
+   mp_digit d;
+   char   *_s = str;
+
+   /* check range of the maxlen, radix */
+   if ((maxlen < 2) || (radix < 2) || (radix > 64)) {
+      return MP_VAL;
+   }
+
+   /* quick out if its zero */
+   if (MP_IS_ZERO(a)) {
+      *str++ = '0';
+      *str = '\0';
+      return MP_OKAY;
+   }
+
+   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
+      return err;
+   }
+
+   /* if it is negative output a - */
+   if (t.sign == MP_NEG) {
+      /* we have to reverse our digits later... but not the - sign!! */
+      ++_s;
+
+      /* store the flag and mark the number as positive */
+      *str++ = '-';
+      t.sign = MP_ZPOS;
+
+      /* subtract a char */
+      --maxlen;
+   }
+
+   digs = 0;
+   while (!MP_IS_ZERO(&t)) {
+      if (--maxlen < 1) {
+         /* no more room */
+         err = MP_VAL;
+         break;
+      }
+      if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
+         mp_clear(&t);
+         return err;
+      }
+      *str++ = mp_s_rmap[d];
+      ++digs;
+   }
+
+   /* reverse the digits of the string.  In this case _s points
+    * to the first digit [exluding the sign] of the number
+    */
+   s_mp_reverse((unsigned char *)_s, digs);
+
+   /* append a NULL so the string is properly terminated */
+   *str = '\0';
+
+   mp_clear(&t);
+   return err;
+}
+
+#endif
diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c
deleted file mode 100644
index 6322e91..0000000
--- a/bn_mp_toradix.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TORADIX_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* stores a bignum as a ASCII string in a given radix (2..64) */
-mp_err mp_toradix(const mp_int *a, char *str, int radix)
-{
-   mp_err  err;
-   int digs;
-   mp_int  t;
-   mp_digit d;
-   char   *_s = str;
-
-   /* check range of the radix */
-   if ((radix < 2) || (radix > 64)) {
-      return MP_VAL;
-   }
-
-   /* quick out if its zero */
-   if (MP_IS_ZERO(a)) {
-      *str++ = '0';
-      *str = '\0';
-      return MP_OKAY;
-   }
-
-   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
-      return err;
-   }
-
-   /* if it is negative output a - */
-   if (t.sign == MP_NEG) {
-      ++_s;
-      *str++ = '-';
-      t.sign = MP_ZPOS;
-   }
-
-   digs = 0;
-   while (!MP_IS_ZERO(&t)) {
-      if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
-         mp_clear(&t);
-         return err;
-      }
-      *str++ = mp_s_rmap[d];
-      ++digs;
-   }
-
-   /* reverse the digits of the string.  In this case _s points
-    * to the first digit [exluding the sign] of the number]
-    */
-   s_mp_reverse((unsigned char *)_s, digs);
-
-   /* append a NULL so the string is properly terminated */
-   *str = '\0';
-
-   mp_clear(&t);
-   return MP_OKAY;
-}
-
-#endif
diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c
deleted file mode 100644
index bb8af88..0000000
--- a/bn_mp_toradix_n.c
+++ /dev/null
@@ -1,73 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_TORADIX_N_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* stores a bignum as a ASCII string in a given radix (2..64)
- *
- * Stores upto maxlen-1 chars and always a NULL byte
- */
-mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
-{
-   int     digs;
-   mp_err  err;
-   mp_int  t;
-   mp_digit d;
-   char   *_s = str;
-
-   /* check range of the maxlen, radix */
-   if ((maxlen < 2) || (radix < 2) || (radix > 64)) {
-      return MP_VAL;
-   }
-
-   /* quick out if its zero */
-   if (MP_IS_ZERO(a)) {
-      *str++ = '0';
-      *str = '\0';
-      return MP_OKAY;
-   }
-
-   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
-      return err;
-   }
-
-   /* if it is negative output a - */
-   if (t.sign == MP_NEG) {
-      /* we have to reverse our digits later... but not the - sign!! */
-      ++_s;
-
-      /* store the flag and mark the number as positive */
-      *str++ = '-';
-      t.sign = MP_ZPOS;
-
-      /* subtract a char */
-      --maxlen;
-   }
-
-   digs = 0;
-   while (!MP_IS_ZERO(&t)) {
-      if (--maxlen < 1) {
-         /* no more room */
-         break;
-      }
-      if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
-         mp_clear(&t);
-         return err;
-      }
-      *str++ = mp_s_rmap[d];
-      ++digs;
-   }
-
-   /* reverse the digits of the string.  In this case _s points
-    * to the first digit [exluding the sign] of the number
-    */
-   s_mp_reverse((unsigned char *)_s, digs);
-
-   /* append a NULL so the string is properly terminated */
-   *str = '\0';
-
-   mp_clear(&t);
-   return MP_OKAY;
-}
-
-#endif
diff --git a/demo/main.c b/demo/main.c
index 4cebb12..b2e670d 100644
--- a/demo/main.c
+++ b/demo/main.c
@@ -16,9 +16,9 @@ void ndraw(mp_int *a, const char *name)
    }
 
    printf("%s: ", name);
-   mp_toradix(a, buf, 10);
+   mp_to_decimal(a, buf, (size_t) size);
    printf("%s\n", buf);
-   mp_toradix(a, buf, 16);
+   mp_to_hex(a, buf, (size_t) size);
    printf("0x%s\n", buf);
 
    free(buf);
diff --git a/demo/test.c b/demo/test.c
index 7ea3225..38a75a9 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1090,10 +1090,10 @@ static int test_mp_montgomery_reduce(void)
             if (mp_cmp(&c, &d) != MP_EQ) {
 /* *INDENT-OFF* */
                printf("d = e mod a, c = e MOD a\n");
-               mp_todecimal(&a, buf); printf("a = %s\n", buf);
-               mp_todecimal(&e, buf); printf("e = %s\n", buf);
-               mp_todecimal(&d, buf); printf("d = %s\n", buf);
-               mp_todecimal(&c, buf); printf("c = %s\n", buf);
+               mp_to_decimal(&a, buf, sizeof(buf)); printf("a = %s\n", buf);
+               mp_to_decimal(&e, buf, sizeof(buf)); printf("e = %s\n", buf);
+               mp_to_decimal(&d, buf, sizeof(buf)); printf("d = %s\n", buf);
+               mp_to_decimal(&c, buf, sizeof(buf)); printf("c = %s\n", buf);
                printf("compare no compare!\n"); goto LBL_ERR;
 /* *INDENT-ON* */
             }
@@ -1126,22 +1126,21 @@ static int test_mp_read_radix(void)
    }
 
    mp_read_radix(&a, "123456", 10);
-   mp_toradix_n(&a, buf, 10, 3);
+   mp_to_radix(&a, buf, 3, 10);
    printf("a == %s\n", buf);
-   mp_toradix_n(&a, buf, 10, 4);
+   mp_to_radix(&a, buf, 4, 10);
    printf("a == %s\n", buf);
-   mp_toradix_n(&a, buf, 10, 30);
+   mp_to_radix(&a, buf, 30, 10);
    printf("a == %s\n", buf);
 
-#if 0
-   for (;;) {
-      fgets(buf, sizeof(buf), stdin);
+   while (0) {
+      char *s = fgets(buf, sizeof(buf), stdin);
+      if (s != buf) break;
       mp_read_radix(&a, buf, 10);
       mp_prime_next_prime(&a, 5, 1);
-      mp_toradix(&a, buf, 10);
-      printf("%s, %lu\n", buf, a.dp[0] & 3);
+      mp_to_radix(&a, buf, sizeof(buf), 10);
+      printf("%s, %lu\n", buf, (unsigned long)a.dp[0] & 3uL);
    }
-#endif
 
    mp_clear_multi(&a, NULL);
    return EXIT_SUCCESS;
@@ -1311,7 +1310,9 @@ LBL_ERR:
 static int test_mp_reduce_2k_l(void)
 {
 #   if LTM_DEMO_TEST_REDUCE_2K_L
-   mp_int a, b;
+   mp_int a, b, c, d;
+   int cnt;
+   char buf[4096];
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
    }
@@ -1332,7 +1333,7 @@ static int test_mp_reduce_2k_l(void)
 #         error oops
 #      endif
 
-   mp_todecimal(&a, buf);
+   mp_to_decimal(&a, buf, sizeof(buf));
    printf("\n\np==%s\n", buf);
    /* now mp_reduce_is_2k_l() should return */
    if (mp_reduce_is_2k_l(&a) != 1) {
@@ -1355,9 +1356,9 @@ static int test_mp_reduce_2k_l(void)
       mp_mod(&c, &a, &c);
       if (mp_cmp(&b, &c) != MP_EQ) {
          printf("mp_reduce_2k_l() failed at step %d\n", cnt);
-         mp_tohex(&b, buf);
+         mp_to_hex(&b, buf, sizeof(buf));
          printf("b == %s\n", buf);
-         mp_tohex(&c, buf);
+         mp_to_hex(&c, buf, sizeof(buf));
          printf("c == %s\n", buf);
          goto LBL_ERR;
       }
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index bfe9bfd..f61123e 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -817,6 +817,10 @@
 			>
 		</File>
 		<File
+			RelativePath="bn_mp_to_radix.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_mp_to_signed_bin.c"
 			>
 		</File>
@@ -833,14 +837,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_toradix.c"
-			>
-		</File>
-		<File
-			RelativePath="bn_mp_toradix_n.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_unsigned_bin_size.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index ea4df4c..beab4fc 100644
--- a/makefile
+++ b/makefile
@@ -48,14 +48,13 @@ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.
 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_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_prime_is_divisible.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_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_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_prime_is_divisible.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 793415d..5497a1f 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -51,14 +51,13 @@ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.
 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_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_prime_is_divisible.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_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_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_prime_is_divisible.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
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
diff --git a/makefile.msvc b/makefile.msvc
index 736cc10..bddadd1 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -43,14 +43,13 @@ bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_roo
 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_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_prime_is_divisible.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_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_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_prime_is_divisible.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
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
diff --git a/makefile.shared b/makefile.shared
index f000b46..25e2fe5 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -45,14 +45,13 @@ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.
 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_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_prime_is_divisible.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_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_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_prime_is_divisible.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.unix b/makefile.unix
index 5a9767d..4bdb5d5 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -52,14 +52,13 @@ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.
 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_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_prime_is_divisible.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_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_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_prime_is_divisible.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
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB)
diff --git a/tommath.def b/tommath.def
index 8418843..1deeb94 100644
--- a/tommath.def
+++ b/tommath.def
@@ -136,12 +136,11 @@ EXPORTS
     mp_sub
     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_toradix
-    mp_toradix_n
     mp_unsigned_bin_size
     mp_xor
     mp_zero
diff --git a/tommath.h b/tommath.h
index 8efb9f9..18f6acb 100644
--- a/tommath.h
+++ b/tommath.h
@@ -699,8 +699,9 @@ 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_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
-mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
-mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
+MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
+MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
+mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) MP_WUR;
 mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
 
 #ifndef MP_NO_FILE
@@ -715,10 +716,15 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR;
 #define mp_mag_size(mp)           (MP_DEPRECATED_PRAGMA("replaced by mp_unsigned_bin_size") mp_unsigned_bin_size(mp))
 #define mp_tomag(mp, str)         (MP_DEPRECATED_PRAGMA("replaced by mp_to_unsigned_bin") mp_to_unsigned_bin((mp), (str)))
 
-#define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
-#define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
-#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
-#define mp_tohex(M, S)     mp_toradix((M), (S), 16)
+#define mp_tobinary(M, S)  (MP_DEPRECATED_PRAGMA("replaced by mp_to_binary")  mp_toradix((M), (S), 2))
+#define mp_tooctal(M, S)   (MP_DEPRECATED_PRAGMA("replaced by mp_to_octal")   mp_toradix((M), (S), 8))
+#define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10))
+#define mp_tohex(M, S)     (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex")     mp_toradix((M), (S), 16))
+
+#define mp_to_binary(M, S, N)  mp_to_radix((M), (S), (N), 2)
+#define mp_to_octal(M, S, N)   mp_to_radix((M), (S), (N), 8)
+#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), 10)
+#define mp_to_hex(M, S, N)     mp_to_radix((M), (S), (N), 16)
 
 #ifdef __cplusplus
 }
diff --git a/tommath_class.h b/tommath_class.h
index cc94e16..1bd0c0b 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -137,12 +137,11 @@
 #   define BN_MP_SUB_C
 #   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_TORADIX_C
-#   define BN_MP_TORADIX_N_C
 #   define BN_MP_UNSIGNED_BIN_SIZE_C
 #   define BN_MP_XOR_C
 #   define BN_MP_ZERO_C
@@ -221,6 +220,9 @@
 #   define BN_MP_TC_XOR_C
 #   define BN_MP_TOOM_MUL_C
 #   define BN_MP_TOOM_SQR_C
+#   define BN_MP_TORADIX_C
+#   define BN_MP_TORADIX_N_C
+#   define BN_MP_TO_RADIX_C
 #   define BN_MP_XOR_C
 #   define BN_S_MP_BALANCE_MUL_C
 #   define BN_S_MP_EXPTMOD_FAST_C
@@ -434,7 +436,7 @@
 
 #if defined(BN_MP_FWRITE_C)
 #   define BN_MP_RADIX_SIZE_C
-#   define BN_MP_TORADIX_C
+#   define BN_MP_TO_RADIX_C
 #endif
 
 #if defined(BN_MP_GCD_C)
@@ -1050,6 +1052,13 @@
 #   define BN_MP_SUB_C
 #endif
 
+#if defined(BN_MP_TO_RADIX_C)
+#   define BN_MP_CLEAR_C
+#   define BN_MP_DIV_D_C
+#   define BN_MP_INIT_COPY_C
+#   define BN_S_MP_REVERSE_C
+#endif
+
 #if defined(BN_MP_TO_SIGNED_BIN_C)
 #   define BN_MP_TO_UNSIGNED_BIN_C
 #endif
@@ -1071,20 +1080,6 @@
 #   define BN_MP_UNSIGNED_BIN_SIZE_C
 #endif
 
-#if defined(BN_MP_TORADIX_C)
-#   define BN_MP_CLEAR_C
-#   define BN_MP_DIV_D_C
-#   define BN_MP_INIT_COPY_C
-#   define BN_S_MP_REVERSE_C
-#endif
-
-#if defined(BN_MP_TORADIX_N_C)
-#   define BN_MP_CLEAR_C
-#   define BN_MP_DIV_D_C
-#   define BN_MP_INIT_COPY_C
-#   define BN_S_MP_REVERSE_C
-#endif
-
 #if defined(BN_MP_UNSIGNED_BIN_SIZE_C)
 #   define BN_MP_COUNT_BITS_C
 #endif