7#ifndef SECP256K1_TESTRAND_IMPL_H
8#define SECP256K1_TESTRAND_IMPL_H
22 static const unsigned char PREFIX[19] =
"secp256k1 test init";
23 unsigned char out32[32];
32 for (i = 0; i < 4; ++i) {
35 for (j = 0; j < 8; ++j) s = (s << 8) | out32[8*i + j];
42 return (x << k) | (x >> (64 - k));
67 ret &= ((~((uint64_t)0)) >> (64 - bits));
87 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};
88 uint32_t trange, mult;
99 bits = bits + addbits[bits];
100 mult = ((~((uint32_t)0)) >> (32 - bits)) / range;
101 trange = range * mult;
109 return (mult == 1) ? x : (x % range);
116 for (i = 0; i < 4; ++i) {
132 memset(bytes, 0, len);
133 while (bits < len * 8) {
138 while (now > 0 && bits < len * 8) {
139 bytes[bits / 8] |= val << (bits % 8);
155 unsigned char seed16[16] = {0};
156 if (hexseed && strlen(hexseed) != 0) {
158 while (pos < 16 && hexseed[0] != 0 && hexseed[1] != 0) {
160 if ((sscanf(hexseed,
"%2hx", &sh)) == 1) {
169 FILE *frand =
fopen(
"/dev/urandom",
"rb");
170 if ((frand == NULL) || fread(&seed16, 1,
sizeof(seed16), frand) !=
sizeof(seed16)) {
171 uint64_t t = time(NULL) * (uint64_t)1337;
172 fprintf(stderr,
"WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n");
175 seed16[2] ^= t >> 16;
176 seed16[3] ^= t >> 24;
177 seed16[4] ^= t >> 32;
178 seed16[5] ^= t >> 40;
179 seed16[6] ^= t >> 48;
180 seed16[7] ^= t >> 56;
187 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]);
192 unsigned char run32[32];
194 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_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
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 SECP256K1_INLINE uint64_t secp256k1_testrand64(void)
static void secp256k1_testrand256(unsigned char *b32)
static SECP256K1_INLINE void secp256k1_testrand_seed(const unsigned char *seed16)
static void secp256k1_testrand_init(const char *hexseed)
static uint64_t secp256k1_test_state[4]
static void secp256k1_testrand_finish(void)
static SECP256K1_INLINE uint32_t secp256k1_testrand32(void)
static SECP256K1_INLINE uint64_t rotl(const uint64_t x, int k)
static uint64_t secp256k1_test_rng_integer
static void secp256k1_testrand256_test(unsigned char *b32)
static SECP256K1_INLINE uint64_t secp256k1_testrand_bits(int bits)