1 /**********************************************************************
2 * Copyright (c) 2013, 2014 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;
20 SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
21 secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
24 SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
25 if (secp256k1_test_rng_precomputed_used == 8) {
26 secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, (unsigned char*)(&secp256k1_test_rng_precomputed[0]), sizeof(secp256k1_test_rng_precomputed));
27 secp256k1_test_rng_precomputed_used = 0;
29 return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
32 static void secp256k1_rand256(unsigned char *b32) {
33 secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
36 static void secp256k1_rand256_test(unsigned char *b32) {
45 ent |= ((uint64_t)secp256k1_rand32()) << entleft;
48 now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
49 val = 1 & (ent >> 11);
52 while (now > 0 && bits < 256) {
53 b32[bits / 8] |= val << (bits % 8);