Reseed Dice RNG on every Roll - #2478
Open
DavisNT wants to merge 1 commit into
Open
Conversation
This improves entropy of Dice, especially when triggered by shaking. As a pleasant side effect this also makes the Dice app more like a physical dice - dependent on how it is shaken, but in an unpredictable manner. The monotonic counter xTaskGetTickCount() in the seed should greatly reduce chances of generating the same RNG seed in multiple rolls.
|
Build size and comparison to main:
|
This was referenced Aug 17, 2026
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.
This improves entropy of the Dice app, especially when triggered by shaking.
As a pleasant side effect this also makes the Dice app more like a physical dice - dependent on how it is shaken, but in an unpredictable (or at least not easy to predict) manner.
The monotonic counter
xTaskGetTickCount()in the seed should greatly reduce chances of generating the same RNG seed in multiple rolls.At least on my PineTime watch the
motionControllerseems to return 12 bits for each axis and single axis very often gets saturated (returns 2047 or -2048) during shake, sometimes two axis get saturated during shake.Assuming
xTaskGetTickCount()resolution is close to 1 ms (and people have precision of no more than 64 - 256 ms),it should also provide at least around 6-8 bits of entropyits provided entropy likely will be limited by the temporal resolution of the mechanism triggering the roll (e.g. the accelerometer refresh rate of 10 Hz).I think in most cases (unless the roller intentionally precisely times the roll and controls/predicts all 3 accelerometer axis), this PR should provide enough entropy to make a single number (in the 2-99 sides range) per roll truly-random. 🔒
And this PR should eliminate the biggest security problem with current Dice app implementation (regardless of whether
std::mt19937orstd::minstd_randis used) - ability to analyze results of certain number of Dice app rolls (likely little over 13 rolls of 6 sides in case ofstd::minstd_rand- assumingstd::minstd_randhas 32-bit internal state and every roll leaks around 2.58 bits of it) and predict the results of all subsequent rolls (until the Dice app gets restarted).TL;DR I think this could be a good addition to #2473 and a pretty cool thing on it's own as it makes the Dice app more like a real physical dice. 😎 🎲