]> Git Repo - secp256k1.git/blame - src/testrand_impl.h
Implement endomorphism optimization for secp256k1_ecmult_const
[secp256k1.git] / src / testrand_impl.h
CommitLineData
71712b27
GM
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 **********************************************************************/
0a433ea2 6
f0709ac5
GM
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;
3fd6253e 19
89561118 20SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
3e6f1e20 21 secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
3fd6253e
PW
22}
23
a4a43d75 24SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
02efd065
PW
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;
28 }
29 return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
d06e61cb
PW
30}
31
32static void secp256k1_rand256(unsigned char *b32) {
02efd065 33 secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
d06e61cb
PW
34}
35
36static void secp256k1_rand256_test(unsigned char *b32) {
37 int bits=0;
02efd065
PW
38 uint64_t ent = 0;
39 int entleft = 0;
d06e61cb
PW
40 memset(b32, 0, 32);
41 while (bits < 256) {
02efd065
PW
42 int now;
43 uint32_t val;
44 if (entleft < 12) {
45 ent |= ((uint64_t)secp256k1_rand32()) << entleft;
46 entleft += 32;
47 }
48 now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
49 val = 1 & (ent >> 11);
50 ent >>= 12;
51 entleft -= 12;
d06e61cb
PW
52 while (now > 0 && bits < 256) {
53 b32[bits / 8] |= val << (bits % 8);
54 now--;
55 bits++;
56 }
57 }
58}
59
60#endif
This page took 0.033107 seconds and 4 git commands to generate.