openGPMP
Open Source Mathematics Package
Public Member Functions | List of all members
gpmp::Factorization Class Reference

#include <factorization.hpp>

Public Member Functions

uint64_t pollard_rho (uint64_t n)
 

Detailed Description

Examples
factors.cpp.

Definition at line 46 of file factorization.hpp.

Member Function Documentation

◆ pollard_rho()

uint64_t gpmp::Factorization::pollard_rho ( uint64_t  n)
Examples
factors.cpp.

Definition at line 78 of file factorization.cpp.

78  {
79  /* initialize random seed */
80  std::mt19937_64 rng(
81  std::chrono::steady_clock::now().time_since_epoch().count());
82 
83  /* no prime divisor for 1 */
84  if (n == 1) {
85  return n;
86  }
87 
88  /* even number means one of the divisors is 2 */
89  if (n % 2 == 0) {
90  return 2;
91  }
92 
93  /* we will pick from the range [2, N) */
94  std::uniform_int_distribution<uint64_t> unif_dist(2, n - 1);
95  uint64_t x = unif_dist(rng);
96  uint64_t y = x;
97 
98  /* the constant in f(x).
99  * Algorithm can be re-run with a different c
100  * if it throws failure for a composite. */
101  std::uniform_int_distribution<uint64_t> c_dist(1, n - 1);
102  uint64_t c = c_dist(rng);
103 
104  /* Initialize candidate divisor (or result) */
105  uint64_t divisor = 1;
106 
107  /* until the prime factor isn't obtained.
108  If n is prime, return n */
109  while (divisor == 1) {
110  /* Tortoise Move: x(i+1) = f(x(i)) */
111  x = __FACT_PRIMES__.mod_pow(x, 2, n) + c;
112  if (x >= n) {
113  x -= n;
114  }
115 
116  /* Hare Move: y(i+1) = f(f(y(i))) */
117  y = __FACT_PRIMES__.mod_pow(y, 2, n) + c;
118  if (y >= n) {
119  y -= n;
120  }
121  y = __FACT_PRIMES__.mod_pow(y, 2, n) + c;
122  if (y >= n) {
123  y -= n;
124  }
125 
126  /* check gcd of |x-y| and n */
127  divisor = std::gcd(std::llabs(x - y), n);
128 
129  /* retry if the algorithm fails to find prime factor
130  * with chosen x and c */
131  if (divisor == n) {
132  return pollard_rho(n);
133  }
134  }
135 
136  return divisor;
137 }
uint64_t pollard_rho(uint64_t n)
uint64_t mod_pow(uint64_t a, uint64_t b, uint64_t m)
Definition: prime_test.cpp:72
gpmp::PrimalityTest __FACT_PRIMES__

References __FACT_PRIMES__, and gpmp::PrimalityTest::mod_pow().

Referenced by main().


The documentation for this class was generated from the following files: