diff --git a/doc/bn.tex b/doc/bn.tex index 8187de8..292f680 100644 --- a/doc/bn.tex +++ b/doc/bn.tex @@ -91,7 +91,7 @@ release the textbook ``Implementing Multiple Precision Arithmetic'' has been pla release as well. This textbook is meant to compliment the project by providing a more solid walkthrough of the development algorithms used in the library. -Since both\footnote{Note that the MPI files under mtest/ are copyrighted by Michael Fromberger. They are not required to use LibTomMath.} are in the +Since both\footnote{Note that the MPI files under \texttt{mtest/} are copyrighted by Michael Fromberger. They are not required to use LibTomMath.} are in the public domain everyone is entitled to do with them as they see fit. \section{Building LibTomMath} @@ -142,7 +142,7 @@ make -f makefile.shared \end{alltt} This requires the ``libtool'' package (common on most Linux/BSD systems). It will build LibTomMath as both shared and static then install (by default) into /usr/lib as well as install the header files in /usr/include. The shared -library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''. Generally +library (resource) will be called \texttt{libtommath.la} while the static library called \texttt{libtommath.a}. Generally you use libtool to link your application against the shared object. To run a program to adapt the Toom-Cook cut-off values to your architecture type @@ -152,9 +152,9 @@ make -f makefile.shared tune This will take some time. \subsubsection{Microsoft Windows based Operating Systems} -There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile. It requires +There is limited support for making a ``DLL'' in windows via the \texttt{makefile.cygwin\_dll} makefile. It requires Cygwin to work with since it requires the auto-export/import functionality. The resulting DLL and import library -``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin. +\texttt{libtommath.dll.a} can be used to link LibTomMath dynamically to any Windows program using Cygwin. \subsubsection{OpenBSD} OpenBSD replaced some of their GNU-tools, especially \texttt{libtool} with their own, slightly different versions. To ease the workload of LibTomMath's developer team, only a static library can be build with the included \texttt{makefile.unix}. @@ -223,16 +223,16 @@ To build the library and the test harness type make test \end{alltt} -This will build the library, ``test'' and ``mtest/mtest''. The ``test'' program will accept test vectors and verify the -results. ``mtest/mtest'' will generate test vectors using the MPI library by Michael Fromberger\footnote{A copy of MPI -is included in the package}. Simply pipe mtest into test using +This will build the library, \texttt{test} and \texttt{mtest/mtest}. The \texttt{test} program will accept test vectors and verify the +results. \texttt{mtest/mtest} will generate test vectors using the MPI library by Michael Fromberger\footnote{A copy of MPI +is included in the package}. Simply pipe \texttt{mtest} into \texttt{test} using \begin{alltt} mtest/mtest | test \end{alltt} -If you do not have a ``/dev/urandom'' style RNG source you will have to write your own PRNG and simply pipe that into -mtest. For example, if your PRNG program is called ``myprng'' simply invoke +If you do not have a \texttt{/dev/urandom} style RNG source you will have to write your own PRNG and simply pipe that into +\texttt{mtest}. For example, if your PRNG program is called \texttt{myprng} simply invoke \begin{alltt} myprng | mtest/mtest | test @@ -247,24 +247,24 @@ LibTomMath can configured at build time in three phases we shall call ``depends' Each phase changes how the library is built and they are applied one after another respectively. To make the system more powerful you can tweak the build process. Classes are defined in the file -``tommath\_superclass.h''. By default, the symbol ``LTM\_ALL'' shall be defined which simply +\texttt{tommath\_superclass.h}. By default, the symbol \texttt{LTM\_ALL} shall be defined which simply instructs the system to build all of the functions. This is how LibTomMath used to be packaged. This will give you access to every function LibTomMath offers. However, there are cases where such a build is not optional. For instance, you want to perform RSA operations. You -don't need the vast majority of the library to perform these operations. Aside from LTM\_ALL there is -another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt. Additional +don't need the vast majority of the library to perform these operations. Aside from \texttt{LTM\_ALL} there is +another pre--defined class \texttt{SC\_RSA\_1} which works in conjunction with the RSA from LibTomCrypt. Additional classes can be defined base on the need of the user. \subsection{Build Depends} -In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs'' +In the file \texttt{tommath\_class.h} you will see a large list of C ``defines'' followed by a series of ``ifdefs'' which further define symbols. All of the symbols (technically they're macros $\ldots$) represent a given C source -file. For instance, MP\_ADD\_C represents the file ``bn\_mp\_add.c''. When a define has been enabled the +file. For instance, \texttt{MP\_ADD\_C} represents the file \texttt{mp\_add.c}. When a define has been enabled the function in the respective file will be compiled and linked into the library. Accordingly when the define is absent the file will not be compiled and not contribute any size to the library. -You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice). -This is to help resolve as many dependencies as possible. In the last pass the symbol LTM\_LAST will be defined. +You will also note that the header \texttt{tommath\_class.h} is actually recursively included (it includes itself twice). +This is to help resolve as many dependencies as possible. In the last pass the symbol \texttt{LTM\_LAST} will be defined. This is useful for ``trims''. \subsection{Build Tweaks} @@ -286,7 +286,7 @@ They can be enabled at any pass of the configuration phase. A trim is a manner of removing functionality from a function that is not required. For instance, to perform RSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed. Build trims are meant to be defined on the last pass of the configuration which means they are to be defined -only if LTM\_LAST has been defined. +only if \texttt{LTM\_LAST} has been defined. \subsubsection{Moduli Related} \begin{small} @@ -388,9 +388,9 @@ In order to use LibTomMath you must include ``tommath.h'' and link against the a libtommath.a). There is no library initialization required and the entire library is thread safe. \section{Return Codes} -There are three possible return codes a function may return. +There are five possible return codes a function may return. -\index{MP\_OKAY}\index{MP\_YES}\index{MP\_NO}\index{MP\_VAL}\index{MP\_MEM} +\index{MP\_OKAY}\index{MP\_YES}\index{MP\_NO}\index{MP\_VAL}\index{MP\_MEM}\index{MP\_ITER}\index{MP\_BUF} \begin{figure}[h!] \begin{center} \begin{small} @@ -399,6 +399,8 @@ There are three possible return codes a function may return. \hline MP\_OKAY & The function succeeded. \\ \hline MP\_VAL & The function input was invalid. \\ \hline MP\_MEM & Heap memory exhausted. \\ +\hline MP\_ITER & Maximum iterations reached. \\ +\hline MP\_BUF & Buffer overflow, supplied buffer too small.\\ \hline &\\ \hline MP\_YES & Response is yes. \\ \hline MP\_NO & Response is no. \\ @@ -409,6 +411,8 @@ There are three possible return codes a function may return. \caption{Return Codes} \end{figure} +The error codes \texttt{MP\_OKAY},\texttt{MP\_VAL}, \texttt{MP\_MEM}, \texttt{MP\_ITER}, and \texttt{MP\_BUF} are of the type \texttt{mp\_err}, the codes \texttt{MP\_YES} and \texttt{MP\_NO} are of type \texttt{mp\_bool}. + The last two codes listed are not actually ``return'ed'' by a function. They are placed in an integer (the caller must provide the address of an integer it can store to) which the caller can access. To convert one of the three return codes to a string use the following function. @@ -418,29 +422,29 @@ to a string use the following function. char *mp_error_to_string(int code); \end{alltt} -This will return a pointer to a string which describes the given error code. It will not work for the return codes -MP\_YES and MP\_NO. +This will return a pointer to a string which describes the given error code. It will not work for the return codes \texttt{MP\_YES} and \texttt{MP\_NO}. \section{Data Types} -The basic ``multiple precision integer'' type is known as the ``mp\_int'' within LibTomMath. This data type is used to +The basic ``multiple precision integer'' type is known as the \texttt{mp\_int} within LibTomMath. This data type is used to organize all of the data required to manipulate the integer it represents. Within LibTomMath it has been prototyped as the following. \index{mp\_int} \begin{alltt} typedef struct \{ - int used, alloc, sign; - mp_digit *dp; + int used, alloc; + mp_sign sign; + mp_digit *dp; \} mp_int; \end{alltt} -Where ``mp\_digit'' is a data type that represents individual digits of the integer. By default, an mp\_digit is the -ISO C ``unsigned long'' data type and each digit is $28-$bits long. The mp\_digit type can be configured to suit other +Where \texttt{mp\_digit} is a data type that represents individual digits of the integer. By default, an \texttt{mp\_digit} is the +ISO C \texttt{unsigned long} data type and each digit is $28-$bits long. The \texttt{mp\_digit} type can be configured to suit other platforms by defining the appropriate macros. -All LTM functions that use the mp\_int type will expect a pointer to mp\_int structure. You must allocate memory to +All LTM functions that use the \texttt{mp\_int} type will expect a pointer to \texttt{mp\_int} structure. You must allocate memory to hold the structure itself by yourself (whether off stack or heap it doesn't matter). The very first thing that must be -done to use an mp\_int is that it must be initialized. +done to use an \texttt{mp\_int} is that it must be initialized. \section{Function Organization} @@ -465,22 +469,23 @@ This allows operands to be re-used which can make programming simpler. \section{Initialization} \subsection{Single Initialization} -A single mp\_int can be initialized with the ``mp\_init'' function. +A single \texttt{mp\_int} can be initialized with the \texttt{mp\_init} function. \index{mp\_init} \begin{alltt} -int mp_init (mp_int * a); +mp_err mp_init (mp_int *a); \end{alltt} -This function expects a pointer to an mp\_int structure and will initialize the members of the structure so the mp\_int -represents the default integer which is zero. If the functions returns MP\_OKAY then the mp\_int is ready to be used +This function expects a pointer to an \texttt{mp\_int} structure and will initialize the members of the structure so the \texttt{mp\_int} +represents the default integer which is zero. If the functions returns \texttt{MP\_OKAY} then the \texttt{mp\_int} is ready to be used by the other LibTomMath functions. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -492,26 +497,28 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} \subsection{Single Free} -When you are finished with an mp\_int it is ideal to return the heap it used back to the system. The following function +When you are finished with an \texttt{mp\_int} it is ideal to return the heap it used back to the system. The following function provides this functionality. \index{mp\_clear} \begin{alltt} -void mp_clear (mp_int * a); +void mp_clear (mp_int *a); \end{alltt} -The function expects a pointer to a previously initialized mp\_int structure and frees the heap it uses. It sets the -pointer\footnote{The ``dp'' member.} within the mp\_int to \textbf{NULL} which is used to prevent double free situations. -Is is legal to call mp\_clear() twice on the same mp\_int in a row. +The function expects a pointer to a previously initialized \texttt{mp\_int} structure and frees the heap it uses. It sets the +pointer\footnote{The \texttt{dp} member.} within the \texttt{mp\_int} to \texttt{NULL} which is used to prevent double free situations. +Is is legal to call \texttt{mp\_clear} twice on the same \texttt{mp\_int} in a row. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -526,30 +533,32 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} \subsection{Multiple Initializations} -Certain algorithms require more than one large integer. In these instances it is ideal to initialize all of the mp\_int +Certain algorithms require more than one large integer. In these instances it is ideal to initialize all of the \texttt{mp\_int} variables in an ``all or nothing'' fashion. That is, they are either all initialized successfully or they are all not initialized. -The mp\_init\_multi() function provides this functionality. +The \texttt{mp\_init\_multi} function provides this functionality. \index{mp\_init\_multi} \index{mp\_clear\_multi} \begin{alltt} -int mp_init_multi(mp_int *mp, ...); +mp_err mp_init_multi(mp_int *mp, ...); \end{alltt} -It accepts a \textbf{NULL} terminated list of pointers to mp\_int structures. It will attempt to initialize them all -at once. If the function returns MP\_OKAY then all of the mp\_int variables are ready to use, otherwise none of them -are available for use. A complementary mp\_clear\_multi() function allows multiple mp\_int variables to be free'd +It accepts a \texttt{NULL} terminated list of pointers to \texttt{mp\_int} structures. It will attempt to initialize them all +at once. If the function returns \texttt{MP\_OKAY} then all of the \texttt{mp\_int} variables are ready to use, otherwise none of them +are available for use. A complementary \texttt{mp\_clear\_multi} function allows multiple \texttt{mp\_int} variables to be free'd from the heap at the same time. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int num1, num2, num3; - int result; + mp_err result; if ((result = mp_init_multi(&num1, &num2, @@ -566,23 +575,25 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} \subsection{Other Initializers} -To initialized and make a copy of an mp\_int the mp\_init\_copy() function has been provided. +To initialized and make a copy of an \texttt{mp\_int} the \texttt{mp\_init\_copy} function has been provided. \index{mp\_init\_copy} \begin{alltt} -int mp_init_copy (mp_int * a, mp_int * b); +mp_err mp_init_copy (mp_int *a, mp_int *b); \end{alltt} This function will initialize $a$ and make it a copy of $b$ if all goes well. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int num1, num2; - int result; + mp_err result; /* initialize and do work on num1 ... */ @@ -600,25 +611,27 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} -Another less common initializer is mp\_init\_size() which allows the user to initialize an mp\_int with a given -default number of digits. By default, all initializers allocate \textbf{MP\_PREC} digits. This function lets +Another less common initializer is \texttt{mp\_init\_size} which allows the user to initialize an \texttt{mp\_int} with a given +default number of digits. By default, all initializers allocate \texttt{MP\_PREC} digits. This function lets you override this behaviour. \index{mp\_init\_size} \begin{alltt} -int mp_init_size (mp_int * a, int size); +mp_err mp_init_size (mp_int *a, int size); \end{alltt} -The $size$ parameter must be greater than zero. If the function succeeds the mp\_int $a$ will be initialized +The $size$ parameter must be greater than zero. If the function succeeds the \texttt{mp\_int} $a$ will be initialized to have $size$ digits (which are all initially zero). -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; /* we need a 60-digit number */ if ((result = mp_init_size(&number, 60)) != MP_OKAY) \{ @@ -631,12 +644,13 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} \section{Maintenance Functions} \subsection{Clear Leading Zeros} -This is used to ensure that leading zero digits are trimed and the leading "used" digit will be non-zero. +This is used to ensure that leading zero digits are trimmed and the leading "used" digit will be non-zero. It also fixes the sign if there are no more leading digits. \index{mp\_clamp} @@ -655,24 +669,25 @@ void mp_zero(mp_int *a); \subsection{Reducing Memory Usage} -When an mp\_int is in a state where it won't be changed again\footnote{A Diffie-Hellman modulus for instance.} excess -digits can be removed to return memory to the heap with the mp\_shrink() function. +When an \texttt{mp\_int} is in a state where it won't be changed again\footnote{A Diffie-Hellman modulus for instance.} excess +digits can be removed to return memory to the heap with the \texttt{mp\_shrink} function. \index{mp\_shrink} \begin{alltt} -int mp_shrink (mp_int * a); +mp_err mp_shrink (mp_int *a); \end{alltt} -This will remove excess digits of the mp\_int $a$. If the operation fails the mp\_int should be intact without the -excess digits being removed. Note that you can use a shrunk mp\_int in further computations, however, such operations -will require heap operations which can be slow. It is not ideal to shrink mp\_int variables that you will further +This will remove excess digits of the \texttt{mp\_int} $a$. If the operation fails the \texttt{mp\_int} should be intact without the +excess digits being removed. Note that you can use a shrunk \texttt{mp\_int} in further computations, however, such operations +will require heap operations which can be slow. It is not ideal to shrink \texttt{mp\_int} variables that you will further modify in the system (unless you are seriously low on memory). -\begin{small} \begin{alltt} +\begin{small} + \begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -697,29 +712,31 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} + \end{small} \subsection{Adding additional digits} Within the mp\_int structure are two parameters which control the limitations of the array of digits that represent -the integer the mp\_int is meant to equal. The \textit{used} parameter dictates how many digits are significant, that is, -contribute to the value of the mp\_int. The \textit{alloc} parameter dictates how many digits are currently available in +the integer the mp\_int is meant to equal. The \texttt{used} parameter dictates how many digits are significant, that is, +contribute to the value of the mp\_int. The \texttt{alloc} parameter dictates how many digits are currently available in the array. If you need to perform an operation that requires more digits you will have to mp\_grow() the mp\_int to your desired size. \index{mp\_grow} \begin{alltt} -int mp_grow (mp_int * a, int size); +mp_err mp_grow (mp_int *a, int size); \end{alltt} -This will grow the array of digits of $a$ to $size$. If the \textit{alloc} parameter is already bigger than +This will grow the array of digits of $a$ to $size$. If the \texttt{alloc} parameter is already bigger than $size$ the function will not do anything. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -744,7 +761,8 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} \chapter{Basic Operations} \section{Copying} @@ -753,14 +771,14 @@ A so called ``deep copy'', where new memory is allocated and all contents of $a$ \index{mp\_copy} \begin{alltt} -int mp_copy (mp_int * a, mp_int *b); +mp_err mp_copy (const mp_int *a, mp_int *b); \end{alltt} You can also just swap $a$ and $b$. It does the normal pointer changing with a temporary pointer variable, just that you do not have to. \index{mp\_exch} \begin{alltt} -void mp_exch (mp_int * a, mp_int *b); +void mp_exch (mp_int *a, mp_int *b); \end{alltt} \section{Bit Counting} @@ -772,7 +790,7 @@ To get the position of the lowest bit set (LSB, the Lowest Significant Bit; the int mp_cnt_lsb(const mp_int *a); \end{alltt} -To get the position of the highest bit set (MSB, the Most Significant Bit; the number of bits in teh ``bignum'') +To get the position of the highest bit set (MSB, the Most Significant Bit; the number of bits in the ``bignum'') \index{mp\_count\_bits} \begin{alltt} @@ -781,7 +799,7 @@ int mp_count_bits(const mp_int *a); \section{Small Constants} -Setting mp\_ints to small constants is a relatively common operation. To accommodate these instances there is a +Setting an \texttt{mp\_int} to a small constant is a relatively common operation. To accommodate these instances there is a small constant assignment function. This function is used to set a single digit constant. The reason for this function is efficiency. Setting a single digit is quick but the domain of a digit can change (it's always at least $0 \ldots 127$). @@ -792,18 +810,19 @@ Setting a single digit can be accomplished with the following function. \index{mp\_set} \begin{alltt} -void mp_set (mp_int * a, mp_digit b); +void mp_set (mp_int *a, mp_digit b); \end{alltt} This will zero the contents of $a$ and make it represent an integer equal to the value of $b$. Note that this -function has a return type of \textbf{void}. It cannot cause an error so it is safe to assume the function +function has a return type of \texttt{void}. It cannot cause an error so it is safe to assume the function succeeded. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -819,7 +838,8 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} \subsection{Int32 and Int64 Constants} @@ -828,34 +848,35 @@ These functions can be used to set a constant with 32 or 64 bits. \index{mp\_set\_i32} \index{mp\_set\_u32} \index{mp\_set\_i64} \index{mp\_set\_u64} \begin{alltt} -void mp_set_i32 (mp_int * a, int32_t b); -void mp_set_u32 (mp_int * a, uint32_t b); -void mp_set_i64 (mp_int * a, int64_t b); -void mp_set_u64 (mp_int * a, uint64_t b); +void mp_set_i32 (mp_int *a, int32_t b); +void mp_set_u32 (mp_int *a, uint32_t b); +void mp_set_i64 (mp_int *a, int64_t b); +void mp_set_u64 (mp_int *a, uint64_t b); \end{alltt} -These functions assign the sign and value of the input \texttt{b} to \texttt{mp\_int a}. +These functions assign the sign and value of the input $b$ to the big integer $a$. The value can be obtained again by calling the following functions. \index{mp\_get\_i32} \index{mp\_get\_u32} \index{mp\_get\_mag\_u32} \index{mp\_get\_i64} \index{mp\_get\_u64} \index{mp\_get\_mag\_u64} \begin{alltt} -int32_t mp_get_i32 (mp_int * a); -uint32_t mp_get_u32 (mp_int * a); -uint32_t mp_get_mag_u32 (mp_int * a); -int64_t mp_get_i64 (mp_int * a); -uint64_t mp_get_u64 (mp_int * a); -uint64_t mp_get_mag_u64 (mp_int * a); +int32_t mp_get_i32 (const mp_int *a); +uint32_t mp_get_u32 (const mp_int *a); +uint32_t mp_get_mag_u32 (const mp_int *a); +int64_t mp_get_i64 (const mp_int *a); +uint64_t mp_get_u64 (const mp_int *a); +uint64_t mp_get_mag_u64 (const mp_int *a); \end{alltt} These functions return the 32 or 64 least significant bits of $a$ respectively. The unsigned functions -return negative values in a twos complement representation. The absolute value or magnitude can be obtained using the mp\_get\_mag functions. +return negative values in a twos complement representation. The absolute value or magnitude can be obtained using the \texttt{mp\_get\_mag*} functions. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -866,14 +887,15 @@ int main(void) /* set the number to 654321 (note this is bigger than 127) */ mp_set_u32(&number, 654321); - printf("number == \%" PRIi32, mp_get_i32(&number)); + printf("number == \%" PRIi32 "\textbackslash{}n", mp_get_i32(&number)); /* we're done with it. */ mp_clear(&number); return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} This should output the following if the program succeeds. @@ -885,32 +907,32 @@ number == 654321 \index{mp\_set\_l} \index{mp\_set\_ul} \begin{alltt} -void mp_set_l (mp_int * a, long b); -void mp_set_ul (mp_int * a, unsigned long b); +void mp_set_l (mp_int *a, long b); +void mp_set_ul (mp_int *a, unsigned long b); \end{alltt} -This will assign the value of the platform-dependent sized variable $b$ to the mp\_int $a$. +This will assign the value of the platform-dependent sized variable $b$ to the big integer $a$. To retrieve the value, the following functions can be used. \index{mp\_get\_l} \index{mp\_get\_ul} \index{mp\_get\_mag\_ul} \begin{alltt} -long mp_get_l (mp_int * a); -unsigned long mp_get_ul (mp_int * a); -unsigned long mp_get_mag_ul (mp_int * a); +long mp_get_l (const mp_int *a); +unsigned long mp_get_ul (const mp_int *a); +unsigned long mp_get_mag_ul (const mp_int *a); \end{alltt} -This will return the least significant bits of the mp\_int $a$ that fit into a ``long''. +This will return the least significant bits of the big integer $a$ that fit into the native data type \texttt{long}. \subsection{Long Long Constants - platform dependant} \index{mp\_set\_ll} \index{mp\_set\_ull} \begin{alltt} -void mp_set_ll (mp_int * a, long long b); -void mp_set_ull (mp_int * a, unsigned long long b); +void mp_set_ll (mp_int *a, long long b); +void mp_set_ull (mp_int *a, unsigned long long b); \end{alltt} -This will assign the value of the platform-dependent sized variable $b$ to the mp\_int $a$. +This will assign the value of the platform-dependent sized variable $b$ to the big integer $a$. To retrieve the value, the following functions can be used. @@ -918,35 +940,52 @@ To retrieve the value, the following functions can be used. \index{mp\_get\_ull} \index{mp\_get\_mag\_ull} \begin{alltt} -long long mp_get_ll (mp_int * a); -unsigned long long mp_get_ull (mp_int * a); -unsigned long long mp_get_mag_ull (mp_int * a); +long long mp_get_ll (const mp_int *a); +unsigned long long mp_get_ull (const mp_int *a); +unsigned long long mp_get_mag_ull (const mp_int *a); +\end{alltt} + +This will return the least significant bits of $a$ that fit into the native data type \texttt{long long}. + +\subsection{Floating Point Constants - platform dependant} + +\index{mp\_set\_double} +\begin{alltt} +mp_err mp_set_double(mp_int *a, double b); +\end{alltt} + +If the platform supports the floating point data type \texttt{double} (binary64) this function will assign the integer part of \texttt{b} to the big integer $a$. This function will return \texttt{MP\_VAL} if \texttt{b} is \texttt{+/-inf} or \texttt{NaN}. + +To convert a big integer to a \texttt{double} use + +\index{mp\_get\_double} +\begin{alltt} +double mp_get_double(const mp_int *a); \end{alltt} -This will return the least significant bits of the mp\_int $a$ that fit into a ``long long''. \subsection{Initialize and Setting Constants} -To both initialize and set small constants the following two functions are available. +To both initialize and set small constants the following nine functions are available. \index{mp\_init\_set} \index{mp\_init\_set\_int} \begin{alltt} -int mp_init_set (mp_int * a, mp_digit b); -int mp_init_i32 (mp_int * a, int32_t b); -int mp_init_u32 (mp_int * a, uint32_t b); -int mp_init_i64 (mp_int * a, int64_t b); -int mp_init_u64 (mp_int * a, uint64_t b); -int mp_init_l (mp_int * a, long b); -int mp_init_ul (mp_int * a, unsigned long b); -int mp_init_ll (mp_int * a, long long b); -int mp_init_ull (mp_int * a, unsigned long long b); +mp_err mp_init_set (mp_int *a, mp_digit b); +mp_err mp_init_i32 (mp_int *a, int32_t b); +mp_err mp_init_u32 (mp_int *a, uint32_t b); +mp_err mp_init_i64 (mp_int *a, int64_t b); +mp_err mp_init_u64 (mp_int *a, uint64_t b); +mp_err mp_init_l (mp_int *a, long b); +mp_err mp_init_ul (mp_int *a, unsigned long b); +mp_err mp_init_ll (mp_int *a, long long b); +mp_err mp_init_ull (mp_int *a, unsigned long long b); \end{alltt} -Both functions work like the previous counterparts except they first mp\_init $a$ before setting the values. +Both functions work like the previous counterparts except they first initialize $a$ with the function \texttt{mp\_init} before setting the values. \begin{alltt} int main(void) \{ mp_int number1, number2; - int result; + mp_err result; /* initialize and set a single digit */ if ((result = mp_init_set(&number1, 100)) != MP_OKAY) \{ @@ -963,7 +1002,7 @@ int main(void) \} /* display */ - printf("Number1, Number2 == \%" PRIi32 ", \%" PRIi32, + printf("Number1, Number2 == \%" PRIi32 ", \%" PRIi32 "\textbackslash{}n", mp_get_i32(&number1), mp_get_i32(&number2)); /* clear */ @@ -978,6 +1017,8 @@ If this program succeeds it shall output. Number1, Number2 == 100, 1023 \end{alltt} + + \section{Comparisons} Comparisons in LibTomMath are always performed in a ``left to right'' fashion. There are three possible return codes @@ -999,26 +1040,27 @@ for any comparison. \end{figure} In figure \ref{fig:CMP} two integers $a$ and $b$ are being compared. In this case $a$ is said to be ``to the left'' of -$b$. +$b$. The return codes are of type \texttt{mp\_ord}. \subsection{Unsigned comparison} -An unsigned comparison considers only the digits themselves and not the associated \textit{sign} flag of the -mp\_int structures. This is analogous to an absolute comparison. The function mp\_cmp\_mag() will compare two -mp\_int variables based on their digits only. +An unsigned comparison considers only the digits themselves and not the associated \texttt{sign} flag of the +\texttt{mp\_int} structures. This is analogous to an absolute comparison. The function \texttt{mp\_cmp\_mag} will compare two +\texttt{mp\_int} variables based on their digits only. \index{mp\_cmp\_mag} \begin{alltt} -int mp_cmp_mag(mp_int * a, mp_int * b); +mp_ord mp_cmp_mag(mp_int *a, mp_int *b); \end{alltt} This will compare $a$ to $b$ placing $a$ to the left of $b$. This function cannot fail and will return one of the three compare codes listed in figure \ref{fig:CMP}. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number1, number2; - int result; + mp_err result; if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{ printf("Error initializing the numbers. \%s", @@ -1048,9 +1090,10 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} -If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes +If this program\footnote{This function uses the \texttt{mp\_neg} function which is discussed in section \ref{sec:NEG}.} completes successfully it should print the following. \begin{alltt} @@ -1061,22 +1104,23 @@ This is because $\vert -6 \vert = 6$ and obviously $5 < 6$. \subsection{Signed comparison} -To compare two mp\_int variables based on their signed value the mp\_cmp() function is provided. +To compare two \texttt{mp\_int} variables based on their signed value the \texttt{mp\_cmp} function is provided. \index{mp\_cmp} \begin{alltt} -int mp_cmp(mp_int * a, mp_int * b); +mp_ord mp_cmp(mp_int *a, mp_int *b); \end{alltt} -This will compare $a$ to the left of $b$. It will first compare the signs of the two mp\_int variables. If they +This will compare $a$ to the left of $b$. It will first compare the signs of the two \texttt{mp\_int} variables. If they differ it will return immediately based on their signs. If the signs are equal then it will compare the digits individually. This function will return one of the compare conditions codes listed in figure \ref{fig:CMP}. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number1, number2; - int result; + mp_err result; if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{ printf("Error initializing the numbers. \%s", @@ -1106,9 +1150,10 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} -If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes +If this program\footnote{This function uses the \texttt{mp\_neg} function which is discussed in section \ref{sec:NEG}.} completes successfully it should print the following. \begin{alltt} @@ -1117,11 +1162,11 @@ number1 > number2 \subsection{Single Digit} -To compare a single digit against an mp\_int the following function has been provided. +To compare a single digit against an \texttt{mp\_int} the following function has been provided. \index{mp\_cmp\_d} \begin{alltt} -int mp_cmp_d(mp_int * a, mp_digit b); +mp_ord mp_cmp_d(mp_int *a, mp_digit b); \end{alltt} This will compare $a$ to the left of $b$ using a signed comparison. Note that it will always treat $b$ as @@ -1130,11 +1175,12 @@ comes up in cryptography). The function cannot fail and will return one of the listed in figure \ref{fig:CMP}. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -1156,7 +1202,8 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} If this program functions properly it will print out the following. @@ -1177,18 +1224,19 @@ right depending on the operation. When multiplying or dividing by two a special case routine can be used which are as follows. \index{mp\_mul\_2} \index{mp\_div\_2} \begin{alltt} -int mp_mul_2(mp_int * a, mp_int * b); -int mp_div_2(mp_int * a, mp_int * b); +mp_err mp_mul_2(const mp_int *a, mp_int *b); +mp_err mp_div_2(const mp_int *a, mp_int *b); \end{alltt} The former will assign twice $a$ to $b$ while the latter will assign half $a$ to $b$. These functions are fast since the shift counts and maskes are hardcoded into the routines. -\begin{small} \begin{alltt} +\begin{small} +\begin{alltt} int main(void) \{ mp_int number; - int result; + mp_err result; if ((result = mp_init(&number)) != MP_OKAY) \{ printf("Error initializing the number. \%s", @@ -1228,7 +1276,8 @@ int main(void) return EXIT_SUCCESS; \} -\end{alltt} \end{small} +\end{alltt} +\end{small} If this program is successful it will print out the following text. @@ -1243,29 +1292,29 @@ To multiply by a power of two the following function can be used. \index{mp\_mul\_2d} \begin{alltt} -int mp_mul_2d(mp_int * a, int b, mp_int * c); +mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c); \end{alltt} -This will multiply $a$ by $2^b$ and store the result in ``c''. If the value of $b$ is less than or equal to -zero the function will copy $a$ to ``c'' without performing any further actions. The multiplication itself +This will multiply $a$ by $2^b$ and store the result in $c$. If the value of $b$ is less than or equal to +zero the function will copy $a$ to $c$ without performing any further actions. The multiplication itself is implemented as a right-shift operation of $a$ by $b$ bits. To divide by a power of two use the following. \index{mp\_div\_2d} \begin{alltt} -int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d); +mp_err mp_div_2d (const mp_int *a, int b, mp_int *c, mp_int *d); \end{alltt} -Which will divide $a$ by $2^b$, store the quotient in ``c'' and the remainder in ``d'. If $b \le 0$ then the -function simply copies $a$ over to ``c'' and zeroes $d$. The variable $d$ may be passed as a \textbf{NULL} +Which will divide $a$ by $2^b$, store the quotient in $c$ and the remainder in $d$. If $b \le 0$ then the +function simply copies $a$ over to $c$ and zeroes $d$. The variable $d$ may be passed as a \texttt{NULL} value to signal that the remainder is not desired. The division itself is implemented as a left-shift operation of $a$ by $b$ bits. -It is also not very uncommon to need just the power of two $2^b$; for example the startvalue for the Newton method. +It is also not very uncommon to need just the power of two $2^b$; for example as a start-value for the Newton method. \index{mp\_2expt} \begin{alltt} -int mp_2expt(mp_int *a, int b); +mp_err mp_2expt(mp_int *a, int b); \end{alltt} It is faster than doing it by shifting $1$ with \texttt{mp\_mul\_2d}. @@ -1281,7 +1330,7 @@ following function provides this operation. \index{mp\_lshd} \begin{alltt} -int mp_lshd (mp_int * a, int b); +mp_err mp_lshd (mp_int *a, int b); \end{alltt} This will multiply $a$ in place by $x^b$ which is equivalent to shifting the digits left $b$ places and inserting zeroes @@ -1289,7 +1338,7 @@ in the least significant digits. Similarly to divide by a power of $x$ the foll \index{mp\_rshd} \begin{alltt} -void mp_rshd (mp_int * a, int b) +void mp_rshd (mp_int *a, int b) \end{alltt} This will divide $a$ in place by $x^b$ and discard the remainder. This function cannot fail as it performs the operations in place and no new digits are required to complete it. @@ -1301,33 +1350,24 @@ are treated as if they are in two-complement representation, while internally th \index{mp\_or} \index{mp\_and} \index{mp\_xor} \index{mp\_complement} \begin{alltt} -int mp_or (mp_int * a, mp_int * b, mp_int * c); -int mp_and (mp_int * a, mp_int * b, mp_int * c); -int mp_xor (mp_int * a, mp_int * b, mp_int * c); -int mp_complement(const mp_int *a, mp_int *b); -int mp_signed_rsh(mp_int * a, int b, mp_int * c, mp_int * d); +mp_err mp_or (const mp_int *a, mp_int *b, mp_int *c); +mp_err mp_and (const mp_int *a, mp_int *b, mp_int *c); +mp_err mp_xor (const mp_int *a, mp_int *b, mp_int *c); +mp_err mp_complement(const mp_int *a, mp_int *b); +mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c, mp_int *d); \end{alltt} The function \texttt{mp\_complement} computes a two-complement $b = \sim a$. The function \texttt{mp\_signed\_rsh} performs sign extending right shift. For positive numbers it is equivalent to \texttt{mp\_div\_2d}. -\subsection{Bit Picking} -\index{mp\_get\_bit} -\begin{alltt} -int mp_get_bit(mp_int *a, int b) -\end{alltt} - -Pick a bit: returns \texttt{MP\_YES} if the bit at position $b$ (0-index) is set, that is if it is 1 (one), \texttt{MP\_NO} -if the bit is 0 (zero) and \texttt{MP\_VAL} if $b < 0$. - \section{Addition and Subtraction} To compute an addition or subtraction the following two functions can be used. \index{mp\_add} \index{mp\_sub} \begin{alltt} -int mp_add (mp_int * a, mp_int * b, mp_int * c); -int mp_sub (mp_int * a, mp_int * b, mp_int * c) +mp_err mp_add (const mp_int *a, const mp_int *b, mp_int *c); +mp_err mp_sub (const mp_int *a, const mp_int *b, mp_int *c) \end{alltt} Which perform $c = a \odot b$ where $\odot$ is one of signed addition or subtraction. The operations are fully sign @@ -1340,7 +1380,7 @@ Simple integer negation can be performed with the following. \index{mp\_neg} \begin{alltt} -int mp_neg (mp_int * a, mp_int * b); +mp_err mp_neg (const mp_int *a, mp_int *b); \end{alltt} Which assigns $-a$ to $b$. @@ -1350,7 +1390,7 @@ Simple integer absolutes can be performed with the following. \index{mp\_abs} \begin{alltt} -int mp_abs (mp_int * a, mp_int * b); +mp_err mp_abs (const mp_int *a, mp_int *b); \end{alltt} Which assigns $\vert a \vert$ to $b$. @@ -1360,12 +1400,12 @@ To perform a complete and general integer division with remainder use the follow \index{mp\_div} \begin{alltt} -int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d); +mp_err mp_div (const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); \end{alltt} This divides $a$ by $b$ and stores the quotient in $c$ and $d$. The signed quotient is computed such that -$bc + d = a$. Note that either of $c$ or $d$ can be set to \textbf{NULL} if their value is not required. If -$b$ is zero the function returns \textbf{MP\_VAL}. +$bc + d = a$. Note that either of $c$ or $d$ can be set to \texttt{NULL} if their value is not required. If +$b$ is zero the function returns \texttt{MP\_VAL}. \chapter{Multiplication and Squaring} @@ -1373,7 +1413,7 @@ $b$ is zero the function returns \textbf{MP\_VAL}. A full signed integer multiplication can be performed with the following. \index{mp\_mul} \begin{alltt} -int mp_mul (mp_int * a, mp_int * b, mp_int * c); +mp_err mp_mul (const mp_int *a, const mp_int *b, mp_int *c); \end{alltt} Which assigns the full signed product $ab$ to $c$. This function actually breaks into one of four cases which are specific multiplication routines optimized for given parameters. First there are the Toom-Cook multiplications which @@ -1387,7 +1427,7 @@ will determine on its own\footnote{Some tweaking may be required but \texttt{mak int main(void) \{ mp_int number1, number2; - int result; + mp_err result; /* Initialize the numbers */ if ((result = mp_init_multi(&number1, @@ -1427,23 +1467,23 @@ number1 * number2 == 262911 \section{Squaring} Since squaring can be performed faster than multiplication it is performed it's own function instead of just using -mp\_mul(). +\texttt{mp\_mul}. \index{mp\_sqr} \begin{alltt} -int mp_sqr (mp_int * a, mp_int * b); +mp_err mp_sqr (const mp_int *a, mp_int *b); \end{alltt} Will square $a$ and store it in $b$. Like the case of multiplication there are four different squaring -algorithms all which can be called from mp\_sqr(). It is ideal to use mp\_sqr over mp\_mul when squaring terms because +algorithms all which can be called from the function \texttt{mp\_sqr}. It is ideal to use \texttt{mp\_sqr} over \texttt{mp\_mul} when squaring terms because of the speed difference. \section{Tuning Polynomial Basis Routines} Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that the Comba and baseline algorithms use. At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectively they require -considerably less work. For example, a 10000-digit multiplication would take roughly 724,000 single precision -multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor +considerably less work. For example, a $10\,000$-digit multiplication would take roughly $724\,000$ single precision +multiplications with Toom-Cook or $100\,000\,000$ single precision multiplications with the standard Comba (a factor of 138). So why not always use Karatsuba or Toom-Cook? The simple answer is that they have so much overhead that they're not @@ -1451,9 +1491,6 @@ actually faster than Comba until you hit distinct ``cutoff'' points. For Karat GCC 3.3.1 and an Athlon XP processor the cutoff point is roughly 110 digits (about 70 for the Intel P4). That is, at 110 digits Karatsuba and Comba multiplications just about break even and for 110+ digits Karatsuba is faster. -Toom-Cook has incredible overhead and is probably only useful for very large inputs. So far no known cutoff points -exist and for the most part I just set the cutoff points very high to make sure they're not called. - To get reasonable values for the cut-off points for your architecture, type \begin{alltt} @@ -1480,14 +1517,14 @@ Of particular interest to cryptography are reductions where $b$ is limited to th fast reduction algorithms can be written for the limited range. Note that one of the four optimized reduction algorithms are automatically chosen in the modular exponentiation -algorithm mp\_exptmod when an appropriate modulus is detected. +algorithm \texttt{mp\_exptmod} when an appropriate modulus is detected. \section{Straight Division} In order to effect an arbitrary modular reduction the following algorithm is provided. \index{mp\_mod} \begin{alltt} -int mp_mod(mp_int *a, mp_int *b, mp_int *c); +mp_err mp_mod(const mp_int *a,const mp_int *b, mp_int *c); \end{alltt} This reduces $a$ modulo $b$ and stores the result in $c$. The sign of $c$ shall agree with the sign @@ -1500,7 +1537,7 @@ a decent speedup over straight division. First a $\mu$ value must be precompute \index{mp\_reduce\_setup} \begin{alltt} -int mp_reduce_setup(mp_int *a, mp_int *b); +mp_err mp_reduce_setup(const mp_int *a, mp_int *b); \end{alltt} Given a modulus in $b$ this produces the required $\mu$ value in $a$. For any given modulus this only has to @@ -1508,7 +1545,7 @@ be computed once. Modular reduction can now be performed with the following. \index{mp\_reduce} \begin{alltt} -int mp_reduce(mp_int *a, mp_int *b, mp_int *c); +mp_err mp_reduce(const mp_int *a, const mp_int *b, mp_int *c); \end{alltt} This will reduce $a$ in place modulo $b$ with the precomputed $\mu$ value in $c$. $a$ must be in the range @@ -1518,7 +1555,7 @@ $0 \le a < b^2$. int main(void) \{ mp_int a, b, c, mu; - int result; + mp_err result; /* initialize a,b to desired values, mp_init mu, * c and set c to 1...we want to compute a^3 mod b @@ -1574,7 +1611,7 @@ step is required. This is accomplished with the following. \index{mp\_montgomery\_setup} \begin{alltt} -int mp_montgomery_setup(mp_int *a, mp_digit *mp); +mp_err mp_montgomery_setup(const mp_int *a, mp_digit *mp); \end{alltt} For the given odd moduli $a$ the precomputation value is placed in $mp$. The reduction is computed with the @@ -1582,23 +1619,23 @@ following. \index{mp\_montgomery\_reduce} \begin{alltt} -int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); +mp_err mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); \end{alltt} This reduces $a$ in place modulo $m$ with the pre--computed value $mp$. $a$ must be in the range $0 \le a < b^2$. -Montgomery reduction is faster than Barrett reduction for moduli smaller than the ``comba'' limit. With the default +Montgomery reduction is faster than Barrett reduction for moduli smaller than the ``Comba'' limit. With the default setup for instance, the limit is $127$ digits ($3556$--bits). Note that this function is not limited to $127$ digits just that it falls back to a baseline algorithm after that point. An important observation is that this reduction does not return $a \mbox{ mod }m$ but $aR^{-1} \mbox{ mod }m$ -where $R = \beta^n$, $n$ is the n number of digits in $m$ and $\beta$ is radix used (default is $2^{28}$). +where $R = \beta^n$, $n$ is the n number of digits in $m$ and $\beta$ is the radix used (default is $2^{28}$). To quickly calculate $R$ the following function was provided. \index{mp\_montgomery\_calc\_normalization} \begin{alltt} -int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); +mp_err mp_montgomery_calc_normalization(mp_int *a, mp_int *b); \end{alltt} Which calculates $a = R$ for the odd moduli $b$ without using multiplication or division. @@ -1611,7 +1648,7 @@ int main(void) \{ mp_int a, b, c, R; mp_digit mp; - int result; + mp_err result; /* initialize a,b to desired values, * mp_init R, c and set c to 1.... @@ -1683,7 +1720,7 @@ This particular example does not look too efficient but it demonstrates the poin normalizing the inputs the reduced results are always of the form $aR$ for some variable $a$. This allows a single final reduction to correct for the normalization and the fast reduction used within the algorithm. -For more details consider examining the file \textit{bn\_mp\_exptmod\_fast.c}. +For more details consider examining the file \texttt{bn\_mp\_exptmod\_fast.c}. \section{Restricted Diminished Radix} @@ -1695,16 +1732,23 @@ As in the case of Montgomery reduction there is a pre--computation phase require \index{mp\_dr\_setup} \begin{alltt} -void mp_dr_setup(mp_int *a, mp_digit *d); +void mp_dr_setup(const mp_int *a, mp_digit *d); \end{alltt} This computes the value required for the modulus $a$ and stores it in $d$. This function cannot fail -and does not return any error codes. After the pre--computation a reduction can be performed with the -following. +and does not return any error codes. + +To determine if $a$ is a valid DR modulus: +\index{mp\_dr\_is\_modulus} +\begin{alltt} +mp_bool mp_dr_is_modulus(const mp_int *a); +\end{alltt} + +After the pre--computation a reduction can be performed with the following. \index{mp\_dr\_reduce} \begin{alltt} -int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp); +mp_err mp_dr_reduce(mp_int *a, const mp_int *b, mp_digit mp); \end{alltt} This reduces $a$ in place modulo $b$ with the pre--computed value $mp$. $b$ must be of a restricted @@ -1724,20 +1768,30 @@ Unrestricted reductions work much like the restricted counterparts except in thi form $2^k - p$ for $0 < p < \beta$. In this sense the unrestricted reductions are more flexible as they can be applied to a wider range of numbers. -\index{mp\_reduce\_2k\_setup} +\index{mp\_reduce\_2k\_setup}\index{mp\_reduce\_2k\_setup\_l} \begin{alltt} -int mp_reduce_2k_setup(mp_int *a, mp_digit *d); +mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d); +mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d); \end{alltt} This will compute the required $d$ value for the given moduli $a$. -\index{mp\_reduce\_2k} +\index{mp\_reduce\_2k}\index{mp\_reduce\_2k\_l} \begin{alltt} -int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); +mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d); +mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d); \end{alltt} This will reduce $a$ in place modulo $n$ with the pre--computed value $d$. From my experience this routine is -slower than mp\_dr\_reduce but faster for most moduli sizes than the Montgomery reduction. +slower than the function \texttt{mp\_dr\_reduce} but faster for most moduli sizes than the Montgomery reduction. + +To determine if \texttt{mp\_reduce\_2k} can be used at all, ask the function \texttt{mp\_reduce\_is\_2k}. + +\index{mp\_reduce\_is\_2k}\index{mp\_reduce\_is\_2k\_l} +\begin{alltt} +mp_bool mp_reduce_is_2k(const mp_int *a); +mp_bool mp_reduce_is_2k_l(const mp_int *a); +\end{alltt} \section{Combined Modular Reduction} @@ -1746,38 +1800,38 @@ Some of the combinations of an arithmetic operations followed by a modular reduc Addition $d = (a + b) \mod c$ \index{mp\_addmod} \begin{alltt} -int mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); +mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); \end{alltt} Subtraction $d = (a - b) \mod c$ \begin{alltt} -int mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); +mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); \end{alltt} Multiplication $d = (ab) \mod c$ \begin{alltt} -int mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); +mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); \end{alltt} Squaring $d = (a^2) \mod c$ \begin{alltt} -int mp_sqrmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); +mp_err mp_sqrmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d); \end{alltt} \chapter{Exponentiation} \section{Single Digit Exponentiation} -\index{mp\_expt\_d} +\index{mp\_expt\_u32} \begin{alltt} -int mp_expt_d (mp_int * a, mp_digit b, mp_int * c) +mp_err mp_expt_u32 (const mp_int *a, uint32_t b, mp_int *c) \end{alltt} This function computes $c = a^b$. \section{Modular Exponentiation} \index{mp\_exptmod} \begin{alltt} -int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) +mp_err mp_exptmod (const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) \end{alltt} This computes $Y \equiv G^X \mbox{ (mod }P\mbox{)}$ using a variable width sliding window algorithm. This function will automatically detect the fastest modular reduction technique to use during the operation. For negative values of @@ -1792,14 +1846,14 @@ and the other two algorithms. \section{Modulus a Power of Two} \index{mp\_mod\_2d} \begin{alltt} -int mp_mod_2d(const mp_int *a, int b, mp_int *c) +mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) \end{alltt} It calculates $c = a \mod 2^b$. \section{Root Finding} \index{mp\_n\_root} \begin{alltt} -int mp_n_root (mp_int * a, mp_digit b, mp_int * c) +mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) \end{alltt} This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. Will return a positive root only for even roots and return a root with the sign of the input for odd roots. For example, performing $4^{1/2}$ will return $2$ whereas $(-8)^{1/3}$ @@ -1811,7 +1865,7 @@ The square root $c = a^{1/2}$ (with the same conditions $c^2 \le a$ and $(c+1)^ \index{mp\_sqrt} \begin{alltt} -int mp_sqrt (mp_int * a, mp_digit b, mp_int * c) +mp_err mp_sqrt(const mp_int *arg, mp_int *ret) \end{alltt} @@ -1820,7 +1874,7 @@ int mp_sqrt (mp_int * a, mp_digit b, mp_int * c) A logarithm function for positive integer input \texttt{a, base} computing $\floor{\log_bx}$ such that $(\log_b x)^b \le x$. \index{mp\_ilogb} \begin{alltt} -int mp_ilogb(mp_int *a, mp_digit base, mp_int *c) +mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) \end{alltt} \subsection{Example} \begin{alltt} @@ -1833,8 +1887,8 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c) int main(int argc, char **argv) { mp_int x, output; - mp_digit base; - int e; + uint32_t base; + mp_err e; if (argc != 3) { fprintf(stderr,"Usage %s base x\textbackslash{}n", argv[0]); @@ -1847,11 +1901,12 @@ int main(int argc, char **argv) } errno = 0; #ifdef MP_64BIT - base = (mp_digit)strtoull(argv[1], NULL, 10); + /* Check for overflow skipped */ + base = (uint32_t)strtoull(argv[1], NULL, 10); #else - base = (mp_digit)strtoul(argv[1], NULL, 10); + base = (uint32_t)strtoul(argv[1], NULL, 10); #endif - if ((errno == ERANGE) || (base > (base & MP_MASK))) { + if (errno == ERANGE) { fprintf(stderr,"strtoul(l) failed: input out of range\textbackslash{}n"); exit(EXIT_FAILURE); } @@ -1860,7 +1915,7 @@ int main(int argc, char **argv) mp_error_to_string(e)); exit(EXIT_FAILURE); } - if ((e = mp_ilogb(&x, base, &output)) != MP_OKAY) { + if ((e = mp_log_u32(&x, base, &output)) != MP_OKAY) { fprintf(stderr,"mp_ilogb failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n", mp_error_to_string(e)); exit(EXIT_FAILURE); @@ -1878,23 +1933,12 @@ int main(int argc, char **argv) } \end{alltt} - - \chapter{Prime Numbers} -\section{Trial Division} -\index{mp\_prime\_is\_divisible} -\begin{alltt} -int mp_prime_is_divisible (mp_int * a, int *result) -\end{alltt} -This will attempt to evenly divide $a$ by a list of primes\footnote{Default is the first 256 primes.} and store the -outcome in ``result''. That is if $result = 0$ then $a$ is not divisible by the primes, otherwise it is. Note that -if the function does not return \textbf{MP\_OKAY} the value in ``result'' should be considered undefined\footnote{Currently -the default is to set it to zero first.}. \section{Fermat Test} \index{mp\_prime\_fermat} \begin{alltt} -int mp_prime_fermat (mp_int * a, mp_int * b, int *result) +mp_err mp_prime_fermat (const mp_int *a, const mp_int *b, int *result) \end{alltt} Performs a Fermat primality test to the base $b$. That is it computes $b^a \mbox{ mod }a$ and tests whether the value is equal to $b$ or not. If the values are equal then $a$ is probably prime and $result$ is set to one. Otherwise $result$ @@ -1903,13 +1947,13 @@ is set to zero. \section{Miller-Rabin Test} \index{mp\_prime\_miller\_rabin} \begin{alltt} -int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) +mp_err mp_prime_miller_rabin (const mp_int *a, const mp_int *b, int *result) \end{alltt} Performs a Miller-Rabin test to the base $b$ of $a$. This test is much stronger than the Fermat test and is very hard to fool (besides with Carmichael numbers). If $a$ passes the test (therefore is probably prime) $result$ is set to one. Otherwise $result$ is set to zero. -Note that is suggested that you use the Miller-Rabin test instead of the Fermat test since all of the failures of +Note that it is suggested that you use the Miller-Rabin test instead of the Fermat test since all of the failures of Miller-Rabin are a subset of the failures of the Fermat test. \subsection{Required Number of Tests} @@ -1919,9 +1963,9 @@ This is why a simple function has been provided to help out. \index{mp\_prime\_rabin\_miller\_trials} \begin{alltt} -int mp_prime_rabin_miller_trials(int size) +mp_err mp_prime_rabin_miller_trials(int size) \end{alltt} -This returns the number of trials required for a low probability of failure for a given ``size'' expressed in bits. This comes in handy specially since larger numbers are slower to test. For example, a 512-bit number would require 18 tests for a probability of $2^{-160}$ whereas a 1024-bit number would only require 12 tests for a probability of $2^{-192}$. The exact values as implemented are listed in table \ref{table:millerrabinrunsimpl}. +This returns the number of trials required for a low probability of failure for a given \texttt{size} expressed in bits. This comes in handy specially since larger numbers are slower to test. For example, a 512-bit number would require 18 tests for a probability of $2^{-160}$ whereas a 1024-bit number would only require 12 tests for a probability of $2^{-192}$. The exact values as implemented are listed in table \ref{table:millerrabinrunsimpl}. \begin{table}[h] \begin{center} @@ -1952,9 +1996,8 @@ This returns the number of trials required for a low probability of failure for \end{center} \end{table} -You should always still perform a trial division before a Miller-Rabin test though. +A small table, broke in two for typographical reasons, with the number of rounds of Miller-Rabin tests is shown below. The numbers have been computed with a PARI/GP script listed in appendix \ref{app:numberofmrcomp}. -A small table, broke in two for typographical reasons, with the number of rounds of Miller-Rabin tests is shown below. The numbers have been compute with a PARI/GP script listed in appendix \ref{app:numberofmrcomp}. The first column is the number of bits $b$ in the prime $p = 2^b$, the numbers in the first row represent the probability that the number that all of the Miller-Rabin tests deemed a pseudoprime is actually a composite. There is a deterministic test for numbers smaller than $2^{80}$. @@ -2029,7 +2072,7 @@ See also table C.1 in FIPS 186-4. \section{Strong Lucas-Selfridge Test} \index{mp\_prime\_strong\_lucas\_selfridge} \begin{alltt} -int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result) +mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) \end{alltt} Performs a strong Lucas-Selfridge test. The strong Lucas-Selfridge test together with the Rabin-Miler test with bases $2$ and $3$ resemble the BPSW test. The single internal use is a compile-time option in \texttt{mp\_prime\_is\_prime} and can be excluded from the Libtommath build if not needed. @@ -2037,7 +2080,7 @@ from the Libtommath build if not needed. \section{Frobenius (Underwood) Test} \index{mp\_prime\_frobenius\_underwood} \begin{alltt} -int mp_prime_frobenius_underwood(const mp_int *N, int *result) +mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) \end{alltt} Performs the variant of the Frobenius test as described by Paul Underwood. It can be included at build-time if the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST} is defined and will be used instead of the Lucas-Selfridge test. @@ -2047,40 +2090,39 @@ It returns \texttt{MP\_ITER} if the number of iterations is exhausted, assumes a Testing if a number is a square can be done a bit faster than just by calculating the square root. It is used by the primality testing function described below. \index{mp\_is\_square} \begin{alltt} -int mp_is_square(const mp_int *arg, int *ret); +mp_err mp_is_square(const mp_int *arg, mp_bool *ret); \end{alltt} \index{mp\_prime\_is\_prime} \begin{alltt} -int mp_prime_is_prime (mp_int * a, int t, int *result) +mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) \end{alltt} This will perform a trial division followed by two rounds of Miller-Rabin with bases 2 and 3 and a Lucas-Selfridge test. The Frobenius-Underwood is available as a compile-time option with the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST}. See file \texttt{bn\_mp\_prime\_is\_prime.c} for the necessary details. It shall be noted that both functions are much slower than -the Miller-Rabin test and if speed is an essential issue, the macro \texttt{LTM\_USE\_ONLY\_MR} switches both functions, the Frobenius-Underwood test and the Lucas-Selfridge test off and their code will not even be compiled into the library. +the Miller-Rabin test and if speed is an essential issue, the macro \texttt{LTM\_USE\_ONLY\_MR} switches the Frobenius-Underwood test and the Lucas-Selfridge test off and their code will not even be compiled into the library. -If $t$ is set to a positive value $t$ additional rounds of the Miller-Rabin test with random bases will be performed to allow for Fips 186.4 (vid.~p.~126ff) compliance. The function \texttt{mp\_prime\_rabin\_miller\_trials} can be used to determine the number of rounds. It is vital that the function \texttt{mp\_rand()} has a cryptographically strong random number generator available. +If $t$ is set to a positive value $t$ additional rounds of the Miller-Rabin test with random bases will be performed to allow for Fips 186.4 (vid.~p.~126ff) compliance. The function \texttt{mp\_prime\_rabin\_miller\_trials} can be used to determine the number of rounds. It is vital that the function \texttt{mp\_rand} has a cryptographically strong random number generator available. One Miller-Rabin tests with a random base will be run automatically, so by setting $t$ to a positive value this function will run $t + 1$ Miller-Rabin tests with random bases. -If $t$ is set to a negative value the test will run the deterministic Miller-Rabin test for the primes up to $3317044064679887385961981$. That limit has to be checked by the caller. +If $t$ is set to a negative value the test will run the deterministic Miller-Rabin test for the primes up to $3\,317\,044\,064\,679\,887\,385\,961\,981$\footnote{The semiprime $1287836182261\cdot 2575672364521$ with both factors smaller than $2^64$. An alternative with all factors smaller than $2^32$ is $4290067842\cdot 262853\cdot 1206721\cdot 2134439 + 3$}. That limit has to be checked by the caller. -If $a$ passes all of the tests $result$ is set to one, otherwise it is set to zero. +If $a$ passes all of the tests $result$ is set to \texttt{MP\_YES}, otherwise it is set to \texttt{MP\_NO}. \section{Next Prime} \index{mp\_prime\_next\_prime} \begin{alltt} -int mp_prime_next_prime(mp_int *a, int t, int bbs_style) +mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style) \end{alltt} -This finds the next prime after $a$ that passes mp\_prime\_is\_prime() with $t$ tests but see the documentation for -mp\_prime\_is\_prime for details regarding the use of the argument $t$. Set $bbs\_style$ to one if you -want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to zero to find any next prime. +This finds the next prime after $a$ that passes the function \texttt{mp\_prime\_is\_prime} with $t$ tests but see the documentation for +\texttt{mp\_prime\_is\_prime} for details regarding the use of the argument $t$. Set $bbs\_style$ to \texttt{MP\_YES} if you +want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to \texttt{MP\_NO} to find any next prime. \section{Random Primes} \index{mp\_prime\_rand} \begin{alltt} -int mp_prime_rand(mp_int *a, int t, - int size, int flags); +mp_err mp_prime_rand(mp_int *a, int t, int size, int flags); \end{alltt} This will generate a prime in $a$ using $t$ tests of the primality testing algorithms. See the documentation for mp\_prime\_is\_prime for details regarding the use of the argument $t$. @@ -2115,88 +2157,95 @@ is no skew on the least significant bits. \section{PRNG} \index{mp\_rand\_digit} \begin{alltt} -int mp_rand_digit(mp_digit *r) +mp_err mp_rand_digit(mp_digit *r) \end{alltt} -This function generates a random number in \texttt{r} of the size given in \texttt{r} (that is, the variable is used for in- and output) but not more than \texttt{MP\_MASK} bits. +This function generates a random number in \texttt{r} of the size given in \texttt{r} (that is, the variable is used for in- and output) but not more than \texttt{MP\_DIGIT\_MAX} bits. \index{mp\_rand} \begin{alltt} -int mp_rand(mp_int *a, int digits) +mp_err mp_rand(mp_int *a, int digits) \end{alltt} This function generates a random number of \texttt{digits} bits. The random number generated with these two functions is cryptographically secure if the source of random numbers the operating systems offers is cryptographically secure. It will use \texttt{arc4random()} if the OS is a BSD flavor, Wincrypt on Windows, or \texttt{/dev/urandom} on all operating systems that have it. +If you have a custom random source you might find the function \texttt(mp\_rand\_source) useful. +\index{mp\_rand\_source} +\begin{alltt} +void mp_rand_source(mp_err(*source)(void *out, size_t size)); +\end{alltt} + \chapter{Input and Output} \section{ASCII Conversions} \subsection{To ASCII} \index{mp\_to\_radix} \begin{alltt} -int mp_to_radix (mp_int *a, char *str, size_t maxlen, size_t *written, int radix); +mp_err mp_to_radix (const mp_int *a, char *str, size_t maxlen, size_t *written, int radix); \end{alltt} This stores $a$ in \texttt{str} of maximum length \texttt{maxlen} as a base-\texttt{radix} string of ASCII chars and appends a \texttt{NUL} character to terminate the string. -Valid values of \texttt{radix} line in the range $[2, 64]$. +Valid values of \texttt{radix} are in the range $[2, 64]$. The exact number of characters in \texttt{str} plus the \texttt{NUL} will be put in \texttt{written} if that variable is not set to \texttt{NULL}. If \texttt{str} is not big enough to hold $a$, \texttt{str} will be filled with the least-significant digits -of length \texttt{maxlen-1}, then \texttt{str} will be \texttt{NUL} terminated and the error \texttt{MP\_VAL} is returned. +of length \texttt{maxlen-1}, then \texttt{str} will be \texttt{NUL} terminated and the error \texttt{MP\_BUF} is returned. Please be aware that this function cannot evaluate the actual size of the buffer, it relies on the correctness of \texttt{maxlen}! \index{mp\_radix\_size} \begin{alltt} -int mp_radix_size (mp_int * a, int radix, int *size) +mp_err mp_radix_size (const mp_int *a, int radix, int *size) \end{alltt} -This stores in ``size'' the number of characters (including space for the NUL terminator) required. Upon error this -function returns an error code and ``size'' will be zero. +This stores in \texttt{size} the number of characters (including space for the NUL terminator) required. Upon error this +function returns an error code and \texttt{size} will be zero. If \texttt{MP\_NO\_FILE} is not defined a function to write to a file is also available. \index{mp\_fwrite} \begin{alltt} -int mp_fwrite(const mp_int *a, int radix, FILE *stream); +mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream); \end{alltt} \subsection{From ASCII} \index{mp\_read\_radix} \begin{alltt} -int mp_read_radix (mp_int * a, char *str, int radix); +mp_err mp_read_radix (mp_int *a, const char *str, int radix); \end{alltt} -This will read the base-``radix'' NUL terminated string from ``str'' into $a$. It will stop reading when it reads a -character it does not recognize (which happens to include th NUL char... imagine that...). A single leading $-$ sign +This will read a \texttt{NUL} terminated string in base \texttt{radix} from \texttt{str} into $a$. It will stop reading when it reads a +character it does not recognize (which happens to include the \texttt{NUL} char... imagine that...). A single leading $-$ sign can be used to denote a negative number. +The input encoding is currently restricted to ASCII only. If \texttt{MP\_NO\_FILE} is not defined a function to read from a file is also available. \index{mp\_fread} \begin{alltt} -int mp_fread(mp_int *a, int radix, FILE *stream); +mp_err mp_fread(mp_int *a, int radix, FILE *stream); \end{alltt} \section{Binary Conversions} -Converting an mp\_int to and from binary is another keen idea. +Converting an \texttt{mp\_int} to and from binary is another keen idea. \index{mp\_ubin\_size} \begin{alltt} -size_t mp_ubin_size(mp_int *a); +size_t mp_ubin_size(const mp_int *a); \end{alltt} This will return the number of bytes (octets) required to store the unsigned copy of the integer $a$. \index{mp\_to\_ubin} \begin{alltt} -int mp_to_unsigned_bin(mp_int *a, unsigned char *b, size_t maxlen, size_t *len); +mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) \end{alltt} This will store $a$ into the buffer $b$ of size \texttt{maxlen} in big--endian format storing the number of bytes written in \texttt{len}. Fortunately this is exactly what DER (or is it ASN?) requires. It does not store the sign of the integer. \index{mp\_from\_ubin} \begin{alltt} -int mp_from_ubin(mp_int *a, unsigned char *b, size_t size); +mp_err mp_from_ubin(mp_int *a, unsigned char *b, size_t size); \end{alltt} This will read in an unsigned big--endian array of bytes (octets) from $b$ of length \texttt{size} into $a$. The resulting big-integer $a$ will always be positive. @@ -2204,26 +2253,26 @@ For those who acknowledge the existence of negative numbers (heretic!) there are previous functions. \index{mp\_signed\_bin\_size} \index{mp\_to\_signed\_bin} \index{mp\_read\_signed\_bin} \begin{alltt} -int mp_sbin_size(mp_int *a); -int mp_from_sbin(mp_int *a, unsigned char *b, size_t size); -int mp_to_sbin(mp_int *a, unsigned char *b, size_t maxsize, size_t *len); +size_t mp_sbin_size(const mp_int *a); +mp_err mp_from_sbin(mp_int *a, const unsigned char *b, size_t size); +mp_err mp_to_sbin(const mp_int *a, unsigned char *b, size_t maxsize, size_t *len); \end{alltt} They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero -byte depending on the sign. If the sign is zpos (e.g. not negative) the prefix is zero, otherwise the prefix +byte depending on the sign. If the sign is \texttt{MP\_ZPOS} (e.g. not negative) the prefix is zero, otherwise the prefix is non--zero. The two functions \texttt{mp\_unpack} (get your gifts out of the box, import binary data) and \texttt{mp\_pack} (put your gifts into the box, export binary data) implement the similarly working GMP functions as described at \url{http://gmplib.org/manual/Integer-Import-and-Export.html} with the exception that \texttt{mp\_pack} will not allocate memory if \texttt{rop} is \texttt{NULL}. \index{mp\_unpack} \index{mp\_pack} \begin{alltt} -int mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, +mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian, size_t nails, const void *op, size_t maxsize); -int mp_pack(void *rop, size_t *countp, mp_order order, size_t size, +mp_err mp_pack(void *rop, size_t *countp, mp_order order, size_t size, mp_endian endian, size_t nails, const mp_int *op); \end{alltt} The function \texttt{mp\_pack} has the additional variable \texttt{maxsize} which must hold the size of the buffer \texttt{rop} in bytes. Use \begin{alltt} /* Parameters "nails" and "size" are the same as in mp_pack */ -size_t mp_pack_size(mp_int *a, size_t nails, size_t size); +size_t mp_pack_count(const mp_int *a, size_t nails, size_t size); \end{alltt} To get the size in bytes necessary to be put in \texttt{maxsize}). @@ -2244,58 +2293,52 @@ typedef enum { \section{Extended Euclidean Algorithm} \index{mp\_exteuclid} \begin{alltt} -int mp_exteuclid(mp_int *a, mp_int *b, +mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); \end{alltt} -This finds the triple U1/U2/U3 using the Extended Euclidean algorithm such that the following equation holds. +This finds the triple $U_1$/$U_2$/$U_3$ using the Extended Euclidean algorithm such that the following equation holds. \begin{equation} -a \cdot U1 + b \cdot U2 = U3 +a \cdot U_1 + b \cdot U_2 = U_3 \end{equation} -Any of the U1/U2/U3 parameters can be set to \textbf{NULL} if they are not desired. +Any of the \texttt{U1}/\texttt{U2}/\texttt{U3} parameters can be set to \textbf{NULL} if they are not desired. \section{Greatest Common Divisor} \index{mp\_gcd} \begin{alltt} -int mp_gcd (mp_int * a, mp_int * b, mp_int * c) +mp_err mp_gcd (const mp_int *a, const mp_int *b, mp_int *c) \end{alltt} This will compute the greatest common divisor of $a$ and $b$ and store it in $c$. \section{Least Common Multiple} \index{mp\_lcm} \begin{alltt} -int mp_lcm (mp_int * a, mp_int * b, mp_int * c) +mp_err mp_lcm (const mp_int *a, const mp_int *b, mp_int *c) \end{alltt} This will compute the least common multiple of $a$ and $b$ and store it in $c$. -\section{Jacobi Symbol} -\index{mp\_jacobi} -\begin{alltt} -int mp_jacobi (mp_int * a, mp_int * p, int *c) -\end{alltt} -This will compute the Jacobi symbol for $a$ with respect to $p$. If $p$ is prime this essentially computes the Legendre -symbol. The result is stored in $c$ and can take on one of three values $\lbrace -1, 0, 1 \rbrace$. If $p$ is prime -then the result will be $-1$ when $a$ is not a quadratic residue modulo $p$. The result will be $0$ if $a$ divides $p$ -and the result will be $1$ if $a$ is a quadratic residue modulo $p$. \section{Kronecker Symbol} \index{mp\_kronecker} \begin{alltt} -int mp_kronecker (mp_int * a, mp_int * p, int *c) +mp_err mp_kronecker (const mp_int *a, const mp_int *p, int *c) \end{alltt} -Extension of the Jacoby symbol to all $\lbrace a, p \rbrace \in \mathbb{Z}$ . +This will compute the Kronecker symbol (an extension of the Jacobi symbol) for $a$ with respect to $p$ with $\lbrace a, p \rbrace \in \mathbb{Z}$. If $p$ is prime this essentially computes the Legendre +symbol. The result is stored in $c$ and can take on one of three values $\lbrace -1, 0, 1 \rbrace$. If $p$ is prime +then the result will be $-1$ when $a$ is not a quadratic residue modulo $p$. The result will be $0$ if $a$ divides $p$ +and the result will be $1$ if $a$ is a quadratic residue modulo $p$. \section{Modular square root} \index{mp\_sqrtmod\_prime} \begin{alltt} -int mp_sqrtmod_prime(mp_int *n, mp_int *p, mp_int *r) +mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *p, mp_int *r) \end{alltt} -This will solve the modular equatioon $r^2 = n \mod p$ where $p$ is a prime number greater than 2 (odd prime). -The result is returned in the third argument $r$, the function returns \textbf{MP\_OKAY} on success, +This will solve the modular equation $r^2 = n \mod p$ where $p$ is a prime number greater than 2 (odd prime). +The result is returned in the third argument $r$, the function returns \texttt{MP\_OKAY} on success, other return values indicate failure. The implementation is split for two different cases: @@ -2307,12 +2350,12 @@ $r = n^{(p+1)/4} \mod p$ The function does not check the primality of parameter $p$ thus it is up to the caller to assure that this parameter is a prime number. When $p$ is a composite the function behaviour is undefined, it may even return a false-positive -\textbf{MP\_OKAY}. +\texttt{MP\_OKAY}. \section{Modular Inverse} \index{mp\_invmod} \begin{alltt} -int mp_invmod (mp_int * a, mp_int * b, mp_int * c) +mp_err mp_invmod (const mp_int *a, const mp_int *b, mp_int *c) \end{alltt} Computes the multiplicative inverse of $a$ modulo $b$ and stores the result in $c$ such that $ac \equiv 1 \mbox{ (mod }b\mbox{)}$. @@ -2322,21 +2365,21 @@ For those using small numbers (\textit{snicker snicker}) there are several ``hel \index{mp\_add\_d} \index{mp\_sub\_d} \index{mp\_mul\_d} \index{mp\_div\_d} \index{mp\_mod\_d} \begin{alltt} -int mp_add_d(mp_int *a, mp_digit b, mp_int *c); -int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); -int mp_mul_d(mp_int *a, mp_digit b, mp_int *c); -int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d); -int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c); +mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c); +mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c); +mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c); +mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d); +mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c); \end{alltt} -These work like the full mp\_int capable variants except the second parameter $b$ is a mp\_digit. These +These work like the full \texttt{mp\_int} capable variants except the second parameter $b$ is a \texttt{mp\_digit}. These functions fairly handy if you have to work with relatively small numbers since you will not have to allocate -an entire mp\_int to store a number like $1$ or $2$. +an entire \texttt{mp\_int} to store a number like $1$ or $2$. The functions \texttt{mp\_incr} and \texttt{mp\_decr} mimic the postfix operators \texttt{++} and \texttt{--} respectively, to increment the input by one. They call the full single-digit functions if the addition would carry. Both functions need to be included in a minimized library because they call each other in case of a negative input, These functions change the inputs! \begin{alltt} -int mp_incr(mp_int *a); -int mp_decr(mp_int *a); +mp_err mp_incr(mp_int *a); +mp_err mp_decr(mp_int *a); \end{alltt} @@ -2344,7 +2387,7 @@ The division by three can be made faster by replacing the division with a multip \index{mp\_div\_3} \begin{alltt} -int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d); +mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d); \end{alltt} \chapter{Little Helpers} @@ -2354,26 +2397,26 @@ To make this overview simpler the macros are given as function prototypes. The r \index{mp\_iseven} \begin{alltt} -int mp_iseven(mp_int *a) +mp_bool mp_iseven(const mp_int *a) \end{alltt} Checks if $a = 0 mod 2$ \index{mp\_isodd} \begin{alltt} -int mp_isodd(mp_int *a) +mp_bool mp_isodd(const mp_int *a) \end{alltt} Checks if $a = 1 mod 2$ \index{mp\_isneg} \begin{alltt} -int mp_isneg(mp_int *a) +mp_bool mp_isneg(mp_int *a) \end{alltt} Checks if $a < 0$ \index{mp\_iszero} \begin{alltt} -int mp_iszero(mp_int *a) +mp_bool mp_iszero(mp_int *a) \end{alltt} Checks if $a = 0$. It does not check if the amount of memory allocated for $a$ is also minimal.