Bitcoin ABC 0.30.5
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
22
23SECP256K1_INLINE static void secp256k1_testrand_seed(const unsigned char *seed16) {
25}
26
31 }
33}
34
35static uint32_t secp256k1_testrand_bits(int bits) {
36 uint32_t ret;
40 }
44 ret &= ((~((uint32_t)0)) >> (32 - bits));
45 return ret;
46}
47
48static uint32_t secp256k1_testrand_int(uint32_t range) {
49 /* We want a uniform integer between 0 and range-1, inclusive.
50 * B is the smallest number such that range <= 2**B.
51 * two mechanisms implemented here:
52 * - generate B bits numbers until one below range is found, and return it
53 * - find the largest multiple M of range that is <= 2**(B+A), generate B+A
54 * bits numbers until one below M is found, and return it modulo range
55 * The second mechanism consumes A more bits of entropy in every iteration,
56 * but may need fewer iterations due to M being closer to 2**(B+A) then
57 * range is to 2**B. The array below (indexed by B) contains a 0 when the
58 * first mechanism is to be used, and the number A otherwise.
59 */
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;
62 int bits = 0;
63 if (range <= 1) {
64 return 0;
65 }
66 trange = range - 1;
67 while (trange > 0) {
68 trange >>= 1;
69 bits++;
70 }
71 if (addbits[bits]) {
72 bits = bits + addbits[bits];
73 mult = ((~((uint32_t)0)) >> (32 - bits)) / range;
74 trange = range * mult;
75 } else {
76 trange = range;
77 mult = 1;
78 }
79 while(1) {
80 uint32_t x = secp256k1_testrand_bits(bits);
81 if (x < trange) {
82 return (mult == 1) ? x : (x % range);
83 }
84 }
85}
86
87static void secp256k1_testrand256(unsigned char *b32) {
89}
90
91static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len) {
92 size_t bits = 0;
93 memset(bytes, 0, len);
94 while (bits < len * 8) {
95 int now;
96 uint32_t val;
97 now = 1 + (secp256k1_testrand_bits(6) * secp256k1_testrand_bits(5) + 16) / 31;
99 while (now > 0 && bits < len * 8) {
100 bytes[bits / 8] |= val << (bits % 8);
101 now--;
102 bits++;
103 }
104 }
105}
106
107static void secp256k1_testrand256_test(unsigned char *b32) {
109}
110
111static void secp256k1_testrand_flip(unsigned char *b, size_t len) {
113}
114
115static void secp256k1_testrand_init(const char* hexseed) {
116 unsigned char seed16[16] = {0};
117 if (hexseed && strlen(hexseed) != 0) {
118 int pos = 0;
119 while (pos < 16 && hexseed[0] != 0 && hexseed[1] != 0) {
120 unsigned short sh;
121 if ((sscanf(hexseed, "%2hx", &sh)) == 1) {
122 seed16[pos] = sh;
123 } else {
124 break;
125 }
126 hexseed += 2;
127 pos++;
128 }
129 } else {
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");
134 seed16[0] ^= t;
135 seed16[1] ^= t >> 8;
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;
142 }
143 if (frand) {
144 fclose(frand);
145 }
146 }
147
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]);
150}
151
152static void secp256k1_testrand_finish(void) {
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]);
156}
157
158#endif /* SECP256K1_TESTRAND_IMPL_H */
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1126
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)
#define SECP256K1_INLINE
Definition: secp256k1.h:127
static uint32_t secp256k1_test_rng_precomputed[8]
Definition: testrand_impl.h:18
static int secp256k1_test_rng_integer_bits_left
Definition: testrand_impl.h:21
static uint32_t secp256k1_testrand_int(uint32_t range)
Definition: testrand_impl.h:48
static void secp256k1_testrand_flip(unsigned char *b, size_t len)
static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len)
Definition: testrand_impl.h:91
static void secp256k1_testrand256(unsigned char *b32)
Definition: testrand_impl.h:87
static int secp256k1_test_rng_precomputed_used
Definition: testrand_impl.h:19
static SECP256K1_INLINE void secp256k1_testrand_seed(const unsigned char *seed16)
Definition: testrand_impl.h:23
static void secp256k1_testrand_init(const char *hexseed)
static secp256k1_rfc6979_hmac_sha256 secp256k1_test_rng
Definition: testrand_impl.h:17
static void secp256k1_testrand_finish(void)
static SECP256K1_INLINE uint32_t secp256k1_testrand32(void)
Definition: testrand_impl.h:27
static uint64_t secp256k1_test_rng_integer
Definition: testrand_impl.h:20
static void secp256k1_testrand256_test(unsigned char *b32)
static uint32_t secp256k1_testrand_bits(int bits)
Definition: testrand_impl.h:35