add crandom.py, for our RNG, where we mix stuff with os.urandom - #10794
Draft
SomberNight wants to merge 2 commits into
Draft
add crandom.py, for our RNG, where we mix stuff with os.urandom#10794SomberNight wants to merge 2 commits into
SomberNight wants to merge 2 commits into
Conversation
Currently we rely on `os.urandom()` ~everywhere for cryptographically secure randomness. Existing code already checks at runtime that the output of `os.urandom()` looks at least somewhat sane (see if it compresses with zlib) and hard-fails if it does not. However, `os.urandom` could still be subtly "broken" (undetected by us) and produce bad quality output. That's the motivation of the new code here. We expect `os.urandom` to work well, BUT if it undetectably does not, mixing in other sources of entropy mitigates the situation somewhat. This PR introduces a new module `crandom.py` that manages the RNG, and that our other code should call. Extreme care should be taken here not to make things worse than the status quo by "rolling our own" thing. The logic is split across two modules: `crandom.py` and `crandom_env.py`. - The core sensitive logic (RNG mixing, extracting random bytes) is in `crandom.py`, which is absolutely security critical and is kept concise to ease review. - `crandom_env.py` contains secondary sources of entropy and potentially platform-specific code. It is just a companion to `crandom.py` and is only intended to be accessed from there. Even if all the entropy sources listed in `crandom_env.py` are broken, assuming `os.urandom()` produces high quality random, `crandom.py` should never produce low-quality random output. Hence `crandom_env.py` is much less critical tro review in depth. This is inspired by https://github.com/bitcoin/bitcoin/blob/67efced1fc83a0b7215cc1513e7c4754fee0f12f/src/random.h#L25, which is significantly more advanced. But I wanted to (1) lessen our dependence on `os.urandom()`, (2) while keeping it simple.
SomberNight
force-pushed
the
202607_crandom
branch
from
August 3, 2026 20:34
9fcec9d to
57237d7
Compare
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.
(still WIP)
Currently we rely on
os.urandom()~everywhere for cryptographically secure randomness. Existing code already checks at runtime that the output ofos.urandom()looks at least somewhat sane (see if it compresses with zlib) and hard-fails if it does not.However,
os.urandomcould still be subtly "broken" (undetected by us) and produce bad quality output. That's the motivation of the new code here. We expectos.urandomto work well, BUT if it undetectably does not, mixing in other sources of entropy mitigates the situation somewhat.This PR introduces a new module
crandom.pythat manages the RNG, and that our other code should call. Extreme care should be taken here not to make things worse than the status quo by "rolling our own" thing.The logic is split across two modules:
crandom.pyandcrandom_env.py.crandom.py, which is absolutely security critical and is kept concise to ease review.crandom_env.pycontains secondary sources of entropy and potentially platform-specific code. It is just a companion tocrandom.pyand is only intended to be accessed from there. Even if all the entropy sources listed incrandom_env.pyare broken, assumingos.urandom()produces high quality random,crandom.pyshould never produce low-quality random output. Hencecrandom_env.pyis much less critical tro review in depth.This is inspired by https://github.com/bitcoin/bitcoin/blob/67efced1fc83a0b7215cc1513e7c4754fee0f12f/src/random.h#L25, which is significantly more advanced. But I wanted to (1) lessen our dependence on
os.urandom(), (2) while keeping it simple.