26 #ifndef LBCRYPTO_MATH_DISTRIBUTIONGENERATOR_H_ 27 #define LBCRYPTO_MATH_DISTRIBUTIONGENERATOR_H_ 34 #include "math/backend.h" 35 #include "utils/prng/blake2engine.h" 47 typedef Blake2Engine PRNG;
63 int threads = PalisadeParallelControls.GetNumThreads();
67 #pragma omp parallel for num_threads(threads) 68 for (
int i = 0; i < threads; ++i) {
73 static PRNG &GetPRNG() {
75 if (m_prng ==
nullptr) {
78 #if defined(FIXED_SEED) 80 std::cerr <<
"**FOR DEBUGGING ONLY!!!! Using fixed initializer for " 81 "PRNG. Use a single thread only, e.g., OMP_NUM_THREADS=1!" 84 std::array<uint32_t, 16> seed{};
86 m_prng = std::make_shared<PRNG>(seed);
102 std::array<uint32_t, 16> initKey{};
106 initKey[0] = std::chrono::high_resolution_clock::now()
110 initKey[1] = std::hash<std::thread::id>{}(std::this_thread::get_id());
113 #if !defined(__arm__) && !defined(__EMSCRIPTEN__) 114 if (
sizeof(
size_t) == 8)
116 (std::hash<std::thread::id>{}(std::this_thread::get_id()) >> 32);
122 void *mem = malloc(1);
123 uint32_t counter =
reinterpret_cast<long long>(mem);
126 PRNG gen(initKey, counter);
128 std::uniform_int_distribution<uint32_t> distribution(0);
129 std::array<uint32_t, 16> seed{};
130 for (uint32_t i = 0; i < 16; i++) {
131 seed[i] = distribution(gen);
134 std::array<uint32_t, 16> rdseed{};
136 bool rdGenPassed =
false;
138 while (!rdGenPassed && idx < attempts) {
140 std::random_device genR;
141 for (uint32_t i = 0; i < 16; i++) {
149 rdseed[i] = distribution(genR);
152 }
catch (std::exception &e) {
157 for (uint32_t i = 0; i < 16; i++) {
158 seed[i] += rdseed[i];
161 m_prng = std::make_shared<PRNG>(seed);
170 static std::shared_ptr<PRNG> m_prng;
172 #if !defined(FIXED_SEED) 175 #pragma omp threadprivate(m_prng) 187 template <
typename VecType>
196 #endif // LBCRYPTO_MATH_DISTRIBUTIONGENERATOR_H_ static void InitPRNG()
Returns a reference to the PRNG engine.
Definition: distributiongenerator.h:62
The class providing the PRNG capability to all random distribution generators in PALISADE. THe security of Ring Learning With Errors (used for all crypto capabilities in PALISADE) depends on the randomness of uniform, ternary, and Gaussian distributions, which derive their randomness from the PRNG.
Definition: distributiongenerator.h:56
Abstract class describing generator requirements.
Definition: distributiongenerator.h:188
Definition: binfhecontext.h:36