-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprng64.c
113 lines (72 loc) · 2.01 KB
/
prng64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Alexander PUKALL PRNG 64
// LSFR 64 + LCG 64
// 128 bits seed
// prng64.c
// The output will be 2^64 random bits.
// With lfsr seed = 0xffffffffffffffff
// and lcg seed = 0xffffffffffffffff
// the 50 first bytes are
// 83 CA C9 DF F3 82 B1 EB 1D 2D A2 6A 08 A4 7C 58 99 EB 8A 4F
// 99 B0 43 01 66 66 49 EC 77 C2 6E 91 6E E0 28 58 DD EF 5D 9D
// BE F4 DF 57 1D 27 CA B4 2B 16
// Practrand test : ./prng64 | /rng_test stdin8
// All tests OK (no anomalies), test with 8TB
//
// CODE FREE TO USE EVEN FOR COMMERCIAL APPLICATIONS
// NO RESTRICTION
#include <stdint.h>
#include <stdio.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
int main()
{
static char buf[65536];
uint64_t lfsr = 0xffffffffffffffffu; //LFSR 64 seed from 0x1 to 0xffffffffffffffff
uint64_t b=0xffffffffffffffffu; //LCG 64 seed from 0x0 to 0xffffffffffffffff
uint8_t rndbyte,tmp,shift;
int i,q,w;
if (lfsr==0) lfsr=1; // never 0 in an LFSR
setvbuf( stdout, buf, _IOFBF, sizeof(buf) );
#ifdef _WIN32
_setmode(_fileno(stdout), _O_BINARY);
#endif
for (i=0;i<64;i++) // mix LFSR 64 times before use
{
rndbyte=0;
for (q=0;q<8;q++)
{
uint64_t msb= lfsr & 0x8000000000000000;
lfsr <<= 1;
if (msb==0x8000000000000000) lfsr ^= 0x7287db1e3afac4f1u;
}
}
while(1)
{
b=(b*0x5851f42d4c957f2d)+1;
for (w=0;w<4;w++)
{
rndbyte=0;
for (q=0;q<8;q++)
{
uint64_t msb= lfsr & 0x8000000000000000;
lfsr <<= 1;
if (msb==0x8000000000000000)
{
lfsr ^= 0x7287db1e3afac4f1u;
tmp=1;
}
else
{
tmp=0;
}
rndbyte=rndbyte+(tmp<<q);
}
shift=48-(8*w);
rndbyte=rndbyte+((b>>shift)& 0xff);
printf("%c",rndbyte);
}
}
return(0);
}