math_prequisites.tex 11 KB

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