Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions ipcl/include/ipcl/utils/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ inline void vec_size_check(const std::vector<T>& v, const char* file,

#define VEC_SIZE_CHECK(v) vec_size_check(v, __FILE__, __LINE__)

enum class RNGenType { RDSEED = 1, RDRAND = 2, PSEUDO = 3 };

#ifdef IPCL_RUNTIME_DETECT_CPU_FEATURES
static const bool disable_avx512ifma =
(std::getenv("IPCL_DISABLE_AVX512IFMA") != nullptr);
Expand All @@ -53,10 +55,22 @@ static const bool prefer_ipp_prng =
static const cpu_features::X86Features features =
cpu_features::GetX86Info().features;
static const bool has_avx512ifma = features.avx512ifma && !disable_avx512ifma;
static const bool has_rdseed =
static const bool use_rdseed =
features.rdseed && !prefer_rdrand && !prefer_ipp_prng;
static const bool has_rdrand = features.rdrnd && prefer_rdrand;
static const bool use_rdrand = features.rdrnd && prefer_rdrand;

static const RNGenType kRNGenType = use_rdseed ? RNGenType::RDSEED
: use_rdrand ? RNGenType::RDRAND
: RNGenType::PSEUDO;

#else // compile time detection of cpu feature
#ifdef IPCL_RNG_INSTR_RDSEED
static const RNGenType kRNGenType = RNGenType::RDSEED;
#elif defined(IPCL_RNG_INSTR_RDRAND)
static const RNGenType kRNGenType = RNGenType::RDRAND;
#else
static const RNGenType kRNGenType = RNGenType::PSEUDO;
#endif
#endif // IPCL_RUNTIME_DETECT_CPU_FEATURES

#ifdef IPCL_USE_OMP
Expand Down
19 changes: 9 additions & 10 deletions ipcl/keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ BigNumber getPrimeBN(int max_bits) {
auto prime_ctx = std::vector<Ipp8u>(prime_size);
ippsPrimeInit(max_bits, reinterpret_cast<IppsPrimeState*>(prime_ctx.data()));

#if defined(IPCL_RNG_INSTR_RDSEED) || defined(IPCL_RNG_INSTR_RDRAND)
Ipp8u* rand_param = NULL;
#else
constexpr int seed_size = 160;
auto buff = std::vector<Ipp8u>(prime_size);
auto rand_param = buff.data();
ippsPRNGInit(seed_size, reinterpret_cast<IppsPRNGState*>(rand_param));

auto seed = std::vector<Ipp32u>(seed_size);
rand32u(seed);
BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS);
ippsPRNGSetSeed(BN(seed_bn), reinterpret_cast<IppsPRNGState*>(rand_param));
#endif
if (kRNGenType == RNGenType::PSEUDO) {
rand_param = buff.data();
ippsPRNGInit(seed_size, reinterpret_cast<IppsPRNGState*>(rand_param));

auto seed = std::vector<Ipp32u>(seed_size);
rand32u(seed);
BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS);
ippsPRNGSetSeed(BN(seed_bn), reinterpret_cast<IppsPRNGState*>(rand_param));
}

BigNumber prime_bn(0, max_bits / 8);
while (ippStsNoErr !=
Expand Down
49 changes: 17 additions & 32 deletions ipcl/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,31 @@
namespace ipcl {

IppStatus ippGenRandom(Ipp32u* rand, int bits, void* ctx) {
#ifdef IPCL_RUNTIME_DETECT_CPU_FEATURES
if (has_rdseed)
if (kRNGenType == RNGenType::RDSEED)
return ippsTRNGenRDSEED(rand, bits, ctx);
else if (has_rdrand)
else if (kRNGenType == RNGenType::RDRAND)
return ippsPRNGenRDRAND(rand, bits, ctx);
else
else if (kRNGenType == RNGenType::PSEUDO)
return ippsPRNGen(rand, bits, ctx);
#else
#ifdef IPCL_RNG_INSTR_RDSEED
return ippsTRNGenRDSEED(rand, bits, ctx);
#elif defined(IPCL_RNG_INSTR_RDRAND)
return ippsPRNGenRDRAND(rand, bits, ctx);
#else
return ippsPRNGen(rand, bits, ctx);
#endif
#endif // IPCL_RUNTIME_IPP_RNG
else
ERROR_CHECK(false, "ippGenRandom: RNGenType does not exist.");
}

IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx) {
#ifdef IPCL_RUNTIME_DETECT_CPU_FEATURES
if (has_rdseed)
if (kRNGenType == RNGenType::RDSEED) {
return ippsTRNGenRDSEED_BN(rand, bits, ctx);
else if (has_rdrand)
} else if (kRNGenType == RNGenType::RDRAND) {
return ippsPRNGenRDRAND_BN(rand, bits, ctx);
else
return ippsPRNGen_BN(rand, bits, ctx);
#else
#ifdef IPCL_RNG_INSTR_RDSEED
return ippsTRNGenRDSEED_BN(rand, bits, ctx);
#elif defined(IPCL_RNG_INSTR_RDRAND)
return ippsPRNGenRDRAND_BN(rand, bits, ctx);
#else
int size;
ippsPRNGGetSize(&size);
auto prng = std::vector<Ipp8u>(size);
ippsPRNGInit(160, reinterpret_cast<IppsPRNGState*>(prng.data()));
return ippsPRNGen_BN(rand, bits,
reinterpret_cast<IppsPRNGState*>(prng.data()));
#endif
#endif // IPCL_RUNTIME_IPP_RNG
} else if (kRNGenType == RNGenType::PSEUDO) {
int size;
ippsPRNGGetSize(&size);
auto prng = std::vector<Ipp8u>(size);
ippsPRNGInit(160, reinterpret_cast<IppsPRNGState*>(prng.data()));
return ippsPRNGen_BN(rand, bits,
reinterpret_cast<IppsPRNGState*>(prng.data()));
} else {
ERROR_CHECK(false, "ippGenRandomBN: RNGenType does not exist.");
}
}

BigNumber getRandomBN(int bits) {
Expand Down