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
19 changes: 18 additions & 1 deletion ipcl/keygen.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include <climits>
#include <iostream>
#include <random>
#include <vector>

#include "ipcl/ipcl.hpp"
Expand All @@ -10,6 +13,13 @@ namespace ipcl {
constexpr int N_BIT_SIZE_MAX = 2048;
constexpr int N_BIT_SIZE_MIN = 200;

static void rand32u(std::vector<Ipp32u>& addr) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(0, UINT_MAX);
for (auto& x : addr) x = (dist(rng) << 16) + dist(rng);
}

BigNumber getPrimeBN(int max_bits) {
int prime_size;
ippsPrimeGetSize(max_bits, &prime_size);
Expand All @@ -19,9 +29,15 @@ BigNumber getPrimeBN(int max_bits) {
#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(160, reinterpret_cast<IppsPRNGState*>(rand_param));
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

BigNumber prime_bn(0, max_bits / 8);
Expand Down Expand Up @@ -68,6 +84,7 @@ static void getNormalBN(int64_t n_length, BigNumber& p, BigNumber& q,
static void getDJNBN(int64_t n_length, BigNumber& p, BigNumber& q, BigNumber& n,
BigNumber& ref_dist) {
BigNumber gcd;

do {
do {
p = getPrimeBN(n_length / 2);
Expand Down
7 changes: 6 additions & 1 deletion ipcl/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx) {
#elif defined(IPCL_RNG_INSTR_RDRAND)
return ippsPRNGenRDRAND_BN(rand, bits, ctx);
#else
return ippsPRNGen_BN(rand, bits, ctx);
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
}
Expand Down