added libtommath-0.32

This commit is contained in:
Tom St Denis 2004-10-29 22:07:18 +00:00 committed by Steffen Jaeckel
parent 8eaa98807b
commit e549ccfec5
147 changed files with 17871 additions and 5351 deletions

156
bn.ind
View File

@ -1,82 +1,82 @@
\begin{theindex} \begin{theindex}
\item mp\_add, \hyperpage{25} \item mp\_add, \hyperpage{29}
\item mp\_add\_d, \hyperpage{48} \item mp\_add\_d, \hyperpage{52}
\item mp\_and, \hyperpage{25} \item mp\_and, \hyperpage{29}
\item mp\_clear, \hyperpage{7} \item mp\_clear, \hyperpage{11}
\item mp\_clear\_multi, \hyperpage{8} \item mp\_clear\_multi, \hyperpage{12}
\item mp\_cmp, \hyperpage{20} \item mp\_cmp, \hyperpage{24}
\item mp\_cmp\_d, \hyperpage{21} \item mp\_cmp\_d, \hyperpage{25}
\item mp\_cmp\_mag, \hyperpage{19} \item mp\_cmp\_mag, \hyperpage{23}
\item mp\_div, \hyperpage{26} \item mp\_div, \hyperpage{30}
\item mp\_div\_2, \hyperpage{22} \item mp\_div\_2, \hyperpage{26}
\item mp\_div\_2d, \hyperpage{24} \item mp\_div\_2d, \hyperpage{28}
\item mp\_div\_d, \hyperpage{48} \item mp\_div\_d, \hyperpage{52}
\item mp\_dr\_reduce, \hyperpage{36} \item mp\_dr\_reduce, \hyperpage{40}
\item mp\_dr\_setup, \hyperpage{36} \item mp\_dr\_setup, \hyperpage{40}
\item MP\_EQ, \hyperpage{18} \item MP\_EQ, \hyperpage{22}
\item mp\_error\_to\_string, \hyperpage{6} \item mp\_error\_to\_string, \hyperpage{10}
\item mp\_expt\_d, \hyperpage{39} \item mp\_expt\_d, \hyperpage{43}
\item mp\_exptmod, \hyperpage{39} \item mp\_exptmod, \hyperpage{43}
\item mp\_exteuclid, \hyperpage{47} \item mp\_exteuclid, \hyperpage{51}
\item mp\_gcd, \hyperpage{47} \item mp\_gcd, \hyperpage{51}
\item mp\_get\_int, \hyperpage{16} \item mp\_get\_int, \hyperpage{20}
\item mp\_grow, \hyperpage{12} \item mp\_grow, \hyperpage{16}
\item MP\_GT, \hyperpage{18} \item MP\_GT, \hyperpage{22}
\item mp\_init, \hyperpage{7} \item mp\_init, \hyperpage{11}
\item mp\_init\_copy, \hyperpage{9} \item mp\_init\_copy, \hyperpage{13}
\item mp\_init\_multi, \hyperpage{8} \item mp\_init\_multi, \hyperpage{12}
\item mp\_init\_set, \hyperpage{17} \item mp\_init\_set, \hyperpage{21}
\item mp\_init\_set\_int, \hyperpage{17} \item mp\_init\_set\_int, \hyperpage{21}
\item mp\_init\_size, \hyperpage{10} \item mp\_init\_size, \hyperpage{14}
\item mp\_int, \hyperpage{6} \item mp\_int, \hyperpage{10}
\item mp\_invmod, \hyperpage{48} \item mp\_invmod, \hyperpage{52}
\item mp\_jacobi, \hyperpage{48} \item mp\_jacobi, \hyperpage{52}
\item mp\_lcm, \hyperpage{47} \item mp\_lcm, \hyperpage{51}
\item mp\_lshd, \hyperpage{24} \item mp\_lshd, \hyperpage{28}
\item MP\_LT, \hyperpage{18} \item MP\_LT, \hyperpage{22}
\item MP\_MEM, \hyperpage{5} \item MP\_MEM, \hyperpage{9}
\item mp\_mod, \hyperpage{31} \item mp\_mod, \hyperpage{35}
\item mp\_mod\_d, \hyperpage{48} \item mp\_mod\_d, \hyperpage{52}
\item mp\_montgomery\_calc\_normalization, \hyperpage{34} \item mp\_montgomery\_calc\_normalization, \hyperpage{38}
\item mp\_montgomery\_reduce, \hyperpage{33} \item mp\_montgomery\_reduce, \hyperpage{37}
\item mp\_montgomery\_setup, \hyperpage{33} \item mp\_montgomery\_setup, \hyperpage{37}
\item mp\_mul, \hyperpage{27} \item mp\_mul, \hyperpage{31}
\item mp\_mul\_2, \hyperpage{22} \item mp\_mul\_2, \hyperpage{26}
\item mp\_mul\_2d, \hyperpage{24} \item mp\_mul\_2d, \hyperpage{28}
\item mp\_mul\_d, \hyperpage{48} \item mp\_mul\_d, \hyperpage{52}
\item mp\_n\_root, \hyperpage{40} \item mp\_n\_root, \hyperpage{44}
\item mp\_neg, \hyperpage{25} \item mp\_neg, \hyperpage{29}
\item MP\_NO, \hyperpage{5} \item MP\_NO, \hyperpage{9}
\item MP\_OKAY, \hyperpage{5} \item MP\_OKAY, \hyperpage{9}
\item mp\_or, \hyperpage{25} \item mp\_or, \hyperpage{29}
\item mp\_prime\_fermat, \hyperpage{41} \item mp\_prime\_fermat, \hyperpage{45}
\item mp\_prime\_is\_divisible, \hyperpage{41} \item mp\_prime\_is\_divisible, \hyperpage{45}
\item mp\_prime\_is\_prime, \hyperpage{42} \item mp\_prime\_is\_prime, \hyperpage{46}
\item mp\_prime\_miller\_rabin, \hyperpage{41} \item mp\_prime\_miller\_rabin, \hyperpage{45}
\item mp\_prime\_next\_prime, \hyperpage{42} \item mp\_prime\_next\_prime, \hyperpage{46}
\item mp\_prime\_rabin\_miller\_trials, \hyperpage{42} \item mp\_prime\_rabin\_miller\_trials, \hyperpage{46}
\item mp\_prime\_random, \hyperpage{43} \item mp\_prime\_random, \hyperpage{47}
\item mp\_prime\_random\_ex, \hyperpage{43} \item mp\_prime\_random\_ex, \hyperpage{47}
\item mp\_radix\_size, \hyperpage{45} \item mp\_radix\_size, \hyperpage{49}
\item mp\_read\_radix, \hyperpage{45} \item mp\_read\_radix, \hyperpage{49}
\item mp\_read\_unsigned\_bin, \hyperpage{46} \item mp\_read\_unsigned\_bin, \hyperpage{50}
\item mp\_reduce, \hyperpage{32} \item mp\_reduce, \hyperpage{36}
\item mp\_reduce\_2k, \hyperpage{37} \item mp\_reduce\_2k, \hyperpage{41}
\item mp\_reduce\_2k\_setup, \hyperpage{37} \item mp\_reduce\_2k\_setup, \hyperpage{41}
\item mp\_reduce\_setup, \hyperpage{32} \item mp\_reduce\_setup, \hyperpage{36}
\item mp\_rshd, \hyperpage{24} \item mp\_rshd, \hyperpage{28}
\item mp\_set, \hyperpage{15} \item mp\_set, \hyperpage{19}
\item mp\_set\_int, \hyperpage{16} \item mp\_set\_int, \hyperpage{20}
\item mp\_shrink, \hyperpage{11} \item mp\_shrink, \hyperpage{15}
\item mp\_sqr, \hyperpage{29} \item mp\_sqr, \hyperpage{33}
\item mp\_sub, \hyperpage{25} \item mp\_sub, \hyperpage{29}
\item mp\_sub\_d, \hyperpage{48} \item mp\_sub\_d, \hyperpage{52}
\item mp\_to\_unsigned\_bin, \hyperpage{46} \item mp\_to\_unsigned\_bin, \hyperpage{50}
\item mp\_toradix, \hyperpage{45} \item mp\_toradix, \hyperpage{49}
\item mp\_unsigned\_bin\_size, \hyperpage{46} \item mp\_unsigned\_bin\_size, \hyperpage{50}
\item MP\_VAL, \hyperpage{5} \item MP\_VAL, \hyperpage{9}
\item mp\_xor, \hyperpage{25} \item mp\_xor, \hyperpage{29}
\item MP\_YES, \hyperpage{5} \item MP\_YES, \hyperpage{9}
\end{theindex} \end{theindex}

BIN
bn.pdf

Binary file not shown.

123
bn.tex
View File

@ -49,7 +49,7 @@
\begin{document} \begin{document}
\frontmatter \frontmatter
\pagestyle{empty} \pagestyle{empty}
\title{LibTomMath User Manual \\ v0.31} \title{LibTomMath User Manual \\ v0.32}
\author{Tom St Denis \\ tomstdenis@iahu.ca} \author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle \maketitle
This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
@ -96,27 +96,34 @@ LibTomMath is meant to be very ``GCC friendly'' as it comes with a makefile well
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 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
developer. developer.
To build the library for GCC simply issue the \subsection{Static Libraries}
To build as a static library for GCC issue the following
\begin{alltt} \begin{alltt}
make make
\end{alltt} \end{alltt}
command. This will build the library and archive the object files in ``libtommath.a''. Now you simply link against that command. This will build the library and archive the object files in ``libtommath.a''. Now you link against
and include ``tommath.h'' within your programs. that and include ``tommath.h'' within your programs. Alternatively to build with MSVC issue the following
Alternatively to build with MSVC type
\begin{alltt} \begin{alltt}
nmake -f makefile.msvc nmake -f makefile.msvc
\end{alltt} \end{alltt}
This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC version 6.00 This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC
with service pack 5. version 6.00 with service pack 5.
There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile. It requires Cygwin \subsection{Shared Libraries}
to work with since it requires the auto-export/import functionality. The resulting DLL and imprt library ``libtomcrypt.dll.a'' To build as a shared library for GCC issue the following
can be used to link LibTomMath dynamically to any Windows program using Cygwin. \begin{alltt}
make -f makefile.shared
\end{alltt}
This requires the ``libtool'' package (common on most Linux/BSD systems). It will build LibTomMath as both shared
and static then install (by default) into /usr/lib as well as install the header files in /usr/include. The shared
library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''. Generally
you use libtool to link your application against the shared object.
There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile. It requires
Cygwin to work with since it requires the auto-export/import functionality. The resulting DLL and import library
``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin.
\subsection{Testing} \subsection{Testing}
To build the library and the test harness type To build the library and the test harness type
@ -144,6 +151,96 @@ This will output a row of numbers that are increasing. Each column is a differe
that is being performed. The numbers represent how many times the test was invoked. If an error is detected the program that is being performed. The numbers represent how many times the test was invoked. If an error is detected the program
will exit with a dump of the relevent numbers it was working with. will exit with a dump of the relevent numbers it was working with.
\section{Build Configuration}
LibTomMath can configured at build time in three phases we shall call ``depends'', ``tweaks'' and ``trims''.
Each phase changes how the library is built and they are applied one after another respectively.
To make the system more powerful you can tweak the build process. Classes are defined in the file
``tommath\_superclass.h''. By default, the symbol ``LTM\_ALL'' shall be defined which simply
instructs the system to build all of the functions. This is how LibTomMath used to be packaged. This will give you
access to every function LibTomMath offers.
However, there are cases where such a build is not optional. For instance, you want to perform RSA operations. You
don't need the vast majority of the library to perform these operations. Aside from LTM\_ALL there is
another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt. Additional
classes can be defined base on the need of the user.
\subsection{Build Depends}
In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs''
which further define symbols. All of the symbols (technically they're macros $\ldots$) represent a given C source
file. For instance, BN\_MP\_ADD\_C represents the file ``bn\_mp\_add.c''. When a define has been enabled the
function in the respective file will be compiled and linked into the library. Accordingly when the define
is absent the file will not be compiled and not contribute any size to the library.
You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice).
This is to help resolve as many dependencies as possible. In the last pass the symbol LTM\_LAST will be defined.
This is useful for ``trims''.
\subsection{Build Tweaks}
A tweak is an algorithm ``alternative''. For example, to provide tradeoffs (usually between size and space).
They can be enabled at any pass of the configuration phase.
\begin{small}
\begin{center}
\begin{tabular}{|l|l|}
\hline \textbf{Define} & \textbf{Purpose} \\
\hline BN\_MP\_DIV\_SMALL & Enables a slower, smaller and equally \\
& functional mp\_div() function \\
\hline
\end{tabular}
\end{center}
\end{small}
\subsection{Build Trims}
A trim is a manner of removing functionality from a function that is not required. For instance, to perform
RSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed.
Build trims are meant to be defined on the last pass of the configuration which means they are to be defined
only if LTM\_LAST has been defined.
\subsubsection{Moduli Related}
\begin{small}
\begin{center}
\begin{tabular}{|l|l|}
\hline \textbf{Restriction} & \textbf{Undefine} \\
\hline Exponentiation with odd moduli only & BN\_S\_MP\_EXPTMOD\_C \\
& BN\_MP\_REDUCE\_C \\
& BN\_MP\_REDUCE\_SETUP\_C \\
& BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
& BN\_FAST\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
\hline Exponentiation with random odd moduli & (The above plus the following) \\
& BN\_MP\_REDUCE\_2K\_C \\
& BN\_MP\_REDUCE\_2K\_SETUP\_C \\
& BN\_MP\_REDUCE\_IS\_2K\_C \\
& BN\_MP\_DR\_IS\_MODULUS\_C \\
& BN\_MP\_DR\_REDUCE\_C \\
& BN\_MP\_DR\_SETUP\_C \\
\hline Modular inverse odd moduli only & BN\_MP\_INVMOD\_SLOW\_C \\
\hline Modular inverse (both, smaller/slower) & BN\_FAST\_MP\_INVMOD\_C \\
\hline
\end{tabular}
\end{center}
\end{small}
\subsubsection{Operand Size Related}
\begin{small}
\begin{center}
\begin{tabular}{|l|l|}
\hline \textbf{Restriction} & \textbf{Undefine} \\
\hline Moduli $\le 2560$ bits & BN\_MP\_MONTGOMERY\_REDUCE\_C \\
& BN\_S\_MP\_MUL\_DIGS\_C \\
& BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
& BN\_S\_MP\_SQR\_C \\
\hline Polynomial Schmolynomial & BN\_MP\_KARATSUBA\_MUL\_C \\
& BN\_MP\_KARATSUBA\_SQR\_C \\
& BN\_MP\_TOOM\_MUL\_C \\
& BN\_MP\_TOOM\_SQR\_C \\
\hline
\end{tabular}
\end{center}
\end{small}
\section{Purpose of LibTomMath} \section{Purpose of LibTomMath}
Unlike GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with Unlike GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with
bleeding edge performance in mind. First and foremost LibTomMath was written to be entirely open. Not only is the bleeding edge performance in mind. First and foremost LibTomMath was written to be entirely open. Not only is the

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_ERROR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
static const struct { static const struct {
int code; int code;
@ -39,3 +40,4 @@ char *mp_error_to_string(int code)
return "Invalid error code"; return "Invalid error code";
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_FAST_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,12 +14,11 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes the modular inverse via binary extended euclidean algorithm, /* computes the modular inverse via binary extended euclidean algorithm,
* that is c = 1/a mod b * that is c = 1/a mod b
* *
* Based on mp_invmod except this is optimized for the case where b is * Based on slow invmod except this is optimized for the case where b is
* odd as per HAC Note 14.64 on pp. 610 * odd as per HAC Note 14.64 on pp. 610
*/ */
int int
@ -141,3 +142,4 @@ top:
__ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); __ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,11 +14,10 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes xR**-1 == x (mod N) via Montgomery Reduction /* computes xR**-1 == x (mod N) via Montgomery Reduction
* *
* This is an optimized implementation of mp_montgomery_reduce * This is an optimized implementation of montgomery_reduce
* which uses the comba method to quickly calculate the columns of the * which uses the comba method to quickly calculate the columns of the
* reduction. * reduction.
* *
@ -165,3 +166,4 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* Fast (comba) multiplier /* Fast (comba) multiplier
* *
@ -33,8 +34,9 @@
int int
fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
int olduse, res, pa, ix; int olduse, res, pa, ix, iz;
mp_word W[MP_WARRAY]; mp_digit W[MP_WARRAY];
register mp_word _W;
/* grow the destination as required */ /* grow the destination as required */
if (c->alloc < digs) { if (c->alloc < digs) {
@ -43,48 +45,39 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
} }
/* clear temp buf (the columns) */ /* number of output digits to produce */
memset (W, 0, sizeof (mp_word) * digs); pa = MIN(digs, a->used + b->used);
/* calculate the columns */ /* clear the carry */
pa = a->used; _W = 0;
for (ix = 0; ix < pa; ix++) { for (ix = 0; ix <= pa; ix++) {
/* this multiplier has been modified to allow you to int tx, ty;
* control how many digits of output are produced. int iy;
* So at most we want to make upto "digs" digits of output. mp_digit *tmpx, *tmpy;
*
* this adds products to distinct columns (at ix+iy) of W
* note that each step through the loop is not dependent on
* the previous which means the compiler can easily unroll
* the loop without scheduling problems
*/
{
register mp_digit tmpx, *tmpy;
register mp_word *_W;
register int iy, pb;
/* alias for the the word on the left e.g. A[ix] * A[iy] */ /* get offsets into the two bignums */
tmpx = a->dp[ix]; ty = MIN(b->used-1, ix);
tx = ix - ty;
/* alias for the right side */ /* setup temp aliases */
tmpy = b->dp; tmpx = a->dp + tx;
tmpy = b->dp + ty;
/* alias for the columns, each step through the loop adds a new /* this is the number of times the loop will iterrate, essentially its
term to each column while (tx++ < a->used && ty-- >= 0) { ... }
*/ */
_W = W + ix; iy = MIN(a->used-tx, ty+1);
/* the number of digits is limited by their placement. E.g. /* execute loop */
we avoid multiplying digits that will end up above the # of for (iz = 0; iz < iy; ++iz) {
digits of precision requested _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
*/
pb = MIN (b->used, digs - ix);
for (iy = 0; iy < pb; iy++) {
*_W++ += ((mp_word)tmpx) * ((mp_word)*tmpy++);
} }
}
/* store term */
W[ix] = ((mp_digit)_W) & MP_MASK;
/* make next carry */
_W = _W >> ((mp_word)DIGIT_BIT);
} }
/* setup dest */ /* setup dest */
@ -93,32 +86,11 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
register mp_digit *tmpc; register mp_digit *tmpc;
/* At this point W[] contains the sums of each column. To get the
* correct result we must take the extra bits from each column and
* carry them down
*
* Note that while this adds extra code to the multiplier it
* saves time since the carry propagation is removed from the
* above nested loop.This has the effect of reducing the work
* from N*(N+N*c)==N**2 + c*N**2 to N**2 + N*c where c is the
* cost of the shifting. On very small numbers this is slower
* but on most cryptographic size numbers it is faster.
*
* In this particular implementation we feed the carries from
* behind which means when the loop terminates we still have one
* last digit to copy
*/
tmpc = c->dp; tmpc = c->dp;
for (ix = 1; ix < digs; ix++) { for (ix = 0; ix < digs; ix++) {
/* forward the carry from the previous temp */
W[ix] += (W[ix - 1] >> ((mp_word) DIGIT_BIT));
/* now extract the previous digit [below the carry] */ /* now extract the previous digit [below the carry] */
*tmpc++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK)); *tmpc++ = W[ix];
} }
/* fetch the last digit */
*tmpc++ = (mp_digit) (W[digs - 1] & ((mp_word) MP_MASK));
/* clear unused digits [that existed in the old copy of c] */ /* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) { for (; ix < olduse; ix++) {
@ -128,3 +100,4 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
mp_clamp (c); mp_clamp (c);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,10 +14,9 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* this is a modified version of fast_s_mp_mul_digs that only produces /* this is a modified version of fast_s_mul_digs that only produces
* output digits *above* digs. See the comments for fast_s_mp_mul_digs * output digits *above* digs. See the comments for fast_s_mul_digs
* to see how it works. * to see how it works.
* *
* This is used in the Barrett reduction since for one of the multiplications * This is used in the Barrett reduction since for one of the multiplications
@ -26,73 +27,69 @@
int int
fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
int oldused, newused, res, pa, pb, ix; int olduse, res, pa, ix, iz;
mp_word W[MP_WARRAY]; mp_digit W[MP_WARRAY];
mp_word _W;
/* calculate size of product and allocate more space if required */ /* grow the destination as required */
newused = a->used + b->used + 1; pa = a->used + b->used;
if (c->alloc < newused) { if (c->alloc < pa) {
if ((res = mp_grow (c, newused)) != MP_OKAY) { if ((res = mp_grow (c, pa)) != MP_OKAY) {
return res; return res;
} }
} }
/* like the other comba method we compute the columns first */ /* number of output digits to produce */
pa = a->used; pa = a->used + b->used;
pb = b->used; _W = 0;
memset (W + digs, 0, (pa + pb + 1 - digs) * sizeof (mp_word)); for (ix = digs; ix <= pa; ix++) {
for (ix = 0; ix < pa; ix++) { int tx, ty, iy;
{ mp_digit *tmpx, *tmpy;
register mp_digit tmpx, *tmpy;
register int iy;
register mp_word *_W;
/* work todo, that is we only calculate digits that are at "digs" or above */ /* get offsets into the two bignums */
iy = digs - ix; ty = MIN(b->used-1, ix);
tx = ix - ty;
/* copy of word on the left of A[ix] * B[iy] */ /* setup temp aliases */
tmpx = a->dp[ix]; tmpx = a->dp + tx;
tmpy = b->dp + ty;
/* alias for right side */ /* this is the number of times the loop will iterrate, essentially its
tmpy = b->dp + iy; while (tx++ < a->used && ty-- >= 0) { ... }
/* alias for the columns of output. Offset to be equal to or above the
* smallest digit place requested
*/ */
_W = W + digs; iy = MIN(a->used-tx, ty+1);
/* skip cases below zero where ix > digs */ /* execute loop */
if (iy < 0) { for (iz = 0; iz < iy; iz++) {
iy = abs(iy); _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
tmpy += iy;
_W += iy;
iy = 0;
} }
/* compute column products for digits above the minimum */ /* store term */
for (; iy < pb; iy++) { W[ix] = ((mp_digit)_W) & MP_MASK;
*_W++ += ((mp_word) tmpx) * ((mp_word)*tmpy++);
} /* make next carry */
} _W = _W >> ((mp_word)DIGIT_BIT);
} }
/* setup dest */ /* setup dest */
oldused = c->used; olduse = c->used;
c->used = newused; c->used = pa;
/* now convert the array W downto what we need {
* register mp_digit *tmpc;
* See comments in bn_fast_s_mp_mul_digs.c
*/
for (ix = digs + 1; ix < newused; ix++) {
W[ix] += (W[ix - 1] >> ((mp_word) DIGIT_BIT));
c->dp[ix - 1] = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
}
c->dp[newused - 1] = (mp_digit) (W[newused - 1] & ((mp_word) MP_MASK));
for (; ix < oldused; ix++) { tmpc = c->dp + digs;
c->dp[ix] = 0; for (ix = digs; ix <= pa; ix++) {
/* now extract the previous digit [below the carry] */
*tmpc++ = W[ix];
}
/* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) {
*tmpc++ = 0;
}
} }
mp_clamp (c); mp_clamp (c);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_FAST_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* fast squaring /* fast squaring
* *
@ -31,109 +32,98 @@
* Based on Algorithm 14.16 on pp.597 of HAC. * Based on Algorithm 14.16 on pp.597 of HAC.
* *
*/ */
/* the jist of squaring...
you do like mult except the offset of the tmpx [one that starts closer to zero]
can't equal the offset of tmpy. So basically you set up iy like before then you min it with
(ty-tx) so that it never happens. You double all those you add in the inner loop
After that loop you do the squares and add them in.
Remove W2 and don't memset W
*/
int fast_s_mp_sqr (mp_int * a, mp_int * b) int fast_s_mp_sqr (mp_int * a, mp_int * b)
{ {
int olduse, newused, res, ix, pa; int olduse, res, pa, ix, iz;
mp_word W2[MP_WARRAY], W[MP_WARRAY]; mp_digit W[MP_WARRAY], *tmpx;
mp_word W1;
/* calculate size of product and allocate as required */ /* grow the destination as required */
pa = a->used; pa = a->used + a->used;
newused = pa + pa + 1; if (b->alloc < pa) {
if (b->alloc < newused) { if ((res = mp_grow (b, pa)) != MP_OKAY) {
if ((res = mp_grow (b, newused)) != MP_OKAY) {
return res; return res;
} }
} }
/* zero temp buffer (columns) /* number of output digits to produce */
* Note that there are two buffers. Since squaring requires W1 = 0;
* a outer and inner product and the inner product requires for (ix = 0; ix <= pa; ix++) {
* computing a product and doubling it (a relatively expensive int tx, ty, iy;
* op to perform n**2 times if you don't have to) the inner and mp_word _W;
* outer products are computed in different buffers. This way mp_digit *tmpy;
* the inner product can be doubled using n doublings instead of
* n**2
*/
memset (W, 0, newused * sizeof (mp_word));
memset (W2, 0, newused * sizeof (mp_word));
/* This computes the inner product. To simplify the inner N**2 loop /* clear counter */
* the multiplication by two is done afterwards in the N loop. _W = 0;
*/
for (ix = 0; ix < pa; ix++) {
/* compute the outer product
*
* Note that every outer product is computed
* for a particular column only once which means that
* there is no need todo a double precision addition
* into the W2[] array.
*/
W2[ix + ix] = ((mp_word)a->dp[ix]) * ((mp_word)a->dp[ix]);
{ /* get offsets into the two bignums */
register mp_digit tmpx, *tmpy; ty = MIN(a->used-1, ix);
register mp_word *_W; tx = ix - ty;
register int iy;
/* copy of left side */ /* setup temp aliases */
tmpx = a->dp[ix]; tmpx = a->dp + tx;
tmpy = a->dp + ty;
/* alias for right side */ /* this is the number of times the loop will iterrate, essentially its
tmpy = a->dp + (ix + 1); while (tx++ < a->used && ty-- >= 0) { ... }
*/
iy = MIN(a->used-tx, ty+1);
/* the column to store the result in */ /* now for squaring tx can never equal ty
_W = W + (ix + ix + 1); * we halve the distance since they approach at a rate of 2x
* and we have to round because odd cases need to be executed
*/
iy = MIN(iy, (ty-tx+1)>>1);
/* inner products */ /* execute loop */
for (iy = ix + 1; iy < pa; iy++) { for (iz = 0; iz < iy; iz++) {
*_W++ += ((mp_word)tmpx) * ((mp_word)*tmpy++); _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
} }
}
/* double the inner product and add carry */
_W = _W + _W + W1;
/* even columns have the square term in them */
if ((ix&1) == 0) {
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
}
/* store it */
W[ix] = _W;
/* make next carry */
W1 = _W >> ((mp_word)DIGIT_BIT);
} }
/* setup dest */ /* setup dest */
olduse = b->used; olduse = b->used;
b->used = newused; b->used = a->used+a->used;
/* now compute digits
*
* We have to double the inner product sums, add in the
* outer product sums, propagate carries and convert
* to single precision.
*/
{ {
register mp_digit *tmpb; mp_digit *tmpb;
/* double first value, since the inner products are
* half of what they should be
*/
W[0] += W[0] + W2[0];
tmpb = b->dp; tmpb = b->dp;
for (ix = 1; ix < newused; ix++) { for (ix = 0; ix < pa; ix++) {
/* double/add next digit */ *tmpb++ = W[ix] & MP_MASK;
W[ix] += W[ix] + W2[ix];
/* propagate carry forwards [from the previous digit] */
W[ix] = W[ix] + (W[ix - 1] >> ((mp_word) DIGIT_BIT));
/* store the current digit now that the carry isn't
* needed
*/
*tmpb++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
} }
/* set the last value. Note even if the carry is zero
* this is required since the next step will not zero
* it if b originally had a value at b->dp[2*a.used]
*/
*tmpb++ = (mp_digit) (W[(newused) - 1] & ((mp_word) MP_MASK));
/* clear high digits of b if there were any originally */ /* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) { for (; ix < olduse; ix++) {
*tmpb++ = 0; *tmpb++ = 0;
} }
} }
mp_clamp (b); mp_clamp (b);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_2EXPT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes a = 2**b /* computes a = 2**b
* *
@ -40,3 +41,4 @@ mp_2expt (mp_int * a, int b)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_ABS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* b = |a| /* b = |a|
* *
@ -35,3 +36,4 @@ mp_abs (mp_int * a, mp_int * b)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* high level addition (handles signs) */ /* high level addition (handles signs) */
int mp_add (mp_int * a, mp_int * b, mp_int * c) int mp_add (mp_int * a, mp_int * b, mp_int * c)
@ -45,3 +46,4 @@ int mp_add (mp_int * a, mp_int * b, mp_int * c)
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_ADD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* single digit addition */ /* single digit addition */
int int
@ -101,3 +102,4 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_ADDMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* d = a + b (mod c) */ /* d = a + b (mod c) */
int int
@ -33,3 +34,4 @@ mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
mp_clear (&t); mp_clear (&t);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_AND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* AND two ints together */ /* AND two ints together */
int int
@ -49,3 +50,4 @@ mp_and (mp_int * a, mp_int * b, mp_int * c)
mp_clear (&t); mp_clear (&t);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CLAMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* trim unused digits /* trim unused digits
* *
@ -36,3 +37,4 @@ mp_clamp (mp_int * a)
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
} }
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CLEAR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* clear one (frees) */ /* clear one (frees) */
void void
@ -36,3 +37,4 @@ mp_clear (mp_int * a)
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
} }
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CLEAR_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
#include <stdarg.h> #include <stdarg.h>
void mp_clear_multi(mp_int *mp, ...) void mp_clear_multi(mp_int *mp, ...)
@ -26,3 +27,4 @@ void mp_clear_multi(mp_int *mp, ...)
} }
va_end(args); va_end(args);
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* compare two ints (signed)*/ /* compare two ints (signed)*/
int int
@ -35,3 +36,4 @@ mp_cmp (mp_int * a, mp_int * b)
return mp_cmp_mag(a, b); return mp_cmp_mag(a, b);
} }
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CMP_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* compare a digit */ /* compare a digit */
int mp_cmp_d(mp_int * a, mp_digit b) int mp_cmp_d(mp_int * a, mp_digit b)
@ -36,3 +37,4 @@ int mp_cmp_d(mp_int * a, mp_digit b)
return MP_EQ; return MP_EQ;
} }
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CMP_MAG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* compare maginitude of two ints (unsigned) */ /* compare maginitude of two ints (unsigned) */
int mp_cmp_mag (mp_int * a, mp_int * b) int mp_cmp_mag (mp_int * a, mp_int * b)
@ -47,3 +48,4 @@ int mp_cmp_mag (mp_int * a, mp_int * b)
} }
return MP_EQ; return MP_EQ;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_CNT_LSB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
static const int lnz[16] = { static const int lnz[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
@ -45,3 +46,4 @@ int mp_cnt_lsb(mp_int *a)
return x; return x;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* copy, b = a */ /* copy, b = a */
int int
@ -60,3 +61,4 @@ mp_copy (mp_int * a, mp_int * b)
b->sign = a->sign; b->sign = a->sign;
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_COUNT_BITS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* returns the number of bits in an int */ /* returns the number of bits in an int */
int int
@ -37,3 +38,4 @@ mp_count_bits (mp_int * a)
} }
return r; return r;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DIV_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,78 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
#ifdef BN_MP_DIV_SMALL
/* slower bit-bang division... also smaller */
int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{
mp_int ta, tb, tq, q;
int res, n, n2;
/* is divisor zero ? */
if (mp_iszero (b) == 1) {
return MP_VAL;
}
/* if a < b then q=0, r = a */
if (mp_cmp_mag (a, b) == MP_LT) {
if (d != NULL) {
res = mp_copy (a, d);
} else {
res = MP_OKAY;
}
if (c != NULL) {
mp_zero (c);
}
return res;
}
/* init our temps */
if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != MP_OKAY)) {
return res;
}
mp_set(&tq, 1);
n = mp_count_bits(a) - mp_count_bits(b);
if (((res = mp_copy(a, &ta)) != MP_OKAY) ||
((res = mp_copy(b, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
goto __ERR;
}
while (n-- >= 0) {
if (mp_cmp(&tb, &ta) != MP_GT) {
if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
goto __ERR;
}
}
if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
goto __ERR;
}
}
/* now q == quotient and ta == remainder */
n = a->sign;
n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
if (c != NULL) {
mp_exch(c, &q);
c->sign = n2;
}
if (d != NULL) {
mp_exch(d, &ta);
d->sign = n;
}
__ERR:
mp_clear_multi(&ta, &tb, &tq, &q, NULL);
return res;
}
#else
/* integer signed division. /* integer signed division.
* c*b + d == a [e.g. a/b, c=quotient, d=remainder] * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
@ -209,3 +282,7 @@ __T1:mp_clear (&t1);
__Q:mp_clear (&q); __Q:mp_clear (&q);
return res; return res;
} }
#endif
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DIV_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* b = a/2 */ /* b = a/2 */
int mp_div_2(mp_int * a, mp_int * b) int mp_div_2(mp_int * a, mp_int * b)
@ -60,3 +61,4 @@ int mp_div_2(mp_int * a, mp_int * b)
mp_clamp (b); mp_clamp (b);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DIV_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */ /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d) int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
@ -89,3 +90,4 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
mp_clear (&t); mp_clear (&t);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DIV_3_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* divide by three (based on routine from MPI and the GMP manual) */ /* divide by three (based on routine from MPI and the GMP manual) */
int int
@ -71,3 +72,4 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DIV_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
static int s_is_power_of_two(mp_digit b, int *p) static int s_is_power_of_two(mp_digit b, int *p)
{ {
@ -54,7 +55,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
/* power of two ? */ /* power of two ? */
if (s_is_power_of_two(b, &ix) == 1) { if (s_is_power_of_two(b, &ix) == 1) {
if (d != NULL) { if (d != NULL) {
*d = a->dp[0] & ((1<<ix) - 1); *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
} }
if (c != NULL) { if (c != NULL) {
return mp_div_2d(a, ix, c, NULL); return mp_div_2d(a, ix, c, NULL);
@ -62,10 +63,12 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
return MP_OKAY; return MP_OKAY;
} }
#ifdef BN_MP_DIV_3_C
/* three? */ /* three? */
if (b == 3) { if (b == 3) {
return mp_div_3(a, c, d); return mp_div_3(a, c, d);
} }
#endif
/* no easy answer [c'est la vie]. Just division */ /* no easy answer [c'est la vie]. Just division */
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
@ -100,3 +103,4 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DR_IS_MODULUS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* determines if a number is a valid DR modulus */ /* determines if a number is a valid DR modulus */
int mp_dr_is_modulus(mp_int *a) int mp_dr_is_modulus(mp_int *a)
@ -35,3 +36,4 @@ int mp_dr_is_modulus(mp_int *a)
return 1; return 1;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DR_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm. /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
* *
@ -86,3 +87,4 @@ top:
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_DR_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* determines the setup value */ /* determines the setup value */
void mp_dr_setup(mp_int *a, mp_digit *d) void mp_dr_setup(mp_int *a, mp_digit *d)
@ -24,3 +25,4 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
((mp_word)a->dp[0])); ((mp_word)a->dp[0]));
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_EXCH_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* swap the elements of two integers, for cases where you can't simply swap the /* swap the elements of two integers, for cases where you can't simply swap the
* mp_int pointers around * mp_int pointers around
@ -26,3 +27,4 @@ mp_exch (mp_int * a, mp_int * b)
*a = *b; *a = *b;
*b = t; *b = t;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_EXPT_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* calculate c = a**b using a square-multiply algorithm */ /* calculate c = a**b using a square-multiply algorithm */
int mp_expt_d (mp_int * a, mp_digit b, mp_int * c) int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
@ -49,3 +50,4 @@ int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
mp_clear (&g); mp_clear (&g);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_EXPTMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* this is a shell function that calls either the normal or Montgomery /* this is a shell function that calls either the normal or Montgomery
@ -31,6 +32,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
/* if exponent X is negative we have to recurse */ /* if exponent X is negative we have to recurse */
if (X->sign == MP_NEG) { if (X->sign == MP_NEG) {
#ifdef BN_MP_INVMOD_C
mp_int tmpG, tmpX; mp_int tmpG, tmpX;
int err; int err;
@ -57,22 +59,42 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
err = mp_exptmod(&tmpG, &tmpX, P, Y); err = mp_exptmod(&tmpG, &tmpX, P, Y);
mp_clear_multi(&tmpG, &tmpX, NULL); mp_clear_multi(&tmpG, &tmpX, NULL);
return err; return err;
#else
/* no invmod */
return MP_VAL
#endif
} }
#ifdef BN_MP_DR_IS_MODULUS_C
/* is it a DR modulus? */ /* is it a DR modulus? */
dr = mp_dr_is_modulus(P); dr = mp_dr_is_modulus(P);
#else
dr = 0;
#endif
#ifdef BN_MP_REDUCE_IS_2K_C
/* if not, is it a uDR modulus? */ /* if not, is it a uDR modulus? */
if (dr == 0) { if (dr == 0) {
dr = mp_reduce_is_2k(P) << 1; dr = mp_reduce_is_2k(P) << 1;
} }
#endif
/* if the modulus is odd or dr != 0 use the fast method */ /* if the modulus is odd or dr != 0 use the fast method */
#ifdef BN_MP_EXPTMOD_FAST_C
if (mp_isodd (P) == 1 || dr != 0) { if (mp_isodd (P) == 1 || dr != 0) {
return mp_exptmod_fast (G, X, P, Y, dr); return mp_exptmod_fast (G, X, P, Y, dr);
} else { } else {
#endif
#ifdef BN_S_MP_EXPTMOD_C
/* otherwise use the generic Barrett reduction technique */ /* otherwise use the generic Barrett reduction technique */
return s_mp_exptmod (G, X, P, Y); return s_mp_exptmod (G, X, P, Y);
#else
/* no exptmod for evens */
return MP_VAL;
#endif
#ifdef BN_MP_EXPTMOD_FAST_C
} }
#endif
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_EXPTMOD_FAST_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
* *
@ -84,29 +85,52 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
/* determine and setup reduction code */ /* determine and setup reduction code */
if (redmode == 0) { if (redmode == 0) {
#ifdef BN_MP_MONTGOMERY_SETUP_C
/* now setup montgomery */ /* now setup montgomery */
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
goto __M; goto __M;
} }
#else
err = MP_VAL;
goto __M;
#endif
/* automatically pick the comba one if available (saves quite a few calls/ifs) */ /* automatically pick the comba one if available (saves quite a few calls/ifs) */
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
if (((P->used * 2 + 1) < MP_WARRAY) && if (((P->used * 2 + 1) < MP_WARRAY) &&
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
redux = fast_mp_montgomery_reduce; redux = fast_mp_montgomery_reduce;
} else { } else
#endif
{
#ifdef BN_MP_MONTGOMERY_REDUCE_C
/* use slower baseline Montgomery method */ /* use slower baseline Montgomery method */
redux = mp_montgomery_reduce; redux = mp_montgomery_reduce;
#else
err = MP_VAL;
goto __M;
#endif
} }
} else if (redmode == 1) { } else if (redmode == 1) {
#if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
/* setup DR reduction for moduli of the form B**k - b */ /* setup DR reduction for moduli of the form B**k - b */
mp_dr_setup(P, &mp); mp_dr_setup(P, &mp);
redux = mp_dr_reduce; redux = mp_dr_reduce;
#else
err = MP_VAL;
goto __M;
#endif
} else { } else {
#if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
/* setup DR reduction for moduli of the form 2**k - b */ /* setup DR reduction for moduli of the form 2**k - b */
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) { if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
goto __M; goto __M;
} }
redux = mp_reduce_2k; redux = mp_reduce_2k;
#else
err = MP_VAL;
goto __M;
#endif
} }
/* setup result */ /* setup result */
@ -116,16 +140,21 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
/* create M table /* create M table
* *
* The M table contains powers of the input base, e.g. M[x] = G^x mod P
* *
* The first half of the table is not computed though accept for M[0] and M[1] * The first half of the table is not computed though accept for M[0] and M[1]
*/ */
if (redmode == 0) { if (redmode == 0) {
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
/* now we need R mod m */ /* now we need R mod m */
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) { if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
goto __RES; goto __RES;
} }
#else
err = MP_VAL;
goto __RES;
#endif
/* now set M[1] to G * R mod m */ /* now set M[1] to G * R mod m */
if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) { if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
@ -269,7 +298,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
* to reduce one more time to cancel out the factor * to reduce one more time to cancel out the factor
* of R. * of R.
*/ */
if ((err = mp_montgomery_reduce (&res, P, mp)) != MP_OKAY) { if ((err = redux(&res, P, mp)) != MP_OKAY) {
goto __RES; goto __RES;
} }
} }
@ -285,3 +314,5 @@ __M:
} }
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_EXTEUCLID_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* Extended euclidean algorithm of (a, b) produces /* Extended euclidean algorithm of (a, b) produces
a*u1 + b*u2 = u3 a*u1 + b*u2 = u3
@ -67,3 +68,4 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_FREAD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* read a bigint from a file stream in ASCII */ /* read a bigint from a file stream in ASCII */
int mp_fread(mp_int *a, int radix, FILE *stream) int mp_fread(mp_int *a, int radix, FILE *stream)
@ -59,3 +60,4 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_FWRITE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
int mp_fwrite(mp_int *a, int radix, FILE *stream) int mp_fwrite(mp_int *a, int radix, FILE *stream)
{ {
@ -44,3 +45,4 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_GCD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* Greatest Common Divisor using the binary method */ /* Greatest Common Divisor using the binary method */
int mp_gcd (mp_int * a, mp_int * b, mp_int * c) int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
@ -105,3 +106,4 @@ __V:mp_clear (&u);
__U:mp_clear (&v); __U:mp_clear (&v);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_GET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* get the lower 32-bits of an mp_int */ /* get the lower 32-bits of an mp_int */
unsigned long mp_get_int(mp_int * a) unsigned long mp_get_int(mp_int * a)
@ -37,3 +38,4 @@ unsigned long mp_get_int(mp_int * a)
/* force result to 32-bits always so it is consistent on non 32-bit platforms */ /* force result to 32-bits always so it is consistent on non 32-bit platforms */
return res & 0xFFFFFFFFUL; return res & 0xFFFFFFFFUL;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_GROW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* grow as required */ /* grow as required */
int mp_grow (mp_int * a, int size) int mp_grow (mp_int * a, int size)
@ -49,3 +50,4 @@ int mp_grow (mp_int * a, int size)
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INIT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* init a new mp_int */ /* init a new mp_int */
int mp_init (mp_int * a) int mp_init (mp_int * a)
@ -38,3 +39,4 @@ int mp_init (mp_int * a)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INIT_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* creates "a" then copies b into it */ /* creates "a" then copies b into it */
int mp_init_copy (mp_int * a, mp_int * b) int mp_init_copy (mp_int * a, mp_int * b)
@ -24,3 +25,4 @@ int mp_init_copy (mp_int * a, mp_int * b)
} }
return mp_copy (b, a); return mp_copy (b, a);
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INIT_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
#include <stdarg.h> #include <stdarg.h>
int mp_init_multi(mp_int *mp, ...) int mp_init_multi(mp_int *mp, ...)
@ -51,3 +52,4 @@ int mp_init_multi(mp_int *mp, ...)
return res; /* Assumed ok, if error flagged above. */ return res; /* Assumed ok, if error flagged above. */
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INIT_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* initialize and set a digit */ /* initialize and set a digit */
int mp_init_set (mp_int * a, mp_digit b) int mp_init_set (mp_int * a, mp_digit b)
@ -24,3 +25,4 @@ int mp_init_set (mp_int * a, mp_digit b)
mp_set(a, b); mp_set(a, b);
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INIT_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* initialize and set a digit */ /* initialize and set a digit */
int mp_init_set_int (mp_int * a, unsigned long b) int mp_init_set_int (mp_int * a, unsigned long b)
@ -23,3 +24,4 @@ int mp_init_set_int (mp_int * a, unsigned long b)
} }
return mp_set_int(a, b); return mp_set_int(a, b);
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INIT_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,22 +14,31 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* init an mp_init for a given size */ /* init an mp_init for a given size */
int mp_init_size (mp_int * a, int size) int mp_init_size (mp_int * a, int size)
{ {
int x;
/* pad size so there are always extra digits */ /* pad size so there are always extra digits */
size += (MP_PREC * 2) - (size % MP_PREC); size += (MP_PREC * 2) - (size % MP_PREC);
/* alloc mem */ /* alloc mem */
a->dp = OPT_CAST(mp_digit) XCALLOC (sizeof (mp_digit), size); a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
if (a->dp == NULL) { if (a->dp == NULL) {
return MP_MEM; return MP_MEM;
} }
/* set the members */
a->used = 0; a->used = 0;
a->alloc = size; a->alloc = size;
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
/* zero the digits */
for (x = 0; x < size; x++) {
a->dp[x] = 0;
}
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,163 +14,26 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* hac 14.61, pp608 */ /* hac 14.61, pp608 */
int mp_invmod (mp_int * a, mp_int * b, mp_int * c) int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
{ {
mp_int x, y, u, v, A, B, C, D;
int res;
/* b cannot be negative */ /* b cannot be negative */
if (b->sign == MP_NEG || mp_iszero(b) == 1) { if (b->sign == MP_NEG || mp_iszero(b) == 1) {
return MP_VAL; return MP_VAL;
} }
#ifdef BN_FAST_MP_INVMOD_C
/* if the modulus is odd we can use a faster routine instead */ /* if the modulus is odd we can use a faster routine instead */
if (mp_isodd (b) == 1) { if (mp_isodd (b) == 1) {
return fast_mp_invmod (a, b, c); return fast_mp_invmod (a, b, c);
} }
#endif
/* init temps */
if ((res = mp_init_multi(&x, &y, &u, &v,
&A, &B, &C, &D, NULL)) != MP_OKAY) {
return res;
}
/* x = a, y = b */ #ifdef BN_MP_INVMOD_SLOW_C
if ((res = mp_copy (a, &x)) != MP_OKAY) { return mp_invmod_slow(a, b, c);
goto __ERR; #endif
}
if ((res = mp_copy (b, &y)) != MP_OKAY) {
goto __ERR;
}
/* 2. [modified] if x,y are both even then return an error! */ return MP_VAL;
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
res = MP_VAL;
goto __ERR;
}
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
goto __ERR;
}
mp_set (&A, 1);
mp_set (&D, 1);
top:
/* 4. while u is even do */
while (mp_iseven (&u) == 1) {
/* 4.1 u = u/2 */
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto __ERR;
}
/* 4.2 if A or B is odd then */
if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
/* A = (A+y)/2, B = (B-x)/2 */
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
goto __ERR;
}
}
/* A = A/2, B = B/2 */
if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
goto __ERR;
}
}
/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
/* 5.1 v = v/2 */
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
goto __ERR;
}
/* 5.2 if C or D is odd then */
if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
/* C = (C+y)/2, D = (D-x)/2 */
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto __ERR;
}
}
/* C = C/2, D = D/2 */
if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
goto __ERR;
}
}
/* 6. if u >= v then */
if (mp_cmp (&u, &v) != MP_LT) {
/* u = u - v, A = A - C, B = B - D */
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
goto __ERR;
}
} else {
/* v - v - u, C = C - A, D = D - B */
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
goto __ERR;
}
}
/* if not zero goto step 4 */
if (mp_iszero (&u) == 0)
goto top;
/* now a = C, b = D, gcd == g*v */
/* if v != 1 then there is no inverse */
if (mp_cmp_d (&v, 1) != MP_EQ) {
res = MP_VAL;
goto __ERR;
}
/* if its too low */
while (mp_cmp_d(&C, 0) == MP_LT) {
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
goto __ERR;
}
}
/* too big */
while (mp_cmp_mag(&C, b) != MP_LT) {
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
goto __ERR;
}
}
/* C is now the inverse */
mp_exch (&C, c);
res = MP_OKAY;
__ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
return res;
} }
#endif

171
bn_mp_invmod_slow.c Normal file
View File

@ -0,0 +1,171 @@
#include <tommath.h>
#ifdef BN_MP_INVMOD_SLOW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
/* hac 14.61, pp608 */
int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
{
mp_int x, y, u, v, A, B, C, D;
int res;
/* b cannot be negative */
if (b->sign == MP_NEG || mp_iszero(b) == 1) {
return MP_VAL;
}
/* init temps */
if ((res = mp_init_multi(&x, &y, &u, &v,
&A, &B, &C, &D, NULL)) != MP_OKAY) {
return res;
}
/* x = a, y = b */
if ((res = mp_copy (a, &x)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_copy (b, &y)) != MP_OKAY) {
goto __ERR;
}
/* 2. [modified] if x,y are both even then return an error! */
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
res = MP_VAL;
goto __ERR;
}
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
goto __ERR;
}
mp_set (&A, 1);
mp_set (&D, 1);
top:
/* 4. while u is even do */
while (mp_iseven (&u) == 1) {
/* 4.1 u = u/2 */
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto __ERR;
}
/* 4.2 if A or B is odd then */
if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
/* A = (A+y)/2, B = (B-x)/2 */
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
goto __ERR;
}
}
/* A = A/2, B = B/2 */
if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
goto __ERR;
}
}
/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
/* 5.1 v = v/2 */
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
goto __ERR;
}
/* 5.2 if C or D is odd then */
if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
/* C = (C+y)/2, D = (D-x)/2 */
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto __ERR;
}
}
/* C = C/2, D = D/2 */
if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
goto __ERR;
}
}
/* 6. if u >= v then */
if (mp_cmp (&u, &v) != MP_LT) {
/* u = u - v, A = A - C, B = B - D */
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
goto __ERR;
}
} else {
/* v - v - u, C = C - A, D = D - B */
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
goto __ERR;
}
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
goto __ERR;
}
}
/* if not zero goto step 4 */
if (mp_iszero (&u) == 0)
goto top;
/* now a = C, b = D, gcd == g*v */
/* if v != 1 then there is no inverse */
if (mp_cmp_d (&v, 1) != MP_EQ) {
res = MP_VAL;
goto __ERR;
}
/* if its too low */
while (mp_cmp_d(&C, 0) == MP_LT) {
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
goto __ERR;
}
}
/* too big */
while (mp_cmp_mag(&C, b) != MP_LT) {
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
goto __ERR;
}
}
/* C is now the inverse */
mp_exch (&C, c);
res = MP_OKAY;
__ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
return res;
}
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_IS_SQUARE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* Check if remainders are possible squares - fast exclude non-squares */ /* Check if remainders are possible squares - fast exclude non-squares */
static const char rem_128[128] = { static const char rem_128[128] = {
@ -69,7 +70,7 @@ int mp_is_square(mp_int *arg,int *ret)
return MP_OKAY; return MP_OKAY;
} }
/* product of primes less than 2^31 */
if ((res = mp_init_set_int(&t,11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) { if ((res = mp_init_set_int(&t,11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
return res; return res;
} }
@ -101,3 +102,4 @@ int mp_is_square(mp_int *arg,int *ret)
ERR:mp_clear(&t); ERR:mp_clear(&t);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_JACOBI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes the jacobi c = (a | n) (or Legendre if n is prime) /* computes the jacobi c = (a | n) (or Legendre if n is prime)
* HAC pp. 73 Algorithm 2.149 * HAC pp. 73 Algorithm 2.149
@ -97,3 +98,4 @@ __P1:mp_clear (&p1);
__A1:mp_clear (&a1); __A1:mp_clear (&a1);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_KARATSUBA_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* c = |a| * |b| using Karatsuba Multiplication using /* c = |a| * |b| using Karatsuba Multiplication using
* three half size multiplications * three half size multiplications
@ -159,3 +160,4 @@ X0:mp_clear (&x0);
ERR: ERR:
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_KARATSUBA_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,12 +14,11 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* Karatsuba squaring, computes b = a*a using three /* Karatsuba squaring, computes b = a*a using three
* half size squarings * half size squarings
* *
* See comments of mp_karatsuba_mul for details. It * See comments of karatsuba_mul for details. It
* is essentially the same algorithm but merely * is essentially the same algorithm but merely
* tuned to perform recursive squarings. * tuned to perform recursive squarings.
*/ */
@ -113,3 +114,4 @@ X0:mp_clear (&x0);
ERR: ERR:
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_LCM_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes least common multiple as |a*b|/(a, b) */ /* computes least common multiple as |a*b|/(a, b) */
int mp_lcm (mp_int * a, mp_int * b, mp_int * c) int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
@ -52,3 +53,4 @@ __T:
mp_clear_multi (&t1, &t2, NULL); mp_clear_multi (&t1, &t2, NULL);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_LSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* shift left a certain amount of digits */ /* shift left a certain amount of digits */
int mp_lshd (mp_int * a, int b) int mp_lshd (mp_int * a, int b)
@ -59,3 +60,4 @@ int mp_lshd (mp_int * a, int b)
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* c = a mod b, 0 <= c < b */ /* c = a mod b, 0 <= c < b */
int int
@ -40,3 +41,4 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
mp_clear (&t); mp_clear (&t);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MOD_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* calc a value mod 2**b */ /* calc a value mod 2**b */
int int
@ -47,3 +48,4 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
mp_clamp (c); mp_clamp (c);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MOD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,10 +14,10 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
int int
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c) mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
{ {
return mp_div_d(a, b, NULL, c); return mp_div_d(a, b, NULL, c);
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,31 +14,31 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* calculates a = B^n mod b for Montgomery reduction /*
* Where B is the base [e.g. 2^DIGIT_BIT].
* B^n mod b is computed by first computing
* A = B^(n-1) which doesn't require a reduction but a simple OR.
* then C = A * B = B^n is computed by performing upto DIGIT_BIT
* shifts with subtractions when the result is greater than b. * shifts with subtractions when the result is greater than b.
* *
* The method is slightly modified to shift B unconditionally upto just under * The method is slightly modified to shift B unconditionally upto just under
* the leading bit of b. This saves alot of multiple precision shifting. * the leading bit of b. This saves alot of multiple precision shifting.
*/ */
int int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
{ {
int x, bits, res; int x, bits, res;
/* how many bits of last digit does b use */ /* how many bits of last digit does b use */
bits = mp_count_bits (b) % DIGIT_BIT; bits = mp_count_bits (b) % DIGIT_BIT;
/* compute A = B^(n-1) * 2^(bits-1) */
if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) { if (b->used > 1) {
return res; if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) {
return res;
}
} else {
mp_set(a, 1);
bits = 1;
} }
/* now compute C = A * B mod b */ /* now compute C = A * B mod b */
for (x = bits - 1; x < (int)DIGIT_BIT; x++) { for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
if ((res = mp_mul_2 (a, a)) != MP_OKAY) { if ((res = mp_mul_2 (a, a)) != MP_OKAY) {
@ -51,3 +53,4 @@ mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes xR**-1 == x (mod N) via Montgomery Reduction */ /* computes xR**-1 == x (mod N) via Montgomery Reduction */
int int
@ -23,7 +24,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
/* can the fast reduction [comba] method be used? /* can the fast reduction [comba] method be used?
* *
* Note that unlike in mp_mul you're safely allowed *less* * Note that unlike in mul you're safely allowed *less*
* than the available columns [255 per default] since carries * than the available columns [255 per default] since carries
* are fixed up in the inner loop. * are fixed up in the inner loop.
*/ */
@ -46,7 +47,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
/* mu = ai * rho mod b /* mu = ai * rho mod b
* *
* The value of rho must be precalculated via * The value of rho must be precalculated via
* bn_mp_montgomery_setup() such that * montgomery_setup() such that
* it equals -1/n0 mod b this allows the * it equals -1/n0 mod b this allows the
* following inner loop to reduce the * following inner loop to reduce the
* input one digit at a time * input one digit at a time
@ -110,3 +111,4 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MONTGOMERY_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* setups the montgomery reduction stuff */ /* setups the montgomery reduction stuff */
int int
@ -47,7 +48,8 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho)
#endif #endif
/* rho = -1/m mod b */ /* rho = -1/m mod b */
*rho = (((mp_digit) 1 << ((mp_digit) DIGIT_BIT)) - x) & MP_MASK; *rho = (((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* high level multiplication (handles sign) */ /* high level multiplication (handles sign) */
int mp_mul (mp_int * a, mp_int * b, mp_int * c) int mp_mul (mp_int * a, mp_int * b, mp_int * c)
@ -21,12 +22,18 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
/* use Toom-Cook? */ /* use Toom-Cook? */
#ifdef BN_MP_TOOM_MUL_C
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
res = mp_toom_mul(a, b, c); res = mp_toom_mul(a, b, c);
} else
#endif
#ifdef BN_MP_KARATSUBA_MUL_C
/* use Karatsuba? */ /* use Karatsuba? */
} else if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
res = mp_karatsuba_mul (a, b, c); res = mp_karatsuba_mul (a, b, c);
} else { } else
#endif
{
/* can we use the fast multiplier? /* can we use the fast multiplier?
* *
* The fast multiplier can be used if the output will * The fast multiplier can be used if the output will
@ -35,14 +42,21 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
*/ */
int digs = a->used + b->used + 1; int digs = a->used + b->used + 1;
#ifdef BN_FAST_S_MP_MUL_DIGS_C
if ((digs < MP_WARRAY) && if ((digs < MP_WARRAY) &&
MIN(a->used, b->used) <= MIN(a->used, b->used) <=
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
res = fast_s_mp_mul_digs (a, b, c, digs); res = fast_s_mp_mul_digs (a, b, c, digs);
} else { } else
res = s_mp_mul (a, b, c); #endif
} #ifdef BN_S_MP_MUL_DIGS_C
res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
#else
res = MP_VAL;
#endif
} }
c->sign = (c->used > 0) ? neg : MP_ZPOS; c->sign = (c->used > 0) ? neg : MP_ZPOS;
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MUL_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* b = a*2 */ /* b = a*2 */
int mp_mul_2(mp_int * a, mp_int * b) int mp_mul_2(mp_int * a, mp_int * b)
@ -74,3 +75,4 @@ int mp_mul_2(mp_int * a, mp_int * b)
b->sign = a->sign; b->sign = a->sign;
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MUL_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* shift left by a certain bit count */ /* shift left by a certain bit count */
int mp_mul_2d (mp_int * a, int b, mp_int * c) int mp_mul_2d (mp_int * a, int b, mp_int * c)
@ -77,3 +78,4 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
mp_clamp (c); mp_clamp (c);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MUL_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* multiply by a digit */ /* multiply by a digit */
int int
@ -70,3 +71,4 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_MULMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* d = a * b (mod c) */ /* d = a * b (mod c) */
int int
@ -33,3 +34,4 @@ mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
mp_clear (&t); mp_clear (&t);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_N_ROOT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* find the n'th root of an integer /* find the n'th root of an integer
* *
@ -124,3 +125,4 @@ __T2:mp_clear (&t2);
__T1:mp_clear (&t1); __T1:mp_clear (&t1);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_NEG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* b = -a */ /* b = -a */
int mp_neg (mp_int * a, mp_int * b) int mp_neg (mp_int * a, mp_int * b)
@ -26,3 +27,4 @@ int mp_neg (mp_int * a, mp_int * b)
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_OR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* OR two ints together */ /* OR two ints together */
int mp_or (mp_int * a, mp_int * b, mp_int * c) int mp_or (mp_int * a, mp_int * b, mp_int * c)
@ -42,3 +43,4 @@ int mp_or (mp_int * a, mp_int * b, mp_int * c)
mp_clear (&t); mp_clear (&t);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_FERMAT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* performs one Fermat test. /* performs one Fermat test.
* *
@ -54,3 +55,4 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
__T:mp_clear (&t); __T:mp_clear (&t);
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* determines if an integers is divisible by one /* determines if an integers is divisible by one
* of the first PRIME_SIZE primes or not * of the first PRIME_SIZE primes or not
@ -42,3 +43,4 @@ int mp_prime_is_divisible (mp_int * a, int *result)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_IS_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,12 +14,11 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* performs a variable number of rounds of Miller-Rabin /* performs a variable number of rounds of Miller-Rabin
* *
* Probability of error after t rounds is no more than * Probability of error after t rounds is no more than
* (1/4)^t when 1 <= t <= PRIME_SIZE
* *
* Sets result to 1 if probably prime, 0 otherwise * Sets result to 1 if probably prime, 0 otherwise
*/ */
@ -75,3 +76,4 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
__B:mp_clear (&b); __B:mp_clear (&b);
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_MILLER_RABIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* Miller-Rabin test of "a" to the base of "b" as described in /* Miller-Rabin test of "a" to the base of "b" as described in
* HAC pp. 139 Algorithm 4.24 * HAC pp. 139 Algorithm 4.24
@ -95,3 +96,4 @@ __R:mp_clear (&r);
__N1:mp_clear (&n1); __N1:mp_clear (&n1);
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_NEXT_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* finds the next prime after the number "a" using "t" trials /* finds the next prime after the number "a" using "t" trials
* of Miller-Rabin. * of Miller-Rabin.
@ -162,3 +163,4 @@ __ERR:
return err; return err;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,9 +14,8 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* this table gives the # of rabin miller trials for a prob of failure lower than 2^-96 */
static const struct { static const struct {
int k, t; int k, t;
} sizes[] = { } sizes[] = {
@ -25,13 +26,8 @@ static const struct {
{ 640, 6 }, { 640, 6 },
{ 768, 5 }, { 768, 5 },
{ 896, 4 }, { 896, 4 },
{ 1024, 4 }, { 1024, 4 }
{ 1152, 3 }, };
{ 1280, 3 },
{ 1408, 3 },
{ 1536, 3 },
{ 1664, 3 },
{ 1792, 2 } };
/* returns # of RM trials required for a given bit size */ /* returns # of RM trials required for a given bit size */
int mp_prime_rabin_miller_trials(int size) int mp_prime_rabin_miller_trials(int size)
@ -45,7 +41,8 @@ int mp_prime_rabin_miller_trials(int size)
return (x == 0) ? sizes[0].t : sizes[x - 1].t; return (x == 0) ? sizes[0].t : sizes[x - 1].t;
} }
} }
return 1; return sizes[x-1].t + 1;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_PRIME_RANDOM_EX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* makes a truly random prime of a given size (bits), /* makes a truly random prime of a given size (bits),
* *
@ -92,6 +93,9 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
/* is it prime? */ /* is it prime? */
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; } if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
if (res == MP_NO) {
continue;
}
if (flags & LTM_PRIME_SAFE) { if (flags & LTM_PRIME_SAFE) {
/* see if (a-1)/2 is prime */ /* see if (a-1)/2 is prime */
@ -116,3 +120,4 @@ error:
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_RADIX_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* returns size of ASCII reprensentation */ /* returns size of ASCII reprensentation */
int mp_radix_size (mp_int * a, int radix, int *size) int mp_radix_size (mp_int * a, int radix, int *size)
@ -63,3 +64,4 @@ int mp_radix_size (mp_int * a, int radix, int *size)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_RADIX_SMAP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,7 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* chars used in radix conversions */ /* chars used in radix conversions */
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_RAND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* makes a pseudo-random int of a given size */ /* makes a pseudo-random int of a given size */
int int
@ -47,3 +48,4 @@ mp_rand (mp_int * a, int digits)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_READ_RADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* read a string [ASCII] in a given radix */ /* read a string [ASCII] in a given radix */
int mp_read_radix (mp_int * a, char *str, int radix) int mp_read_radix (mp_int * a, char *str, int radix)
@ -74,3 +75,4 @@ int mp_read_radix (mp_int * a, char *str, int radix)
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_READ_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* read signed bin, big endian, first byte is 0==positive or 1==negative */ /* read signed bin, big endian, first byte is 0==positive or 1==negative */
int int
@ -34,3 +35,4 @@ mp_read_signed_bin (mp_int * a, unsigned char *b, int c)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_READ_UNSIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* reads a unsigned char array, assumes the msb is stored first [big endian] */ /* reads a unsigned char array, assumes the msb is stored first [big endian] */
int int
@ -48,3 +49,4 @@ mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
mp_clamp (a); mp_clamp (a);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* reduces x mod m, assumes 0 < x < m**2, mu is /* reduces x mod m, assumes 0 < x < m**2, mu is
* precomputed via mp_reduce_setup. * precomputed via mp_reduce_setup.
@ -38,9 +39,20 @@ mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
goto CLEANUP; goto CLEANUP;
} }
} else { } else {
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
if ((res = s_mp_mul_high_digs (&q, mu, &q, um - 1)) != MP_OKAY) { if ((res = s_mp_mul_high_digs (&q, mu, &q, um - 1)) != MP_OKAY) {
goto CLEANUP; goto CLEANUP;
} }
#elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um - 1)) != MP_OKAY) {
goto CLEANUP;
}
#else
{
res = MP_VAL;
goto CLEANUP;
}
#endif
} }
/* q3 = q2 / b**(k+1) */ /* q3 = q2 / b**(k+1) */
@ -82,3 +94,4 @@ CLEANUP:
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* reduces a modulo n where n is of the form 2**p - d */ /* reduces a modulo n where n is of the form 2**p - d */
int int
@ -54,3 +55,4 @@ ERR:
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* determines the setup value */ /* determines the setup value */
int int
@ -40,3 +41,4 @@ mp_reduce_2k_setup(mp_int *a, mp_digit *d)
mp_clear(&tmp); mp_clear(&tmp);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* determines if mp_reduce_2k can be used */ /* determines if mp_reduce_2k can be used */
int mp_reduce_is_2k(mp_int *a) int mp_reduce_is_2k(mp_int *a)
@ -44,3 +45,4 @@ int mp_reduce_is_2k(mp_int *a)
return 1; return 1;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,13 +14,11 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* pre-calculate the value required for Barrett reduction /* pre-calculate the value required for Barrett reduction
* For a given modulus "b" it calulates the value required in "a" * For a given modulus "b" it calulates the value required in "a"
*/ */
int int mp_reduce_setup (mp_int * a, mp_int * b)
mp_reduce_setup (mp_int * a, mp_int * b)
{ {
int res; int res;
@ -27,3 +27,4 @@ mp_reduce_setup (mp_int * a, mp_int * b)
} }
return mp_div (a, b, a, NULL); return mp_div (a, b, a, NULL);
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_RSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* shift right a certain amount of digits */ /* shift right a certain amount of digits */
void mp_rshd (mp_int * a, int b) void mp_rshd (mp_int * a, int b)
@ -64,3 +65,4 @@ void mp_rshd (mp_int * a, int b)
/* remove excess digits */ /* remove excess digits */
a->used -= b; a->used -= b;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* set to a digit */ /* set to a digit */
void mp_set (mp_int * a, mp_digit b) void mp_set (mp_int * a, mp_digit b)
@ -21,3 +22,4 @@ void mp_set (mp_int * a, mp_digit b)
a->dp[0] = b & MP_MASK; a->dp[0] = b & MP_MASK;
a->used = (a->dp[0] != 0) ? 1 : 0; a->used = (a->dp[0] != 0) ? 1 : 0;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* set a 32-bit const */ /* set a 32-bit const */
int mp_set_int (mp_int * a, unsigned long b) int mp_set_int (mp_int * a, unsigned long b)
@ -40,3 +41,4 @@ int mp_set_int (mp_int * a, unsigned long b)
mp_clamp (a); mp_clamp (a);
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SHRINK_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* shrink a bignum */ /* shrink a bignum */
int mp_shrink (mp_int * a) int mp_shrink (mp_int * a)
@ -27,3 +28,4 @@ int mp_shrink (mp_int * a)
} }
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SIGNED_BIN_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,10 +14,10 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* get the size for an signed equivalent */ /* get the size for an signed equivalent */
int mp_signed_bin_size (mp_int * a) int mp_signed_bin_size (mp_int * a)
{ {
return 1 + mp_unsigned_bin_size (a); return 1 + mp_unsigned_bin_size (a);
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* computes b = a*a */ /* computes b = a*a */
int int
@ -20,22 +21,34 @@ mp_sqr (mp_int * a, mp_int * b)
{ {
int res; int res;
#ifdef BN_MP_TOOM_SQR_C
/* use Toom-Cook? */ /* use Toom-Cook? */
if (a->used >= TOOM_SQR_CUTOFF) { if (a->used >= TOOM_SQR_CUTOFF) {
res = mp_toom_sqr(a, b); res = mp_toom_sqr(a, b);
/* Karatsuba? */ /* Karatsuba? */
} else if (a->used >= KARATSUBA_SQR_CUTOFF) { } else
#endif
#ifdef BN_MP_KARATSUBA_SQR_C
if (a->used >= KARATSUBA_SQR_CUTOFF) {
res = mp_karatsuba_sqr (a, b); res = mp_karatsuba_sqr (a, b);
} else { } else
#endif
{
#ifdef BN_FAST_S_MP_SQR_C
/* can we use the fast comba multiplier? */ /* can we use the fast comba multiplier? */
if ((a->used * 2 + 1) < MP_WARRAY && if ((a->used * 2 + 1) < MP_WARRAY &&
a->used < a->used <
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) { (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
res = fast_s_mp_sqr (a, b); res = fast_s_mp_sqr (a, b);
} else { } else
#endif
#ifdef BN_S_MP_SQR_C
res = s_mp_sqr (a, b); res = s_mp_sqr (a, b);
} #else
res = MP_VAL;
#endif
} }
b->sign = MP_ZPOS; b->sign = MP_ZPOS;
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SQRMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* c = a * a (mod b) */ /* c = a * a (mod b) */
int int
@ -33,3 +34,4 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
mp_clear (&t); mp_clear (&t);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SQRT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* this function is less generic than mp_n_root, simpler and faster */ /* this function is less generic than mp_n_root, simpler and faster */
int mp_sqrt(mp_int *arg, mp_int *ret) int mp_sqrt(mp_int *arg, mp_int *ret)
@ -73,3 +74,4 @@ E2: mp_clear(&t1);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SUB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* high level subtraction (handles signs) */ /* high level subtraction (handles signs) */
int int
@ -51,3 +52,4 @@ mp_sub (mp_int * a, mp_int * b, mp_int * c)
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SUB_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* single digit subtraction */ /* single digit subtraction */
int int
@ -81,3 +82,4 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
return MP_OKAY; return MP_OKAY;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_SUBMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* d = a - b (mod c) */ /* d = a - b (mod c) */
int int
@ -34,3 +35,4 @@ mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
mp_clear (&t); mp_clear (&t);
return res; return res;
} }
#endif

View File

@ -1,3 +1,5 @@
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis /* LibTomMath, multiple-precision integer library -- Tom St Denis
* *
* LibTomMath is a library that provides multiple-precision * LibTomMath is a library that provides multiple-precision
@ -12,7 +14,6 @@
* *
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/ */
#include <tommath.h>
/* store in signed [big endian] format */ /* store in signed [big endian] format */
int int
@ -26,3 +27,4 @@ mp_to_signed_bin (mp_int * a, unsigned char *b)
b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1); b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1);
return MP_OKAY; return MP_OKAY;
} }
#endif

Some files were not shown because too many files have changed in this diff Show More