comparison libtommath/bn.tex @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
47 \def\gap{\vspace{0.5ex}} 47 \def\gap{\vspace{0.5ex}}
48 \makeindex 48 \makeindex
49 \begin{document} 49 \begin{document}
50 \frontmatter 50 \frontmatter
51 \pagestyle{empty} 51 \pagestyle{empty}
52 \title{LibTomMath User Manual \\ v0.40} 52 \title{LibTomMath User Manual \\ v1.0}
53 \author{Tom St Denis \\ [email protected]} 53 \author{Tom St Denis \\ [email protected]}
54 \maketitle 54 \maketitle
55 This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been 55 This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
56 formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package. 56 formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.
57 57
58 \vspace{10cm} 58 \vspace{10cm}
59 59
60 \begin{flushright}Open Source. Open Academia. Open Minds. 60 \begin{flushright}Open Source. Open Academia. Open Minds.
72 \pagestyle{headings} 72 \pagestyle{headings}
73 \chapter{Introduction} 73 \chapter{Introduction}
74 \section{What is LibTomMath?} 74 \section{What is LibTomMath?}
75 LibTomMath is a library of source code which provides a series of efficient and carefully written functions for manipulating 75 LibTomMath is a library of source code which provides a series of efficient and carefully written functions for manipulating
76 large integer numbers. It was written in portable ISO C source code so that it will build on any platform with a conforming 76 large integer numbers. It was written in portable ISO C source code so that it will build on any platform with a conforming
77 C compiler. 77 C compiler.
78 78
79 In a nutshell the library was written from scratch with verbose comments to help instruct computer science students how 79 In a nutshell the library was written from scratch with verbose comments to help instruct computer science students how
80 to implement ``bignum'' math. However, the resulting code has proven to be very useful. It has been used by numerous 80 to implement ``bignum'' math. However, the resulting code has proven to be very useful. It has been used by numerous
81 universities, commercial and open source software developers. It has been used on a variety of platforms ranging from 81 universities, commercial and open source software developers. It has been used on a variety of platforms ranging from
82 Linux and Windows based x86 to ARM based Gameboys and PPC based MacOS machines. 82 Linux and Windows based x86 to ARM based Gameboys and PPC based MacOS machines.
83 83
84 \section{License} 84 \section{License}
85 As of the v0.25 the library source code has been placed in the public domain with every new release. As of the v0.28 85 As 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 release the textbook ``Implementing Multiple Precision Arithmetic'' has been placed in the public domain with every new 86 release the textbook ``Implementing Multiple Precision Arithmetic'' has been placed in the public domain with every new
87 release as well. This textbook is meant to compliment the project by providing a more solid walkthrough of the development 87 release as well. This textbook is meant to compliment the project by providing a more solid walkthrough of the development
88 algorithms used in the library. 88 algorithms used in the library.
89 89
90 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 90 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
91 public domain everyone is entitled to do with them as they see fit. 91 public domain everyone is entitled to do with them as they see fit.
92 92
93 \section{Building LibTomMath} 93 \section{Building LibTomMath}
94 94
95 LibTomMath is meant to be very ``GCC friendly'' as it comes with a makefile well suited for GCC. However, the library will 95 LibTomMath is meant to be very ``GCC friendly'' as it comes with a makefile well suited for GCC. However, the library will
96 also 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 96 also 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 developer. 97 developer.
98 98
99 \subsection{Static Libraries} 99 \subsection{Static Libraries}
100 To build as a static library for GCC issue the following 100 To build as a static library for GCC issue the following
101 \begin{alltt} 101 \begin{alltt}
102 make 102 make
103 \end{alltt} 103 \end{alltt}
104 104
105 command. This will build the library and archive the object files in ``libtommath.a''. Now you link against 105 command. This will build the library and archive the object files in ``libtommath.a''. Now you link against
106 that and include ``tommath.h'' within your programs. Alternatively to build with MSVC issue the following 106 that and include ``tommath.h'' within your programs. Alternatively to build with MSVC issue the following
107 \begin{alltt} 107 \begin{alltt}
108 nmake -f makefile.msvc 108 nmake -f makefile.msvc
109 \end{alltt} 109 \end{alltt}
110 110
111 This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC 111 This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC
112 version 6.00 with service pack 5. 112 version 6.00 with service pack 5.
113 113
114 \subsection{Shared Libraries} 114 \subsection{Shared Libraries}
115 To build as a shared library for GCC issue the following 115 To build as a shared library for GCC issue the following
116 \begin{alltt} 116 \begin{alltt}
117 make -f makefile.shared 117 make -f makefile.shared
118 \end{alltt} 118 \end{alltt}
119 This requires the ``libtool'' package (common on most Linux/BSD systems). It will build LibTomMath as both shared 119 This requires the ``libtool'' package (common on most Linux/BSD systems). It will build LibTomMath as both shared
120 and static then install (by default) into /usr/lib as well as install the header files in /usr/include. The shared 120 and static then install (by default) into /usr/lib as well as install the header files in /usr/include. The shared
121 library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''. Generally 121 library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''. Generally
122 you use libtool to link your application against the shared object. 122 you use libtool to link your application against the shared object.
123 123
124 There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile. It requires 124 There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile. It requires
125 Cygwin to work with since it requires the auto-export/import functionality. The resulting DLL and import library 125 Cygwin to work with since it requires the auto-export/import functionality. The resulting DLL and import library
126 ``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin. 126 ``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin.
127 127
128 \subsection{Testing} 128 \subsection{Testing}
129 To build the library and the test harness type 129 To build the library and the test harness type
130 130
138 138
139 \begin{alltt} 139 \begin{alltt}
140 mtest/mtest | test 140 mtest/mtest | test
141 \end{alltt} 141 \end{alltt}
142 142
143 If you do not have a ``/dev/urandom'' style RNG source you will have to write your own PRNG and simply pipe that into 143 If you do not have a ``/dev/urandom'' style RNG source you will have to write your own PRNG and simply pipe that into
144 mtest. For example, if your PRNG program is called ``myprng'' simply invoke 144 mtest. For example, if your PRNG program is called ``myprng'' simply invoke
145 145
146 \begin{alltt} 146 \begin{alltt}
147 myprng | mtest/mtest | test 147 myprng | mtest/mtest | test
148 \end{alltt} 148 \end{alltt}
150 This will output a row of numbers that are increasing. Each column is a different test (such as addition, multiplication, etc) 150 This will output a row of numbers that are increasing. Each column is a different test (such as addition, multiplication, etc)
151 that is being performed. The numbers represent how many times the test was invoked. If an error is detected the program 151 that is being performed. The numbers represent how many times the test was invoked. If an error is detected the program
152 will exit with a dump of the relevent numbers it was working with. 152 will exit with a dump of the relevent numbers it was working with.
153 153
154 \section{Build Configuration} 154 \section{Build Configuration}
155 LibTomMath can configured at build time in three phases we shall call ``depends'', ``tweaks'' and ``trims''. 155 LibTomMath can configured at build time in three phases we shall call ``depends'', ``tweaks'' and ``trims''.
156 Each phase changes how the library is built and they are applied one after another respectively. 156 Each phase changes how the library is built and they are applied one after another respectively.
157 157
158 To make the system more powerful you can tweak the build process. Classes are defined in the file 158 To make the system more powerful you can tweak the build process. Classes are defined in the file
159 ``tommath\_superclass.h''. By default, the symbol ``LTM\_ALL'' shall be defined which simply 159 ``tommath\_superclass.h''. By default, the symbol ``LTM\_ALL'' shall be defined which simply
160 instructs the system to build all of the functions. This is how LibTomMath used to be packaged. This will give you 160 instructs the system to build all of the functions. This is how LibTomMath used to be packaged. This will give you
161 access to every function LibTomMath offers. 161 access to every function LibTomMath offers.
162 162
163 However, there are cases where such a build is not optional. For instance, you want to perform RSA operations. You 163 However, there are cases where such a build is not optional. For instance, you want to perform RSA operations. You
164 don't need the vast majority of the library to perform these operations. Aside from LTM\_ALL there is 164 don't need the vast majority of the library to perform these operations. Aside from LTM\_ALL there is
165 another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt. Additional 165 another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt. Additional
166 classes can be defined base on the need of the user. 166 classes can be defined base on the need of the user.
167 167
168 \subsection{Build Depends} 168 \subsection{Build Depends}
169 In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs'' 169 In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs''
170 which further define symbols. All of the symbols (technically they're macros $\ldots$) represent a given C source 170 which further define symbols. All of the symbols (technically they're macros $\ldots$) represent a given C source
171 file. For instance, BN\_MP\_ADD\_C represents the file ``bn\_mp\_add.c''. When a define has been enabled the 171 file. For instance, BN\_MP\_ADD\_C represents the file ``bn\_mp\_add.c''. When a define has been enabled the
172 function in the respective file will be compiled and linked into the library. Accordingly when the define 172 function in the respective file will be compiled and linked into the library. Accordingly when the define
173 is absent the file will not be compiled and not contribute any size to the library. 173 is absent the file will not be compiled and not contribute any size to the library.
174 174
175 You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice). 175 You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice).
176 This is to help resolve as many dependencies as possible. In the last pass the symbol LTM\_LAST will be defined. 176 This is to help resolve as many dependencies as possible. In the last pass the symbol LTM\_LAST will be defined.
177 This is useful for ``trims''. 177 This is useful for ``trims''.
178 178
179 \subsection{Build Tweaks} 179 \subsection{Build Tweaks}
180 A tweak is an algorithm ``alternative''. For example, to provide tradeoffs (usually between size and space). 180 A tweak is an algorithm ``alternative''. For example, to provide tradeoffs (usually between size and space).
181 They can be enabled at any pass of the configuration phase. 181 They can be enabled at any pass of the configuration phase.
191 \end{center} 191 \end{center}
192 \end{small} 192 \end{small}
193 193
194 \subsection{Build Trims} 194 \subsection{Build Trims}
195 A trim is a manner of removing functionality from a function that is not required. For instance, to perform 195 A trim is a manner of removing functionality from a function that is not required. For instance, to perform
196 RSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed. 196 RSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed.
197 Build trims are meant to be defined on the last pass of the configuration which means they are to be defined 197 Build trims are meant to be defined on the last pass of the configuration which means they are to be defined
198 only if LTM\_LAST has been defined. 198 only if LTM\_LAST has been defined.
199 199
200 \subsubsection{Moduli Related} 200 \subsubsection{Moduli Related}
201 \begin{small} 201 \begin{small}
230 & BN\_S\_MP\_MUL\_DIGS\_C \\ 230 & BN\_S\_MP\_MUL\_DIGS\_C \\
231 & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\ 231 & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
232 & BN\_S\_MP\_SQR\_C \\ 232 & BN\_S\_MP\_SQR\_C \\
233 \hline Polynomial Schmolynomial & BN\_MP\_KARATSUBA\_MUL\_C \\ 233 \hline Polynomial Schmolynomial & BN\_MP\_KARATSUBA\_MUL\_C \\
234 & BN\_MP\_KARATSUBA\_SQR\_C \\ 234 & BN\_MP\_KARATSUBA\_SQR\_C \\
235 & BN\_MP\_TOOM\_MUL\_C \\ 235 & BN\_MP\_TOOM\_MUL\_C \\
236 & BN\_MP\_TOOM\_SQR\_C \\ 236 & BN\_MP\_TOOM\_SQR\_C \\
237 237
238 \hline 238 \hline
239 \end{tabular} 239 \end{tabular}
240 \end{center} 240 \end{center}
241 \end{small} 241 \end{small}
242 242
243 243
244 \section{Purpose of LibTomMath} 244 \section{Purpose of LibTomMath}
245 Unlike GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with 245 Unlike GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with
246 bleeding edge performance in mind. First and foremost LibTomMath was written to be entirely open. Not only is the 246 bleeding edge performance in mind. First and foremost LibTomMath was written to be entirely open. Not only is the
247 source code public domain (unlike various other GPL/etc licensed code), not only is the code freely downloadable but the 247 source code public domain (unlike various other GPL/etc licensed code), not only is the code freely downloadable but the
248 source code is also accessible for computer science students attempting to learn ``BigNum'' or multiple precision 248 source code is also accessible for computer science students attempting to learn ``BigNum'' or multiple precision
249 arithmetic techniques. 249 arithmetic techniques.
250 250
251 LibTomMath was written to be an instructive collection of source code. This is why there are many comments, only one 251 LibTomMath was written to be an instructive collection of source code. This is why there are many comments, only one
252 function per source file and often I use a ``middle-road'' approach where I don't cut corners for an extra 2\% speed 252 function per source file and often I use a ``middle-road'' approach where I don't cut corners for an extra 2\% speed
253 increase. 253 increase.
254 254
275 \end{center} 275 \end{center}
276 \end{small} 276 \end{small}
277 \caption{LibTomMath Valuation} 277 \caption{LibTomMath Valuation}
278 \end{figure} 278 \end{figure}
279 279
280 It may seem odd to compare LibTomMath to GnuPG since the math in GnuPG is only a small portion of the entire application. 280 It may seem odd to compare LibTomMath to GnuPG since the math in GnuPG is only a small portion of the entire application.
281 However, LibTomMath was written with cryptography in mind. It provides essentially all of the functions a cryptosystem 281 However, LibTomMath was written with cryptography in mind. It provides essentially all of the functions a cryptosystem
282 would require when working with large integers. 282 would require when working with large integers.
283 283
284 So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in your 284 So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in your
285 own application but I think there are reasons not to. While LibTomMath is slower than libraries such as GnuMP it is 285 own application but I think there are reasons not to. While LibTomMath is slower than libraries such as GnuMP it is
286 not normally significantly slower. On x86 machines the difference is normally a factor of two when performing modular 286 not normally significantly slower. On x86 machines the difference is normally a factor of two when performing modular
287 exponentiations. It depends largely on the processor, compiler and the moduli being used. 287 exponentiations. It depends largely on the processor, compiler and the moduli being used.
288 288
289 Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern. However, 289 Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern. However,
290 on the other side of the coin LibTomMath offers you a totally free (public domain) well structured math library 290 on the other side of the coin LibTomMath offers you a totally free (public domain) well structured math library
291 that is very flexible, complete and performs well in resource contrained environments. Fast RSA for example can 291 that is very flexible, complete and performs well in resource contrained environments. Fast RSA for example can
292 be performed with as little as 8KB of ram for data (again depending on build options). 292 be performed with as little as 8KB of ram for data (again depending on build options).
293 293
294 \chapter{Getting Started with LibTomMath} 294 \chapter{Getting Started with LibTomMath}
295 \section{Building Programs} 295 \section{Building Programs}
296 In order to use LibTomMath you must include ``tommath.h'' and link against the appropriate library file (typically 296 In order to use LibTomMath you must include ``tommath.h'' and link against the appropriate library file (typically
297 libtommath.a). There is no library initialization required and the entire library is thread safe. 297 libtommath.a). There is no library initialization required and the entire library is thread safe.
298 298
299 \section{Return Codes} 299 \section{Return Codes}
300 There are three possible return codes a function may return. 300 There are three possible return codes a function may return.
301 301
325 \index{mp\_error\_to\_string} 325 \index{mp\_error\_to\_string}
326 \begin{alltt} 326 \begin{alltt}
327 char *mp_error_to_string(int code); 327 char *mp_error_to_string(int code);
328 \end{alltt} 328 \end{alltt}
329 329
330 This will return a pointer to a string which describes the given error code. It will not work for the return codes 330 This will return a pointer to a string which describes the given error code. It will not work for the return codes
331 MP\_YES and MP\_NO. 331 MP\_YES and MP\_NO.
332 332
333 \section{Data Types} 333 \section{Data Types}
334 The basic ``multiple precision integer'' type is known as the ``mp\_int'' within LibTomMath. This data type is used to 334 The basic ``multiple precision integer'' type is known as the ``mp\_int'' within LibTomMath. This data type is used to
335 organize all of the data required to manipulate the integer it represents. Within LibTomMath it has been prototyped 335 organize all of the data required to manipulate the integer it represents. Within LibTomMath it has been prototyped
336 as the following. 336 as the following.
343 \} mp_int; 343 \} mp_int;
344 \end{alltt} 344 \end{alltt}
345 345
346 Where ``mp\_digit'' is a data type that represents individual digits of the integer. By default, an mp\_digit is the 346 Where ``mp\_digit'' is a data type that represents individual digits of the integer. By default, an mp\_digit is the
347 ISO C ``unsigned long'' data type and each digit is $28-$bits long. The mp\_digit type can be configured to suit other 347 ISO C ``unsigned long'' data type and each digit is $28-$bits long. The mp\_digit type can be configured to suit other
348 platforms by defining the appropriate macros. 348 platforms by defining the appropriate macros.
349 349
350 All LTM functions that use the mp\_int type will expect a pointer to mp\_int structure. You must allocate memory to 350 All LTM functions that use the mp\_int type will expect a pointer to mp\_int structure. You must allocate memory to
351 hold the structure itself by yourself (whether off stack or heap it doesn't matter). The very first thing that must be 351 hold the structure itself by yourself (whether off stack or heap it doesn't matter). The very first thing that must be
352 done to use an mp\_int is that it must be initialized. 352 done to use an mp\_int is that it must be initialized.
353 353
372 372
373 This allows operands to be re-used which can make programming simpler. 373 This allows operands to be re-used which can make programming simpler.
374 374
375 \section{Initialization} 375 \section{Initialization}
376 \subsection{Single Initialization} 376 \subsection{Single Initialization}
377 A single mp\_int can be initialized with the ``mp\_init'' function. 377 A single mp\_int can be initialized with the ``mp\_init'' function.
378 378
379 \index{mp\_init} 379 \index{mp\_init}
380 \begin{alltt} 380 \begin{alltt}
381 int mp_init (mp_int * a); 381 int mp_init (mp_int * a);
382 \end{alltt} 382 \end{alltt}
390 \{ 390 \{
391 mp_int number; 391 mp_int number;
392 int result; 392 int result;
393 393
394 if ((result = mp_init(&number)) != MP_OKAY) \{ 394 if ((result = mp_init(&number)) != MP_OKAY) \{
395 printf("Error initializing the number. \%s", 395 printf("Error initializing the number. \%s",
396 mp_error_to_string(result)); 396 mp_error_to_string(result));
397 return EXIT_FAILURE; 397 return EXIT_FAILURE;
398 \} 398 \}
399 399
400 /* use the number */ 400 /* use the number */
401 401
402 return EXIT_SUCCESS; 402 return EXIT_SUCCESS;
403 \} 403 \}
404 \end{alltt} \end{small} 404 \end{alltt} \end{small}
405 405
406 \subsection{Single Free} 406 \subsection{Single Free}
407 When you are finished with an mp\_int it is ideal to return the heap it used back to the system. The following function 407 When you are finished with an mp\_int it is ideal to return the heap it used back to the system. The following function
408 provides this functionality. 408 provides this functionality.
409 409
410 \index{mp\_clear} 410 \index{mp\_clear}
411 \begin{alltt} 411 \begin{alltt}
412 void mp_clear (mp_int * a); 412 void mp_clear (mp_int * a);
413 \end{alltt} 413 \end{alltt}
414 414
415 The function expects a pointer to a previously initialized mp\_int structure and frees the heap it uses. It sets the 415 The function expects a pointer to a previously initialized mp\_int structure and frees the heap it uses. It sets the
416 pointer\footnote{The ``dp'' member.} within the mp\_int to \textbf{NULL} which is used to prevent double free situations. 416 pointer\footnote{The ``dp'' member.} within the mp\_int to \textbf{NULL} which is used to prevent double free situations.
417 Is is legal to call mp\_clear() twice on the same mp\_int in a row. 417 Is is legal to call mp\_clear() twice on the same mp\_int in a row.
418 418
419 \begin{small} \begin{alltt} 419 \begin{small} \begin{alltt}
420 int main(void) 420 int main(void)
421 \{ 421 \{
422 mp_int number; 422 mp_int number;
423 int result; 423 int result;
424 424
425 if ((result = mp_init(&number)) != MP_OKAY) \{ 425 if ((result = mp_init(&number)) != MP_OKAY) \{
426 printf("Error initializing the number. \%s", 426 printf("Error initializing the number. \%s",
427 mp_error_to_string(result)); 427 mp_error_to_string(result));
428 return EXIT_FAILURE; 428 return EXIT_FAILURE;
429 \} 429 \}
430 430
431 /* use the number */ 431 /* use the number */
432 432
433 /* We're done with it. */ 433 /* We're done with it. */
434 mp_clear(&number); 434 mp_clear(&number);
435 435
449 int mp_init_multi(mp_int *mp, ...); 449 int mp_init_multi(mp_int *mp, ...);
450 \end{alltt} 450 \end{alltt}
451 451
452 It accepts a \textbf{NULL} terminated list of pointers to mp\_int structures. It will attempt to initialize them all 452 It accepts a \textbf{NULL} terminated list of pointers to mp\_int structures. It will attempt to initialize them all
453 at once. If the function returns MP\_OKAY then all of the mp\_int variables are ready to use, otherwise none of them 453 at once. If the function returns MP\_OKAY then all of the mp\_int variables are ready to use, otherwise none of them
454 are available for use. A complementary mp\_clear\_multi() function allows multiple mp\_int variables to be free'd 454 are available for use. A complementary mp\_clear\_multi() function allows multiple mp\_int variables to be free'd
455 from the heap at the same time. 455 from the heap at the same time.
456 456
457 \begin{small} \begin{alltt} 457 \begin{small} \begin{alltt}
458 int main(void) 458 int main(void)
459 \{ 459 \{
460 mp_int num1, num2, num3; 460 mp_int num1, num2, num3;
461 int result; 461 int result;
462 462
463 if ((result = mp_init_multi(&num1, 463 if ((result = mp_init_multi(&num1,
464 &num2, 464 &num2,
465 &num3, NULL)) != MP\_OKAY) \{ 465 &num3, NULL)) != MP\_OKAY) \{
466 printf("Error initializing the numbers. \%s", 466 printf("Error initializing the numbers. \%s",
467 mp_error_to_string(result)); 467 mp_error_to_string(result));
468 return EXIT_FAILURE; 468 return EXIT_FAILURE;
469 \} 469 \}
470 470
471 /* use the numbers */ 471 /* use the numbers */
472 472
473 /* We're done with them. */ 473 /* We're done with them. */
474 mp_clear_multi(&num1, &num2, &num3, NULL); 474 mp_clear_multi(&num1, &num2, &num3, NULL);
475 475
476 return EXIT_SUCCESS; 476 return EXIT_SUCCESS;
477 \} 477 \}
478 \end{alltt} \end{small} 478 \end{alltt} \end{small}
479 479
480 \subsection{Other Initializers} 480 \subsection{Other Initializers}
481 To initialized and make a copy of an mp\_int the mp\_init\_copy() function has been provided. 481 To initialized and make a copy of an mp\_int the mp\_init\_copy() function has been provided.
482 482
483 \index{mp\_init\_copy} 483 \index{mp\_init\_copy}
484 \begin{alltt} 484 \begin{alltt}
485 int mp_init_copy (mp_int * a, mp_int * b); 485 int mp_init_copy (mp_int * a, mp_int * b);
486 \end{alltt} 486 \end{alltt}
495 495
496 /* initialize and do work on num1 ... */ 496 /* initialize and do work on num1 ... */
497 497
498 /* We want a copy of num1 in num2 now */ 498 /* We want a copy of num1 in num2 now */
499 if ((result = mp_init_copy(&num2, &num1)) != MP_OKAY) \{ 499 if ((result = mp_init_copy(&num2, &num1)) != MP_OKAY) \{
500 printf("Error initializing the copy. \%s", 500 printf("Error initializing the copy. \%s",
501 mp_error_to_string(result)); 501 mp_error_to_string(result));
502 return EXIT_FAILURE; 502 return EXIT_FAILURE;
503 \} 503 \}
504 504
505 /* now num2 is ready and contains a copy of num1 */ 505 /* now num2 is ready and contains a copy of num1 */
506 506
507 /* We're done with them. */ 507 /* We're done with them. */
508 mp_clear_multi(&num1, &num2, NULL); 508 mp_clear_multi(&num1, &num2, NULL);
509 509
519 \begin{alltt} 519 \begin{alltt}
520 int mp_init_size (mp_int * a, int size); 520 int mp_init_size (mp_int * a, int size);
521 \end{alltt} 521 \end{alltt}
522 522
523 The $size$ parameter must be greater than zero. If the function succeeds the mp\_int $a$ will be initialized 523 The $size$ parameter must be greater than zero. If the function succeeds the mp\_int $a$ will be initialized
524 to have $size$ digits (which are all initially zero). 524 to have $size$ digits (which are all initially zero).
525 525
526 \begin{small} \begin{alltt} 526 \begin{small} \begin{alltt}
527 int main(void) 527 int main(void)
528 \{ 528 \{
529 mp_int number; 529 mp_int number;
530 int result; 530 int result;
531 531
532 /* we need a 60-digit number */ 532 /* we need a 60-digit number */
533 if ((result = mp_init_size(&number, 60)) != MP_OKAY) \{ 533 if ((result = mp_init_size(&number, 60)) != MP_OKAY) \{
534 printf("Error initializing the number. \%s", 534 printf("Error initializing the number. \%s",
535 mp_error_to_string(result)); 535 mp_error_to_string(result));
536 return EXIT_FAILURE; 536 return EXIT_FAILURE;
537 \} 537 \}
538 538
539 /* use the number */ 539 /* use the number */
540 540
541 return EXIT_SUCCESS; 541 return EXIT_SUCCESS;
542 \} 542 \}
543 \end{alltt} \end{small} 543 \end{alltt} \end{small}
554 \end{alltt} 554 \end{alltt}
555 555
556 This will remove excess digits of the mp\_int $a$. If the operation fails the mp\_int should be intact without the 556 This will remove excess digits of the mp\_int $a$. If the operation fails the mp\_int should be intact without the
557 excess digits being removed. Note that you can use a shrunk mp\_int in further computations, however, such operations 557 excess digits being removed. Note that you can use a shrunk mp\_int in further computations, however, such operations
558 will require heap operations which can be slow. It is not ideal to shrink mp\_int variables that you will further 558 will require heap operations which can be slow. It is not ideal to shrink mp\_int variables that you will further
559 modify in the system (unless you are seriously low on memory). 559 modify in the system (unless you are seriously low on memory).
560 560
561 \begin{small} \begin{alltt} 561 \begin{small} \begin{alltt}
562 int main(void) 562 int main(void)
563 \{ 563 \{
564 mp_int number; 564 mp_int number;
565 int result; 565 int result;
566 566
567 if ((result = mp_init(&number)) != MP_OKAY) \{ 567 if ((result = mp_init(&number)) != MP_OKAY) \{
568 printf("Error initializing the number. \%s", 568 printf("Error initializing the number. \%s",
569 mp_error_to_string(result)); 569 mp_error_to_string(result));
570 return EXIT_FAILURE; 570 return EXIT_FAILURE;
571 \} 571 \}
572 572
573 /* use the number [e.g. pre-computation] */ 573 /* use the number [e.g. pre-computation] */
574 574
575 /* We're done with it for now. */ 575 /* We're done with it for now. */
576 if ((result = mp_shrink(&number)) != MP_OKAY) \{ 576 if ((result = mp_shrink(&number)) != MP_OKAY) \{
577 printf("Error shrinking the number. \%s", 577 printf("Error shrinking the number. \%s",
578 mp_error_to_string(result)); 578 mp_error_to_string(result));
579 return EXIT_FAILURE; 579 return EXIT_FAILURE;
580 \} 580 \}
581 581
582 /* use it .... */ 582 /* use it .... */
583 583
584 584
585 /* we're done with it. */ 585 /* we're done with it. */
586 mp_clear(&number); 586 mp_clear(&number);
587 587
588 return EXIT_SUCCESS; 588 return EXIT_SUCCESS;
589 \} 589 \}
590 \end{alltt} \end{small} 590 \end{alltt} \end{small}
593 593
594 Within the mp\_int structure are two parameters which control the limitations of the array of digits that represent 594 Within the mp\_int structure are two parameters which control the limitations of the array of digits that represent
595 the integer the mp\_int is meant to equal. The \textit{used} parameter dictates how many digits are significant, that is, 595 the integer the mp\_int is meant to equal. The \textit{used} parameter dictates how many digits are significant, that is,
596 contribute to the value of the mp\_int. The \textit{alloc} parameter dictates how many digits are currently available in 596 contribute to the value of the mp\_int. The \textit{alloc} parameter dictates how many digits are currently available in
597 the array. If you need to perform an operation that requires more digits you will have to mp\_grow() the mp\_int to 597 the array. If you need to perform an operation that requires more digits you will have to mp\_grow() the mp\_int to
598 your desired size. 598 your desired size.
599 599
600 \index{mp\_grow} 600 \index{mp\_grow}
601 \begin{alltt} 601 \begin{alltt}
602 int mp_grow (mp_int * a, int size); 602 int mp_grow (mp_int * a, int size);
603 \end{alltt} 603 \end{alltt}
610 \{ 610 \{
611 mp_int number; 611 mp_int number;
612 int result; 612 int result;
613 613
614 if ((result = mp_init(&number)) != MP_OKAY) \{ 614 if ((result = mp_init(&number)) != MP_OKAY) \{
615 printf("Error initializing the number. \%s", 615 printf("Error initializing the number. \%s",
616 mp_error_to_string(result)); 616 mp_error_to_string(result));
617 return EXIT_FAILURE; 617 return EXIT_FAILURE;
618 \} 618 \}
619 619
620 /* use the number */ 620 /* use the number */
621 621
622 /* We need to add 20 digits to the number */ 622 /* We need to add 20 digits to the number */
623 if ((result = mp_grow(&number, number.alloc + 20)) != MP_OKAY) \{ 623 if ((result = mp_grow(&number, number.alloc + 20)) != MP_OKAY) \{
624 printf("Error growing the number. \%s", 624 printf("Error growing the number. \%s",
625 mp_error_to_string(result)); 625 mp_error_to_string(result));
626 return EXIT_FAILURE; 626 return EXIT_FAILURE;
627 \} 627 \}
628 628
629 629
630 /* use the number */ 630 /* use the number */
631 631
632 /* we're done with it. */ 632 /* we're done with it. */
633 mp_clear(&number); 633 mp_clear(&number);
634 634
635 return EXIT_SUCCESS; 635 return EXIT_SUCCESS;
636 \} 636 \}
637 \end{alltt} \end{small} 637 \end{alltt} \end{small}
639 \chapter{Basic Operations} 639 \chapter{Basic Operations}
640 \section{Small Constants} 640 \section{Small Constants}
641 Setting mp\_ints to small constants is a relatively common operation. To accomodate these instances there are two 641 Setting mp\_ints to small constants is a relatively common operation. To accomodate these instances there are two
642 small constant assignment functions. The first function is used to set a single digit constant while the second sets 642 small constant assignment functions. The first function is used to set a single digit constant while the second sets
643 an ISO C style ``unsigned long'' constant. The reason for both functions is efficiency. Setting a single digit is quick but the 643 an ISO C style ``unsigned long'' constant. The reason for both functions is efficiency. Setting a single digit is quick but the
644 domain of a digit can change (it's always at least $0 \ldots 127$). 644 domain of a digit can change (it's always at least $0 \ldots 127$).
645 645
646 \subsection{Single Digit} 646 \subsection{Single Digit}
647 647
648 Setting a single digit can be accomplished with the following function. 648 Setting a single digit can be accomplished with the following function.
649 649
661 \{ 661 \{
662 mp_int number; 662 mp_int number;
663 int result; 663 int result;
664 664
665 if ((result = mp_init(&number)) != MP_OKAY) \{ 665 if ((result = mp_init(&number)) != MP_OKAY) \{
666 printf("Error initializing the number. \%s", 666 printf("Error initializing the number. \%s",
667 mp_error_to_string(result)); 667 mp_error_to_string(result));
668 return EXIT_FAILURE; 668 return EXIT_FAILURE;
669 \} 669 \}
670 670
671 /* set the number to 5 */ 671 /* set the number to 5 */
672 mp_set(&number, 5); 672 mp_set(&number, 5);
673 673
674 /* we're done with it. */ 674 /* we're done with it. */
675 mp_clear(&number); 675 mp_clear(&number);
676 676
677 return EXIT_SUCCESS; 677 return EXIT_SUCCESS;
678 \} 678 \}
679 \end{alltt} \end{small} 679 \end{alltt} \end{small}
680 680
681 \subsection{Long Constants} 681 \subsection{Long Constants}
682 682
683 To set a constant that is the size of an ISO C ``unsigned long'' and larger than a single digit the following function 683 To set a constant that is the size of an ISO C ``unsigned long'' and larger than a single digit the following function
684 can be used. 684 can be used.
685 685
686 \index{mp\_set\_int} 686 \index{mp\_set\_int}
687 \begin{alltt} 687 \begin{alltt}
688 int mp_set_int (mp_int * a, unsigned long b); 688 int mp_set_int (mp_int * a, unsigned long b);
689 \end{alltt} 689 \end{alltt}
690 690
691 This will assign the value of the 32-bit variable $b$ to the mp\_int $a$. Unlike mp\_set() this function will always 691 This will assign the value of the 32-bit variable $b$ to the mp\_int $a$. Unlike mp\_set() this function will always
692 accept a 32-bit input regardless of the size of a single digit. However, since the value may span several digits 692 accept a 32-bit input regardless of the size of a single digit. However, since the value may span several digits
693 this function can fail if it runs out of heap memory. 693 this function can fail if it runs out of heap memory.
694 694
695 To get the ``unsigned long'' copy of an mp\_int the following function can be used. 695 To get the ``unsigned long'' copy of an mp\_int the following function can be used.
696 696
697 \index{mp\_get\_int} 697 \index{mp\_get\_int}
698 \begin{alltt} 698 \begin{alltt}
699 unsigned long mp_get_int (mp_int * a); 699 unsigned long mp_get_int (mp_int * a);
700 \end{alltt} 700 \end{alltt}
701 701
702 This will return the 32 least significant bits of the mp\_int $a$. 702 This will return the 32 least significant bits of the mp\_int $a$.
703 703
704 \begin{small} \begin{alltt} 704 \begin{small} \begin{alltt}
705 int main(void) 705 int main(void)
706 \{ 706 \{
707 mp_int number; 707 mp_int number;
708 int result; 708 int result;
709 709
710 if ((result = mp_init(&number)) != MP_OKAY) \{ 710 if ((result = mp_init(&number)) != MP_OKAY) \{
711 printf("Error initializing the number. \%s", 711 printf("Error initializing the number. \%s",
712 mp_error_to_string(result)); 712 mp_error_to_string(result));
713 return EXIT_FAILURE; 713 return EXIT_FAILURE;
714 \} 714 \}
715 715
716 /* set the number to 654321 (note this is bigger than 127) */ 716 /* set the number to 654321 (note this is bigger than 127) */
717 if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{ 717 if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{
718 printf("Error setting the value of the number. \%s", 718 printf("Error setting the value of the number. \%s",
719 mp_error_to_string(result)); 719 mp_error_to_string(result));
720 return EXIT_FAILURE; 720 return EXIT_FAILURE;
721 \} 721 \}
722 722
723 printf("number == \%lu", mp_get_int(&number)); 723 printf("number == \%lu", mp_get_int(&number));
724 724
725 /* we're done with it. */ 725 /* we're done with it. */
726 mp_clear(&number); 726 mp_clear(&number);
727 727
728 return EXIT_SUCCESS; 728 return EXIT_SUCCESS;
729 \} 729 \}
730 \end{alltt} \end{small} 730 \end{alltt} \end{small}
733 733
734 \begin{alltt} 734 \begin{alltt}
735 number == 654321 735 number == 654321
736 \end{alltt} 736 \end{alltt}
737 737
738 \subsection{Long Constants - platform dependant}
739
740 \index{mp\_set\_long}
741 \begin{alltt}
742 int mp_set_long (mp_int * a, unsigned long b);
743 \end{alltt}
744
745 This will assign the value of the platform-dependant sized variable $b$ to the mp\_int $a$.
746
747 To get the ``unsigned long'' copy of an mp\_int the following function can be used.
748
749 \index{mp\_get\_long}
750 \begin{alltt}
751 unsigned long mp_get_long (mp_int * a);
752 \end{alltt}
753
754 This will return the least significant bits of the mp\_int $a$ that fit into an ``unsigned long''.
755
756 \subsection{Long Long Constants}
757
758 \index{mp\_set\_long\_long}
759 \begin{alltt}
760 int mp_set_long_long (mp_int * a, unsigned long long b);
761 \end{alltt}
762
763 This will assign the value of the 64-bit variable $b$ to the mp\_int $a$.
764
765 To get the ``unsigned long long'' copy of an mp\_int the following function can be used.
766
767 \index{mp\_get\_long\_long}
768 \begin{alltt}
769 unsigned long long mp_get_long_long (mp_int * a);
770 \end{alltt}
771
772 This will return the 64 least significant bits of the mp\_int $a$.
773
738 \subsection{Initialize and Setting Constants} 774 \subsection{Initialize and Setting Constants}
739 To both initialize and set small constants the following two functions are available. 775 To both initialize and set small constants the following two functions are available.
740 \index{mp\_init\_set} \index{mp\_init\_set\_int} 776 \index{mp\_init\_set} \index{mp\_init\_set\_int}
741 \begin{alltt} 777 \begin{alltt}
742 int mp_init_set (mp_int * a, mp_digit b); 778 int mp_init_set (mp_int * a, mp_digit b);
743 int mp_init_set_int (mp_int * a, unsigned long b); 779 int mp_init_set_int (mp_int * a, unsigned long b);
744 \end{alltt} 780 \end{alltt}
745 781
746 Both functions work like the previous counterparts except they first mp\_init $a$ before setting the values. 782 Both functions work like the previous counterparts except they first mp\_init $a$ before setting the values.
747 783
748 \begin{alltt} 784 \begin{alltt}
749 int main(void) 785 int main(void)
750 \{ 786 \{
751 mp_int number1, number2; 787 mp_int number1, number2;
752 int result; 788 int result;
753 789
754 /* initialize and set a single digit */ 790 /* initialize and set a single digit */
755 if ((result = mp_init_set(&number1, 100)) != MP_OKAY) \{ 791 if ((result = mp_init_set(&number1, 100)) != MP_OKAY) \{
756 printf("Error setting number1: \%s", 792 printf("Error setting number1: \%s",
757 mp_error_to_string(result)); 793 mp_error_to_string(result));
758 return EXIT_FAILURE; 794 return EXIT_FAILURE;
759 \} 795 \}
760 796
761 /* initialize and set a long */ 797 /* initialize and set a long */
762 if ((result = mp_init_set_int(&number2, 1023)) != MP_OKAY) \{ 798 if ((result = mp_init_set_int(&number2, 1023)) != MP_OKAY) \{
763 printf("Error setting number2: \%s", 799 printf("Error setting number2: \%s",
764 mp_error_to_string(result)); 800 mp_error_to_string(result));
765 return EXIT_FAILURE; 801 return EXIT_FAILURE;
766 \} 802 \}
767 803
768 /* display */ 804 /* display */
799 \end{center} 835 \end{center}
800 \caption{Comparison Codes for $a, b$} 836 \caption{Comparison Codes for $a, b$}
801 \label{fig:CMP} 837 \label{fig:CMP}
802 \end{figure} 838 \end{figure}
803 839
804 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 840 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
805 $b$. 841 $b$.
806 842
807 \subsection{Unsigned comparison} 843 \subsection{Unsigned comparison}
808 844
809 An unsigned comparison considers only the digits themselves and not the associated \textit{sign} flag of the 845 An unsigned comparison considers only the digits themselves and not the associated \textit{sign} flag of the
810 mp\_int structures. This is analogous to an absolute comparison. The function mp\_cmp\_mag() will compare two 846 mp\_int structures. This is analogous to an absolute comparison. The function mp\_cmp\_mag() will compare two
811 mp\_int variables based on their digits only. 847 mp\_int variables based on their digits only.
812 848
813 \index{mp\_cmp\_mag} 849 \index{mp\_cmp\_mag}
814 \begin{alltt} 850 \begin{alltt}
815 int mp_cmp_mag(mp_int * a, mp_int * b); 851 int mp_cmp_mag(mp_int * a, mp_int * b);
816 \end{alltt} 852 \end{alltt}
822 \{ 858 \{
823 mp_int number1, number2; 859 mp_int number1, number2;
824 int result; 860 int result;
825 861
826 if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{ 862 if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{
827 printf("Error initializing the numbers. \%s", 863 printf("Error initializing the numbers. \%s",
828 mp_error_to_string(result)); 864 mp_error_to_string(result));
829 return EXIT_FAILURE; 865 return EXIT_FAILURE;
830 \} 866 \}
831 867
832 /* set the number1 to 5 */ 868 /* set the number1 to 5 */
833 mp_set(&number1, 5); 869 mp_set(&number1, 5);
834 870
835 /* set the number2 to -6 */ 871 /* set the number2 to -6 */
836 mp_set(&number2, 6); 872 mp_set(&number2, 6);
837 if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{ 873 if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{
838 printf("Error negating number2. \%s", 874 printf("Error negating number2. \%s",
839 mp_error_to_string(result)); 875 mp_error_to_string(result));
840 return EXIT_FAILURE; 876 return EXIT_FAILURE;
841 \} 877 \}
842 878
843 switch(mp_cmp_mag(&number1, &number2)) \{ 879 switch(mp_cmp_mag(&number1, &number2)) \{
844 case MP_GT: printf("|number1| > |number2|"); break; 880 case MP_GT: printf("|number1| > |number2|"); break;
845 case MP_EQ: printf("|number1| = |number2|"); break; 881 case MP_EQ: printf("|number1| = |number2|"); break;
846 case MP_LT: printf("|number1| < |number2|"); break; 882 case MP_LT: printf("|number1| < |number2|"); break;
847 \} 883 \}
848 884
849 /* we're done with it. */ 885 /* we're done with it. */
850 mp_clear_multi(&number1, &number2, NULL); 886 mp_clear_multi(&number1, &number2, NULL);
851 887
852 return EXIT_SUCCESS; 888 return EXIT_SUCCESS;
853 \} 889 \}
854 \end{alltt} \end{small} 890 \end{alltt} \end{small}
855 891
856 If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes 892 If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes
857 successfully it should print the following. 893 successfully it should print the following.
858 894
859 \begin{alltt} 895 \begin{alltt}
860 |number1| < |number2| 896 |number1| < |number2|
861 \end{alltt} 897 \end{alltt}
880 \{ 916 \{
881 mp_int number1, number2; 917 mp_int number1, number2;
882 int result; 918 int result;
883 919
884 if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{ 920 if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{
885 printf("Error initializing the numbers. \%s", 921 printf("Error initializing the numbers. \%s",
886 mp_error_to_string(result)); 922 mp_error_to_string(result));
887 return EXIT_FAILURE; 923 return EXIT_FAILURE;
888 \} 924 \}
889 925
890 /* set the number1 to 5 */ 926 /* set the number1 to 5 */
891 mp_set(&number1, 5); 927 mp_set(&number1, 5);
892 928
893 /* set the number2 to -6 */ 929 /* set the number2 to -6 */
894 mp_set(&number2, 6); 930 mp_set(&number2, 6);
895 if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{ 931 if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{
896 printf("Error negating number2. \%s", 932 printf("Error negating number2. \%s",
897 mp_error_to_string(result)); 933 mp_error_to_string(result));
898 return EXIT_FAILURE; 934 return EXIT_FAILURE;
899 \} 935 \}
900 936
901 switch(mp_cmp(&number1, &number2)) \{ 937 switch(mp_cmp(&number1, &number2)) \{
902 case MP_GT: printf("number1 > number2"); break; 938 case MP_GT: printf("number1 > number2"); break;
903 case MP_EQ: printf("number1 = number2"); break; 939 case MP_EQ: printf("number1 = number2"); break;
904 case MP_LT: printf("number1 < number2"); break; 940 case MP_LT: printf("number1 < number2"); break;
905 \} 941 \}
906 942
907 /* we're done with it. */ 943 /* we're done with it. */
908 mp_clear_multi(&number1, &number2, NULL); 944 mp_clear_multi(&number1, &number2, NULL);
909 945
910 return EXIT_SUCCESS; 946 return EXIT_SUCCESS;
911 \} 947 \}
912 \end{alltt} \end{small} 948 \end{alltt} \end{small}
913 949
914 If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes 950 If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes
915 successfully it should print the following. 951 successfully it should print the following.
916 952
917 \begin{alltt} 953 \begin{alltt}
918 number1 > number2 954 number1 > number2
919 \end{alltt} 955 \end{alltt}
925 \index{mp\_cmp\_d} 961 \index{mp\_cmp\_d}
926 \begin{alltt} 962 \begin{alltt}
927 int mp_cmp_d(mp_int * a, mp_digit b); 963 int mp_cmp_d(mp_int * a, mp_digit b);
928 \end{alltt} 964 \end{alltt}
929 965
930 This will compare $a$ to the left of $b$ using a signed comparison. Note that it will always treat $b$ as 966 This will compare $a$ to the left of $b$ using a signed comparison. Note that it will always treat $b$ as
931 positive. This function is rather handy when you have to compare against small values such as $1$ (which often 967 positive. This function is rather handy when you have to compare against small values such as $1$ (which often
932 comes up in cryptography). The function cannot fail and will return one of the tree compare condition codes 968 comes up in cryptography). The function cannot fail and will return one of the tree compare condition codes
933 listed in figure \ref{fig:CMP}. 969 listed in figure \ref{fig:CMP}.
934 970
935 971
938 \{ 974 \{
939 mp_int number; 975 mp_int number;
940 int result; 976 int result;
941 977
942 if ((result = mp_init(&number)) != MP_OKAY) \{ 978 if ((result = mp_init(&number)) != MP_OKAY) \{
943 printf("Error initializing the number. \%s", 979 printf("Error initializing the number. \%s",
944 mp_error_to_string(result)); 980 mp_error_to_string(result));
945 return EXIT_FAILURE; 981 return EXIT_FAILURE;
946 \} 982 \}
947 983
948 /* set the number to 5 */ 984 /* set the number to 5 */
949 mp_set(&number, 5); 985 mp_set(&number, 5);
950 986
951 switch(mp_cmp_d(&number, 7)) \{ 987 switch(mp_cmp_d(&number, 7)) \{
952 case MP_GT: printf("number > 7"); break; 988 case MP_GT: printf("number > 7"); break;
953 case MP_EQ: printf("number = 7"); break; 989 case MP_EQ: printf("number = 7"); break;
954 case MP_LT: printf("number < 7"); break; 990 case MP_LT: printf("number < 7"); break;
955 \} 991 \}
956 992
957 /* we're done with it. */ 993 /* we're done with it. */
958 mp_clear(&number); 994 mp_clear(&number);
959 995
960 return EXIT_SUCCESS; 996 return EXIT_SUCCESS;
961 \} 997 \}
962 \end{alltt} \end{small} 998 \end{alltt} \end{small}
973 AND, XOR and OR directly. These operations are very quick. 1009 AND, XOR and OR directly. These operations are very quick.
974 1010
975 \subsection{Multiplication by two} 1011 \subsection{Multiplication by two}
976 1012
977 Multiplications and divisions by any power of two can be performed with quick logical shifts either left or 1013 Multiplications and divisions by any power of two can be performed with quick logical shifts either left or
978 right depending on the operation. 1014 right depending on the operation.
979 1015
980 When multiplying or dividing by two a special case routine can be used which are as follows. 1016 When multiplying or dividing by two a special case routine can be used which are as follows.
981 \index{mp\_mul\_2} \index{mp\_div\_2} 1017 \index{mp\_mul\_2} \index{mp\_div\_2}
982 \begin{alltt} 1018 \begin{alltt}
983 int mp_mul_2(mp_int * a, mp_int * b); 1019 int mp_mul_2(mp_int * a, mp_int * b);
992 \{ 1028 \{
993 mp_int number; 1029 mp_int number;
994 int result; 1030 int result;
995 1031
996 if ((result = mp_init(&number)) != MP_OKAY) \{ 1032 if ((result = mp_init(&number)) != MP_OKAY) \{
997 printf("Error initializing the number. \%s", 1033 printf("Error initializing the number. \%s",
998 mp_error_to_string(result)); 1034 mp_error_to_string(result));
999 return EXIT_FAILURE; 1035 return EXIT_FAILURE;
1000 \} 1036 \}
1001 1037
1002 /* set the number to 5 */ 1038 /* set the number to 5 */
1003 mp_set(&number, 5); 1039 mp_set(&number, 5);
1004 1040
1005 /* multiply by two */ 1041 /* multiply by two */
1006 if ((result = mp\_mul\_2(&number, &number)) != MP_OKAY) \{ 1042 if ((result = mp\_mul\_2(&number, &number)) != MP_OKAY) \{
1007 printf("Error multiplying the number. \%s", 1043 printf("Error multiplying the number. \%s",
1008 mp_error_to_string(result)); 1044 mp_error_to_string(result));
1009 return EXIT_FAILURE; 1045 return EXIT_FAILURE;
1010 \} 1046 \}
1011 switch(mp_cmp_d(&number, 7)) \{ 1047 switch(mp_cmp_d(&number, 7)) \{
1012 case MP_GT: printf("2*number > 7"); break; 1048 case MP_GT: printf("2*number > 7"); break;
1014 case MP_LT: printf("2*number < 7"); break; 1050 case MP_LT: printf("2*number < 7"); break;
1015 \} 1051 \}
1016 1052
1017 /* now divide by two */ 1053 /* now divide by two */
1018 if ((result = mp\_div\_2(&number, &number)) != MP_OKAY) \{ 1054 if ((result = mp\_div\_2(&number, &number)) != MP_OKAY) \{
1019 printf("Error dividing the number. \%s", 1055 printf("Error dividing the number. \%s",
1020 mp_error_to_string(result)); 1056 mp_error_to_string(result));
1021 return EXIT_FAILURE; 1057 return EXIT_FAILURE;
1022 \} 1058 \}
1023 switch(mp_cmp_d(&number, 7)) \{ 1059 switch(mp_cmp_d(&number, 7)) \{
1024 case MP_GT: printf("2*number/2 > 7"); break; 1060 case MP_GT: printf("2*number/2 > 7"); break;
1025 case MP_EQ: printf("2*number/2 = 7"); break; 1061 case MP_EQ: printf("2*number/2 = 7"); break;
1026 case MP_LT: printf("2*number/2 < 7"); break; 1062 case MP_LT: printf("2*number/2 < 7"); break;
1027 \} 1063 \}
1028 1064
1029 /* we're done with it. */ 1065 /* we're done with it. */
1030 mp_clear(&number); 1066 mp_clear(&number);
1031 1067
1032 return EXIT_SUCCESS; 1068 return EXIT_SUCCESS;
1033 \} 1069 \}
1034 \end{alltt} \end{small} 1070 \end{alltt} \end{small}
1038 \begin{alltt} 1074 \begin{alltt}
1039 2*number > 7 1075 2*number > 7
1040 2*number/2 < 7 1076 2*number/2 < 7
1041 \end{alltt} 1077 \end{alltt}
1042 1078
1043 Since $10 > 7$ and $5 < 7$. To multiply by a power of two the following function can be used. 1079 Since $10 > 7$ and $5 < 7$.
1080
1081 To multiply by a power of two the following function can be used.
1044 1082
1045 \index{mp\_mul\_2d} 1083 \index{mp\_mul\_2d}
1046 \begin{alltt} 1084 \begin{alltt}
1047 int mp_mul_2d(mp_int * a, int b, mp_int * c); 1085 int mp_mul_2d(mp_int * a, int b, mp_int * c);
1048 \end{alltt} 1086 \end{alltt}
1049 1087
1050 This will multiply $a$ by $2^b$ and store the result in ``c''. If the value of $b$ is less than or equal to 1088 This will multiply $a$ by $2^b$ and store the result in ``c''. If the value of $b$ is less than or equal to
1051 zero the function will copy $a$ to ``c'' without performing any further actions. 1089 zero the function will copy $a$ to ``c'' without performing any further actions. The multiplication itself
1090 is implemented as a right-shift operation of $a$ by $b$ bits.
1052 1091
1053 To divide by a power of two use the following. 1092 To divide by a power of two use the following.
1054 1093
1055 \index{mp\_div\_2d} 1094 \index{mp\_div\_2d}
1056 \begin{alltt} 1095 \begin{alltt}
1057 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d); 1096 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
1058 \end{alltt} 1097 \end{alltt}
1059 Which will divide $a$ by $2^b$, store the quotient in ``c'' and the remainder in ``d'. If $b \le 0$ then the 1098 Which will divide $a$ by $2^b$, store the quotient in ``c'' and the remainder in ``d'. If $b \le 0$ then the
1060 function simply copies $a$ over to ``c'' and zeroes $d$. The variable $d$ may be passed as a \textbf{NULL} 1099 function simply copies $a$ over to ``c'' and zeroes $d$. The variable $d$ may be passed as a \textbf{NULL}
1061 value to signal that the remainder is not desired. 1100 value to signal that the remainder is not desired. The division itself is implemented as a left-shift
1101 operation of $a$ by $b$ bits.
1062 1102
1063 \subsection{Polynomial Basis Operations} 1103 \subsection{Polynomial Basis Operations}
1064 1104
1065 Strictly speaking the organization of the integers within the mp\_int structures is what is known as a 1105 Strictly speaking the organization of the integers within the mp\_int structures is what is known as a
1066 ``polynomial basis''. This simply means a field element is stored by divisions of a radix. For example, if 1106 ``polynomial basis''. This simply means a field element is stored by divisions of a radix. For example, if
1067 $f(x) = \sum_{i=0}^{k} y_ix^k$ for any vector $\vec y$ then the array of digits in $\vec y$ are said to be 1107 $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 the polynomial basis representation of $z$ if $f(\beta) = z$ for a given radix $\beta$. 1108 the polynomial basis representation of $z$ if $f(\beta) = z$ for a given radix $\beta$.
1069 1109
1070 To multiply by the polynomial $g(x) = x$ all you have todo is shift the digits of the basis left one place. The 1110 To multiply by the polynomial $g(x) = x$ all you have todo is shift the digits of the basis left one place. The
1071 following function provides this operation. 1111 following function provides this operation.
1072 1112
1073 \index{mp\_lshd} 1113 \index{mp\_lshd}
1095 int mp_or (mp_int * a, mp_int * b, mp_int * c); 1135 int mp_or (mp_int * a, mp_int * b, mp_int * c);
1096 int mp_and (mp_int * a, mp_int * b, mp_int * c); 1136 int mp_and (mp_int * a, mp_int * b, mp_int * c);
1097 int mp_xor (mp_int * a, mp_int * b, mp_int * c); 1137 int mp_xor (mp_int * a, mp_int * b, mp_int * c);
1098 \end{alltt} 1138 \end{alltt}
1099 1139
1100 Which compute $c = a \odot b$ where $\odot$ is one of OR, AND or XOR. 1140 Which compute $c = a \odot b$ where $\odot$ is one of OR, AND or XOR.
1101 1141
1102 \section{Addition and Subtraction} 1142 \section{Addition and Subtraction}
1103 1143
1104 To compute an addition or subtraction the following two functions can be used. 1144 To compute an addition or subtraction the following two functions can be used.
1105 1145
1120 \index{mp\_neg} 1160 \index{mp\_neg}
1121 \begin{alltt} 1161 \begin{alltt}
1122 int mp_neg (mp_int * a, mp_int * b); 1162 int mp_neg (mp_int * a, mp_int * b);
1123 \end{alltt} 1163 \end{alltt}
1124 1164
1125 Which assigns $-a$ to $b$. 1165 Which assigns $-a$ to $b$.
1126 1166
1127 \subsection{Absolute} 1167 \subsection{Absolute}
1128 Simple integer absolutes can be performed with the following. 1168 Simple integer absolutes can be performed with the following.
1129 1169
1130 \index{mp\_neg} 1170 \index{mp\_neg}
1131 \begin{alltt} 1171 \begin{alltt}
1132 int mp_abs (mp_int * a, mp_int * b); 1172 int mp_abs (mp_int * a, mp_int * b);
1133 \end{alltt} 1173 \end{alltt}
1134 1174
1135 Which assigns $\vert a \vert$ to $b$. 1175 Which assigns $\vert a \vert$ to $b$.
1136 1176
1137 \section{Integer Division and Remainder} 1177 \section{Integer Division and Remainder}
1138 To perform a complete and general integer division with remainder use the following function. 1178 To perform a complete and general integer division with remainder use the following function.
1139 1179
1140 \index{mp\_div} 1180 \index{mp\_div}
1141 \begin{alltt} 1181 \begin{alltt}
1142 int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d); 1182 int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
1143 \end{alltt} 1183 \end{alltt}
1144 1184
1145 This divides $a$ by $b$ and stores the quotient in $c$ and $d$. The signed quotient is computed such that 1185 This divides $a$ by $b$ and stores the quotient in $c$ and $d$. The signed quotient is computed such that
1146 $bc + d = a$. Note that either of $c$ or $d$ can be set to \textbf{NULL} if their value is not required. If 1186 $bc + d = a$. Note that either of $c$ or $d$ can be set to \textbf{NULL} if their value is not required. If
1147 $b$ is zero the function returns \textbf{MP\_VAL}. 1187 $b$ is zero the function returns \textbf{MP\_VAL}.
1148 1188
1149 1189
1150 \chapter{Multiplication and Squaring} 1190 \chapter{Multiplication and Squaring}
1151 \section{Multiplication} 1191 \section{Multiplication}
1152 A full signed integer multiplication can be performed with the following. 1192 A full signed integer multiplication can be performed with the following.
1153 \index{mp\_mul} 1193 \index{mp\_mul}
1154 \begin{alltt} 1194 \begin{alltt}
1155 int mp_mul (mp_int * a, mp_int * b, mp_int * c); 1195 int mp_mul (mp_int * a, mp_int * b, mp_int * c);
1156 \end{alltt} 1196 \end{alltt}
1157 Which assigns the full signed product $ab$ to $c$. This function actually breaks into one of four cases which are 1197 Which assigns the full signed product $ab$ to $c$. This function actually breaks into one of four cases which are
1158 specific multiplication routines optimized for given parameters. First there are the Toom-Cook multiplications which 1198 specific multiplication routines optimized for given parameters. First there are the Toom-Cook multiplications which
1159 should only be used with very large inputs. This is followed by the Karatsuba multiplications which are for moderate 1199 should only be used with very large inputs. This is followed by the Karatsuba multiplications which are for moderate
1160 sized inputs. Then followed by the Comba and baseline multipliers. 1200 sized inputs. Then followed by the Comba and baseline multipliers.
1161 1201
1162 Fortunately for the developer you don't really need to know this unless you really want to fine tune the system. mp\_mul() 1202 Fortunately for the developer you don't really need to know this unless you really want to fine tune the system. mp\_mul()
1167 \{ 1207 \{
1168 mp_int number1, number2; 1208 mp_int number1, number2;
1169 int result; 1209 int result;
1170 1210
1171 /* Initialize the numbers */ 1211 /* Initialize the numbers */
1172 if ((result = mp_init_multi(&number1, 1212 if ((result = mp_init_multi(&number1,
1173 &number2, NULL)) != MP_OKAY) \{ 1213 &number2, NULL)) != MP_OKAY) \{
1174 printf("Error initializing the numbers. \%s", 1214 printf("Error initializing the numbers. \%s",
1175 mp_error_to_string(result)); 1215 mp_error_to_string(result));
1176 return EXIT_FAILURE; 1216 return EXIT_FAILURE;
1177 \} 1217 \}
1178 1218
1179 /* set the terms */ 1219 /* set the terms */
1180 if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{ 1220 if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{
1181 printf("Error setting number1. \%s", 1221 printf("Error setting number1. \%s",
1182 mp_error_to_string(result)); 1222 mp_error_to_string(result));
1183 return EXIT_FAILURE; 1223 return EXIT_FAILURE;
1184 \} 1224 \}
1185 1225
1186 if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{ 1226 if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{
1187 printf("Error setting number2. \%s", 1227 printf("Error setting number2. \%s",
1188 mp_error_to_string(result)); 1228 mp_error_to_string(result));
1189 return EXIT_FAILURE; 1229 return EXIT_FAILURE;
1190 \} 1230 \}
1191 1231
1192 /* multiply them */ 1232 /* multiply them */
1193 if ((result = mp_mul(&number1, &number2, 1233 if ((result = mp_mul(&number1, &number2,
1194 &number1)) != MP_OKAY) \{ 1234 &number1)) != MP_OKAY) \{
1195 printf("Error multiplying terms. \%s", 1235 printf("Error multiplying terms. \%s",
1196 mp_error_to_string(result)); 1236 mp_error_to_string(result));
1197 return EXIT_FAILURE; 1237 return EXIT_FAILURE;
1198 \} 1238 \}
1199 1239
1200 /* display */ 1240 /* display */
1203 /* free terms and return */ 1243 /* free terms and return */
1204 mp_clear_multi(&number1, &number2, NULL); 1244 mp_clear_multi(&number1, &number2, NULL);
1205 1245
1206 return EXIT_SUCCESS; 1246 return EXIT_SUCCESS;
1207 \} 1247 \}
1208 \end{alltt} 1248 \end{alltt}
1209 1249
1210 If this program succeeds it shall output the following. 1250 If this program succeeds it shall output the following.
1211 1251
1212 \begin{alltt} 1252 \begin{alltt}
1213 number1 * number2 == 262911 1253 number1 * number2 == 262911
1222 int mp_sqr (mp_int * a, mp_int * b); 1262 int mp_sqr (mp_int * a, mp_int * b);
1223 \end{alltt} 1263 \end{alltt}
1224 1264
1225 Will square $a$ and store it in $b$. Like the case of multiplication there are four different squaring 1265 Will square $a$ and store it in $b$. Like the case of multiplication there are four different squaring
1226 algorithms all which can be called from mp\_sqr(). It is ideal to use mp\_sqr over mp\_mul when squaring terms because 1266 algorithms all which can be called from mp\_sqr(). It is ideal to use mp\_sqr over mp\_mul when squaring terms because
1227 of the speed difference. 1267 of the speed difference.
1228 1268
1229 \section{Tuning Polynomial Basis Routines} 1269 \section{Tuning Polynomial Basis Routines}
1230 1270
1231 Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that 1271 Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that
1232 the Comba and baseline algorithms use. At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectively they require 1272 the Comba and baseline algorithms use. At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectively they require
1233 considerably less work. For example, a 10000-digit multiplication would take roughly 724,000 single precision 1273 considerably less work. For example, a 10000-digit multiplication would take roughly 724,000 single precision
1234 multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor 1274 multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor
1235 of 138). 1275 of 138).
1236 1276
1237 So why not always use Karatsuba or Toom-Cook? The simple answer is that they have so much overhead that they're not 1277 So why not always use Karatsuba or Toom-Cook? The simple answer is that they have so much overhead that they're not
1238 actually faster than Comba until you hit distinct ``cutoff'' points. For Karatsuba with the default configuration, 1278 actually faster than Comba until you hit distinct ``cutoff'' points. For Karatsuba with the default configuration,
1239 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 1279 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
1240 110 digits Karatsuba and Comba multiplications just about break even and for 110+ digits Karatsuba is faster. 1280 110 digits Karatsuba and Comba multiplications just about break even and for 110+ digits Karatsuba is faster.
1241 1281
1242 Toom-Cook has incredible overhead and is probably only useful for very large inputs. So far no known cutoff points 1282 Toom-Cook has incredible overhead and is probably only useful for very large inputs. So far no known cutoff points
1243 exist and for the most part I just set the cutoff points very high to make sure they're not called. 1283 exist and for the most part I just set the cutoff points very high to make sure they're not called.
1244 1284
1245 A demo program in the ``etc/'' directory of the project called ``tune.c'' can be used to find the cutoff points. This 1285 A demo program in the ``etc/'' directory of the project called ``tune.c'' can be used to find the cutoff points. This
1246 can be built with GCC as follows 1286 can be built with GCC as follows
1247 1287
1271 good Karatsuba squaring and multiplication points. Then it proceeds to find Toom-Cook points. Note that the Toom-Cook 1311 good Karatsuba squaring and multiplication points. Then it proceeds to find Toom-Cook points. Note that the Toom-Cook
1272 tuning takes a very long time as the cutoff points are likely to be very high. 1312 tuning takes a very long time as the cutoff points are likely to be very high.
1273 1313
1274 \chapter{Modular Reduction} 1314 \chapter{Modular Reduction}
1275 1315
1276 Modular reduction is process of taking the remainder of one quantity divided by another. Expressed 1316 Modular reduction is process of taking the remainder of one quantity divided by another. Expressed
1277 as (\ref{eqn:mod}) the modular reduction is equivalent to the remainder of $b$ divided by $c$. 1317 as (\ref{eqn:mod}) the modular reduction is equivalent to the remainder of $b$ divided by $c$.
1278 1318
1279 \begin{equation} 1319 \begin{equation}
1280 a \equiv b \mbox{ (mod }c\mbox{)} 1320 a \equiv b \mbox{ (mod }c\mbox{)}
1281 \label{eqn:mod} 1321 \label{eqn:mod}
1282 \end{equation} 1322 \end{equation}
1283 1323
1284 Of particular interest to cryptography are reductions where $b$ is limited to the range $0 \le b < c^2$ since particularly 1324 Of particular interest to cryptography are reductions where $b$ is limited to the range $0 \le b < c^2$ since particularly
1285 fast reduction algorithms can be written for the limited range. 1325 fast reduction algorithms can be written for the limited range.
1286 1326
1287 Note that one of the four optimized reduction algorithms are automatically chosen in the modular exponentiation 1327 Note that one of the four optimized reduction algorithms are automatically chosen in the modular exponentiation
1288 algorithm mp\_exptmod when an appropriate modulus is detected. 1328 algorithm mp\_exptmod when an appropriate modulus is detected.
1289 1329
1290 \section{Straight Division} 1330 \section{Straight Division}
1291 In order to effect an arbitrary modular reduction the following algorithm is provided. 1331 In order to effect an arbitrary modular reduction the following algorithm is provided.
1292 1332
1293 \index{mp\_mod} 1333 \index{mp\_mod}
1294 \begin{alltt} 1334 \begin{alltt}
1295 int mp_mod(mp_int *a, mp_int *b, mp_int *c); 1335 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
1296 \end{alltt} 1336 \end{alltt}
1297 1337
1298 This reduces $a$ modulo $b$ and stores the result in $c$. The sign of $c$ shall agree with the sign 1338 This reduces $a$ modulo $b$ and stores the result in $c$. The sign of $c$ shall agree with the sign
1299 of $b$. This algorithm accepts an input $a$ of any range and is not limited by $0 \le a < b^2$. 1339 of $b$. This algorithm accepts an input $a$ of any range and is not limited by $0 \le a < b^2$.
1300 1340
1301 \section{Barrett Reduction} 1341 \section{Barrett Reduction}
1302 1342
1303 Barrett reduction is a generic optimized reduction algorithm that requires pre--computation to achieve 1343 Barrett reduction is a generic optimized reduction algorithm that requires pre--computation to achieve
1323 int main(void) 1363 int main(void)
1324 \{ 1364 \{
1325 mp_int a, b, c, mu; 1365 mp_int a, b, c, mu;
1326 int result; 1366 int result;
1327 1367
1328 /* initialize a,b to desired values, mp_init mu, 1368 /* initialize a,b to desired values, mp_init mu,
1329 * c and set c to 1...we want to compute a^3 mod b 1369 * c and set c to 1...we want to compute a^3 mod b
1330 */ 1370 */
1331 1371
1332 /* get mu value */ 1372 /* get mu value */
1333 if ((result = mp_reduce_setup(&mu, b)) != MP_OKAY) \{ 1373 if ((result = mp_reduce_setup(&mu, b)) != MP_OKAY) \{
1334 printf("Error getting mu. \%s", 1374 printf("Error getting mu. \%s",
1335 mp_error_to_string(result)); 1375 mp_error_to_string(result));
1336 return EXIT_FAILURE; 1376 return EXIT_FAILURE;
1337 \} 1377 \}
1338 1378
1339 /* square a to get c = a^2 */ 1379 /* square a to get c = a^2 */
1340 if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{ 1380 if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{
1341 printf("Error squaring. \%s", 1381 printf("Error squaring. \%s",
1342 mp_error_to_string(result)); 1382 mp_error_to_string(result));
1343 return EXIT_FAILURE; 1383 return EXIT_FAILURE;
1344 \} 1384 \}
1345 1385
1346 /* now reduce `c' modulo b */ 1386 /* now reduce `c' modulo b */
1347 if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{ 1387 if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{
1348 printf("Error reducing. \%s", 1388 printf("Error reducing. \%s",
1349 mp_error_to_string(result)); 1389 mp_error_to_string(result));
1350 return EXIT_FAILURE; 1390 return EXIT_FAILURE;
1351 \} 1391 \}
1352 1392
1353 /* multiply a to get c = a^3 */ 1393 /* multiply a to get c = a^3 */
1354 if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{ 1394 if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{
1355 printf("Error reducing. \%s", 1395 printf("Error reducing. \%s",
1356 mp_error_to_string(result)); 1396 mp_error_to_string(result));
1357 return EXIT_FAILURE; 1397 return EXIT_FAILURE;
1358 \} 1398 \}
1359 1399
1360 /* now reduce `c' modulo b */ 1400 /* now reduce `c' modulo b */
1361 if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{ 1401 if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{
1362 printf("Error reducing. \%s", 1402 printf("Error reducing. \%s",
1363 mp_error_to_string(result)); 1403 mp_error_to_string(result));
1364 return EXIT_FAILURE; 1404 return EXIT_FAILURE;
1365 \} 1405 \}
1366 1406
1367 /* c now equals a^3 mod b */ 1407 /* c now equals a^3 mod b */
1368 1408
1369 return EXIT_SUCCESS; 1409 return EXIT_SUCCESS;
1370 \} 1410 \}
1371 \end{alltt} 1411 \end{alltt}
1372 1412
1373 This program will calculate $a^3 \mbox{ mod }b$ if all the functions succeed. 1413 This program will calculate $a^3 \mbox{ mod }b$ if all the functions succeed.
1374 1414
1375 \section{Montgomery Reduction} 1415 \section{Montgomery Reduction}
1376 1416
1377 Montgomery is a specialized reduction algorithm for any odd moduli. Like Barrett reduction a pre--computation 1417 Montgomery is a specialized reduction algorithm for any odd moduli. Like Barrett reduction a pre--computation
1378 step is required. This is accomplished with the following. 1418 step is required. This is accomplished with the following.
1380 \index{mp\_montgomery\_setup} 1420 \index{mp\_montgomery\_setup}
1381 \begin{alltt} 1421 \begin{alltt}
1382 int mp_montgomery_setup(mp_int *a, mp_digit *mp); 1422 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
1383 \end{alltt} 1423 \end{alltt}
1384 1424
1385 For the given odd moduli $a$ the precomputation value is placed in $mp$. The reduction is computed with the 1425 For the given odd moduli $a$ the precomputation value is placed in $mp$. The reduction is computed with the
1386 following. 1426 following.
1387 1427
1388 \index{mp\_montgomery\_reduce} 1428 \index{mp\_montgomery\_reduce}
1389 \begin{alltt} 1429 \begin{alltt}
1390 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); 1430 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
1392 This reduces $a$ in place modulo $m$ with the pre--computed value $mp$. $a$ must be in the range 1432 This reduces $a$ in place modulo $m$ with the pre--computed value $mp$. $a$ must be in the range
1393 $0 \le a < b^2$. 1433 $0 \le a < b^2$.
1394 1434
1395 Montgomery reduction is faster than Barrett reduction for moduli smaller than the ``comba'' limit. With the default 1435 Montgomery reduction is faster than Barrett reduction for moduli smaller than the ``comba'' limit. With the default
1396 setup for instance, the limit is $127$ digits ($3556$--bits). Note that this function is not limited to 1436 setup for instance, the limit is $127$ digits ($3556$--bits). Note that this function is not limited to
1397 $127$ digits just that it falls back to a baseline algorithm after that point. 1437 $127$ digits just that it falls back to a baseline algorithm after that point.
1398 1438
1399 An important observation is that this reduction does not return $a \mbox{ mod }m$ but $aR^{-1} \mbox{ mod }m$ 1439 An important observation is that this reduction does not return $a \mbox{ mod }m$ but $aR^{-1} \mbox{ mod }m$
1400 where $R = \beta^n$, $n$ is the n number of digits in $m$ and $\beta$ is radix used (default is $2^{28}$). 1440 where $R = \beta^n$, $n$ is the n number of digits in $m$ and $\beta$ is radix used (default is $2^{28}$).
1401 1441
1402 To quickly calculate $R$ the following function was provided. 1442 To quickly calculate $R$ the following function was provided.
1403 1443
1404 \index{mp\_montgomery\_calc\_normalization} 1444 \index{mp\_montgomery\_calc\_normalization}
1405 \begin{alltt} 1445 \begin{alltt}
1406 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); 1446 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
1407 \end{alltt} 1447 \end{alltt}
1408 Which calculates $a = R$ for the odd moduli $b$ without using multiplication or division. 1448 Which calculates $a = R$ for the odd moduli $b$ without using multiplication or division.
1409 1449
1410 The normal modus operandi for Montgomery reductions is to normalize the integers before entering the system. For 1450 The normal modus operandi for Montgomery reductions is to normalize the integers before entering the system. For
1411 example, to calculate $a^3 \mbox { mod }b$ using Montgomery reduction the value of $a$ can be normalized by 1451 example, to calculate $a^3 \mbox { mod }b$ using Montgomery reduction the value of $a$ can be normalized by
1412 multiplying it by $R$. Consider the following code snippet. 1452 multiplying it by $R$. Consider the following code snippet.
1413 1453
1416 \{ 1456 \{
1417 mp_int a, b, c, R; 1457 mp_int a, b, c, R;
1418 mp_digit mp; 1458 mp_digit mp;
1419 int result; 1459 int result;
1420 1460
1421 /* initialize a,b to desired values, 1461 /* initialize a,b to desired values,
1422 * mp_init R, c and set c to 1.... 1462 * mp_init R, c and set c to 1....
1423 */ 1463 */
1424 1464
1425 /* get normalization */ 1465 /* get normalization */
1426 if ((result = mp_montgomery_calc_normalization(&R, b)) != MP_OKAY) \{ 1466 if ((result = mp_montgomery_calc_normalization(&R, b)) != MP_OKAY) \{
1427 printf("Error getting norm. \%s", 1467 printf("Error getting norm. \%s",
1428 mp_error_to_string(result)); 1468 mp_error_to_string(result));
1429 return EXIT_FAILURE; 1469 return EXIT_FAILURE;
1430 \} 1470 \}
1431 1471
1432 /* get mp value */ 1472 /* get mp value */
1433 if ((result = mp_montgomery_setup(&c, &mp)) != MP_OKAY) \{ 1473 if ((result = mp_montgomery_setup(&c, &mp)) != MP_OKAY) \{
1434 printf("Error setting up montgomery. \%s", 1474 printf("Error setting up montgomery. \%s",
1435 mp_error_to_string(result)); 1475 mp_error_to_string(result));
1436 return EXIT_FAILURE; 1476 return EXIT_FAILURE;
1437 \} 1477 \}
1438 1478
1439 /* normalize `a' so now a is equal to aR */ 1479 /* normalize `a' so now a is equal to aR */
1440 if ((result = mp_mulmod(&a, &R, &b, &a)) != MP_OKAY) \{ 1480 if ((result = mp_mulmod(&a, &R, &b, &a)) != MP_OKAY) \{
1441 printf("Error computing aR. \%s", 1481 printf("Error computing aR. \%s",
1442 mp_error_to_string(result)); 1482 mp_error_to_string(result));
1443 return EXIT_FAILURE; 1483 return EXIT_FAILURE;
1444 \} 1484 \}
1445 1485
1446 /* square a to get c = a^2R^2 */ 1486 /* square a to get c = a^2R^2 */
1447 if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{ 1487 if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{
1448 printf("Error squaring. \%s", 1488 printf("Error squaring. \%s",
1449 mp_error_to_string(result)); 1489 mp_error_to_string(result));
1450 return EXIT_FAILURE; 1490 return EXIT_FAILURE;
1451 \} 1491 \}
1452 1492
1453 /* now reduce `c' back down to c = a^2R^2 * R^-1 == a^2R */ 1493 /* now reduce `c' back down to c = a^2R^2 * R^-1 == a^2R */
1454 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{ 1494 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
1455 printf("Error reducing. \%s", 1495 printf("Error reducing. \%s",
1456 mp_error_to_string(result)); 1496 mp_error_to_string(result));
1457 return EXIT_FAILURE; 1497 return EXIT_FAILURE;
1458 \} 1498 \}
1459 1499
1460 /* multiply a to get c = a^3R^2 */ 1500 /* multiply a to get c = a^3R^2 */
1461 if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{ 1501 if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{
1462 printf("Error reducing. \%s", 1502 printf("Error reducing. \%s",
1463 mp_error_to_string(result)); 1503 mp_error_to_string(result));
1464 return EXIT_FAILURE; 1504 return EXIT_FAILURE;
1465 \} 1505 \}
1466 1506
1467 /* now reduce `c' back down to c = a^3R^2 * R^-1 == a^3R */ 1507 /* now reduce `c' back down to c = a^3R^2 * R^-1 == a^3R */
1468 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{ 1508 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
1469 printf("Error reducing. \%s", 1509 printf("Error reducing. \%s",
1470 mp_error_to_string(result)); 1510 mp_error_to_string(result));
1471 return EXIT_FAILURE; 1511 return EXIT_FAILURE;
1472 \} 1512 \}
1473 1513
1474 /* now reduce (again) `c' back down to c = a^3R * R^-1 == a^3 */ 1514 /* now reduce (again) `c' back down to c = a^3R * R^-1 == a^3 */
1475 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{ 1515 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
1476 printf("Error reducing. \%s", 1516 printf("Error reducing. \%s",
1477 mp_error_to_string(result)); 1517 mp_error_to_string(result));
1478 return EXIT_FAILURE; 1518 return EXIT_FAILURE;
1479 \} 1519 \}
1480 1520
1481 /* c now equals a^3 mod b */ 1521 /* c now equals a^3 mod b */
1482 1522
1483 return EXIT_SUCCESS; 1523 return EXIT_SUCCESS;
1484 \} 1524 \}
1485 \end{alltt} 1525 \end{alltt}
1486 1526
1487 This particular example does not look too efficient but it demonstrates the point of the algorithm. By 1527 This particular example does not look too efficient but it demonstrates the point of the algorithm. By
1488 normalizing the inputs the reduced results are always of the form $aR$ for some variable $a$. This allows 1528 normalizing the inputs the reduced results are always of the form $aR$ for some variable $a$. This allows
1489 a single final reduction to correct for the normalization and the fast reduction used within the algorithm. 1529 a single final reduction to correct for the normalization and the fast reduction used within the algorithm.
1490 1530
1491 For more details consider examining the file \textit{bn\_mp\_exptmod\_fast.c}. 1531 For more details consider examining the file \textit{bn\_mp\_exptmod\_fast.c}.
1492 1532
1493 \section{Restricted Dimminished Radix} 1533 \section{Restricted Dimminished Radix}
1494 1534
1495 ``Dimminished Radix'' reduction refers to reduction with respect to moduli that are ameniable to simple 1535 ``Dimminished Radix'' reduction refers to reduction with respect to moduli that are ameniable to simple
1496 digit shifting and small multiplications. In this case the ``restricted'' variant refers to moduli of the 1536 digit shifting and small multiplications. In this case the ``restricted'' variant refers to moduli of the
1497 form $\beta^k - p$ for some $k \ge 0$ and $0 < p < \beta$ where $\beta$ is the radix (default to $2^{28}$). 1537 form $\beta^k - p$ for some $k \ge 0$ and $0 < p < \beta$ where $\beta$ is the radix (default to $2^{28}$).
1498 1538
1499 As in the case of Montgomery reduction there is a pre--computation phase required for a given modulus. 1539 As in the case of Montgomery reduction there is a pre--computation phase required for a given modulus.
1500 1540
1501 \index{mp\_dr\_setup} 1541 \index{mp\_dr\_setup}
1502 \begin{alltt} 1542 \begin{alltt}
1511 \begin{alltt} 1551 \begin{alltt}
1512 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp); 1552 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
1513 \end{alltt} 1553 \end{alltt}
1514 1554
1515 This reduces $a$ in place modulo $b$ with the pre--computed value $mp$. $b$ must be of a restricted 1555 This reduces $a$ in place modulo $b$ with the pre--computed value $mp$. $b$ must be of a restricted
1516 dimminished radix form and $a$ must be in the range $0 \le a < b^2$. Dimminished radix reductions are 1556 dimminished radix form and $a$ must be in the range $0 \le a < b^2$. Dimminished radix reductions are
1517 much faster than both Barrett and Montgomery reductions as they have a much lower asymtotic running time. 1557 much faster than both Barrett and Montgomery reductions as they have a much lower asymtotic running time.
1518 1558
1519 Since the moduli are restricted this algorithm is not particularly useful for something like Rabin, RSA or 1559 Since the moduli are restricted this algorithm is not particularly useful for something like Rabin, RSA or
1520 BBS cryptographic purposes. This reduction algorithm is useful for Diffie-Hellman and ECC where fixed 1560 BBS cryptographic purposes. This reduction algorithm is useful for Diffie-Hellman and ECC where fixed
1521 primes are acceptable. 1561 primes are acceptable.
1522 1562
1523 Note that unlike Montgomery reduction there is no normalization process. The result of this function is 1563 Note that unlike Montgomery reduction there is no normalization process. The result of this function is
1524 equal to the correct residue. 1564 equal to the correct residue.
1525 1565
1526 \section{Unrestricted Dimminshed Radix} 1566 \section{Unrestricted Dimminshed Radix}
1527 1567
1528 Unrestricted reductions work much like the restricted counterparts except in this case the moduli is of the 1568 Unrestricted reductions work much like the restricted counterparts except in this case the moduli is of the
1529 form $2^k - p$ for $0 < p < \beta$. In this sense the unrestricted reductions are more flexible as they 1569 form $2^k - p$ for $0 < p < \beta$. In this sense the unrestricted reductions are more flexible as they
1530 can be applied to a wider range of numbers. 1570 can be applied to a wider range of numbers.
1531 1571
1532 \index{mp\_reduce\_2k\_setup} 1572 \index{mp\_reduce\_2k\_setup}
1533 \begin{alltt} 1573 \begin{alltt}
1534 int mp_reduce_2k_setup(mp_int *a, mp_digit *d); 1574 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
1535 \end{alltt} 1575 \end{alltt}
1536 1576
1537 This will compute the required $d$ value for the given moduli $a$. 1577 This will compute the required $d$ value for the given moduli $a$.
1538 1578
1539 \index{mp\_reduce\_2k} 1579 \index{mp\_reduce\_2k}
1540 \begin{alltt} 1580 \begin{alltt}
1541 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); 1581 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
1542 \end{alltt} 1582 \end{alltt}
1543 1583
1544 This will reduce $a$ in place modulo $n$ with the pre--computed value $d$. From my experience this routine is 1584 This will reduce $a$ in place modulo $n$ with the pre--computed value $d$. From my experience this routine is
1545 slower than mp\_dr\_reduce but faster for most moduli sizes than the Montgomery reduction. 1585 slower than mp\_dr\_reduce but faster for most moduli sizes than the Montgomery reduction.
1546 1586
1547 \chapter{Exponentiation} 1587 \chapter{Exponentiation}
1548 \section{Single Digit Exponentiation} 1588 \section{Single Digit Exponentiation}
1589 \index{mp\_expt\_d\_ex}
1590 \begin{alltt}
1591 int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
1592 \end{alltt}
1593 This function computes $c = a^b$.
1594
1595 With parameter \textit{fast} set to $0$ the old version of the algorithm is used,
1596 when \textit{fast} is $1$, a faster but not statically timed version of the algorithm is used.
1597
1598 The old version uses a simple binary left-to-right algorithm.
1599 It is faster than repeated multiplications by $a$ for all values of $b$ greater than three.
1600
1601 The new version uses a binary right-to-left algorithm.
1602
1603 The difference between the old and the new version is that the old version always
1604 executes $DIGIT\_BIT$ iterations. The new algorithm executes only $n$ iterations
1605 where $n$ is equal to the position of the highest bit that is set in $b$.
1606
1549 \index{mp\_expt\_d} 1607 \index{mp\_expt\_d}
1550 \begin{alltt} 1608 \begin{alltt}
1551 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c) 1609 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
1552 \end{alltt} 1610 \end{alltt}
1553 This computes $c = a^b$ using a simple binary left-to-right algorithm. It is faster than repeated multiplications by 1611 mp\_expt\_d(a, b, c) is a wrapper function to mp\_expt\_d\_ex(a, b, c, 0).
1554 $a$ for all values of $b$ greater than three.
1555 1612
1556 \section{Modular Exponentiation} 1613 \section{Modular Exponentiation}
1557 \index{mp\_exptmod} 1614 \index{mp\_exptmod}
1558 \begin{alltt} 1615 \begin{alltt}
1559 int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) 1616 int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
1560 \end{alltt} 1617 \end{alltt}
1561 This computes $Y \equiv G^X \mbox{ (mod }P\mbox{)}$ using a variable width sliding window algorithm. This function 1618 This computes $Y \equiv G^X \mbox{ (mod }P\mbox{)}$ using a variable width sliding window algorithm. This function
1562 will automatically detect the fastest modular reduction technique to use during the operation. For negative values of 1619 will automatically detect the fastest modular reduction technique to use during the operation. For negative values of
1563 $X$ the operation is performed as $Y \equiv (G^{-1} \mbox{ mod }P)^{\vert X \vert} \mbox{ (mod }P\mbox{)}$ provided that 1620 $X$ the operation is performed as $Y \equiv (G^{-1} \mbox{ mod }P)^{\vert X \vert} \mbox{ (mod }P\mbox{)}$ provided that
1564 $gcd(G, P) = 1$. 1621 $gcd(G, P) = 1$.
1565 1622
1566 This function is actually a shell around the two internal exponentiation functions. This routine will automatically 1623 This function is actually a shell around the two internal exponentiation functions. This routine will automatically
1567 detect when Barrett, Montgomery, Restricted and Unrestricted Dimminished Radix based exponentiation can be used. Generally 1624 detect when Barrett, Montgomery, Restricted and Unrestricted Dimminished Radix based exponentiation can be used. Generally
1568 moduli of the a ``restricted dimminished radix'' form lead to the fastest modular exponentiations. Followed by Montgomery 1625 moduli of the a ``restricted dimminished radix'' form lead to the fastest modular exponentiations. Followed by Montgomery
1571 \section{Root Finding} 1628 \section{Root Finding}
1572 \index{mp\_n\_root} 1629 \index{mp\_n\_root}
1573 \begin{alltt} 1630 \begin{alltt}
1574 int mp_n_root (mp_int * a, mp_digit b, mp_int * c) 1631 int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
1575 \end{alltt} 1632 \end{alltt}
1576 This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. The implementation of this function is not 1633 This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. The implementation of this function is not
1577 ideal for values of $b$ greater than three. It will work but become very slow. So unless you are working with very small 1634 ideal for values of $b$ greater than three. It will work but become very slow. So unless you are working with very small
1578 numbers (less than 1000 bits) I'd avoid $b > 3$ situations. Will return a positive root only for even roots and return 1635 numbers (less than 1000 bits) I'd avoid $b > 3$ situations. Will return a positive root only for even roots and return
1579 a root with the sign of the input for odd roots. For example, performing $4^{1/2}$ will return $2$ whereas $(-8)^{1/3}$ 1636 a root with the sign of the input for odd roots. For example, performing $4^{1/2}$ will return $2$ whereas $(-8)^{1/3}$
1580 will return $-2$. 1637 will return $-2$.
1581 1638
1582 This algorithm uses the ``Newton Approximation'' method and will converge on the correct root fairly quickly. Since 1639 This algorithm uses the ``Newton Approximation'' method and will converge on the correct root fairly quickly. Since
1583 the algorithm requires raising $a$ to the power of $b$ it is not ideal to attempt to find roots for large 1640 the algorithm requires raising $a$ to the power of $b$ it is not ideal to attempt to find roots for large
1584 values of $b$. If particularly large roots are required then a factor method could be used instead. For example, 1641 values of $b$. If particularly large roots are required then a factor method could be used instead. For example,
1585 $a^{1/16}$ is equivalent to $\left (a^{1/4} \right)^{1/4}$ or simply 1642 $a^{1/16}$ is equivalent to $\left (a^{1/4} \right)^{1/4}$ or simply
1586 $\left ( \left ( \left ( a^{1/2} \right )^{1/2} \right )^{1/2} \right )^{1/2}$ 1643 $\left ( \left ( \left ( a^{1/2} \right )^{1/2} \right )^{1/2} \right )^{1/2}$
1587 1644
1588 \chapter{Prime Numbers} 1645 \chapter{Prime Numbers}
1589 \section{Trial Division} 1646 \section{Trial Division}
1590 \index{mp\_prime\_is\_divisible} 1647 \index{mp\_prime\_is\_divisible}
1591 \begin{alltt} 1648 \begin{alltt}
1592 int mp_prime_is_divisible (mp_int * a, int *result) 1649 int mp_prime_is_divisible (mp_int * a, int *result)
1593 \end{alltt} 1650 \end{alltt}
1594 This will attempt to evenly divide $a$ by a list of primes\footnote{Default is the first 256 primes.} and store the 1651 This will attempt to evenly divide $a$ by a list of primes\footnote{Default is the first 256 primes.} and store the
1595 outcome in ``result''. That is if $result = 0$ then $a$ is not divisible by the primes, otherwise it is. Note that 1652 outcome in ``result''. That is if $result = 0$ then $a$ is not divisible by the primes, otherwise it is. Note that
1596 if the function does not return \textbf{MP\_OKAY} the value in ``result'' should be considered undefined\footnote{Currently 1653 if the function does not return \textbf{MP\_OKAY} the value in ``result'' should be considered undefined\footnote{Currently
1597 the default is to set it to zero first.}. 1654 the default is to set it to zero first.}.
1598 1655
1599 \section{Fermat Test} 1656 \section{Fermat Test}
1600 \index{mp\_prime\_fermat} 1657 \index{mp\_prime\_fermat}
1609 \index{mp\_prime\_miller\_rabin} 1666 \index{mp\_prime\_miller\_rabin}
1610 \begin{alltt} 1667 \begin{alltt}
1611 int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) 1668 int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
1612 \end{alltt} 1669 \end{alltt}
1613 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 1670 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
1614 fool (besides with Carmichael numbers). If $a$ passes the test (therefore is probably prime) $result$ is set to one. 1671 fool (besides with Carmichael numbers). If $a$ passes the test (therefore is probably prime) $result$ is set to one.
1615 Otherwise $result$ is set to zero. 1672 Otherwise $result$ is set to zero.
1616 1673
1617 Note that is suggested that you use the Miller-Rabin test instead of the Fermat test since all of the failures of 1674 Note that is suggested that you use the Miller-Rabin test instead of the Fermat test since all of the failures of
1618 Miller-Rabin are a subset of the failures of the Fermat test. 1675 Miller-Rabin are a subset of the failures of the Fermat test.
1619 1676
1620 \subsection{Required Number of Tests} 1677 \subsection{Required Number of Tests}
1621 Generally to ensure a number is very likely to be prime you have to perform the Miller-Rabin with at least a half-dozen 1678 Generally to ensure a number is very likely to be prime you have to perform the Miller-Rabin with at least a half-dozen
1622 or so unique bases. However, it has been proven that the probability of failure goes down as the size of the input goes up. 1679 or so unique bases. However, it has been proven that the probability of failure goes down as the size of the input goes up.
1626 \begin{alltt} 1683 \begin{alltt}
1627 int mp_prime_rabin_miller_trials(int size) 1684 int mp_prime_rabin_miller_trials(int size)
1628 \end{alltt} 1685 \end{alltt}
1629 This returns the number of trials required for a $2^{-96}$ (or lower) probability of failure for a given ``size'' expressed 1686 This returns the number of trials required for a $2^{-96}$ (or lower) probability of failure for a given ``size'' expressed
1630 in bits. This comes in handy specially since larger numbers are slower to test. For example, a 512-bit number would 1687 in bits. This comes in handy specially since larger numbers are slower to test. For example, a 512-bit number would
1631 require ten tests whereas a 1024-bit number would only require four tests. 1688 require ten tests whereas a 1024-bit number would only require four tests.
1632 1689
1633 You should always still perform a trial division before a Miller-Rabin test though. 1690 You should always still perform a trial division before a Miller-Rabin test though.
1634 1691
1635 \section{Primality Testing} 1692 \section{Primality Testing}
1636 \index{mp\_prime\_is\_prime} 1693 \index{mp\_prime\_is\_prime}
1637 \begin{alltt} 1694 \begin{alltt}
1638 int mp_prime_is_prime (mp_int * a, int t, int *result) 1695 int mp_prime_is_prime (mp_int * a, int t, int *result)
1639 \end{alltt} 1696 \end{alltt}
1640 This will perform a trial division followed by $t$ rounds of Miller-Rabin tests on $a$ and store the result in $result$. 1697 This will perform a trial division followed by $t$ rounds of Miller-Rabin tests on $a$ and store the result in $result$.
1641 If $a$ passes all of the tests $result$ is set to one, otherwise it is set to zero. Note that $t$ is bounded by 1698 If $a$ passes all of the tests $result$ is set to one, otherwise it is set to zero. Note that $t$ is bounded by
1642 $1 \le t < PRIME\_SIZE$ where $PRIME\_SIZE$ is the number of primes in the prime number table (by default this is $256$). 1699 $1 \le t < PRIME\_SIZE$ where $PRIME\_SIZE$ is the number of primes in the prime number table (by default this is $256$).
1643 1700
1644 \section{Next Prime} 1701 \section{Next Prime}
1645 \index{mp\_prime\_next\_prime} 1702 \index{mp\_prime\_next\_prime}
1646 \begin{alltt} 1703 \begin{alltt}
1647 int mp_prime_next_prime(mp_int *a, int t, int bbs_style) 1704 int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
1648 \end{alltt} 1705 \end{alltt}
1649 This finds the next prime after $a$ that passes mp\_prime\_is\_prime() with $t$ tests. Set $bbs\_style$ to one if you 1706 This finds the next prime after $a$ that passes mp\_prime\_is\_prime() with $t$ tests. Set $bbs\_style$ to one if you
1650 want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to zero to find any next prime. 1707 want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to zero to find any next prime.
1651 1708
1652 \section{Random Primes} 1709 \section{Random Primes}
1653 \index{mp\_prime\_random} 1710 \index{mp\_prime\_random}
1654 \begin{alltt} 1711 \begin{alltt}
1655 int mp_prime_random(mp_int *a, int t, int size, int bbs, 1712 int mp_prime_random(mp_int *a, int t, int size, int bbs,
1656 ltm_prime_callback cb, void *dat) 1713 ltm_prime_callback cb, void *dat)
1657 \end{alltt} 1714 \end{alltt}
1658 This will find a prime greater than $256^{size}$ which can be ``bbs\_style'' or not depending on $bbs$ and must pass 1715 This will find a prime greater than $256^{size}$ which can be ``bbs\_style'' or not depending on $bbs$ and must pass
1659 $t$ rounds of tests. The ``ltm\_prime\_callback'' is a typedef for 1716 $t$ rounds of tests. The ``ltm\_prime\_callback'' is a typedef for
1660 1717
1661 \begin{alltt} 1718 \begin{alltt}
1662 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); 1719 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
1663 \end{alltt} 1720 \end{alltt}
1664 1721
1665 Which is a function that must read $len$ bytes (and return the amount stored) into $dst$. The $dat$ variable is simply 1722 Which is a function that must read $len$ bytes (and return the amount stored) into $dst$. The $dat$ variable is simply
1666 copied from the original input. It can be used to pass RNG context data to the callback. The function 1723 copied from the original input. It can be used to pass RNG context data to the callback. The function
1667 mp\_prime\_random() is more suitable for generating primes which must be secret (as in the case of RSA) since there 1724 mp\_prime\_random() is more suitable for generating primes which must be secret (as in the case of RSA) since there
1668 is no skew on the least significant bits. 1725 is no skew on the least significant bits.
1669 1726
1670 \textit{Note:} As of v0.30 of the LibTomMath library this function has been deprecated. It is still available 1727 \textit{Note:} As of v0.30 of the LibTomMath library this function has been deprecated. It is still available
1671 but users are encouraged to use the new mp\_prime\_random\_ex() function instead. 1728 but users are encouraged to use the new mp\_prime\_random\_ex() function instead.
1672 1729
1673 \subsection{Extended Generation} 1730 \subsection{Extended Generation}
1674 \index{mp\_prime\_random\_ex} 1731 \index{mp\_prime\_random\_ex}
1675 \begin{alltt} 1732 \begin{alltt}
1676 int mp_prime_random_ex(mp_int *a, int t, 1733 int mp_prime_random_ex(mp_int *a, int t,
1677 int size, int flags, 1734 int size, int flags,
1678 ltm_prime_callback cb, void *dat); 1735 ltm_prime_callback cb, void *dat);
1679 \end{alltt} 1736 \end{alltt}
1680 This will generate a prime in $a$ using $t$ tests of the primality testing algorithms. The variable $size$ 1737 This will generate a prime in $a$ using $t$ tests of the primality testing algorithms. The variable $size$
1681 specifies the bit length of the prime desired. The variable $flags$ specifies one of several options available 1738 specifies the bit length of the prime desired. The variable $flags$ specifies one of several options available
1682 (see fig. \ref{fig:primeopts}) which can be OR'ed together. The callback parameters are used as in 1739 (see fig. \ref{fig:primeopts}) which can be OR'ed together. The callback parameters are used as in
1683 mp\_prime\_random(). 1740 mp\_prime\_random().
1684 1741
1685 \begin{figure}[here] 1742 \begin{figure}[here]
1686 \begin{center} 1743 \begin{center}
1687 \begin{small} 1744 \begin{small}
1715 1772
1716 \index{mp\_radix\_size} 1773 \index{mp\_radix\_size}
1717 \begin{alltt} 1774 \begin{alltt}
1718 int mp_radix_size (mp_int * a, int radix, int *size) 1775 int mp_radix_size (mp_int * a, int radix, int *size)
1719 \end{alltt} 1776 \end{alltt}
1720 This stores in ``size'' the number of characters (including space for the NUL terminator) required. Upon error this 1777 This stores in ``size'' the number of characters (including space for the NUL terminator) required. Upon error this
1721 function returns an error code and ``size'' will be zero. 1778 function returns an error code and ``size'' will be zero.
1722 1779
1723 \subsection{From ASCII} 1780 \subsection{From ASCII}
1724 \index{mp\_read\_radix} 1781 \index{mp\_read\_radix}
1725 \begin{alltt} 1782 \begin{alltt}
1726 int mp_read_radix (mp_int * a, char *str, int radix); 1783 int mp_read_radix (mp_int * a, char *str, int radix);
1762 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c); 1819 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
1763 int mp_to_signed_bin(mp_int *a, unsigned char *b); 1820 int mp_to_signed_bin(mp_int *a, unsigned char *b);
1764 \end{alltt} 1821 \end{alltt}
1765 They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero 1822 They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero
1766 byte depending on the sign. If the sign is zpos (e.g. not negative) the prefix is zero, otherwise the prefix 1823 byte depending on the sign. If the sign is zpos (e.g. not negative) the prefix is zero, otherwise the prefix
1767 is non--zero. 1824 is non--zero.
1768 1825
1769 \chapter{Algebraic Functions} 1826 \chapter{Algebraic Functions}
1770 \section{Extended Euclidean Algorithm} 1827 \section{Extended Euclidean Algorithm}
1771 \index{mp\_exteuclid} 1828 \index{mp\_exteuclid}
1772 \begin{alltt} 1829 \begin{alltt}
1773 int mp_exteuclid(mp_int *a, mp_int *b, 1830 int mp_exteuclid(mp_int *a, mp_int *b,
1774 mp_int *U1, mp_int *U2, mp_int *U3); 1831 mp_int *U1, mp_int *U2, mp_int *U3);
1775 \end{alltt} 1832 \end{alltt}
1776 1833
1777 This finds the triple U1/U2/U3 using the Extended Euclidean algorithm such that the following equation holds. 1834 This finds the triple U1/U2/U3 using the Extended Euclidean algorithm such that the following equation holds.
1778 1835
1779 \begin{equation} 1836 \begin{equation}
1780 a \cdot U1 + b \cdot U2 = U3 1837 a \cdot U1 + b \cdot U2 = U3
1781 \end{equation} 1838 \end{equation}
1782 1839
1783 Any of the U1/U2/U3 paramters can be set to \textbf{NULL} if they are not desired. 1840 Any of the U1/U2/U3 paramters can be set to \textbf{NULL} if they are not desired.
1784 1841
1785 \section{Greatest Common Divisor} 1842 \section{Greatest Common Divisor}
1786 \index{mp\_gcd} 1843 \index{mp\_gcd}
1787 \begin{alltt} 1844 \begin{alltt}
1788 int mp_gcd (mp_int * a, mp_int * b, mp_int * c) 1845 int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
1802 int mp_jacobi (mp_int * a, mp_int * p, int *c) 1859 int mp_jacobi (mp_int * a, mp_int * p, int *c)
1803 \end{alltt} 1860 \end{alltt}
1804 This will compute the Jacobi symbol for $a$ with respect to $p$. If $p$ is prime this essentially computes the Legendre 1861 This will compute the Jacobi symbol for $a$ with respect to $p$. If $p$ is prime this essentially computes the Legendre
1805 symbol. The result is stored in $c$ and can take on one of three values $\lbrace -1, 0, 1 \rbrace$. If $p$ is prime 1862 symbol. The result is stored in $c$ and can take on one of three values $\lbrace -1, 0, 1 \rbrace$. If $p$ is prime
1806 then the result will be $-1$ when $a$ is not a quadratic residue modulo $p$. The result will be $0$ if $a$ divides $p$ 1863 then the result will be $-1$ when $a$ is not a quadratic residue modulo $p$. The result will be $0$ if $a$ divides $p$
1807 and the result will be $1$ if $a$ is a quadratic residue modulo $p$. 1864 and the result will be $1$ if $a$ is a quadratic residue modulo $p$.
1865
1866 \section{Modular square root}
1867 \index{mp\_sqrtmod\_prime}
1868 \begin{alltt}
1869 int mp_sqrtmod_prime(mp_int *n, mp_int *p, mp_int *r)
1870 \end{alltt}
1871
1872 This will solve the modular equatioon $r^2 = n \mod p$ where $p$ is a prime number greater than 2 (odd prime).
1873 The result is returned in the third argument $r$, the function returns \textbf{MP\_OKAY} on success,
1874 other return values indicate failure.
1875
1876 The implementation is split for two different cases:
1877
1878 1. if $p \mod 4 == 3$ we apply \href{http://cacr.uwaterloo.ca/hac/}{Handbook of Applied Cryptography algorithm 3.36} and compute $r$ directly as
1879 $r = n^{(p+1)/4} \mod p$
1880
1881 2. otherwise we use \href{https://en.wikipedia.org/wiki/Tonelli-Shanks_algorithm}{Tonelli-Shanks algorithm}
1882
1883 The function does not check the primality of parameter $p$ thus it is up to the caller to assure that this parameter
1884 is a prime number. When $p$ is a composite the function behaviour is undefined, it may even return a false-positive
1885 \textbf{MP\_OKAY}.
1808 1886
1809 \section{Modular Inverse} 1887 \section{Modular Inverse}
1810 \index{mp\_invmod} 1888 \index{mp\_invmod}
1811 \begin{alltt} 1889 \begin{alltt}
1812 int mp_invmod (mp_int * a, mp_int * b, mp_int * c) 1890 int mp_invmod (mp_int * a, mp_int * b, mp_int * c)