1 /**********************************************************************
2 * Copyright (c) 2013-2015 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef SECP256K1_TESTRAND_IMPL_H
8 #define SECP256K1_TESTRAND_IMPL_H
16 static secp256k1_rfc6979_hmac_sha256_t secp256k1_test_rng
;
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);
26 SECP256K1_INLINE
static uint32_t secp256k1_rand32(void) {
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
) {
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.
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);
110 #endif /* SECP256K1_TESTRAND_IMPL_H */