math_prequisites.tex 13 KB

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