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

#include <logarithms.hpp>

Public Member Functions

uint64_t pollard_rho_log (uint64_t a, uint64_t b, uint64_t m)
 
uint64_t BSGS (uint64_t a, uint64_t b, uint64_t m)
 

Detailed Description

Examples
logarithms.cpp.

Definition at line 47 of file logarithms.hpp.

Member Function Documentation

◆ BSGS()

uint64_t gpmp::Logarithms::BSGS ( uint64_t  a,
uint64_t  b,
uint64_t  m 
)
Examples
logarithms.cpp.

Definition at line 98 of file logarithms.cpp.

98  {
99  uint64_t n = (uint64_t)sqrt(m) + 1;
100 
101  std::unordered_map<uint64_t, uint64_t> value;
102 
103  // store all values of a^(n*i) of LHS
104  for (uint64_t i = n; i >= 1; --i) {
105  value[__LOG_PRIMES__.mod_pow(a, i * n, m)] = i;
106  }
107 
108  for (uint64_t j = 0; j < n; ++j) {
109  // calculate (a ^ j) * b and check
110  uint64_t cur = (__LOG_PRIMES__.mod_pow(a, j, m) * b) % m;
111 
112  // If collision occurs i.e., LHS = RHS
113  if (value[cur]) {
114  uint64_t ans = value[cur] * n - j;
115  // Check whether ans lies below m or not
116  if (ans < m) {
117  return ans;
118  }
119  }
120  }
121  return -1;
122 }
uint64_t mod_pow(uint64_t a, uint64_t b, uint64_t m)
Definition: prime_test.cpp:72
gpmp::PrimalityTest __LOG_PRIMES__
Definition: logarithms.cpp:51

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

Referenced by main().

◆ pollard_rho_log()

uint64_t gpmp::Logarithms::pollard_rho_log ( uint64_t  a,
uint64_t  b,
uint64_t  m 
)
Examples
logarithms.cpp.

Definition at line 58 of file logarithms.cpp.

58  {
59  uint64_t x = rand() % (p - 1) + 1;
60 
61 #ifdef USE_SRAND
62  srand(time(NULL));
63  x = rand() % (p - 1) + 1;
64 #endif
65 
66  uint64_t y1 = y;
67  uint64_t y2 = y;
68  uint64_t d = 1;
69  std::unordered_map<uint64_t, uint64_t> values;
70 
71  // Compute the iterated values of the sequence x_n = g^x_n-1 mod p
72  // until a collision is found
73  while (d == 1) {
74  x = (x * x + 1) % p;
75  y1 = (y1 * g) % p;
76  y2 = (y2 * g) % p;
77  y2 = (y2 * g) % p;
78  d = std::gcd(std::llabs(y2 - y1), p);
79  values[x] = y1;
80  }
81 
82  // Compute the discrete logarithm using the collision point
83  if (d == p) {
84  return -1;
85  } else {
86  uint64_t x0 = std::llabs(y2 - y1) / d;
87  uint64_t y0 = values[x0];
88  uint64_t k = 1;
89  while (y0 != y) {
90  y0 = (y0 * g) % p;
91  k++;
92  }
93  return k * x0 % (p - 1);
94  }
95 }

Referenced by main().


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