Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
seed.h
Go to the documentation of this file.
1/**************************************************************************\
2* *
3* init_rand_digits(n) *
4* *
5* This routine creates a random 64-bit pattern which can be used to *
6* initialize the constant s in the state vector. The pattern is chosen *
7* so that the 8 upper hexadecimal digits are different and also that the *
8* 8 lower digits are different. Only non-zero digits are used and the *
9* last digit is chosen to be odd. Roughly half the bits will be set to *
10* one and roughly half the bits will be set to zero. This assures *
11* sufficient change in the Weyl sequence on each iteration of the RNG. *
12* *
13* The input parameter n is a 64-bit number in the range of 0 to 2^64-1. *
14* A set of random digits will be created for this input value. This *
15* routine has been verified to produce unique constants for input values *
16* from 0 to 3 billion. For input values greater than 3 billion, some *
17* previously generated constants may occur at random. *
18* *
19* About 33 quadrillion constants may generated. Since the stream length *
20* is 2^64, this provides about 2^118 random numbers in total. *
21* *
22* One may use this routine as follows to initialize the state: *
23* *
24* #include "init.h" *
25* *
26* x = w = s = init_rand_digits(n); *
27* *
28* Alternatively, one may use init_rand_digits offline to create an *
29* include file of constants which could be compiled into the the program.*
30* This would provide the fastest initialization of the state. This may *
31* useful for certain time critical applications which require a fast *
32* initialization. The seed directory contains a program which creates *
33* a seed.h file which may be used as shown below. *
34* *
35* uint64_t seed[] = { *
36* #include "seed.h" *
37* }; *
38* *
39* x = w = s = seed[n]; *
40* *
41* See streams_example for example usage. *
42* *
43\**************************************************************************/
44
45#pragma once
46
47#include <stdint.h>
48#include <third/random/state.h>
49
50namespace Msws {
51
52constexpr uint64_t sconst[] = {
53 0x37e1c9b5e1a2b843, 0x56e9d7a3d6234c87, 0xc361be549a24e8c7, 0xd25b9768a1582d7b, 0x18b2547d3de29b67,
54 0xc1752836875c29ad, 0x4e85ba61e814cd25, 0x17489dc6729386c1, 0x7c1563ad89c2a65d, 0xcdb798e4ed82c675,
55 0xd98b72e4b4e682c1, 0xdacb7524e4b3927d, 0x53a8e9d7d1b5c827, 0xe28459db142e98a7, 0x72c1b3461e4569db,
56 0x1864e2d745e3b169, 0x6a2c143bdec97213, 0xb5e1d923d741a985, 0xb4875e967bc63d19, 0x92b64d5a82db4697,
57 0x7cae812d896eb1a5, 0xb53827d41769542d, 0x6d89b42c68a31db5, 0x75e26d434e2986d5, 0x7c82643d293cb865,
58 0x64c3bd82e8637a95, 0x2895c34d9dc83e61, 0xa7d58c34dea35721, 0x3dbc5e687c8e61d5, 0xb468a235e6d2b193,
59};
60
61/* local copy of msws rng */
62
63uint64_t xi, wi, si;
64
65inline static uint32_t mswsi() {
66 xi *= xi;
67 xi += (wi += si);
68 return xi = (xi >> 32) | (xi << 32);
69}
70
71inline static uint64_t init_rand_digits(uint64_t n) {
72 uint64_t c, i, j, k, m, r, t, u, v;
73
74 /* initialize state for local msws rng */
75
76 r = n / 100000000;
77 t = n % 100000000;
78 si = sconst[r % 30];
79 r /= 30;
80 xi = wi = t * si + r * si * 100000000;
81
82 /* get odd random number for low order digit */
83
84 u = (mswsi() % 8) * 2 + 1;
85 v = (1 << u);
86
87 /* get rest of digits */
88
89 for (m = 60, c = 0; m > 0;) {
90 j = mswsi(); /* get 8 digit 32-bit random word */
91 for (i = 0; i < 32; i += 4) {
92 k = (j >> i) & 0xf; /* get a digit */
93 if (k != 0 && (c & (1 << k)) == 0) { /* not 0 and not previous */
94 c |= (1 << k);
95 u |= (k << m); /* add digit to output */
96 m -= 4;
97 if (m == 24 || m == 28) c = (1 << k) | v;
98 if (m == 0) break;
99 }
100 }
101 }
102
103 return u;
104}
105
106inline static State seed(uint64_t n) {
107 auto seed = init_rand_digits(n);
108 return State{.x = seed, .w = seed, .s = seed};
109}
110
111} // namespace Msws
Wrapper class for internal state of the Msws generator.
Definition: state.h:17
uint64_t x
Definition: state.h:19
uint64_t w
Definition: state.h:20
uint64_t s
Definition: state.h:21
Definition: random.h:43
uint64_t si
Definition: seed.h:63
constexpr uint64_t sconst[]
Definition: seed.h:52
uint64_t wi
Definition: seed.h:63
static uint64_t init_rand_digits(uint64_t n)
Definition: seed.h:71
uint64_t xi
Definition: seed.h:63
static State seed(uint64_t n)
Definition: seed.h:106
static uint32_t mswsi()
Definition: seed.h:65