Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getRandomBN error when using OpenMP, if using RDSEED #57

Closed
zfscgy opened this issue Mar 6, 2023 · 4 comments
Closed

getRandomBN error when using OpenMP, if using RDSEED #57

zfscgy opened this issue Mar 6, 2023 · 4 comments

Comments

@zfscgy
Copy link

zfscgy commented Mar 6, 2023

I use OpenMP for parallel encrypting multiple plaintexts. However, after running for some time, the program crashes with
"getRandomBN: generate random big number error." This error is very weird since:

  • It happens with OpenMP and does not happen when OpenMP is disabled.
  • The error mysteriously disappears when I add some std::cout codes in the function ippGenRandomBN for debugging. I don't know why the error could be related to std::cout.
  • The error happens somewhat randomly, and before the error occurs, all computation is correct.

Finally, I was able to locate the error in the ippsTRNGenRDSEED function of the ipp-crypto lib. After I change the utils.hpp to force RNGenType kRNGenType = RNGenType::PSEUDO, the error seems to be resolved.

@justalittlenoob
Copy link
Contributor

Hi @zfscgy
Thanks for the info. At present, I can't reproduce this problem. If I reproduce this problem, I will fix it.
Can you try to use RNGenType::RDRAND? See if similar problems occur.

@justalittlenoob
Copy link
Contributor

justalittlenoob commented Mar 15, 2023

Hi @zfscgy
This issue is fixed with PR #59 Please have a try with the latest development branch.

@zfscgy
Copy link
Author

zfscgy commented Mar 16, 2023

Hi @zfscgy This issue is fixed with PR #59 Please have a try with the latest development branch.

Thank you very much! I have tried RDRAND and the error also disappeared. I have read PR #59 and it seems that when RDSEED fails it turns to RDRAND.

@zfscgy
Copy link
Author

zfscgy commented Mar 16, 2023

Hi @zfscgy Thanks for the info. At present, I can't reproduce this problem. If I reproduce this problem, I will fix it. Can you try to use RNGenType::RDRAND? See if similar problems occur.

Here is a simplest example:

#include <omp.h>
#include <ipcl/ipcl.hpp>



int main()
{
	ipcl::KeyPair keyPair = ipcl::generateKeypair(2048);
	std::vector<BigNumber> bignums;
	for (size_t i = 0; i < 100; i++)
	{
		bignums.push_back(BigNumber(uint32_t(i)));
	}

	for (size_t t = 0; t < 100; t++)
	{
		std::cout << "Start to encrypt 100 BigNumbers ==== time " << t << std::endl;
#pragma omp parallel for
		for (size_t i = 0; i < 100; i++)
		{
			keyPair.pub_key.encrypt(ipcl::PlainText(bignums[i]));
		}
		std::cout << "Encrypted 100 BigNumbers" << std::endl;
	}

}

And the utils.h is changed as follows:

// 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
static const RNGenType kRNGenType = RNGenType::RDSEED; // Change different random algorithms

Using RDSEED, the runtime error occurs at the first loop or second loop:

Start to encrypt 100 BigNumbers ==== time 0
terminate called after throwing an instance of 'std::runtime_error'
what():
File: /home/zf/projects/third-party/pailliercryptolib/ipcl/utils/common.cpp
Line: 71
Error: getRandomBN: generate random big number error.-2
terminate called recursively

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants