Commit 58fb93fd790974cedb670f115b8f05c715eca57b

Daniel Mendler 2019-10-24T18:14:18

cleanup prime rand function

diff --git a/mp_prime_rand.c b/mp_prime_rand.c
index 2ee8e74..cc19fa2 100644
--- a/mp_prime_rand.c
+++ b/mp_prime_rand.c
@@ -18,7 +18,7 @@
  */
 
 /* This is possibly the mother of all prime generation functions, muahahahahaha! */
-mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_callback cb, void *dat)
+mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
 {
    unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
    int bsize, maskOR_msb_offset;
@@ -62,9 +62,8 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_call
 
    do {
       /* read the bytes */
-      if (cb(tmp, bsize, dat) != bsize) {
-         err = MP_VAL;
-         goto error;
+      if ((err = s_mp_rand_source(tmp, (size_t)bsize)) != MP_OKAY) {
+         goto LBL_ERR;
       }
 
       /* work over the MSbyte */
@@ -78,12 +77,12 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_call
       /* read it in */
       /* 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;
+         goto LBL_ERR;
       }
 
       /* is it prime? */
       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
-         goto error;
+         goto LBL_ERR;
       }
       if (!res) {
          continue;
@@ -92,15 +91,15 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_call
       if ((flags & MP_PRIME_SAFE) != 0) {
          /* see if (a-1)/2 is prime */
          if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
-            goto error;
+            goto LBL_ERR;
          }
          if ((err = mp_div_2(a, a)) != MP_OKAY) {
-            goto error;
+            goto LBL_ERR;
          }
 
          /* is it prime? */
          if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
-            goto error;
+            goto LBL_ERR;
          }
       }
    } while (!res);
@@ -108,34 +107,17 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_call
    if ((flags & MP_PRIME_SAFE) != 0) {
       /* restore a to the original value */
       if ((err = mp_mul_2(a, a)) != MP_OKAY) {
-         goto error;
+         goto LBL_ERR;
       }
       if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) {
-         goto error;
+         goto LBL_ERR;
       }
    }
 
    err = MP_OKAY;
-error:
+LBL_ERR:
    MP_FREE_BUFFER(tmp, (size_t)bsize);
    return err;
 }
 
-static int s_mp_rand_cb(unsigned char *dst, int len, void *dat)
-{
-   (void)dat;
-   if (len <= 0) {
-      return len;
-   }
-   if (s_mp_rand_source(dst, (size_t)len) != MP_OKAY) {
-      return 0;
-   }
-   return len;
-}
-
-mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
-{
-   return s_mp_prime_random_ex(a, t, size, flags, s_mp_rand_cb, NULL);
-}
-
 #endif
diff --git a/tommath_private.h b/tommath_private.h
index 07a8985..8b30c67 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -1,4 +1,3 @@
-
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
@@ -203,14 +202,10 @@ MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_dig
 MP_PRIVATE mp_err s_mp_exptmod_fast(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_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;
-typedef int mp_prime_callback(unsigned char *dst, int len, void *dat);
-MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_callback cb, void *dat);
 MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
 MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n);
 MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c);
 MP_PRIVATE uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base);
-
-
 MP_PRIVATE mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);
 MP_PRIVATE mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
 MP_PRIVATE mp_err s_mp_div_small(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);