Skip to content

Conversation

@mrdomino
Copy link
Contributor

Replaces the fancy platform-word-based logic with a simple call to random_bits_core with rejection sampling. Each retry has p > 0.5 (worst case) of selecting a number inside the range; compare against OpenBSD:

/*
 * This could theoretically loop forever but each retry has
 * p > 0.5 (worst case, usually far better) of selecting a
 * number inside the range we need, so it should rarely need
 * to re-roll.
 */
for (;;) {
	r = arc4random();
	if (r >= min)
		break;
}

NB. This change will generally result in different numbers being produced by Uint::random_mod, as evidenced by the test change.

Fixes #1009

Succeeds:
```sh
cargo test  # --target=x86_64-unknown-linux-gnu
```

Fails:
```sh
cargo test --target=i386-unknown-linux-gnu
```
Replaces the fancy platform-word-based logic with a simple call to
`random_bits_core` with rejection sampling. Each retry has p > 0.5
(worst case) of selecting a number inside the range; compare against
[OpenBSD][0]:

```c
	/*
	 * This could theoretically loop forever but each retry has
	 * p > 0.5 (worst case, usually far better) of selecting a
	 * number inside the range we need, so it should rarely need
	 * to re-roll.
	 */
	for (;;) {
		r = arc4random();
		if (r >= min)
			break;
	}
```

**NB**. This change will generally result in different numbers being
produced after calls to `Uint::random_mod`, as evidenced by the test
change.

[0]: https://github.com/openbsd/src/blob/0c90e2b526b26d9f50dde7f7712cec107cda3326/lib/libc/crypt/arc4random_uniform.c#L43-L53
@codecov
Copy link

codecov bot commented Nov 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.87%. Comparing base (3cce1fe) to head (52ea351).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1010      +/-   ##
==========================================
+ Coverage   79.86%   79.87%   +0.01%     
==========================================
  Files         162      162              
  Lines       17575    17577       +2     
==========================================
+ Hits        14036    14040       +4     
+ Misses       3539     3537       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mrdomino
Copy link
Contributor Author

mrdomino commented Nov 24, 2025

#1011 shows failing test results. Surprisingly (to me) i686 fails but wasm32 passes… (the behavior I was seeing elsewhere was variance between amd64 / arm64 on the one hand and wasm32 on the other.) (looks like wasm32 is just built, not tested.)

Uses a modulus with even odds of rejection so that the number sequence
produced exercises the rejection logic in `random_mod_core`. Also tests
a sequence of five numbers instead of four, giving us about 64 (in fact,
exactly 65) bits against this modulus.
@tarcieri tarcieri merged commit 62b90b8 into RustCrypto:master Nov 25, 2025
25 of 26 checks passed
@tarcieri
Copy link
Member

Opened #1017 to revert this. It seems it never actually passed CI (my bad)

tarcieri added a commit that referenced this pull request Nov 26, 2025
This reverts commit 75001b2 (#1010)

We're noticing CI failures on cross which seem to be related to this
change, e.g.

https://github.com/RustCrypto/crypto-bigint/actions/runs/19647957432/job/56267760132

It looks like it's potentially getting stuck in an infinite loop.
tarcieri added a commit that referenced this pull request Nov 26, 2025
This reverts commit 62b90b8 (#1010)

We're noticing CI failures on cross which seem to be related to this
change, e.g.

https://github.com/RustCrypto/crypto-bigint/actions/runs/19647957432/job/56267760132

It looks like it's potentially getting stuck in an infinite loop.
tarcieri added a commit that referenced this pull request Nov 26, 2025
This reverts commit 62b90b8 (#1010)

We're noticing CI failures on cross which seem to be related to this
change, e.g.

https://github.com/RustCrypto/crypto-bigint/actions/runs/19647957432/job/56267760132

It looks like it's potentially getting stuck in an infinite loop.
mrdomino pushed a commit to mrdomino/crypto-bigint that referenced this pull request Nov 26, 2025
Re-applies the random_mod platform-independence change from RustCrypto#1010
with additional diagnostic tests to help identify the cause of the
CI hang on aarch64-unknown-linux-gnu under cross/QEMU.

Diagnostic tests added:
- random_bits_core_small_bits_diagnostic: Verify 14-bit generation
- random_mod_minimal_rejection: Use 2^14-1 modulus (minimal rejection)
- ct_lt_small_values_diagnostic: Verify ct_lt works for small values
- random_mod_manual_step_diagnostic: Step through random_mod_core logic
- random_mod_detailed_debug: Print values for debugging

If the issue is in random_bits_core or ct_lt, these tests should fail
with informative error messages instead of hanging.
@mrdomino mrdomino deleted the rng-platform-dependence branch November 27, 2025 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

random_mod is not platform-independent

3 participants