Skip to content

Commit

Permalink
Add Xor-shift based random bytes
Browse files Browse the repository at this point in the history
A xorshift32 random generator is implemented. The algorithm can be
refered to https://en.wikipedia.org/wiki/Xorshift which is from
p. 4 of Marsaglia, "Xorshift RNGs". The generator is tested with
100,000,000 number generations and shows shannon entropy of 5.52 out of
5.54 expected value.
  • Loading branch information
millaker committed Mar 25, 2024
1 parent 0ce3882 commit 05c5a23
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static cmd_element_t *cmd_list = NULL;
static param_element_t *param_list = NULL;
static bool block_flag = false;
static bool prompt_flag = true;
int prng = 0;

/* Am I timing a command that has the console blocked? */
static bool block_timing = false;
Expand Down Expand Up @@ -435,6 +436,7 @@ void init_cmd()
add_param("error", &err_limit, "Number of errors until exit", NULL);
add_param("echo", &echo, "Do/don't echo commands", NULL);
add_param("entropy", &show_entropy, "Show/Hide Shannon entropy", NULL);
add_param("prng", &prng, "Activate default/xorshift prng", NULL);

init_in();
init_time(&last_time);
Expand Down
9 changes: 8 additions & 1 deletion qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void timsort(void *priv, struct list_head *head, list_cmp_func_t cmp);
extern double shannon_entropy(const uint8_t *input_data);
extern int show_entropy;

/* PRNG option */
extern int prng;

/* Our program needs to use regular malloc/free */
#define INTERNAL 1
#include "harness.h"
Expand Down Expand Up @@ -187,7 +190,11 @@ static void fill_rand_string(char *buf, size_t buf_size)
len = rand() % buf_size;

uint64_t randstr_buf_64[MAX_RANDSTR_LEN] = {0};
randombytes((uint8_t *) randstr_buf_64, len * sizeof(uint64_t));
if (prng == 1)
randombytes_xorshift((uint8_t *) randstr_buf_64,
len * sizeof(uint64_t));
else
randombytes((uint8_t *) randstr_buf_64, len * sizeof(uint64_t));
for (size_t n = 0; n < len; n++)
buf[n] = charset[randstr_buf_64[n] % (sizeof(charset) - 1)];

Expand Down
25 changes: 25 additions & 0 deletions random.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,28 @@ int randombytes(uint8_t *buf, size_t n)
#error "randombytes(...) is not supported on this platform"
#endif
}

static uint32_t xorshift32()
{
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
static uint32_t x = 1;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}

static uint32_t xorshift8()
{
return xorshift32() & 0xFF;
}

int randombytes_xorshift(uint8_t *buf, size_t n)
{
while (n > 0) {
*buf = xorshift8();
buf++;
n--;
}
return 0;
}
1 change: 1 addition & 0 deletions random.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>

extern int randombytes(uint8_t *buf, size_t len);
extern int randombytes_xorshift(uint8_t *buf, size_t n);

static inline uint8_t randombit(void)
{
Expand Down

0 comments on commit 05c5a23

Please sign in to comment.