https://mooseframework.inl.gov
MooseRandomPerturbation.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "MooseError.h"
13 
14 #include <cstdint>
15 
32 {
33 public:
42  MooseRandomPerturbation(uint64_t seed, unsigned int n, unsigned int rounds = 8);
43 
54  uint32_t permute(uint32_t x) const;
55 
64  uint32_t invert(uint32_t y) const;
65 
66 private:
75  uint32_t permutePadded(uint32_t x) const;
76 
83  uint32_t invertPadded(uint32_t y) const;
84 
96  uint32_t roundFunction(uint32_t half, unsigned int round) const;
97 
104  static unsigned int ceilLog2(uint32_t n);
105 
113  static uint32_t mix32(uint32_t x);
114 
116  const uint32_t _k0;
118  const uint32_t _k1;
120  const unsigned int _n;
122  const unsigned int _half_bits;
124  const uint32_t _half_mask;
126  const unsigned int _rounds;
127 };
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.
Generates a keyed pseudo-random permutation of the integers [0, n) using a balanced Feistel network...
T round(T x)
Definition: MathUtils.h:77
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)