]> Git Repo - secp256k1.git/blame - src/testrand_impl.h
Fix header guards using reserved identifiers
[secp256k1.git] / src / testrand_impl.h
CommitLineData
71712b27 1/**********************************************************************
251b1a62 2 * Copyright (c) 2013-2015 Pieter Wuille *
71712b27
GM
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
0a433ea2 6
abe2d3e8
DR
7#ifndef SECP256K1_TESTRAND_IMPL_H
8#define SECP256K1_TESTRAND_IMPL_H
d06e61cb
PW
9
10#include <stdint.h>
11#include <string.h>
12
f0709ac5 13#include "testrand.h"
02efd065 14#include "hash.h"
d06e61cb 15
02efd065
PW
16static secp256k1_rfc6979_hmac_sha256_t secp256k1_test_rng;
17static uint32_t secp256k1_test_rng_precomputed[8];
18static int secp256k1_test_rng_precomputed_used = 8;
251b1a62
PW
19static uint64_t secp256k1_test_rng_integer;
20static int secp256k1_test_rng_integer_bits_left = 0;
3fd6253e 21
89561118 22SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
3e6f1e20 23 secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
3fd6253e
PW
24}
25
a4a43d75 26SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
02efd065
PW
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;
30 }
31 return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
d06e61cb
PW
32}
33
251b1a62
PW
34static uint32_t secp256k1_rand_bits(int bits) {
35 uint32_t ret;
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;
39 }
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));
44 return ret;
45}
46
47static uint32_t secp256k1_rand_int(uint32_t range) {
f684d7d9
PW
48 /* We want a uniform integer between 0 and range-1, inclusive.
49 * B is the smallest number such that range <= 2**B.
50 * two mechanisms implemented here:
51 * - generate B bits numbers until one below range is found, and return it
52 * - find the largest multiple M of range that is <= 2**(B+A), generate B+A
53 * bits numbers until one below M is found, and return it modulo range
54 * The second mechanism consumes A more bits of entropy in every iteration,
55 * but may need fewer iterations due to M being closer to 2**(B+A) then
56 * range is to 2**B. The array below (indexed by B) contains a 0 when the
57 * first mechanism is to be used, and the number A otherwise.
58 */
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;
251b1a62 61 int bits = 0;
251b1a62
PW
62 if (range <= 1) {
63 return 0;
64 }
f684d7d9
PW
65 trange = range - 1;
66 while (trange > 0) {
67 trange >>= 1;
251b1a62
PW
68 bits++;
69 }
f684d7d9
PW
70 if (addbits[bits]) {
71 bits = bits + addbits[bits];
72 mult = ((~((uint32_t)0)) >> (32 - bits)) / range;
73 trange = range * mult;
74 } else {
75 trange = range;
76 mult = 1;
77 }
78 while(1) {
79 uint32_t x = secp256k1_rand_bits(bits);
80 if (x < trange) {
81 return (mult == 1) ? x : (x % range);
251b1a62
PW
82 }
83 }
84}
85
d06e61cb 86static void secp256k1_rand256(unsigned char *b32) {
02efd065 87 secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
d06e61cb
PW
88}
89
251b1a62
PW
90static void secp256k1_rand_bytes_test(unsigned char *bytes, size_t len) {
91 size_t bits = 0;
92 memset(bytes, 0, len);
93 while (bits < len * 8) {
02efd065
PW
94 int now;
95 uint32_t val;
251b1a62
PW
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);
d06e61cb
PW
100 now--;
101 bits++;
102 }
103 }
104}
105
251b1a62
PW
106static void secp256k1_rand256_test(unsigned char *b32) {
107 secp256k1_rand_bytes_test(b32, 32);
108}
109
abe2d3e8 110#endif /* SECP256K1_TESTRAND_IMPL_H */
This page took 0.045266 seconds and 4 git commands to generate.