fermat.tex 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. \chapter{Fermat's Factorization Algorithm \label{chap:fermat}}
  2. Excluding the trial division, Fermat's method is the oldest known systematic
  3. method for factorizing integers. Even if its algorithmic complexity is not
  4. among the most efficient, it holds still a practical interest whenever
  5. the two primes are sufficiently close.
  6. Indeed, \cite{DSS2009} \S B.3.6 explicitly recommends that
  7. $|p-q| \geq \sqrt{N}2^{-100}$
  8. for any key of bitlength $1024,\ 2048,\ 3072$ in order to address this kind of
  9. threat.\\
  10. %% it would be nice here to explain that this magic 2^100 is just about wonting
  11. %% the most significant digits to be different.
  12. The basic idea is to attempt to write $N$ as a difference of squares,
  13. \begin{align}
  14. \label{eq:fermat_problem}
  15. x^2 - N = y^2
  16. \end{align}
  17. So, we start by $x = \ceil{\sqrt{N}}$ and check that $x^2-N$ is a perfect
  18. square. If it isn't, we iteratively increment $x$ and check again, until we
  19. find a pair $\angular{x, y}$ satisfying equation \ref{eq:fermat_problem}.
  20. Once found, we claim that $N = pq = (x+y)(x-y)$; it is indeed true that, if we
  21. decompose $x^2 - y^2$ as difference of squares, then it is immediately clear
  22. that $x+y \mid N \ \land \ x-y \mid N$, and that both are non-trivial
  23. divisors.
  24. \paragraph{Complexity} \cite{riesel} contains a detailed proof for the
  25. complexity of this algorithm, which is
  26. $\bigO{\frac{(1-k)^2}{2k} \sqrt{N}} \;\;, 0 < k < 1$. We summarize it down
  27. below here to better clarify the limits of this algorithm.
  28. \begin{proof}
  29. Since, once we reach the final step $x_f$ it holds $N = pq = x_f^2 - y_f^2$,
  30. the number of steps required to reach the result is:
  31. \begin{align*}
  32. x_f - \sqrt{N} &= \frac{p + q}{2} - \sqrt{N} \\
  33. &= \frac{p + \frac{N}{p}}{2} - \sqrt{N} \\
  34. &= \frac{(\sqrt{N} - p)^2}{2p}
  35. \end{align*}
  36. If we finally suppose that $p = k\sqrt{N}, \; 0 < k < 1$, then the number of cycles
  37. becomes
  38. $\frac{(1-k)^2}{2k} \sqrt{N}$.
  39. \end{proof}
  40. \begin{remark}
  41. Note that, for the algorithm to be effective, the two primes must be
  42. ``really close'' to $\sqrt{N}$. As much as the lowest prime gets near to
  43. $1$, the ratio $\frac{(1-k)^2}{2k}$ becomes larger, until the actual magnitude
  44. of this factorization method approaches \bigO{N}.
  45. \end{remark}
  46. \section{An Implementation Perspective \label{sec:fermat:implementation}}
  47. At each iteration, the $i-$th state is hold by the pair $\angular{x, x^2}$.\\
  48. The later step, described by $\angular{x+1, (x+1)^2}$ can be computed efficiently
  49. considering the square of a binomial: $\angular{x+1, (x^2) + (x \ll 1) + 1}$.
  50. The upper-bound, instead, is reached when
  51. $ \Delta = p - q = x + y - x + y = 2y > 2^{-100}\sqrt{N}$.
  52. Algorithm ~\ref{alg:fermat} presents a simple implementation of this
  53. factorization method, taking into account the small optimizations
  54. aforementioned.
  55. \begin{algorithm}[H]
  56. \caption{Fermat Factorization \label{alg:fermat}}
  57. \begin{algorithmic}[1]
  58. \State $x \gets \floor{\sqrt{N}}$
  59. \State $x^2 \gets xx$
  60. \Repeat
  61. \State $x \gets x+1$
  62. \State $x^2 \gets x^2 + x \ll 1 + 1$
  63. \State $y, rest \gets \dsqrt{x^2 - N}$
  64. \Until{ $rest \neq 0 \land y < \frac{\sqrt{N}}{2^{101}}$ }
  65. \If{ $rest = 0$ }
  66. \State $p \gets x+y$
  67. \State $q \gets x-y$
  68. \State \Return $p, q$
  69. \Else
  70. \State \Return \textbf{nil}
  71. \EndIf
  72. \end{algorithmic}
  73. \end{algorithm}
  74. \paragraph{How to chose the upper limit?} Our choice of keeping straight with
  75. the limits of the standard is a mere choice of commodity: we are interested in
  76. finding public keys not respecting it. Though, it it worth noting that what this
  77. limit \emph{states} is that at least one of the most significant $100$ bits
  78. should be different between the two primes:
  79. \begin{bytefield}[
  80. endianness=big,
  81. bitwidth=1.5em,
  82. % bitformatting=\fakerange,
  83. ]{16}
  84. \\
  85. % \bitheader{}
  86. \\[1px]
  87. \begin{rightwordgroup}{$2^{\log{\frac{N}{2}}-100}$}
  88. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  89. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  90. \bitbox{3}{\tiny $\cdots$} &
  91. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  92. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  93. \bitbox{5}{\tiny $\cdots$} & \bitbox{1}{0} & \bitbox{1}{0} &
  94. \end{rightwordgroup}
  95. \\[1ex]
  96. \wordbox[]{1}{} &&
  97. \\[1ex]
  98. \begin{rightwordgroup}{$p$}
  99. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  100. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{1} &
  101. \bitbox{3}{\tiny $\cdots$} &
  102. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  103. \bitbox{1}{0} &
  104. \colorbitbox{lightgray}{1}{1} & \colorbitbox{lightgray}{1}{0} &
  105. \colorbitbox{lightgray}{1}{0} &
  106. \colorbitbox{lightgray}{6}{\tiny{$\cdots$ least signif. bits $\cdots$}} &
  107. \colorbitbox{lightgray}{1}{0} &
  108. \end{rightwordgroup}
  109. \\[1ex]
  110. \begin{rightwordgroup}{$q$}
  111. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  112. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{1} &
  113. \bitbox{3}{\tiny $\cdots$} &
  114. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  115. \bitbox{1}{0} &
  116. \colorbitbox{lightgray}{1}{0} & \colorbitbox{lightgray}{1}{0} &
  117. \colorbitbox{lightgray}{1}{0} &
  118. \colorbitbox{lightgray}{6}{\tiny{$\cdots$ least signif. bits $\cdots$}} &
  119. \colorbitbox{lightgray}{1}{0} &
  120. \end{rightwordgroup}
  121. \end{bytefield}
  122. \vspace{20pt}
  123. For example, in the case of a RSA key $1024$, the binary difference between $p$
  124. and $q$ has to be greater than $2^{412}$, which means that, excluding corner-cases
  125. where the remainder is involved, there must be at least one difference in the
  126. top 100 most significant bits for the key to be considered safe.
  127. \section{Thoughts about a parallel solution}
  128. At first glance we might be willing to split the entire interval
  129. $\{ \ceil{\sqrt{N}}, \ldots, N-1 \}$ in equal parts, one assigned to per each
  130. node. However, this would not be any more efficient than the trial division
  131. algorithm, and nevertheless it is worth noting that during each single iteration,
  132. the computational complexity is dominated by the square root $\dsqrt$ function,
  133. which belongs to the class \bigO{\log^2 N}, as we saw in section
  134. ~\ref{sec:preq:sqrt}. Computing separatedly $x^2$ would add an overhead of the
  135. same order of magnitude \bigO{\log^2 N}, and thus result in a complete waste of
  136. resources.
  137. %%% Local Variables:
  138. %%% TeX-master: "question_authority.tex"
  139. %%% End: