math_prequisites.tex 12 KB

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