Skip to content

Commit

Permalink
Merge pull request #900 from openfheorg/dev
Browse files Browse the repository at this point in the history
Updates to v1.2.3
  • Loading branch information
yspolyakov authored Oct 30, 2024
2 parents 14a03e6 + 83603f4 commit 7b8346f
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 185 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ project (OpenFHE C CXX)

set(OPENFHE_VERSION_MAJOR 1)
set(OPENFHE_VERSION_MINOR 2)
set(OPENFHE_VERSION_PATCH 2)
set(OPENFHE_VERSION_PATCH 3)
set(OPENFHE_VERSION ${OPENFHE_VERSION_MAJOR}.${OPENFHE_VERSION_MINOR}.${OPENFHE_VERSION_PATCH})

set(CMAKE_CXX_STANDARD 17)
Expand Down
6 changes: 6 additions & 0 deletions docs/static_docs/Release_Notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
10/30/2024: OpenFHE 1.2.3 (stable) is released

* Adds support for a custom PRNG implemented in an external library

The detailed list of changes is available at https://github.com/openfheorg/openfhe-development/issues?q=is%3Aissue+milestone%3A%22Release+1.2.3%22

10/28/2024: OpenFHE 1.2.2 (stable) is released

* Improves the runtime of inverse NTT (for clang++; #872)
Expand Down
9 changes: 3 additions & 6 deletions src/core/include/math/distributiongenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
#include <string>

namespace lbcrypto {
// if FIXED_SEED is defined, then PRNG uses a fixed seed number for reproducible results during debug.
// Use only one OMP thread to ensure reproducibility
// #define FIXED_SEED


/**
* @brief PseudoRandomNumberGenerator provides the PRNG capability to all random distribution generators in OpenFHE.
Expand All @@ -59,7 +55,8 @@ class PseudoRandomNumberGenerator {
* @brief InitPRNGEngine() initializes the PRNG generator
* @param libPath a string with the absolute path to an external PRNG library ("/path/to/libprng.so").
* If the string is empty, then the default (OpenFHE's built-in PRNG) library will be used.
* @note this function should be called at the beginning of main() if an external library to be used
* @note this function should be called at the beginning of main() if an external library to be used and
* prints a trace in this case. There is no trace for the built-in PRNG
*/
static void InitPRNGEngine(const std::string& libPath = std::string());

Expand All @@ -69,7 +66,7 @@ class PseudoRandomNumberGenerator {
static PRNG& GetPRNG();

private:
using GenPRNGEngineFuncPtr = PRNG* (*)(const PRNG::seed_array_t&, uint64_t counter);
using GenPRNGEngineFuncPtr = PRNG* (*)();

// shared pointer to a thread-specific PRNG engine
static std::shared_ptr<PRNG> m_prng;
Expand Down
7 changes: 6 additions & 1 deletion src/core/include/utils/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ void MoveAppend(std::vector<X>& dst, std::vector<X>& src) {
}
}

void secure_memset(void* mem, uint8_t c, size_t len);
/**
* @brief secure_memset() is a function with the same functionality which is provided by std::memset.
* Usually, the compiler optimizes a call to std::memset out if it is called for a memory which goes out of scope.
* This function is never optimized out and used to re-initialize a memory for security reasons.
*/
void secure_memset(volatile void* mem, uint8_t c, size_t len);

} // namespace lbcrypto

Expand Down
70 changes: 43 additions & 27 deletions src/core/include/utils/prng/blake2engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "utils/prng/prng.h"

#include <cstddef>
#include <array>

namespace default_prng {
/**
Expand All @@ -47,50 +48,65 @@ namespace default_prng {
*/
class Blake2Engine : public PRNG {
public:
/**
* @brief Main constructor taking a vector of MAX_SEED_GENS integers as a seed and a counter.
* If there is no value for the counter, then pass zero as the counter value
*/
explicit Blake2Engine(const PRNG::seed_array_t& seed, uint64_t counter) : PRNG(seed, counter) {}

/**
* @brief main call to the PRNG
*/
PRNG::result_type operator()() override {
if (m_bufferIndex == static_cast<size_t>(PRNG::PRNG_BUFFER_SIZE))
m_bufferIndex = 0;

// makes a call to the BLAKE2 generator only when the currently buffered values are all consumed precomputations and
// done only once for the current buffer
if (m_bufferIndex == 0)
Generate();

PRNG::result_type result = m_buffer[m_bufferIndex];
m_bufferIndex++;

return result;
}
enum {
MAX_SEED_GENS = 16,
// the buffer stores 1024 samples of 32-bit integers
PRNG_BUFFER_SIZE = 1024
};
using blake2_seed_array_t = std::array<PRNG::result_type, MAX_SEED_GENS>;

/**
* @brief Main constructor taking an array of integers as a seed and a counter.
* If there is no value for the counter, then pass zero as the counter value
*/
explicit Blake2Engine(const blake2_seed_array_t& seed, uint64_t counter) : m_seed(seed), m_counter(counter) {}

~Blake2Engine();

/**
* @brief main call to the PRNG
*/
PRNG::result_type operator()() override {
if (m_bufferIndex == static_cast<size_t>(PRNG_BUFFER_SIZE))
m_bufferIndex = 0;

// makes a call to the BLAKE2 generator only when the currently buffered values are all consumed precomputations and
// done only once for the current buffer
if (m_bufferIndex == 0)
Generate();

PRNG::result_type result = m_buffer[m_bufferIndex];
m_bufferIndex++;

return result;
}

private:
/**
* @brief The main call to blake2xb function
*/
void Generate();

// The vector that stores random samples generated using the hash function
std::array<PRNG::result_type, PRNG::PRNG_BUFFER_SIZE> m_buffer{};
// The vector to store random samples generated using the hash function
std::array<PRNG::result_type, PRNG_BUFFER_SIZE> m_buffer{};

// Index in m_buffer corresponding to the current PRNG sample
size_t m_bufferIndex = 0;

// the seed for the hash function
blake2_seed_array_t m_seed{};

// counter used as input to the hash function; gets incremented after each call
uint64_t m_counter = 0;
};

/**
* @brief createEngineInstance() generates a Blake2Engine object which is dynamically allocated
* @return pointer to the generated Blake2Engine object
* @attention the caller is responsible for freeing the memory allocated by this function
* @attention the caller is responsible for freeing the memory allocated by this function
**/
extern "C" {
PRNG* createEngineInstance(const PRNG::seed_array_t& seed, uint64_t counter);
PRNG* createEngineInstance();
}

} // namespace default_prng
Expand Down
37 changes: 10 additions & 27 deletions src/core/include/utils/prng/prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

/**
* DISCLAIMER: IMPORTANT NOTICE ABOUT FILE MODIFICATIONS
*
* This file is used in OpenFHE's built-in PRNG and ANY EXTERNAL PRNG.
*
* This file is used in OpenFHE's built-in PRNG and ANY EXTERNAL PRNG.
* The file is critical to the functionality and the security of the library.
*
*
* Modifications should only be performed by personnel who understand the potential impacts.
*
*
* By proceeding with changes to this file, you acknowledge that you understand the risks involved and
* accept full responsibility for any resulting issues.
*/
Expand All @@ -50,53 +50,36 @@

#include <cstdint>
#include <limits>
#include <array>


// ATTENTION (VERY IMPORTANT):
// for any engine class derived from the PRNG class there must be a C function named "createEngineInstance"
// returning a dynamically allocated object of that derived class (see how it is done in blake2engine.h)
// returning a dynamically allocated object of that derived class (see how it is done in blake2engine.h)
class PRNG {
public:
enum {
MAX_SEED_GENS = 16,
// the buffer stores 1024 samples of 32-bit integers
PRNG_BUFFER_SIZE = 1024
};

// all C++11 distributions used in OpenFHE work with uint32_t by default.
// a different data type can be specified if needed for a particular architecture
using result_type = uint32_t;
using seed_array_t = std::array<result_type, MAX_SEED_GENS>;
using result_type = uint32_t;

/**
* @brief minimum value used by C++11 distribution generators when no lower
* bound is explicitly specified by the user
*/
static constexpr result_type min() {
return std::numeric_limits<result_type>::min();
return std::numeric_limits<result_type>::min();
}

/**
* @brief maximum value used by C++11 distribution generators when no upper
* bound is explicitly specified by the user
*/
static constexpr result_type max() {
return std::numeric_limits<result_type>::max();
return std::numeric_limits<result_type>::max();
}

virtual result_type operator()() = 0;
virtual ~PRNG() = default;
virtual ~PRNG() = default;

protected:
PRNG() = default;
PRNG(const seed_array_t &seed, uint64_t counter) : m_counter(counter), m_seed(seed) {}

// counter used as input to the hash function; gets incremented after each call
uint64_t m_counter = 0;

// the seed for the hash function
seed_array_t m_seed{};
};
#endif // __PRNG_H__

#endif // __PRNG_H__
Loading

0 comments on commit 7b8346f

Please sign in to comment.