Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions go/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import (
log "github.com/golang/glog"
)

// TODO: Add test coverage for the various exported
// noise-generating functions.

var (
randBufLock sync.Mutex
randBuf io.Reader = bufio.NewReaderSize(cryptorand.Reader, 65536)
// randBuf must wrap crypto/rand: every DP mechanism in this library
// relies on it for unpredictable noise.
randBuf io.Reader = bufio.NewReaderSize(cryptorand.Reader, 65536)

randBitLock sync.Mutex
randBitBuf uint8
Expand Down Expand Up @@ -91,6 +90,9 @@ func Boolean() bool {
// I63n returns an integer from the set {0,...,n-1} uniformly at random.
// The value of n must be positive.
func I63n(n int64) int64 {
// TODO: n is not validated. n == 0 panics with divide-by-zero on the
// line below. n < 0 returns non-negative values in [0, |n|) instead
// of failing, violating the documented [0, n) contract.
largestMultipleOfN := (math.MaxInt64 / n) * n
var positiveRandomInteger int64
for true {
Expand Down Expand Up @@ -147,6 +149,9 @@ func (rs randSource) Int63() int64 {
log.Fatalf("out of randomness, should never happen: %v", err)
}
i := int64(binary.LittleEndian.Uint64(r[:]))
// TODO: -i overflows when i == math.MinInt64 (uint64 0x8000000000000000),
// returning math.MinInt64 itself and violating the [0, 1<<63) contract.
// Replace with `& math.MaxInt64` to clear the sign bit uniformly.
if i < 0 {
return -i
}
Expand Down
Loading