PRCYCoin  2.0.0.7rc1
P2P Digital Currency
testrand_impl.h
Go to the documentation of this file.
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  **********************************************************************/
6 
7 #ifndef _SECP256K1_TESTRAND_IMPL_H_
8 #define _SECP256K1_TESTRAND_IMPL_H_
9 
10 #include <stdint.h>
11 #include <string.h>
12 
13 #include "testrand.h"
14 
15 static uint32_t secp256k1_Rz = 11, secp256k1_Rw = 11;
16 
17 SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v) {
18  secp256k1_Rz = v >> 32;
19  secp256k1_Rw = v;
20 
21  if (secp256k1_Rz == 0 || secp256k1_Rz == 0x9068ffffU) {
22  secp256k1_Rz = 111;
23  }
24  if (secp256k1_Rw == 0 || secp256k1_Rw == 0x464fffffU) {
25  secp256k1_Rw = 111;
26  }
27 }
28 
29 SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
30  secp256k1_Rz = 36969 * (secp256k1_Rz & 0xFFFF) + (secp256k1_Rz >> 16);
31  secp256k1_Rw = 18000 * (secp256k1_Rw & 0xFFFF) + (secp256k1_Rw >> 16);
32  return (secp256k1_Rw << 16) + (secp256k1_Rw >> 16) + secp256k1_Rz;
33 }
34 
35 static void secp256k1_rand256(unsigned char *b32) {
36  for (int i=0; i<8; i++) {
37  uint32_t r = secp256k1_rand32();
38  b32[i*4 + 0] = (r >> 0) & 0xFF;
39  b32[i*4 + 1] = (r >> 8) & 0xFF;
40  b32[i*4 + 2] = (r >> 16) & 0xFF;
41  b32[i*4 + 3] = (r >> 24) & 0xFF;
42  }
43 }
44 
45 static void secp256k1_rand256_test(unsigned char *b32) {
46  int bits=0;
47  memset(b32, 0, 32);
48  while (bits < 256) {
49  uint32_t ent = secp256k1_rand32();
50  int now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
51  uint32_t val = 1 & (ent >> 11);
52  while (now > 0 && bits < 256) {
53  b32[bits / 8] |= val << (bits % 8);
54  now--;
55  bits++;
56  }
57  }
58 }
59 
60 #endif
testrand.h
r
void const uint64_t uint64_t * r
Definition: field_5x52_asm_impl.h:10
SECP256K1_INLINE
#define SECP256K1_INLINE
Definition: secp256k1.h:23