Skip to content

WIP: PseudoRandomNumberGenerator is not initialized for threads that are not started using `#pragma omp parallel`

Some background

We have a project written in Rust utilizing PALISADE for crypto and math primitives. The project links via a small extern "C" header/source file that hooks directly into PALISADE functions.

The problem

When we call any method that utilizes PseudoRandomNumberGenerator::GetPRNG on any thread that is not the main thread we get a nullptr.

Looking in distributiongenerator.h we see that there's a threadprivate pointer to an RNG which is initialized once for all threads using #pragma omp parallel.

This means that when running on any thread that is not the main thread or a #pragma omp parallel scope, the RNG is not initialized resulting in segfault when using the RNG.

Proposed solution

The MR adds a new flag to the PseudoRandomNumberGenerator class m_omp_flag and makes the old m_flag threadprivate. There's an extra check in GetPRNG on the value of m_flag, which will not be set for threads not run with omp parallel. If the flag is not set the shared pointer is initialized. This means that any thread will in fact get an RNG, albeit a freshly initialized one.

I assume the original initialization is a security consideration since a part of the seed is predictable. This change moves the burden of thread-pooling to the user of the library, but at the same time it enables the use of a num_threads that is not the default along with language agnostic threads like pthread.

Comments and discussions are appreciated. Thank you for your time.

Edited by Jesenko Mehmedbašić

Merge request reports