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