Skip to content

Commit 3c53796

Browse files
author
Edward Thomson
committed
rand: introduce git_rand PRNG
Introduce `git_rand`, a PRNG based on xoroshiro256**, a fast, all-purpose pseudo-random number generator: https://prng.di.unimi.it The PRNG will be seeded by the system's entropy store when possible, falling back to current time and system data (pid, uptime, etc). Inspiration for this was taken from libressl, but since our PRNG is not used for cryptographic purposes (and indeed currently only generates a unique temp file name that is written in a protected directory), this should be more than sufficient. Our implementation of xoroshiro256** was taken almost strictly from the original author's sources, but was tested against PractRand to ensure that there were no foolish mistranslations: ``` RNG_test using PractRand version 0.94 RNG = RNG_stdin64, seed = unknown test set = core, folding = standard (64 bit) rng=RNG_stdin64, seed=unknown length= 256 megabytes (2^28 bytes), time= 2.9 seconds no anomalies in 210 test result(s) rng=RNG_stdin64, seed=unknown length= 512 megabytes (2^29 bytes), time= 6.2 seconds no anomalies in 226 test result(s) rng=RNG_stdin64, seed=unknown length= 1 gigabyte (2^30 bytes), time= 12.7 seconds no anomalies in 243 test result(s) rng=RNG_stdin64, seed=unknown length= 2 gigabytes (2^31 bytes), time= 25.4 seconds no anomalies in 261 test result(s) rng=RNG_stdin64, seed=unknown length= 4 gigabytes (2^32 bytes), time= 50.6 seconds no anomalies in 277 test result(s) rng=RNG_stdin64, seed=unknown length= 8 gigabytes (2^33 bytes), time= 104 seconds no anomalies in 294 test result(s) ```
1 parent d299a7a commit 3c53796

File tree

6 files changed

+281
-0
lines changed

6 files changed

+281
-0
lines changed

COPYING

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,3 +1132,15 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
11321132
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
11331133
OF THE POSSIBILITY OF SUCH DAMAGE.
11341134

1135+
----------------------------------------------------------------------
1136+
1137+
The xoroshiro256** implementation is licensed in the public domain:
1138+
1139+
Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
1140+
1141+
To the extent possible under law, the author has dedicated all copyright
1142+
and related and neighboring rights to this software to the public domain
1143+
worldwide. This software is distributed without any warranty.
1144+
1145+
See <http://creativecommons.org/publicdomain/zero/1.0/>.
1146+

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ check_prototype_definition(qsort_r
5555

5656
check_function_exists(qsort_s GIT_QSORT_S)
5757

58+
check_function_exists(getentropy GIT_RAND_GETENTROPY)
59+
5860
# Find required dependencies
5961

6062
if(WIN32)

src/features.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@
4848
#cmakedefine GIT_SHA1_OPENSSL 1
4949
#cmakedefine GIT_SHA1_MBEDTLS 1
5050

51+
#cmakedefine GIT_RAND_GETENTROPY 1
52+
5153
#endif

src/libgit2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mwindow.h"
2121
#include "object.h"
2222
#include "odb.h"
23+
#include "rand.h"
2324
#include "refs.h"
2425
#include "runtime.h"
2526
#include "sysdir.h"
@@ -70,6 +71,7 @@ int git_libgit2_init(void)
7071
git_allocator_global_init,
7172
git_threadstate_global_init,
7273
git_threads_global_init,
74+
git_rand_global_init,
7375
git_hash_global_init,
7476
git_sysdir_global_init,
7577
git_filter_global_init,

src/rand.c

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
2+
3+
To the extent possible under law, the author has dedicated all copyright
4+
and related and neighboring rights to this software to the public domain
5+
worldwide. This software is distributed without any warranty.
6+
7+
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
8+
9+
#include "common.h"
10+
#include "rand.h"
11+
#include "runtime.h"
12+
13+
#if defined(GIT_RAND_GETENTROPY)
14+
# include <sys/random.h>
15+
#endif
16+
17+
static uint64_t state[4];
18+
static git_mutex state_lock;
19+
20+
typedef union {
21+
double f;
22+
uint64_t d;
23+
} bits;
24+
25+
#if defined(GIT_WIN32)
26+
GIT_INLINE(int) getseed(uint64_t *seed)
27+
{
28+
HCRYPTPROV provider;
29+
SYSTEMTIME systemtime;
30+
FILETIME filetime, idletime, kerneltime, usertime;
31+
bits convert;
32+
33+
if (CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL,
34+
CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
35+
BOOL success = CryptGenRandom(provider, sizeof(uint64_t), (void *)seed);
36+
CryptReleaseContext(provider, 0);
37+
38+
if (success)
39+
return 0;
40+
}
41+
42+
GetSystemTime(&systemtime);
43+
if (!SystemTimeToFileTime(&systemtime, &filetime)) {
44+
git_error_set(GIT_ERROR_OS, "could not get time for random seed");
45+
return -1;
46+
}
47+
48+
/* Fall-through: generate a seed from the time and system state */
49+
*seed = 0;
50+
*seed |= ((uint64_t)filetime.dwLowDateTime << 32);
51+
*seed |= ((uint64_t)filetime.dwHighDateTime);
52+
53+
GetSystemTimes(&idletime, &kerneltime, &usertime);
54+
55+
*seed ^= ((uint64_t)idletime.dwLowDateTime << 32);
56+
*seed ^= ((uint64_t)kerneltime.dwLowDateTime);
57+
*seed ^= ((uint64_t)usertime.dwLowDateTime << 32);
58+
59+
*seed ^= ((uint64_t)idletime.dwHighDateTime);
60+
*seed ^= ((uint64_t)kerneltime.dwHighDateTime << 12);
61+
*seed ^= ((uint64_t)usertime.dwHighDateTime << 24);
62+
63+
*seed ^= ((uint64_t)GetCurrentProcessId() << 32);
64+
*seed ^= ((uint64_t)GetCurrentThreadId() << 48);
65+
66+
convert.f = git__timer(); *seed ^= (convert.d);
67+
68+
/* Mix in the addresses of some functions and variables */
69+
*seed ^= (((uint64_t)((uintptr_t)seed) << 32));
70+
*seed ^= (((uint64_t)((uintptr_t)&errno)));
71+
72+
return 0;
73+
}
74+
75+
#else
76+
77+
GIT_INLINE(int) getseed(uint64_t *seed)
78+
{
79+
struct timeval tv;
80+
double loadavg[3];
81+
bits convert;
82+
int fd;
83+
84+
# if defined(GIT_RAND_GETENTROPY)
85+
GIT_UNUSED((fd = 0));
86+
87+
if (getentropy(seed, sizeof(uint64_t)) == 0)
88+
return 0;
89+
# else
90+
/*
91+
* Try to read from /dev/urandom; most modern systems will have
92+
* this, but we may be chrooted, etc, so it's not a fatal error
93+
*/
94+
if ((fd = open("/dev/urandom", O_RDONLY)) >= 0) {
95+
ssize_t ret = read(fd, seed, sizeof(uint64_t));
96+
close(fd);
97+
98+
if (ret == (ssize_t)sizeof(uint64_t))
99+
return 0;
100+
}
101+
# endif
102+
103+
/* Fall-through: generate a seed from the time and system state */
104+
if (gettimeofday(&tv, NULL) < 0) {
105+
git_error_set(GIT_ERROR_OS, "could get time for random seed");
106+
return -1;
107+
}
108+
109+
getloadavg(loadavg, 3);
110+
111+
*seed = 0;
112+
*seed |= ((uint64_t)tv.tv_usec << 40);
113+
*seed |= ((uint64_t)tv.tv_sec);
114+
115+
*seed ^= ((uint64_t)getpid() << 48);
116+
*seed ^= ((uint64_t)getppid() << 32);
117+
*seed ^= ((uint64_t)getpgid(0) << 28);
118+
*seed ^= ((uint64_t)getsid(0) << 16);
119+
*seed ^= ((uint64_t)getuid() << 8);
120+
*seed ^= ((uint64_t)getgid());
121+
122+
convert.f = loadavg[0]; *seed ^= (convert.d >> 36);
123+
convert.f = loadavg[1]; *seed ^= (convert.d);
124+
convert.f = loadavg[2]; *seed ^= (convert.d >> 16);
125+
126+
convert.f = git__timer(); *seed ^= (convert.d);
127+
128+
/* Mix in the addresses of some variables */
129+
*seed ^= ((uint64_t)((size_t)((void *)seed)) << 32);
130+
*seed ^= ((uint64_t)((size_t)((void *)&errno)));
131+
132+
return 0;
133+
}
134+
#endif
135+
136+
static void git_rand_global_shutdown(void)
137+
{
138+
git_mutex_free(&state_lock);
139+
}
140+
141+
int git_rand_global_init(void)
142+
{
143+
uint64_t seed = 0;
144+
145+
if (git_mutex_init(&state_lock) < 0 || getseed(&seed) < 0)
146+
return -1;
147+
148+
if (!seed) {
149+
git_error_set(GIT_ERROR_INTERNAL, "failed to generate random seed");
150+
return -1;
151+
}
152+
153+
git_rand_seed(seed);
154+
git_runtime_shutdown_register(git_rand_global_shutdown);
155+
156+
return 0;
157+
}
158+
159+
/*
160+
* This is splitmix64. xoroshiro256** uses 256 bit seed; this is used
161+
* to generate 256 bits of seed from the given 64, per the author's
162+
* recommendation.
163+
*/
164+
GIT_INLINE(uint64_t) splitmix64(uint64_t *in)
165+
{
166+
uint64_t z;
167+
168+
*in += 0x9e3779b97f4a7c15;
169+
170+
z = *in;
171+
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
172+
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
173+
return z ^ (z >> 31);
174+
}
175+
176+
void git_rand_seed(uint64_t seed)
177+
{
178+
uint64_t mixer;
179+
180+
mixer = seed;
181+
182+
git_mutex_lock(&state_lock);
183+
state[0] = splitmix64(&mixer);
184+
state[1] = splitmix64(&mixer);
185+
state[2] = splitmix64(&mixer);
186+
state[3] = splitmix64(&mixer);
187+
git_mutex_unlock(&state_lock);
188+
}
189+
190+
/* This is xoshiro256** 1.0, one of our all-purpose, rock-solid
191+
generators. It has excellent (sub-ns) speed, a state (256 bits) that is
192+
large enough for any parallel application, and it passes all tests we
193+
are aware of.
194+
195+
For generating just floating-point numbers, xoshiro256+ is even faster.
196+
197+
The state must be seeded so that it is not everywhere zero. If you have
198+
a 64-bit seed, we suggest to seed a splitmix64 generator and use its
199+
output to fill s. */
200+
201+
GIT_INLINE(uint64_t) rotl(const uint64_t x, int k) {
202+
return (x << k) | (x >> (64 - k));
203+
}
204+
205+
uint64_t git_rand_next(void) {
206+
uint64_t t, result;
207+
208+
git_mutex_lock(&state_lock);
209+
210+
result = rotl(state[1] * 5, 7) * 9;
211+
212+
t = state[1] << 17;
213+
214+
state[2] ^= state[0];
215+
state[3] ^= state[1];
216+
state[1] ^= state[2];
217+
state[0] ^= state[3];
218+
219+
state[2] ^= t;
220+
221+
state[3] = rotl(state[3], 45);
222+
223+
git_mutex_unlock(&state_lock);
224+
225+
return result;
226+
}

src/rand.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_rand_h__
8+
#define INCLUDE_rand_h__
9+
10+
#include "common.h"
11+
12+
/**
13+
* Initialize the random number generation subsystem. This will
14+
* seed the random number generator with the system's entropy pool,
15+
* if available, and will fall back to the current time and
16+
* system information if not.
17+
*/
18+
int git_rand_global_init(void);
19+
20+
/**
21+
* Seed the pseudo-random number generator. This is not needed to be
22+
* called; the PRNG is seeded by `git_rand_global_init`, but it may
23+
* be useful for testing. When the same seed is specified, the same
24+
* sequence of random numbers from `git_rand_next` is emitted.
25+
*
26+
* @param seed the seed to use
27+
*/
28+
void git_rand_seed(uint64_t seed);
29+
30+
/**
31+
* Get the next pseudo-random number in the sequence.
32+
*
33+
* @return a 64-bit pseudo-random number
34+
*/
35+
uint64_t git_rand_next(void);
36+
37+
#endif

0 commit comments

Comments
 (0)