2018-08-21 15:23:22 -04:00
|
|
|
#ifndef RANDOM_H
|
|
|
|
#define RANDOM_H
|
|
|
|
|
2018-08-23 16:30:44 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-08-25 23:00:00 -04:00
|
|
|
#include "chacha.h"
|
|
|
|
|
|
|
|
#define RANDOM_CACHE_SIZE 256ULL
|
|
|
|
#define RANDOM_RESEED_SIZE 256ULL * 1024
|
2018-08-23 16:30:44 -04:00
|
|
|
|
2018-08-21 15:23:22 -04:00
|
|
|
struct random_state {
|
2018-08-23 16:30:44 -04:00
|
|
|
size_t index;
|
2018-08-25 23:00:00 -04:00
|
|
|
size_t reseed;
|
|
|
|
chacha_ctx ctx;
|
2018-08-23 16:30:44 -04:00
|
|
|
uint8_t cache[RANDOM_CACHE_SIZE];
|
2018-08-21 15:23:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void random_state_init(struct random_state *state);
|
2018-08-25 18:32:44 -04:00
|
|
|
uint16_t get_random_u16(struct random_state *state);
|
|
|
|
uint16_t get_random_u16_uniform(struct random_state *state, uint16_t bound);
|
|
|
|
uint64_t get_random_u64(struct random_state *state);
|
|
|
|
uint64_t get_random_u64_uniform(struct random_state *state, uint64_t bound);
|
2018-08-21 15:23:22 -04:00
|
|
|
|
|
|
|
#endif
|