7#ifndef SECP256K1_TESTRAND_IMPL_H
8#define SECP256K1_TESTRAND_IMPL_H
17static uint64_t secp256k1_test_state[4];
18static uint64_t secp256k1_test_rng_integer;
19static int secp256k1_test_rng_integer_bits_left = 0;
21SECP256K1_INLINE static void secp256k1_testrand_seed(
const unsigned char *seed16) {
22 static const unsigned char PREFIX[19] =
"secp256k1 test init";
23 unsigned char out32[32];
28 secp256k1_sha256_initialize(&hash);
29 secp256k1_sha256_write(&hash, PREFIX,
sizeof(PREFIX));
30 secp256k1_sha256_write(&hash, seed16, 16);
31 secp256k1_sha256_finalize(&hash, out32);
32 for (i = 0; i < 4; ++i) {
35 for (
j = 0;
j < 8; ++
j)
s = (
s << 8) | out32[8*i +
j];
36 secp256k1_test_state[i] =
s;
38 secp256k1_test_rng_integer_bits_left = 0;
42 return (x << k) | (x >> (64 - k));
47 const uint64_t result = rotl(secp256k1_test_state[0] + secp256k1_test_state[3], 23) + secp256k1_test_state[0];
48 const uint64_t t = secp256k1_test_state[1] << 17;
49 secp256k1_test_state[2] ^= secp256k1_test_state[0];
50 secp256k1_test_state[3] ^= secp256k1_test_state[1];
51 secp256k1_test_state[1] ^= secp256k1_test_state[2];
52 secp256k1_test_state[0] ^= secp256k1_test_state[3];
53 secp256k1_test_state[2] ^= t;
54 secp256k1_test_state[3] = rotl(secp256k1_test_state[3], 45);
60 if (secp256k1_test_rng_integer_bits_left < bits) {
61 secp256k1_test_rng_integer = secp256k1_testrand64();
62 secp256k1_test_rng_integer_bits_left = 64;
64 ret = secp256k1_test_rng_integer;
65 secp256k1_test_rng_integer >>= bits;
66 secp256k1_test_rng_integer_bits_left -= bits;
72 return secp256k1_testrand_bits(32);
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};
99 bits = bits + addbits[bits];
100 mult = ((~((
uint32_t)0)) >> (32 - bits)) / range;
101 trange = range * mult;
107 uint32_t x = secp256k1_testrand_bits(bits);
109 return (mult == 1) ? x : (x % range);
114static void secp256k1_testrand256(
unsigned char *b32) {
116 for (i = 0; i < 4; ++i) {
117 uint64_t val = secp256k1_testrand64();
130static void secp256k1_testrand_bytes_test(
unsigned char *bytes,
size_t len) {
133 while (bits <
len * 8) {
136 now = 1 + (secp256k1_testrand_bits(6) * secp256k1_testrand_bits(5) + 16) / 31;
137 val = secp256k1_testrand_bits(1);
138 while (now > 0 && bits <
len * 8) {
139 bytes[bits / 8] |= val << (bits % 8);
146static void secp256k1_testrand256_test(
unsigned char *b32) {
147 secp256k1_testrand_bytes_test(b32, 32);
150static void secp256k1_testrand_flip(
unsigned char *b,
size_t len) {
151 b[secp256k1_testrand_int(
len)] ^= (1 << secp256k1_testrand_bits(3));
154static void secp256k1_testrand_init(
const char* hexseed) {
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)) {
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]);
188 secp256k1_testrand_seed(seed16);
191static void secp256k1_testrand_finish(
void) {
192 unsigned char run32[32];
193 secp256k1_testrand256(run32);
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]);
LOGGING_API void printf(Category category, const char *format,...)
unsigned __int64 uint64_t
memset(pInfo->slotDescription, ' ', 64)