Files
palladum-lightning/common/pseudorand.c
Rusty Russell 6e5cb299dd global: remove unnecessary includes from C files.
Basically, `devtools/reduce-includes.sh */*.c`.

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s
After:
	real    0m37.872000-39.974000(39.5466+/-0.59)s
	user    3m1.211000-14.968000(12.4556+/-3.9)s
	sys     0m35.008000-36.830000(36.4143+/-0.5)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

After:
	real    0m18.384000-21.307000(20.8605+/-0.92)s
	user    2m5.585000-26.843000(23.6017+/-6.7)s
	sys     0m19.650000-22.003000(21.4943+/-0.69)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30

88 lines
1.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "config.h"
#include <assert.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/isaac/isaac64.h>
#include <ccan/tal/tal.h>
#include <common/pseudorand.h>
#include <sodium/randombytes.h>
static struct isaac64_ctx isaac64;
static struct siphash_seed siphashseed;
static bool pseudorand_initted = false;
static void init_if_needed(void)
{
if (unlikely(!pseudorand_initted)) {
unsigned char seedbuf[16];
struct sha256 sha;
randombytes_buf(seedbuf, sizeof(seedbuf));
memcpy(&siphashseed, seedbuf, sizeof(siphashseed));
/* In case isaac is reversible, don't leak seed. */
sha256(&sha, seedbuf, sizeof(seedbuf));
isaac64_init(&isaac64, sha.u.u8, sizeof(sha.u.u8));
pseudorand_initted = true;
}
}
uint64_t pseudorand(uint64_t max)
{
init_if_needed();
assert(max);
return isaac64_next_uint(&isaac64, max);
}
uint64_t pseudorand_u64(void)
{
init_if_needed();
return isaac64_next_uint64(&isaac64);
}
double pseudorand_double(void)
{
init_if_needed();
return isaac64_next_double(&isaac64);
}
const struct siphash_seed *siphash_seed(void)
{
init_if_needed();
return &siphashseed;
}
void tal_arr_randomize_(void *arr, size_t elemsize)
{
/* Easier arith. */
char *carr = arr;
size_t n = tal_bytelen(arr) / elemsize;
assert(tal_bytelen(arr) % elemsize == 0);
/* From Wikipedia's Fischer-Yates shuffle article:
*
* for i from 0 to n2 do
* j ← random integer such that i ≤ j < n
* exchange a[i] and a[j]
*/
if (n < 2)
return;
for (size_t i = 0; i < n - 1; i++) {
size_t j = i + pseudorand(n - i);
char tmp[elemsize];
/* Technically, memcpy in place is undefined (src and dest overlap). */
if (j == i)
continue;
memcpy(tmp, carr + i * elemsize, elemsize);
memcpy(carr + i * elemsize, carr + j * elemsize, elemsize);
memcpy(carr + j * elemsize, tmp, elemsize);
}
}