fermat.tex 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. \chapter{Fermat's factorization method \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 + 2x + 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. \Function{fermat}{\PKArg}
  59. \State $x \gets \floor{\sqrt{N}}$
  60. \State $x' \gets x \cdot x$
  61. \Repeat
  62. \State $x' \gets x' + 2x + 1$
  63. \State $x \gets x+1$
  64. \State $y, rest \gets \dsqrt{x' - N}$
  65. \Until{ $rest \neq 0 \strong{ and } y < \frac{\sqrt{N}}{2^{101}}$ }
  66. \Comment i.e., \ref{eq:fermat_problem} holds?
  67. \If{ $rest = 0$ }
  68. \State $p \gets x+y$
  69. \State $q \gets x-y$
  70. \State \Return $p, q$
  71. \Else
  72. \State \Return \textbf{nil}
  73. \EndIf
  74. \EndFunction
  75. \end{algorithmic}
  76. \end{algorithm}
  77. \paragraph{How to chose the upper limit?} Our choice of keeping straight with
  78. the limits of the standard is a mere choice of commodity: we are interested in
  79. finding public keys not respecting the standard.
  80. Though, it is worth noting that what this limit \emph{states} is that at least
  81. one of the most significant $100$ bits should be different between the two
  82. primes:
  83. \begin{bytefield}[
  84. endianness=big,
  85. bitwidth=1.35em,
  86. % bitformatting=\fakerange,
  87. ]{16}
  88. \\
  89. % \bitheader{}
  90. \\[1px]
  91. \begin{rightwordgroup}{\small{$2^{\frac{\log N}{2}-100}$}}
  92. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  93. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  94. \bitbox{3}{\tiny $\cdots$} &
  95. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} &
  96. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  97. \bitbox{3}{\tiny $\cdots$} & \bitbox{1}{0} & \bitbox{1}{0} &
  98. \end{rightwordgroup}
  99. \\[1ex]
  100. \wordbox[]{1}{} &&
  101. \\[1ex]
  102. \begin{rightwordgroup}{$p$}
  103. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  104. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{1} &
  105. \bitbox{3}{\tiny $\cdots$} &
  106. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  107. \bitbox{1}{0} &
  108. \colorbitbox{lightgray}{1}{1} & \colorbitbox{lightgray}{1}{0} &
  109. \colorbitbox{lightgray}{1}{0} &
  110. \colorbitbox{lightgray}{4}{\tiny{$\cdots$ LSB $\cdots$}} &
  111. \colorbitbox{lightgray}{1}{0} &
  112. \end{rightwordgroup}
  113. \\[1ex]
  114. \begin{rightwordgroup}{$q$}
  115. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  116. \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{0} & \bitbox{1}{1} &
  117. \bitbox{3}{\tiny $\cdots$} &
  118. \bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}{0} & \bitbox{1}{0} &
  119. \bitbox{1}{0} &
  120. \colorbitbox{lightgray}{1}{0} & \colorbitbox{lightgray}{1}{0} &
  121. \colorbitbox{lightgray}{1}{0} &
  122. \colorbitbox{lightgray}{4}{\tiny{$\cdots$ LSB $\cdots$}} &
  123. \colorbitbox{lightgray}{1}{0} &
  124. \end{rightwordgroup}
  125. \end{bytefield}
  126. \vfill
  127. For example, in the case of a RSA key $1024$, the binary difference between $p$
  128. and $q$ has to be greater than $2^{412}$, which means that, excluding corner-cases
  129. where the remainder is involved, there must be at least one difference in the
  130. top 100 most significant bits for the key to be considered safe.
  131. \section{Thoughts about a parallel solution}
  132. At first glance we might be willing to split the entire interval
  133. $\{ \ceil{\sqrt{N}}, \ldots, N-1 \}$ in equal parts, one per each
  134. node. However, this would not be any more efficient than the trial division
  135. algorithm, and nevertheless during each single iteration, the computational
  136. complexity is dominated by the square root $\dsqrt$ function, which belongs to
  137. the class \bigO{\log^2 N}, as we saw in section ~\ref{sec:preq:sqrt}.
  138. Computing separatedly $x^2$ would add an overhead of the same order of magnitude
  139. \bigO{\log^2 N}, and thus result in a complete waste of resources.
  140. %%As a result of this, we advice the use of a strictly limited number of
  141. %%processors - like two or three - performing in parallel fermat's factorization
  142. %%method over different intervals.
  143. %%% Local Variables:
  144. %%% TeX-master: "question_authority.tex"
  145. %%% End: