Skip to content

Commit 0971e05

Browse files
authored
Merge pull request #242 from libtom/random-prime
deprecate mp_prime_random_ex in favor of mp_prime_rand
2 parents d4c5fbf + 0669e92 commit 0971e05

File tree

14 files changed

+60
-91
lines changed

14 files changed

+60
-91
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "tommath_private.h"
2-
#ifdef BN_MP_PRIME_RANDOM_EX_C
2+
#ifdef BN_MP_PRIME_RAND_C
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

@@ -18,7 +18,7 @@
1818
*/
1919

2020
/* This is possibly the mother of all prime generation functions, muahahahahaha! */
21-
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_callback cb, void *dat)
21+
static int s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
2222
{
2323
unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
2424
int res, err, bsize, maskOR_msb_offset;
@@ -118,5 +118,26 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_callback
118118
return err;
119119
}
120120

121+
static int s_rand_cb(unsigned char *dst, int len, void *dat)
122+
{
123+
(void)dat;
124+
if (len <= 0) {
125+
return len;
126+
}
127+
if (s_rand_source(dst, (size_t)len) != MP_OKAY) {
128+
return 0;
129+
}
130+
return len;
131+
}
132+
133+
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
134+
{
135+
return s_mp_prime_random_ex(a, t, size, flags, cb, dat);
136+
}
137+
138+
int mp_prime_rand(mp_int *a, int t, int size, int flags)
139+
{
140+
return s_mp_prime_random_ex(a, t, size, flags, s_rand_cb, NULL);
141+
}
121142

122143
#endif

bn_mp_rand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static int s_mp_rand_source_platform(void *p, size_t n)
161161
#endif
162162
}
163163

164-
static int (*s_rand_source)(void *, size_t) = s_mp_rand_source_platform;
164+
int (*s_rand_source)(void *, size_t) = s_mp_rand_source_platform;
165165

166166
void mp_rand_source(int (*get)(void *out, size_t size))
167167
{

callgraph.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9981,7 +9981,7 @@ BN_MP_PRIME_NEXT_PRIME_C
99819981
BN_MP_PRIME_RABIN_MILLER_TRIALS_C
99829982

99839983

9984-
BN_MP_PRIME_RANDOM_EX_C
9984+
BN_MP_PRIME_RAND_C
99859985
+--->BN_MP_READ_UNSIGNED_BIN_C
99869986
| +--->BN_MP_GROW_C
99879987
| +--->BN_MP_ZERO_C

demo/test.c

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -782,32 +782,7 @@ static int test_mp_sqrtmod_prime(void)
782782
return EXIT_FAILURE;
783783
}
784784

785-
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
786-
static FILE *fd_urandom = 0;
787-
#endif
788-
789-
static int myrng(unsigned char *dst, int len, void *dat)
790-
{
791-
int x;
792-
(void)dat;
793-
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
794-
if (!fd_urandom) {
795-
fprintf(stderr, "\nno /dev/urandom\n");
796-
} else {
797-
return fread(dst, 1uL, len, fd_urandom);
798-
}
799-
#endif
800-
for (x = 0; x < len;) {
801-
unsigned int r = (unsigned int)rand();
802-
do {
803-
dst[x++] = r & 0xFFu;
804-
r >>= 8;
805-
} while ((r != 0u) && (x < len));
806-
}
807-
return len;
808-
}
809-
810-
static int test_mp_prime_random_ex(void)
785+
static int test_mp_prime_rand(void)
811786
{
812787
int ix, err;
813788

@@ -820,9 +795,7 @@ static int test_mp_prime_random_ex(void)
820795
for (ix = 10; ix < 128; ix++) {
821796
printf("Testing (not safe-prime): %9d bits \r", ix);
822797
fflush(stdout);
823-
err = mp_prime_random_ex(&a, 8, ix,
824-
(rand() & 1) ? 0 : MP_PRIME_2MSB_ON, myrng,
825-
NULL);
798+
err = mp_prime_rand(&a, 8, ix, (rand() & 1) ? 0 : MP_PRIME_2MSB_ON);
826799
if (err != MP_OKAY) {
827800
printf("\nfailed with error: %s\n", mp_error_to_string(err));
828801
goto LBL_ERR;
@@ -883,9 +856,7 @@ static int test_mp_prime_is_prime(void)
883856
for (ix = 16; ix < 128; ix++) {
884857
printf("Testing ( safe-prime): %9d bits \r", ix);
885858
fflush(stdout);
886-
err = mp_prime_random_ex(
887-
&a, 8, ix, ((rand() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE,
888-
myrng, NULL);
859+
err = mp_prime_rand(&a, 8, ix, ((rand() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE);
889860
if (err != MP_OKAY) {
890861
printf("\nfailed with error: %s\n", mp_error_to_string(err));
891862
goto LBL_ERR;
@@ -1866,7 +1837,7 @@ int unit_tests(int argc, char **argv)
18661837
T(mp_kronecker),
18671838
T(mp_montgomery_reduce),
18681839
T(mp_prime_is_prime),
1869-
T(mp_prime_random_ex),
1840+
T(mp_prime_rand),
18701841
T(mp_rand),
18711842
T(mp_read_radix),
18721843
T(mp_reduce_2k),
@@ -1888,13 +1859,6 @@ int unit_tests(int argc, char **argv)
18881859
unsigned long i;
18891860
int res = EXIT_SUCCESS, j;
18901861

1891-
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
1892-
fd_urandom = fopen("/dev/urandom", "r");
1893-
if (!fd_urandom) {
1894-
fprintf(stderr, "\ncould not open /dev/urandom\n");
1895-
}
1896-
#endif
1897-
18981862
for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) {
18991863
if (argc > 1) {
19001864
for (j = 1; j < argc; ++j) {
@@ -1913,10 +1877,5 @@ int unit_tests(int argc, char **argv)
19131877
printf("\n\n");
19141878
}
19151879

1916-
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
1917-
if (fd_urandom) {
1918-
fclose(fd_urandom);
1919-
}
1920-
#endif
19211880
return res;
19221881
}

doc/bn.tex

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ \subsubsection{OpenBSD}
173173
cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo...
174174
cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo...
175175
cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo...
176-
libtool --mode=link --tag=CC cc bn_error.lo bn_s_mp_invmod_fast.lo bn_fast_mp_mo
176+
libtool --mode=link --tag=CC cc bn_error.lo bn_s_mp_invmod_fast.lo bn_fast_mp_mo
177177
libtool: link: cc bn_error.lo bn_s_mp_invmod_fast.lo bn_s_mp_montgomery_reduce_fast0
178178
bn_error.lo: file not recognized: File format not recognized
179179
cc: error: linker command failed with exit code 1 (use -v to see invocation)
@@ -187,7 +187,7 @@ \subsubsection{OpenBSD}
187187
\end{alltt}
188188
At this time two versions of \texttt{libtool} are installed and both are named \texttt{libtool}, unfortunately but GNU \texttt{libtool} has been placed in \texttt{/usr/local/bin/} and the native version in \texttt{/usr/bin/}. The path might be different in other versions of OpenBSD but both programms differ in the output of \texttt{libtool --version}
189189
\begin{alltt}
190-
$ /usr/local/bin/libtool --version
190+
$ /usr/local/bin/libtool --version
191191
libtool (GNU libtool) 2.4.2
192192
Written by Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
193193
@@ -2076,38 +2076,21 @@ \section{Next Prime}
20762076
want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to zero to find any next prime.
20772077
20782078
\section{Random Primes}
2079-
\index{mp\_prime\_random}
2080-
\begin{alltt}
2081-
int mp_prime_random(mp_int *a, int t, int size, int bbs,
2082-
ltm_prime_callback cb, void *dat)
2083-
\end{alltt}
2084-
This will find a prime greater than $256^{size}$ which can be ``bbs\_style'' or not depending on $bbs$ and must pass
2085-
$t$ rounds of tests but see the documentation for mp\_prime\_is\_prime for details regarding the use of the argument $t$.
2086-
The ``ltm\_prime\_callback'' is a typedef for
2087-
2079+
\index{mp\_prime\_random\_ex}
20882080
\begin{alltt}
2089-
typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
2081+
int mp_prime_rand(mp_int *a, int t,
2082+
int size, int flags);
20902083
\end{alltt}
2084+
This will generate a prime in $a$ using $t$ tests of the primality testing algorithms.
2085+
See the documentation for mp\_prime\_is\_prime for details regarding the use of the argument $t$.
2086+
The variable $size$ specifies the bit length of the prime desired.
2087+
The variable $flags$ specifies one of several options available
2088+
(see fig. \ref{fig:primeopts}) which can be OR'ed together.
20912089
2092-
Which is a function that must read $len$ bytes (and return the amount stored) into $dst$. The $dat$ variable is simply
2093-
copied from the original input. It can be used to pass RNG context data to the callback. The function
2094-
mp\_prime\_random() is more suitable for generating primes which must be secret (as in the case of RSA) since there
2090+
The function mp\_prime\_rand() is suitable for generating primes which must be secret (as in the case of RSA) since there
20952091
is no skew on the least significant bits.
20962092
2097-
\textit{Note:} As of v0.30 of the LibTomMath library this function has been deprecated. It is still available
2098-
but users are encouraged to use the new mp\_prime\_random\_ex() function instead.
2099-
2100-
\subsection{Extended Generation}
2101-
\index{mp\_prime\_random\_ex}
2102-
\begin{alltt}
2103-
int mp_prime_random_ex(mp_int *a, int t,
2104-
int size, int flags,
2105-
ltm_prime_callback cb, void *dat);
2106-
\end{alltt}
2107-
This will generate a prime in $a$ using $t$ tests of the primality testing algorithms. The variable $size$
2108-
specifies the bit length of the prime desired. The variable $flags$ specifies one of several options available
2109-
(see fig. \ref{fig:primeopts}) which can be OR'ed together. The callback parameters are used as in
2110-
mp\_prime\_random().
2093+
\textit{Note:} This function replaces the deprecated mp\_prime\_random and mp\_prime\_random\_ex functions.
21112094
21122095
\begin{figure}[h]
21132096
\begin{center}

libtommath_VS2008.vcproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@
641641
>
642642
</File>
643643
<File
644-
RelativePath="bn_mp_prime_random_ex.c"
644+
RelativePath="bn_mp_prime_rand.c"
645645
>
646646
</File>
647647
<File

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery
4040
bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
4141
bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
4242
bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
43-
bn_mp_prime_rabin_miller_trials.o bn_mp_prime_random_ex.o bn_mp_prime_strong_lucas_selfridge.o \
43+
bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
4444
bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
4545
bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
4646
bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \

makefile.mingw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery
4343
bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
4444
bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
4545
bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
46-
bn_mp_prime_rabin_miller_trials.o bn_mp_prime_random_ex.o bn_mp_prime_strong_lucas_selfridge.o \
46+
bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
4747
bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
4848
bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
4949
bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \

makefile.msvc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bn_mp_montgomery_calc_normalization.obj bn_mp_montgomery_reduce.obj bn_mp_montgo
3535
bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj bn_mp_n_root.obj bn_mp_n_root_ex.obj bn_mp_neg.obj \
3636
bn_mp_or.obj bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_divisible.obj \
3737
bn_mp_prime_is_prime.obj bn_mp_prime_miller_rabin.obj bn_mp_prime_next_prime.obj \
38-
bn_mp_prime_rabin_miller_trials.obj bn_mp_prime_random_ex.obj bn_mp_prime_strong_lucas_selfridge.obj \
38+
bn_mp_prime_rabin_miller_trials.obj bn_mp_prime_rand.obj bn_mp_prime_strong_lucas_selfridge.obj \
3939
bn_mp_radix_size.obj bn_mp_radix_smap.obj bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj \
4040
bn_mp_read_unsigned_bin.obj bn_mp_reduce.obj bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj bn_mp_reduce_2k_setup.obj \
4141
bn_mp_reduce_2k_setup_l.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_rshd.obj \

makefile.shared

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery
3737
bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
3838
bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_divisible.o \
3939
bn_mp_prime_is_prime.o bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o \
40-
bn_mp_prime_rabin_miller_trials.o bn_mp_prime_random_ex.o bn_mp_prime_strong_lucas_selfridge.o \
40+
bn_mp_prime_rabin_miller_trials.o bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o \
4141
bn_mp_radix_size.o bn_mp_radix_smap.o bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o \
4242
bn_mp_read_unsigned_bin.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o \
4343
bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o \

0 commit comments

Comments
 (0)