pollardrho.tex 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. \chapter{Pollard's $\rho$ factorization method \label{chap:pollardrho}}
  2. A \emph{Monte Carlo} factorization method, published by J. M. Pollard in
  3. ~\cite{pollardMC}, consists into identifying a periodically recurrent sequence
  4. of integers within a random walk $\pmod{N}$ that could leak one of the two
  5. factors.
  6. Consider a function $f$ from $\mathcal{S}$ to $\mathcal{S}$, where
  7. $\mathcal{S} = \{0, 1, \ldots, q-1\}$ and $q \in \naturalPrime$.
  8. Let $s$ be a random element in $\mathcal{S}$, and consider the sequence
  9. \begin{align*}
  10. s,\ f(s),\ f(f(s)),\ \ldots
  11. \end{align*}
  12. Since $f$ acts over a finite set, it is clear that this sequence must
  13. eventually repeat, and become cyclic.
  14. We might diagram it with the letter $\rho$, where the tail represent the
  15. aperiodic part, or \emph{epacts}, and the oval the cyclic part, or
  16. \emph{period}.
  17. \begin{center}
  18. \begin{tikzpicture}[scale=0.7, thick]
  19. \tikzstyle{every node}=[draw,circle,fill=white,minimum size=4pt,
  20. inner sep=0pt]
  21. \node (1) at (1.4, 0.2) [label=left:$x_1$] {};
  22. \node (2) at (2.5, 3) [label=left:$x_{i-2}$] {};
  23. \node (3) at (3.25, 5) [label=left:$x_{i-1}$] {};
  24. \node (4) at (4, 7) [label=left:$ x_i \equiv x_j $] {};
  25. \node (5) at (6, 9) [label=above:$x_{i+1}$] {};
  26. \node (6) at (8, 7) [label=right:$x_{i+2}$] {};
  27. \node (7) at (6, 5) [label=below:$x_{j-1}$] {};
  28. \path (1) edge [dashed] (2);
  29. \path (2) edge (3);
  30. \path (3) edge (4);
  31. \path (4) edge [bend left] (5);
  32. \path (5) edge [bend left] (6);
  33. \path (6) edge [bend left, dashed] (7);
  34. \path (7) edge [bend left] (4);
  35. %%\draw [decorate,decoration={brace, raise=1.5cm}] (1) -- (3)
  36. %%node[draw=no] at (-1.5, 4) {tail};
  37. \draw [decorate,decoration={brace, raise=3cm}] (5) -- (7)
  38. node[draw=none] at (13, 7) {\footnotesize {period $\pmod{q}$}};
  39. \end{tikzpicture}
  40. \end{center}
  41. Now, consider $N=pq$.
  42. Let $F(x)$ be any function generating pseudorandom integers
  43. $\angular{x_1, x_2, \ldots}$, and let $f(x) = F(x) \pmod{q}$.
  44. As we said above, without any luck, there will be a pair $\angular{x_i, x_j}$
  45. generated by $F$ such that $x_i \equiv x_j \pmod{q}$, but $x_i \neq x_j$.
  46. Therefore, in order to factorize $N$, we proceed as follows: starting from a
  47. random $s$, we iteratively apply $F$ reduced modulo $N$. Whenever we find a
  48. period, if $\gcd(x_i - x_j, N) > 1$ then we found a non-trivial
  49. factor of $N$.
  50. \paragraph{Choosing the function} Ideally, $F$ should be easily computable, but
  51. at the same time random enough to reduce as much as possible the epacts
  52. ~\cite{Crandall} \S 5.2.1. Any quadratic function $F(x) = x^2 + b$ should be
  53. enough\footnote{
  54. Note that this has been only empirically verified, and so far not been proved
  55. (~\cite{riesel}, p. 177)},
  56. provided that $b \in \naturalN \setminus \{0, 2\}$.
  57. For example, ~\cite{pollardMC} uses $x^2 -1$, meanwhile we are going to choose
  58. $F(x) = x^2 + 1$.
  59. \paragraph{Finding the period} The trivial way to discover a period would be to
  60. test $x_i$ with all $x_j, \quad j < i$. Though, in \cite{AOCPv2} \S 3.1,
  61. Knuth gives a simple and elegant algorithm, attributed to Floyd, for finding a
  62. multiple of the period.
  63. This algorithm is the same one finally adopted by Pollard in
  64. ~\cite{pollardMC}.
  65. \begin{theorem*}[Floyd]
  66. Given an \emph{ultimately periodic} sequence, in the sense that there exists
  67. numbers $\lambda$ and $\mu$ for which the values:
  68. \begin{align*}
  69. X_0, X_1, \ldots, X_{\mu}, \ldots, X_{\mu + \lambda - 1}
  70. \end{align*}
  71. are distinct, but $X_{n+\lambda} = X_n$ when $n \geq \mu$,
  72. then there exists an
  73. $\mu < n < \mu + \lambda$ such that $X_n = X_{2n}$.
  74. \end{theorem*}
  75. \begin{proof}
  76. First, if $X_n = X_{2n}$, then the sequence is obviously periodic from
  77. $X_{2n}$ onward, possibly even earlier.
  78. Conversely, it is true that $X_n = X_m \quad (n \geq \mu)$ for
  79. $m = n + k\lambda, \quad k \in \naturalN$. Hence, there will eventually
  80. be an $n$ such that $X_n = X_{2n}$ if and only if $n - \mu$ is a multiple of
  81. $\lambda$.
  82. The first such value happens for $n = (\lambda + 1)\floor{\rfrac{\mu}{\lambda}}$.
  83. \end{proof}
  84. The immediate consequence of this is that we can find a collision simply by
  85. checking $\gcd(x_{2i} - x_i, N) > 1$ for incremental $i$.
  86. \paragraph{Brent's Improvement} In 1979, Brent discovered an entire family of
  87. cycle-finding algorithms whose optimal version resulted to be 36\% faster than
  88. Floyd's one \cite{pollard-brent}.
  89. Instead of looking for the period of the sequence using $x_{2i} - x_i$, Brent
  90. considers
  91. $\abs{x_j - x_{2^k}}$ for $ 3 \cdot 2^{k-1} < j \leq 2^{k+1}$, resulting in
  92. fewer operations required by the algorithm. Pragmatically, this boils down to
  93. compare:
  94. \medskip
  95. \begin{tabular}{l@{\hskip 40pt} l@{\hskip 50pt} l}
  96. $k = 0$ & $j \in \{1+1 \ldots 2\}$ & $\abs{x_1 - x_2}$ \\
  97. $k = 1$ & $j \in \{3+1 \ldots 4\}$ & $\abs{x_2 - x_4}$ \\
  98. $k = 2$ & $j \in \{6+1 \ldots 8\}$ & $\abs{x_4 - x_7}$, $\abs{x_4 - x_8}$ \\
  99. $k = 3$ & $j \in \{12+1, \ldots 16\}$ &
  100. $\abs{x_8 - x_{13}}$, $\abs{x_8 -x_{14}}$, $\ldots$, $|x_8 - x_{16}|$\\
  101. $k = 4$ & $j \in \{24+1, \ldots 32 \}$ &
  102. $\abs{x_{16} - x_{25}}$, $\ldots$, $\abs{x_{16} - x_{32}}$ \\[2pt]
  103. $\quad \vdots$ & $\quad \vdots$ & $\quad \quad \vdots$ \\
  104. %\multicolumn{1}{c}{$\vdots$} &
  105. %\multicolumn{1}{c}{$\vdots$} &
  106. %\multicolumn{1}{c}{$\vdots$} \\
  107. \end{tabular}
  108. A Pollard's $\rho$ variant that implements Brent's cycle-finding algorithm
  109. instead of Floyd's one runs around 25\% faster on average
  110. ~\cite{pollard-brent}.
  111. \section{Complexity}
  112. \cite{riesel} presents a nice proof of the \emph{average} complexity of
  113. this algorithm, based on the birthday paradox.
  114. \newtheorem*{birthday}{The Birthday Paradox}
  115. \begin{birthday}
  116. How many persons needs to be selected at random in order that the probability
  117. of at least two of them having the same birthday exceeds $\rfrac{1}{2}$?
  118. \end{birthday}
  119. \begin{proof}[Solution]
  120. The probability that $\epsilon$ different persons have different birthdays is:
  121. \begin{align*}
  122. \Big(1 - \frac{1}{365}\Big)
  123. \Big(1 - \frac{2}{365}\Big)
  124. \Big(1 - \frac{3}{365}\Big)
  125. \cdots
  126. \Big(1 - \frac{\epsilon -1}{365}\Big)
  127. =
  128. \frac{365!}{365^\epsilon (365-\epsilon)!}
  129. \end{align*}
  130. This expression becomes $< \rfrac{1}{2}$ for $\epsilon \geq 23$.
  131. \end{proof}
  132. We can obviously substitute the $365$ with any set of cardinality $\zeta$
  133. to express the probability that a random function from $\integerZ_{\epsilon}$
  134. to $\integerZ_{\zeta}$ is injective. However, back to our particular case,
  135. we want to answer the question:
  136. \emph{
  137. How many random numbers do we have to run through before finding at least
  138. two integers equivalent $\mod{q}$?
  139. }
  140. Using the same reasoning presented above over the previously defined function
  141. $f(x): \mathcal{S} \to \mathcal{S}$, we will discover that after
  142. $\approx 1.18 \sqrt{q}$ steps the probability to have fallen inside the period
  143. is $\rfrac{1}{2}$. %% is it clear that q is either one of the two primes, and
  144. %% that here we want to examinate only a portion of the
  145. %% domain?
  146. Since any of the two primes factoring $N$ is bounded above by $\sqrt{N}$, we
  147. will find a periodic sequence, and thus a factor, in time \bigO{\sqrt[4]{N}}.
  148. \section{An Implementation Perspective}
  149. The initial algorithm described by Pollard \cite{pollardMC} and consultable
  150. immediately below, looks for the pair $\angular{x_i, x_{2i}}$ such that
  151. $\gcd(x_{2i} - x_i, N) > 1$. This is achieved by keeping two variables $x, y$
  152. and respectively updating them via $x \gets f(x)$ and $y \gets f(f(y))$.
  153. \begin{algorithm}
  154. \caption{Pollard's $\rho$ factorization}
  155. \begin{algorithmic}[1]
  156. \Function{rho}{\PKArg}
  157. \State $x \getsRandom \naturalN$
  158. \State $y \gets x$
  159. \State $g \gets 1$
  160. \While{$g = 1$}
  161. \State $x \gets x^2 + 1 \pmod{N}$
  162. \State $y \gets y^4 + 2y^2 + 2 \pmod{N}$
  163. \State $g \gets gcd(|x - y|, N)$
  164. \EndWhile
  165. \If{$g = N$} \Return \strong{nil}
  166. \Else \ \ \Return $g, N//g$
  167. \EndIf
  168. \EndFunction
  169. \end{algorithmic}
  170. \end{algorithm}
  171. \begin{remark}
  172. It is intresting to see how in its basic version, Pollard's $\rho$
  173. method just needs 3 variables to preserve the
  174. state. This places it among the most parsimonious factorization algorithms in
  175. terms of memory footprint.
  176. \end{remark}
  177. An immediate improvement of this algorithm would be to occasionally compute Euclid's
  178. algorithm over the accumulated product to save some computation cycles, just as
  179. we saw in section~\ref{sec:pollard-1:implementing}. The next code fragment - algorithm
  180. \ref{alg:pollardrho} - adopts this trick together with Brent's cycle-finding variant
  181. (\cite{pollard-brent}\S 7).
  182. \paragraph{Parallelism}
  183. Unfortunately, a parallel implementation of the $\rho$ algorithm would not
  184. provide a linear speedup.
  185. The computation of the $x_i$ sequence is intrinsecally serial; the only
  186. plausible approach to parallelism would be to try several different pseudorandom
  187. sequences, in which case $m$ different machines processing $m$ different
  188. sequences in parallel would be no more than \bigO{\sqrt{m}}
  189. efficient (\cite{brent:parallel} \S 3).
  190. \begin{algorithm}
  191. \caption{Pollard-Brent's factorization
  192. \label{alg:pollardrho}}
  193. \begin{algorithmic}[1]
  194. \Function{rho}{\PKArg}
  195. \State $r \gets 1$
  196. \State $q \gets 1$
  197. \Comment the accumulated $\gcd$
  198. \State $g \gets 1$
  199. \State $m \gets 100$
  200. \Comment steps before checking for $\gcd$
  201. \State $y \getsRandom \naturalN_{< N}$
  202. \While{$g = 1$}
  203. \State $x \gets y$
  204. \For{$r \strong{ times }$}
  205. \State $y \gets y^2 + 1 \pmod{N}$
  206. \EndFor
  207. \State $k \gets 0$
  208. \While{$k \leq r \strong{ and } g = 1$}
  209. \State $ys \gets y$
  210. \Comment backup state
  211. \For{$\min\{m, r-k\} \strong{ times }$}
  212. \Comment accumulate values to test later
  213. \State $y \gets y^2 + 1 \pmod{N}$
  214. \State $q \gets q \cdot \abs{x -y} \pmod{N}$
  215. \EndFor
  216. \State $k \gets k + m$
  217. \State $g \gets \gcd(q, N)$
  218. \EndWhile
  219. \State $r \gets r \ll 1$
  220. \EndWhile
  221. \If{$g = N$}
  222. \Comment too far; fall back to latest epoch
  223. \Repeat
  224. \State $ys \gets ys^2 + 1 \pmod{N}$
  225. \State $g \gets \gcd(N, \abs{x -ys})$
  226. \Until{$g > 1$} \EndIf
  227. \If{$g = 1$} \Return \strong{nil}
  228. \Else \ \ \Return $g, N//g$
  229. \EndIf
  230. \EndFunction
  231. \end{algorithmic}
  232. \end{algorithm}
  233. %%% Local Variables:
  234. %%% mode: latex
  235. %%% TeX-master: "question_authority"
  236. %%% End: