From 0f3cfad1e35b21e35564ef1476a956724942edb4 Mon Sep 17 00:00:00 2001 From: gltile-two-electric-boogaloo <80788438+gltile-two-electric-boogaloo@users.noreply.github.com> Date: Sat, 21 May 2022 00:49:11 +0100 Subject: [PATCH] implement faster random number generation without segfaulting --- README.md | 2 ++ uwurandom.c | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index be66e8b..89600fc 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ [`/dev/uwurandom`](https://github.com/valadaptive/uwurandom) generates data through a tiny catgirl furiously typing away utter nonsense inside your computer. uwurandom-in-userspace does the same thing without the need to insert a kernel module. + +(uwurandom-in-userspace does not use a cryptographically secure random number generator.) ## Installation and usage ### Download uwurandom diff --git a/uwurandom.c b/uwurandom.c index 8ae8b62..64f9f6f 100644 --- a/uwurandom.c +++ b/uwurandom.c @@ -1310,7 +1310,8 @@ uint64_t rol64(uint64_t x, int k) { return (x << k) | (x >> (64 - k)); } -uint64_t xoshiro256ss(uint64_t s[4]) { +uint64_t xoshiro256ss(uwu_state *state) { + uint64_t *s = state->rand_state; uint64_t const result = rol64(s[1] * 5, 7) * 9; uint64_t const t = s[1] << 17; @@ -1328,9 +1329,9 @@ uint64_t xoshiro256ss(uint64_t s[4]) { static void get_random_buffered(uwu_state *state, void *dst, size_t size) { // TODO: handle cases where size is more than 8 bytes - if (size > (RAND_SIZE - state->rng_idx)) { - uint64_t rand = xoshiro256ss(state->rand_state); - state->rng_buf = (char*)rand; + if (size < (sizeof(uint64_t) - state->rng_idx)) { + uint64_t rand = xoshiro256ss(state); + *(state->rng_buf) = rand; state->rng_idx = 0; } memcpy(dst, state->rng_buf + state->rng_idx, size);