added libtommath-0.32
This commit is contained in:
parent
8eaa98807b
commit
e549ccfec5
156
bn.ind
156
bn.ind
@ -1,82 +1,82 @@
|
||||
\begin{theindex}
|
||||
|
||||
\item mp\_add, \hyperpage{25}
|
||||
\item mp\_add\_d, \hyperpage{48}
|
||||
\item mp\_and, \hyperpage{25}
|
||||
\item mp\_clear, \hyperpage{7}
|
||||
\item mp\_clear\_multi, \hyperpage{8}
|
||||
\item mp\_cmp, \hyperpage{20}
|
||||
\item mp\_cmp\_d, \hyperpage{21}
|
||||
\item mp\_cmp\_mag, \hyperpage{19}
|
||||
\item mp\_div, \hyperpage{26}
|
||||
\item mp\_div\_2, \hyperpage{22}
|
||||
\item mp\_div\_2d, \hyperpage{24}
|
||||
\item mp\_div\_d, \hyperpage{48}
|
||||
\item mp\_dr\_reduce, \hyperpage{36}
|
||||
\item mp\_dr\_setup, \hyperpage{36}
|
||||
\item MP\_EQ, \hyperpage{18}
|
||||
\item mp\_error\_to\_string, \hyperpage{6}
|
||||
\item mp\_expt\_d, \hyperpage{39}
|
||||
\item mp\_exptmod, \hyperpage{39}
|
||||
\item mp\_exteuclid, \hyperpage{47}
|
||||
\item mp\_gcd, \hyperpage{47}
|
||||
\item mp\_get\_int, \hyperpage{16}
|
||||
\item mp\_grow, \hyperpage{12}
|
||||
\item MP\_GT, \hyperpage{18}
|
||||
\item mp\_init, \hyperpage{7}
|
||||
\item mp\_init\_copy, \hyperpage{9}
|
||||
\item mp\_init\_multi, \hyperpage{8}
|
||||
\item mp\_init\_set, \hyperpage{17}
|
||||
\item mp\_init\_set\_int, \hyperpage{17}
|
||||
\item mp\_init\_size, \hyperpage{10}
|
||||
\item mp\_int, \hyperpage{6}
|
||||
\item mp\_invmod, \hyperpage{48}
|
||||
\item mp\_jacobi, \hyperpage{48}
|
||||
\item mp\_lcm, \hyperpage{47}
|
||||
\item mp\_lshd, \hyperpage{24}
|
||||
\item MP\_LT, \hyperpage{18}
|
||||
\item MP\_MEM, \hyperpage{5}
|
||||
\item mp\_mod, \hyperpage{31}
|
||||
\item mp\_mod\_d, \hyperpage{48}
|
||||
\item mp\_montgomery\_calc\_normalization, \hyperpage{34}
|
||||
\item mp\_montgomery\_reduce, \hyperpage{33}
|
||||
\item mp\_montgomery\_setup, \hyperpage{33}
|
||||
\item mp\_mul, \hyperpage{27}
|
||||
\item mp\_mul\_2, \hyperpage{22}
|
||||
\item mp\_mul\_2d, \hyperpage{24}
|
||||
\item mp\_mul\_d, \hyperpage{48}
|
||||
\item mp\_n\_root, \hyperpage{40}
|
||||
\item mp\_neg, \hyperpage{25}
|
||||
\item MP\_NO, \hyperpage{5}
|
||||
\item MP\_OKAY, \hyperpage{5}
|
||||
\item mp\_or, \hyperpage{25}
|
||||
\item mp\_prime\_fermat, \hyperpage{41}
|
||||
\item mp\_prime\_is\_divisible, \hyperpage{41}
|
||||
\item mp\_prime\_is\_prime, \hyperpage{42}
|
||||
\item mp\_prime\_miller\_rabin, \hyperpage{41}
|
||||
\item mp\_prime\_next\_prime, \hyperpage{42}
|
||||
\item mp\_prime\_rabin\_miller\_trials, \hyperpage{42}
|
||||
\item mp\_prime\_random, \hyperpage{43}
|
||||
\item mp\_prime\_random\_ex, \hyperpage{43}
|
||||
\item mp\_radix\_size, \hyperpage{45}
|
||||
\item mp\_read\_radix, \hyperpage{45}
|
||||
\item mp\_read\_unsigned\_bin, \hyperpage{46}
|
||||
\item mp\_reduce, \hyperpage{32}
|
||||
\item mp\_reduce\_2k, \hyperpage{37}
|
||||
\item mp\_reduce\_2k\_setup, \hyperpage{37}
|
||||
\item mp\_reduce\_setup, \hyperpage{32}
|
||||
\item mp\_rshd, \hyperpage{24}
|
||||
\item mp\_set, \hyperpage{15}
|
||||
\item mp\_set\_int, \hyperpage{16}
|
||||
\item mp\_shrink, \hyperpage{11}
|
||||
\item mp\_sqr, \hyperpage{29}
|
||||
\item mp\_sub, \hyperpage{25}
|
||||
\item mp\_sub\_d, \hyperpage{48}
|
||||
\item mp\_to\_unsigned\_bin, \hyperpage{46}
|
||||
\item mp\_toradix, \hyperpage{45}
|
||||
\item mp\_unsigned\_bin\_size, \hyperpage{46}
|
||||
\item MP\_VAL, \hyperpage{5}
|
||||
\item mp\_xor, \hyperpage{25}
|
||||
\item MP\_YES, \hyperpage{5}
|
||||
\item mp\_add, \hyperpage{29}
|
||||
\item mp\_add\_d, \hyperpage{52}
|
||||
\item mp\_and, \hyperpage{29}
|
||||
\item mp\_clear, \hyperpage{11}
|
||||
\item mp\_clear\_multi, \hyperpage{12}
|
||||
\item mp\_cmp, \hyperpage{24}
|
||||
\item mp\_cmp\_d, \hyperpage{25}
|
||||
\item mp\_cmp\_mag, \hyperpage{23}
|
||||
\item mp\_div, \hyperpage{30}
|
||||
\item mp\_div\_2, \hyperpage{26}
|
||||
\item mp\_div\_2d, \hyperpage{28}
|
||||
\item mp\_div\_d, \hyperpage{52}
|
||||
\item mp\_dr\_reduce, \hyperpage{40}
|
||||
\item mp\_dr\_setup, \hyperpage{40}
|
||||
\item MP\_EQ, \hyperpage{22}
|
||||
\item mp\_error\_to\_string, \hyperpage{10}
|
||||
\item mp\_expt\_d, \hyperpage{43}
|
||||
\item mp\_exptmod, \hyperpage{43}
|
||||
\item mp\_exteuclid, \hyperpage{51}
|
||||
\item mp\_gcd, \hyperpage{51}
|
||||
\item mp\_get\_int, \hyperpage{20}
|
||||
\item mp\_grow, \hyperpage{16}
|
||||
\item MP\_GT, \hyperpage{22}
|
||||
\item mp\_init, \hyperpage{11}
|
||||
\item mp\_init\_copy, \hyperpage{13}
|
||||
\item mp\_init\_multi, \hyperpage{12}
|
||||
\item mp\_init\_set, \hyperpage{21}
|
||||
\item mp\_init\_set\_int, \hyperpage{21}
|
||||
\item mp\_init\_size, \hyperpage{14}
|
||||
\item mp\_int, \hyperpage{10}
|
||||
\item mp\_invmod, \hyperpage{52}
|
||||
\item mp\_jacobi, \hyperpage{52}
|
||||
\item mp\_lcm, \hyperpage{51}
|
||||
\item mp\_lshd, \hyperpage{28}
|
||||
\item MP\_LT, \hyperpage{22}
|
||||
\item MP\_MEM, \hyperpage{9}
|
||||
\item mp\_mod, \hyperpage{35}
|
||||
\item mp\_mod\_d, \hyperpage{52}
|
||||
\item mp\_montgomery\_calc\_normalization, \hyperpage{38}
|
||||
\item mp\_montgomery\_reduce, \hyperpage{37}
|
||||
\item mp\_montgomery\_setup, \hyperpage{37}
|
||||
\item mp\_mul, \hyperpage{31}
|
||||
\item mp\_mul\_2, \hyperpage{26}
|
||||
\item mp\_mul\_2d, \hyperpage{28}
|
||||
\item mp\_mul\_d, \hyperpage{52}
|
||||
\item mp\_n\_root, \hyperpage{44}
|
||||
\item mp\_neg, \hyperpage{29}
|
||||
\item MP\_NO, \hyperpage{9}
|
||||
\item MP\_OKAY, \hyperpage{9}
|
||||
\item mp\_or, \hyperpage{29}
|
||||
\item mp\_prime\_fermat, \hyperpage{45}
|
||||
\item mp\_prime\_is\_divisible, \hyperpage{45}
|
||||
\item mp\_prime\_is\_prime, \hyperpage{46}
|
||||
\item mp\_prime\_miller\_rabin, \hyperpage{45}
|
||||
\item mp\_prime\_next\_prime, \hyperpage{46}
|
||||
\item mp\_prime\_rabin\_miller\_trials, \hyperpage{46}
|
||||
\item mp\_prime\_random, \hyperpage{47}
|
||||
\item mp\_prime\_random\_ex, \hyperpage{47}
|
||||
\item mp\_radix\_size, \hyperpage{49}
|
||||
\item mp\_read\_radix, \hyperpage{49}
|
||||
\item mp\_read\_unsigned\_bin, \hyperpage{50}
|
||||
\item mp\_reduce, \hyperpage{36}
|
||||
\item mp\_reduce\_2k, \hyperpage{41}
|
||||
\item mp\_reduce\_2k\_setup, \hyperpage{41}
|
||||
\item mp\_reduce\_setup, \hyperpage{36}
|
||||
\item mp\_rshd, \hyperpage{28}
|
||||
\item mp\_set, \hyperpage{19}
|
||||
\item mp\_set\_int, \hyperpage{20}
|
||||
\item mp\_shrink, \hyperpage{15}
|
||||
\item mp\_sqr, \hyperpage{33}
|
||||
\item mp\_sub, \hyperpage{29}
|
||||
\item mp\_sub\_d, \hyperpage{52}
|
||||
\item mp\_to\_unsigned\_bin, \hyperpage{50}
|
||||
\item mp\_toradix, \hyperpage{49}
|
||||
\item mp\_unsigned\_bin\_size, \hyperpage{50}
|
||||
\item MP\_VAL, \hyperpage{9}
|
||||
\item mp\_xor, \hyperpage{29}
|
||||
\item MP\_YES, \hyperpage{9}
|
||||
|
||||
\end{theindex}
|
||||
|
123
bn.tex
123
bn.tex
@ -49,7 +49,7 @@
|
||||
\begin{document}
|
||||
\frontmatter
|
||||
\pagestyle{empty}
|
||||
\title{LibTomMath User Manual \\ v0.31}
|
||||
\title{LibTomMath User Manual \\ v0.32}
|
||||
\author{Tom St Denis \\ tomstdenis@iahu.ca}
|
||||
\maketitle
|
||||
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
|
||||
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}
|
||||
make
|
||||
\end{alltt}
|
||||
|
||||
command. This will build the library and archive the object files in ``libtommath.a''. Now you simply link against that
|
||||
and include ``tommath.h'' within your programs.
|
||||
|
||||
Alternatively to build with MSVC type
|
||||
|
||||
command. This will build the library and archive the object files in ``libtommath.a''. Now you link against
|
||||
that and include ``tommath.h'' within your programs. Alternatively to build with MSVC issue the following
|
||||
\begin{alltt}
|
||||
nmake -f makefile.msvc
|
||||
\end{alltt}
|
||||
|
||||
This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC version 6.00
|
||||
with service pack 5.
|
||||
This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC
|
||||
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
|
||||
to work with since it requires the auto-export/import functionality. The resulting DLL and imprt library ``libtomcrypt.dll.a''
|
||||
can be used to link LibTomMath dynamically to any Windows program using Cygwin.
|
||||
\subsection{Shared Libraries}
|
||||
To build as a shared library for GCC issue the following
|
||||
\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}
|
||||
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
|
||||
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}
|
||||
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
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_ERROR_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
static const struct {
|
||||
int code;
|
||||
@ -39,3 +40,4 @@ char *mp_error_to_string(int code)
|
||||
return "Invalid error code";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_FAST_MP_INVMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,12 +14,11 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||
* 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
|
||||
*/
|
||||
int
|
||||
@ -141,3 +142,4 @@ top:
|
||||
__ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,11 +14,10 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* 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
|
||||
* reduction.
|
||||
*
|
||||
@ -165,3 +166,4 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* Fast (comba) multiplier
|
||||
*
|
||||
@ -33,8 +34,9 @@
|
||||
int
|
||||
fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
{
|
||||
int olduse, res, pa, ix;
|
||||
mp_word W[MP_WARRAY];
|
||||
int olduse, res, pa, ix, iz;
|
||||
mp_digit W[MP_WARRAY];
|
||||
register mp_word _W;
|
||||
|
||||
/* grow the destination as required */
|
||||
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) */
|
||||
memset (W, 0, sizeof (mp_word) * digs);
|
||||
/* number of output digits to produce */
|
||||
pa = MIN(digs, a->used + b->used);
|
||||
|
||||
/* calculate the columns */
|
||||
pa = a->used;
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
/* this multiplier has been modified to allow you to
|
||||
* control how many digits of output are produced.
|
||||
* So at most we want to make upto "digs" digits of output.
|
||||
*
|
||||
* 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
|
||||
/* clear the carry */
|
||||
_W = 0;
|
||||
for (ix = 0; ix <= pa; ix++) {
|
||||
int tx, ty;
|
||||
int iy;
|
||||
mp_digit *tmpx, *tmpy;
|
||||
|
||||
/* get offsets into the two bignums */
|
||||
ty = MIN(b->used-1, ix);
|
||||
tx = ix - ty;
|
||||
|
||||
/* setup temp aliases */
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = b->dp + ty;
|
||||
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
{
|
||||
register mp_digit tmpx, *tmpy;
|
||||
register mp_word *_W;
|
||||
register int iy, pb;
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
||||
/* alias for the the word on the left e.g. A[ix] * A[iy] */
|
||||
tmpx = a->dp[ix];
|
||||
|
||||
/* alias for the right side */
|
||||
tmpy = b->dp;
|
||||
|
||||
/* alias for the columns, each step through the loop adds a new
|
||||
term to each column
|
||||
*/
|
||||
_W = W + ix;
|
||||
|
||||
/* the number of digits is limited by their placement. E.g.
|
||||
we avoid multiplying digits that will end up above the # of
|
||||
digits of precision requested
|
||||
*/
|
||||
pb = MIN (b->used, digs - ix);
|
||||
|
||||
for (iy = 0; iy < pb; iy++) {
|
||||
*_W++ += ((mp_word)tmpx) * ((mp_word)*tmpy++);
|
||||
}
|
||||
/* execute loop */
|
||||
for (iz = 0; iz < iy; ++iz) {
|
||||
_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 */
|
||||
@ -93,32 +86,11 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
{
|
||||
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;
|
||||
for (ix = 1; ix < digs; ix++) {
|
||||
/* forward the carry from the previous temp */
|
||||
W[ix] += (W[ix - 1] >> ((mp_word) DIGIT_BIT));
|
||||
|
||||
for (ix = 0; ix < digs; ix++) {
|
||||
/* 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] */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -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 is a library that provides multiple-precision
|
||||
@ -12,10 +14,9 @@
|
||||
*
|
||||
* 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
|
||||
* output digits *above* digs. See the comments for fast_s_mp_mul_digs
|
||||
/* this is a modified version of fast_s_mul_digs that only produces
|
||||
* output digits *above* digs. See the comments for fast_s_mul_digs
|
||||
* to see how it works.
|
||||
*
|
||||
* This is used in the Barrett reduction since for one of the multiplications
|
||||
@ -26,73 +27,69 @@
|
||||
int
|
||||
fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
{
|
||||
int oldused, newused, res, pa, pb, ix;
|
||||
mp_word W[MP_WARRAY];
|
||||
int olduse, res, pa, ix, iz;
|
||||
mp_digit W[MP_WARRAY];
|
||||
mp_word _W;
|
||||
|
||||
/* calculate size of product and allocate more space if required */
|
||||
newused = a->used + b->used + 1;
|
||||
if (c->alloc < newused) {
|
||||
if ((res = mp_grow (c, newused)) != MP_OKAY) {
|
||||
/* grow the destination as required */
|
||||
pa = a->used + b->used;
|
||||
if (c->alloc < pa) {
|
||||
if ((res = mp_grow (c, pa)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* like the other comba method we compute the columns first */
|
||||
pa = a->used;
|
||||
pb = b->used;
|
||||
memset (W + digs, 0, (pa + pb + 1 - digs) * sizeof (mp_word));
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
{
|
||||
register mp_digit tmpx, *tmpy;
|
||||
register int iy;
|
||||
register mp_word *_W;
|
||||
/* number of output digits to produce */
|
||||
pa = a->used + b->used;
|
||||
_W = 0;
|
||||
for (ix = digs; ix <= pa; ix++) {
|
||||
int tx, ty, iy;
|
||||
mp_digit *tmpx, *tmpy;
|
||||
|
||||
/* work todo, that is we only calculate digits that are at "digs" or above */
|
||||
iy = digs - ix;
|
||||
/* get offsets into the two bignums */
|
||||
ty = MIN(b->used-1, ix);
|
||||
tx = ix - ty;
|
||||
|
||||
/* copy of word on the left of A[ix] * B[iy] */
|
||||
tmpx = a->dp[ix];
|
||||
/* setup temp aliases */
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = b->dp + ty;
|
||||
|
||||
/* alias for right side */
|
||||
tmpy = b->dp + iy;
|
||||
|
||||
/* alias for the columns of output. Offset to be equal to or above the
|
||||
* smallest digit place requested
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
_W = W + digs;
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
||||
/* skip cases below zero where ix > digs */
|
||||
if (iy < 0) {
|
||||
iy = abs(iy);
|
||||
tmpy += iy;
|
||||
_W += iy;
|
||||
iy = 0;
|
||||
/* execute loop */
|
||||
for (iz = 0; iz < iy; iz++) {
|
||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
||||
}
|
||||
|
||||
/* compute column products for digits above the minimum */
|
||||
for (; 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 */
|
||||
oldused = c->used;
|
||||
c->used = newused;
|
||||
olduse = c->used;
|
||||
c->used = pa;
|
||||
|
||||
/* now convert the array W downto what we need
|
||||
*
|
||||
* 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));
|
||||
{
|
||||
register mp_digit *tmpc;
|
||||
|
||||
tmpc = c->dp + digs;
|
||||
for (ix = digs; ix <= pa; ix++) {
|
||||
/* now extract the previous digit [below the carry] */
|
||||
*tmpc++ = W[ix];
|
||||
}
|
||||
c->dp[newused - 1] = (mp_digit) (W[newused - 1] & ((mp_word) MP_MASK));
|
||||
|
||||
for (; ix < oldused; ix++) {
|
||||
c->dp[ix] = 0;
|
||||
/* clear unused digits [that existed in the old copy of c] */
|
||||
for (; ix < olduse; ix++) {
|
||||
*tmpc++ = 0;
|
||||
}
|
||||
}
|
||||
mp_clamp (c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_FAST_S_MP_SQR_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* fast squaring
|
||||
*
|
||||
@ -31,109 +32,98 @@
|
||||
* 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 olduse, newused, res, ix, pa;
|
||||
mp_word W2[MP_WARRAY], W[MP_WARRAY];
|
||||
int olduse, res, pa, ix, iz;
|
||||
mp_digit W[MP_WARRAY], *tmpx;
|
||||
mp_word W1;
|
||||
|
||||
/* calculate size of product and allocate as required */
|
||||
pa = a->used;
|
||||
newused = pa + pa + 1;
|
||||
if (b->alloc < newused) {
|
||||
if ((res = mp_grow (b, newused)) != MP_OKAY) {
|
||||
/* grow the destination as required */
|
||||
pa = a->used + a->used;
|
||||
if (b->alloc < pa) {
|
||||
if ((res = mp_grow (b, pa)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* zero temp buffer (columns)
|
||||
* Note that there are two buffers. Since squaring requires
|
||||
* a outer and inner product and the inner product requires
|
||||
* computing a product and doubling it (a relatively expensive
|
||||
* op to perform n**2 times if you don't have to) the inner and
|
||||
* outer products are computed in different buffers. This way
|
||||
* the inner product can be doubled using n doublings instead of
|
||||
* n**2
|
||||
/* number of output digits to produce */
|
||||
W1 = 0;
|
||||
for (ix = 0; ix <= pa; ix++) {
|
||||
int tx, ty, iy;
|
||||
mp_word _W;
|
||||
mp_digit *tmpy;
|
||||
|
||||
/* clear counter */
|
||||
_W = 0;
|
||||
|
||||
/* get offsets into the two bignums */
|
||||
ty = MIN(a->used-1, ix);
|
||||
tx = ix - ty;
|
||||
|
||||
/* setup temp aliases */
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = a->dp + ty;
|
||||
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
memset (W, 0, newused * sizeof (mp_word));
|
||||
memset (W2, 0, newused * sizeof (mp_word));
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
||||
/* This computes the inner product. To simplify the inner N**2 loop
|
||||
* the multiplication by two is done afterwards in the N loop.
|
||||
/* now for squaring tx can never equal ty
|
||||
* we halve the distance since they approach at a rate of 2x
|
||||
* and we have to round because odd cases need to be executed
|
||||
*/
|
||||
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]);
|
||||
iy = MIN(iy, (ty-tx+1)>>1);
|
||||
|
||||
{
|
||||
register mp_digit tmpx, *tmpy;
|
||||
register mp_word *_W;
|
||||
register int iy;
|
||||
|
||||
/* copy of left side */
|
||||
tmpx = a->dp[ix];
|
||||
|
||||
/* alias for right side */
|
||||
tmpy = a->dp + (ix + 1);
|
||||
|
||||
/* the column to store the result in */
|
||||
_W = W + (ix + ix + 1);
|
||||
|
||||
/* inner products */
|
||||
for (iy = ix + 1; iy < pa; iy++) {
|
||||
*_W++ += ((mp_word)tmpx) * ((mp_word)*tmpy++);
|
||||
/* execute loop */
|
||||
for (iz = 0; iz < iy; iz++) {
|
||||
_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 */
|
||||
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;
|
||||
|
||||
/* double first value, since the inner products are
|
||||
* half of what they should be
|
||||
*/
|
||||
W[0] += W[0] + W2[0];
|
||||
|
||||
mp_digit *tmpb;
|
||||
tmpb = b->dp;
|
||||
for (ix = 1; ix < newused; ix++) {
|
||||
/* double/add next digit */
|
||||
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));
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
*tmpb++ = W[ix] & 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++) {
|
||||
*tmpb++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
mp_clamp (b);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_2EXPT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* computes a = 2**b
|
||||
*
|
||||
@ -40,3 +41,4 @@ mp_2expt (mp_int * a, int b)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_ABS_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* b = |a|
|
||||
*
|
||||
@ -35,3 +36,4 @@ mp_abs (mp_int * a, mp_int * b)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_ADD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* high level addition (handles signs) */
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_ADD_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* single digit addition */
|
||||
int
|
||||
@ -101,3 +102,4 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_ADDMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* d = a + b (mod c) */
|
||||
int
|
||||
@ -33,3 +34,4 @@ mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_AND_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* AND two ints together */
|
||||
int
|
||||
@ -49,3 +50,4 @@ mp_and (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_clear (&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CLAMP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* trim unused digits
|
||||
*
|
||||
@ -36,3 +37,4 @@ mp_clamp (mp_int * a)
|
||||
a->sign = MP_ZPOS;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CLEAR_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* clear one (frees) */
|
||||
void
|
||||
@ -36,3 +37,4 @@ mp_clear (mp_int * a)
|
||||
a->sign = MP_ZPOS;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CLEAR_MULTI_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
void mp_clear_multi(mp_int *mp, ...)
|
||||
@ -26,3 +27,4 @@ void mp_clear_multi(mp_int *mp, ...)
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CMP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* compare two ints (signed)*/
|
||||
int
|
||||
@ -35,3 +36,4 @@ mp_cmp (mp_int * a, mp_int * b)
|
||||
return mp_cmp_mag(a, b);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CMP_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* compare a digit */
|
||||
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;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CMP_MAG_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* compare maginitude of two ints (unsigned) */
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_CNT_LSB_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
static const int lnz[16] = {
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_COPY_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* copy, b = a */
|
||||
int
|
||||
@ -60,3 +61,4 @@ mp_copy (mp_int * a, mp_int * b)
|
||||
b->sign = a->sign;
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_COUNT_BITS_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* returns the number of bits in an int */
|
||||
int
|
||||
@ -37,3 +38,4 @@ mp_count_bits (mp_int * a)
|
||||
}
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
79
bn_mp_div.c
79
bn_mp_div.c
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DIV_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,78 @@
|
||||
*
|
||||
* 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.
|
||||
* c*b + d == a [e.g. a/b, c=quotient, d=remainder]
|
||||
@ -209,3 +282,7 @@ __T1:mp_clear (&t1);
|
||||
__Q:mp_clear (&q);
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DIV_2_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* b = a/2 */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DIV_2D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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) */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DIV_3_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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) */
|
||||
int
|
||||
@ -71,3 +72,4 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DIV_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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)
|
||||
{
|
||||
@ -54,7 +55,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
||||
/* power of two ? */
|
||||
if (s_is_power_of_two(b, &ix) == 1) {
|
||||
if (d != NULL) {
|
||||
*d = a->dp[0] & ((1<<ix) - 1);
|
||||
*d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
|
||||
}
|
||||
if (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;
|
||||
}
|
||||
|
||||
#ifdef BN_MP_DIV_3_C
|
||||
/* three? */
|
||||
if (b == 3) {
|
||||
return mp_div_3(a, c, d);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* no easy answer [c'est la vie]. Just division */
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DR_IS_MODULUS_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* determines if a number is a valid DR modulus */
|
||||
int mp_dr_is_modulus(mp_int *a)
|
||||
@ -35,3 +36,4 @@ int mp_dr_is_modulus(mp_int *a)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DR_REDUCE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
|
||||
*
|
||||
@ -86,3 +87,4 @@ top:
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_DR_SETUP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* determines the setup value */
|
||||
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]));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_EXCH_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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
|
||||
* mp_int pointers around
|
||||
@ -26,3 +27,4 @@ mp_exch (mp_int * a, mp_int * b)
|
||||
*a = *b;
|
||||
*b = t;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_EXPT_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* calculate c = a**b using a square-multiply algorithm */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_EXPTMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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
|
||||
@ -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 (X->sign == MP_NEG) {
|
||||
#ifdef BN_MP_INVMOD_C
|
||||
mp_int tmpG, tmpX;
|
||||
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);
|
||||
mp_clear_multi(&tmpG, &tmpX, NULL);
|
||||
return err;
|
||||
#else
|
||||
/* no invmod */
|
||||
return MP_VAL
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef BN_MP_DR_IS_MODULUS_C
|
||||
/* is it a DR modulus? */
|
||||
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 (dr == 0) {
|
||||
dr = mp_reduce_is_2k(P) << 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 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) {
|
||||
return mp_exptmod_fast (G, X, P, Y, dr);
|
||||
} else {
|
||||
#endif
|
||||
#ifdef BN_S_MP_EXPTMOD_C
|
||||
/* otherwise use the generic Barrett reduction technique */
|
||||
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
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_EXPTMOD_FAST_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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
|
||||
*
|
||||
@ -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 */
|
||||
if (redmode == 0) {
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
/* now setup montgomery */
|
||||
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
||||
goto __M;
|
||||
}
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto __M;
|
||||
#endif
|
||||
|
||||
/* 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) &&
|
||||
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||
redux = fast_mp_montgomery_reduce;
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
||||
/* use slower baseline Montgomery method */
|
||||
redux = mp_montgomery_reduce;
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto __M;
|
||||
#endif
|
||||
}
|
||||
} 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 */
|
||||
mp_dr_setup(P, &mp);
|
||||
redux = mp_dr_reduce;
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto __M;
|
||||
#endif
|
||||
} 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 */
|
||||
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
|
||||
goto __M;
|
||||
}
|
||||
redux = mp_reduce_2k;
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto __M;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* 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
|
||||
*
|
||||
* 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]
|
||||
*/
|
||||
|
||||
if (redmode == 0) {
|
||||
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
||||
/* now we need R mod m */
|
||||
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||
goto __RES;
|
||||
}
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto __RES;
|
||||
#endif
|
||||
|
||||
/* now set M[1] to G * R mod m */
|
||||
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
|
||||
* of R.
|
||||
*/
|
||||
if ((err = mp_montgomery_reduce (&res, P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||
goto __RES;
|
||||
}
|
||||
}
|
||||
@ -285,3 +314,5 @@ __M:
|
||||
}
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_EXTEUCLID_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* Extended euclidean algorithm of (a, b) produces
|
||||
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);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_FREAD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* read a bigint from a file stream in ASCII */
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_FWRITE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_GCD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* Greatest Common Divisor using the binary method */
|
||||
int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||
@ -105,3 +106,4 @@ __V:mp_clear (&u);
|
||||
__U:mp_clear (&v);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_GET_INT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* get the lower 32-bits of an mp_int */
|
||||
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 */
|
||||
return res & 0xFFFFFFFFUL;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_GROW_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* grow as required */
|
||||
int mp_grow (mp_int * a, int size)
|
||||
@ -49,3 +50,4 @@ int mp_grow (mp_int * a, int size)
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INIT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* init a new mp_int */
|
||||
int mp_init (mp_int * a)
|
||||
@ -38,3 +39,4 @@ int mp_init (mp_int * a)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INIT_COPY_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* creates "a" then copies b into it */
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INIT_MULTI_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
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. */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INIT_SET_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* initialize and set a digit */
|
||||
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);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INIT_SET_INT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* initialize and set a digit */
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INIT_SIZE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,22 +14,31 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* init an mp_init for a given size */
|
||||
int mp_init_size (mp_int * a, int size)
|
||||
{
|
||||
int x;
|
||||
|
||||
/* pad size so there are always extra digits */
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
/* 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) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
||||
/* set the members */
|
||||
a->used = 0;
|
||||
a->alloc = size;
|
||||
a->sign = MP_ZPOS;
|
||||
|
||||
/* zero the digits */
|
||||
for (x = 0; x < size; x++) {
|
||||
a->dp[x] = 0;
|
||||
}
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
153
bn_mp_invmod.c
153
bn_mp_invmod.c
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_INVMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,163 +14,26 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* hac 14.61, pp608 */
|
||||
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 */
|
||||
if (b->sign == MP_NEG || mp_iszero(b) == 1) {
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
#ifdef BN_FAST_MP_INVMOD_C
|
||||
/* if the modulus is odd we can use a faster routine instead */
|
||||
if (mp_isodd (b) == 1) {
|
||||
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;
|
||||
}
|
||||
#ifdef BN_MP_INVMOD_SLOW_C
|
||||
return mp_invmod_slow(a, b, c);
|
||||
#endif
|
||||
|
||||
/* 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;
|
||||
return MP_VAL;
|
||||
}
|
||||
#endif
|
||||
|
171
bn_mp_invmod_slow.c
Normal file
171
bn_mp_invmod_slow.c
Normal 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
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_IS_SQUARE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* Check if remainders are possible squares - fast exclude non-squares */
|
||||
static const char rem_128[128] = {
|
||||
@ -69,7 +70,7 @@ int mp_is_square(mp_int *arg,int *ret)
|
||||
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) {
|
||||
return res;
|
||||
}
|
||||
@ -101,3 +102,4 @@ int mp_is_square(mp_int *arg,int *ret)
|
||||
ERR:mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_JACOBI_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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)
|
||||
* HAC pp. 73 Algorithm 2.149
|
||||
@ -97,3 +98,4 @@ __P1:mp_clear (&p1);
|
||||
__A1:mp_clear (&a1);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* c = |a| * |b| using Karatsuba Multiplication using
|
||||
* three half size multiplications
|
||||
@ -159,3 +160,4 @@ X0:mp_clear (&x0);
|
||||
ERR:
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_KARATSUBA_SQR_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,12 +14,11 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* Karatsuba squaring, computes b = a*a using three
|
||||
* 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
|
||||
* tuned to perform recursive squarings.
|
||||
*/
|
||||
@ -113,3 +114,4 @@ X0:mp_clear (&x0);
|
||||
ERR:
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_LCM_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* computes least common multiple as |a*b|/(a, b) */
|
||||
int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
|
||||
@ -52,3 +53,4 @@ __T:
|
||||
mp_clear_multi (&t1, &t2, NULL);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_LSHD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* shift left a certain amount of digits */
|
||||
int mp_lshd (mp_int * a, int b)
|
||||
@ -59,3 +60,4 @@ int mp_lshd (mp_int * a, int b)
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* c = a mod b, 0 <= c < b */
|
||||
int
|
||||
@ -40,3 +41,4 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MOD_2D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* calc a value mod 2**b */
|
||||
int
|
||||
@ -47,3 +48,4 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
||||
mp_clamp (c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MOD_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,10 +14,10 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
int
|
||||
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
|
||||
{
|
||||
return mp_div_d(a, b, NULL, c);
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,30 +14,30 @@
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The method is slightly modified to shift B unconditionally upto just under
|
||||
* the leading bit of b. This saves alot of multiple precision shifting.
|
||||
*/
|
||||
int
|
||||
mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
||||
int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
||||
{
|
||||
int x, bits, res;
|
||||
|
||||
/* how many bits of last digit does b use */
|
||||
bits = mp_count_bits (b) % DIGIT_BIT;
|
||||
|
||||
/* compute A = B^(n-1) * 2^(bits-1) */
|
||||
|
||||
if (b->used > 1) {
|
||||
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 */
|
||||
for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
|
||||
@ -51,3 +53,4 @@ mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
|
||||
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?
|
||||
*
|
||||
* 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
|
||||
* 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
|
||||
*
|
||||
* 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
|
||||
* following inner loop to reduce the
|
||||
* 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;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* setups the montgomery reduction stuff */
|
||||
int
|
||||
@ -47,7 +48,8 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho)
|
||||
#endif
|
||||
|
||||
/* 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;
|
||||
}
|
||||
#endif
|
||||
|
26
bn_mp_mul.c
26
bn_mp_mul.c
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MUL_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* high level multiplication (handles sign) */
|
||||
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;
|
||||
|
||||
/* use Toom-Cook? */
|
||||
#ifdef BN_MP_TOOM_MUL_C
|
||||
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||
res = mp_toom_mul(a, b, c);
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||
/* 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);
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
/* can we use the fast multiplier?
|
||||
*
|
||||
* 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;
|
||||
|
||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||
if ((digs < MP_WARRAY) &&
|
||||
MIN(a->used, b->used) <=
|
||||
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||
res = fast_s_mp_mul_digs (a, b, c, digs);
|
||||
} else {
|
||||
res = s_mp_mul (a, b, c);
|
||||
}
|
||||
} else
|
||||
#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;
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MUL_2_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* b = a*2 */
|
||||
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;
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MUL_2D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* shift left by a certain bit count */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MUL_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* multiply by a digit */
|
||||
int
|
||||
@ -70,3 +71,4 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_MULMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* d = a * b (mod c) */
|
||||
int
|
||||
@ -33,3 +34,4 @@ mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_N_ROOT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* find the n'th root of an integer
|
||||
*
|
||||
@ -124,3 +125,4 @@ __T2:mp_clear (&t2);
|
||||
__T1:mp_clear (&t1);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_NEG_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* b = -a */
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_OR_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* OR two ints together */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_FERMAT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* performs one Fermat test.
|
||||
*
|
||||
@ -54,3 +55,4 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
||||
__T:mp_clear (&t);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* determines if an integers is divisible by one
|
||||
* 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;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_IS_PRIME_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,12 +14,11 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* performs a variable number of rounds of Miller-Rabin
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
@ -75,3 +76,4 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
||||
__B:mp_clear (&b);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_MILLER_RABIN_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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
|
||||
* HAC pp. 139 Algorithm 4.24
|
||||
@ -95,3 +96,4 @@ __R:mp_clear (&r);
|
||||
__N1:mp_clear (&n1);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_NEXT_PRIME_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* finds the next prime after the number "a" using "t" trials
|
||||
* of Miller-Rabin.
|
||||
@ -162,3 +163,4 @@ __ERR:
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,9 +14,8 @@
|
||||
*
|
||||
* 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 {
|
||||
int k, t;
|
||||
} sizes[] = {
|
||||
@ -25,13 +26,8 @@ static const struct {
|
||||
{ 640, 6 },
|
||||
{ 768, 5 },
|
||||
{ 896, 4 },
|
||||
{ 1024, 4 },
|
||||
{ 1152, 3 },
|
||||
{ 1280, 3 },
|
||||
{ 1408, 3 },
|
||||
{ 1536, 3 },
|
||||
{ 1664, 3 },
|
||||
{ 1792, 2 } };
|
||||
{ 1024, 4 }
|
||||
};
|
||||
|
||||
/* returns # of RM trials required for a given bit 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 1;
|
||||
return sizes[x-1].t + 1;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_PRIME_RANDOM_EX_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* 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? */
|
||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
||||
if (res == MP_NO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (flags & LTM_PRIME_SAFE) {
|
||||
/* see if (a-1)/2 is prime */
|
||||
@ -116,3 +120,4 @@ error:
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_RADIX_SIZE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* returns size of ASCII reprensentation */
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_RADIX_SMAP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,7 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* chars used in radix conversions */
|
||||
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_RAND_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* makes a pseudo-random int of a given size */
|
||||
int
|
||||
@ -47,3 +48,4 @@ mp_rand (mp_int * a, int digits)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_READ_RADIX_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* read a string [ASCII] in a given 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;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_READ_SIGNED_BIN_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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 */
|
||||
int
|
||||
@ -34,3 +35,4 @@ mp_read_signed_bin (mp_int * a, unsigned char *b, int c)
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_READ_UNSIGNED_BIN_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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] */
|
||||
int
|
||||
@ -48,3 +49,4 @@ mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
|
||||
mp_clamp (a);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_REDUCE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
||||
* precomputed via mp_reduce_setup.
|
||||
@ -38,9 +39,20 @@ mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
goto CLEANUP;
|
||||
}
|
||||
} else {
|
||||
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
|
||||
if ((res = s_mp_mul_high_digs (&q, mu, &q, um - 1)) != MP_OKAY) {
|
||||
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) */
|
||||
@ -82,3 +94,4 @@ CLEANUP:
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_REDUCE_2K_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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 */
|
||||
int
|
||||
@ -54,3 +55,4 @@ ERR:
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_REDUCE_2K_SETUP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* determines the setup value */
|
||||
int
|
||||
@ -40,3 +41,4 @@ mp_reduce_2k_setup(mp_int *a, mp_digit *d)
|
||||
mp_clear(&tmp);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_REDUCE_IS_2K_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* determines if mp_reduce_2k can be used */
|
||||
int mp_reduce_is_2k(mp_int *a)
|
||||
@ -44,3 +45,4 @@ int mp_reduce_is_2k(mp_int *a)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_REDUCE_SETUP_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,13 +14,11 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* pre-calculate the value required for Barrett reduction
|
||||
* For a given modulus "b" it calulates the value required in "a"
|
||||
*/
|
||||
int
|
||||
mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
int mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
@ -27,3 +27,4 @@ mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
}
|
||||
return mp_div (a, b, a, NULL);
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_RSHD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* shift right a certain amount of digits */
|
||||
void mp_rshd (mp_int * a, int b)
|
||||
@ -64,3 +65,4 @@ void mp_rshd (mp_int * a, int b)
|
||||
/* remove excess digits */
|
||||
a->used -= b;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SET_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* set to a digit */
|
||||
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->used = (a->dp[0] != 0) ? 1 : 0;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SET_INT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* set a 32-bit const */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SHRINK_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* shrink a bignum */
|
||||
int mp_shrink (mp_int * a)
|
||||
@ -27,3 +28,4 @@ int mp_shrink (mp_int * a)
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SIGNED_BIN_SIZE_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,10 +14,10 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* get the size for an signed equivalent */
|
||||
int mp_signed_bin_size (mp_int * a)
|
||||
{
|
||||
return 1 + mp_unsigned_bin_size (a);
|
||||
}
|
||||
#endif
|
||||
|
23
bn_mp_sqr.c
23
bn_mp_sqr.c
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SQR_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* computes b = a*a */
|
||||
int
|
||||
@ -20,22 +21,34 @@ mp_sqr (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
#ifdef BN_MP_TOOM_SQR_C
|
||||
/* use Toom-Cook? */
|
||||
if (a->used >= TOOM_SQR_CUTOFF) {
|
||||
res = mp_toom_sqr(a, b);
|
||||
/* 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);
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_FAST_S_MP_SQR_C
|
||||
/* can we use the fast comba multiplier? */
|
||||
if ((a->used * 2 + 1) < MP_WARRAY &&
|
||||
a->used <
|
||||
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
|
||||
res = fast_s_mp_sqr (a, b);
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_S_MP_SQR_C
|
||||
res = s_mp_sqr (a, b);
|
||||
}
|
||||
#else
|
||||
res = MP_VAL;
|
||||
#endif
|
||||
}
|
||||
b->sign = MP_ZPOS;
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SQRMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* c = a * a (mod b) */
|
||||
int
|
||||
@ -33,3 +34,4 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SQRT_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* 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 */
|
||||
int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
@ -73,3 +74,4 @@ E2: mp_clear(&t1);
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SUB_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* high level subtraction (handles signs) */
|
||||
int
|
||||
@ -51,3 +52,4 @@ mp_sub (mp_int * a, mp_int * b, mp_int * c)
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SUB_D_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* single digit subtraction */
|
||||
int
|
||||
@ -81,3 +82,4 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_SUBMOD_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* d = a - b (mod c) */
|
||||
int
|
||||
@ -34,3 +35,4 @@ mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <tommath.h>
|
||||
#ifdef BN_MP_TO_SIGNED_BIN_C
|
||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||
*
|
||||
* LibTomMath is a library that provides multiple-precision
|
||||
@ -12,7 +14,6 @@
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
||||
/* store in signed [big endian] format */
|
||||
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);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user