fermat.tex 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 the standard.
  77. Though, it is worth noting that what this limit \emph{states} is that at least
  78. one of the most significant $100$ bits should be different between the two
  79. primes:
  80. \begin{bytefield}[
  81. endianness=big,
  82. bitwidth=1.35em,
  83. % bitformatting=\fakerange,
  84. ]{16}
  85. \\
  86. % \bitheader{}
  87. \\[1px]
  88. \begin{rightwordgroup}{\small{$2^{\frac{\log N}{2}-100}$}}
  89. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  90. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  91. \bitbox{3}{\tiny $\cdots$} &
  92. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  93. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  94. \bitbox{3}{\tiny $\cdots$} & \bitbox{1}{0} & \bitbox{1}{0} &
  95. \end{rightwordgroup}
  96. \\[1ex]
  97. \wordbox[]{1}{} &&
  98. \\[1ex]
  99. \begin{rightwordgroup}{$p$}
  100. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  101. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{1} &
  102. \bitbox{3}{\tiny $\cdots$} &
  103. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  104. \bitbox{1}{0} &
  105. \colorbitbox{lightgray}{1}{1} & \colorbitbox{lightgray}{1}{0} &
  106. \colorbitbox{lightgray}{1}{0} &
  107. \colorbitbox{lightgray}{4}{\tiny{$\cdots$ LSB $\cdots$}} &
  108. \colorbitbox{lightgray}{1}{0} &
  109. \end{rightwordgroup}
  110. \\[1ex]
  111. \begin{rightwordgroup}{$q$}
  112. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  113. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{1} &
  114. \bitbox{3}{\tiny $\cdots$} &
  115. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  116. \bitbox{1}{0} &
  117. \colorbitbox{lightgray}{1}{0} & \colorbitbox{lightgray}{1}{0} &
  118. \colorbitbox{lightgray}{1}{0} &
  119. \colorbitbox{lightgray}{4}{\tiny{$\cdots$ LSB $\cdots$}} &
  120. \colorbitbox{lightgray}{1}{0} &
  121. \end{rightwordgroup}
  122. \end{bytefield}
  123. \vfill
  124. For example, in the case of a RSA key $1024$, the binary difference between $p$
  125. and $q$ has to be greater than $2^{412}$, which means that, excluding corner-cases
  126. where the remainder is involved, there must be at least one difference in the
  127. top 100 most significant bits for the key to be considered safe.
  128. \section{Thoughts about a parallel solution}
  129. At first glance we might be willing to split the entire interval
  130. $\{ \ceil{\sqrt{N}}, \ldots, N-1 \}$ in equal parts, one per each
  131. node. However, this would not be any more efficient than the trial division
  132. algorithm, and nevertheless during each single iteration, the computational
  133. complexity is dominated by the square root $\dsqrt$ function, which belongs to
  134. the class \bigO{\log^2 N}, as we saw in section ~\ref{sec:preq:sqrt}.
  135. Computing separatedly $x^2$ would add an overhead of the same order of magnitude
  136. \bigO{\log^2 N}, and thus result in a complete waste of resources.
  137. %%% Local Variables:
  138. %%% TeX-master: "question_authority.tex"
  139. %%% End: