math_prequisites.tex 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. \chapter{Mathematical prequisites \label{chap:preq}}
  2. This chapter formalizes the notation used in the rest of the thesis, and
  3. moreover attempts to discuss and study the elementary functions on which the
  4. project has been grounded. For example, the $\ll$ and $\gg$ are respectively
  5. used with the meaning of left and right binary shift, as usual in the computer
  6. science field; meanwhile, the $\dsqrt$ function will be defined in section
  7. \ref{sec:preq:sqrt}, with the acceptation of discrete square root.
  8. \section{Euclid's Greatest Common Divisor}
  9. Being the greatest common divisor a foundamental algebraic operation in the ssl
  10. protocol, \openssl implemented it with the following signature:
  11. \begin{minted}[fontsize=\small]{c}
  12. int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
  13. \end{minted}
  14. The computation proceeds under the well-known Euclidean algorithm, specifically
  15. the binary variant developed by Josef Stein in 1961 \cite{AOCPv2}. This variant
  16. exploits some interesting properties of $gcd(u, v)$:
  17. \begin{itemize}
  18. \setlength{\itemsep}{1pt}
  19. \setlength{\parskip}{0pt}
  20. \setlength{\parsep}{0pt}
  21. \item if $u,\ v$ are even, then $gcd(u, v) = 2gcd(u/2, v/2)$
  22. \item if $u$ is even and $v$ is odd, then $gcd(u, v) = gcd(u/2, v)$
  23. \item $gcd(u, v) = gcd(u-v, v)$, as in the standard Euclid's algorithm
  24. \item the sum of two odd numbers is always even
  25. \end{itemize}
  26. % Donald Knuth, TAOCP, "a binary method", p. 388 VOL 2
  27. Both \cite{AOCPv2} and \cite{MITalg} analyze the running time for the algorithm,
  28. even if \cite{MITalg}'s demonstration is fairly simpler and proceeds %elegantly
  29. by induction.
  30. Anyway, both show that algorithm ~\ref{alg:gcd} belongs to the class
  31. \bigO{\log b}.
  32. \begin{algorithm}
  33. \caption{\openssl's GCD \label{alg:gcd}}
  34. \begin{algorithmic}[1]
  35. \State $k \gets 0$
  36. \While{$b \neq 0$}
  37. \If{$a$ is odd}
  38. \If{$b$ is odd}
  39. \State $a \gets (a-b) \gg 1$
  40. \Else
  41. \State $b = b \gg 1$
  42. \EndIf
  43. \If{$a < b$} $a, b \gets b, a$ \EndIf
  44. \Else
  45. \If{$b$ is odd}
  46. \State $a = a \gg 1$
  47. \If{$a < b$} $a, b = b, a$ \EndIf
  48. \Else
  49. \State $k = k+1$
  50. \State $a, b = a \gg 1, b \gg 1$
  51. \EndIf
  52. \EndIf
  53. \EndWhile
  54. \State \Return $a \ll k$
  55. \end{algorithmic}
  56. \end{algorithm}
  57. Unfortunately, there is yet no known parallel solution that significantly improves
  58. Euclid's \textsc{gcd}.
  59. \section{RSA Cipher}
  60. XXX.
  61. define RSA, provide the simple keypair generation algorithm.
  62. From now on, except otherwise specified, the variable $N=pq$ will refer to the
  63. public modulis of a generis RSA keypair, with $p, q\ .\ p > q$ being the two primes
  64. factorizing it. Again, $e, d$ will respectively refer to the public exponent and
  65. the private exponent.
  66. \section{Algorithmic Complexity Notation}
  67. The notation used to describe asymptotic complexity follows the $O$-notation,
  68. abused under the conventions and limits of MIT's Introduction to Algorithms.
  69. Let \bigO{g} be the asymptotic upper bound of g:`
  70. $$
  71. O(g(n)) = \{ f(n) : \exists n_0, c \in \naturalN \mid 0 \leq f(n) \leq cg(n)
  72. \ \forall n > n_0 \}
  73. $$
  74. With the writing $f(n) = O(g(n))$ we will actually interpret
  75. $f(n) \in O(g(n))$.
  76. \section{Square Root \label{sec:preq:sqrt}}
  77. Computing the square root has been another foundamental requirement of the
  78. project, though not satisfied by \openssl. Apparently,
  79. % \openssl is a great pile of crap, as phk states
  80. \openssl does only provide the discrete quare root implementation using the
  81. Tonelli/Shanks algorithm, which specifically solves $x$ in $x^2 = a \pmod{p}$,
  82. with $p \in \naturalPrime$.
  83. \begin{minted}{c}
  84. BIGNUM* BN_mod_sqrt(BIGNUM* x, const BIGNUM* a, const BIGNUM* p,
  85. const BN_CTX* ctx);
  86. \end{minted}
  87. Instead, we are interested in finding the square root in $\naturalN$, hence we
  88. did come out with our specific implementation, first using Bombelli's algorithm
  89. described in ~\ref{par:preq:sqrt:bombelli},
  90. and later with Dijkstra one discussed in ~\ref{par:preq:sqrt:dijkstra}.
  91. Unless otherwise specified, in the later pages we will use $\sqrt{n}$ with the
  92. usual meaning ``the half power of $n$'', while with $x, r = \dsqrt{n}$ we will
  93. intend the pair $\angular{x, r} \in \naturalN^2 \mid x^2 + r = n$.
  94. \paragraph{Bombelli's Algorithm \label{par:preq:sqrt:bombelli}} here here here.
  95. \paragraph{Dijkstra's Algorithm \label{par:preq:sqrt:dijkstra}} can be found in
  96. \cite{Dijkstra:adop}, \S 8, p.61. There, Dijkstra presents an elightning
  97. process for the computation of the square root, making only use of binary shift
  98. and algebraic additions.
  99. Specifically, the problem attempts to find, given a natual $n$, the $a$ that
  100. establishes:
  101. \begin{align}
  102. \label{eq:preq:dijkstra_problem}
  103. a^2 \leq n \: \land \: (a+1)^2 > n
  104. \end{align}
  105. Take now the pair $\angular{a=0, b=n+1}$, and think about the problem as dicotomic
  106. search: we want to diminuish the dinstance between the upper bound $b$ and the
  107. lower bound $a$ while holding the guard \ref{eq:preq:dijkstra_problem}:
  108. \begin{align*}
  109. a^2 \leq n \: \land \: b > n
  110. \end{align*}
  111. The speed of convergence is determined by the choice of dinstance $d$, which is optimal when
  112. $d = (b-a) \idiv 2$.
  113. \begin{algorithm}
  114. \caption{Square Root: an intuitive, na\"ive implementation}
  115. \label{alg:sqrt:dijkstra_naif}
  116. \begin{algorithmic}[1]
  117. \State $a, b \gets 0, n+1$
  118. \While{$a+1 \neq b$}
  119. \State $d = (b-a) \idiv 2$
  120. \If{$(a+d)^2 \leq n$}
  121. $a \gets a+d$
  122. \ElsIf{$(b-d)^2 > n$}
  123. $b \gets b-d$
  124. \EndIf
  125. \EndWhile
  126. \State \Return a
  127. \end{algorithmic}
  128. \end{algorithm}
  129. % heh, there's not much to explain here, that's almost the same in Dijkstra's
  130. % book, excluding the inspirative familiar portrait that led to the insight of
  131. % this change of varaibles.
  132. Now optimization proceeds by abstration: the variables $a, b$ are sobstituded
  133. introducing:
  134. $c = b-a$,
  135. $p = ac$,
  136. $q = c^2$,
  137. $r = n-a^2$
  138. and finally $h$ as local optimization. For any further details and
  139. explainations, the reference is still \cite{Dijkstra:adop}.
  140. \begin{algorithm}
  141. \caption{Square Root: final version}
  142. \label{alg:sqrt:dijkstra}
  143. \begin{algorithmic}
  144. \State $p, q, r \gets 0, 1, n$
  145. \While{$q \leq n$} $q \gets q \gg 2$ \EndWhile
  146. \While{$q \neq 1$}
  147. \State $q \gets q \ll 2$
  148. \State $h \gets p+q$
  149. \State $p \gets q \ll 1$
  150. \State $h \gets 2p + q$
  151. \If{$r \geq h$} $p, r \gets p+q, r-h$ \EndIf
  152. \EndWhile
  153. \State \Return p
  154. \end{algorithmic}
  155. \end{algorithm}
  156. A fair approxidmation of the magnitude of the Dijkstra algorithm can be studied
  157. by looking at the pseudocode in ~\ref{alg:sqrt:dijkstra_naif}.Exactely as with
  158. the dicotomic search case, we split the interval $[a, b]$ in half on each step,
  159. and choose wether to take the leftmost or the rightmost part. This results in
  160. $log(n+1)$ steps. During each iteration, instead, as we have seen in
  161. ~\ref{alg:sqrt:dijkstra} we just apply summations and binary shifts, which are
  162. upper bounded by \bigO{\log{n}/2}. Thus, the order of magnitude belongs to
  163. \bigO{\log^2{n}}.
  164. \paragraph{}
  165. Even if both algorithms presented have \emph{asymptotically} the same
  166. complexity, we believe that adopting Dijkstra's one has lead to a pragmatic,
  167. substantial performance improvement.
  168. %%% Local Variables:
  169. %%% mode: latex
  170. %%% TeX-master: "question_authority"
  171. %%% End: