Commit 08eb995d014ae9056f5392074d03db8ab6aa1206

czurnieden 2019-01-13T20:21:36

implemented Steffen Jaeckels superior suggestion regarding MoarVM's problem

diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index 10ec695..17aa5a2 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -172,23 +172,13 @@ static int s_rand_digit(mp_digit *p)
 }
 
 /* makes a pseudo-random int of a given size */
-static int s_gen_random(mp_digit *r)
+int mp_rand_digit(mp_digit *r)
 {
    int ret = s_rand_digit(r);
    *r &= MP_MASK;
    return ret;
 }
 
-/* 
-   Public for legacy reasons only, do not use elsewhere!
-   There is a good reason it is not officially documented!
- */
-mp_digit gen_random_mp_digit(void) {
-   mp_digit p;
-   (void) s_gen_random(&p);
-   return p;
-}
-
 int mp_rand(mp_int *a, int digits)
 {
    int     res;
@@ -201,7 +191,7 @@ int mp_rand(mp_int *a, int digits)
 
    /* first place a random non-zero digit */
    do {
-      if (s_gen_random(&d) != MP_OKAY) {
+      if (mp_rand_digit(&d) != MP_OKAY) {
          return MP_VAL;
       }
    } while (d == 0u);
@@ -215,7 +205,7 @@ int mp_rand(mp_int *a, int digits)
          return res;
       }
 
-      if (s_gen_random(&d) != MP_OKAY) {
+      if (mp_rand_digit(&d) != MP_OKAY) {
          return MP_VAL;
       }
       if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
diff --git a/doc/bn.tex b/doc/bn.tex
index e81d039..a2b8a09 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -2003,13 +2003,19 @@ mp\_prime\_random().
 
 \chapter{Random Number Generation}
 \section{PRNG}
+\index{mp\_rand\_digit}
+\begin{alltt}
+int mp_rand_digit(mp_digit *r)
+\end{alltt}
+This function generates a random number in \texttt{r} of the size given in \texttt{r} (that is, the variable is used for in- and output) but not more than \texttt{MP_MASK} bits.
+
 \index{mp\_rand}
 \begin{alltt}
 int mp_rand(mp_int *a, int digits)
 \end{alltt}
-The function generates a random number of \texttt{digits} bits.
+This function generates a random number of \texttt{digits} bits.
 
-This random number is cryptographically secure if the source of random numbers the operating systems offers is cryptographically secure. It will use \texttt{arc4random()} if the OS is a BSD flavor, Wincrypt on Windows, and \texttt{\dev\urandom} on all operating systems that have it.
+The random number generated with these two functions is cryptographically secure if the source of random numbers the operating systems offers is cryptographically secure. It will use \texttt{arc4random()} if the OS is a BSD flavor, Wincrypt on Windows, or \texttt{\dev\urandom} on all operating systems that have it.
 
 
 \chapter{Input and Output}
diff --git a/tommath.h b/tommath.h
index 3785d0f..6469edf 100644
--- a/tommath.h
+++ b/tommath.h
@@ -277,8 +277,10 @@ int mp_cnt_lsb(const mp_int *a);
 
 /* I Love Earth! */
 
-/* makes a pseudo-random int of a given size */
+/* makes a pseudo-random mp_int of a given size */
 int mp_rand(mp_int *a, int digits);
+/* makes a pseudo-random small int of a given size */
+int mp_rand_digit(mp_digit *r);
 
 #ifdef MP_PRNG_ENABLE_LTM_RNG
 /* as last resort we will fall back to libtomcrypt's rng_get_bytes()
@@ -600,14 +602,6 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream);
 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
 #define mp_tohex(M, S)     mp_toradix((M), (S), 16)
 
-/* 
-   Public for legacy reasons only, do not use elsewhere!
-   There is a good reason it is not officially documented!
- */
-mp_digit gen_random_mp_digit(void);
-#define MP_GEN_RANDOM_MAX MP_MASK
-#define MP_GEN_RANDOM()   gen_random_mp_digit(void)
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/tommath_class.h b/tommath_class.h
index 7dfd838..944ee52 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -1,15 +1,3 @@
-/* LibTomMath, multiple-precision integer library -- Tom St Denis
- *
- * LibTomMath is a library that provides multiple-precision
- * integer arithmetic as well as number theoretic functionality.
- *
- * The library was designed directly after the MPI library by
- * Michael Fromberger but has been written from scratch with
- * additional optimizations in place.
- *
- * SPDX-License-Identifier: Unlicense
- */
-
 #if !(defined(LTM1) && defined(LTM2) && defined(LTM3))
 #if defined(LTM2)
 #   define LTM3
@@ -785,7 +773,7 @@
 #if defined(BN_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C)
 #   define BN_MP_PRIME_IS_PRIME_C
 #   define BN_MP_MUL_D_C
-#   define BN_MP_MUL_SI_C
+#   define BN_S_MP_MUL_SI_C
 #   define BN_MP_INIT_C
 #   define BN_MP_SET_LONG_C
 #   define BN_MP_MUL_C
@@ -828,6 +816,7 @@
 #endif
 
 #if defined(BN_MP_RAND_C)
+#   define BN_MP_RAND_DIGIT_C
 #   define BN_MP_ZERO_C
 #   define BN_MP_ADD_D_C
 #   define BN_MP_LSHD_C
@@ -1218,7 +1207,3 @@
 #else
 #   define LTM_LAST
 #endif
-
-/* ref:         $Format:%D$ */
-/* git commit:  $Format:%H$ */
-/* commit time: $Format:%ai$ */