aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/make_prime.cpp
blob: 4135bd197e93e33704bab75c0bb9b4e0a7e0276b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <botan/numthry.h>
#include <botan/auto_rng.h>

using namespace Botan;

#include <set>
#include <iostream>
#include <iterator>
#include <map>

int main()
   {
   AutoSeeded_RNG rng;

   std::set<BigInt> primes;

   std::map<int, int> bit_count;

   int not_new = 0;

   while(primes.size() < 10000)
      {
      u32bit start_cnt = primes.size();

      u32bit bits = 18;

      if(rng.next_byte() % 128 == 0)
         bits -= rng.next_byte() % (bits-2);

      bit_count[bits]++;

      //std::cout << "random_prime(" << bits << ")\n";

      BigInt p = random_prime(rng, bits);

      if(p.bits() != bits)
         {
         std::cout << "Asked for " << bits << " got " << p 
                   << " " << p.bits() << " bits\n";
         return 1;
         }

      primes.insert(random_prime(rng, bits));

      if(primes.size() != start_cnt)
         std::cout << primes.size() << "\n";
      else
         not_new++;

      //std::cout << "miss: " << not_new << "\n";

      if(not_new % 100000 == 0)
         {
         for(std::map<int, int>::iterator i = bit_count.begin();
             i != bit_count.end(); ++i)
            std::cout << "bit_count[" << i->first << "] = "
                      << i->second << "\n";
         std::copy(primes.begin(), primes.end(),
                   std::ostream_iterator<BigInt>(std::cout, " "));
         }
      }

   std::cout << "Generated all? primes\n";
   /*
   for(u32bit j = 0; j != PRIME_TABLE_SIZE; ++j)
      {
      if(primes.count(PRIMES[j]) != 1)
         std::cout << "Missing " << PRIMES[j] << "\n";
      }
   */
   }