Skip to content

Commit

Permalink
runtime: seed fastrand() with hardware randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski authored and aykevl committed Oct 2, 2024
1 parent b3e1974 commit f3dfe1d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions builder/sizes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -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 {
Expand Down

0 comments on commit f3dfe1d

Please sign in to comment.