aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/factor.cpp
blob: 1855c8ad23cda8ccf9025085b532f20f05261cf4 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
   Factor integers using a combination of trial division by small primes,
   and Pollard's Rho algorithm
*/
#include <botan/botan.h>
#include <botan/reducer.h>
#include <botan/numthry.h>
using namespace Botan;

#include <iostream>

BigInt rho(const BigInt& n)
   {
   BigInt x = random_integer(0, n-1);
   BigInt y = x;
   BigInt d = 0;

   Modular_Reducer mod_n(n);

   u32bit i = 1, k = 2;
   while(true)
      {
      i++;

      if(i == 0) // fail
         break;

      x = mod_n.reduce(square(x) - 1);
      d = gcd(y - x, n);
      if(d != 1 && d != n)
         return d;

      if(i == k)
         {
         y = x;
         k = 2*k;
         }
      }
   return 0;
   }

void concat(std::vector<BigInt>& x, const std::vector<BigInt>& y)
   {
   for(u32bit j = 0; j != y.size(); j++)
      x.push_back(y[j]);
   }

std::vector<BigInt> factorize(const BigInt& n)
   {
   std::vector<BigInt> factors;

   if(n <= 1) // no prime factors at all
      return factors;

   if(is_prime(n)) // just n itself
      {
      factors.push_back(n);
      return factors;
      }

   if(n.is_even())
      {
      factors.push_back(2);
      concat(factors, factorize(n / 2));
      return factors;
      }

   BigInt factor = 0;
   while(factor == 0)
      factor = rho(n);

   concat(factors, factorize(factor));
   concat(factors, factorize(n / factor));

   return factors;
   }


int main(int argc, char* argv[])
   {
   if(argc != 2)
      {
      std::cerr << "Usage: " << argv[0] << " integer\n";
      return 1;
      }

   try
      {
      LibraryInitializer init;

      BigInt n(argv[1]);
      std::vector<BigInt> factors = factorize(n);

      std::sort(factors.begin(), factors.end());

      std::cout << n << ": ";
      for(u32bit j = 0; j != factors.size(); j++)
         std::cout << factors[j] << " ";
      std::cout << "\n";
      

      }
   catch(std::exception& e)
      {
      std::cout << e.what() << std::endl;
      return 1;
      }
   return 0;
   }