Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/quantum_rng/quantum_rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,20 +401,28 @@ int32_t qrng_range32(qrng_ctx *ctx, int32_t min, int32_t max) {
if (!ctx || min > max) {
return max;
}

uint32_t range = (uint32_t)(max - min + 1);
if (range == 0) {
return max;

/* Span in [1, 2^32]. Compute in 64-bit signed so (max - min + 1) can
never overflow: the original int32 form is signed-overflow UB at the
full span, and at -O2 the compiler then PROVES range != 0 and deletes
the (range == 0) guard below -- leaving r % 0 (SIGFPE). */
uint64_t span = (uint64_t)((int64_t)max - (int64_t)min) + 1;

if (span > 0xFFFFFFFFULL) {
/* Full 32-bit span: every uint32 is valid, no rejection or modulo. */
return (int32_t)((uint32_t)min + (uint32_t)qrng_uint64(ctx));
}

uint32_t threshold = (uint32_t)-range % range;

uint32_t range = (uint32_t)span; /* guaranteed nonzero */
uint32_t threshold = (0u - range) % range; /* == 2^32 % range, unbiased */
uint32_t r;

do {
r = (uint32_t)qrng_uint64(ctx);
} while (r < threshold);

return min + (r % range);

/* Unsigned add avoids signed-overflow UB when min<0 and result near max. */
return (int32_t)((uint32_t)min + (r % range));
}

uint64_t qrng_range64(qrng_ctx *ctx, uint64_t min, uint64_t max) {
Expand All @@ -428,7 +436,9 @@ uint64_t qrng_range64(qrng_ctx *ctx, uint64_t min, uint64_t max) {

uint64_t range = max - min + 1;
if (range == 0) {
return max;
/* range wrapped => full 2^64 span (min==0, max==UINT64_MAX): every
uint64 is valid. Return a raw draw, not the constant max. */
return qrng_uint64(ctx);
}

uint64_t threshold = -range % range;
Expand Down