Skip to content

Commit 6327a69

Browse files
authored
Merge pull request #256 from libtom/rand-cleanup
Use Jenkins rand in test suite
2 parents 625c246 + f85bd31 commit 6327a69

20 files changed

+364
-294
lines changed

.travis.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ matrix:
8686
# GCC for the x64_32 architecture (32-bit longs and 32-bit pointers)
8787
# TODO: Probably not possible to run anything in x32 in Travis
8888
# but needs to be checked to be sure.
89-
- env: BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
89+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
9090
addons:
9191
apt:
9292
packages:
9393
- libc6-dev-x32
9494
- gcc-multilib
9595

9696
# GCC for the x86-64 architecture (64-bit longs and 64-bit pointers)
97-
- env: BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
97+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
9898
- env: BUILDOPTIONS='--with-cc=gcc-4.7 --with-m64 --with-valgrind'
9999
addons:
100100
apt:
@@ -112,13 +112,13 @@ matrix:
112112
- gcc-4.9
113113

114114
# clang for x86-64 architecture (64-bit longs and 64-bit pointers)
115-
- env: CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
116-
- env: BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
115+
- env: SANITIZER=1 CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
116+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
117117
addons:
118118
apt:
119119
packages:
120120
- clang-6.0
121-
- env: BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
121+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
122122
addons:
123123
apt:
124124
packages:
@@ -132,24 +132,24 @@ matrix:
132132
# GCC for the x86-64 architecture with restricted limb sizes
133133
# formerly started with the option "--with-low-mp" to testme.sh
134134
# but testing all three in one run took to long and timed out.
135-
- env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind'
136-
- env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
137-
- env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
135+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind'
136+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
137+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
138138

139139
# clang for the x86-64 architecture with restricted limb sizes
140-
- env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind'
141-
- env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
142-
- env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
140+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind'
141+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
142+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
143143

144144
# Test "autotuning", the automatic evaluation and setting of the Toom-Cook cut-offs.
145-
- env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
146-
- env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
147-
- env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
148-
- env: BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
149-
- env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
150-
- env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
151-
- env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
152-
- env: BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
145+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
146+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
147+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
148+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
149+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
150+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
151+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
152+
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
153153

154154
# GCC for the x86-64 architecture testing against a different Bigint-implementation
155155
# with 333333 different inputs.

bn_mp_prime_rand.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ static int s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_m
118118
return err;
119119
}
120120

121-
static int s_rand_cb(unsigned char *dst, int len, void *dat)
121+
static int s_mp_rand_cb(unsigned char *dst, int len, void *dat)
122122
{
123123
(void)dat;
124124
if (len <= 0) {
125125
return len;
126126
}
127-
if (s_rand_source(dst, (size_t)len) != MP_OKAY) {
127+
if (s_mp_rand_source(dst, (size_t)len) != MP_OKAY) {
128128
return 0;
129129
}
130130
return len;
@@ -137,7 +137,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_c
137137

138138
int mp_prime_rand(mp_int *a, int t, int size, int flags)
139139
{
140-
return s_mp_prime_random_ex(a, t, size, flags, s_rand_cb, NULL);
140+
return s_mp_prime_random_ex(a, t, size, flags, s_mp_rand_cb, NULL);
141141
}
142142

143143
#endif

bn_mp_rand.c

Lines changed: 5 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -3,175 +3,17 @@
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

6-
/* First the OS-specific special cases
7-
* - *BSD
8-
* - Windows
9-
*/
10-
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
11-
# define MP_ARC4RANDOM
12-
#endif
13-
14-
#if defined(_WIN32) || defined(_WIN32_WCE)
15-
#define MP_WIN_CSP
16-
17-
#ifndef _WIN32_WINNT
18-
#define _WIN32_WINNT 0x0400
19-
#endif
20-
#ifdef _WIN32_WCE
21-
#define UNDER_CE
22-
#define ARM
23-
#endif
24-
25-
#define WIN32_LEAN_AND_MEAN
26-
#include <windows.h>
27-
#include <wincrypt.h>
28-
29-
static HCRYPTPROV hProv = 0;
30-
31-
static void s_cleanup_win_csp(void)
32-
{
33-
CryptReleaseContext(hProv, 0);
34-
hProv = 0;
35-
}
36-
37-
static int s_read_win_csp(void *p, size_t n)
38-
{
39-
if (hProv == 0) {
40-
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
41-
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
42-
!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
43-
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
44-
hProv = 0;
45-
return MP_ERR;
46-
}
47-
atexit(s_cleanup_win_csp);
48-
}
49-
return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
50-
}
51-
#endif /* WIN32 */
52-
53-
#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
54-
#if __GLIBC_PREREQ(2, 25)
55-
#define MP_GETRANDOM
56-
#include <sys/random.h>
57-
#include <errno.h>
58-
59-
static int s_read_getrandom(void *p, size_t n)
60-
{
61-
char *q = (char *)p;
62-
while (n > 0) {
63-
ssize_t ret = getrandom(q, n, 0);
64-
if (ret < 0) {
65-
if (errno == EINTR) {
66-
continue;
67-
}
68-
return MP_ERR;
69-
}
70-
q += ret;
71-
n -= (size_t)ret;
72-
}
73-
return MP_OKAY;
74-
}
75-
#endif
76-
#endif
77-
78-
/* We assume all platforms besides windows provide "/dev/urandom".
79-
* In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
80-
*/
81-
#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
82-
#ifndef MP_DEV_URANDOM
83-
#define MP_DEV_URANDOM "/dev/urandom"
84-
#endif
85-
#include <fcntl.h>
86-
#include <errno.h>
87-
#include <unistd.h>
88-
89-
static int s_read_dev_urandom(void *p, size_t n)
90-
{
91-
int fd;
92-
char *q = (char *)p;
93-
94-
do {
95-
fd = open(MP_DEV_URANDOM, O_RDONLY);
96-
} while ((fd == -1) && (errno == EINTR));
97-
if (fd == -1) return MP_ERR;
98-
99-
while (n > 0) {
100-
ssize_t ret = read(fd, p, n);
101-
if (ret < 0) {
102-
if (errno == EINTR) {
103-
continue;
104-
}
105-
close(fd);
106-
return MP_ERR;
107-
}
108-
q += ret;
109-
n -= (size_t)ret;
110-
}
111-
112-
close(fd);
113-
return MP_OKAY;
114-
}
115-
#endif
116-
117-
#if defined(MP_PRNG_ENABLE_LTM_RNG)
118-
unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
119-
void (*ltm_rng_callback)(void);
120-
121-
static int s_read_ltm_rng(void *p, size_t n)
122-
{
123-
unsigned long ret;
124-
if (ltm_rng == NULL) return MP_ERR;
125-
ret = ltm_rng(p, n, ltm_rng_callback);
126-
if (ret != n) return MP_ERR;
127-
return MP_OKAY;
128-
}
129-
#endif
130-
131-
static int s_mp_rand_source_platform(void *p, size_t n)
132-
{
133-
#if defined(MP_ARC4RANDOM)
134-
arc4random_buf(p, n);
135-
return MP_OKAY;
136-
#else
137-
138-
int ret = MP_ERR;
139-
140-
#if defined(MP_WIN_CSP)
141-
ret = s_read_win_csp(p, n);
142-
if (ret == MP_OKAY) return ret;
143-
#endif
144-
145-
#if defined(MP_GETRANDOM)
146-
ret = s_read_getrandom(p, n);
147-
if (ret == MP_OKAY) return ret;
148-
#endif
149-
150-
#if defined(MP_DEV_URANDOM)
151-
ret = s_read_dev_urandom(p, n);
152-
if (ret == MP_OKAY) return ret;
153-
#endif
154-
155-
#if defined(MP_PRNG_ENABLE_LTM_RNG)
156-
ret = s_read_ltm_rng(p, n);
157-
if (ret == MP_OKAY) return ret;
158-
#endif
159-
160-
return ret;
161-
#endif
162-
}
163-
164-
int (*s_rand_source)(void *out, size_t size) = s_mp_rand_source_platform;
6+
int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_platform;
1657

1668
void mp_rand_source(int (*source)(void *out, size_t size))
1679
{
168-
s_rand_source = (source == NULL) ? s_mp_rand_source_platform : source;
10+
s_mp_rand_source = (source == NULL) ? s_mp_rand_platform : source;
16911
}
17012

17113
/* makes a pseudo-random int of a given size */
17214
int mp_rand_digit(mp_digit *r)
17315
{
174-
int ret = s_rand_source(r, sizeof(mp_digit));
16+
int ret = s_mp_rand_source(r, sizeof(mp_digit));
17517
*r &= MP_MASK;
17618
return ret;
17719
}
@@ -190,13 +32,13 @@ int mp_rand(mp_int *a, int digits)
19032
return ret;
19133
}
19234

193-
if ((ret = s_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) {
35+
if ((ret = s_mp_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) {
19436
return ret;
19537
}
19638

19739
/* TODO: We ensure that the highest digit is nonzero. Should this be removed? */
19840
while ((a->dp[digits - 1] & MP_MASK) == 0) {
199-
if ((ret = s_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) {
41+
if ((ret = s_mp_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) {
20042
return ret;
20143
}
20244
}

bn_s_mp_rand_jenkins.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_S_MP_RAND_JENKINS_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
7+
/* Chosen for speed and a good "mix" */
8+
typedef struct ranctx {
9+
uint64_t a;
10+
uint64_t b;
11+
uint64_t c;
12+
uint64_t d;
13+
} ranctx;
14+
15+
static ranctx jenkins_x;
16+
17+
#define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
18+
static uint64_t s_rand_jenkins_val(void)
19+
{
20+
uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7);
21+
jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13);
22+
jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37);
23+
jenkins_x.c = jenkins_x.d + e;
24+
jenkins_x.d = e + jenkins_x.a;
25+
return jenkins_x.d;
26+
}
27+
28+
void s_mp_rand_jenkins_init(uint64_t seed)
29+
{
30+
uint64_t i;
31+
jenkins_x.a = 0xf1ea5eed;
32+
jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
33+
for (i = 0; i < 20; ++i) {
34+
(void)s_rand_jenkins_val();
35+
}
36+
}
37+
38+
int s_mp_rand_jenkins(void *p, size_t n)
39+
{
40+
char *q = (char *)p;
41+
while (n > 0) {
42+
int i;
43+
uint64_t x = s_rand_jenkins_val();
44+
for (i = 0; i < 8 && n > 0; ++i, --n) {
45+
*q++ = (char)(x & 0xFF);
46+
x >>= 8;
47+
}
48+
}
49+
return MP_OKAY;
50+
}
51+
52+
#endif

0 commit comments

Comments
 (0)