random.c 946 Bytes
Newer Older
Martin Schläffer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#include "random.h"

#include <stdlib.h>
#include <time.h>

#include "config.h"

#if ASCON_MASK_RNG == 'R'

void randinit() { srand(time(0)); }

uint32_t rand32() {
  uint32_t r;
  randombytes(&r, 4);
  return r;
}

uint64_t rand64() {
  uint64_t r;
  randombytes(&r, 8);
  return r;
}

#elif ASCON_MASK_RNG == 'S'

void randinit() { srand(time(0)); }

uint32_t rand32() { return ((uint32_t)rand() << 21) ^ rand(); }

uint64_t rand64() {
  return ((uint64_t)rand() << 43) ^ ((uint64_t)rand() << 21) ^ rand();
}

#elif ASCON_MASK_RNG == 'X'

uint32_t xorshift32;
uint64_t xorshift64;

void randinit() {
  srand(time(0));
  xorshift32 = rand();
  xorshift64 = (uint64_t)rand() << 32 | rand();
}

uint32_t rand32() {
  uint32_t x = xorshift32;
  x ^= x << 13;
  x ^= x >> 17;
  x ^= x << 5;
  return xorshift32 = x;
}

uint64_t rand64() {
  uint64_t x = xorshift64;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  return xorshift64 = x;
}

#endif