]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/internal/randutil: use math/rand/v2
authorDamien Neil <dneil@google.com>
Fri, 27 Sep 2024 21:19:30 +0000 (14:19 -0700)
committerDamien Neil <dneil@google.com>
Mon, 30 Sep 2024 15:49:07 +0000 (15:49 +0000)
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>
src/crypto/internal/randutil/randutil.go
src/go/build/deps_test.go

index 84b1295a8770600491d2776a428cef13a648a8c5..5a8b096d1529752d57e711e354df5e52a4e1215a 100644 (file)
@@ -8,31 +8,19 @@ package randutil
 
 import (
        "io"
-       "sync"
+       "math/rand/v2"
 )
 
-var (
-       closedChanOnce sync.Once
-       closedChan     chan struct{}
-)
-
-// MaybeReadByte reads a single byte from r with ~50% probability. This is used
+// 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.
 // 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
 // source (e.g. a zeroReader).
 func MaybeReadByte(r io.Reader) {
-       closedChanOnce.Do(func() {
-               closedChan = make(chan struct{})
-               close(closedChan)
-       })
-
-       select {
-       case <-closedChan:
+       if rand.Uint64()&1 == 1 {
                return
-       case <-closedChan:
-               var buf [1]byte
-               r.Read(buf[:])
        }
+       var buf [1]byte
+       r.Read(buf[:])
 }
index 40034263cc284a3891233e0683062ed90583866d..e233535f752dfcae98f6fee03e1b8500c8c669a0 100644 (file)
@@ -460,7 +460,7 @@ var depsRules = `
        < crypto/internal/boring
        < crypto/boring;
 
-       crypto/internal/alias
+       crypto/internal/alias, math/rand/v2
        < crypto/internal/randutil
        < crypto/internal/nistec/fiat
        < crypto/internal/nistec