From fc9a6f1416be514e9d26b301d05e7e347560560b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 3 Nov 2024 22:37:48 -0500 Subject: Add RNGs to the language --- stdlib/stdlib.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'stdlib/stdlib.c') diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c index fb37fcfe..f2c6897c 100644 --- a/stdlib/stdlib.c +++ b/stdlib/stdlib.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include "metamethods.h" #include "patterns.h" #include "paths.h" +#include "rng.h" #include "siphash.h" #include "stdlib.h" #include "tables.h" @@ -30,14 +32,15 @@ public void tomo_init(void) GC_INIT(); USE_COLOR = getenv("COLOR") ? strcmp(getenv("COLOR"), "1") == 0 : isatty(STDOUT_FILENO); getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0); - unsigned int seed; - getrandom(&seed, sizeof(seed), 0); - srand(seed); - srand48(seed); - - long long_seed; - getrandom(&long_seed, sizeof(long_seed), 0); - Int$init_random(long_seed); + + int rng_fd = open("/dev/urandom", O_RDONLY); + if (rng_fd < 0) + fail("Couldn't read from /dev/urandom"); + uint8_t *random_bytes = GC_MALLOC_ATOMIC(40); + if (read(rng_fd, (void*)random_bytes, 40) < 40) + fail("Couldn't read from /dev/urandom"); + Array_t rng_seed = {.length=40, .data=random_bytes, .stride=1, .atomic=1}; + default_rng = RNG$new(rng_seed); if (register_printf_specifier('k', printf_text, printf_text_size)) errx(1, "Couldn't set printf specifier"); -- cgit v1.2.3