runtime: seed fastrand() with hardware randomness

This commit is contained in:
Damian Gryski
2024-09-18 12:28:55 -07:00
committed by Ayke
parent b3e1974c30
commit f3dfe1d49e
2 changed files with 10 additions and 4 deletions
+2 -2
View File
@@ -42,8 +42,8 @@ func TestBinarySize(t *testing.T) {
tests := []sizeTest{
// microcontrollers
{"hifive1b", "examples/echo", 4484, 280, 0, 2252},
{"microbit", "examples/serial", 2732, 388, 8, 2256},
{"wioterminal", "examples/pininterrupt", 6016, 1484, 116, 6816},
{"microbit", "examples/serial", 2808, 388, 8, 2256},
{"wioterminal", "examples/pininterrupt", 6064, 1484, 116, 6816},
// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
+8 -2
View File
@@ -23,7 +23,13 @@ func fastrand() uint32 {
return xorshift32State
}
var xorshift32State uint32 = 1
func init() {
r, _ := hardwareRand()
xorshift64State = uint64(r | 1) // protect against 0
xorshift32State = uint32(xorshift64State)
}
var xorshift32State uint32
func xorshift32(x uint32) uint32 {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
@@ -43,7 +49,7 @@ func fastrand64() uint64 {
return xorshift64State
}
var xorshift64State uint64 = 1
var xorshift64State uint64
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
func xorshiftMult64(x uint64) uint64 {