math_prequisites.tex 11 KB

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