Commit 9074c7792e3c66462fec82dce875820ee9210bf1

Steffen Jaeckel 2019-09-03T15:07:53

Merge pull request #337 from libtom/fixup-332 Fixup 332

diff --git a/bn_mp_to_radix.c b/bn_mp_to_radix.c
index d6e8fcc..6e99777 100644
--- a/bn_mp_to_radix.c
+++ b/bn_mp_to_radix.c
@@ -1,5 +1,5 @@
 #include "tommath_private.h"
-#ifdef BN_MP_TORADIX_N_C
+#ifdef BN_MP_TO_RADIX_C
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
diff --git a/demo/timing.c b/demo/timing.c
index 5db905b..5ce576e 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -22,7 +22,7 @@ static void ndraw(mp_int *a, const char *name)
    char buf[4096];
 
    printf("%s: ", name);
-   mp_toradix(a, buf, 64);
+   mp_to_radix(a, buf, sizeof(buf), 64);
    printf("%s\n", buf);
 }
 
diff --git a/doc/bn.tex b/doc/bn.tex
index a13aee5..e1fa1e5 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -2100,18 +2100,16 @@ The random number generated with these two functions is cryptographically secure
 \subsection{To ASCII}
 \index{mp\_toradix}
 \begin{alltt}
-int mp_toradix (mp_int * a, char *str, int radix);
+int mp_to_radix (mp_int * a, char *str, size_t maxlen, int radix);
 \end{alltt}
-This still store $a$ in ``str'' as a base-``radix'' string of ASCII chars.  This function appends a NUL character
-to terminate the string.  Valid values of ``radix'' line in the range $[2, 64]$.  To determine the size (exact) required
-by the conversion before storing any data use the following function.
+This stores $a$ in ``str'' of maximum length ``maxlen'' as a base-``radix'' string of ASCII chars.
+This function appends a NUL character to terminate the string.
+Valid values of ``radix'' line in the range $[2, 64]$.
 
-\index{mp\_toradix\_n}
-\begin{alltt}
-int mp_toradix_n (mp_int * a, char *str, int radix, int maxlen);
-\end{alltt}
+If ``str'' is not big enough to hold $a$, ``str' will be filled with the least-significant digits
+of length ``maxlen-1'', then ``str' will be NUL terminated and the error \texttt{MP\_VAL} is returned.
 
-Like \texttt{mp\_toradix} but stores upto maxlen-1 chars and always a NULL byte.
+To determine the size (exact) required by the conversion before storing any data use the following function.
 
 \index{mp\_radix\_size}
 \begin{alltt}
@@ -2374,27 +2372,27 @@ Other macros which are either shortcuts to normal functions or just other names 
 
 \subsection{Shortcuts}
 
-\index{mp\_tobinary}
+\index{mp\_to\_binary}
 \begin{alltt}
-#define mp_tobinary(M, S) mp_toradix((M), (S), 2)
+#define mp_to_binary(M, S, N)  mp_to_radix((M), (S), (N), 2)
 \end{alltt}
 
 
-\index{mp\_tooctal}
+\index{mp\_to\_octal}
 \begin{alltt}
-#define mp_tooctal(M, S) mp_toradix((M), (S), 8)
+#define mp_to_octal(M, S, N)   mp_to_radix((M), (S), (N), 8)
 \end{alltt}
 
 
-\index{mp\_todecimal}
+\index{mp\_to\_decimal}
 \begin{alltt}
-#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
+#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), 10)
 \end{alltt}
 
 
-\index{mp\_tohex}
+\index{mp\_to\_hex}
 \begin{alltt}
-#define mp_tohex(M, S)     mp_toradix((M), (S), 16)
+#define mp_to_hex(M, S, N)     mp_to_radix((M), (S), (N), 16)
 \end{alltt}
 
 
diff --git a/etc/2kprime.c b/etc/2kprime.c
index a075e18..55cb5f4 100644
--- a/etc/2kprime.c
+++ b/etc/2kprime.c
@@ -69,7 +69,7 @@ top:
             goto top;
          }
 
-         mp_toradix(&q, buf, 10);
+         mp_to_decimal(&q, buf, sizeof(buf));
          printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
          fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
          fflush(out);
diff --git a/etc/drprime.c b/etc/drprime.c
index 1714e68..146455e 100644
--- a/etc/drprime.c
+++ b/etc/drprime.c
@@ -50,7 +50,7 @@ top:
             sizes[x] += 1;
             goto top;
          } else {
-            mp_toradix(&a, buf, 10);
+            mp_to_decimal(&a, buf, sizeof(buf));
             printf("\n\np == %s\n\n", buf);
             fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf);
             fflush(out);
diff --git a/etc/pprime.c b/etc/pprime.c
index 4c60331..706aec8 100644
--- a/etc/pprime.c
+++ b/etc/pprime.c
@@ -331,11 +331,11 @@ top:
       {
          char buf[4096];
 
-         mp_toradix(&n, buf, 10);
+         mp_to_decimal(&n, buf, sizeof(buf));
          printf("Certificate of primality for:\n%s\n\n", buf);
-         mp_toradix(&a, buf, 10);
+         mp_to_decimal(&a, buf, sizeof(buf));
          printf("A == \n%s\n\n", buf);
-         mp_toradix(&b, buf, 10);
+         mp_to_decimal(&b, buf, sizeof(buf));
          printf("B == \n%s\n\nG == %lu\n", buf, bases[ii]);
          printf("----------------------------------------------------------------\n");
       }
@@ -400,9 +400,9 @@ int main(void)
 
    printf("\n\nTook %d ticks, %d bits\n", t1, mp_count_bits(&p));
 
-   mp_toradix(&p, buf, 10);
+   mp_to_decimal(&p, buf, sizeof(buf));
    printf("P == %s\n", buf);
-   mp_toradix(&q, buf, 10);
+   mp_to_decimal(&q, buf, sizeof(buf));
    printf("Q == %s\n", buf);
 
    return 0;