1
0
mirror of https://github.com/golang/go synced 2024-11-14 21:00:28 -07:00

crypto/internal/randutil: use math/rand/v2

Instead of using a select on a closed channel to generate one bit
of randomness, use math/rand/v2.

Change-Id: I36d294f17b9f5b437ca50e1c95da97bbe632c0a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/616475
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
Damien Neil 2024-09-27 14:19:30 -07:00
parent 3637ebf1e0
commit f4b7291d73
2 changed files with 6 additions and 18 deletions

View File

@ -8,31 +8,19 @@ package randutil
import ( import (
"io" "io"
"sync" "math/rand/v2"
) )
var ( // MaybeReadByte reads a single byte from r with 50% probability. This is used
closedChanOnce sync.Once
closedChan chan struct{}
)
// MaybeReadByte reads a single byte from r with ~50% probability. This is used
// to ensure that callers do not depend on non-guaranteed behaviour, e.g. // to ensure that callers do not depend on non-guaranteed behaviour, e.g.
// assuming that rsa.GenerateKey is deterministic w.r.t. a given random stream. // assuming that rsa.GenerateKey is deterministic w.r.t. a given random stream.
// //
// This does not affect tests that pass a stream of fixed bytes as the random // This does not affect tests that pass a stream of fixed bytes as the random
// source (e.g. a zeroReader). // source (e.g. a zeroReader).
func MaybeReadByte(r io.Reader) { func MaybeReadByte(r io.Reader) {
closedChanOnce.Do(func() { if rand.Uint64()&1 == 1 {
closedChan = make(chan struct{})
close(closedChan)
})
select {
case <-closedChan:
return return
case <-closedChan:
var buf [1]byte
r.Read(buf[:])
} }
var buf [1]byte
r.Read(buf[:])
} }

View File

@ -460,7 +460,7 @@ var depsRules = `
< crypto/internal/boring < crypto/internal/boring
< crypto/boring; < crypto/boring;
crypto/internal/alias crypto/internal/alias, math/rand/v2
< crypto/internal/randutil < crypto/internal/randutil
< crypto/internal/nistec/fiat < crypto/internal/nistec/fiat
< crypto/internal/nistec < crypto/internal/nistec