Mercurial > dropbear
comparison tommath.src @ 142:d29b64170cf0 libtommath-orig
import of libtommath 0.32
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 19 Dec 2004 11:33:56 +0000 |
parents | e1037a1e12e7 |
children | d8254fc979e9 |
comparison
equal
deleted
inserted
replaced
19:e1037a1e12e7 | 142:d29b64170cf0 |
---|---|
256 floating point is meant to be implemented in hardware the precision of the mantissa is often fairly small | 256 floating point is meant to be implemented in hardware the precision of the mantissa is often fairly small |
257 (\textit{23, 48 and 64 bits}). The mantissa is merely an integer and a multiple precision integer could be used to create | 257 (\textit{23, 48 and 64 bits}). The mantissa is merely an integer and a multiple precision integer could be used to create |
258 a mantissa of much larger precision than hardware alone can efficiently support. This approach could be useful where | 258 a mantissa of much larger precision than hardware alone can efficiently support. This approach could be useful where |
259 scientific applications must minimize the total output error over long calculations. | 259 scientific applications must minimize the total output error over long calculations. |
260 | 260 |
261 Another use for large integers is within arithmetic on polynomials of large characteristic (i.e. $GF(p)[x]$ for large $p$). | 261 Yet another use for large integers is within arithmetic on polynomials of large characteristic (i.e. $GF(p)[x]$ for large $p$). |
262 In fact the library discussed within this text has already been used to form a polynomial basis library\footnote{See \url{http://poly.libtomcrypt.org} for more details.}. | 262 In fact the library discussed within this text has already been used to form a polynomial basis library\footnote{See \url{http://poly.libtomcrypt.org} for more details.}. |
263 | 263 |
264 \subsection{Benefits of Multiple Precision Arithmetic} | 264 \subsection{Benefits of Multiple Precision Arithmetic} |
265 \index{precision} | 265 \index{precision} |
266 The benefit of multiple precision representations over single or fixed precision representations is that | 266 The benefit of multiple precision representations over single or fixed precision representations is that |
314 This text shall also serve as a walkthrough of the creation of multiple precision algorithms from scratch. Showing | 314 This text shall also serve as a walkthrough of the creation of multiple precision algorithms from scratch. Showing |
315 the reader how the algorithms fit together as well as where to start on various taskings. | 315 the reader how the algorithms fit together as well as where to start on various taskings. |
316 | 316 |
317 \section{Discussion and Notation} | 317 \section{Discussion and Notation} |
318 \subsection{Notation} | 318 \subsection{Notation} |
319 A multiple precision integer of $n$-digits shall be denoted as $x = (x_{n-1} ... x_1 x_0)_{ \beta }$ and represent | 319 A multiple precision integer of $n$-digits shall be denoted as $x = (x_{n-1}, \ldots, x_1, x_0)_{ \beta }$ and represent |
320 the integer $x \equiv \sum_{i=0}^{n-1} x_i\beta^i$. The elements of the array $x$ are said to be the radix $\beta$ digits | 320 the integer $x \equiv \sum_{i=0}^{n-1} x_i\beta^i$. The elements of the array $x$ are said to be the radix $\beta$ digits |
321 of the integer. For example, $x = (1,2,3)_{10}$ would represent the integer | 321 of the integer. For example, $x = (1,2,3)_{10}$ would represent the integer |
322 $1\cdot 10^2 + 2\cdot10^1 + 3\cdot10^0 = 123$. | 322 $1\cdot 10^2 + 2\cdot10^1 + 3\cdot10^0 = 123$. |
323 | 323 |
324 \index{mp\_int} | 324 \index{mp\_int} |
337 mp\_ints as inputs do not concern themselves with the housekeeping operations required such as memory management. These | 337 mp\_ints as inputs do not concern themselves with the housekeeping operations required such as memory management. These |
338 algorithms will be used to establish the relevant theory which will subsequently be used to describe a multiple | 338 algorithms will be used to establish the relevant theory which will subsequently be used to describe a multiple |
339 precision algorithm to solve the same problem. | 339 precision algorithm to solve the same problem. |
340 | 340 |
341 \subsection{Precision Notation} | 341 \subsection{Precision Notation} |
342 For the purposes of this text a single precision variable must be able to represent integers in the range | 342 The variable $\beta$ represents the radix of a single digit of a multiple precision integer and |
343 $0 \le x < q \beta$ while a double precision variable must be able to represent integers in the range | 343 must be of the form $q^p$ for $q, p \in \Z^+$. A single precision variable must be able to represent integers in |
344 $0 \le x < q \beta^2$. The variable $\beta$ represents the radix of a single digit of a multiple precision integer and | 344 the range $0 \le x < q \beta$ while a double precision variable must be able to represent integers in the range |
345 must be of the form $q^p$ for $q, p \in \Z^+$. The extra radix-$q$ factor allows additions and subtractions to proceed | 345 $0 \le x < q \beta^2$. The extra radix-$q$ factor allows additions and subtractions to proceed without truncation of the |
346 without truncation of the carry. Since all modern computers are binary, it is assumed that $q$ is two, for all intents | 346 carry. Since all modern computers are binary, it is assumed that $q$ is two. |
347 and purposes. | |
348 | 347 |
349 \index{mp\_digit} \index{mp\_word} | 348 \index{mp\_digit} \index{mp\_word} |
350 Within the source code that will be presented for each algorithm, the data type \textbf{mp\_digit} will represent | 349 Within the source code that will be presented for each algorithm, the data type \textbf{mp\_digit} will represent |
351 a single precision integer type, while, the data type \textbf{mp\_word} will represent a double precision integer type. In | 350 a single precision integer type, while, the data type \textbf{mp\_word} will represent a double precision integer type. In |
352 several algorithms (notably the Comba routines) temporary results will be stored in arrays of double precision mp\_words. | 351 several algorithms (notably the Comba routines) temporary results will be stored in arrays of double precision mp\_words. |
374 rounded to an integer not less than the expression itself. For example, $\lceil 5.1 \rceil = 6$. Typically when | 373 rounded to an integer not less than the expression itself. For example, $\lceil 5.1 \rceil = 6$. Typically when |
375 the $/$ division symbol is used the intention is to perform an integer division with truncation. For example, | 374 the $/$ division symbol is used the intention is to perform an integer division with truncation. For example, |
376 $5/2 = 2$ which will often be written as $\lfloor 5/2 \rfloor = 2$ for clarity. When an expression is written as a | 375 $5/2 = 2$ which will often be written as $\lfloor 5/2 \rfloor = 2$ for clarity. When an expression is written as a |
377 fraction a real value division is implied, for example ${5 \over 2} = 2.5$. | 376 fraction a real value division is implied, for example ${5 \over 2} = 2.5$. |
378 | 377 |
379 The norm of a multiple precision integer, for example, $\vert \vert x \vert \vert$ will be used to represent the number of digits in the representation | 378 The norm of a multiple precision integer, for example $\vert \vert x \vert \vert$, will be used to represent the number of digits in the representation |
380 of the integer. For example, $\vert \vert 123 \vert \vert = 3$ and $\vert \vert 79452 \vert \vert = 5$. | 379 of the integer. For example, $\vert \vert 123 \vert \vert = 3$ and $\vert \vert 79452 \vert \vert = 5$. |
381 | 380 |
382 \subsection{Work Effort} | 381 \subsection{Work Effort} |
383 \index{big-Oh} | 382 \index{big-Oh} |
384 To measure the efficiency of the specified algorithms, a modified big-Oh notation is used. In this system all | 383 To measure the efficiency of the specified algorithms, a modified big-Oh notation is used. In this system all |
567 before implementing a modular exponentiation algorithm one would implement a modular reduction algorithm. | 566 before implementing a modular exponentiation algorithm one would implement a modular reduction algorithm. |
568 By building outwards from a base foundation instead of using a parallel design methodology the resulting project is | 567 By building outwards from a base foundation instead of using a parallel design methodology the resulting project is |
569 highly modular. Being highly modular is a desirable property of any project as it often means the resulting product | 568 highly modular. Being highly modular is a desirable property of any project as it often means the resulting product |
570 has a small footprint and updates are easy to perform. | 569 has a small footprint and updates are easy to perform. |
571 | 570 |
572 Usually when I start a project I will begin with the header file. I define the data types I think I will need and | 571 Usually when I start a project I will begin with the header files. I define the data types I think I will need and |
573 prototype the initial functions that are not dependent on other functions (within the library). After I | 572 prototype the initial functions that are not dependent on other functions (within the library). After I |
574 implement these base functions I prototype more dependent functions and implement them. The process repeats until | 573 implement these base functions I prototype more dependent functions and implement them. The process repeats until |
575 I implement all of the functions I require. For example, in the case of LibTomMath I implemented functions such as | 574 I implement all of the functions I require. For example, in the case of LibTomMath I implemented functions such as |
576 mp\_init() well before I implemented mp\_mul() and even further before I implemented mp\_exptmod(). As an example as to | 575 mp\_init() well before I implemented mp\_mul() and even further before I implemented mp\_exptmod(). As an example as to |
577 why this design works note that the Karatsuba and Toom-Cook multipliers were written \textit{after} the | 576 why this design works note that the Karatsuba and Toom-Cook multipliers were written \textit{after} the |
617 The mp\_int structure is the ISO C based manifestation of what represents a multiple precision integer. The ISO C standard does not provide for | 616 The mp\_int structure is the ISO C based manifestation of what represents a multiple precision integer. The ISO C standard does not provide for |
618 any such data type but it does provide for making composite data types known as structures. The following is the structure definition | 617 any such data type but it does provide for making composite data types known as structures. The following is the structure definition |
619 used within LibTomMath. | 618 used within LibTomMath. |
620 | 619 |
621 \index{mp\_int} | 620 \index{mp\_int} |
622 \begin{verbatim} | 621 \begin{figure}[here] |
623 typedef struct { | 622 \begin{center} |
624 int used, alloc, sign; | 623 \begin{small} |
625 mp_digit *dp; | 624 %\begin{verbatim} |
626 } mp_int; | 625 \begin{tabular}{|l|} |
627 \end{verbatim} | 626 \hline |
628 | 627 typedef struct \{ \\ |
629 The mp\_int structure can be broken down as follows. | 628 \hspace{3mm}int used, alloc, sign;\\ |
629 \hspace{3mm}mp\_digit *dp;\\ | |
630 \} \textbf{mp\_int}; \\ | |
631 \hline | |
632 \end{tabular} | |
633 %\end{verbatim} | |
634 \end{small} | |
635 \caption{The mp\_int Structure} | |
636 \label{fig:mpint} | |
637 \end{center} | |
638 \end{figure} | |
639 | |
640 The mp\_int structure (fig. \ref{fig:mpint}) can be broken down as follows. | |
630 | 641 |
631 \begin{enumerate} | 642 \begin{enumerate} |
632 \item The \textbf{used} parameter denotes how many digits of the array \textbf{dp} contain the digits used to represent | 643 \item The \textbf{used} parameter denotes how many digits of the array \textbf{dp} contain the digits used to represent |
633 a given integer. The \textbf{used} count must be positive (or zero) and may not exceed the \textbf{alloc} count. | 644 a given integer. The \textbf{used} count must be positive (or zero) and may not exceed the \textbf{alloc} count. |
634 | 645 |
699 fault by dereferencing memory not owned by the application. | 710 fault by dereferencing memory not owned by the application. |
700 | 711 |
701 In the case of LibTomMath the only errors that are checked for are related to inappropriate inputs (division by zero for | 712 In the case of LibTomMath the only errors that are checked for are related to inappropriate inputs (division by zero for |
702 instance) and memory allocation errors. It will not check that the mp\_int passed to any function is valid nor | 713 instance) and memory allocation errors. It will not check that the mp\_int passed to any function is valid nor |
703 will it check pointers for validity. Any function that can cause a runtime error will return an error code as an | 714 will it check pointers for validity. Any function that can cause a runtime error will return an error code as an |
704 \textbf{int} data type with one of the following values. | 715 \textbf{int} data type with one of the following values (fig \ref{fig:errcodes}). |
705 | 716 |
706 \index{MP\_OKAY} \index{MP\_VAL} \index{MP\_MEM} | 717 \index{MP\_OKAY} \index{MP\_VAL} \index{MP\_MEM} |
718 \begin{figure}[here] | |
707 \begin{center} | 719 \begin{center} |
708 \begin{tabular}{|l|l|} | 720 \begin{tabular}{|l|l|} |
709 \hline \textbf{Value} & \textbf{Meaning} \\ | 721 \hline \textbf{Value} & \textbf{Meaning} \\ |
710 \hline \textbf{MP\_OKAY} & The function was successful \\ | 722 \hline \textbf{MP\_OKAY} & The function was successful \\ |
711 \hline \textbf{MP\_VAL} & One of the input value(s) was invalid \\ | 723 \hline \textbf{MP\_VAL} & One of the input value(s) was invalid \\ |
712 \hline \textbf{MP\_MEM} & The function ran out of heap memory \\ | 724 \hline \textbf{MP\_MEM} & The function ran out of heap memory \\ |
713 \hline | 725 \hline |
714 \end{tabular} | 726 \end{tabular} |
715 \end{center} | 727 \end{center} |
728 \caption{LibTomMath Error Codes} | |
729 \label{fig:errcodes} | |
730 \end{figure} | |
716 | 731 |
717 When an error is detected within a function it should free any memory it allocated, often during the initialization of | 732 When an error is detected within a function it should free any memory it allocated, often during the initialization of |
718 temporary mp\_ints, and return as soon as possible. The goal is to leave the system in the same state it was when the | 733 temporary mp\_ints, and return as soon as possible. The goal is to leave the system in the same state it was when the |
719 function was called. Error checking with this style of API is fairly simple. | 734 function was called. Error checking with this style of API is fairly simple. |
720 | 735 |
746 | 761 |
747 \subsection{Initializing an mp\_int} | 762 \subsection{Initializing an mp\_int} |
748 An mp\_int is said to be initialized if it is set to a valid, preferably default, state such that all of the members of the | 763 An mp\_int is said to be initialized if it is set to a valid, preferably default, state such that all of the members of the |
749 structure are set to valid values. The mp\_init algorithm will perform such an action. | 764 structure are set to valid values. The mp\_init algorithm will perform such an action. |
750 | 765 |
766 \index{mp\_init} | |
751 \begin{figure}[here] | 767 \begin{figure}[here] |
752 \begin{center} | 768 \begin{center} |
753 \begin{tabular}{l} | 769 \begin{tabular}{l} |
754 \hline Algorithm \textbf{mp\_init}. \\ | 770 \hline Algorithm \textbf{mp\_init}. \\ |
755 \textbf{Input}. An mp\_int $a$ \\ | 771 \textbf{Input}. An mp\_int $a$ \\ |
768 \end{center} | 784 \end{center} |
769 \caption{Algorithm mp\_init} | 785 \caption{Algorithm mp\_init} |
770 \end{figure} | 786 \end{figure} |
771 | 787 |
772 \textbf{Algorithm mp\_init.} | 788 \textbf{Algorithm mp\_init.} |
773 The \textbf{MP\_PREC} name represents a constant\footnote{Defined in the ``tommath.h'' header file within LibTomMath.} | 789 The purpose of this function is to initialize an mp\_int structure so that the rest of the library can properly |
774 used to dictate the minimum precision of allocated mp\_int integers. Ideally, it is at least equal to $32$ since for most | 790 manipulte it. It is assumed that the input may not have had any of its members previously initialized which is certainly |
775 purposes that will be more than enough. | 791 a valid assumption if the input resides on the stack. |
776 | 792 |
777 Memory for the default number of digits is allocated first. If the allocation fails the algorithm returns immediately | 793 Before any of the members such as \textbf{sign}, \textbf{used} or \textbf{alloc} are initialized the memory for |
778 with the \textbf{MP\_MEM} error code. If the allocation succeeds the remaining members of the mp\_int structure | 794 the digits is allocated. If this fails the function returns before setting any of the other members. The \textbf{MP\_PREC} |
779 must be initialized to reflect the default initial state. | 795 name represents a constant\footnote{Defined in the ``tommath.h'' header file within LibTomMath.} |
780 | 796 used to dictate the minimum precision of newly initialized mp\_int integers. Ideally, it is at least equal to the smallest |
781 The allocated digits are all set to zero (step three) to ensure they are in a known state. The \textbf{sign}, \textbf{used} | 797 precision number you'll be working with. |
782 and \textbf{alloc} are subsequently initialized to represent the zero integer. By step seven the algorithm returns a success | 798 |
783 code and the mp\_int $a$ has been successfully initialized to a valid state representing the integer zero. | 799 Allocating a block of digits at first instead of a single digit has the benefit of lowering the number of usually slow |
800 heap operations later functions will have to perform in the future. If \textbf{MP\_PREC} is set correctly the slack | |
801 memory and the number of heap operations will be trivial. | |
802 | |
803 Once the allocation has been made the digits have to be set to zero as well as the \textbf{used}, \textbf{sign} and | |
804 \textbf{alloc} members initialized. This ensures that the mp\_int will always represent the default state of zero regardless | |
805 of the original condition of the input. | |
784 | 806 |
785 \textbf{Remark.} | 807 \textbf{Remark.} |
786 This function introduces the idiosyncrasy that all iterative loops, commonly initiated with the ``for'' keyword, iterate incrementally | 808 This function introduces the idiosyncrasy that all iterative loops, commonly initiated with the ``for'' keyword, iterate incrementally |
787 when the ``to'' keyword is placed between two expressions. For example, ``for $a$ from $b$ to $c$ do'' means that | 809 when the ``to'' keyword is placed between two expressions. For example, ``for $a$ from $b$ to $c$ do'' means that |
788 a subsequent expression (or body of expressions) are to be evaluated upto $c - b$ times so long as $b \le c$. In each | 810 a subsequent expression (or body of expressions) are to be evaluated upto $c - b$ times so long as $b \le c$. In each |
794 | 816 |
795 One immediate observation of this initializtion function is that it does not return a pointer to a mp\_int structure. It | 817 One immediate observation of this initializtion function is that it does not return a pointer to a mp\_int structure. It |
796 is assumed that the caller has already allocated memory for the mp\_int structure, typically on the application stack. The | 818 is assumed that the caller has already allocated memory for the mp\_int structure, typically on the application stack. The |
797 call to mp\_init() is used only to initialize the members of the structure to a known default state. | 819 call to mp\_init() is used only to initialize the members of the structure to a known default state. |
798 | 820 |
799 Before any of the other members of the structure are initialized memory from the application heap is allocated with | 821 Here we see (line @23,XMALLOC@) the memory allocation is performed first. This allows us to exit cleanly and quickly |
800 the calloc() function (line @22,calloc@). The size of the allocated memory is large enough to hold \textbf{MP\_PREC} | 822 if there is an error. If the allocation fails the routine will return \textbf{MP\_MEM} to the caller to indicate there |
801 mp\_digit variables. The calloc() function is used instead\footnote{calloc() will allocate memory in the same | 823 was a memory error. The function XMALLOC is what actually allocates the memory. Technically XMALLOC is not a function |
802 manner as malloc() except that it also sets the contents to zero upon successfully allocating the memory.} of malloc() | 824 but a macro defined in ``tommath.h``. By default, XMALLOC will evaluate to malloc() which is the C library's built--in |
803 since digits have to be set to zero for the function to finish correctly. The \textbf{OPT\_CAST} token is a macro | 825 memory allocation routine. |
804 definition which will turn into a cast from void * to mp\_digit * for C++ compilers. It is not required for C compilers. | 826 |
805 | 827 In order to assure the mp\_int is in a known state the digits must be set to zero. On most platforms this could have been |
806 After the memory has been successfully allocated the remainder of the members are initialized | 828 accomplished by using calloc() instead of malloc(). However, to correctly initialize a integer type to a given value in a |
829 portable fashion you have to actually assign the value. The for loop (line @28,for@) performs this required | |
830 operation. | |
831 | |
832 After the memory has been successfully initialized the remainder of the members are initialized | |
807 (lines @29,used@ through @31,sign@) to their respective default states. At this point the algorithm has succeeded and | 833 (lines @29,used@ through @31,sign@) to their respective default states. At this point the algorithm has succeeded and |
808 a success code is returned to the calling function. | 834 a success code is returned to the calling function. If this function returns \textbf{MP\_OKAY} it is safe to assume the |
809 | 835 mp\_int structure has been properly initialized and is safe to use with other functions within the library. |
810 If this function returns \textbf{MP\_OKAY} it is safe to assume the mp\_int structure has been properly initialized and | |
811 is safe to use with other functions within the library. | |
812 | 836 |
813 \subsection{Clearing an mp\_int} | 837 \subsection{Clearing an mp\_int} |
814 When an mp\_int is no longer required by the application, the memory that has been allocated for its digits must be | 838 When an mp\_int is no longer required by the application, the memory that has been allocated for its digits must be |
815 returned to the application's memory pool with the mp\_clear algorithm. | 839 returned to the application's memory pool with the mp\_clear algorithm. |
816 | 840 |
817 \begin{figure}[here] | 841 \begin{figure}[here] |
818 \begin{center} | 842 \begin{center} |
819 \begin{tabular}{l} | 843 \begin{tabular}{l} |
820 \hline Algorithm \textbf{mp\_clear}. \\ | 844 \hline Algorithm \textbf{mp\_clear}. \\ |
821 \textbf{Input}. An mp\_int $a$ \\ | 845 \textbf{Input}. An mp\_int $a$ \\ |
822 \textbf{Output}. The memory for $a$ is freed for reuse. \\ | 846 \textbf{Output}. The memory for $a$ shall be deallocated. \\ |
823 \hline \\ | 847 \hline \\ |
824 1. If $a$ has been previously freed then return(\textit{MP\_OKAY}). \\ | 848 1. If $a$ has been previously freed then return(\textit{MP\_OKAY}). \\ |
825 2. for $n$ from 0 to $a.used - 1$ do \\ | 849 2. for $n$ from 0 to $a.used - 1$ do \\ |
826 \hspace{3mm}2.1 $a_n \leftarrow 0$ \\ | 850 \hspace{3mm}2.1 $a_n \leftarrow 0$ \\ |
827 3. Free the memory allocated for the digits of $a$. \\ | 851 3. Free the memory allocated for the digits of $a$. \\ |
834 \end{center} | 858 \end{center} |
835 \caption{Algorithm mp\_clear} | 859 \caption{Algorithm mp\_clear} |
836 \end{figure} | 860 \end{figure} |
837 | 861 |
838 \textbf{Algorithm mp\_clear.} | 862 \textbf{Algorithm mp\_clear.} |
839 This algorithm releases the memory allocated for an mp\_int back into the memory pool for reuse. It is designed | 863 This algorithm accomplishes two goals. First, it clears the digits and the other mp\_int members. This ensures that |
840 such that a given mp\_int structure can be cleared multiple times between initializations without attempting to | 864 if a developer accidentally re-uses a cleared structure it is less likely to cause problems. The second goal |
841 free the memory twice\footnote{In ISO C for example, calling free() twice on the same memory block causes undefinied | 865 is to free the allocated memory. |
842 behaviour.}. | 866 |
843 | 867 The logic behind the algorithm is extended by marking cleared mp\_int structures so that subsequent calls to this |
844 The first step determines if the mp\_int structure has been marked as free already. If it has, the algorithm returns | 868 algorithm will not try to free the memory multiple times. Cleared mp\_ints are detectable by having a pre-defined invalid |
845 success immediately as no further actions are required. Otherwise, the algorithm will proceed to put the structure | 869 digit pointer \textbf{dp} setting. |
846 in a known empty and otherwise invalid state. First the digits of the mp\_int are set to zero. The memory that has been allocated for the | 870 |
847 digits is then freed. The \textbf{used} and \textbf{alloc} counts are both set to zero and the \textbf{sign} set to | 871 Once an mp\_int has been cleared the mp\_int structure is no longer in a valid state for any other algorithm |
848 \textbf{MP\_ZPOS}. This known fixed state for cleared mp\_int structures will make debuging easier for the end | |
849 developer. That is, if they spot (via their debugger) an mp\_int they are using that is in this state it will be | |
850 obvious that they erroneously and prematurely cleared the mp\_int structure. | |
851 | |
852 Note that once an mp\_int has been cleared the mp\_int structure is no longer in a valid state for any other algorithm | |
853 with the exception of algorithms mp\_init, mp\_init\_copy, mp\_init\_size and mp\_clear. | 872 with the exception of algorithms mp\_init, mp\_init\_copy, mp\_init\_size and mp\_clear. |
854 | 873 |
855 EXAM,bn_mp_clear.c | 874 EXAM,bn_mp_clear.c |
856 | 875 |
857 The ``if'' statement (line @21,a->dp != NULL@) prevents the heap from being corrupted if a user double-frees an | 876 The algorithm only operates on the mp\_int if it hasn't been previously cleared. The if statement (line @23,a->dp != NULL@) |
858 mp\_int. This is because once the memory is freed the pointer is set to \textbf{NULL} (line @30,NULL@). | 877 checks to see if the \textbf{dp} member is not \textbf{NULL}. If the mp\_int is a valid mp\_int then \textbf{dp} cannot be |
859 | 878 \textbf{NULL} in which case the if statement will evaluate to true. |
860 Without the check, code that accidentally calls mp\_clear twice for a given mp\_int structure would try to free the memory | 879 |
861 allocated for the digits twice. This may cause some C libraries to signal a fault. By setting the pointer to | 880 The digits of the mp\_int are cleared by the for loop (line @25,for@) which assigns a zero to every digit. Similar to mp\_init() |
862 \textbf{NULL} it helps debug code that may inadvertently free the mp\_int before it is truly not needed, because attempts | 881 the digits are assigned zero instead of using block memory operations (such as memset()) since this is more portable. |
863 to reference digits should fail immediately. The allocated digits are set to zero before being freed (line @24,memset@). | 882 |
864 This is ideal for cryptographic situations where the integer that the mp\_int represents might need to be kept a secret. | 883 The digits are deallocated off the heap via the XFREE macro. Similar to XMALLOC the XFREE macro actually evaluates to |
884 a standard C library function. In this case the free() function. Since free() only deallocates the memory the pointer | |
885 still has to be reset to \textbf{NULL} manually (line @33,NULL@). | |
886 | |
887 Now that the digits have been cleared and deallocated the other members are set to their final values (lines @34,= 0@ and @35,ZPOS@). | |
865 | 888 |
866 \section{Maintenance Algorithms} | 889 \section{Maintenance Algorithms} |
867 | 890 |
868 The previous sections describes how to initialize and clear an mp\_int structure. To further support operations | 891 The previous sections describes how to initialize and clear an mp\_int structure. To further support operations |
869 that are to be performed on mp\_int structures (such as addition and multiplication) the dependent algorithms must be | 892 that are to be performed on mp\_int structures (such as addition and multiplication) the dependent algorithms must be |
887 \textbf{Output}. $a$ is expanded to accomodate $b$ digits. \\ | 910 \textbf{Output}. $a$ is expanded to accomodate $b$ digits. \\ |
888 \hline \\ | 911 \hline \\ |
889 1. if $a.alloc \ge b$ then return(\textit{MP\_OKAY}) \\ | 912 1. if $a.alloc \ge b$ then return(\textit{MP\_OKAY}) \\ |
890 2. $u \leftarrow b\mbox{ (mod }MP\_PREC\mbox{)}$ \\ | 913 2. $u \leftarrow b\mbox{ (mod }MP\_PREC\mbox{)}$ \\ |
891 3. $v \leftarrow b + 2 \cdot MP\_PREC - u$ \\ | 914 3. $v \leftarrow b + 2 \cdot MP\_PREC - u$ \\ |
892 4. Re-Allocate the array of digits $a$ to size $v$ \\ | 915 4. Re-allocate the array of digits $a$ to size $v$ \\ |
893 5. If the allocation failed then return(\textit{MP\_MEM}). \\ | 916 5. If the allocation failed then return(\textit{MP\_MEM}). \\ |
894 6. for n from a.alloc to $v - 1$ do \\ | 917 6. for n from a.alloc to $v - 1$ do \\ |
895 \hspace{+3mm}6.1 $a_n \leftarrow 0$ \\ | 918 \hspace{+3mm}6.1 $a_n \leftarrow 0$ \\ |
896 7. $a.alloc \leftarrow v$ \\ | 919 7. $a.alloc \leftarrow v$ \\ |
897 8. Return(\textit{MP\_OKAY}) \\ | 920 8. Return(\textit{MP\_OKAY}) \\ |
912 akin to how the \textit{realloc} function from the standard C library works. Since the newly allocated digits are | 935 akin to how the \textit{realloc} function from the standard C library works. Since the newly allocated digits are |
913 assumed to contain undefined values they are initially set to zero. | 936 assumed to contain undefined values they are initially set to zero. |
914 | 937 |
915 EXAM,bn_mp_grow.c | 938 EXAM,bn_mp_grow.c |
916 | 939 |
917 The first step is to see if we actually need to perform a re-allocation at all (line @24,a->alloc < size@). If a reallocation | 940 A quick optimization is to first determine if a memory re-allocation is required at all. The if statement (line @23,if@) checks |
918 must occur the digit count is padded upwards to help prevent many trivial reallocations (line @28,size@). Next the reallocation is performed | 941 if the \textbf{alloc} member of the mp\_int is smaller than the requested digit count. If the count is not larger than \textbf{alloc} |
919 and the return of realloc() is stored in a temporary pointer named $tmp$ (line @36,realloc@). The return is stored in a temporary | 942 the function skips the re-allocation part thus saving time. |
920 instead of $a.dp$ to prevent the code from losing the original pointer in case the reallocation fails. Had the return been stored | 943 |
921 in $a.dp$ instead there would be no way to reclaim the heap originally used. | 944 When a re-allocation is performed it is turned into an optimal request to save time in the future. The requested digit count is |
922 | 945 padded upwards to 2nd multiple of \textbf{MP\_PREC} larger than \textbf{alloc} (line @25, size@). The XREALLOC function is used |
923 If the reallocation fails the function will return \textbf{MP\_MEM} (line @39,return@), otherwise, the value of $tmp$ is assigned | 946 to re-allocate the memory. As per the other functions XREALLOC is actually a macro which evaluates to realloc by default. The realloc |
924 to the pointer $a.dp$ and the function continues. A simple for loop from line @48,a->alloc@ to line @50,}@ will zero all digits | 947 function leaves the base of the allocation intact which means the first \textbf{alloc} digits of the mp\_int are the same as before |
925 that were above the old \textbf{alloc} limit to make sure the integer is in a known state. | 948 the re-allocation. All that is left is to clear the newly allocated digits and return. |
949 | |
950 Note that the re-allocation result is actually stored in a temporary pointer $tmp$. This is to allow this function to return | |
951 an error with a valid pointer. Earlier releases of the library stored the result of XREALLOC into the mp\_int $a$. That would | |
952 result in a memory leak if XREALLOC ever failed. | |
926 | 953 |
927 \subsection{Initializing Variable Precision mp\_ints} | 954 \subsection{Initializing Variable Precision mp\_ints} |
928 Occasionally the number of digits required will be known in advance of an initialization, based on, for example, the size | 955 Occasionally the number of digits required will be known in advance of an initialization, based on, for example, the size |
929 of input mp\_ints to a given algorithm. The purpose of algorithm mp\_init\_size is similar to mp\_init except that it | 956 of input mp\_ints to a given algorithm. The purpose of algorithm mp\_init\_size is similar to mp\_init except that it |
930 will allocate \textit{at least} a specified number of digits. | 957 will allocate \textit{at least} a specified number of digits. |
968 The number of digits $b$ requested is padded (line @22,MP_PREC@) by first augmenting it to the next multiple of | 995 The number of digits $b$ requested is padded (line @22,MP_PREC@) by first augmenting it to the next multiple of |
969 \textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result. If the memory can be successfully allocated the | 996 \textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result. If the memory can be successfully allocated the |
970 mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be | 997 mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be |
971 returned (line @27,return@). | 998 returned (line @27,return@). |
972 | 999 |
973 The digits are allocated and set to zero at the same time with the calloc() function (line @25,calloc@). The | 1000 The digits are allocated and set to zero at the same time with the calloc() function (line @25,XCALLOC@). The |
974 \textbf{used} count is set to zero, the \textbf{alloc} count set to the padded digit count and the \textbf{sign} flag set | 1001 \textbf{used} count is set to zero, the \textbf{alloc} count set to the padded digit count and the \textbf{sign} flag set |
975 to \textbf{MP\_ZPOS} to achieve a default valid mp\_int state (lines @29,used@, @30,alloc@ and @31,sign@). If the function | 1002 to \textbf{MP\_ZPOS} to achieve a default valid mp\_int state (lines @29,used@, @30,alloc@ and @31,sign@). If the function |
976 returns succesfully then it is correct to assume that the mp\_int structure is in a valid state for the remainder of the | 1003 returns succesfully then it is correct to assume that the mp\_int structure is in a valid state for the remainder of the |
977 functions to work with. | 1004 functions to work with. |
978 | 1005 |