math_prequisites.tex 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. \chapter{Mathematical prequisites \label{chap:preq}}
  2. In this chapter we formalize the notation used in the rest of the thesis, and
  3. furthermore attempt to discuss and study the elementary functions on which the
  4. project has been grounded.
  5. \\
  6. The $\ll$ and $\gg$ are respectively used with the meaning of left and right
  7. bitwise shift, as usual in computer science.
  8. \\
  9. The $\dsqrt$ function will be defined in section \ref{sec:preq:sqrt}, with the
  10. acceptation of discrete square root.
  11. \\
  12. The logarithmic $\log$ function is assumed to be in base two, i.e. $\log_2$.
  13. \\
  14. The $\idiv$ symbol is the integer division over $\naturalN$, i.e.
  15. $a \idiv b = \floor{\frac{a}{b}}$.
  16. \\
  17. $\naturalPrime \subset \naturalN$ is the set containing all prime intgers.
  18. \section{Algorithmic Complexity Notation}
  19. The notation used to describe asymptotic complexity follows the $O$-notation,
  20. abused under the conventions and limits of MIT's Introduction to Algorithms.
  21. Let \bigO{g} be the asymptotic upper bound of g:
  22. $$
  23. \bigO{g(n)} = \{ f(n) : \exists n_0, c \in \naturalN \mid 0 \leq f(n) \leq cg(n)
  24. \ \forall n > n_0 \}
  25. $$
  26. With $f(n) = \bigO{g(n)}$ we actually mean
  27. $f(n) \in \bigO{g(n)}$.
  28. \section{Euclid's Greatest Common Divisor \label{sec:preq:gcd}}
  29. Being the greatest common divisor a foundamental algebraic operation in the TLS
  30. protocol, \openssl implemented it with the following signature:
  31. \begin{minted}[fontsize=\small]{c}
  32. int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
  33. \end{minted}
  34. The computation proceeds under the well-known Euclidean algorithm, specifically
  35. the binary variant developed by Josef Stein in 1961 \cite{AOCPv2}. This variant
  36. exploits some interesting properties of $gcd(a, b)$:
  37. \begin{itemize}
  38. \setlength{\itemsep}{1pt}
  39. \setlength{\parskip}{0pt}
  40. \setlength{\parsep}{0pt}
  41. \item if $a,\ b$ are even, then $gcd(a, b) = 2gcd(a/2, b/2)$;
  42. \item if $a$ is even and $b$ is odd, then $gcd(a, b) = gcd(a/2, b)$;
  43. \item $gcd(a, b) = gcd(a-b, b)$, as in the standard Euclid algorithm;
  44. \item the sum of two odd numbers is always even.
  45. \end{itemize}
  46. % Donald Knuth, TAOCP, "a binary method", p. 388 VOL 2
  47. Both \cite{AOCPv2} and \cite{MITalg} analyze the running time of the
  48. algorithm; \cite{MITalg}'s proof is fairly simpler and proceeds %elegantly
  49. by induction.
  50. Anyway, both show that algorithm ~\ref{alg:gcd} belongs to the class
  51. \bigO{\log b}.
  52. \begin{algorithm}[H]
  53. \caption{\openssl's GCD \label{alg:gcd}}
  54. \begin{algorithmic}[1]
  55. \State $k \gets 0$
  56. \While{$b \neq 0$}
  57. \If{$a$ is odd}
  58. \If{$b$ is odd}
  59. \State $a \gets (a-b) \gg 1$
  60. \Else
  61. \State $b \gets b \gg 1$
  62. \EndIf
  63. \If{$a < b$} $a, b \gets b, a$ \EndIf
  64. \Else
  65. \If{$b$ is odd}
  66. \State $a \gets a \gg 1$
  67. \If{$a < b$} $a, b \gets b, a$ \EndIf
  68. \Else
  69. \State $k \gets k+1$
  70. \State $a, b \gets a \gg 1, b \gg 1$
  71. \EndIf
  72. \EndIf
  73. \EndWhile
  74. \State \Return $a \ll k$
  75. \end{algorithmic}
  76. \end{algorithm}
  77. Unfortunately, there is yet no known parallel solution that significantly improves
  78. Euclid's \textsc{gcd}.
  79. \section{Square Root \label{sec:preq:sqrt}}
  80. Computing the square root is another important building block of the project,
  81. though not available in \openssl\!.
  82. Apparently,
  83. % \openssl is a great pile of crap, as phk states
  84. \openssl does only provide the discrete square root implementation using the
  85. Tonelli/Shanks algorithm, which specifically solves in $x$ the equation
  86. $x^2 = a \pmod{p}$, with $p \in \naturalPrime$:
  87. \begin{minted}{c}
  88. BIGNUM* BN_mod_sqrt(BIGNUM* x, const BIGNUM* a, const BIGNUM* p,
  89. const BN_CTX* ctx);
  90. \end{minted}
  91. Instead, we are interested in finding the the pair
  92. $\angular{x, r} \in \naturalN^2 \mid x^2 + r = n$, that is, the integer part of
  93. the square root of a natural number and its rest.
  94. Hence, we did come out with our specific implementation, first using Bombelli's
  95. algorithm, and later with the one of Dijkstra. Both are going to be discussed
  96. below.
  97. Unless otherwise specified, in the later pages we use $\sqrt{n}$ with the
  98. usual meaning ``the half power of $n$'', while with $x, r = \dsqrt{n}$ we mean
  99. the pair just defined.
  100. \paragraph{Bombelli's Algorithm \label{par:preq:sqrt:bombelli}} dates back to
  101. the XVI century, and approaches the problem of finding the square root by using
  102. continued fractions. Unfortunately, we weren't able to fully assert the
  103. correctness of the algorithm, since the original document
  104. ~\cite{bombelli:algebra} presents a difficult, inconvenient notation. Though,
  105. for completeness' sake, we report in table
  106. ~\ref{alg:sqrt:bombelli} the pseudocode adopted and tested for its correctness.
  107. \begin{algorithm}[H]
  108. \caption{Square Root: Bombelli's algorithm}
  109. \label{alg:sqrt:bombelli}
  110. \begin{algorithmic}[1]
  111. \Procedure{sqrt}{$n$}
  112. \State $i, g \gets 0, \{\}$
  113. \While{$n > 0$}
  114. \State $g_i \gets n \pmod{100}$
  115. \State $n \gets n // 100$
  116. \State $i++$
  117. \EndWhile
  118. \State $x, r \gets 0, 0$
  119. \For{$j \in \; [i-1..0]$}
  120. \State $r = 100r + g_i$
  121. \For{$d \in \; [0, 9]$}
  122. \State $y' \gets d(20x + d)$
  123. \If{$y' > r$} \textbf{break}
  124. \Else \ \ $y \gets y'$
  125. \EndIf
  126. \EndFor
  127. \State $r \gets r - y$
  128. \State $x \gets 10x + d - 1$
  129. \EndFor
  130. \State \Return $x, r$
  131. \EndProcedure
  132. \end{algorithmic}
  133. \end{algorithm}
  134. For each digit of the result, we perform a subtraction, and a limited number of
  135. multiplications. This means that the complexity of this solutions belongs to
  136. \bigO{\log n \log n} = \bigO{\log^2 n}.
  137. \begin{remark}
  138. Note that Bombelli actually has found a solution in $x$ for a slightly
  139. different equation than the one we initially formulated. Specifically, he
  140. found the pair $\angular{x, r}$ such that $(x+r)^2=a$, where $x$ is the mantissa,
  141. while $r$ is the decimal part. For our purpose this change is irrelevant: we
  142. just need to be able to distinguish perfect squares, and thus assert that $r$
  143. is nonzero.
  144. \end{remark}
  145. \paragraph{Dijkstra's Algorithm \label{par:preq:sqrt:dijkstra}} can be found in
  146. \cite{Dijkstra:adop}, \S 8, p.61. There, Dijkstra presents an elightning
  147. process for the computation of the square root, making only use of binary shift
  148. and algebraic additions.
  149. Specifically, the problem attempts to find, given a natual $n$, the integer $a$
  150. that establishes:
  151. \begin{align}
  152. \label{eq:preq:dijkstra_problem}
  153. a^2 \leq n \: \land \: (a+1)^2 > n
  154. \end{align}
  155. Take now the pair $\angular{a=0, b=n+1}$, and consider the inverval
  156. $[a, b[$. We would like to reduce the distance between the upper bound $b$ and
  157. the lower bound $a$, while holding the guard \ref{eq:preq:dijkstra_problem}:
  158. \begin{align*}
  159. a^2 \leq n \: \land \: b > n
  160. \end{align*}
  161. %% XXX. I am not so sure about this, pure fantasy.
  162. The speed of convergence is determined by the choice of the distance $d$, which
  163. analougously to the dicotomic search problem, is optimal when
  164. $d = (b-a) \idiv 2$.
  165. \begin{algorithm}[H]
  166. \caption{Square Root: an intuitive, na\"ive implementation}
  167. \label{alg:sqrt:dijkstra_naif}
  168. \begin{algorithmic}[1]
  169. \State $a, b \gets 0, n+1$
  170. \While{$a+1 \neq b$}
  171. \State $d \gets (b-a) \idiv 2$
  172. \If{$(a+d)^2 \leq n$}
  173. $a \gets a+d$
  174. \ElsIf{$(b-d)^2 > n$}
  175. $b \gets b-d$
  176. \EndIf
  177. \EndWhile
  178. \State \Return a
  179. \end{algorithmic}
  180. \end{algorithm}
  181. % heh, there's not much to explain here, that's almost the same in Dijkstra's
  182. % book, excluding the inspirative familiar portrait that led to the insight of
  183. % this change of varaibles.
  184. Now optimization proceeds with the following change of variables:
  185. $c = b-a$,
  186. $p = ac$,
  187. $q = c^2$,
  188. $r = n-a^2$;
  189. For any further details and explainations, the reference is still
  190. \cite{Dijkstra:adop}.
  191. \begin{algorithm}[H]
  192. \caption{Square Root: final version}
  193. \label{alg:sqrt:dijkstra}
  194. \begin{algorithmic}[1]
  195. \State $p, q, r \gets 0, 1, n$
  196. \While{$q \leq n$} $q \gets q \gg 2$ \EndWhile
  197. \While{$q \neq 1$}
  198. \State $q \gets q \ll 2$
  199. \State $h \gets p+q$
  200. \State $p \gets q \ll 1$
  201. \State $h \gets 2p + q$
  202. \If{$r \geq h$} $p, r \gets p+q, r-h$ \EndIf
  203. \EndWhile
  204. \State \Return p
  205. \end{algorithmic}
  206. \end{algorithm}
  207. A fair approximation of the magnitude of the Dijkstra algorithm can be studied
  208. by looking at the pseudocode in ~\ref{alg:sqrt:dijkstra_naif}. Exactly as in
  209. the dicotomic search case, we split the interval $[a, b]$ in half on each step,
  210. and choose whether to take the leftmost or the rightmost part. This results in
  211. $log(n+1)$ steps. During each iteration, instead, as we have seen in
  212. ~\ref{alg:sqrt:dijkstra} we just apply summations and binary shifts, which are
  213. upper bounded by \bigO{\log{n}/2}. Thus, the order of magnitude belongs to
  214. \bigO{\log^2{n}}.
  215. \paragraph{}
  216. Even if both algorithms presented have \emph{asymptotically} the same
  217. complexity, we believe that adopting the one of Dijkstra has lead to a
  218. pragmatic, substantial performance improvement.
  219. \section{RSA Cipher \label{sec:preq:rsa}}
  220. The RSA cryptosystem, invented by Ron Rivest, Adi Shamir, and Len Adleman
  221. ~\cite{rsa}, was first published in August 1977's issue of
  222. \emph{Scientific American}. In its basic version, this \emph{asymmetric} cipher
  223. works as follows:
  224. \begin{itemize}
  225. \item choose a pair $\angular{p, q}$ of \emph{random} \emph{prime} numbers;
  226. let $N$ be the product of the two, $N=pq$, and call it \emph{public modulus};
  227. \item choose a pair $\angular{e, d}$ of \emph{random} numbers, both in
  228. $\integerZ^*_{\varphi(N)}$, such that one is the multiplicative inverse of the
  229. other, $ed \equiv 1 \pmod{\varphi(N)}$ and $\varphi(N)$ is Euler's totient
  230. function;
  231. \end{itemize}
  232. Now, call $\angular{N, e}$ \emph{public key}, and $\angular{N, d}$
  233. \emph{private key}, and let the encryption function $E(m)$ be the $e$-th power of
  234. the message $m$:
  235. \begin{align}
  236. \label{eq:rsa:encrypt}
  237. E(m) = m^e \pmod{N}
  238. \end{align}
  239. while the decryption function $D(c)$ is the $d$-th power of the ciphertext $c$:
  240. \begin{align}
  241. \label{eq:rsa:decrypt}
  242. D(c) = c^d \equiv E(m)^d \equiv m^{ed} \equiv m \pmod{N}
  243. \end{align}
  244. that, due to Fermat's little theorem, is the inverse of $E$.
  245. \paragraph{}
  246. %% less unless <https://www.youtube.com/watch?v=XnbnuY7Kxhc>
  247. From now on, unless otherwise specified, the variable $N=pq$ will always refer
  248. to the public modulus of a generic RSA keypair, with
  249. $p, q$ being the two primes factorizing it, such that $p > q$.
  250. Again, $e, d$ will respectively refer to the public
  251. exponent and the private exponent.
  252. %%% Local Variables:
  253. %%% mode: latex
  254. %%% TeX-master: "question_authority"
  255. %%% End: