fix(provably-fair): use rejection sampling in computeDiceRoll to eliminate modulo bias#4
Open
amathxbt wants to merge 1 commit into
Open
Conversation
computeDiceRoll() computed raw % 10000 on a uint32. Because 2^32 (4,294,967,296) is not evenly divisible by 10,000, values 0–7,295 appear slightly more often than values 7,296–9,999 — a systematic bias of ~1.7 per million per value in the favoured range. In a high-volume casino context this is not negligible: over millions of rolls the house retains a hidden edge on top of the declared house edge. Replace modulo with rejection sampling over successive 4-byte HMAC windows using a rejection threshold of floor(2^32 / 10000) * 10000 (4,294,960,000). Values at or above the threshold are discarded; the next 4-byte window is used. Average iterations per roll < 2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
computeDiceRoll()maps a uniform uint32 to [0, 9999] with a simple modulo:Because 2³² = 4,294,967,296 is not evenly divisible by 10,000, values 0–7,295 are each produced by 429,497 possible inputs while values 7,296–9,999 are each produced by only 429,496 — a systematic bias of ~1 in 429,496 per value.
Impact: In a high-volume casino context this is exploitable. Over millions of rolls, the house retains a hidden edge on top of the declared house edge for outcomes in the biased range (0–7,295). Players who bet consistently on those values receive slightly worse expected returns than the published odds imply.
Fix
Replace the modulo with rejection sampling over successive 4-byte HMAC windows. A rejection threshold of
floor(2³² / 10000) × 10000 = 4,294,960,000is used; values at or above the threshold are discarded and the next 4-byte window is tried. Average iterations per roll is less than 2. The output distribution is exactly uniform.