added libtommath-0.34

This commit is contained in:
Tom St Denis 2005-02-12 08:40:15 +00:00 committed by Steffen Jaeckel
parent 4b7111d96e
commit 3d0fcaab0a
55 changed files with 5477 additions and 3867 deletions

BIN
bn.pdf

Binary file not shown.

29
bn.tex
View File

@ -49,7 +49,7 @@
\begin{document} \begin{document}
\frontmatter \frontmatter
\pagestyle{empty} \pagestyle{empty}
\title{LibTomMath User Manual \\ v0.33} \title{LibTomMath User Manual \\ v0.34}
\author{Tom St Denis \\ tomstdenis@iahu.ca} \author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle \maketitle
This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
@ -263,12 +263,12 @@ are the pros and cons of LibTomMath by comparing it to the math routines from Gn
\begin{center} \begin{center}
\begin{tabular}{|l|c|c|l|} \begin{tabular}{|l|c|c|l|}
\hline \textbf{Criteria} & \textbf{Pro} & \textbf{Con} & \textbf{Notes} \\ \hline \textbf{Criteria} & \textbf{Pro} & \textbf{Con} & \textbf{Notes} \\
\hline Few lines of code per file & X & & GnuPG $ = 300.9$, LibTomMath $ = 76.04$ \\ \hline Few lines of code per file & X & & GnuPG $ = 300.9$, LibTomMath $ = 71.97$ \\
\hline Commented function prototypes & X && GnuPG function names are cryptic. \\ \hline Commented function prototypes & X && GnuPG function names are cryptic. \\
\hline Speed && X & LibTomMath is slower. \\ \hline Speed && X & LibTomMath is slower. \\
\hline Totally free & X & & GPL has unfavourable restrictions.\\ \hline Totally free & X & & GPL has unfavourable restrictions.\\
\hline Large function base & X & & GnuPG is barebones. \\ \hline Large function base & X & & GnuPG is barebones. \\
\hline Four modular reduction algorithms & X & & Faster modular exponentiation. \\ \hline Five modular reduction algorithms & X & & Faster modular exponentiation for a variety of moduli. \\
\hline Portable & X & & GnuPG requires configuration to build. \\ \hline Portable & X & & GnuPG requires configuration to build. \\
\hline \hline
\end{tabular} \end{tabular}
@ -284,9 +284,12 @@ would require when working with large integers.
So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in your So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in your
own application but I think there are reasons not to. While LibTomMath is slower than libraries such as GnuMP it is own application but I think there are reasons not to. While LibTomMath is slower than libraries such as GnuMP it is
not normally significantly slower. On x86 machines the difference is normally a factor of two when performing modular not normally significantly slower. On x86 machines the difference is normally a factor of two when performing modular
exponentiations. exponentiations. It depends largely on the processor, compiler and the moduli being used.
Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern. Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern. However,
on the other side of the coin LibTomMath offers you a totally free (public domain) well structured math library
that is very flexible, complete and performs well in resource contrained environments. Fast RSA for example can
be performed with as little as 8KB of ram for data (again depending on build options).
\chapter{Getting Started with LibTomMath} \chapter{Getting Started with LibTomMath}
\section{Building Programs} \section{Building Programs}
@ -809,7 +812,7 @@ mp\_int variables based on their digits only.
\index{mp\_cmp\_mag} \index{mp\_cmp\_mag}
\begin{alltt} \begin{alltt}
int mp_cmp(mp_int * a, mp_int * b); int mp_cmp_mag(mp_int * a, mp_int * b);
\end{alltt} \end{alltt}
This will compare $a$ to $b$ placing $a$ to the left of $b$. This function cannot fail and will return one of the This will compare $a$ to $b$ placing $a$ to the left of $b$. This function cannot fail and will return one of the
three compare codes listed in figure \ref{fig:CMP}. three compare codes listed in figure \ref{fig:CMP}.
@ -1220,12 +1223,13 @@ int mp_sqr (mp_int * a, mp_int * b);
\end{alltt} \end{alltt}
Will square $a$ and store it in $b$. Like the case of multiplication there are four different squaring Will square $a$ and store it in $b$. Like the case of multiplication there are four different squaring
algorithms all which can be called from mp\_sqr(). It is ideal to use mp\_sqr over mp\_mul when squaring terms. algorithms all which can be called from mp\_sqr(). It is ideal to use mp\_sqr over mp\_mul when squaring terms because
of the speed difference.
\section{Tuning Polynomial Basis Routines} \section{Tuning Polynomial Basis Routines}
Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that
the Comba and baseline algorithms use. At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectfully they require the Comba and baseline algorithms use. At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectively they require
considerably less work. For example, a 10000-digit multiplication would take roughly 724,000 single precision considerably less work. For example, a 10000-digit multiplication would take roughly 724,000 single precision
multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor
of 138). of 138).
@ -1297,14 +1301,14 @@ of $b$. This algorithm accepts an input $a$ of any range and is not limited by
\section{Barrett Reduction} \section{Barrett Reduction}
Barrett reduction is a generic optimized reduction algorithm that requires pre--computation to achieve Barrett reduction is a generic optimized reduction algorithm that requires pre--computation to achieve
a decent speedup over straight division. First a $mu$ value must be precomputed with the following function. a decent speedup over straight division. First a $\mu$ value must be precomputed with the following function.
\index{mp\_reduce\_setup} \index{mp\_reduce\_setup}
\begin{alltt} \begin{alltt}
int mp_reduce_setup(mp_int *a, mp_int *b); int mp_reduce_setup(mp_int *a, mp_int *b);
\end{alltt} \end{alltt}
Given a modulus in $b$ this produces the required $mu$ value in $a$. For any given modulus this only has to Given a modulus in $b$ this produces the required $\mu$ value in $a$. For any given modulus this only has to
be computed once. Modular reduction can now be performed with the following. be computed once. Modular reduction can now be performed with the following.
\index{mp\_reduce} \index{mp\_reduce}
@ -1312,7 +1316,7 @@ be computed once. Modular reduction can now be performed with the following.
int mp_reduce(mp_int *a, mp_int *b, mp_int *c); int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
\end{alltt} \end{alltt}
This will reduce $a$ in place modulo $b$ with the precomputed $mu$ value in $c$. $a$ must be in the range This will reduce $a$ in place modulo $b$ with the precomputed $\mu$ value in $c$. $a$ must be in the range
$0 \le a < b^2$. $0 \le a < b^2$.
\begin{alltt} \begin{alltt}
@ -1578,7 +1582,8 @@ will return $-2$.
This algorithm uses the ``Newton Approximation'' method and will converge on the correct root fairly quickly. Since This algorithm uses the ``Newton Approximation'' method and will converge on the correct root fairly quickly. Since
the algorithm requires raising $a$ to the power of $b$ it is not ideal to attempt to find roots for large the algorithm requires raising $a$ to the power of $b$ it is not ideal to attempt to find roots for large
values of $b$. If particularly large roots are required then a factor method could be used instead. For example, values of $b$. If particularly large roots are required then a factor method could be used instead. For example,
$a^{1/16}$ is equivalent to $\left (a^{1/4} \right)^{1/4}$. $a^{1/16}$ is equivalent to $\left (a^{1/4} \right)^{1/4}$ or simply
$\left ( \left ( \left ( a^{1/2} \right )^{1/2} \right )^{1/2} \right )^{1/2}$
\chapter{Prime Numbers} \chapter{Prime Numbers}
\section{Trial Division} \section{Trial Division}

View File

@ -21,8 +21,7 @@
* Based on slow invmod except this is optimized for the case where b is * Based on slow invmod except this is optimized for the case where b is
* odd as per HAC Note 14.64 on pp. 610 * odd as per HAC Note 14.64 on pp. 610
*/ */
int int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
{ {
mp_int x, y, u, v, B, D; mp_int x, y, u, v, B, D;
int res, neg; int res, neg;

View File

@ -23,8 +23,7 @@
* *
* Based on Algorithm 14.32 on pp.601 of HAC. * Based on Algorithm 14.32 on pp.601 of HAC.
*/ */
int int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
{ {
int ix, res, olduse; int ix, res, olduse;
mp_word W[MP_WARRAY]; mp_word W[MP_WARRAY];

View File

@ -31,8 +31,7 @@
* Based on Algorithm 14.12 on pp.595 of HAC. * Based on Algorithm 14.12 on pp.595 of HAC.
* *
*/ */
int int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY]; mp_digit W[MP_WARRAY];
@ -81,7 +80,7 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
/* store final carry */ /* store final carry */
W[ix] = _W; W[ix] = _W & MP_MASK;
/* setup dest */ /* setup dest */
olduse = c->used; olduse = c->used;

View File

@ -24,8 +24,7 @@
* *
* Based on Algorithm 14.12 on pp.595 of HAC. * Based on Algorithm 14.12 on pp.595 of HAC.
*/ */
int int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY]; mp_digit W[MP_WARRAY];
@ -72,7 +71,7 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
/* store final carry */ /* store final carry */
W[ix] = _W; W[ix] = _W & MP_MASK;
/* setup dest */ /* setup dest */
olduse = c->used; olduse = c->used;

View File

@ -101,7 +101,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
} }
/* store it */ /* store it */
W[ix] = _W; W[ix] = _W & MP_MASK;
/* make next carry */ /* make next carry */
W1 = _W >> ((mp_word)DIGIT_BIT); W1 = _W >> ((mp_word)DIGIT_BIT);

View File

@ -65,21 +65,29 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
#endif #endif
} }
/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C)
if (mp_reduce_is_2k_l(P) == MP_YES) {
return s_mp_exptmod(G, X, P, Y, 1);
}
#endif
#ifdef BN_MP_DR_IS_MODULUS_C #ifdef BN_MP_DR_IS_MODULUS_C
/* is it a DR modulus? */ /* is it a DR modulus? */
dr = mp_dr_is_modulus(P); dr = mp_dr_is_modulus(P);
#else #else
/* default to no */
dr = 0; dr = 0;
#endif #endif
#ifdef BN_MP_REDUCE_IS_2K_C #ifdef BN_MP_REDUCE_IS_2K_C
/* if not, is it a uDR modulus? */ /* if not, is it a unrestricted DR modulus? */
if (dr == 0) { if (dr == 0) {
dr = mp_reduce_is_2k(P) << 1; dr = mp_reduce_is_2k(P) << 1;
} }
#endif #endif
/* if the modulus is odd or dr != 0 use the fast method */ /* if the modulus is odd or dr != 0 use the montgomery method */
#ifdef BN_MP_EXPTMOD_FAST_C #ifdef BN_MP_EXPTMOD_FAST_C
if (mp_isodd (P) == 1 || dr != 0) { if (mp_isodd (P) == 1 || dr != 0) {
return mp_exptmod_fast (G, X, P, Y, dr); return mp_exptmod_fast (G, X, P, Y, dr);
@ -87,7 +95,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
#endif #endif
#ifdef BN_S_MP_EXPTMOD_C #ifdef BN_S_MP_EXPTMOD_C
/* otherwise use the generic Barrett reduction technique */ /* otherwise use the generic Barrett reduction technique */
return s_mp_exptmod (G, X, P, Y); return s_mp_exptmod (G, X, P, Y, 0);
#else #else
/* no exptmod for evens */ /* no exptmod for evens */
return MP_VAL; return MP_VAL;

View File

@ -29,8 +29,7 @@
#define TAB_SIZE 256 #define TAB_SIZE 256
#endif #endif
int int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
{ {
mp_int M[TAB_SIZE], res; mp_int M[TAB_SIZE], res;
mp_digit buf, mp; mp_digit buf, mp;

View File

@ -57,8 +57,9 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
} }
/* store final carry [if any] */ /* store final carry [if any] and increment ix offset */
*tmpc++ = u; *tmpc++ = u;
++ix;
/* now zero digits above the top */ /* now zero digits above the top */
while (ix++ < olduse) { while (ix++ < olduse) {

View File

@ -60,7 +60,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
/* calc the maskOR_msb */ /* calc the maskOR_msb */
maskOR_msb = 0; maskOR_msb = 0;
maskOR_msb_offset = (size - 2) >> 3; maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
if (flags & LTM_PRIME_2MSB_ON) { if (flags & LTM_PRIME_2MSB_ON) {
maskOR_msb |= 1 << ((size - 2) & 7); maskOR_msb |= 1 << ((size - 2) & 7);
} else if (flags & LTM_PRIME_2MSB_OFF) { } else if (flags & LTM_PRIME_2MSB_OFF) {
@ -68,7 +68,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
} }
/* get the maskOR_lsb */ /* get the maskOR_lsb */
maskOR_lsb = 0; maskOR_lsb = 1;
if (flags & LTM_PRIME_BBS) { if (flags & LTM_PRIME_BBS) {
maskOR_lsb |= 3; maskOR_lsb |= 3;
} }

View File

@ -16,7 +16,7 @@
*/ */
/* read a string [ASCII] in a given radix */ /* read a string [ASCII] in a given radix */
int mp_read_radix (mp_int * a, char *str, int radix) int mp_read_radix (mp_int * a, const char *str, int radix)
{ {
int y, res, neg; int y, res, neg;
char ch; char ch;

View File

@ -19,8 +19,7 @@
* precomputed via mp_reduce_setup. * precomputed via mp_reduce_setup.
* From HAC pp.604 Algorithm 14.42 * From HAC pp.604 Algorithm 14.42
*/ */
int int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
{ {
mp_int q; mp_int q;
int res, um = m->used; int res, um = m->used;

View File

@ -16,8 +16,7 @@
*/ */
/* reduces a modulo n where n is of the form 2**p - d */ /* reduces a modulo n where n is of the form 2**p - d */
int int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
{ {
mp_int q; mp_int q;
int p, res; int p, res;

58
bn_mp_reduce_2k_l.c Normal file
View File

@ -0,0 +1,58 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_L_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
*/
/* reduces a modulo n where n is of the form 2**p - d
This differs from reduce_2k since "d" can be larger
than a single digit.
*/
int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
{
mp_int q;
int p, res;
if ((res = mp_init(&q)) != MP_OKAY) {
return res;
}
p = mp_count_bits(n);
top:
/* q = a/2**p, a = a mod 2**p */
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto ERR;
}
/* q = q * d */
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
goto ERR;
}
/* a = a + q */
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR;
}
if (mp_cmp_mag(a, n) != MP_LT) {
s_mp_sub(a, n, a);
goto top;
}
ERR:
mp_clear(&q);
return res;
}
#endif

View File

@ -16,8 +16,7 @@
*/ */
/* determines the setup value */ /* determines the setup value */
int int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
mp_reduce_2k_setup(mp_int *a, mp_digit *d)
{ {
int res, p; int res, p;
mp_int tmp; mp_int tmp;

40
bn_mp_reduce_2k_setup_l.c Normal file
View File

@ -0,0 +1,40 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_L_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
*/
/* determines the setup value */
int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
{
int res;
mp_int tmp;
if ((res = mp_init(&tmp)) != MP_OKAY) {
return res;
}
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
goto ERR;
}
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
goto ERR;
}
ERR:
mp_clear(&tmp);
return res;
}
#endif

View File

@ -22,9 +22,9 @@ int mp_reduce_is_2k(mp_int *a)
mp_digit iz; mp_digit iz;
if (a->used == 0) { if (a->used == 0) {
return 0; return MP_NO;
} else if (a->used == 1) { } else if (a->used == 1) {
return 1; return MP_YES;
} else if (a->used > 1) { } else if (a->used > 1) {
iy = mp_count_bits(a); iy = mp_count_bits(a);
iz = 1; iz = 1;
@ -33,7 +33,7 @@ int mp_reduce_is_2k(mp_int *a)
/* Test every bit from the second digit up, must be 1 */ /* Test every bit from the second digit up, must be 1 */
for (ix = DIGIT_BIT; ix < iy; ix++) { for (ix = DIGIT_BIT; ix < iy; ix++) {
if ((a->dp[iw] & iz) == 0) { if ((a->dp[iw] & iz) == 0) {
return 0; return MP_NO;
} }
iz <<= 1; iz <<= 1;
if (iz > (mp_digit)MP_MASK) { if (iz > (mp_digit)MP_MASK) {
@ -42,7 +42,7 @@ int mp_reduce_is_2k(mp_int *a)
} }
} }
} }
return 1; return MP_YES;
} }
#endif #endif

40
bn_mp_reduce_is_2k_l.c Normal file
View File

@ -0,0 +1,40 @@
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_L_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
*/
/* determines if reduce_2k_l can be used */
int mp_reduce_is_2k_l(mp_int *a)
{
int ix, iy;
if (a->used == 0) {
return MP_NO;
} else if (a->used == 1) {
return MP_YES;
} else if (a->used > 1) {
/* if more than half of the digits are -1 we're sold */
for (iy = ix = 0; ix < a->used; ix++) {
if (a->dp[ix] == MP_MASK) {
++iy;
}
}
return (iy >= (a->used/2)) ? MP_YES : MP_NO;
}
return MP_NO;
}
#endif

View File

@ -16,8 +16,7 @@
*/ */
/* store in signed [big endian] format */ /* store in signed [big endian] format */
int int mp_to_signed_bin (mp_int * a, unsigned char *b)
mp_to_signed_bin (mp_int * a, unsigned char *b)
{ {
int res; int res;

27
bn_mp_to_signed_bin_n.c Normal file
View File

@ -0,0 +1,27 @@
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_N_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
*/
/* store in signed [big endian] format */
int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
{
if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
return MP_VAL;
}
*outlen = mp_signed_bin_size(a);
return mp_to_signed_bin(a, b);
}
#endif

View File

@ -16,8 +16,7 @@
*/ */
/* store in unsigned [big endian] format */ /* store in unsigned [big endian] format */
int int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
mp_to_unsigned_bin (mp_int * a, unsigned char *b)
{ {
int x, res; int x, res;
mp_int t; mp_int t;

27
bn_mp_to_unsigned_bin_n.c Normal file
View File

@ -0,0 +1,27 @@
#include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_N_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
*/
/* store in unsigned [big endian] format */
int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
{
if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
return MP_VAL;
}
*outlen = mp_unsigned_bin_size(a);
return mp_to_unsigned_bin(a, b);
}
#endif

View File

@ -16,8 +16,7 @@
*/ */
/* get the size for an unsigned equivalent */ /* get the size for an unsigned equivalent */
int int mp_unsigned_bin_size (mp_int * a)
mp_unsigned_bin_size (mp_int * a)
{ {
int size = mp_count_bits (a); int size = mp_count_bits (a);
return (size / 8 + ((size & 7) != 0 ? 1 : 0)); return (size / 8 + ((size & 7) != 0 ? 1 : 0));

View File

@ -21,11 +21,12 @@
#define TAB_SIZE 256 #define TAB_SIZE 256
#endif #endif
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
{ {
mp_int M[TAB_SIZE], res, mu; mp_int M[TAB_SIZE], res, mu;
mp_digit buf; mp_digit buf;
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
int (*redux)(mp_int*,mp_int*,mp_int*);
/* find window size */ /* find window size */
x = mp_count_bits (X); x = mp_count_bits (X);
@ -72,8 +73,17 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_init (&mu)) != MP_OKAY) { if ((err = mp_init (&mu)) != MP_OKAY) {
goto LBL_M; goto LBL_M;
} }
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
goto LBL_MU; if (redmode == 0) {
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
goto LBL_MU;
}
redux = mp_reduce;
} else {
if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
goto LBL_MU;
}
redux = mp_reduce_2k_l;
} }
/* create M table /* create M table
@ -96,11 +106,14 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
} }
for (x = 0; x < (winsize - 1); x++) { for (x = 0; x < (winsize - 1); x++) {
/* square it */
if ((err = mp_sqr (&M[1 << (winsize - 1)], if ((err = mp_sqr (&M[1 << (winsize - 1)],
&M[1 << (winsize - 1)])) != MP_OKAY) { &M[1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
/* reduce modulo P */
if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
} }
@ -112,7 +125,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) { if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
} }
@ -161,7 +174,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
continue; continue;
@ -178,7 +191,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
} }
@ -187,7 +200,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
@ -205,7 +218,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
@ -215,7 +228,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
} }

View File

@ -20,11 +20,12 @@
CPU /Compiler /MUL CUTOFF/SQR CUTOFF CPU /Compiler /MUL CUTOFF/SQR CUTOFF
------------------------------------------------------------- -------------------------------------------------------------
Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-) Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-)
AMD Athlon64 /GCC v3.4.4 / 74/ 124/LTM 0.34
*/ */
int KARATSUBA_MUL_CUTOFF = 88, /* Min. number of digits before Karatsuba multiplication is used. */ int KARATSUBA_MUL_CUTOFF = 74, /* Min. number of digits before Karatsuba multiplication is used. */
KARATSUBA_SQR_CUTOFF = 128, /* Min. number of digits before Karatsuba squaring is used. */ KARATSUBA_SQR_CUTOFF = 124, /* Min. number of digits before Karatsuba squaring is used. */
TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */ TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
TOOM_SQR_CUTOFF = 400; TOOM_SQR_CUTOFF = 400;

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,15 @@
February 12th, 2005
v0.34 -- Fixed two more small errors in mp_prime_random_ex()
-- Fixed overflow in mp_mul_d() [Kevin Kenny]
-- Added mp_to_(un)signed_bin_n() functions which do bounds checking for ya [and report the size]
-- Added "large" diminished radix support. Speeds up things like DSA where the moduli is of the form 2^k - P for some P < 2^(k/2) or so
Actually is faster than Montgomery on my AMD64 (and probably much faster on a P4)
-- Updated the manual a bit
-- Ok so I haven't done the textbook work yet... My current freelance gig has landed me in France till the
end of Feb/05. Once I get back I'll have tons of free time and I plan to go to town on the book.
As of this release the API will freeze. At least until the book catches up with all the changes. I welcome
bug reports but new algorithms will have to wait.
December 23rd, 2004 December 23rd, 2004
v0.33 -- Fixed "small" variant for mp_div() which would munge with negative dividends... v0.33 -- Fixed "small" variant for mp_div() which would munge with negative dividends...
-- Fixed bug in mp_prime_random_ex() which would set the most significant byte to zero when -- Fixed bug in mp_prime_random_ex() which would set the most significant byte to zero when

File diff suppressed because it is too large Load Diff

View File

@ -11,15 +11,16 @@ ulong64 _tt;
#endif #endif
void ndraw(mp_int *a, char *name) void ndraw(mp_int * a, char *name)
{ {
char buf[4096]; char buf[4096];
printf("%s: ", name); printf("%s: ", name);
mp_toradix(a, buf, 64); mp_toradix(a, buf, 64);
printf("%s\n", buf); printf("%s\n", buf);
} }
static void draw(mp_int *a) static void draw(mp_int * a)
{ {
ndraw(a, ""); ndraw(a, "");
} }
@ -39,35 +40,38 @@ int lbit(void)
} }
/* RDTSC from Scott Duplichan */ /* RDTSC from Scott Duplichan */
static ulong64 TIMFUNC (void) static ulong64 TIMFUNC(void)
{ {
#if defined __GNUC__ #if defined __GNUC__
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
unsigned long long a; unsigned long long a;
__asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx"); __asm__ __volatile__("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::
return a; "m"(a):"%eax", "%edx");
#else /* gcc-IA64 version */ return a;
unsigned long result; #else /* gcc-IA64 version */
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); unsigned long result;
while (__builtin_expect ((int) result == -1, 0)) __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory");
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
return result; while (__builtin_expect((int) result == -1, 0))
#endif __asm__ __volatile__("mov %0=ar.itc":"=r"(result)::"memory");
return result;
#endif
// Microsoft and Intel Windows compilers // Microsoft and Intel Windows compilers
#elif defined _M_IX86 #elif defined _M_IX86
__asm rdtsc __asm rdtsc
#elif defined _M_AMD64 #elif defined _M_AMD64
return __rdtsc (); return __rdtsc();
#elif defined _M_IA64 #elif defined _M_IA64
#if defined __INTEL_COMPILER #if defined __INTEL_COMPILER
#include <ia64intrin.h> #include <ia64intrin.h>
#endif #endif
return __getReg (3116); return __getReg(3116);
#else #else
#error need rdtsc function for this build #error need rdtsc function for this build
#endif #endif
} }
#define DO(x) x; x; #define DO(x) x; x;
//#define DO4(x) DO2(x); DO2(x); //#define DO4(x) DO2(x); DO2(x);
@ -77,7 +81,7 @@ static ulong64 TIMFUNC (void)
int main(void) int main(void)
{ {
ulong64 tt, gg, CLK_PER_SEC; ulong64 tt, gg, CLK_PER_SEC;
FILE *log, *logb, *logc; FILE *log, *logb, *logc, *logd;
mp_int a, b, c, d, e, f; mp_int a, b, c, d, e, f;
int n, cnt, ix, old_kara_m, old_kara_s; int n, cnt, ix, old_kara_m, old_kara_s;
unsigned rr; unsigned rr;
@ -92,166 +96,189 @@ int main(void)
srand(time(NULL)); srand(time(NULL));
/* temp. turn off TOOM */ /* temp. turn off TOOM */
TOOM_MUL_CUTOFF = TOOM_SQR_CUTOFF = 100000; TOOM_MUL_CUTOFF = TOOM_SQR_CUTOFF = 100000;
CLK_PER_SEC = TIMFUNC(); CLK_PER_SEC = TIMFUNC();
sleep(1); sleep(1);
CLK_PER_SEC = TIMFUNC() - CLK_PER_SEC; CLK_PER_SEC = TIMFUNC() - CLK_PER_SEC;
printf("CLK_PER_SEC == %llu\n", CLK_PER_SEC); printf("CLK_PER_SEC == %llu\n", CLK_PER_SEC);
goto exptmod;
log = fopen("logs/add.log", "w");
for (cnt = 8; cnt <= 128; cnt += 8) {
SLEEP;
mp_rand(&a, cnt);
mp_rand(&b, cnt);
rr = 0;
tt = -1;
do {
gg = TIMFUNC();
DO(mp_add(&a, &b, &c));
gg = (TIMFUNC() - gg) >> 1;
if (tt > gg)
tt = gg;
} while (++rr < 100000);
printf("Adding\t\t%4d-bit => %9llu/sec, %9llu cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt);
fflush(log);
}
fclose(log);
log = fopen("logs/add.log", "w"); log = fopen("logs/sub.log", "w");
for (cnt = 8; cnt <= 128; cnt += 8) { for (cnt = 8; cnt <= 128; cnt += 8) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
mp_rand(&b, cnt); mp_rand(&b, cnt);
rr = 0; rr = 0;
tt = -1; tt = -1;
do { do {
gg = TIMFUNC(); gg = TIMFUNC();
DO(mp_add(&a,&b,&c)); DO(mp_sub(&a, &b, &c));
gg = (TIMFUNC() - gg)>>1; gg = (TIMFUNC() - gg) >> 1;
if (tt > gg) tt = gg; if (tt > gg)
} while (++rr < 100000); tt = gg;
printf("Adding\t\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC/tt, tt); } while (++rr < 100000);
fprintf(log, "%d %9llu\n", cnt*DIGIT_BIT, tt); fflush(log);
}
fclose(log);
log = fopen("logs/sub.log", "w"); printf("Subtracting\t\t%4d-bit => %9llu/sec, %9llu cycles\n",
for (cnt = 8; cnt <= 128; cnt += 8) { mp_count_bits(&a), CLK_PER_SEC / tt, tt);
SLEEP; fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt);
mp_rand(&a, cnt); fflush(log);
mp_rand(&b, cnt); }
rr = 0; fclose(log);
tt = -1;
do {
gg = TIMFUNC();
DO(mp_sub(&a,&b,&c));
gg = (TIMFUNC() - gg)>>1;
if (tt > gg) tt = gg;
} while (++rr < 100000);
printf("Subtracting\t\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC/tt, tt);
fprintf(log, "%d %9llu\n", cnt*DIGIT_BIT, tt); fflush(log);
}
fclose(log);
/* do mult/square twice, first without karatsuba and second with */ /* do mult/square twice, first without karatsuba and second with */
multtest:
old_kara_m = KARATSUBA_MUL_CUTOFF; old_kara_m = KARATSUBA_MUL_CUTOFF;
old_kara_s = KARATSUBA_SQR_CUTOFF; old_kara_s = KARATSUBA_SQR_CUTOFF;
for (ix = 0; ix < 1; ix++) { for (ix = 0; ix < 2; ix++) {
printf("With%s Karatsuba\n", (ix==0)?"out":""); printf("With%s Karatsuba\n", (ix == 0) ? "out" : "");
KARATSUBA_MUL_CUTOFF = (ix==0)?9999:old_kara_m; KARATSUBA_MUL_CUTOFF = (ix == 0) ? 9999 : old_kara_m;
KARATSUBA_SQR_CUTOFF = (ix==0)?9999:old_kara_s; KARATSUBA_SQR_CUTOFF = (ix == 0) ? 9999 : old_kara_s;
log = fopen((ix==0)?"logs/mult.log":"logs/mult_kara.log", "w"); log = fopen((ix == 0) ? "logs/mult.log" : "logs/mult_kara.log", "w");
for (cnt = 4; cnt <= 288; cnt += 2) { for (cnt = 4; cnt <= 10240 / DIGIT_BIT; cnt += 2) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
mp_rand(&b, cnt); mp_rand(&b, cnt);
rr = 0; rr = 0;
tt = -1; tt = -1;
do { do {
gg = TIMFUNC(); gg = TIMFUNC();
DO(mp_mul(&a, &b, &c)); DO(mp_mul(&a, &b, &c));
gg = (TIMFUNC() - gg)>>1; gg = (TIMFUNC() - gg) >> 1;
if (tt > gg) tt = gg; if (tt > gg)
} while (++rr < 100); tt = gg;
printf("Multiplying\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC/tt, tt); } while (++rr < 100);
fprintf(log, "%d %9llu\n", mp_count_bits(&a), tt); fflush(log); printf("Multiplying\t%4d-bit => %9llu/sec, %9llu cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
fprintf(log, "%d %9llu\n", mp_count_bits(&a), tt);
fflush(log);
} }
fclose(log); fclose(log);
log = fopen((ix==0)?"logs/sqr.log":"logs/sqr_kara.log", "w"); log = fopen((ix == 0) ? "logs/sqr.log" : "logs/sqr_kara.log", "w");
for (cnt = 4; cnt <= 288; cnt += 2) { for (cnt = 4; cnt <= 10240 / DIGIT_BIT; cnt += 2) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
rr = 0; rr = 0;
tt = -1; tt = -1;
do { do {
gg = TIMFUNC(); gg = TIMFUNC();
DO(mp_sqr(&a, &b)); DO(mp_sqr(&a, &b));
gg = (TIMFUNC() - gg)>>1; gg = (TIMFUNC() - gg) >> 1;
if (tt > gg) tt = gg; if (tt > gg)
} while (++rr < 100); tt = gg;
printf("Squaring\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC/tt, tt); } while (++rr < 100);
fprintf(log, "%d %9llu\n", mp_count_bits(&a), tt); fflush(log); printf("Squaring\t%4d-bit => %9llu/sec, %9llu cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
fprintf(log, "%d %9llu\n", mp_count_bits(&a), tt);
fflush(log);
} }
fclose(log); fclose(log);
} }
exptmod:
{ {
char *primes[] = { char *primes[] = {
/* 2K moduli mersenne primes */ /* 2K large moduli */
"6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", "179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586239334100047359817950870678242457666208137217",
"531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127", "32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638099733077152121140120031150424541696791951097529546801429027668869927491725169",
"10407932194664399081925240327364085538615262247266704805319112350403608059673360298012239441732324184842421613954281007791383566248323464908139906605677320762924129509389220345773183349661583550472959420547689811211693677147548478866962501384438260291732348885311160828538416585028255604666224831890918801847068222203140521026698435488732958028878050869736186900714720710555703168729087", "1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085902995208257421855249796721729039744118165938433694823325696642096892124547425283",
"1475979915214180235084898622737381736312066145333169775147771216478570297878078949377407337049389289382748507531496480477281264838760259191814463365330269540496961201113430156902396093989090226259326935025281409614983499388222831448598601834318536230923772641390209490231836446899608210795482963763094236630945410832793769905399982457186322944729636418890623372171723742105636440368218459649632948538696905872650486914434637457507280441823676813517852099348660847172579408422316678097670224011990280170474894487426924742108823536808485072502240519452587542875349976558572670229633962575212637477897785501552646522609988869914013540483809865681250419497686697771007", /* 2K moduli mersenne primes */
"259117086013202627776246767922441530941818887553125427303974923161874019266586362086201209516800483406550695241733194177441689509238807017410377709597512042313066624082916353517952311186154862265604547691127595848775610568757931191017711408826252153849035830401185072116424747461823031471398340229288074545677907941037288235820705892351068433882986888616658650280927692080339605869308790500409503709875902119018371991620994002568935113136548829739112656797303241986517250116412703509705427773477972349821676443446668383119322540099648994051790241624056519054483690809616061625743042361721863339415852426431208737266591962061753535748892894599629195183082621860853400937932839420261866586142503251450773096274235376822938649407127700846077124211823080804139298087057504713825264571448379371125032081826126566649084251699453951887789613650248405739378594599444335231188280123660406262468609212150349937584782292237144339628858485938215738821232393687046160677362909315071", "6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151",
"190797007524439073807468042969529173669356994749940177394741882673528979787005053706368049835514900244303495954950709725762186311224148828811920216904542206960744666169364221195289538436845390250168663932838805192055137154390912666527533007309292687539092257043362517857366624699975402375462954490293259233303137330643531556539739921926201438606439020075174723029056838272505051571967594608350063404495977660656269020823960825567012344189908927956646011998057988548630107637380993519826582389781888135705408653045219655801758081251164080554609057468028203308718724654081055323215860189611391296030471108443146745671967766308925858547271507311563765171008318248647110097614890313562856541784154881743146033909602737947385055355960331855614540900081456378659068370317267696980001187750995491090350108417050917991562167972281070161305972518044872048331306383715094854938415738549894606070722584737978176686422134354526989443028353644037187375385397838259511833166416134323695660367676897722287918773420968982326089026150031515424165462111337527431154890666327374921446276833564519776797633875503548665093914556482031482248883127023777039667707976559857333357013727342079099064400455741830654320379350833236245819348824064783585692924881021978332974949906122664421376034687815350484991", "531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127",
"10407932194664399081925240327364085538615262247266704805319112350403608059673360298012239441732324184842421613954281007791383566248323464908139906605677320762924129509389220345773183349661583550472959420547689811211693677147548478866962501384438260291732348885311160828538416585028255604666224831890918801847068222203140521026698435488732958028878050869736186900714720710555703168729087",
"1475979915214180235084898622737381736312066145333169775147771216478570297878078949377407337049389289382748507531496480477281264838760259191814463365330269540496961201113430156902396093989090226259326935025281409614983499388222831448598601834318536230923772641390209490231836446899608210795482963763094236630945410832793769905399982457186322944729636418890623372171723742105636440368218459649632948538696905872650486914434637457507280441823676813517852099348660847172579408422316678097670224011990280170474894487426924742108823536808485072502240519452587542875349976558572670229633962575212637477897785501552646522609988869914013540483809865681250419497686697771007",
"259117086013202627776246767922441530941818887553125427303974923161874019266586362086201209516800483406550695241733194177441689509238807017410377709597512042313066624082916353517952311186154862265604547691127595848775610568757931191017711408826252153849035830401185072116424747461823031471398340229288074545677907941037288235820705892351068433882986888616658650280927692080339605869308790500409503709875902119018371991620994002568935113136548829739112656797303241986517250116412703509705427773477972349821676443446668383119322540099648994051790241624056519054483690809616061625743042361721863339415852426431208737266591962061753535748892894599629195183082621860853400937932839420261866586142503251450773096274235376822938649407127700846077124211823080804139298087057504713825264571448379371125032081826126566649084251699453951887789613650248405739378594599444335231188280123660406262468609212150349937584782292237144339628858485938215738821232393687046160677362909315071",
"190797007524439073807468042969529173669356994749940177394741882673528979787005053706368049835514900244303495954950709725762186311224148828811920216904542206960744666169364221195289538436845390250168663932838805192055137154390912666527533007309292687539092257043362517857366624699975402375462954490293259233303137330643531556539739921926201438606439020075174723029056838272505051571967594608350063404495977660656269020823960825567012344189908927956646011998057988548630107637380993519826582389781888135705408653045219655801758081251164080554609057468028203308718724654081055323215860189611391296030471108443146745671967766308925858547271507311563765171008318248647110097614890313562856541784154881743146033909602737947385055355960331855614540900081456378659068370317267696980001187750995491090350108417050917991562167972281070161305972518044872048331306383715094854938415738549894606070722584737978176686422134354526989443028353644037187375385397838259511833166416134323695660367676897722287918773420968982326089026150031515424165462111337527431154890666327374921446276833564519776797633875503548665093914556482031482248883127023777039667707976559857333357013727342079099064400455741830654320379350833236245819348824064783585692924881021978332974949906122664421376034687815350484991",
/* DR moduli */ /* DR moduli */
"14059105607947488696282932836518693308967803494693489478439861164411992439598399594747002144074658928593502845729752797260025831423419686528151609940203368612079", "14059105607947488696282932836518693308967803494693489478439861164411992439598399594747002144074658928593502845729752797260025831423419686528151609940203368612079",
"101745825697019260773923519755878567461315282017759829107608914364075275235254395622580447400994175578963163918967182013639660669771108475957692810857098847138903161308502419410142185759152435680068435915159402496058513611411688900243039", "101745825697019260773923519755878567461315282017759829107608914364075275235254395622580447400994175578963163918967182013639660669771108475957692810857098847138903161308502419410142185759152435680068435915159402496058513611411688900243039",
"736335108039604595805923406147184530889923370574768772191969612422073040099331944991573923112581267542507986451953227192970402893063850485730703075899286013451337291468249027691733891486704001513279827771740183629161065194874727962517148100775228363421083691764065477590823919364012917984605619526140821797602431", "736335108039604595805923406147184530889923370574768772191969612422073040099331944991573923112581267542507986451953227192970402893063850485730703075899286013451337291468249027691733891486704001513279827771740183629161065194874727962517148100775228363421083691764065477590823919364012917984605619526140821797602431",
"38564998830736521417281865696453025806593491967131023221754800625044118265468851210705360385717536794615180260494208076605798671660719333199513807806252394423283413430106003596332513246682903994829528690198205120921557533726473585751382193953592127439965050261476810842071573684505878854588706623484573925925903505747545471088867712185004135201289273405614415899438276535626346098904241020877974002916168099951885406379295536200413493190419727789712076165162175783", "38564998830736521417281865696453025806593491967131023221754800625044118265468851210705360385717536794615180260494208076605798671660719333199513807806252394423283413430106003596332513246682903994829528690198205120921557533726473585751382193953592127439965050261476810842071573684505878854588706623484573925925903505747545471088867712185004135201289273405614415899438276535626346098904241020877974002916168099951885406379295536200413493190419727789712076165162175783",
"542189391331696172661670440619180536749994166415993334151601745392193484590296600979602378676624808129613777993466242203025054573692562689251250471628358318743978285860720148446448885701001277560572526947619392551574490839286458454994488665744991822837769918095117129546414124448777033941223565831420390846864429504774477949153794689948747680362212954278693335653935890352619041936727463717926744868338358149568368643403037768649616778526013610493696186055899318268339432671541328195724261329606699831016666359440874843103020666106568222401047720269951530296879490444224546654729111504346660859907296364097126834834235287147", "542189391331696172661670440619180536749994166415993334151601745392193484590296600979602378676624808129613777993466242203025054573692562689251250471628358318743978285860720148446448885701001277560572526947619392551574490839286458454994488665744991822837769918095117129546414124448777033941223565831420390846864429504774477949153794689948747680362212954278693335653935890352619041936727463717926744868338358149568368643403037768649616778526013610493696186055899318268339432671541328195724261329606699831016666359440874843103020666106568222401047720269951530296879490444224546654729111504346660859907296364097126834834235287147",
"1487259134814709264092032648525971038895865645148901180585340454985524155135260217788758027400478312256339496385275012465661575576202252063145698732079880294664220579764848767704076761853197216563262660046602703973050798218246170835962005598561669706844469447435461092542265792444947706769615695252256130901271870341005768912974433684521436211263358097522726462083917939091760026658925757076733484173202927141441492573799914240222628795405623953109131594523623353044898339481494120112723445689647986475279242446083151413667587008191682564376412347964146113898565886683139407005941383669325997475076910488086663256335689181157957571445067490187939553165903773554290260531009121879044170766615232300936675369451260747671432073394867530820527479172464106442450727640226503746586340279816318821395210726268291535648506190714616083163403189943334431056876038286530365757187367147446004855912033137386225053275419626102417236133948503", "1487259134814709264092032648525971038895865645148901180585340454985524155135260217788758027400478312256339496385275012465661575576202252063145698732079880294664220579764848767704076761853197216563262660046602703973050798218246170835962005598561669706844469447435461092542265792444947706769615695252256130901271870341005768912974433684521436211263358097522726462083917939091760026658925757076733484173202927141441492573799914240222628795405623953109131594523623353044898339481494120112723445689647986475279242446083151413667587008191682564376412347964146113898565886683139407005941383669325997475076910488086663256335689181157957571445067490187939553165903773554290260531009121879044170766615232300936675369451260747671432073394867530820527479172464106442450727640226503746586340279816318821395210726268291535648506190714616083163403189943334431056876038286530365757187367147446004855912033137386225053275419626102417236133948503",
"1095121115716677802856811290392395128588168592409109494900178008967955253005183831872715423151551999734857184538199864469605657805519106717529655044054833197687459782636297255219742994736751541815269727940751860670268774903340296040006114013971309257028332849679096824800250742691718610670812374272414086863715763724622797509437062518082383056050144624962776302147890521249477060215148275163688301275847155316042279405557632639366066847442861422164832655874655824221577849928863023018366835675399949740429332468186340518172487073360822220449055340582568461568645259954873303616953776393853174845132081121976327462740354930744487429617202585015510744298530101547706821590188733515880733527449780963163909830077616357506845523215289297624086914545378511082534229620116563260168494523906566709418166011112754529766183554579321224940951177394088465596712620076240067370589036924024728375076210477267488679008016579588696191194060127319035195370137160936882402244399699172017835144537488486396906144217720028992863941288217185353914991583400421682751000603596655790990815525126154394344641336397793791497068253936771017031980867706707490224041075826337383538651825493679503771934836094655802776331664261631740148281763487765852746577808019633679", "1095121115716677802856811290392395128588168592409109494900178008967955253005183831872715423151551999734857184538199864469605657805519106717529655044054833197687459782636297255219742994736751541815269727940751860670268774903340296040006114013971309257028332849679096824800250742691718610670812374272414086863715763724622797509437062518082383056050144624962776302147890521249477060215148275163688301275847155316042279405557632639366066847442861422164832655874655824221577849928863023018366835675399949740429332468186340518172487073360822220449055340582568461568645259954873303616953776393853174845132081121976327462740354930744487429617202585015510744298530101547706821590188733515880733527449780963163909830077616357506845523215289297624086914545378511082534229620116563260168494523906566709418166011112754529766183554579321224940951177394088465596712620076240067370589036924024728375076210477267488679008016579588696191194060127319035195370137160936882402244399699172017835144537488486396906144217720028992863941288217185353914991583400421682751000603596655790990815525126154394344641336397793791497068253936771017031980867706707490224041075826337383538651825493679503771934836094655802776331664261631740148281763487765852746577808019633679",
/* generic unrestricted moduli */ /* generic unrestricted moduli */
"17933601194860113372237070562165128350027320072176844226673287945873370751245439587792371960615073855669274087805055507977323024886880985062002853331424203", "17933601194860113372237070562165128350027320072176844226673287945873370751245439587792371960615073855669274087805055507977323024886880985062002853331424203",
"2893527720709661239493896562339544088620375736490408468011883030469939904368086092336458298221245707898933583190713188177399401852627749210994595974791782790253946539043962213027074922559572312141181787434278708783207966459019479487", "2893527720709661239493896562339544088620375736490408468011883030469939904368086092336458298221245707898933583190713188177399401852627749210994595974791782790253946539043962213027074922559572312141181787434278708783207966459019479487",
"347743159439876626079252796797422223177535447388206607607181663903045907591201940478223621722118173270898487582987137708656414344685816179420855160986340457973820182883508387588163122354089264395604796675278966117567294812714812796820596564876450716066283126720010859041484786529056457896367683122960411136319", "347743159439876626079252796797422223177535447388206607607181663903045907591201940478223621722118173270898487582987137708656414344685816179420855160986340457973820182883508387588163122354089264395604796675278966117567294812714812796820596564876450716066283126720010859041484786529056457896367683122960411136319",
"47266428956356393164697365098120418976400602706072312735924071745438532218237979333351774907308168340693326687317443721193266215155735814510792148768576498491199122744351399489453533553203833318691678263241941706256996197460424029012419012634671862283532342656309677173602509498417976091509154360039893165037637034737020327399910409885798185771003505320583967737293415979917317338985837385734747478364242020380416892056650841470869294527543597349250299539682430605173321029026555546832473048600327036845781970289288898317888427517364945316709081173840186150794397479045034008257793436817683392375274635794835245695887", "47266428956356393164697365098120418976400602706072312735924071745438532218237979333351774907308168340693326687317443721193266215155735814510792148768576498491199122744351399489453533553203833318691678263241941706256996197460424029012419012634671862283532342656309677173602509498417976091509154360039893165037637034737020327399910409885798185771003505320583967737293415979917317338985837385734747478364242020380416892056650841470869294527543597349250299539682430605173321029026555546832473048600327036845781970289288898317888427517364945316709081173840186150794397479045034008257793436817683392375274635794835245695887",
"436463808505957768574894870394349739623346440601945961161254440072143298152040105676491048248110146278752857839930515766167441407021501229924721335644557342265864606569000117714935185566842453630868849121480179691838399545644365571106757731317371758557990781880691336695584799313313687287468894148823761785582982549586183756806449017542622267874275103877481475534991201849912222670102069951687572917937634467778042874315463238062009202992087620963771759666448266532858079402669920025224220613419441069718482837399612644978839925207109870840278194042158748845445131729137117098529028886770063736487420613144045836803985635654192482395882603511950547826439092832800532152534003936926017612446606135655146445620623395788978726744728503058670046885876251527122350275750995227", "436463808505957768574894870394349739623346440601945961161254440072143298152040105676491048248110146278752857839930515766167441407021501229924721335644557342265864606569000117714935185566842453630868849121480179691838399545644365571106757731317371758557990781880691336695584799313313687287468894148823761785582982549586183756806449017542622267874275103877481475534991201849912222670102069951687572917937634467778042874315463238062009202992087620963771759666448266532858079402669920025224220613419441069718482837399612644978839925207109870840278194042158748845445131729137117098529028886770063736487420613144045836803985635654192482395882603511950547826439092832800532152534003936926017612446606135655146445620623395788978726744728503058670046885876251527122350275750995227",
"11424167473351836398078306042624362277956429440521137061889702611766348760692206243140413411077394583180726863277012016602279290144126785129569474909173584789822341986742719230331946072730319555984484911716797058875905400999504305877245849119687509023232790273637466821052576859232452982061831009770786031785669030271542286603956118755585683996118896215213488875253101894663403069677745948305893849505434201763745232895780711972432011344857521691017896316861403206449421332243658855453435784006517202894181640562433575390821384210960117518650374602256601091379644034244332285065935413233557998331562749140202965844219336298970011513882564935538704289446968322281451907487362046511461221329799897350993370560697505809686438782036235372137015731304779072430260986460269894522159103008260495503005267165927542949439526272736586626709581721032189532726389643625590680105784844246152702670169304203783072275089194754889511973916207", "11424167473351836398078306042624362277956429440521137061889702611766348760692206243140413411077394583180726863277012016602279290144126785129569474909173584789822341986742719230331946072730319555984484911716797058875905400999504305877245849119687509023232790273637466821052576859232452982061831009770786031785669030271542286603956118755585683996118896215213488875253101894663403069677745948305893849505434201763745232895780711972432011344857521691017896316861403206449421332243658855453435784006517202894181640562433575390821384210960117518650374602256601091379644034244332285065935413233557998331562749140202965844219336298970011513882564935538704289446968322281451907487362046511461221329799897350993370560697505809686438782036235372137015731304779072430260986460269894522159103008260495503005267165927542949439526272736586626709581721032189532726389643625590680105784844246152702670169304203783072275089194754889511973916207",
"1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979", "1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979",
NULL NULL
}; };
log = fopen("logs/expt.log", "w"); log = fopen("logs/expt.log", "w");
logb = fopen("logs/expt_dr.log", "w"); logb = fopen("logs/expt_dr.log", "w");
logc = fopen("logs/expt_2k.log", "w"); logc = fopen("logs/expt_2k.log", "w");
for (n = 0; primes[n]; n++) { logd = fopen("logs/expt_2kl.log", "w");
SLEEP; for (n = 0; primes[n]; n++) {
mp_read_radix(&a, primes[n], 10); SLEEP;
mp_zero(&b); mp_read_radix(&a, primes[n], 10);
for (rr = 0; rr < (unsigned)mp_count_bits(&a); rr++) { mp_zero(&b);
mp_mul_2(&b, &b); for (rr = 0; rr < (unsigned) mp_count_bits(&a); rr++) {
b.dp[0] |= lbit(); mp_mul_2(&b, &b);
b.used += 1; b.dp[0] |= lbit();
b.used += 1;
}
mp_sub_d(&a, 1, &c);
mp_mod(&b, &c, &b);
mp_set(&c, 3);
rr = 0;
tt = -1;
do {
gg = TIMFUNC();
DO(mp_exptmod(&c, &b, &a, &d));
gg = (TIMFUNC() - gg) >> 1;
if (tt > gg)
tt = gg;
} while (++rr < 10);
mp_sub_d(&a, 1, &e);
mp_sub(&e, &b, &b);
mp_exptmod(&c, &b, &a, &e); /* c^(p-1-b) mod a */
mp_mulmod(&e, &d, &a, &d); /* c^b * c^(p-1-b) == c^p-1 == 1 */
if (mp_cmp_d(&d, 1)) {
printf("Different (%d)!!!\n", mp_count_bits(&a));
draw(&d);
exit(0);
}
printf("Exponentiating\t%4d-bit => %9llu/sec, %9llu cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
fprintf(n < 4 ? logd : (n < 9) ? logc : (n < 16) ? logb : log,
"%d %9llu\n", mp_count_bits(&a), tt);
} }
mp_sub_d(&a, 1, &c);
mp_mod(&b, &c, &b);
mp_set(&c, 3);
rr = 0;
tt = -1;
do {
gg = TIMFUNC();
DO(mp_exptmod(&c, &b, &a, &d));
gg = (TIMFUNC() - gg)>>1;
if (tt > gg) tt = gg;
} while (++rr < 10);
mp_sub_d(&a, 1, &e);
mp_sub(&e, &b, &b);
mp_exptmod(&c, &b, &a, &e); /* c^(p-1-b) mod a */
mp_mulmod(&e, &d, &a, &d); /* c^b * c^(p-1-b) == c^p-1 == 1 */
if (mp_cmp_d(&d, 1)) {
printf("Different (%d)!!!\n", mp_count_bits(&a));
draw(&d);
exit(0);
}
printf("Exponentiating\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC/tt, tt);
fprintf((n < 6) ? logc : (n < 13) ? logb : log, "%d %9llu\n", mp_count_bits(&a), tt);
}
} }
fclose(log); fclose(log);
fclose(logb); fclose(logb);
fclose(logc); fclose(logc);
fclose(logd);
log = fopen("logs/invmod.log", "w"); log = fopen("logs/invmod.log", "w");
for (cnt = 4; cnt <= 128; cnt += 4) { for (cnt = 4; cnt <= 128; cnt += 4) {
@ -260,28 +287,29 @@ int main(void)
mp_rand(&b, cnt); mp_rand(&b, cnt);
do { do {
mp_add_d(&b, 1, &b); mp_add_d(&b, 1, &b);
mp_gcd(&a, &b, &c); mp_gcd(&a, &b, &c);
} while (mp_cmp_d(&c, 1) != MP_EQ); } while (mp_cmp_d(&c, 1) != MP_EQ);
rr = 0; rr = 0;
tt = -1; tt = -1;
do { do {
gg = TIMFUNC(); gg = TIMFUNC();
DO(mp_invmod(&b, &a, &c)); DO(mp_invmod(&b, &a, &c));
gg = (TIMFUNC() - gg)>>1; gg = (TIMFUNC() - gg) >> 1;
if (tt > gg) tt = gg; if (tt > gg)
tt = gg;
} while (++rr < 1000); } while (++rr < 1000);
mp_mulmod(&b, &c, &a, &d); mp_mulmod(&b, &c, &a, &d);
if (mp_cmp_d(&d, 1) != MP_EQ) { if (mp_cmp_d(&d, 1) != MP_EQ) {
printf("Failed to invert\n"); printf("Failed to invert\n");
return 0; return 0;
} }
printf("Inverting mod\t%4d-bit => %9llu/sec, %9llu cycles\n", mp_count_bits(&a), CLK_PER_SEC/tt, tt); printf("Inverting mod\t%4d-bit => %9llu/sec, %9llu cycles\n",
fprintf(log, "%d %9llu\n", cnt*DIGIT_BIT, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
fprintf(log, "%d %9llu\n", cnt * DIGIT_BIT, tt);
} }
fclose(log); fclose(log);
return 0; return 0;
} }

2
dep.pl
View File

@ -13,6 +13,8 @@ print CLASS "#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))\n#if defined
foreach my $filename (glob "bn*.c") { foreach my $filename (glob "bn*.c") {
my $define = $filename; my $define = $filename;
print "Processing $filename\n";
# convert filename to upper case so we can use it as a define # convert filename to upper case so we can use it as a define
$define =~ tr/[a-z]/[A-Z]/; $define =~ tr/[a-z]/[A-Z]/;
$define =~ tr/\./_/; $define =~ tr/\./_/;

View File

@ -10,13 +10,44 @@
*/ */
#define TIMES (1UL<<14UL) #define TIMES (1UL<<14UL)
/* RDTSC from Scott Duplichan */
static ulong64 TIMFUNC (void)
{
#if defined __GNUC__
#if defined(__i386__) || defined(__x86_64__)
unsigned long long a;
__asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx");
return a;
#else /* gcc-IA64 version */
unsigned long result;
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
while (__builtin_expect ((int) result == -1, 0))
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
return result;
#endif
// Microsoft and Intel Windows compilers
#elif defined _M_IX86
__asm rdtsc
#elif defined _M_AMD64
return __rdtsc ();
#elif defined _M_IA64
#if defined __INTEL_COMPILER
#include <ia64intrin.h>
#endif
return __getReg (3116);
#else
#error need rdtsc function for this build
#endif
}
#ifndef X86_TIMER #ifndef X86_TIMER
/* generic ISO C timer */ /* generic ISO C timer */
ulong64 LBL_T; ulong64 LBL_T;
void t_start(void) { LBL_T = clock(); } void t_start(void) { LBL_T = TIMFUNC(); }
ulong64 t_read(void) { return clock() - LBL_T; } ulong64 t_read(void) { return TIMFUNC() - LBL_T; }
#else #else
extern void t_start(void); extern void t_start(void);

View File

@ -1,10 +1,10 @@
480 88 480 87
960 113 960 111
1440 138 1440 135
1920 163 1920 159
2400 202 2400 200
2880 226 2880 224
3360 251 3360 248
3840 272 3840 272
4320 296 4320 296
4800 320 4800 320

View File

@ -1,7 +1,7 @@
513 1499509 513 1489160
769 3682671 769 3688476
1025 8098887 1025 8162061
2049 49332743 2049 49260015
2561 89647783 2561 89579052
3073 149440713 3073 148797060
4097 326135364 4097 324449263

View File

@ -1,6 +1,5 @@
521 1423346 607 2272809
607 1841305 1279 9557382
1279 8375656 2203 36250309
2203 34104708 3217 87666486
3217 83830729 4253 174168369
4253 167916804

4
logs/expt_2kl.log Normal file
View File

@ -0,0 +1,4 @@
1024 6954080
2048 35993987
4096 176068521
521 1683720

View File

@ -1,7 +1,7 @@
532 1803110 532 1989592
784 3607375 784 3898697
1036 6089790 1036 6519700
1540 14739797 1540 15676650
2072 33251589 2072 33128187
3080 82794331 3080 82963362
4116 165212734 4116 168358337

View File

@ -1,143 +1,84 @@
271 580 271 555
390 861 390 855
511 1177 508 1161
630 1598 631 1605
749 2115 749 2117
871 2670 871 2687
991 3276 991 3329
1111 3987 1108 4084
1231 4722 1231 4786
1351 5474 1351 5624
1471 6281 1470 6392
1589 7126 1586 7364
1710 8114 1710 8218
1831 8988 1830 9255
1946 10038 1951 10217
2071 10995 2067 11461
2188 12286 2191 12463
2310 13152 2308 13677
2430 14480 2430 14800
2549 15521 2551 16232
2671 17171 2671 17460
2790 18081 2791 18899
2911 19754 2902 20247
3031 20809 3028 21902
3150 22849 3151 23240
3269 23757 3267 24927
3391 25772 3391 26441
3508 26832 3511 28277
3631 29304 3631 29838
3750 30149 3749 31751
3865 32581 3869 33673
3988 33644 3989 35431
4111 36565 4111 37518
4231 37309 4231 39426
4351 40152 4349 41504
4471 41188 4471 43567
4590 44658 4591 45786
4710 45256 4711 47876
4827 48538 4831 50299
4951 49490 4951 52427
5070 53472 5071 54785
5190 53902 5189 57241
5308 57619 5307 59730
5431 58509 5431 62194
5550 63044 5551 64761
5664 63333 5670 67322
5791 67542 5789 70073
5911 68279 5907 72663
6028 73477 6030 75437
6150 73475 6151 78242
6271 78189 6268 81202
6390 78842 6389 83948
6510 84691 6509 86985
6631 84444 6631 89903
6751 89721 6747 93184
6871 90186 6869 96044
6991 96665 6991 99286
7111 96119 7109 102395
7231 101937 7229 105917
7350 102212 7351 108940
7471 109439 7470 112490
7591 108491 7589 115702
7709 114965 7711 119508
7829 115025 7831 122632
7951 123002 7951 126410
8071 121630 8071 129808
8190 128725 8190 133895
8311 128536 8311 137146
8430 137298 8431 141218
8550 135568 8549 144732
8671 143265 8667 149131
8791 142793 8790 152462
8911 152432 8911 156754
9030 150202 9030 160479
9151 158616 9149 165138
9271 157848 9271 168601
9391 168374 9391 173185
9511 165651 9511 176988
9627 174775 9627 181976
9750 173375 9751 185539
9871 185067 9870 190388
9985 181845 9991 194335
10111 191708 10110 199605
10229 190239 10228 203298
10351 202585
10467 198704
10591 209193
10711 207322
10831 220842
10950 215882
11071 227761
11191 225501
11311 239669
11430 234809
11550 243511
11671 255947
11791 255243
11906 267828
12029 263437
12149 276571
12270 275579
12390 288963
12510 284001
12631 298196
12751 297018
12869 310848
12990 305369
13111 319086
13230 318940
13349 333685
13471 327495
13588 343678
13711 341817
13831 357181
13948 350440
14071 367526
14189 365330
14311 381551
14429 374149
14549 392203
14670 389764
14791 406761
14910 398652
15026 417718
15150 414733
15269 432759
15390 1037071
15511 1053454
15631 1069198
15748 1086164
15871 1112820
15991 1129676
16111 1145924
16230 1163016
16345 1179911
16471 1197048
16586 1214352
16711 1232095
16829 1249338
16947 1266987
17071 1284181
17188 1302521
17311 1320539

View File

@ -1,33 +1,84 @@
924 16686 271 560
1146 25334 391 870
1371 35304 511 1159
1591 47122 631 1605
1820 61500 750 2111
2044 75254 871 2737
2266 91732 991 3361
2492 111656 1111 4054
2716 129428 1231 4778
2937 147508 1351 5600
3164 167758 1471 6404
3388 188248 1591 7323
3612 210826 1710 8255
3836 233814 1831 9239
4059 256898 1948 10257
4284 280210 2070 11397
4508 310372 2190 12531
4731 333902 2308 13665
4955 376502 2429 14870
5179 402854 2550 16175
5404 432004 2671 17539
5626 459010 2787 18879
5849 491868 2911 20350
6076 520550 3031 21807
6300 547400 3150 23415
6524 575968 3270 24897
6747 608482 3388 26567
6971 642850 3511 28205
7196 673670 3627 30076
7419 710680 3751 31744
7644 743942 3869 33657
7868 780394 3991 35425
8092 817342 4111 37522
4229 39363
4351 41503
4470 43491
4590 45827
4711 47795
4828 50166
4951 52318
5070 54911
5191 57036
5308 58237
5431 60248
5551 62678
5671 64786
5791 67294
5908 69343
6031 71607
6151 74166
6271 76590
6391 78734
6511 81175
6631 83742
6750 86403
6868 88873
6990 91150
7110 94211
7228 96922
7351 99445
7469 102216
7589 104968
7711 108113
7827 110758
7950 113714
8071 116511
8186 119643
8310 122679
8425 125581
8551 128715
8669 131778
8788 135116
8910 138138
9031 141628
9148 144754
9268 148367
9391 151551
9511 155033
9631 158652
9751 162125
9871 165248
9988 168627
10111 172427
10231 176412

View File

@ -1,143 +1,84 @@
271 552 265 562
389 883 389 882
510 1191 509 1207
629 1572 631 1572
750 1996 750 1990
863 2428 859 2433
991 2891 991 2894
1108 3539 1109 3555
1231 4182 1230 4228
1351 4980 1350 5018
1471 5771 1471 5805
1590 6551 1591 6579
1711 7313 1709 7415
1830 8240 1829 8329
1951 9184 1949 9225
2070 10087 2071 10139
2191 11140 2188 11239
2311 12111 2309 12178
2431 13219 2431 13212
2550 14247 2551 14294
2669 15353 2671 15551
2791 16446 2791 16512
2911 17692 2911 17718
3029 18848 3030 18876
3151 20028 3150 20259
3268 21282 3270 21374
3391 22696 3391 22650
3511 23971 3511 23948
3631 25303 3631 25493
3751 26675 3750 26756
3871 28245 3870 28225
3990 29736 3989 29705
4111 31124 4110 31409
4229 32714 4230 32834
4347 34397 4351 34327
4471 35877 4471 35818
4587 37269 4591 37636
4710 39011 4711 39228
4831 40884 4830 40868
4950 42501 4949 42393
5070 44005 5070 44541
5191 46026 5191 46269
5310 48168 5310 48162
5431 49801 5429 49728
5551 51385 5548 51985
5671 53604 5671 53948
5787 55942 5791 55885
5910 57757 5910 57584
6031 59391 6031 60082
6151 61754 6150 62239
6271 64234 6270 64309
6390 66110 6390 66014
6511 67845 6511 68766
6627 70474 6631 71012
6751 73113 6750 73172
6871 75064 6871 74952
6990 76940 6991 77909
7111 79681 7111 80371
7230 82548 7231 82666
7351 84597 7351 84531
7471 86507 7469 87698
7591 89497 7589 90318
7711 225216 7711 225384
7831 232192 7830 232428
7951 239583 7950 240009
8071 247302 8070 246522
8191 255497 8190 253662
8308 261587 8310 260961
8431 271490 8431 269253
8550 279492 8549 275743
8671 286927 8671 283769
8790 294680 8789 290811
8910 302974 8911 300034
9030 311300 9030 306873
9150 318635 9149 315085
9271 326740 9270 323944
9390 335304 9390 332390
9511 344297 9508 337519
9630 352056 9631 348986
9748 358652 9749 356904
9870 369723 9871 367013
9991 379119 9989 373831
10111 386982 10108 381033
10231 396075 10230 393475
10349 404396
10470 415375
10590 424146
10711 433390
10829 442662
10950 453238
11071 462178
11186 469811
11311 482529
11431 493214
11550 503210
11671 513486
11791 524244
11911 535277
12031 544872
12151 555695
12271 566893
12391 578385
12510 588658
12628 596914
12751 611324
12871 623437
12991 633907
13110 645605
13231 657684
13351 670037
13471 680939
13591 693047
13710 705363
13829 718178
13949 727930
14069 739641
14190 754817
14310 768192
14431 779875
14551 792655
14667 802847
14791 819806
14911 831684
15031 844936
15151 858813
15270 873037
15387 882123
15510 899117
15631 913465
15750 927989
15870 940790
15991 954948
16110 969483
16231 984544
16350 997837
16470 1012445
16590 1027834
16710 1043032
16831 1056394
16951 1071408
17069 1097263
17191 1113364
17306 1123650

View File

@ -1,33 +1,84 @@
922 11272 271 560
1148 16004 388 878
1370 21958 511 1179
1596 28684 629 1625
1817 37832 751 1988
2044 46386 871 2423
2262 56218 989 2896
2492 66388 1111 3561
2716 77478 1231 4209
2940 89380 1350 5015
3163 103680 1470 5804
3385 116274 1591 6556
3612 135334 1709 7420
3836 151332 1831 8263
4057 164938 1951 9173
4284 183178 2070 10153
4508 198864 2191 11229
4731 215222 2310 12167
4954 231986 2431 13211
5180 251660 2550 14309
5404 269414 2671 15524
5626 288454 2788 16525
5850 307806 2910 17712
6076 329458 3028 18822
6299 347726 3148 20220
6523 369864 3271 21343
6748 387832 3391 22652
6971 413010 3511 23944
7194 453310 3630 25485
7415 476936 3750 26778
7643 497118 3868 28201
7867 521394 3990 29653
8091 540224 4111 31393
4225 32841
4350 34328
4471 35786
4590 37652
4711 39245
4830 40876
4951 42433
5068 44547
5191 46321
5311 48140
5430 49727
5550 52034
5671 53954
5791 55921
5908 57597
6031 60084
6148 62226
6270 64295
6390 66045
6511 68779
6629 71003
6751 73169
6871 74992
6991 77895
7110 80376
7231 82628
7351 84468
7470 87664
7591 90284
7711 91352
7828 93995
7950 96276
8071 98691
8190 101256
8308 103631
8431 105222
8550 108343
8671 110281
8787 112764
8911 115397
9031 117690
9151 120266
9271 122715
9391 124624
9510 127937
9630 130313
9750 132914
9871 136129
9991 138517
10108 141525
10231 144225

View File

@ -1,16 +1,16 @@
480 87 480 94
960 114 960 116
1440 139 1440 140
1920 159 1920 164
2400 204 2400 205
2880 228 2880 229
3360 250 3360 253
3840 273 3840 277
4320 300 4320 299
4800 321 4800 321
5280 348 5280 345
5760 370 5760 371
6240 393 6240 395
6720 420 6720 419
7200 444 7200 441
7680 466 7680 465

View File

@ -3,7 +3,7 @@
#Tom St Denis #Tom St Denis
#version of library #version of library
VERSION=0.33 VERSION=0.34
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
@ -57,11 +57,13 @@ bn_mp_prime_is_prime.o bn_mp_prime_next_prime.o bn_mp_dr_reduce.o \
bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \ bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \
bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \ bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \
bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \ bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \
bn_mp_reduce_2k_l.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_2k_setup_l.o \
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \ bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \ bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \ bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \ bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o
libtommath.a: $(OBJECTS) libtommath.a: $(OBJECTS)
$(AR) $(ARFLAGS) libtommath.a $(OBJECTS) $(AR) $(ARFLAGS) libtommath.a $(OBJECTS)

View File

@ -27,11 +27,13 @@ bn_mp_prime_is_prime.obj bn_mp_prime_next_prime.obj bn_mp_dr_reduce.obj \
bn_mp_dr_is_modulus.obj bn_mp_dr_setup.obj bn_mp_reduce_setup.obj \ bn_mp_dr_is_modulus.obj bn_mp_dr_setup.obj bn_mp_reduce_setup.obj \
bn_mp_toom_mul.obj bn_mp_toom_sqr.obj bn_mp_div_3.obj bn_s_mp_exptmod.obj \ bn_mp_toom_mul.obj bn_mp_toom_sqr.obj bn_mp_div_3.obj bn_s_mp_exptmod.obj \
bn_mp_reduce_2k.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_2k_setup.obj \ bn_mp_reduce_2k.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_2k_setup.obj \
bn_mp_reduce_2k_l.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_2k_setup_l.obj \
bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_toradix.obj bn_mp_radix_size.obj \ bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_toradix.obj bn_mp_radix_size.obj \
bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_cnt_lsb.obj bn_error.obj \ bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_cnt_lsb.obj bn_error.obj \
bn_mp_init_multi.obj bn_mp_clear_multi.obj bn_mp_exteuclid.obj bn_mp_toradix_n.obj \ bn_mp_init_multi.obj bn_mp_clear_multi.obj bn_mp_exteuclid.obj bn_mp_toradix_n.obj \
bn_mp_prime_random_ex.obj bn_mp_get_int.obj bn_mp_sqrt.obj bn_mp_is_square.obj \ bn_mp_prime_random_ex.obj bn_mp_get_int.obj bn_mp_sqrt.obj bn_mp_is_square.obj \
bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_invmod_slow.obj bn_mp_prime_rabin_miller_trials.obj bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_invmod_slow.obj bn_mp_prime_rabin_miller_trials.obj \
bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin_n.obj
TARGET = libtommath.lib TARGET = libtommath.lib

View File

@ -32,11 +32,13 @@ bn_mp_prime_is_prime.o bn_mp_prime_next_prime.o bn_mp_dr_reduce.o \
bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \ bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \
bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \ bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \
bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \ bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \
bn_mp_reduce_2k_l.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_2k_setup_l.o \
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \ bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \ bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \ bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \ bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o
# make a Windows DLL via Cygwin # make a Windows DLL via Cygwin
windll: $(OBJECTS) windll: $(OBJECTS)

View File

@ -59,11 +59,13 @@ bn_mp_prime_is_prime.o bn_mp_prime_next_prime.o bn_mp_dr_reduce.o \
bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \ bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \
bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \ bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \
bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \ bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \
bn_mp_reduce_2k_l.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_2k_setup_l.o \
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \ bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \ bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \ bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \ bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o
libtommath.a: $(OBJECTS) libtommath.a: $(OBJECTS)
$(AR) $(ARFLAGS) libtommath.a $(OBJECTS) $(AR) $(ARFLAGS) libtommath.a $(OBJECTS)

View File

@ -26,11 +26,13 @@ bn_mp_prime_is_prime.obj bn_mp_prime_next_prime.obj bn_mp_dr_reduce.obj \
bn_mp_dr_is_modulus.obj bn_mp_dr_setup.obj bn_mp_reduce_setup.obj \ bn_mp_dr_is_modulus.obj bn_mp_dr_setup.obj bn_mp_reduce_setup.obj \
bn_mp_toom_mul.obj bn_mp_toom_sqr.obj bn_mp_div_3.obj bn_s_mp_exptmod.obj \ bn_mp_toom_mul.obj bn_mp_toom_sqr.obj bn_mp_div_3.obj bn_s_mp_exptmod.obj \
bn_mp_reduce_2k.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_2k_setup.obj \ bn_mp_reduce_2k.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_2k_setup.obj \
bn_mp_reduce_2k_l.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_2k_setup_l.obj \
bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_toradix.obj bn_mp_radix_size.obj \ bn_mp_radix_smap.obj bn_mp_read_radix.obj bn_mp_toradix.obj bn_mp_radix_size.obj \
bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_cnt_lsb.obj bn_error.obj \ bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_cnt_lsb.obj bn_error.obj \
bn_mp_init_multi.obj bn_mp_clear_multi.obj bn_mp_exteuclid.obj bn_mp_toradix_n.obj \ bn_mp_init_multi.obj bn_mp_clear_multi.obj bn_mp_exteuclid.obj bn_mp_toradix_n.obj \
bn_mp_prime_random_ex.obj bn_mp_get_int.obj bn_mp_sqrt.obj bn_mp_is_square.obj \ bn_mp_prime_random_ex.obj bn_mp_get_int.obj bn_mp_sqrt.obj bn_mp_is_square.obj \
bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_invmod_slow.obj bn_mp_prime_rabin_miller_trials.obj bn_mp_init_set.obj bn_mp_init_set_int.obj bn_mp_invmod_slow.obj bn_mp_prime_rabin_miller_trials.obj \
bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin_n.obj
library: $(OBJECTS) library: $(OBJECTS)
lib /out:tommath.lib $(OBJECTS) lib /out:tommath.lib $(OBJECTS)

View File

@ -1,7 +1,7 @@
#Makefile for GCC #Makefile for GCC
# #
#Tom St Denis #Tom St Denis
VERSION=0:33 VERSION=0:34
CC = libtool --mode=compile gcc CC = libtool --mode=compile gcc
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
@ -53,11 +53,14 @@ bn_mp_prime_is_prime.o bn_mp_prime_next_prime.o bn_mp_dr_reduce.o \
bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \ bn_mp_dr_is_modulus.o bn_mp_dr_setup.o bn_mp_reduce_setup.o \
bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \ bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_div_3.o bn_s_mp_exptmod.o \
bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \ bn_mp_reduce_2k.o bn_mp_reduce_is_2k.o bn_mp_reduce_2k_setup.o \
bn_mp_reduce_2k_l.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_2k_setup_l.o \
bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \ bn_mp_radix_smap.o bn_mp_read_radix.o bn_mp_toradix.o bn_mp_radix_size.o \
bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \ bn_mp_fread.o bn_mp_fwrite.o bn_mp_cnt_lsb.o bn_error.o \
bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \ bn_mp_init_multi.o bn_mp_clear_multi.o bn_mp_exteuclid.o bn_mp_toradix_n.o \
bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \ bn_mp_prime_random_ex.o bn_mp_get_int.o bn_mp_sqrt.o bn_mp_is_square.o bn_mp_init_set.o \
bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o bn_mp_init_set_int.o bn_mp_invmod_slow.o bn_mp_prime_rabin_miller_trials.o \
bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin_n.o
libtommath.la: $(OBJECTS) libtommath.la: $(OBJECTS)
libtool --mode=link gcc *.lo -o libtommath.la -rpath $(LIBPATH) -version-info $(VERSION) libtool --mode=link gcc *.lo -o libtommath.la -rpath $(LIBPATH) -version-info $(VERSION)

Binary file not shown.

View File

@ -69,8 +69,7 @@ char *mp_error_to_string(int code)
* Based on slow invmod except this is optimized for the case where b is * Based on slow invmod except this is optimized for the case where b is
* odd as per HAC Note 14.64 on pp. 610 * odd as per HAC Note 14.64 on pp. 610
*/ */
int int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
{ {
mp_int x, y, u, v, B, D; mp_int x, y, u, v, B, D;
int res, neg; int res, neg;
@ -220,8 +219,7 @@ LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
* *
* Based on Algorithm 14.32 on pp.601 of HAC. * Based on Algorithm 14.32 on pp.601 of HAC.
*/ */
int int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
{ {
int ix, res, olduse; int ix, res, olduse;
mp_word W[MP_WARRAY]; mp_word W[MP_WARRAY];
@ -401,8 +399,7 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* Based on Algorithm 14.12 on pp.595 of HAC. * Based on Algorithm 14.12 on pp.595 of HAC.
* *
*/ */
int int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY]; mp_digit W[MP_WARRAY];
@ -451,7 +448,7 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
/* store final carry */ /* store final carry */
W[ix] = _W; W[ix] = _W & MP_MASK;
/* setup dest */ /* setup dest */
olduse = c->used; olduse = c->used;
@ -504,8 +501,7 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
* *
* Based on Algorithm 14.12 on pp.595 of HAC. * Based on Algorithm 14.12 on pp.595 of HAC.
*/ */
int int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY]; mp_digit W[MP_WARRAY];
@ -552,7 +548,7 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
/* store final carry */ /* store final carry */
W[ix] = _W; W[ix] = _W & MP_MASK;
/* setup dest */ /* setup dest */
olduse = c->used; olduse = c->used;
@ -683,7 +679,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
} }
/* store it */ /* store it */
W[ix] = _W; W[ix] = _W & MP_MASK;
/* make next carry */ /* make next carry */
W1 = _W >> ((mp_word)DIGIT_BIT); W1 = _W >> ((mp_word)DIGIT_BIT);
@ -2467,21 +2463,29 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
#endif #endif
} }
/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C)
if (mp_reduce_is_2k_l(P) == MP_YES) {
return s_mp_exptmod(G, X, P, Y, 1);
}
#endif
#ifdef BN_MP_DR_IS_MODULUS_C #ifdef BN_MP_DR_IS_MODULUS_C
/* is it a DR modulus? */ /* is it a DR modulus? */
dr = mp_dr_is_modulus(P); dr = mp_dr_is_modulus(P);
#else #else
/* default to no */
dr = 0; dr = 0;
#endif #endif
#ifdef BN_MP_REDUCE_IS_2K_C #ifdef BN_MP_REDUCE_IS_2K_C
/* if not, is it a uDR modulus? */ /* if not, is it a unrestricted DR modulus? */
if (dr == 0) { if (dr == 0) {
dr = mp_reduce_is_2k(P) << 1; dr = mp_reduce_is_2k(P) << 1;
} }
#endif #endif
/* if the modulus is odd or dr != 0 use the fast method */ /* if the modulus is odd or dr != 0 use the montgomery method */
#ifdef BN_MP_EXPTMOD_FAST_C #ifdef BN_MP_EXPTMOD_FAST_C
if (mp_isodd (P) == 1 || dr != 0) { if (mp_isodd (P) == 1 || dr != 0) {
return mp_exptmod_fast (G, X, P, Y, dr); return mp_exptmod_fast (G, X, P, Y, dr);
@ -2489,7 +2493,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
#endif #endif
#ifdef BN_S_MP_EXPTMOD_C #ifdef BN_S_MP_EXPTMOD_C
/* otherwise use the generic Barrett reduction technique */ /* otherwise use the generic Barrett reduction technique */
return s_mp_exptmod (G, X, P, Y); return s_mp_exptmod (G, X, P, Y, 0);
#else #else
/* no exptmod for evens */ /* no exptmod for evens */
return MP_VAL; return MP_VAL;
@ -2535,8 +2539,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
#define TAB_SIZE 256 #define TAB_SIZE 256
#endif #endif
int int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
{ {
mp_int M[TAB_SIZE], res; mp_int M[TAB_SIZE], res;
mp_digit buf, mp; mp_digit buf, mp;
@ -4989,8 +4992,9 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
} }
/* store final carry [if any] */ /* store final carry [if any] and increment ix offset */
*tmpc++ = u; *tmpc++ = u;
++ix;
/* now zero digits above the top */ /* now zero digits above the top */
while (ix++ < olduse) { while (ix++ < olduse) {
@ -5847,7 +5851,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
/* calc the maskOR_msb */ /* calc the maskOR_msb */
maskOR_msb = 0; maskOR_msb = 0;
maskOR_msb_offset = (size - 2) >> 3; maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
if (flags & LTM_PRIME_2MSB_ON) { if (flags & LTM_PRIME_2MSB_ON) {
maskOR_msb |= 1 << ((size - 2) & 7); maskOR_msb |= 1 << ((size - 2) & 7);
} else if (flags & LTM_PRIME_2MSB_OFF) { } else if (flags & LTM_PRIME_2MSB_OFF) {
@ -5855,7 +5859,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
} }
/* get the maskOR_lsb */ /* get the maskOR_lsb */
maskOR_lsb = 0; maskOR_lsb = 1;
if (flags & LTM_PRIME_BBS) { if (flags & LTM_PRIME_BBS) {
maskOR_lsb |= 3; maskOR_lsb |= 3;
} }
@ -6080,7 +6084,7 @@ mp_rand (mp_int * a, int digits)
*/ */
/* read a string [ASCII] in a given radix */ /* read a string [ASCII] in a given radix */
int mp_read_radix (mp_int * a, char *str, int radix) int mp_read_radix (mp_int * a, const char *str, int radix)
{ {
int y, res, neg; int y, res, neg;
char ch; char ch;
@ -6263,8 +6267,7 @@ mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
* precomputed via mp_reduce_setup. * precomputed via mp_reduce_setup.
* From HAC pp.604 Algorithm 14.42 * From HAC pp.604 Algorithm 14.42
*/ */
int int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
{ {
mp_int q; mp_int q;
int res, um = m->used; int res, um = m->used;
@ -6361,8 +6364,7 @@ CLEANUP:
*/ */
/* reduces a modulo n where n is of the form 2**p - d */ /* reduces a modulo n where n is of the form 2**p - d */
int int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
{ {
mp_int q; mp_int q;
int p, res; int p, res;
@ -6404,6 +6406,68 @@ ERR:
/* End: bn_mp_reduce_2k.c */ /* End: bn_mp_reduce_2k.c */
/* Start: bn_mp_reduce_2k_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_L_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
*/
/* reduces a modulo n where n is of the form 2**p - d
This differs from reduce_2k since "d" can be larger
than a single digit.
*/
int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
{
mp_int q;
int p, res;
if ((res = mp_init(&q)) != MP_OKAY) {
return res;
}
p = mp_count_bits(n);
top:
/* q = a/2**p, a = a mod 2**p */
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto ERR;
}
/* q = q * d */
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
goto ERR;
}
/* a = a + q */
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR;
}
if (mp_cmp_mag(a, n) != MP_LT) {
s_mp_sub(a, n, a);
goto top;
}
ERR:
mp_clear(&q);
return res;
}
#endif
/* End: bn_mp_reduce_2k_l.c */
/* Start: bn_mp_reduce_2k_setup.c */ /* Start: bn_mp_reduce_2k_setup.c */
#include <tommath.h> #include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_C #ifdef BN_MP_REDUCE_2K_SETUP_C
@ -6423,8 +6487,7 @@ ERR:
*/ */
/* determines the setup value */ /* determines the setup value */
int int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
mp_reduce_2k_setup(mp_int *a, mp_digit *d)
{ {
int res, p; int res, p;
mp_int tmp; mp_int tmp;
@ -6452,6 +6515,50 @@ mp_reduce_2k_setup(mp_int *a, mp_digit *d)
/* End: bn_mp_reduce_2k_setup.c */ /* End: bn_mp_reduce_2k_setup.c */
/* Start: bn_mp_reduce_2k_setup_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_2K_SETUP_L_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
*/
/* determines the setup value */
int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
{
int res;
mp_int tmp;
if ((res = mp_init(&tmp)) != MP_OKAY) {
return res;
}
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
goto ERR;
}
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
goto ERR;
}
ERR:
mp_clear(&tmp);
return res;
}
#endif
/* End: bn_mp_reduce_2k_setup_l.c */
/* Start: bn_mp_reduce_is_2k.c */ /* Start: bn_mp_reduce_is_2k.c */
#include <tommath.h> #include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_C #ifdef BN_MP_REDUCE_IS_2K_C
@ -6477,9 +6584,9 @@ int mp_reduce_is_2k(mp_int *a)
mp_digit iz; mp_digit iz;
if (a->used == 0) { if (a->used == 0) {
return 0; return MP_NO;
} else if (a->used == 1) { } else if (a->used == 1) {
return 1; return MP_YES;
} else if (a->used > 1) { } else if (a->used > 1) {
iy = mp_count_bits(a); iy = mp_count_bits(a);
iz = 1; iz = 1;
@ -6488,7 +6595,7 @@ int mp_reduce_is_2k(mp_int *a)
/* Test every bit from the second digit up, must be 1 */ /* Test every bit from the second digit up, must be 1 */
for (ix = DIGIT_BIT; ix < iy; ix++) { for (ix = DIGIT_BIT; ix < iy; ix++) {
if ((a->dp[iw] & iz) == 0) { if ((a->dp[iw] & iz) == 0) {
return 0; return MP_NO;
} }
iz <<= 1; iz <<= 1;
if (iz > (mp_digit)MP_MASK) { if (iz > (mp_digit)MP_MASK) {
@ -6497,13 +6604,57 @@ int mp_reduce_is_2k(mp_int *a)
} }
} }
} }
return 1; return MP_YES;
} }
#endif #endif
/* End: bn_mp_reduce_is_2k.c */ /* End: bn_mp_reduce_is_2k.c */
/* Start: bn_mp_reduce_is_2k_l.c */
#include <tommath.h>
#ifdef BN_MP_REDUCE_IS_2K_L_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
*/
/* determines if reduce_2k_l can be used */
int mp_reduce_is_2k_l(mp_int *a)
{
int ix, iy;
if (a->used == 0) {
return MP_NO;
} else if (a->used == 1) {
return MP_YES;
} else if (a->used > 1) {
/* if more than half of the digits are -1 we're sold */
for (iy = ix = 0; ix < a->used; ix++) {
if (a->dp[ix] == MP_MASK) {
++iy;
}
}
return (iy >= (a->used/2)) ? MP_YES : MP_NO;
}
return MP_NO;
}
#endif
/* End: bn_mp_reduce_is_2k_l.c */
/* Start: bn_mp_reduce_setup.c */ /* Start: bn_mp_reduce_setup.c */
#include <tommath.h> #include <tommath.h>
#ifdef BN_MP_REDUCE_SETUP_C #ifdef BN_MP_REDUCE_SETUP_C
@ -7138,8 +7289,7 @@ mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
*/ */
/* store in signed [big endian] format */ /* store in signed [big endian] format */
int int mp_to_signed_bin (mp_int * a, unsigned char *b)
mp_to_signed_bin (mp_int * a, unsigned char *b)
{ {
int res; int res;
@ -7153,6 +7303,37 @@ mp_to_signed_bin (mp_int * a, unsigned char *b)
/* End: bn_mp_to_signed_bin.c */ /* End: bn_mp_to_signed_bin.c */
/* Start: bn_mp_to_signed_bin_n.c */
#include <tommath.h>
#ifdef BN_MP_TO_SIGNED_BIN_N_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
*/
/* store in signed [big endian] format */
int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
{
if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
return MP_VAL;
}
*outlen = mp_signed_bin_size(a);
return mp_to_signed_bin(a, b);
}
#endif
/* End: bn_mp_to_signed_bin_n.c */
/* Start: bn_mp_to_unsigned_bin.c */ /* Start: bn_mp_to_unsigned_bin.c */
#include <tommath.h> #include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_C #ifdef BN_MP_TO_UNSIGNED_BIN_C
@ -7172,8 +7353,7 @@ mp_to_signed_bin (mp_int * a, unsigned char *b)
*/ */
/* store in unsigned [big endian] format */ /* store in unsigned [big endian] format */
int int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
mp_to_unsigned_bin (mp_int * a, unsigned char *b)
{ {
int x, res; int x, res;
mp_int t; mp_int t;
@ -7202,6 +7382,37 @@ mp_to_unsigned_bin (mp_int * a, unsigned char *b)
/* End: bn_mp_to_unsigned_bin.c */ /* End: bn_mp_to_unsigned_bin.c */
/* Start: bn_mp_to_unsigned_bin_n.c */
#include <tommath.h>
#ifdef BN_MP_TO_UNSIGNED_BIN_N_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
*/
/* store in unsigned [big endian] format */
int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
{
if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
return MP_VAL;
}
*outlen = mp_unsigned_bin_size(a);
return mp_to_unsigned_bin(a, b);
}
#endif
/* End: bn_mp_to_unsigned_bin_n.c */
/* Start: bn_mp_toom_mul.c */ /* Start: bn_mp_toom_mul.c */
#include <tommath.h> #include <tommath.h>
#ifdef BN_MP_TOOM_MUL_C #ifdef BN_MP_TOOM_MUL_C
@ -7894,8 +8105,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
*/ */
/* get the size for an unsigned equivalent */ /* get the size for an unsigned equivalent */
int int mp_unsigned_bin_size (mp_int * a)
mp_unsigned_bin_size (mp_int * a)
{ {
int size = mp_count_bits (a); int size = mp_count_bits (a);
return (size / 8 + ((size & 7) != 0 ? 1 : 0)); return (size / 8 + ((size & 7) != 0 ? 1 : 0));
@ -8218,11 +8428,12 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
#define TAB_SIZE 256 #define TAB_SIZE 256
#endif #endif
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
{ {
mp_int M[TAB_SIZE], res, mu; mp_int M[TAB_SIZE], res, mu;
mp_digit buf; mp_digit buf;
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
int (*redux)(mp_int*,mp_int*,mp_int*);
/* find window size */ /* find window size */
x = mp_count_bits (X); x = mp_count_bits (X);
@ -8269,8 +8480,17 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_init (&mu)) != MP_OKAY) { if ((err = mp_init (&mu)) != MP_OKAY) {
goto LBL_M; goto LBL_M;
} }
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
goto LBL_MU; if (redmode == 0) {
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
goto LBL_MU;
}
redux = mp_reduce;
} else {
if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
goto LBL_MU;
}
redux = mp_reduce_2k_l;
} }
/* create M table /* create M table
@ -8293,11 +8513,14 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
} }
for (x = 0; x < (winsize - 1); x++) { for (x = 0; x < (winsize - 1); x++) {
/* square it */
if ((err = mp_sqr (&M[1 << (winsize - 1)], if ((err = mp_sqr (&M[1 << (winsize - 1)],
&M[1 << (winsize - 1)])) != MP_OKAY) { &M[1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
/* reduce modulo P */
if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
} }
@ -8309,7 +8532,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) { if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
goto LBL_MU; goto LBL_MU;
} }
} }
@ -8358,7 +8581,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
continue; continue;
@ -8375,7 +8598,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
} }
@ -8384,7 +8607,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
@ -8402,7 +8625,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
@ -8412,7 +8635,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { if ((err = redux (&res, P, &mu)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
} }
@ -8803,11 +9026,12 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
CPU /Compiler /MUL CUTOFF/SQR CUTOFF CPU /Compiler /MUL CUTOFF/SQR CUTOFF
------------------------------------------------------------- -------------------------------------------------------------
Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-) Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-)
AMD Athlon64 /GCC v3.4.4 / 74/ 124/LTM 0.34
*/ */
int KARATSUBA_MUL_CUTOFF = 88, /* Min. number of digits before Karatsuba multiplication is used. */ int KARATSUBA_MUL_CUTOFF = 74, /* Min. number of digits before Karatsuba multiplication is used. */
KARATSUBA_SQR_CUTOFF = 128, /* Min. number of digits before Karatsuba squaring is used. */ KARATSUBA_SQR_CUTOFF = 124, /* Min. number of digits before Karatsuba squaring is used. */
TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */ TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
TOOM_SQR_CUTOFF = 400; TOOM_SQR_CUTOFF = 400;

View File

@ -429,6 +429,15 @@ int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
/* returns true if a can be reduced with mp_reduce_2k_l */
int mp_reduce_is_2k_l(mp_int *a);
/* determines k value for 2k reduction */
int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
/* d = a**b (mod c) */ /* d = a**b (mod c) */
int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
@ -511,12 +520,14 @@ int mp_count_bits(mp_int *a);
int mp_unsigned_bin_size(mp_int *a); int mp_unsigned_bin_size(mp_int *a);
int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c); int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
int mp_to_unsigned_bin(mp_int *a, unsigned char *b); int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
int mp_signed_bin_size(mp_int *a); int mp_signed_bin_size(mp_int *a);
int mp_read_signed_bin(mp_int *a, unsigned char *b, int c); int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
int mp_to_signed_bin(mp_int *a, unsigned char *b); int mp_to_signed_bin(mp_int *a, unsigned char *b);
int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
int mp_read_radix(mp_int *a, char *str, int radix); int mp_read_radix(mp_int *a, const char *str, int radix);
int mp_toradix(mp_int *a, char *str, int radix); int mp_toradix(mp_int *a, char *str, int radix);
int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
int mp_radix_size(mp_int *a, int radix, int *size); int mp_radix_size(mp_int *a, int radix, int *size);
@ -554,7 +565,7 @@ int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode); int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y); int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
void bn_reverse(unsigned char *s, int len); void bn_reverse(unsigned char *s, int len);
extern const char *mp_s_rmap; extern const char *mp_s_rmap;

Binary file not shown.

View File

@ -66,7 +66,7 @@ QUALCOMM Australia \\
} }
} }
\maketitle \maketitle
This text has been placed in the public domain. This text corresponds to the v0.30 release of the This text has been placed in the public domain. This text corresponds to the v0.34 release of the
LibTomMath project. LibTomMath project.
\begin{alltt} \begin{alltt}

File diff suppressed because it is too large Load Diff

View File

@ -90,8 +90,11 @@
#define BN_MP_READ_UNSIGNED_BIN_C #define BN_MP_READ_UNSIGNED_BIN_C
#define BN_MP_REDUCE_C #define BN_MP_REDUCE_C
#define BN_MP_REDUCE_2K_C #define BN_MP_REDUCE_2K_C
#define BN_MP_REDUCE_2K_L_C
#define BN_MP_REDUCE_2K_SETUP_C #define BN_MP_REDUCE_2K_SETUP_C
#define BN_MP_REDUCE_2K_SETUP_L_C
#define BN_MP_REDUCE_IS_2K_C #define BN_MP_REDUCE_IS_2K_C
#define BN_MP_REDUCE_IS_2K_L_C
#define BN_MP_REDUCE_SETUP_C #define BN_MP_REDUCE_SETUP_C
#define BN_MP_RSHD_C #define BN_MP_RSHD_C
#define BN_MP_SET_C #define BN_MP_SET_C
@ -105,7 +108,9 @@
#define BN_MP_SUB_D_C #define BN_MP_SUB_D_C
#define BN_MP_SUBMOD_C #define BN_MP_SUBMOD_C
#define BN_MP_TO_SIGNED_BIN_C #define BN_MP_TO_SIGNED_BIN_C
#define BN_MP_TO_SIGNED_BIN_N_C
#define BN_MP_TO_UNSIGNED_BIN_C #define BN_MP_TO_UNSIGNED_BIN_C
#define BN_MP_TO_UNSIGNED_BIN_N_C
#define BN_MP_TOOM_MUL_C #define BN_MP_TOOM_MUL_C
#define BN_MP_TOOM_SQR_C #define BN_MP_TOOM_SQR_C
#define BN_MP_TORADIX_C #define BN_MP_TORADIX_C
@ -324,11 +329,12 @@
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#define BN_MP_ABS_C #define BN_MP_ABS_C
#define BN_MP_CLEAR_MULTI_C #define BN_MP_CLEAR_MULTI_C
#define BN_MP_REDUCE_IS_2K_L_C
#define BN_S_MP_EXPTMOD_C
#define BN_MP_DR_IS_MODULUS_C #define BN_MP_DR_IS_MODULUS_C
#define BN_MP_REDUCE_IS_2K_C #define BN_MP_REDUCE_IS_2K_C
#define BN_MP_ISODD_C #define BN_MP_ISODD_C
#define BN_MP_EXPTMOD_FAST_C #define BN_MP_EXPTMOD_FAST_C
#define BN_S_MP_EXPTMOD_C
#endif #endif
#if defined(BN_MP_EXPTMOD_FAST_C) #if defined(BN_MP_EXPTMOD_FAST_C)
@ -725,6 +731,17 @@
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#endif #endif
#if defined(BN_MP_REDUCE_2K_L_C)
#define BN_MP_INIT_C
#define BN_MP_COUNT_BITS_C
#define BN_MP_DIV_2D_C
#define BN_MP_MUL_C
#define BN_S_MP_ADD_C
#define BN_MP_CMP_MAG_C
#define BN_S_MP_SUB_C
#define BN_MP_CLEAR_C
#endif
#if defined(BN_MP_REDUCE_2K_SETUP_C) #if defined(BN_MP_REDUCE_2K_SETUP_C)
#define BN_MP_INIT_C #define BN_MP_INIT_C
#define BN_MP_COUNT_BITS_C #define BN_MP_COUNT_BITS_C
@ -733,11 +750,22 @@
#define BN_S_MP_SUB_C #define BN_S_MP_SUB_C
#endif #endif
#if defined(BN_MP_REDUCE_2K_SETUP_L_C)
#define BN_MP_INIT_C
#define BN_MP_2EXPT_C
#define BN_MP_COUNT_BITS_C
#define BN_S_MP_SUB_C
#define BN_MP_CLEAR_C
#endif
#if defined(BN_MP_REDUCE_IS_2K_C) #if defined(BN_MP_REDUCE_IS_2K_C)
#define BN_MP_REDUCE_2K_C #define BN_MP_REDUCE_2K_C
#define BN_MP_COUNT_BITS_C #define BN_MP_COUNT_BITS_C
#endif #endif
#if defined(BN_MP_REDUCE_IS_2K_L_C)
#endif
#if defined(BN_MP_REDUCE_SETUP_C) #if defined(BN_MP_REDUCE_SETUP_C)
#define BN_MP_2EXPT_C #define BN_MP_2EXPT_C
#define BN_MP_DIV_C #define BN_MP_DIV_C
@ -815,6 +843,11 @@
#define BN_MP_TO_UNSIGNED_BIN_C #define BN_MP_TO_UNSIGNED_BIN_C
#endif #endif
#if defined(BN_MP_TO_SIGNED_BIN_N_C)
#define BN_MP_SIGNED_BIN_SIZE_C
#define BN_MP_TO_SIGNED_BIN_C
#endif
#if defined(BN_MP_TO_UNSIGNED_BIN_C) #if defined(BN_MP_TO_UNSIGNED_BIN_C)
#define BN_MP_INIT_COPY_C #define BN_MP_INIT_COPY_C
#define BN_MP_ISZERO_C #define BN_MP_ISZERO_C
@ -822,6 +855,11 @@
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#endif #endif
#if defined(BN_MP_TO_UNSIGNED_BIN_N_C)
#define BN_MP_UNSIGNED_BIN_SIZE_C
#define BN_MP_TO_UNSIGNED_BIN_C
#endif
#if defined(BN_MP_TOOM_MUL_C) #if defined(BN_MP_TOOM_MUL_C)
#define BN_MP_INIT_MULTI_C #define BN_MP_INIT_MULTI_C
#define BN_MP_MOD_2D_C #define BN_MP_MOD_2D_C
@ -902,10 +940,12 @@
#define BN_MP_INIT_C #define BN_MP_INIT_C
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#define BN_MP_REDUCE_SETUP_C #define BN_MP_REDUCE_SETUP_C
#define BN_MP_REDUCE_C
#define BN_MP_REDUCE_2K_SETUP_L_C
#define BN_MP_REDUCE_2K_L_C
#define BN_MP_MOD_C #define BN_MP_MOD_C
#define BN_MP_COPY_C #define BN_MP_COPY_C
#define BN_MP_SQR_C #define BN_MP_SQR_C
#define BN_MP_REDUCE_C
#define BN_MP_MUL_C #define BN_MP_MUL_C
#define BN_MP_SET_C #define BN_MP_SET_C
#define BN_MP_EXCH_C #define BN_MP_EXCH_C