cleanup prime rand function
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
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);