Repository navigation
Conversation
a7f0dce to
55d0aee
Compare
MonotonicEntropy.random rejected a validly drawn value of 0 in its rejection-sampling loop, making an increment of exactly 1 unreachable for entropy sources without a fast Int63n path (e.g. crypto/rand.Reader). For inc == 2 this made the increment fully deterministic (always 2), contradicting the documented "random number between 1 and inc inclusive" contract. Restructure the loop to match the crypto/rand.Int pattern it is adapted from, so 0 is a valid draw and the result is correctly uniform over [1, inc].
55d0aee to
d52271c
Compare
agammann
left a comment
There was a problem hiding this comment.
The one-byte regression is covered, but multi-byte increment bounds still have a truncated range because the mask is applied to the low-order byte. I reproduced this on the current PR head; details and a deterministic public-API case are on the inline comment.
| } | ||
|
|
||
| // inc may exceed m.inc; redraw rather than bias the low end of the range. | ||
| if inc < m.inc { |
There was a problem hiding this comment.
This redraw check still samples from a truncated range for multi-byte draws. Line 677 masks m.rand[0], but the bytes are decoded as little-endian below, so the excess-bit mask belongs on m.rand[byteLen-1]. On this PR head, Monotonic(reader, 256) with a nonzero 10-byte seed followed by draw bytes ff 00 yields an entropy increment of 2 across two New(123, entropy) calls; the little-endian candidate is 255, so the increment should be 256. In 200 crypto/rand.Reader draws with this bound I observed only increments 1 or 2. Could you move the mask to the most significant byte and add a regression case across the one-byte boundary?
There was a problem hiding this comment.
yeah, the mask was on the low byte since the draw is little-endian. moved it to the most significant one and added the ff 00 case with inc = 256
the draw is decoded as little-endian, so masking the first byte left the high byte untouched and multi-byte increments only ever came out as 1 or 2.
|
Thanks for the update. I rechecked |
agammann
left a comment
There was a problem hiding this comment.
Rechecked 3cd0b35 on Linux/amd64 with Go 1.26.7. go test -count=1 ./... passed, including the new regression for the multi-byte increment boundary I flagged. That addresses my change request. This was a single-platform test run. AI-assisted review.
Problem
ulid.Monotonic(entropy, inc)documents that calls toMonotonicReadwithinthe same ULID timestamp return entropy "incremented by a random number
between 1 and
incinclusive" (ulid.go, doc comment onMonotonic).That contract does not hold for the
io.Reader-backed slow path used by anyentropy source that does not implement the internal
Int63n(int64) int64fast-path interface -- which includes
crypto/rand.Reader, the entropysource recommended by the package's own README for cases where
math/rand-based entropy is not appropriate.Concretely:
inc == 2, the increment is fully deterministic: it is always 2,never 1. This defeats the purpose of randomizing the increment for that
configuration.
Empirical reproduction (before the fix)
Using
ulid.Monotonic(crand.Reader, 2)and generating 200 ULIDs in the samemillisecond, the entropy delta between consecutive ULIDs was recorded:
All 200 trials produced a delta of exactly 2. A delta of 1 never occurred,
contradicting the documented "1 and
incinclusive" range.Root cause
ulid.go,(*MonotonicEntropy).random(), the rejection-sampling loop usedby the
io.Readerslow path (around line 670 before the fix):The loop is intended to draw a uniform value in
[0, m.inc)(by rejectingdraws
>= m.inc, the same rejection-sampling technique used bycrypto/rand.Int, which this code is explicitly adapted from) and thenreturn
1 + incso the final result lands in[1, m.inc].The
inc == 0 ||clause incorrectly rejects a legitimately drawn value of 0and forces a redraw. For
m.inc == 2, the only accepted raw draw ends upbeing 1 (0 is rejected, 2 and 3 are rejected as out of range), so the
function always returns
1 + 1 == 2. More generally, this excludes thesmallest value in the target range from ever being drawn, making a final
increment of 1 unreachable for any
m.inc.By contrast, the fast path for entropy sources that implement
Int63n(e.g.
math/rand.Rand) does not have this bug:1 + m.rng.Int63n(int64(m.inc))correctly draws from[0, m.inc)(0 inclusive)and shifts to
[1, m.inc]. The io.Reader path was a discrepantre-implementation of the same idea.
Fix
ulid.go,(*MonotonicEntropy).random():Restructured the rejection-sampling loop to match the actual pattern used by
crypto/rand.Int(the function this code says it's adapted from): afor { ... }loop that always executes its body at least once and breaksonly once a value strictly less than
m.inchas been drawn, rather thangating the loop on a pre-loop condition. (A minimal one-line removal of the
inc == 0 ||clause was tried first but is wrong: sinceincis thefunction's named return value, it is Go-zero-initialized to 0, so a bare
for inc >= m.incloop condition would never enter its body -- 0 is never>= m.incfor anym.inc >= 2-- andrandom()would return 1unconditionally without reading any entropy at all. The regression test
below caught this immediately.)
This draws a uniform value in
[0, m.inc)(0 now included) and returns1 + incin[1, m.inc], matching both the documented contract and theexisting fast-path behavior. Two stale doc comments ("Range: [1, m.inc)")
were also corrected to "[1, m.inc]" to match the actual (and now correct)
inclusive range.
This does not weaken the monotonicity guarantee:
random()still alwaysreturns a value
>= 1(never 0), soMonotonicEntropy.increment()stillstrictly increases the entropy on every call within the same millisecond,
and overflow handling (
uint80.Add/ErrMonotonicOverflow) is untouched.The rejection-sampling technique itself (draw
byteLenbytes, mask offexcess bits, reject and redraw if the value is out of range) is unchanged
and remains unbiased and loop-terminating; the fix only removes an
over-restrictive extra rejection, so the expected number of iterations per
call is now less than or equal to what it was before.