1\documentclass[synpaper]{book}
2\usepackage{hyperref}
3\usepackage{makeidx}
4\usepackage{amssymb}
5\usepackage{color}
6\usepackage{alltt}
7\usepackage{graphicx}
8\usepackage{layout}
9\def\union{\cup}
10\def\intersect{\cap}
11\def\getsrandom{\stackrel{\rm R}{\gets}}
12\def\cross{\times}
13\def\cat{\hspace{0.5em} \| \hspace{0.5em}}
14\def\catn{$\|$}
15\def\divides{\hspace{0.3em} | \hspace{0.3em}}
16\def\nequiv{\not\equiv}
17\def\approx{\raisebox{0.2ex}{\mbox{\small $\sim$}}}
18\def\lcm{{\rm lcm}}
19\def\gcd{{\rm gcd}}
20\def\log{{\rm log}}
21\def\ord{{\rm ord}}
22\def\abs{{\mathit abs}}
23\def\rep{{\mathit rep}}
24\def\mod{{\mathit\ mod\ }}
25\renewcommand{\pmod}[1]{\ ({\rm mod\ }{#1})}
26\newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor}
27\newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil}
28\def\Or{{\rm\ or\ }}
29\def\And{{\rm\ and\ }}
30\def\iff{\hspace{1em}\Longleftrightarrow\hspace{1em}}
31\def\implies{\Rightarrow}
32\def\undefined{{\rm ``undefined"}}
33\def\Proof{\vspace{1ex}\noindent {\bf Proof:}\hspace{1em}}
34\let\oldphi\phi
35\def\phi{\varphi}
36\def\Pr{{\rm Pr}}
37\newcommand{\str}[1]{{\mathbf{#1}}}
38\def\F{{\mathbb F}}
39\def\N{{\mathbb N}}
40\def\Z{{\mathbb Z}}
41\def\R{{\mathbb R}}
42\def\C{{\mathbb C}}
43\def\Q{{\mathbb Q}}
44\definecolor{DGray}{gray}{0.5}
45\newcommand{\emailaddr}[1]{\mbox{$<${#1}$>$}}
46\def\twiddle{\raisebox{0.3ex}{\mbox{\tiny $\sim$}}}
47\def\gap{\vspace{0.5ex}}
48\makeindex
49\begin{document}
50\frontmatter
51\pagestyle{empty}
52\title{LibTomMath User Manual \\ v0.40}
53\author{Tom St Denis \\ tomstdenis@gmail.com}
54\maketitle
55This text, the library and the accompanying textbook are all hereby placed in the public domain.  This book has been 
56formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.
57
58\vspace{10cm}
59
60\begin{flushright}Open Source.  Open Academia.  Open Minds.
61
62\mbox{ }
63
64Tom St Denis,
65
66Ontario, Canada
67\end{flushright}
68
69\tableofcontents
70\listoffigures
71\mainmatter
72\pagestyle{headings}
73\chapter{Introduction}
74\section{What is LibTomMath?}
75LibTomMath is a library of source code which provides a series of efficient and carefully written functions for manipulating
76large integer numbers.  It was written in portable ISO C source code so that it will build on any platform with a conforming
77C compiler.  
78
79In a nutshell the library was written from scratch with verbose comments to help instruct computer science students how
80to implement ``bignum'' math.  However, the resulting code has proven to be very useful.  It has been used by numerous 
81universities, commercial and open source software developers.  It has been used on a variety of platforms ranging from
82Linux and Windows based x86 to ARM based Gameboys and PPC based MacOS machines.  
83
84\section{License}
85As of the v0.25 the library source code has been placed in the public domain with every new release.  As of the v0.28
86release the textbook ``Implementing Multiple Precision Arithmetic'' has been placed in the public domain with every new
87release as well.  This textbook is meant to compliment the project by providing a more solid walkthrough of the development
88algorithms used in the library.
89
90Since both\footnote{Note that the MPI files under mtest/ are copyrighted by Michael Fromberger.  They are not required to use LibTomMath.} are in the 
91public domain everyone is entitled to do with them as they see fit.
92
93\section{Building LibTomMath}
94
95LibTomMath is meant to be very ``GCC friendly'' as it comes with a makefile well suited for GCC.  However, the library will
96also build in MSVC, Borland C out of the box.  For any other ISO C compiler a makefile will have to be made by the end
97developer.  
98
99\subsection{Static Libraries}
100To build as a static library for GCC issue the following
101\begin{alltt}
102make
103\end{alltt}
104
105command.  This will build the library and archive the object files in ``libtommath.a''.  Now you link against 
106that and include ``tommath.h'' within your programs.  Alternatively to build with MSVC issue the following
107\begin{alltt}
108nmake -f makefile.msvc
109\end{alltt}
110
111This will build the library and archive the object files in ``tommath.lib''.  This has been tested with MSVC 
112version 6.00 with service pack 5.  
113
114\subsection{Shared Libraries}
115To build as a shared library for GCC issue the following
116\begin{alltt}
117make -f makefile.shared
118\end{alltt}
119This requires the ``libtool'' package (common on most Linux/BSD systems).  It will build LibTomMath as both shared
120and static then install (by default) into /usr/lib as well as install the header files in /usr/include.  The shared 
121library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''.  Generally 
122you use libtool to link your application against the shared object.  
123
124There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile.  It requires 
125Cygwin to work with since it requires the auto-export/import functionality.  The resulting DLL and import library 
126``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin.
127
128\subsection{Testing}
129To build the library and the test harness type
130
131\begin{alltt}
132make test
133\end{alltt}
134
135This will build the library, ``test'' and ``mtest/mtest''.  The ``test'' program will accept test vectors and verify the
136results.  ``mtest/mtest'' will generate test vectors using the MPI library by Michael Fromberger\footnote{A copy of MPI
137is included in the package}.  Simply pipe mtest into test using
138
139\begin{alltt}
140mtest/mtest | test
141\end{alltt}
142
143If you do not have a ``/dev/urandom'' style RNG source you will have to write your own PRNG and simply pipe that into 
144mtest.  For example, if your PRNG program is called ``myprng'' simply invoke
145
146\begin{alltt}
147myprng | mtest/mtest | test
148\end{alltt}
149
150This will output a row of numbers that are increasing.  Each column is a different test (such as addition, multiplication, etc)
151that is being performed.  The numbers represent how many times the test was invoked.  If an error is detected the program
152will exit with a dump of the relevent numbers it was working with.
153
154\section{Build Configuration}
155LibTomMath can configured at build time in three phases we shall call ``depends'', ``tweaks'' and ``trims''.  
156Each phase changes how the library is built and they are applied one after another respectively.  
157
158To make the system more powerful you can tweak the build process.  Classes are defined in the file
159``tommath\_superclass.h''.  By default, the symbol ``LTM\_ALL'' shall be defined which simply 
160instructs the system to build all of the functions.  This is how LibTomMath used to be packaged.  This will give you 
161access to every function LibTomMath offers.
162
163However, there are cases where such a build is not optional.  For instance, you want to perform RSA operations.  You 
164don't need the vast majority of the library to perform these operations.  Aside from LTM\_ALL there is 
165another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt.  Additional 
166classes can be defined base on the need of the user.
167
168\subsection{Build Depends}
169In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs''
170which further define symbols.  All of the symbols (technically they're macros $\ldots$) represent a given C source
171file.  For instance, BN\_MP\_ADD\_C represents the file ``bn\_mp\_add.c''.  When a define has been enabled the
172function in the respective file will be compiled and linked into the library.  Accordingly when the define
173is absent the file will not be compiled and not contribute any size to the library.
174
175You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice).  
176This is to help resolve as many dependencies as possible.  In the last pass the symbol LTM\_LAST will be defined.  
177This is useful for ``trims''.
178
179\subsection{Build Tweaks}
180A tweak is an algorithm ``alternative''.  For example, to provide tradeoffs (usually between size and space).
181They can be enabled at any pass of the configuration phase.
182
183\begin{small}
184\begin{center}
185\begin{tabular}{|l|l|}
186\hline \textbf{Define} & \textbf{Purpose} \\
187\hline BN\_MP\_DIV\_SMALL & Enables a slower, smaller and equally \\
188                          & functional mp\_div() function \\
189\hline
190\end{tabular}
191\end{center}
192\end{small}
193
194\subsection{Build Trims}
195A trim is a manner of removing functionality from a function that is not required.  For instance, to perform
196RSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed.  
197Build trims are meant to be defined on the last pass of the configuration which means they are to be defined
198only if LTM\_LAST has been defined.
199
200\subsubsection{Moduli Related}
201\begin{small}
202\begin{center}
203\begin{tabular}{|l|l|}
204\hline \textbf{Restriction} & \textbf{Undefine} \\
205\hline Exponentiation with odd moduli only & BN\_S\_MP\_EXPTMOD\_C \\
206                                           & BN\_MP\_REDUCE\_C \\
207                                           & BN\_MP\_REDUCE\_SETUP\_C \\
208                                           & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
209                                           & BN\_FAST\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
210\hline Exponentiation with random odd moduli & (The above plus the following) \\
211                                           & BN\_MP\_REDUCE\_2K\_C \\
212                                           & BN\_MP\_REDUCE\_2K\_SETUP\_C \\
213                                           & BN\_MP\_REDUCE\_IS\_2K\_C \\
214                                           & BN\_MP\_DR\_IS\_MODULUS\_C \\
215                                           & BN\_MP\_DR\_REDUCE\_C \\
216                                           & BN\_MP\_DR\_SETUP\_C \\
217\hline Modular inverse odd moduli only     & BN\_MP\_INVMOD\_SLOW\_C \\
218\hline Modular inverse (both, smaller/slower) & BN\_FAST\_MP\_INVMOD\_C \\
219\hline
220\end{tabular}
221\end{center}
222\end{small}
223
224\subsubsection{Operand Size Related}
225\begin{small}
226\begin{center}
227\begin{tabular}{|l|l|}
228\hline \textbf{Restriction} & \textbf{Undefine} \\
229\hline Moduli $\le 2560$ bits              & BN\_MP\_MONTGOMERY\_REDUCE\_C \\
230                                           & BN\_S\_MP\_MUL\_DIGS\_C \\
231                                           & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
232                                           & BN\_S\_MP\_SQR\_C \\
233\hline Polynomial Schmolynomial            & BN\_MP\_KARATSUBA\_MUL\_C \\
234                                           & BN\_MP\_KARATSUBA\_SQR\_C \\
235                                           & BN\_MP\_TOOM\_MUL\_C \\ 
236                                           & BN\_MP\_TOOM\_SQR\_C \\
237
238\hline
239\end{tabular}
240\end{center}
241\end{small}
242
243
244\section{Purpose of LibTomMath}
245Unlike  GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with 
246bleeding edge performance in mind.  First and foremost LibTomMath was written to be entirely open.  Not only is the 
247source code public domain (unlike various other GPL/etc licensed code), not only is the code freely downloadable but the
248source code is also accessible for computer science students attempting to learn ``BigNum'' or multiple precision
249arithmetic techniques. 
250
251LibTomMath was written to be an instructive collection of source code.  This is why there are many comments, only one
252function per source file and often I use a ``middle-road'' approach where I don't cut corners for an extra 2\% speed
253increase.
254
255Source code alone cannot really teach how the algorithms work which is why I also wrote a textbook that accompanies
256the library (beat that!).
257
258So you may be thinking ``should I use LibTomMath?'' and the answer is a definite maybe.  Let me tabulate what I think
259are the pros and cons of LibTomMath by comparing it to the math routines from GnuPG\footnote{GnuPG v1.2.3 versus LibTomMath v0.28}.
260
261\newpage\begin{figure}[here]
262\begin{small}
263\begin{center}
264\begin{tabular}{|l|c|c|l|}
265\hline \textbf{Criteria} & \textbf{Pro} & \textbf{Con} & \textbf{Notes} \\
266\hline Few lines of code per file & X & & GnuPG $ = 300.9$, LibTomMath  $ = 71.97$ \\
267\hline Commented function prototypes & X && GnuPG function names are cryptic. \\
268\hline Speed && X & LibTomMath is slower.  \\
269\hline Totally free & X & & GPL has unfavourable restrictions.\\
270\hline Large function base & X & & GnuPG is barebones. \\
271\hline Five modular reduction algorithms & X & & Faster modular exponentiation for a variety of moduli. \\
272\hline Portable & X & & GnuPG requires configuration to build. \\
273\hline
274\end{tabular}
275\end{center}
276\end{small}
277\caption{LibTomMath Valuation}
278\end{figure}
279
280It may seem odd to compare LibTomMath to GnuPG since the math in GnuPG is only a small portion of the entire application. 
281However, LibTomMath was written with cryptography in mind.  It provides essentially all of the functions a cryptosystem
282would require when working with large integers.  
283
284So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in your
285own application but I think there are reasons not to.  While LibTomMath is slower than libraries such as GnuMP it is
286not normally significantly slower.  On x86 machines the difference is normally a factor of two when performing modular
287exponentiations.  It depends largely on the processor, compiler and the moduli being used.
288
289Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern.  However,
290on the other side of the coin LibTomMath offers you a totally free (public domain) well structured math library
291that is very flexible, complete and performs well in resource contrained environments.  Fast RSA for example can
292be performed with as little as 8KB of ram for data (again depending on build options).  
293
294\chapter{Getting Started with LibTomMath}
295\section{Building Programs}
296In order to use LibTomMath you must include ``tommath.h'' and link against the appropriate library file (typically 
297libtommath.a).  There is no library initialization required and the entire library is thread safe.
298
299\section{Return Codes}
300There are three possible return codes a function may return.
301
302\index{MP\_OKAY}\index{MP\_YES}\index{MP\_NO}\index{MP\_VAL}\index{MP\_MEM}
303\begin{figure}[here!]
304\begin{center}
305\begin{small}
306\begin{tabular}{|l|l|}
307\hline \textbf{Code} & \textbf{Meaning} \\
308\hline MP\_OKAY & The function succeeded. \\
309\hline MP\_VAL  & The function input was invalid. \\
310\hline MP\_MEM  & Heap memory exhausted. \\
311\hline &\\
312\hline MP\_YES  & Response is yes. \\
313\hline MP\_NO   & Response is no. \\
314\hline
315\end{tabular}
316\end{small}
317\end{center}
318\caption{Return Codes}
319\end{figure}
320
321The last two codes listed are not actually ``return'ed'' by a function.  They are placed in an integer (the caller must
322provide the address of an integer it can store to) which the caller can access.  To convert one of the three return codes
323to a string use the following function.
324
325\index{mp\_error\_to\_string}
326\begin{alltt}
327char *mp_error_to_string(int code);
328\end{alltt}
329
330This will return a pointer to a string which describes the given error code.  It will not work for the return codes 
331MP\_YES and MP\_NO.  
332
333\section{Data Types}
334The basic ``multiple precision integer'' type is known as the ``mp\_int'' within LibTomMath.  This data type is used to
335organize all of the data required to manipulate the integer it represents.  Within LibTomMath it has been prototyped
336as the following.
337
338\index{mp\_int}
339\begin{alltt}
340typedef struct  \{
341    int used, alloc, sign;
342    mp_digit *dp;
343\} mp_int;
344\end{alltt}
345
346Where ``mp\_digit'' is a data type that represents individual digits of the integer.  By default, an mp\_digit is the
347ISO C ``unsigned long'' data type and each digit is $28-$bits long.  The mp\_digit type can be configured to suit other
348platforms by defining the appropriate macros.  
349
350All LTM functions that use the mp\_int type will expect a pointer to mp\_int structure.  You must allocate memory to
351hold the structure itself by yourself (whether off stack or heap it doesn't matter).  The very first thing that must be
352done to use an mp\_int is that it must be initialized.
353
354\section{Function Organization}
355
356The arithmetic functions of the library are all organized to have the same style prototype.  That is source operands
357are passed on the left and the destination is on the right.  For instance,
358
359\begin{alltt}
360mp_add(&a, &b, &c);       /* c = a + b */
361mp_mul(&a, &a, &c);       /* c = a * a */
362mp_div(&a, &b, &c, &d);   /* c = [a/b], d = a mod b */
363\end{alltt}
364
365Another feature of the way the functions have been implemented is that source operands can be destination operands as well.
366For instance,
367
368\begin{alltt}
369mp_add(&a, &b, &b);       /* b = a + b */
370mp_div(&a, &b, &a, &c);   /* a = [a/b], c = a mod b */
371\end{alltt}
372
373This allows operands to be re-used which can make programming simpler.
374
375\section{Initialization}
376\subsection{Single Initialization}
377A single mp\_int can be initialized with the ``mp\_init'' function. 
378
379\index{mp\_init}
380\begin{alltt}
381int mp_init (mp_int * a);
382\end{alltt}
383
384This function expects a pointer to an mp\_int structure and will initialize the members of the structure so the mp\_int
385represents the default integer which is zero.  If the functions returns MP\_OKAY then the mp\_int is ready to be used
386by the other LibTomMath functions.
387
388\begin{small} \begin{alltt}
389int main(void)
390\{
391   mp_int number;
392   int result;
393
394   if ((result = mp_init(&number)) != MP_OKAY) \{
395      printf("Error initializing the number.  \%s", 
396             mp_error_to_string(result));
397      return EXIT_FAILURE;
398   \}
399 
400   /* use the number */
401
402   return EXIT_SUCCESS;
403\}
404\end{alltt} \end{small}
405
406\subsection{Single Free}
407When you are finished with an mp\_int it is ideal to return the heap it used back to the system.  The following function 
408provides this functionality.
409
410\index{mp\_clear}
411\begin{alltt}
412void mp_clear (mp_int * a);
413\end{alltt}
414
415The function expects a pointer to a previously initialized mp\_int structure and frees the heap it uses.  It sets the 
416pointer\footnote{The ``dp'' member.} within the mp\_int to \textbf{NULL} which is used to prevent double free situations. 
417Is is legal to call mp\_clear() twice on the same mp\_int in a row.  
418
419\begin{small} \begin{alltt}
420int main(void)
421\{
422   mp_int number;
423   int result;
424
425   if ((result = mp_init(&number)) != MP_OKAY) \{
426      printf("Error initializing the number.  \%s", 
427             mp_error_to_string(result));
428      return EXIT_FAILURE;
429   \}
430 
431   /* use the number */
432
433   /* We're done with it. */
434   mp_clear(&number);
435
436   return EXIT_SUCCESS;
437\}
438\end{alltt} \end{small}
439
440\subsection{Multiple Initializations}
441Certain algorithms require more than one large integer.  In these instances it is ideal to initialize all of the mp\_int
442variables in an ``all or nothing'' fashion.  That is, they are either all initialized successfully or they are all
443not initialized.
444
445The  mp\_init\_multi() function provides this functionality.
446
447\index{mp\_init\_multi} \index{mp\_clear\_multi}
448\begin{alltt}
449int mp_init_multi(mp_int *mp, ...);
450\end{alltt}
451
452It accepts a \textbf{NULL} terminated list of pointers to mp\_int structures.  It will attempt to initialize them all
453at once.  If the function returns MP\_OKAY then all of the mp\_int variables are ready to use, otherwise none of them
454are available for use.  A complementary mp\_clear\_multi() function allows multiple mp\_int variables to be free'd 
455from the heap at the same time.  
456
457\begin{small} \begin{alltt}
458int main(void)
459\{
460   mp_int num1, num2, num3;
461   int result;
462
463   if ((result = mp_init_multi(&num1, 
464                               &num2,
465                               &num3, NULL)) != MP\_OKAY) \{      
466      printf("Error initializing the numbers.  \%s", 
467             mp_error_to_string(result));
468      return EXIT_FAILURE;
469   \}
470 
471   /* use the numbers */
472
473   /* We're done with them. */
474   mp_clear_multi(&num1, &num2, &num3, NULL);
475
476   return EXIT_SUCCESS;
477\}
478\end{alltt} \end{small}
479
480\subsection{Other Initializers}
481To initialized and make a copy of an mp\_int the mp\_init\_copy() function has been provided.  
482
483\index{mp\_init\_copy}
484\begin{alltt}
485int mp_init_copy (mp_int * a, mp_int * b);
486\end{alltt}
487
488This function will initialize $a$ and make it a copy of $b$ if all goes well.
489
490\begin{small} \begin{alltt}
491int main(void)
492\{
493   mp_int num1, num2;
494   int result;
495
496   /* initialize and do work on num1 ... */
497
498   /* We want a copy of num1 in num2 now */
499   if ((result = mp_init_copy(&num2, &num1)) != MP_OKAY) \{
500     printf("Error initializing the copy.  \%s", 
501             mp_error_to_string(result));
502      return EXIT_FAILURE;
503   \}
504 
505   /* now num2 is ready and contains a copy of num1 */
506
507   /* We're done with them. */
508   mp_clear_multi(&num1, &num2, NULL);
509
510   return EXIT_SUCCESS;
511\}
512\end{alltt} \end{small}
513
514Another less common initializer is mp\_init\_size() which allows the user to initialize an mp\_int with a given
515default number of digits.  By default, all initializers allocate \textbf{MP\_PREC} digits.  This function lets
516you override this behaviour.
517
518\index{mp\_init\_size}
519\begin{alltt}
520int mp_init_size (mp_int * a, int size);
521\end{alltt}
522
523The $size$ parameter must be greater than zero.  If the function succeeds the mp\_int $a$ will be initialized
524to have $size$ digits (which are all initially zero).  
525
526\begin{small} \begin{alltt}
527int main(void)
528\{
529   mp_int number;
530   int result;
531
532   /* we need a 60-digit number */
533   if ((result = mp_init_size(&number, 60)) != MP_OKAY) \{
534      printf("Error initializing the number.  \%s", 
535             mp_error_to_string(result));
536      return EXIT_FAILURE;
537   \}
538 
539   /* use the number */
540
541   return EXIT_SUCCESS;
542\}
543\end{alltt} \end{small}
544
545\section{Maintenance Functions}
546
547\subsection{Reducing Memory Usage}
548When an mp\_int is in a state where it won't be changed again\footnote{A Diffie-Hellman modulus for instance.} excess
549digits can be removed to return memory to the heap with the mp\_shrink() function.
550
551\index{mp\_shrink}
552\begin{alltt}
553int mp_shrink (mp_int * a);
554\end{alltt}
555
556This will remove excess digits of the mp\_int $a$.  If the operation fails the mp\_int should be intact without the
557excess digits being removed.  Note that you can use a shrunk mp\_int in further computations, however, such operations
558will require heap operations which can be slow.  It is not ideal to shrink mp\_int variables that you will further
559modify in the system (unless you are seriously low on memory).  
560
561\begin{small} \begin{alltt}
562int main(void)
563\{
564   mp_int number;
565   int result;
566
567   if ((result = mp_init(&number)) != MP_OKAY) \{
568      printf("Error initializing the number.  \%s", 
569             mp_error_to_string(result));
570      return EXIT_FAILURE;
571   \}
572 
573   /* use the number [e.g. pre-computation]  */
574
575   /* We're done with it for now. */
576   if ((result = mp_shrink(&number)) != MP_OKAY) \{
577      printf("Error shrinking the number.  \%s", 
578             mp_error_to_string(result));
579      return EXIT_FAILURE;
580   \}
581
582   /* use it .... */
583
584
585   /* we're done with it. */ 
586   mp_clear(&number);
587
588   return EXIT_SUCCESS;
589\}
590\end{alltt} \end{small}
591
592\subsection{Adding additional digits}
593
594Within the mp\_int structure are two parameters which control the limitations of the array of digits that represent
595the integer the mp\_int is meant to equal.   The \textit{used} parameter dictates how many digits are significant, that is,
596contribute to the value of the mp\_int.  The \textit{alloc} parameter dictates how many digits are currently available in
597the array.  If you need to perform an operation that requires more digits you will have to mp\_grow() the mp\_int to
598your desired size.  
599
600\index{mp\_grow}
601\begin{alltt}
602int mp_grow (mp_int * a, int size);
603\end{alltt}
604
605This will grow the array of digits of $a$ to $size$.  If the \textit{alloc} parameter is already bigger than
606$size$ the function will not do anything.
607
608\begin{small} \begin{alltt}
609int main(void)
610\{
611   mp_int number;
612   int result;
613
614   if ((result = mp_init(&number)) != MP_OKAY) \{
615      printf("Error initializing the number.  \%s", 
616             mp_error_to_string(result));
617      return EXIT_FAILURE;
618   \}
619 
620   /* use the number */
621
622   /* We need to add 20 digits to the number  */
623   if ((result = mp_grow(&number, number.alloc + 20)) != MP_OKAY) \{
624      printf("Error growing the number.  \%s", 
625             mp_error_to_string(result));
626      return EXIT_FAILURE;
627   \}
628
629
630   /* use the number */
631
632   /* we're done with it. */ 
633   mp_clear(&number);
634
635   return EXIT_SUCCESS;
636\}
637\end{alltt} \end{small}
638
639\chapter{Basic Operations}
640\section{Small Constants}
641Setting mp\_ints to small constants is a relatively common operation.  To accomodate these instances there are two
642small constant assignment functions.  The first function is used to set a single digit constant while the second sets
643an ISO C style ``unsigned long'' constant.  The reason for both functions is efficiency.  Setting a single digit is quick but the
644domain of a digit can change (it's always at least $0 \ldots 127$).  
645
646\subsection{Single Digit}
647
648Setting a single digit can be accomplished with the following function.
649
650\index{mp\_set}
651\begin{alltt}
652void mp_set (mp_int * a, mp_digit b);
653\end{alltt}
654
655This will zero the contents of $a$ and make it represent an integer equal to the value of $b$.  Note that this
656function has a return type of \textbf{void}.  It cannot cause an error so it is safe to assume the function
657succeeded.
658
659\begin{small} \begin{alltt}
660int main(void)
661\{
662   mp_int number;
663   int result;
664
665   if ((result = mp_init(&number)) != MP_OKAY) \{
666      printf("Error initializing the number.  \%s", 
667             mp_error_to_string(result));
668      return EXIT_FAILURE;
669   \}
670 
671   /* set the number to 5 */
672   mp_set(&number, 5);
673
674   /* we're done with it. */ 
675   mp_clear(&number);
676
677   return EXIT_SUCCESS;
678\}
679\end{alltt} \end{small}
680
681\subsection{Long Constants}
682
683To set a constant that is the size of an ISO C ``unsigned long'' and larger than a single digit the following function 
684can be used.
685
686\index{mp\_set\_int}
687\begin{alltt}
688int mp_set_int (mp_int * a, unsigned long b);
689\end{alltt}
690
691This will assign the value of the 32-bit variable $b$ to the mp\_int $a$.  Unlike mp\_set() this function will always
692accept a 32-bit input regardless of the size of a single digit.  However, since the value may span several digits 
693this function can fail if it runs out of heap memory.
694
695To get the ``unsigned long'' copy of an mp\_int the following function can be used.
696
697\index{mp\_get\_int}
698\begin{alltt}
699unsigned long mp_get_int (mp_int * a);
700\end{alltt}
701
702This will return the 32 least significant bits of the mp\_int $a$.  
703
704\begin{small} \begin{alltt}
705int main(void)
706\{
707   mp_int number;
708   int result;
709
710   if ((result = mp_init(&number)) != MP_OKAY) \{
711      printf("Error initializing the number.  \%s", 
712             mp_error_to_string(result));
713      return EXIT_FAILURE;
714   \}
715 
716   /* set the number to 654321 (note this is bigger than 127) */
717   if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{
718      printf("Error setting the value of the number.  \%s", 
719             mp_error_to_string(result));
720      return EXIT_FAILURE;
721   \}
722
723   printf("number == \%lu", mp_get_int(&number));
724
725   /* we're done with it. */ 
726   mp_clear(&number);
727
728   return EXIT_SUCCESS;
729\}
730\end{alltt} \end{small}
731
732This should output the following if the program succeeds.
733
734\begin{alltt}
735number == 654321
736\end{alltt}
737
738\subsection{Initialize and Setting Constants}
739To both initialize and set small constants the following two functions are available.
740\index{mp\_init\_set} \index{mp\_init\_set\_int}
741\begin{alltt}
742int mp_init_set (mp_int * a, mp_digit b);
743int mp_init_set_int (mp_int * a, unsigned long b);
744\end{alltt}
745
746Both functions work like the previous counterparts except they first mp\_init $a$ before setting the values.  
747
748\begin{alltt}
749int main(void)
750\{
751   mp_int number1, number2;
752   int    result;
753
754   /* initialize and set a single digit */
755   if ((result = mp_init_set(&number1, 100)) != MP_OKAY) \{
756      printf("Error setting number1: \%s", 
757             mp_error_to_string(result));
758      return EXIT_FAILURE;
759   \}             
760
761   /* initialize and set a long */
762   if ((result = mp_init_set_int(&number2, 1023)) != MP_OKAY) \{
763      printf("Error setting number2: \%s", 
764             mp_error_to_string(result));
765      return EXIT_FAILURE;
766   \}
767
768   /* display */
769   printf("Number1, Number2 == \%lu, \%lu",
770          mp_get_int(&number1), mp_get_int(&number2));
771
772   /* clear */
773   mp_clear_multi(&number1, &number2, NULL);
774
775   return EXIT_SUCCESS;
776\}
777\end{alltt}
778
779If this program succeeds it shall output.
780\begin{alltt}
781Number1, Number2 == 100, 1023
782\end{alltt}
783
784\section{Comparisons}
785
786Comparisons in LibTomMath are always performed in a ``left to right'' fashion.  There are three possible return codes
787for any comparison.
788
789\index{MP\_GT} \index{MP\_EQ} \index{MP\_LT}
790\begin{figure}[here]
791\begin{center}
792\begin{tabular}{|c|c|}
793\hline \textbf{Result Code} & \textbf{Meaning} \\
794\hline MP\_GT & $a > b$ \\
795\hline MP\_EQ & $a = b$ \\
796\hline MP\_LT & $a < b$ \\
797\hline
798\end{tabular}
799\end{center}
800\caption{Comparison Codes for $a, b$}
801\label{fig:CMP}
802\end{figure}
803
804In figure \ref{fig:CMP} two integers $a$ and $b$ are being compared.  In this case $a$ is said to be ``to the left'' of 
805$b$.  
806
807\subsection{Unsigned comparison}
808
809An unsigned comparison considers only the digits themselves and not the associated \textit{sign} flag of the 
810mp\_int structures.  This is analogous to an absolute comparison.  The function mp\_cmp\_mag() will compare two
811mp\_int variables based on their digits only. 
812
813\index{mp\_cmp\_mag}
814\begin{alltt}
815int mp_cmp_mag(mp_int * a, mp_int * b);
816\end{alltt}
817This will compare $a$ to $b$ placing $a$ to the left of $b$.  This function cannot fail and will return one of the
818three compare codes listed in figure \ref{fig:CMP}.
819
820\begin{small} \begin{alltt}
821int main(void)
822\{
823   mp_int number1, number2;
824   int result;
825
826   if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{
827      printf("Error initializing the numbers.  \%s", 
828             mp_error_to_string(result));
829      return EXIT_FAILURE;
830   \}
831 
832   /* set the number1 to 5 */
833   mp_set(&number1, 5);
834  
835   /* set the number2 to -6 */
836   mp_set(&number2, 6);
837   if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{
838      printf("Error negating number2.  \%s", 
839             mp_error_to_string(result));
840      return EXIT_FAILURE;
841   \}
842
843   switch(mp_cmp_mag(&number1, &number2)) \{
844       case MP_GT:  printf("|number1| > |number2|"); break;
845       case MP_EQ:  printf("|number1| = |number2|"); break;
846       case MP_LT:  printf("|number1| < |number2|"); break;
847   \}
848
849   /* we're done with it. */ 
850   mp_clear_multi(&number1, &number2, NULL);
851
852   return EXIT_SUCCESS;
853\}
854\end{alltt} \end{small}
855
856If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes 
857successfully it should print the following.
858
859\begin{alltt}
860|number1| < |number2|
861\end{alltt}
862
863This is because $\vert -6 \vert = 6$ and obviously $5 < 6$.
864
865\subsection{Signed comparison}
866
867To compare two mp\_int variables based on their signed value the mp\_cmp() function is provided.
868
869\index{mp\_cmp}
870\begin{alltt}
871int mp_cmp(mp_int * a, mp_int * b);
872\end{alltt}
873
874This will compare $a$ to the left of $b$.  It will first compare the signs of the two mp\_int variables.  If they
875differ it will return immediately based on their signs.  If the signs are equal then it will compare the digits
876individually.  This function will return one of the compare conditions codes listed in figure \ref{fig:CMP}.
877
878\begin{small} \begin{alltt}
879int main(void)
880\{
881   mp_int number1, number2;
882   int result;
883
884   if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{
885      printf("Error initializing the numbers.  \%s", 
886             mp_error_to_string(result));
887      return EXIT_FAILURE;
888   \}
889 
890   /* set the number1 to 5 */
891   mp_set(&number1, 5);
892  
893   /* set the number2 to -6 */
894   mp_set(&number2, 6);
895   if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{
896      printf("Error negating number2.  \%s", 
897             mp_error_to_string(result));
898      return EXIT_FAILURE;
899   \}
900
901   switch(mp_cmp(&number1, &number2)) \{
902       case MP_GT:  printf("number1 > number2"); break;
903       case MP_EQ:  printf("number1 = number2"); break;
904       case MP_LT:  printf("number1 < number2"); break;
905   \}
906
907   /* we're done with it. */ 
908   mp_clear_multi(&number1, &number2, NULL);
909
910   return EXIT_SUCCESS;
911\}
912\end{alltt} \end{small}
913
914If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes 
915successfully it should print the following.
916
917\begin{alltt}
918number1 > number2
919\end{alltt}
920
921\subsection{Single Digit}
922
923To compare a single digit against an mp\_int the following function has been provided.
924
925\index{mp\_cmp\_d}
926\begin{alltt}
927int mp_cmp_d(mp_int * a, mp_digit b);
928\end{alltt}
929
930This will compare $a$ to the left of $b$ using a signed comparison.  Note that it will always treat $b$ as 
931positive.  This function is rather handy when you have to compare against small values such as $1$ (which often
932comes up in cryptography).  The function cannot fail and will return one of the tree compare condition codes
933listed in figure \ref{fig:CMP}.
934
935
936\begin{small} \begin{alltt}
937int main(void)
938\{
939   mp_int number;
940   int result;
941
942   if ((result = mp_init(&number)) != MP_OKAY) \{
943      printf("Error initializing the number.  \%s", 
944             mp_error_to_string(result));
945      return EXIT_FAILURE;
946   \}
947 
948   /* set the number to 5 */
949   mp_set(&number, 5);
950
951   switch(mp_cmp_d(&number, 7)) \{
952       case MP_GT:  printf("number > 7"); break;
953       case MP_EQ:  printf("number = 7"); break;
954       case MP_LT:  printf("number < 7"); break;
955   \}
956
957   /* we're done with it. */ 
958   mp_clear(&number);
959
960   return EXIT_SUCCESS;
961\}
962\end{alltt} \end{small}
963
964If this program functions properly it will print out the following.
965
966\begin{alltt}
967number < 7
968\end{alltt}
969
970\section{Logical Operations}
971
972Logical operations are operations that can be performed either with simple shifts or boolean operators such as
973AND, XOR and OR directly.  These operations are very quick.
974
975\subsection{Multiplication by two}
976
977Multiplications and divisions by any power of two can be performed with quick logical shifts either left or
978right depending on the operation.  
979
980When multiplying or dividing by two a special case routine can be used which are as follows.
981\index{mp\_mul\_2} \index{mp\_div\_2}
982\begin{alltt}
983int mp_mul_2(mp_int * a, mp_int * b);
984int mp_div_2(mp_int * a, mp_int * b);
985\end{alltt}
986
987The former will assign twice $a$ to $b$ while the latter will assign half $a$ to $b$.  These functions are fast
988since the shift counts and maskes are hardcoded into the routines.
989
990\begin{small} \begin{alltt}
991int main(void)
992\{
993   mp_int number;
994   int result;
995
996   if ((result = mp_init(&number)) != MP_OKAY) \{
997      printf("Error initializing the number.  \%s", 
998             mp_error_to_string(result));
999      return EXIT_FAILURE;
1000   \}
1001 
1002   /* set the number to 5 */
1003   mp_set(&number, 5);
1004
1005   /* multiply by two */
1006   if ((result = mp\_mul\_2(&number, &number)) != MP_OKAY) \{
1007      printf("Error multiplying the number.  \%s", 
1008             mp_error_to_string(result));
1009      return EXIT_FAILURE;
1010   \}
1011   switch(mp_cmp_d(&number, 7)) \{
1012       case MP_GT:  printf("2*number > 7"); break;
1013       case MP_EQ:  printf("2*number = 7"); break;
1014       case MP_LT:  printf("2*number < 7"); break;
1015   \}
1016
1017   /* now divide by two */
1018   if ((result = mp\_div\_2(&number, &number)) != MP_OKAY) \{
1019      printf("Error dividing the number.  \%s", 
1020             mp_error_to_string(result));
1021      return EXIT_FAILURE;
1022   \}
1023   switch(mp_cmp_d(&number, 7)) \{
1024       case MP_GT:  printf("2*number/2 > 7"); break;
1025       case MP_EQ:  printf("2*number/2 = 7"); break;
1026       case MP_LT:  printf("2*number/2 < 7"); break;
1027   \}
1028
1029   /* we're done with it. */ 
1030   mp_clear(&number);
1031
1032   return EXIT_SUCCESS;
1033\}
1034\end{alltt} \end{small}
1035
1036If this program is successful it will print out the following text.
1037
1038\begin{alltt}
10392*number > 7
10402*number/2 < 7
1041\end{alltt}
1042
1043Since $10 > 7$ and $5 < 7$.  To multiply by a power of two the following function can be used.
1044
1045\index{mp\_mul\_2d}
1046\begin{alltt}
1047int mp_mul_2d(mp_int * a, int b, mp_int * c);
1048\end{alltt}
1049
1050This will multiply $a$ by $2^b$ and store the result in ``c''.  If the value of $b$ is less than or equal to 
1051zero the function will copy $a$ to ``c'' without performing any further actions.  
1052
1053To divide by a power of two use the following.
1054
1055\index{mp\_div\_2d}
1056\begin{alltt}
1057int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
1058\end{alltt}
1059Which will divide $a$ by $2^b$, store the quotient in ``c'' and the remainder in ``d'.  If $b \le 0$ then the
1060function simply copies $a$ over to ``c'' and zeroes $d$.  The variable $d$ may be passed as a \textbf{NULL}
1061value to signal that the remainder is not desired.
1062
1063\subsection{Polynomial Basis Operations}
1064
1065Strictly speaking the organization of the integers within the mp\_int structures is what is known as a 
1066``polynomial basis''.  This simply means a field element is stored by divisions of a radix.  For example, if
1067$f(x) = \sum_{i=0}^{k} y_ix^k$ for any vector $\vec y$ then the array of digits in $\vec y$ are said to be 
1068the polynomial basis representation of $z$ if $f(\beta) = z$ for a given radix $\beta$.  
1069
1070To multiply by the polynomial $g(x) = x$ all you have todo is shift the digits of the basis left one place.  The
1071following function provides this operation.
1072
1073\index{mp\_lshd}
1074\begin{alltt}
1075int mp_lshd (mp_int * a, int b);
1076\end{alltt}
1077
1078This will multiply $a$ in place by $x^b$ which is equivalent to shifting the digits left $b$ places and inserting zeroes
1079in the least significant digits.  Similarly to divide by a power of $x$ the following function is provided.
1080
1081\index{mp\_rshd}
1082\begin{alltt}
1083void mp_rshd (mp_int * a, int b)
1084\end{alltt}
1085This will divide $a$ in place by $x^b$ and discard the remainder.  This function cannot fail as it performs the operations
1086in place and no new digits are required to complete it.
1087
1088\subsection{AND, OR and XOR Operations}
1089
1090While AND, OR and XOR operations are not typical ``bignum functions'' they can be useful in several instances.  The
1091three functions are prototyped as follows.
1092
1093\index{mp\_or} \index{mp\_and} \index{mp\_xor}
1094\begin{alltt}
1095int mp_or  (mp_int * a, mp_int * b, mp_int * c);
1096int mp_and (mp_int * a, mp_int * b, mp_int * c);
1097int mp_xor (mp_int * a, mp_int * b, mp_int * c);
1098\end{alltt}
1099
1100Which compute $c = a \odot b$ where $\odot$ is one of OR, AND or XOR.  
1101
1102\section{Addition and Subtraction}
1103
1104To compute an addition or subtraction the following two functions can be used.
1105
1106\index{mp\_add} \index{mp\_sub}
1107\begin{alltt}
1108int mp_add (mp_int * a, mp_int * b, mp_int * c);
1109int mp_sub (mp_int * a, mp_int * b, mp_int * c)
1110\end{alltt}
1111
1112Which perform $c = a \odot b$ where $\odot$ is one of signed addition or subtraction.  The operations are fully sign
1113aware.
1114
1115\section{Sign Manipulation}
1116\subsection{Negation}
1117\label{sec:NEG}
1118Simple integer negation can be performed with the following.
1119
1120\index{mp\_neg}
1121\begin{alltt}
1122int mp_neg (mp_int * a, mp_int * b);
1123\end{alltt}
1124
1125Which assigns $-a$ to $b$.  
1126
1127\subsection{Absolute}
1128Simple integer absolutes can be performed with the following.
1129
1130\index{mp\_neg}
1131\begin{alltt}
1132int mp_abs (mp_int * a, mp_int * b);
1133\end{alltt}
1134
1135Which assigns $\vert a \vert$ to $b$.  
1136
1137\section{Integer Division and Remainder}
1138To perform a complete and general integer division with remainder use the following function.
1139
1140\index{mp\_div}
1141\begin{alltt}
1142int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
1143\end{alltt}
1144                                                        
1145This divides $a$ by $b$ and stores the quotient in $c$ and $d$.  The signed quotient is computed such that 
1146$bc + d = a$.  Note that either of $c$ or $d$ can be set to \textbf{NULL} if their value is not required.  If 
1147$b$ is zero the function returns \textbf{MP\_VAL}.  
1148
1149
1150\chapter{Multiplication and Squaring}
1151\section{Multiplication}
1152A full signed integer multiplication can be performed with the following.
1153\index{mp\_mul}
1154\begin{alltt}
1155int mp_mul (mp_int * a, mp_int * b, mp_int * c);
1156\end{alltt}
1157Which assigns the full signed product $ab$ to $c$.  This function actually breaks into one of four cases which are 
1158specific multiplication routines optimized for given parameters.  First there are the Toom-Cook multiplications which
1159should only be used with very large inputs.  This is followed by the Karatsuba multiplications which are for moderate
1160sized inputs.  Then followed by the Comba and baseline multipliers.
1161
1162Fortunately for the developer you don't really need to know this unless you really want to fine tune the system.  mp\_mul()
1163will determine on its own\footnote{Some tweaking may be required.} what routine to use automatically when it is called.
1164
1165\begin{alltt}
1166int main(void)
1167\{
1168   mp_int number1, number2;
1169   int result;
1170
1171   /* Initialize the numbers */
1172   if ((result = mp_init_multi(&number1, 
1173                               &number2, NULL)) != MP_OKAY) \{
1174      printf("Error initializing the numbers.  \%s", 
1175             mp_error_to_string(result));
1176      return EXIT_FAILURE;
1177   \}
1178
1179   /* set the terms */
1180   if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{
1181      printf("Error setting number1.  \%s", 
1182             mp_error_to_string(result));
1183      return EXIT_FAILURE;
1184   \}
1185 
1186   if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{
1187      printf("Error setting number2.  \%s", 
1188             mp_error_to_string(result));
1189      return EXIT_FAILURE;
1190   \}
1191
1192   /* multiply them */
1193   if ((result = mp_mul(&number1, &number2,
1194                        &number1)) != MP_OKAY) \{
1195      printf("Error multiplying terms.  \%s", 
1196             mp_error_to_string(result));
1197      return EXIT_FAILURE;
1198   \}
1199
1200   /* display */
1201   printf("number1 * number2 == \%lu", mp_get_int(&number1));
1202
1203   /* free terms and return */
1204   mp_clear_multi(&number1, &number2, NULL);
1205
1206   return EXIT_SUCCESS;
1207\}
1208\end{alltt}   
1209
1210If this program succeeds it shall output the following.
1211
1212\begin{alltt}
1213number1 * number2 == 262911
1214\end{alltt}
1215
1216\section{Squaring}
1217Since squaring can be performed faster than multiplication it is performed it's own function instead of just using
1218mp\_mul().
1219
1220\index{mp\_sqr}
1221\begin{alltt}
1222int mp_sqr (mp_int * a, mp_int * b);
1223\end{alltt}
1224
1225Will square $a$ and store it in $b$.  Like the case of multiplication there are four different squaring
1226algorithms all which can be called from mp\_sqr().  It is ideal to use mp\_sqr over mp\_mul when squaring terms because
1227of the speed difference.  
1228
1229\section{Tuning Polynomial Basis Routines}
1230
1231Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that
1232the Comba and baseline algorithms use.  At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectively they require 
1233considerably less work.  For example, a 10000-digit multiplication would take roughly 724,000 single precision
1234multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor
1235of 138).
1236
1237So why not always use Karatsuba or Toom-Cook?   The simple answer is that they have so much overhead that they're not
1238actually faster than Comba until you hit distinct  ``cutoff'' points.  For Karatsuba with the default configuration, 
1239GCC 3.3.1 and an Athlon XP processor the cutoff point is roughly 110 digits (about 70 for the Intel P4).  That is, at 
1240110 digits Karatsuba and Comba multiplications just about break even and for 110+ digits Karatsuba is faster.
1241
1242Toom-Cook has incredible overhead and is probably only useful for very large inputs.  So far no known cutoff points 
1243exist and for the most part I just set the cutoff points very high to make sure they're not called.
1244
1245A demo program in the ``etc/'' directory of the project called ``tune.c'' can be used to find the cutoff points.  This
1246can be built with GCC as follows
1247
1248\begin{alltt}
1249make XXX
1250\end{alltt}
1251Where ``XXX'' is one of the following entries from the table \ref{fig:tuning}.
1252
1253\begin{figure}[here]
1254\begin{center}
1255\begin{small}
1256\begin{tabular}{|l|l|}
1257\hline \textbf{Value of XXX} & \textbf{Meaning} \\
1258\hline tune & Builds portable tuning application \\
1259\hline tune86 & Builds x86 (pentium and up) program for COFF \\
1260\hline tune86c & Builds x86 program for Cygwin \\
1261\hline tune86l & Builds x86 program for Linux (ELF format) \\
1262\hline
1263\end{tabular}
1264\end{small}
1265\end{center}
1266\caption{Build Names for Tuning Programs}
1267\label{fig:tuning}
1268\end{figure}
1269
1270When the program is running it will output a series of measurements for different cutoff points.  It will first find
1271good Karatsuba squaring and multiplication points.  Then it proceeds to find Toom-Cook points.  Note that the Toom-Cook
1272tuning takes a very long time as the cutoff points are likely to be very high.
1273
1274\chapter{Modular Reduction}
1275
1276Modular reduction is process of taking the remainder of one quantity divided by another.  Expressed 
1277as (\ref{eqn:mod}) the modular reduction is equivalent to the remainder of $b$ divided by $c$.  
1278
1279\begin{equation}
1280a \equiv b \mbox{ (mod }c\mbox{)}
1281\label{eqn:mod}
1282\end{equation}
1283
1284Of particular interest to cryptography are reductions where $b$ is limited to the range $0 \le b < c^2$ since particularly 
1285fast reduction algorithms can be written for the limited range.  
1286
1287Note that one of the four optimized reduction algorithms are automatically chosen in the modular exponentiation
1288algorithm mp\_exptmod when an appropriate modulus is detected.  
1289
1290\section{Straight Division}
1291In order to effect an arbitrary modular reduction the following algorithm is provided.
1292
1293\index{mp\_mod}
1294\begin{alltt}
1295int mp_mod(mp_int *a, mp_int *b, mp_int *c);
1296\end{alltt}
1297
1298This reduces $a$ modulo $b$ and stores the result in $c$.  The sign of $c$ shall agree with the sign 
1299of $b$.  This algorithm accepts an input $a$ of any range and is not limited by $0 \le a < b^2$.
1300
1301\section{Barrett Reduction}
1302
1303Barrett reduction is a generic optimized reduction algorithm that requires pre--computation to achieve
1304a decent speedup over straight division.  First a $\mu$ value must be precomputed with the following function.
1305
1306\index{mp\_reduce\_setup}
1307\begin{alltt}
1308int mp_reduce_setup(mp_int *a, mp_int *b);
1309\end{alltt}
1310
1311Given a modulus in $b$ this produces the required $\mu$ value in $a$.  For any given modulus this only has to
1312be computed once.  Modular reduction can now be performed with the following.
1313
1314\index{mp\_reduce}
1315\begin{alltt}
1316int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
1317\end{alltt}
1318
1319This will reduce $a$ in place modulo $b$ with the precomputed $\mu$ value in $c$.  $a$ must be in the range
1320$0 \le a < b^2$.
1321
1322\begin{alltt}
1323int main(void)
1324\{
1325   mp_int   a, b, c, mu;
1326   int      result;
1327
1328   /* initialize a,b to desired values, mp_init mu, 
1329    * c and set c to 1...we want to compute a^3 mod b 
1330    */
1331
1332   /* get mu value */
1333   if ((result = mp_reduce_setup(&mu, b)) != MP_OKAY) \{
1334      printf("Error getting mu.  \%s", 
1335             mp_error_to_string(result));
1336      return EXIT_FAILURE;
1337   \}
1338
1339   /* square a to get c = a^2 */
1340   if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{
1341      printf("Error squaring.  \%s", 
1342             mp_error_to_string(result));
1343      return EXIT_FAILURE;
1344   \}
1345
1346   /* now reduce `c' modulo b */
1347   if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{
1348      printf("Error reducing.  \%s", 
1349             mp_error_to_string(result));
1350      return EXIT_FAILURE;
1351   \}
1352   
1353   /* multiply a to get c = a^3 */
1354   if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{
1355      printf("Error reducing.  \%s", 
1356             mp_error_to_string(result));
1357      return EXIT_FAILURE;
1358   \}
1359
1360   /* now reduce `c' modulo b  */
1361   if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{
1362      printf("Error reducing.  \%s", 
1363             mp_error_to_string(result));
1364      return EXIT_FAILURE;
1365   \}
1366  
1367   /* c now equals a^3 mod b */
1368
1369   return EXIT_SUCCESS;
1370\}
1371\end{alltt} 
1372
1373This program will calculate $a^3 \mbox{ mod }b$ if all the functions succeed.  
1374
1375\section{Montgomery Reduction}
1376
1377Montgomery is a specialized reduction algorithm for any odd moduli.  Like Barrett reduction a pre--computation
1378step is required.  This is accomplished with the following.
1379
1380\index{mp\_montgomery\_setup}
1381\begin{alltt}
1382int mp_montgomery_setup(mp_int *a, mp_digit *mp);
1383\end{alltt}
1384
1385For the given odd moduli $a$ the precomputation value is placed in $mp$.  The reduction is computed with the 
1386following.
1387
1388\index{mp\_montgomery\_reduce}
1389\begin{alltt}
1390int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
1391\end{alltt}
1392This reduces $a$ in place modulo $m$ with the pre--computed value $mp$.   $a$ must be in the range
1393$0 \le a < b^2$.
1394
1395Montgomery reduction is faster than Barrett reduction for moduli smaller than the ``comba'' limit.  With the default
1396setup for instance, the limit is $127$ digits ($3556$--bits).   Note that this function is not limited to
1397$127$ digits just that it falls back to a baseline algorithm after that point.  
1398
1399An important observation is that this reduction does not return $a \mbox{ mod }m$ but $aR^{-1} \mbox{ mod }m$ 
1400where $R = \beta^n$, $n$ is the n number of digits in $m$ and $\beta$ is radix used (default is $2^{28}$).  
1401
1402To quickly calculate $R$ the following function was provided.
1403
1404\index{mp\_montgomery\_calc\_normalization}
1405\begin{alltt}
1406int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
1407\end{alltt}
1408Which calculates $a = R$ for the odd moduli $b$ without using multiplication or division.  
1409
1410The normal modus operandi for Montgomery reductions is to normalize the integers before entering the system.  For
1411example, to calculate $a^3 \mbox { mod }b$ using Montgomery reduction the value of $a$ can be normalized by
1412multiplying it by $R$.  Consider the following code snippet.
1413
1414\begin{alltt}
1415int main(void)
1416\{
1417   mp_int   a, b, c, R;
1418   mp_digit mp;
1419   int      result;
1420
1421   /* initialize a,b to desired values, 
1422    * mp_init R, c and set c to 1.... 
1423    */
1424
1425   /* get normalization */
1426   if ((result = mp_montgomery_calc_normalization(&R, b)) != MP_OKAY) \{
1427      printf("Error getting norm.  \%s", 
1428             mp_error_to_string(result));
1429      return EXIT_FAILURE;
1430   \}
1431
1432   /* get mp value */
1433   if ((result = mp_montgomery_setup(&c, &mp)) != MP_OKAY) \{
1434      printf("Error setting up montgomery.  \%s", 
1435             mp_error_to_string(result));
1436      return EXIT_FAILURE;
1437   \}
1438
1439   /* normalize `a' so now a is equal to aR */
1440   if ((result = mp_mulmod(&a, &R, &b, &a)) != MP_OKAY) \{
1441      printf("Error computing aR.  \%s", 
1442             mp_error_to_string(result));
1443      return EXIT_FAILURE;
1444   \}
1445
1446   /* square a to get c = a^2R^2 */
1447   if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{
1448      printf("Error squaring.  \%s", 
1449             mp_error_to_string(result));
1450      return EXIT_FAILURE;
1451   \}
1452
1453   /* now reduce `c' back down to c = a^2R^2 * R^-1 == a^2R */
1454   if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
1455      printf("Error reducing.  \%s", 
1456             mp_error_to_string(result));
1457      return EXIT_FAILURE;
1458   \}
1459   
1460   /* multiply a to get c = a^3R^2 */
1461   if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{
1462      printf("Error reducing.  \%s", 
1463             mp_error_to_string(result));
1464      return EXIT_FAILURE;
1465   \}
1466
1467   /* now reduce `c' back down to c = a^3R^2 * R^-1 == a^3R */
1468   if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
1469      printf("Error reducing.  \%s", 
1470             mp_error_to_string(result));
1471      return EXIT_FAILURE;
1472   \}
1473   
1474   /* now reduce (again) `c' back down to c = a^3R * R^-1 == a^3 */
1475   if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
1476      printf("Error reducing.  \%s", 
1477             mp_error_to_string(result));
1478      return EXIT_FAILURE;
1479   \}
1480
1481   /* c now equals a^3 mod b */
1482
1483   return EXIT_SUCCESS;
1484\}
1485\end{alltt} 
1486
1487This particular example does not look too efficient but it demonstrates the point of the algorithm.  By 
1488normalizing the inputs the reduced results are always of the form $aR$ for some variable $a$.  This allows
1489a single final reduction to correct for the normalization and the fast reduction used within the algorithm.
1490
1491For more details consider examining the file \textit{bn\_mp\_exptmod\_fast.c}.
1492
1493\section{Restricted Dimminished Radix}
1494
1495``Dimminished Radix'' reduction refers to reduction with respect to moduli that are ameniable to simple
1496digit shifting and small multiplications.  In this case the ``restricted'' variant refers to moduli of the
1497form $\beta^k - p$ for some $k \ge 0$ and $0 < p < \beta$ where $\beta$ is the radix (default to $2^{28}$).  
1498
1499As in the case of Montgomery reduction there is a pre--computation phase required for a given modulus.
1500
1501\index{mp\_dr\_setup}
1502\begin{alltt}
1503void mp_dr_setup(mp_int *a, mp_digit *d);
1504\end{alltt}
1505
1506This computes the value required for the modulus $a$ and stores it in $d$.  This function cannot fail
1507and does not return any error codes.  After the pre--computation a reduction can be performed with the
1508following.
1509
1510\index{mp\_dr\_reduce}
1511\begin{alltt}
1512int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
1513\end{alltt}
1514
1515This reduces $a$ in place modulo $b$ with the pre--computed value $mp$.  $b$ must be of a restricted
1516dimminished radix form and $a$ must be in the range $0 \le a < b^2$.  Dimminished radix reductions are 
1517much faster than both Barrett and Montgomery reductions as they have a much lower asymtotic running time.  
1518
1519Since the moduli are restricted this algorithm is not particularly useful for something like Rabin, RSA or
1520BBS cryptographic purposes.  This reduction algorithm is useful for Diffie-Hellman and ECC where fixed
1521primes are acceptable.  
1522
1523Note that unlike Montgomery reduction there is no normalization process.  The result of this function is
1524equal to the correct residue.
1525
1526\section{Unrestricted Dimminshed Radix}
1527
1528Unrestricted reductions work much like the restricted counterparts except in this case the moduli is of the 
1529form $2^k - p$ for $0 < p < \beta$.  In this sense the unrestricted reductions are more flexible as they 
1530can be applied to a wider range of numbers.  
1531
1532\index{mp\_reduce\_2k\_setup}
1533\begin{alltt}
1534int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
1535\end{alltt}
1536
1537This will compute the required $d$ value for the given moduli $a$.  
1538
1539\index{mp\_reduce\_2k}
1540\begin{alltt}
1541int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
1542\end{alltt}
1543
1544This will reduce $a$ in place modulo $n$ with the pre--computed value $d$.  From my experience this routine is 
1545slower than mp\_dr\_reduce but faster for most moduli sizes than the Montgomery reduction.  
1546
1547\chapter{Exponentiation}
1548\section{Single Digit Exponentiation}
1549\index{mp\_expt\_d}
1550\begin{alltt}
1551int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
1552\end{alltt}
1553This computes $c = a^b$ using a simple binary left-to-right algorithm.  It is faster than repeated multiplications by 
1554$a$ for all values of $b$ greater than three.  
1555
1556\section{Modular Exponentiation}
1557\index{mp\_exptmod}
1558\begin{alltt}
1559int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
1560\end{alltt}
1561This computes $Y \equiv G^X \mbox{ (mod }P\mbox{)}$ using a variable width sliding window algorithm.  This function
1562will automatically detect the fastest modular reduction technique to use during the operation.  For negative values of 
1563$X$ the operation is performed as $Y \equiv (G^{-1} \mbox{ mod }P)^{\vert X \vert} \mbox{ (mod }P\mbox{)}$ provided that 
1564$gcd(G, P) = 1$.
1565
1566This function is actually a shell around the two internal exponentiation functions.  This routine will automatically
1567detect when Barrett, Montgomery, Restricted and Unrestricted Dimminished Radix based exponentiation can be used.  Generally
1568moduli of the a ``restricted dimminished radix'' form lead to the fastest modular exponentiations.  Followed by Montgomery
1569and the other two algorithms.
1570
1571\section{Root Finding}
1572\index{mp\_n\_root}
1573\begin{alltt}
1574int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
1575\end{alltt}
1576This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$.  The implementation of this function is not 
1577ideal for values of $b$ greater than three.  It will work but become very slow.  So unless you are working with very small
1578numbers (less than 1000 bits) I'd avoid $b > 3$ situations.  Will return a positive root only for even roots and return
1579a root with the sign of the input for odd roots.  For example, performing $4^{1/2}$ will return $2$ whereas $(-8)^{1/3}$ 
1580will return $-2$.  
1581
1582This algorithm uses the ``Newton Approximation'' method and will converge on the correct root fairly quickly.  Since
1583the algorithm requires raising $a$ to the power of $b$ it is not ideal to attempt to find roots for large
1584values of $b$.  If particularly large roots are required then a factor method could be used instead.  For example,
1585$a^{1/16}$ is equivalent to $\left (a^{1/4} \right)^{1/4}$ or simply 
1586$\left ( \left ( \left ( a^{1/2} \right )^{1/2} \right )^{1/2} \right )^{1/2}$
1587
1588\chapter{Prime Numbers}
1589\section{Trial Division}
1590\index{mp\_prime\_is\_divisible}
1591\begin{alltt}
1592int mp_prime_is_divisible (mp_int * a, int *result)
1593\end{alltt}
1594This will attempt to evenly divide $a$ by a list of primes\footnote{Default is the first 256 primes.} and store the 
1595outcome in ``result''.  That is if $result = 0$ then $a$ is not divisible by the primes, otherwise it is.  Note that 
1596if the function does not return \textbf{MP\_OKAY} the value in ``result'' should be considered undefined\footnote{Currently
1597the default is to set it to zero first.}.
1598
1599\section{Fermat Test}
1600\index{mp\_prime\_fermat}
1601\begin{alltt}
1602int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
1603\end{alltt}
1604Performs a Fermat primality test to the base $b$.  That is it computes $b^a \mbox{ mod }a$ and tests whether the value is
1605equal to $b$ or not.  If the values are equal then $a$ is probably prime and $result$ is set to one.  Otherwise $result$
1606is set to zero.
1607
1608\section{Miller-Rabin Test}
1609\index{mp\_prime\_miller\_rabin}
1610\begin{alltt}
1611int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
1612\end{alltt}
1613Performs a Miller-Rabin test to the base $b$ of $a$.  This test is much stronger than the Fermat test and is very hard to
1614fool (besides with Carmichael numbers).  If $a$ passes the test (therefore is probably prime) $result$ is set to one.  
1615Otherwise $result$ is set to zero.  
1616
1617Note that is suggested that you use the Miller-Rabin test instead of the Fermat test since all of the failures of 
1618Miller-Rabin are a subset of the failures of the Fermat test.
1619
1620\subsection{Required Number of Tests}
1621Generally to ensure a number is very likely to be prime you have to perform the Miller-Rabin with at least a half-dozen
1622or so unique bases.  However, it has been proven that the probability of failure goes down as the size of the input goes up.
1623This is why a simple function has been provided to help out.
1624
1625\index{mp\_prime\_rabin\_miller\_trials}
1626\begin{alltt}
1627int mp_prime_rabin_miller_trials(int size)
1628\end{alltt}
1629This returns the number of trials required for a $2^{-96}$ (or lower) probability of failure for a given ``size'' expressed
1630in bits.  This comes in handy specially since larger numbers are slower to test.  For example, a 512-bit number would
1631require ten tests whereas a 1024-bit number would only require four tests. 
1632
1633You should always still perform a trial division before a Miller-Rabin test though.
1634
1635\section{Primality Testing}
1636\index{mp\_prime\_is\_prime}
1637\begin{alltt}
1638int mp_prime_is_prime (mp_int * a, int t, int *result)
1639\end{alltt}
1640This will perform a trial division followed by $t$ rounds of Miller-Rabin tests on $a$ and store the result in $result$.  
1641If $a$ passes all of the tests $result$ is set to one, otherwise it is set to zero.  Note that $t$ is bounded by 
1642$1 \le t < PRIME\_SIZE$ where $PRIME\_SIZE$ is the number of primes in the prime number table (by default this is $256$).
1643
1644\section{Next Prime}
1645\index{mp\_prime\_next\_prime}
1646\begin{alltt}
1647int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
1648\end{alltt}
1649This finds the next prime after $a$ that passes mp\_prime\_is\_prime() with $t$ tests.  Set $bbs\_style$ to one if you 
1650want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to zero to find any next prime.  
1651
1652\section{Random Primes}
1653\index{mp\_prime\_random}
1654\begin{alltt}
1655int mp_prime_random(mp_int *a, int t, int size, int bbs, 
1656                    ltm_prime_callback cb, void *dat)
1657\end{alltt}
1658This will find a prime greater than $256^{size}$ which can be ``bbs\_style'' or not depending on $bbs$ and must pass
1659$t$ rounds of tests.  The ``ltm\_prime\_callback'' is a typedef for 
1660
1661\begin{alltt}
1662typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
1663\end{alltt}
1664
1665Which is a function that must read $len$ bytes (and return the amount stored) into $dst$.  The $dat$ variable is simply
1666copied from the original input.  It can be used to pass RNG context data to the callback.  The function 
1667mp\_prime\_random() is more suitable for generating primes which must be secret (as in the case of RSA) since there 
1668is no skew on the least significant bits.
1669
1670\textit{Note:}  As of v0.30 of the LibTomMath library this function has been deprecated.  It is still available
1671but users are encouraged to use the new mp\_prime\_random\_ex() function instead.
1672
1673\subsection{Extended Generation}
1674\index{mp\_prime\_random\_ex}
1675\begin{alltt}
1676int mp_prime_random_ex(mp_int *a,    int t, 
1677                       int     size, int flags, 
1678                       ltm_prime_callback cb, void *dat);
1679\end{alltt}
1680This will generate a prime in $a$ using $t$ tests of the primality testing algorithms.  The variable $size$
1681specifies the bit length of the prime desired.  The variable $flags$ specifies one of several options available
1682(see fig. \ref{fig:primeopts}) which can be OR'ed together.  The callback parameters are used as in 
1683mp\_prime\_random().
1684
1685\begin{figure}[here]
1686\begin{center}
1687\begin{small}
1688\begin{tabular}{|r|l|}
1689\hline \textbf{Flag}         & \textbf{Meaning} \\
1690\hline LTM\_PRIME\_BBS       & Make the prime congruent to $3$ modulo $4$ \\
1691\hline LTM\_PRIME\_SAFE      & Make a prime $p$ such that $(p - 1)/2$ is also prime. \\
1692                             & This option implies LTM\_PRIME\_BBS as well. \\
1693\hline LTM\_PRIME\_2MSB\_OFF & Makes sure that the bit adjacent to the most significant bit \\
1694                             & Is forced to zero.  \\
1695\hline LTM\_PRIME\_2MSB\_ON  & Makes sure that the bit adjacent to the most significant bit \\
1696                             & Is forced to one. \\
1697\hline
1698\end{tabular}
1699\end{small}
1700\end{center}
1701\caption{Primality Generation Options}
1702\label{fig:primeopts}
1703\end{figure}
1704
1705\chapter{Input and Output}
1706\section{ASCII Conversions}
1707\subsection{To ASCII}
1708\index{mp\_toradix}
1709\begin{alltt}
1710int mp_toradix (mp_int * a, char *str, int radix);
1711\end{alltt}
1712This still store $a$ in ``str'' as a base-``radix'' string of ASCII chars.  This function appends a NUL character
1713to terminate the string.  Valid values of ``radix'' line in the range $[2, 64]$.  To determine the size (exact) required
1714by the conversion before storing any data use the following function.
1715
1716\index{mp\_radix\_size}
1717\begin{alltt}
1718int mp_radix_size (mp_int * a, int radix, int *size)
1719\end{alltt}
1720This stores in ``size'' the number of characters (including space for the NUL terminator) required.  Upon error this 
1721function returns an error code and ``size'' will be zero.  
1722
1723\subsection{From ASCII}
1724\index{mp\_read\_radix}
1725\begin{alltt}
1726int mp_read_radix (mp_int * a, char *str, int radix);
1727\end{alltt}
1728This will read the base-``radix'' NUL terminated string from ``str'' into $a$.  It will stop reading when it reads a
1729character it does not recognize (which happens to include th NUL char... imagine that...).  A single leading $-$ sign
1730can be used to denote a negative number.
1731
1732\section{Binary Conversions}
1733
1734Converting an mp\_int to and from binary is another keen idea.
1735
1736\index{mp\_unsigned\_bin\_size}
1737\begin{alltt}
1738int mp_unsigned_bin_size(mp_int *a);
1739\end{alltt}
1740
1741This will return the number of bytes (octets) required to store the unsigned copy of the integer $a$.
1742
1743\index{mp\_to\_unsigned\_bin}
1744\begin{alltt}
1745int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
1746\end{alltt}
1747This will store $a$ into the buffer $b$ in big--endian format.  Fortunately this is exactly what DER (or is it ASN?)
1748requires.  It does not store the sign of the integer.
1749
1750\index{mp\_read\_unsigned\_bin}
1751\begin{alltt}
1752int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
1753\end{alltt}
1754This will read in an unsigned big--endian array of bytes (octets) from $b$ of length $c$ into $a$.  The resulting
1755integer $a$ will always be positive.
1756
1757For those who acknowledge the existence of negative numbers (heretic!) there are ``signed'' versions of the
1758previous functions.
1759
1760\begin{alltt}
1761int mp_signed_bin_size(mp_int *a);
1762int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
1763int mp_to_signed_bin(mp_int *a, unsigned char *b);
1764\end{alltt}
1765They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero
1766byte depending on the sign.  If the sign is zpos (e.g. not negative) the prefix is zero, otherwise the prefix
1767is non--zero.  
1768
1769\chapter{Algebraic Functions}
1770\section{Extended Euclidean Algorithm}
1771\index{mp\_exteuclid}
1772\begin{alltt}
1773int mp_exteuclid(mp_int *a, mp_int *b, 
1774                 mp_int *U1, mp_int *U2, mp_int *U3);
1775\end{alltt}
1776
1777This finds the triple U1/U2/U3 using the Extended Euclidean algorithm such that the following equation holds.
1778
1779\begin{equation}
1780a \cdot U1 + b \cdot U2 = U3
1781\end{equation}
1782
1783Any of the U1/U2/U3 paramters can be set to \textbf{NULL} if they are not desired.  
1784
1785\section{Greatest Common Divisor}
1786\index{mp\_gcd}
1787\begin{alltt}
1788int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
1789\end{alltt}
1790This will compute the greatest common divisor of $a$ and $b$ and store it in $c$.
1791
1792\section{Least Common Multiple}
1793\index{mp\_lcm}
1794\begin{alltt}
1795int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
1796\end{alltt}
1797This will compute the least common multiple of $a$ and $b$ and store it in $c$.
1798
1799\section{Jacobi Symbol}
1800\index{mp\_jacobi}
1801\begin{alltt}
1802int mp_jacobi (mp_int * a, mp_int * p, int *c)
1803\end{alltt}
1804This will compute the Jacobi symbol for $a$ with respect to $p$.  If $p$ is prime this essentially computes the Legendre
1805symbol.  The result is stored in $c$ and can take on one of three values $\lbrace -1, 0, 1 \rbrace$.  If $p$ is prime
1806then the result will be $-1$ when $a$ is not a quadratic residue modulo $p$.  The result will be $0$ if $a$ divides $p$
1807and the result will be $1$ if $a$ is a quadratic residue modulo $p$.  
1808
1809\section{Modular Inverse}
1810\index{mp\_invmod}
1811\begin{alltt}
1812int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
1813\end{alltt}
1814Computes the multiplicative inverse of $a$ modulo $b$ and stores the result in $c$ such that $ac \equiv 1 \mbox{ (mod }b\mbox{)}$.
1815
1816\section{Single Digit Functions}
1817
1818For those using small numbers (\textit{snicker snicker}) there are several ``helper'' functions
1819
1820\index{mp\_add\_d} \index{mp\_sub\_d} \index{mp\_mul\_d} \index{mp\_div\_d} \index{mp\_mod\_d}
1821\begin{alltt}
1822int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
1823int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
1824int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
1825int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
1826int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
1827\end{alltt}
1828
1829These work like the full mp\_int capable variants except the second parameter $b$ is a mp\_digit.  These
1830functions fairly handy if you have to work with relatively small numbers since you will not have to allocate
1831an entire mp\_int to store a number like $1$ or $2$.
1832
1833\input{bn.ind}
1834
1835\end{document}
1836