Skip to content

Commit f3dfe1d

Browse files
dgryskiaykevl
authored andcommitted
runtime: seed fastrand() with hardware randomness
1 parent b3e1974 commit f3dfe1d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

builder/sizes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func TestBinarySize(t *testing.T) {
4242
tests := []sizeTest{
4343
// microcontrollers
4444
{"hifive1b", "examples/echo", 4484, 280, 0, 2252},
45-
{"microbit", "examples/serial", 2732, 388, 8, 2256},
46-
{"wioterminal", "examples/pininterrupt", 6016, 1484, 116, 6816},
45+
{"microbit", "examples/serial", 2808, 388, 8, 2256},
46+
{"wioterminal", "examples/pininterrupt", 6064, 1484, 116, 6816},
4747

4848
// TODO: also check wasm. Right now this is difficult, because
4949
// wasm binaries are run through wasm-opt and therefore the

src/runtime/algorithm.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ func fastrand() uint32 {
2323
return xorshift32State
2424
}
2525

26-
var xorshift32State uint32 = 1
26+
func init() {
27+
r, _ := hardwareRand()
28+
xorshift64State = uint64(r | 1) // protect against 0
29+
xorshift32State = uint32(xorshift64State)
30+
}
31+
32+
var xorshift32State uint32
2733

2834
func xorshift32(x uint32) uint32 {
2935
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
@@ -43,7 +49,7 @@ func fastrand64() uint64 {
4349
return xorshift64State
4450
}
4551

46-
var xorshift64State uint64 = 1
52+
var xorshift64State uint64
4753

4854
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
4955
func xorshiftMult64(x uint64) uint64 {

0 commit comments

Comments
 (0)