通过Crypto ++生成随机素数

时间:2018-03-18 22:56:39

标签: c++ primes crypto++

我尝试使用1.7390995025634766 depth feature0 feature1 feature2 id date 207555809644681 20180104 1 0.03125 0.038623 0.008130 247833985674646 20180106 1 0.03125 0.004378 0.004065 252945024181083 20180107 1 0.03125 0.062836 0.065041 20180107 2 0.00000 0.001870 0.008130 20180109 1 0.00000 0.001870 0.008130 329567241731951 20180117 1 0.00000 0.041952 0.004065 20180117 2 0.03125 0.003101 0.004065 20180117 3 0.00000 0.030780 0.004065 20180118 1 0.03125 0.003101 0.004065 20180118 2 0.00000 0.030780 0.004065 GenerateRandom生成给定位长(4000b)的随机素数,但我找不到如何使用函数FirstPrime,只签名。结果始终为FirstPrime。第一次尝试是使用0 - 它正在运作,但速度太慢。

PrimeAndGenerator

感谢... r.GenerateRandom(rng, params) ... const int PrimeSelector *pSelector = params.GetValueWithDefault(Name::PointerToPrimeSelector(), (const PrimeSelector *)NULL); Integer equiv = params.GetValueWithDefaul("EquivalentTo", Integer::Zero()); FirstPrime(p, 2*r, equiv, 2*r, pSelector); 的帮助。

1 个答案:

答案 0 :(得分:1)

  

我正在尝试使用GenerateRandom

生成给定位长的随机素数

这可能是最简单的方法。它找到没有特殊形式的素数。如果你想要索菲 - 日耳曼素数或其他类型的素数,你必须做其他事情。

$ cat test.cxx
#include "integer.h"
#include "osrng.h"
#include "nbtheory.h"
#include "hrtimer.h"
#include <iostream>

int main(int argc, char* argv[])
{
    using namespace CryptoPP;

    ThreadUserTimer timer;
    AutoSeededRandomPool prng;
    Integer x;

    timer.StartTimer();

    do {
        x.Randomize(prng, 4096);
    } while(!IsPrime(x));

    double t = timer.ElapsedTimeAsDouble();
    std::cout << "Time: " << t << " seconds" << std::endl;
    std::cout << std::hex << x << std::endl;

    return 0;
}

这是使用Crypto ++的NameValuePairs的更完整的示例。参数名称(如 "BitLength" )来自Integer类和GenerateRandomNoThrow函数。 GenerateRandomNoThrow是所有生成方法都被路由到的地方。

$ cat test.cxx
#include "integer.h"
#include "osrng.h"
#include "hrtimer.h"
#include "algparam.h"
#include <iostream>

int main(int argc, char* argv[])
{
    using namespace CryptoPP;

    Integer x;
    ThreadUserTimer timer;
    AutoSeededRandomPool prng;

    AlgorithmParameters params = MakeParameters("BitLength", 4096)
        ("RandomNumberType", Integer::PRIME);

    timer.StartTimer();
    x.GenerateRandom(prng, params);    
    double t = timer.ElapsedTimeAsDouble();

    std::cout << "Time: " << t << " seconds" << std::endl;
    std::cout << std::hex << x << std::endl;

    return 0;
}

最后,如果您添加<cryptopp/nbtheory.h>,则可以使用:

/// \brief Generates a provable prime
/// \param rng a RandomNumberGenerator to produce keying material
/// \param bits the number of bits in the prime number
/// \returns Integer() meeting Maurer's tests for primality
Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);

/// \brief Generates a provable prime
/// \param rng a RandomNumberGenerator to produce keying material
/// \param bits the number of bits in the prime number
/// \returns Integer() meeting Mihailescu's tests for primality
/// \details Mihailescu's methods performs a search using algorithmic progressions.
Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);

/// \brief Finds a random prime of special form
/// \param p an Integer reference to receive the prime
/// \param max the maximum value
/// \param equiv the equivalence class based on the parameter mod
/// \param mod the modulus used to reduce the equivalence class
/// \param pSelector pointer to a PrimeSelector function for the application to signal suitability
/// \returns true if and only if FirstPrime() finds a prime and returns the prime through p. If FirstPrime()
///   returns false, then no such prime exists and the value of p is undefined
/// \details FirstPrime() uses a fast sieve to find the first probable prime
///   in <tt>{x | p<=x<=max and x%mod==equiv}</tt>
bool FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
  

首次尝试使用PrimeAndGenerator - 它正在运行,但速度太慢。

这都是相对的...你应该找到复杂度大约为 log(N) / N 的素数。

以下是运行在3.1 GHz的Core i5 Skylake的两个时序。它是我用于测试的速度更快的机器之一。

$ ./test.exe
Time: 2.87 seconds
ef55559d8b53a21e566b3d814232fe6d159757a2f6133a6a9ee914ef86b8c0f599dee12672bb3004
484c946fd8e2b32e143ef76ccfd850061cf6545b116709fe35d5a5c0ac9ae793e23439db79ee5202
c5a5d443660fd3e119bfb224a5e8481f0f364871d0f5d78894675d98e755dcf1401b76f6271936b8
b7d4b0d5568590e3be893e3394ac145421dba1127d4cf954ee80ec2de7cd738e1ea439884a141923
06c60c1cd929694404477b6dbb4d71bda24a0ac57cfca0c8e1efa669ab153a89f8f1609556783864
db975ccd9bdfe41c28c5e364306187ee1ad73f8afb1fce1a62ac62e275e77e1575a834424c0bca2f
b2f088b08657ca71ee12895826be19b9eb7b007a0a539e4f8600e564459611d9e0b5691a7972986a
57812bf685be369ba6976e4971cbdaa0dd9110d50ba815141b02462a41b6c7f3e768fb774043033d
e49cf814eaa38acd5139c0cff1451ad504f84104e198e7186b35e2c4d7abffdb559863933fe420d2
3df9dbf9160331d3d67efa7f5ddd1361f7aac6a153b449d0747016e6f0b9bdda8fea3561daf5e8ed
5b1202ad47100c0f6fa49a8e33145b7e7519c67c55ea0c023d58dfec159cf15a72972b16c368cb36
f213a24c7c298cd70cb385b7b486e42f4e3765d727b6ca82147dde069970ed7c0ba06404191b2683
8b40c11c720e465fa9eb72aac4f4abe4c683e5a6d7d570b9c3febf538cbee637h

$ ./test.exe
Time: 0.36 seconds
2533da252e2b55e18438307a507fca9046752e10a05a0819a1a19a8aff44c88f4a85d5e93b4b4e8d
e95e7ec34c2dd2add51b315d307e61cd126620618179c5e65e1a86fb62a8c8dda256cf6dee34b36e
a4953e03b4d5c7510043654bda8ace21f91ac8cfde6f498c3fad2bcdb07ca80ae66fe728cf7b0a08
bf94acae1784e4a8d518a6fe184ba3f75569d8a03dcb2c8281a7df047e6731a1ae33d95cf6e54260
84c33ae61e5c1adc09add84e5a771062af78aa0d91bd14bd6c953c8c4e64faf9bbaef5c619e01bd0
663f446a647a3de78ffb2c82c31cdaac33f8709cadb864e6d3be8d2163086c3e4aa7c079f6c8689f
0fd9f93f1723fc23abaa9d2afdc8bc98b14b58fb1cbdca1fa9872f7d4a540296634bea90a8a209ef
50f71313a4c623a13161cd89be2c6d0cee543ed9e337bab428bcf841ef66194e99c382c7f1aa08ab
ab1934b96773fc405f68449a104b20a60d4772415a199d40b84350bf8a2e54a86f8327c6842f3f4a
47de313deda74fe0ad8a5652ea54761c905f52862d3a24e07f59de2d1fa713c0e55a74b478507754
3599c3c1f8c1346bce1f56d7eabc17155a85e93b3ad94ba6435aa18d6787292ae5eeba816516613d
855b7eea13807585ec9f0ad2eab13bff34e9324059c67ece8bc1db0b07b3bfb7979b62dc5261ebb8
a53c3782b8da8633d52c01847d773f1b62dbb416a910b03862df81d1c5ecc1ddh

以下是赛扬J3455以1.5 GHz运行的两个时序。它是我用于测试的低性能但现代化的机器之一。

$ ./test.exe
Time: 11.94
ae7fb4fa8ca52292e59a636de9d7a7f72082180e2859548a18259773901798fba67ff188aa49e8b7
1bf31978d19f796764c5a57708c56af7468c2994b5ecb02abab34e7f31749bbe937955664561c679
fc03fbb32a526a4689d7eb96019d14865320928f6eeaf7d2db3e22f6db6a1c027ba5a0726e2000ea
0e43b3ca0f7fa53f5e1227eeed38519493e56da2540c6bad0e76a7b3ea50931d4f0b17694cb310c9
2979024be0eb0336eafde548b4d7b34f30d17df6f6129c2379526eadc0167056c9fc0cac6b6ebf19
0aea8963ff16463b8d8acbf751e0261f1f755eac4a24814047450b6dcb7f845005f1c9d42a5f8579
91b180e3991e0cc2a0e7ee0b0ed5781b6ec8007bd80525ecae0b5114ff6bb8bedae92cf7a26bfd97
8451217cf316e7fcfa5bee85550381b351e0e7657a1e8e4e78d850ffc75f24d096f12a6be4060781
fc6fdcbf079e8fdb6177af82aa9f37d13cf9f43f92df3bb460c40c25d2012104ab6097a84ddf34fe
7d952f665da49b52b31b7c5479f833f184928044216f7cf453a06f0963ad78ec31460acb52b7c128
d7182c68a8e81cbde2a04d67fb728ec859ca76da4b6bcab4f97a3921a997b5431a1483b9076c8cb4
1d1f39f96c0aa991778a086a2866e196b4612c6cffe8214a2d6d448d6d2d0177e8a5f4cbe13b71d7
95afb5322228d648d649aa8ed1dd7fd9ae160ee9944fc6d67563fd482ab8673h

$ ./test.exe
Time: 0.22
5be555fef0060fb14b4d3991e16767bb5ad674f65cd43b437e258716a3a096184488107015fba0d2
56460a756245d267522afdfbc6a6eb3ad111a5d802cd6a524b0f9f171b07587969ec48cc6cc02e1d
0a3b88787e260e182dfb0f8dc14bb728cf0bbdefc88c5f357d883ea935e9b13809f8e781aa54ee99
3ec0331974616848480edcf82455c7e69161a02be060bc900997b4e19c21b28b69bf23820317420d
fe860ce88f889ef4d9e70659861cd9926925b993bea33687a3a7289ec8a2ff4cc81387b8756f33b1
9172136586d35b348801b202669a5dc8dc519ba27c6ee49da9703c3c6757ce2a8c8d1b46ce526158
701397151b6a4a70137e982fbd2ab1995edd300cb0347d60293fcfea05f3df048d8cc88c229bfd7d
913eb124bfd167bc38441810d145eb658a7f924c607194b1d8916055139b20f09485b676f22eaca8
4aa812d51d78fb3c87b36e6b134c58009b5729a4df3ca96083df3882f9d88ba6d557df1c398cf0c3
f4ea2b9f1f61a749f602e589d97e784009fdd0b267cdda26b3914324e15dc3efca2372951fdab4f9
8c4546668f095171e0d84623604ef311faa828083dc0a5fe41e3bb4b61f1f7ac475524117e5a36cc
2062e4308d549e9efb57df0be94c4772bb72c938e4f89b5d48103c8f8cc9757dedbc66248eb5ea48
68ea792107451952571d07879e3faf2416dde7070fb18b1bf13a4ea986a2e735h