-
Notifications
You must be signed in to change notification settings - Fork 0
/
xoroshiro.go
216 lines (194 loc) · 6.4 KB
/
xoroshiro.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// +build go1.9
package crazy
import (
"encoding/binary"
"io"
"math/bits"
)
// Xoroshiro implements the Xoroshiro128+ PRNG created by David Blackman and
// Sebastiano Vigna. It has period 2**128-1 with 128 state bits. Xoroshiro was
// the recommended PRNG for most non-scientific applications, but it has been
// supplanted by Xoshiro.
type Xoroshiro [2]uint64
// NewXoroshiro produces an unseeded Xoroshiro. Call Seed[IV]() or Restore()
// prior to use.
func NewXoroshiro() *Xoroshiro {
return &Xoroshiro{}
}
// SeedIV initializes the generator using all bits of iv, which may be of any
// size or nil.
func (xoro *Xoroshiro) SeedIV(iv []byte) {
// Seed using Sebastiano Vigna's SplitMix64, as recommended by the
// xoroshiro128+ source. However, that is the recommendation "if you have
// a 64-bit seed," so we need a strategy to use more bits than that in a
// way that will actually increase entropy up to the state size. What we
// shall do is add (GF2) each 64-bit integer from the iv into the SplitMix
// state between each advancement of it until we have no more than 128 bits
// remaining in the iv, then follow the same procedure for the values of
// the xoroshiro state. If we're given an empty (nil) iv, however, just use
// SplitMix with 0 seed.
var sm uint64
if len(iv) == 0 {
sm += 0x9e3779b97f4a7c15
z := sm
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
z ^= z >> 31
(*xoro)[0] = z
sm += 0x9e3779b97f4a7c15
z = sm
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
z ^= z >> 31
(*xoro)[1] = z
return
}
for len(iv) > 8 {
p := []byte{7: 0}
iv = iv[copy(p, iv):]
sm += 0x9e3779b97f4a7c15 ^ binary.LittleEndian.Uint64(p)
}
if len(iv) > 0 {
p := []byte{7: 0}
iv = iv[copy(p, iv):]
sm += 0x9e3779b97f4a7c15
z := sm
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
z ^= z >> 31
(*xoro)[1] = z
sm ^= binary.LittleEndian.Uint64(p)
} else {
sm += 0x9e3779b97f4a7c15
z := sm
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
z ^= z >> 31
(*xoro)[1] = z
}
p := []byte{7: 0}
copy(p, iv)
sm += 0x9e3779b97f4a7c15
z := sm
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
z ^= z >> 31
(*xoro)[0] = z
}
// Uint64 produces a 64-bit pseudo-random value.
func (xoro *Xoroshiro) Uint64() uint64 {
s0, s1 := (*xoro)[0], (*xoro)[1]
x := s0 + s1
s1 ^= s0
(*xoro)[0] = bits.RotateLeft64(s0, 55) ^ s1 ^ s1<<14
(*xoro)[1] = bits.RotateLeft64(s1, 36)
return x
}
// Read fills p with random bytes generated 64 bits at a time, discarding
// unused bytes. n will always be len(p) and err will always be nil.
func (xoro *Xoroshiro) Read(p []byte) (n int, err error) {
n = len(p)
for len(p) >= 8 {
binary.LittleEndian.PutUint64(p, xoro.Uint64())
p = p[8:]
}
b := [8]byte{}
binary.LittleEndian.PutUint64(b[:], xoro.Uint64())
copy(p, b[:])
return n, nil
}
// Save serializes the current state of the xoroshiro128+ generator. Values
// produced by such a generator that has Restore()d this state are guaranteed
// to match those produced by this exact generator. n should always be 16
// bytes.
func (xoro *Xoroshiro) Save(into io.Writer) (n int, err error) {
p := []byte{15: 0}
binary.LittleEndian.PutUint64(p, (*xoro)[0])
binary.LittleEndian.PutUint64(p[8:], (*xoro)[1])
return into.Write(p)
}
// Restore loads a Save()d xoroshiro128+ state.
func (xoro *Xoroshiro) Restore(from io.Reader) (n int, err error) {
p := []byte{15: 0}
if n, err = from.Read(p); n < len(p) {
return n, err
}
(*xoro)[0] = binary.LittleEndian.Uint64(p)
(*xoro)[1] = binary.LittleEndian.Uint64(p[8:])
return n, nil
}
// Seed is a proxy to SeedInt64. This exists to satisfy the rand.Source
// interface.
func (xoro *Xoroshiro) Seed(x int64) {
SeedInt64(xoro, x)
}
// Int63 generates an integer in the interval [0, 2**63 - 1]. This exists to
// satisfy the rand.Source interface.
func (xoro *Xoroshiro) Int63() int64 {
return int64(xoro.Uint64() >> 1)
}
// Copy creates a copy of the generator.
func (xoro *Xoroshiro) Copy() Copier {
x := *xoro
return &x
}
// Rexoroshiro is deprecated. Use Xoshiro instead.
//
// Rexoroshiro is the same as Xoroshiro but yields values that are bytewise
// reversed. This sacrifices some speed to place bits with greater linear
// complexity in the low positions, making the generator stronger when
// producing values interpreted in small ranges; e.g., RNG{rexo}.Intn(2) is
// typically "better" than RNG{xoro}.Intn(2).
type Rexoroshiro Xoroshiro
// NewRexoroshiro creates an uninitialized Rexoroshiro. This is equivalent to
// casting an uninitialized Xoroshiro to Rexoroshiro and has the same caveats.
func NewRexoroshiro() *Rexoroshiro {
return (*Rexoroshiro)(NewXoroshiro())
}
// SeedIV initializes the generator as if it were a Xoroshiro.
func (rexo *Rexoroshiro) SeedIV(iv []byte) {
(*Xoroshiro).SeedIV((*Xoroshiro)(rexo), iv)
}
// Uint64 produces a 64-bit pseudo-random value.
func (rexo *Rexoroshiro) Uint64() uint64 {
return bits.ReverseBytes64((*Xoroshiro).Uint64((*Xoroshiro)(rexo)))
}
// Read fills p with random bytes that are byte-reversed Xoroshiro values.
func (rexo *Rexoroshiro) Read(p []byte) (n int, err error) {
n = len(p)
for len(p) > 8 {
binary.LittleEndian.PutUint64(p, rexo.Uint64())
p = p[8:]
}
b := [8]byte{}
binary.LittleEndian.PutUint64(b[:], rexo.Uint64())
copy(p, b[:])
return n, nil
}
// Save serializes the Rexoroshiro state in the same way as the equivalent
// Xoroshiro state.
func (rexo *Rexoroshiro) Save(into io.Writer) (n int, err error) {
return (*Xoroshiro).Save((*Xoroshiro)(rexo), into)
}
// Restore loads the Rexoroshiro state in the same way as the equivalent
// Xoroshiro state.
func (rexo *Rexoroshiro) Restore(from io.Reader) (n int, err error) {
return (*Xoroshiro).Restore((*Xoroshiro)(rexo), from)
}
// Seed is a proxy to Xoroshiro's Seed. This exists to satisfy the rand.Source
// interface.
func (rexo *Rexoroshiro) Seed(x int64) {
(*Xoroshiro).Seed((*Xoroshiro)(rexo), x)
}
// Int63 generates an integer in the interval [0, 2**63 - 1]. This exists to
// satisfy the rand.Source interface.
func (rexo *Rexoroshiro) Int63() int64 {
// Since the low bits have the highest quality, it makes sense to mask
// instead of shifting.
return int64(rexo.Uint64() & 0x7fffffffffffffff)
}
// Copy creates a copy of the generator.
func (rexo *Rexoroshiro) Copy() Copier {
r := *rexo
return &r
}