7 #ifndef SECP256K1_TESTRAND_IMPL_H 8 #define SECP256K1_TESTRAND_IMPL_H 17 static uint32_t secp256k1_test_rng_precomputed[8];
18 static int secp256k1_test_rng_precomputed_used = 8;
19 static uint64_t secp256k1_test_rng_integer;
20 static int secp256k1_test_rng_integer_bits_left = 0;
22 SECP256K1_INLINE static void secp256k1_rand_seed(
const unsigned char *seed16) {
23 secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
27 if (secp256k1_test_rng_precomputed_used == 8) {
28 secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, (
unsigned char*)(&secp256k1_test_rng_precomputed[0]),
sizeof(secp256k1_test_rng_precomputed));
29 secp256k1_test_rng_precomputed_used = 0;
31 return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
34 static uint32_t secp256k1_rand_bits(
int bits) {
36 if (secp256k1_test_rng_integer_bits_left < bits) {
37 secp256k1_test_rng_integer |= (((uint64_t)secp256k1_rand32()) << secp256k1_test_rng_integer_bits_left);
38 secp256k1_test_rng_integer_bits_left += 32;
40 ret = secp256k1_test_rng_integer;
41 secp256k1_test_rng_integer >>= bits;
42 secp256k1_test_rng_integer_bits_left -= bits;
43 ret &= ((~((uint32_t)0)) >> (32 - bits));
47 static uint32_t secp256k1_rand_int(uint32_t range) {
59 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};
60 uint32_t trange, mult;
71 bits = bits + addbits[bits];
72 mult = ((~((uint32_t)0)) >> (32 - bits)) / range;
73 trange = range * mult;
79 uint32_t
x = secp256k1_rand_bits(bits);
81 return (mult == 1) ? x : (x % range);
86 static void secp256k1_rand256(
unsigned char *b32) {
87 secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
90 static void secp256k1_rand_bytes_test(
unsigned char *
bytes,
size_t len) {
92 memset(bytes, 0, len);
93 while (bits < len * 8) {
96 now = 1 + (secp256k1_rand_bits(6) * secp256k1_rand_bits(5) + 16) / 31;
97 val = secp256k1_rand_bits(1);
98 while (now > 0 && bits < len * 8) {
99 bytes[bits / 8] |= val << (bits % 8);
106 static void secp256k1_rand256_test(
unsigned char *b32) {
107 secp256k1_rand_bytes_test(b32, 32);
std::vector< byte > bytes