7#ifndef SECP256K1_TESTRAND_IMPL_H
8#define SECP256K1_TESTRAND_IMPL_H
44 ret &= ((~((uint32_t)0)) >> (32 - bits));
60 static const int addbits[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0};
61 uint32_t trange, mult;
72 bits = bits + addbits[bits];
73 mult = ((~((uint32_t)0)) >> (32 - bits)) / range;
74 trange = range * mult;
82 return (mult == 1) ? x : (x % range);
93 memset(bytes, 0, len);
94 while (bits < len * 8) {
99 while (now > 0 && bits < len * 8) {
100 bytes[bits / 8] |= val << (bits % 8);
116 unsigned char seed16[16] = {0};
117 if (hexseed && strlen(hexseed) != 0) {
119 while (pos < 16 && hexseed[0] != 0 && hexseed[1] != 0) {
121 if ((sscanf(hexseed,
"%2hx", &sh)) == 1) {
130 FILE *frand =
fopen(
"/dev/urandom",
"r");
131 if ((frand == NULL) || fread(&seed16, 1,
sizeof(seed16), frand) !=
sizeof(seed16)) {
132 uint64_t t = time(NULL) * (uint64_t)1337;
133 fprintf(stderr,
"WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n");
136 seed16[2] ^= t >> 16;
137 seed16[3] ^= t >> 24;
138 seed16[4] ^= t >> 32;
139 seed16[5] ^= t >> 40;
140 seed16[6] ^= t >> 48;
141 seed16[7] ^= t >> 56;
148 printf(
"random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]);
153 unsigned char run32[32];
155 printf(
"random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
FILE * fopen(const fs::path &p, const char *mode)
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen)
static uint32_t secp256k1_test_rng_precomputed[8]
static int secp256k1_test_rng_integer_bits_left
static uint32_t secp256k1_testrand_int(uint32_t range)
static void secp256k1_testrand_flip(unsigned char *b, size_t len)
static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len)
static void secp256k1_testrand256(unsigned char *b32)
static int secp256k1_test_rng_precomputed_used
static SECP256K1_INLINE void secp256k1_testrand_seed(const unsigned char *seed16)
static void secp256k1_testrand_init(const char *hexseed)
static secp256k1_rfc6979_hmac_sha256 secp256k1_test_rng
static void secp256k1_testrand_finish(void)
static SECP256K1_INLINE uint32_t secp256k1_testrand32(void)
static uint64_t secp256k1_test_rng_integer
static void secp256k1_testrand256_test(unsigned char *b32)
static uint32_t secp256k1_testrand_bits(int bits)