13 : _k0(static_cast<uint32_t>(seed)),
14 _k1(static_cast<uint32_t>(seed >> 32)),
16 _half_bits((ceilLog2(n) + 1) / 2),
17 _half_mask((uint32_t(1) << _half_bits) - 1),
20 mooseAssert(
_n > 0,
"n must be > 0");
21 mooseAssert(
_rounds > 0,
"rounds must be greater than 0.");
27 mooseAssert(x <
_n,
"x must be < n");
41 mooseAssert(y <
_n,
"y must be < n");
59 for (
unsigned int r = 0; r <
_rounds; ++r)
63 const uint32_t newL = R;
79 for (
int r = static_cast<int>(
_rounds) - 1; r >= 0; --r)
81 const uint32_t prevR = L;
82 const uint32_t F =
roundFunction(prevR, static_cast<unsigned int>(r));
96 x += 0x9e3779b9U *
static_cast<uint32_t
>(
round + 1);
105 mooseAssert(n > 0,
"n must be > 0");
107 unsigned int bits = 0;
MooseRandomPerturbation(uint64_t seed, unsigned int n, unsigned int rounds=8)
Construct a permutation of [0, n) keyed by seed.
static uint32_t mix32(uint32_t x)
Bijective 32-bit avalanche hash (finalizer from Murmur3 / degski hash).
const uint32_t _k0
Lower 32 bits of the seed, used as the first subkey in the round function.
uint32_t permute(uint32_t x) const
Map x to its permuted index in [0, n).
const uint32_t _k1
Upper 32 bits of the seed, used as the second subkey in the round function.
uint32_t permutePadded(uint32_t x) const
Apply one full pass of the balanced Feistel network over the padded domain [0, 2^(2*half_bits)).
uint32_t invert(uint32_t y) const
Recover the original index from a permuted value, i.e.
const uint32_t _half_mask
Bitmask of width _half_bits, used to keep half-block arithmetic in range.
const unsigned int _half_bits
Number of bits in each Feistel half-block: ceil((ceil(log2(n)) + 1) / 2)
uint32_t roundFunction(uint32_t half, unsigned int round) const
Keyed round function F(half, round) used in each Feistel step.
const unsigned int _rounds
Number of Feistel rounds to apply per permute/invert call.
uint32_t invertPadded(uint32_t y) const
Invert one full pass of the Feistel network by running rounds in reverse.
static unsigned int ceilLog2(uint32_t n)
Return the number of bits needed to represent values in [0, n-1], i.e.
const unsigned int _n
Size of the permutation domain [0, n)