Bitcoin Core  24.99.0
P2P Digital Currency
testrand_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2013-2015 Pieter Wuille *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5  ***********************************************************************/
6 
7 #ifndef SECP256K1_TESTRAND_IMPL_H
8 #define SECP256K1_TESTRAND_IMPL_H
9 
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include "testrand.h"
15 #include "hash.h"
16 
17 static uint64_t secp256k1_test_state[4];
20 
21 SECP256K1_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];
24  secp256k1_sha256 hash;
25  int i;
26 
27  /* Use SHA256(PREFIX || seed16) as initial state. */
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) {
33  uint64_t s = 0;
34  int j;
35  for (j = 0; j < 8; ++j) s = (s << 8) | out32[8*i + j];
36  secp256k1_test_state[i] = s;
37  }
39 }
40 
41 SECP256K1_INLINE static uint64_t rotl(const uint64_t x, int k) {
42  return (x << k) | (x >> (64 - k));
43 }
44 
45 SECP256K1_INLINE static uint64_t secp256k1_testrand64(void) {
46  /* Test-only Xoshiro256++ RNG. See https://prng.di.unimi.it/ */
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;
53  secp256k1_test_state[2] ^= t;
55  return result;
56 }
57 
58 SECP256K1_INLINE static uint64_t secp256k1_testrand_bits(int bits) {
59  uint64_t ret;
63  }
67  ret &= ((~((uint64_t)0)) >> (64 - bits));
68  return ret;
69 }
70 
71 SECP256K1_INLINE static uint32_t secp256k1_testrand32(void) {
72  return secp256k1_testrand_bits(32);
73 }
74 
75 static uint32_t secp256k1_testrand_int(uint32_t range) {
76  /* We want a uniform integer between 0 and range-1, inclusive.
77  * B is the smallest number such that range <= 2**B.
78  * two mechanisms implemented here:
79  * - generate B bits numbers until one below range is found, and return it
80  * - find the largest multiple M of range that is <= 2**(B+A), generate B+A
81  * bits numbers until one below M is found, and return it modulo range
82  * The second mechanism consumes A more bits of entropy in every iteration,
83  * but may need fewer iterations due to M being closer to 2**(B+A) then
84  * range is to 2**B. The array below (indexed by B) contains a 0 when the
85  * first mechanism is to be used, and the number A otherwise.
86  */
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;
89  int bits = 0;
90  if (range <= 1) {
91  return 0;
92  }
93  trange = range - 1;
94  while (trange > 0) {
95  trange >>= 1;
96  bits++;
97  }
98  if (addbits[bits]) {
99  bits = bits + addbits[bits];
100  mult = ((~((uint32_t)0)) >> (32 - bits)) / range;
101  trange = range * mult;
102  } else {
103  trange = range;
104  mult = 1;
105  }
106  while(1) {
107  uint32_t x = secp256k1_testrand_bits(bits);
108  if (x < trange) {
109  return (mult == 1) ? x : (x % range);
110  }
111  }
112 }
113 
114 static void secp256k1_testrand256(unsigned char *b32) {
115  int i;
116  for (i = 0; i < 4; ++i) {
117  uint64_t val = secp256k1_testrand64();
118  b32[0] = val;
119  b32[1] = val >> 8;
120  b32[2] = val >> 16;
121  b32[3] = val >> 24;
122  b32[4] = val >> 32;
123  b32[5] = val >> 40;
124  b32[6] = val >> 48;
125  b32[7] = val >> 56;
126  b32 += 8;
127  }
128 }
129 
130 static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len) {
131  size_t bits = 0;
132  memset(bytes, 0, len);
133  while (bits < len * 8) {
134  int now;
135  uint32_t val;
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);
140  now--;
141  bits++;
142  }
143  }
144 }
145 
146 static void secp256k1_testrand256_test(unsigned char *b32) {
148 }
149 
150 static void secp256k1_testrand_flip(unsigned char *b, size_t len) {
151  b[secp256k1_testrand_int(len)] ^= (1 << secp256k1_testrand_bits(3));
152 }
153 
154 static void secp256k1_testrand_init(const char* hexseed) {
155  unsigned char seed16[16] = {0};
156  if (hexseed && strlen(hexseed) != 0) {
157  int pos = 0;
158  while (pos < 16 && hexseed[0] != 0 && hexseed[1] != 0) {
159  unsigned short sh;
160  if ((sscanf(hexseed, "%2hx", &sh)) == 1) {
161  seed16[pos] = sh;
162  } else {
163  break;
164  }
165  hexseed += 2;
166  pos++;
167  }
168  } else {
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");
173  seed16[0] ^= t;
174  seed16[1] ^= t >> 8;
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;
181  }
182  if (frand) {
183  fclose(frand);
184  }
185  }
186 
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);
189 }
190 
191 static 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]);
195 }
196 
197 #endif /* SECP256K1_TESTRAND_IMPL_H */
int ret
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1077
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)
#define SECP256K1_INLINE
Definition: secp256k1.h:131
static int secp256k1_test_rng_integer_bits_left
Definition: testrand_impl.h:19
static uint32_t secp256k1_testrand_int(uint32_t range)
Definition: testrand_impl.h:75
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)
Definition: testrand_impl.h:45
static void secp256k1_testrand256(unsigned char *b32)
static SECP256K1_INLINE void secp256k1_testrand_seed(const unsigned char *seed16)
Definition: testrand_impl.h:21
static void secp256k1_testrand_init(const char *hexseed)
static uint64_t secp256k1_test_state[4]
Definition: testrand_impl.h:17
static void secp256k1_testrand_finish(void)
static SECP256K1_INLINE uint32_t secp256k1_testrand32(void)
Definition: testrand_impl.h:71
static SECP256K1_INLINE uint64_t rotl(const uint64_t x, int k)
Definition: testrand_impl.h:41
static uint64_t secp256k1_test_rng_integer
Definition: testrand_impl.h:18
static void secp256k1_testrand256_test(unsigned char *b32)
static SECP256K1_INLINE uint64_t secp256k1_testrand_bits(int bits)
Definition: testrand_impl.h:58