Skip to content

Commit 188b7c9

Browse files
avitenzerclaude
andcommitted
test(rand): bound the crypto-backed distribution test by binomial sigmas, not a fixed 7% (MAG-4280)
TestProtocolRandDistribution reused isUniformlyDistributed, whose fixed 7% per-slot bound was written for seeded sources: on those it passes or fails the same way every run. The protocol rand draws from crypto/rand, so the test differs each run. Each slot's count is Binomial(100000, 1/50), mean 2000 and sigma 44.3, which makes 7% only 3.2 sigmas; over the 4 x 50 slots one run checks, about one run in four failed (seen on a stacked branch: slot 29 off by 7.2%). isUniformWithinSigmas bounds each slot at six sigmas of that distribution, about one failing run in 2.5 million. The seeded tests keep the old helper. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent cc2b188 commit 188b7c9

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

‎utils/rand/rand_test.go‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rand
22

33
import (
4+
"math"
45
"strconv"
56
"testing"
67

@@ -64,11 +65,38 @@ func TestDistribution(t *testing.T) {
6465
})
6566
}
6667

68+
// isUniformWithinSigmas is isUniformlyDistributed for a source that differs on every run. A fixed
69+
// percentage bound is only safe on a seeded source, where it passes or fails the same way each
70+
// time; here each slot's count is Binomial(iterations, 1/maxValue), so the bound is a number of
71+
// standard deviations of that distribution. At six sigmas a fair source fails one of the 200 slot
72+
// checks in TestProtocolRandDistribution about once in 2.5 million runs; the fixed 7% bound is
73+
// 3.2 sigmas there and failed about one run in four.
74+
func isUniformWithinSigmas(t *testing.T, f func(n int64) int64) {
75+
t.Helper()
76+
77+
const iterations = 100000
78+
const maxValue = 50
79+
const sigmas = 6
80+
81+
numberToCount := make(map[int64]int64, maxValue)
82+
for i := 0; i < iterations; i++ {
83+
numberToCount[f(maxValue)]++
84+
}
85+
86+
p := 1.0 / maxValue
87+
target := iterations * p
88+
bound := sigmas * math.Sqrt(iterations*p*(1-p))
89+
for i := 0; i < maxValue; i++ {
90+
count := float64(numberToCount[int64(i)])
91+
require.InDelta(t, target, count, bound, "slot "+strconv.Itoa(i))
92+
}
93+
}
94+
6795
func TestProtocolRandDistribution(t *testing.T) {
6896
InitRandomSeed()
69-
isUniformlyDistributed(t, func(n int64) int64 { return int64(Intn(int(n))) })
70-
isUniformlyDistributed(t, func(n int64) int64 { return Int63() % n })
71-
isUniformlyDistributed(t, func(n int64) int64 { return int64(Uint64() % uint64(n)) })
72-
isUniformlyDistributed(t, func(n int64) int64 { return int64(Float64() * float64(n)) })
97+
isUniformWithinSigmas(t, func(n int64) int64 { return int64(Intn(int(n))) })
98+
isUniformWithinSigmas(t, func(n int64) int64 { return Int63() % n })
99+
isUniformWithinSigmas(t, func(n int64) int64 { return int64(Uint64() % uint64(n)) })
100+
isUniformWithinSigmas(t, func(n int64) int64 { return int64(Float64() * float64(n)) })
73101
require.Panics(t, func() { Intn(0) })
74102
}

0 commit comments

Comments
 (0)