use a consistent style for fixed-size int types

The stdint.h types don't cover 128-bit integers and the underscore makes
them ill suited to usage in function suffixes. Instead, use the common
naming style in the Linux kernel and elsewhere including the ChaCha8
implementation included here.
This commit is contained in:
Daniel Micay 2018-10-04 14:25:16 -04:00
parent 92a33182fb
commit dcd969ae04
6 changed files with 44 additions and 43 deletions

View file

@ -36,7 +36,7 @@ static void get_random_seed(void *buf, size_t size) {
}
void random_state_init(struct random_state *state) {
uint8_t rnd[CHACHA_KEY_SIZE + CHACHA_IV_SIZE];
u8 rnd[CHACHA_KEY_SIZE + CHACHA_IV_SIZE];
get_random_seed(rnd, sizeof(rnd));
chacha_keysetup(&state->ctx, rnd);
chacha_ivsetup(&state->ctx, rnd + CHACHA_KEY_SIZE);
@ -55,8 +55,8 @@ static void refill(struct random_state *state) {
}
}
uint16_t get_random_u16(struct random_state *state) {
uint16_t value;
u16 get_random_u16(struct random_state *state) {
u16 value;
size_t remaining = RANDOM_CACHE_SIZE - state->index;
if (remaining < sizeof(value)) {
refill(state);
@ -67,23 +67,23 @@ uint16_t get_random_u16(struct random_state *state) {
}
// See Fast Random Integer Generation in an Interval by Daniel Lemire
uint16_t get_random_u16_uniform(struct random_state *state, uint16_t bound) {
uint32_t random = get_random_u16(state);
uint32_t multiresult = random * bound;
uint16_t leftover = multiresult;
u16 get_random_u16_uniform(struct random_state *state, u16 bound) {
u32 random = get_random_u16(state);
u32 multiresult = random * bound;
u16 leftover = multiresult;
if (leftover < bound) {
uint16_t threshold = -bound % bound;
u16 threshold = -bound % bound;
while (leftover < threshold) {
random = get_random_u16(state);
multiresult = random * bound;
leftover = (uint16_t)multiresult;
leftover = (u16)multiresult;
}
}
return multiresult >> 16;
}
uint64_t get_random_u64(struct random_state *state) {
uint64_t value;
u64 get_random_u64(struct random_state *state) {
u64 value;
size_t remaining = RANDOM_CACHE_SIZE - state->index;
if (remaining < sizeof(value)) {
refill(state);
@ -94,12 +94,12 @@ uint64_t get_random_u64(struct random_state *state) {
}
// See Fast Random Integer Generation in an Interval by Daniel Lemire
uint64_t get_random_u64_uniform(struct random_state *state, uint64_t bound) {
unsigned __int128 random = get_random_u64(state);
unsigned __int128 multiresult = random * bound;
uint64_t leftover = multiresult;
u64 get_random_u64_uniform(struct random_state *state, u64 bound) {
u128 random = get_random_u64(state);
u128 multiresult = random * bound;
u64 leftover = multiresult;
if (leftover < bound) {
uint64_t threshold = -bound % bound;
u64 threshold = -bound % bound;
while (leftover < threshold) {
random = get_random_u64(state);
multiresult = random * bound;