Skip to content
Snippets Groups Projects
Commit 364fb601 authored by Matt Johnston's avatar Matt Johnston
Browse files

Update to libtommath v1.0

parent a79b6151
No related branches found
No related tags found
No related merge requests found
Showing
with 344 additions and 195 deletions
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_CMP_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* compare a digit */
Loading
Loading
@@ -39,6 +39,6 @@ int mp_cmp_d(mp_int * a, mp_digit b)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_cmp_d.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_CMP_MAG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* compare maginitude of two ints (unsigned) */
Loading
Loading
@@ -50,6 +50,6 @@ int mp_cmp_mag (mp_int * a, mp_int * b)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_cmp_mag.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_CNT_LSB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
static const int lnz[16] = {
Loading
Loading
@@ -26,12 +26,12 @@ int mp_cnt_lsb(mp_int *a)
mp_digit q, qq;
 
/* easy out */
if (mp_iszero(a) == 1) {
if (mp_iszero(a) == MP_YES) {
return 0;
}
 
/* scan lower digits until non-zero */
for (x = 0; x < a->used && a->dp[x] == 0; x++);
for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {}
q = a->dp[x];
x *= DIGIT_BIT;
 
Loading
Loading
@@ -48,6 +48,6 @@ int mp_cnt_lsb(mp_int *a)
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_cnt_lsb.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* copy, b = a */
Loading
Loading
@@ -35,7 +35,7 @@ mp_copy (mp_int * a, mp_int * b)
 
/* zero b and copy the parameters over */
{
register mp_digit *tmpa, *tmpb;
mp_digit *tmpa, *tmpb;
 
/* pointer aliases */
 
Loading
Loading
@@ -63,6 +63,6 @@ mp_copy (mp_int * a, mp_int * b)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_copy.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_COUNT_BITS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* returns the number of bits in an int */
Loading
Loading
@@ -40,6 +40,6 @@ mp_count_bits (mp_int * a)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_count_bits.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DIV_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
#ifdef BN_MP_DIV_SMALL
Loading
Loading
@@ -24,7 +24,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int res, n, n2;
 
/* is divisor zero ? */
if (mp_iszero (b) == 1) {
if (mp_iszero (b) == MP_YES) {
return MP_VAL;
}
 
Loading
Loading
@@ -40,9 +40,9 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
}
return res;
}
/* init our temps */
if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != MP_OKAY)) {
if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) {
return res;
}
 
Loading
Loading
@@ -50,7 +50,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
mp_set(&tq, 1);
n = mp_count_bits(a) - mp_count_bits(b);
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
goto LBL_ERR;
Loading
Loading
@@ -71,7 +71,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
 
/* now q == quotient and ta == remainder */
n = a->sign;
n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
n2 = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
if (c != NULL) {
mp_exch(c, &q);
c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
Loading
Loading
@@ -87,17 +87,17 @@ LBL_ERR:
 
#else
 
/* integer signed division.
/* integer signed division.
* c*b + d == a [e.g. a/b, c=quotient, d=remainder]
* HAC pp.598 Algorithm 14.20
*
* Note that the description in HAC is horribly
* incomplete. For example, it doesn't consider
* the case where digits are removed from 'x' in
* the inner loop. It also doesn't consider the
* Note that the description in HAC is horribly
* incomplete. For example, it doesn't consider
* the case where digits are removed from 'x' in
* the inner loop. It also doesn't consider the
* case that y has fewer than three digits, etc..
*
* The overall algorithm is as described as
* The overall algorithm is as described as
* 14.20 from HAC but fixed to treat these cases.
*/
int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
Loading
Loading
@@ -106,7 +106,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int res, n, t, i, norm, neg;
 
/* is divisor zero ? */
if (mp_iszero (b) == 1) {
if (mp_iszero (b) == MP_YES) {
return MP_VAL;
}
 
Loading
Loading
@@ -187,51 +187,52 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
continue;
}
 
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
if (x.dp[i] == y.dp[t]) {
q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
} else {
mp_word tmp;
tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
tmp |= ((mp_word) x.dp[i - 1]);
tmp /= ((mp_word) y.dp[t]);
if (tmp > (mp_word) MP_MASK)
if (tmp > (mp_word) MP_MASK) {
tmp = MP_MASK;
q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
}
q.dp[(i - t) - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
}
 
/* while (q{i-t-1} * (yt * b + y{t-1})) >
xi * b**2 + xi-1 * b + xi-2
do q{i-t-1} -= 1;
/* while (q{i-t-1} * (yt * b + y{t-1})) >
xi * b**2 + xi-1 * b + xi-2
do q{i-t-1} -= 1;
*/
q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK;
do {
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK;
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK;
 
/* find left hand */
mp_zero (&t1);
t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1];
t1.dp[1] = y.dp[t];
t1.used = 2;
if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
if ((res = mp_mul_d (&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
goto LBL_Y;
}
 
/* find right hand */
t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2];
t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
t2.dp[2] = x.dp[i];
t2.used = 3;
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
 
/* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
if ((res = mp_mul_d (&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
goto LBL_Y;
}
 
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
goto LBL_Y;
}
 
Loading
Loading
@@ -244,23 +245,23 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
goto LBL_Y;
}
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
goto LBL_Y;
}
if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
goto LBL_Y;
}
 
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK;
}
}
 
/* now q is the quotient and x is the remainder
* [which we have to normalize]
/* now q is the quotient and x is the remainder
* [which we have to normalize]
*/
/* get sign before writing to c */
x.sign = x.used == 0 ? MP_ZPOS : a->sign;
x.sign = (x.used == 0) ? MP_ZPOS : a->sign;
 
if (c != NULL) {
mp_clamp (&q);
Loading
Loading
@@ -270,8 +271,8 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
 
if (d != NULL) {
if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) {
goto LBL_Y;
}
goto LBL_Y;
}
mp_exch (&x, d);
}
 
Loading
Loading
@@ -289,6 +290,6 @@ LBL_Q:mp_clear (&q);
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_div.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DIV_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* b = a/2 */
Loading
Loading
@@ -30,7 +30,7 @@ int mp_div_2(mp_int * a, mp_int * b)
oldused = b->used;
b->used = a->used;
{
register mp_digit r, rr, *tmpa, *tmpb;
mp_digit r, rr, *tmpa, *tmpb;
 
/* source alias */
tmpa = a->dp + b->used - 1;
Loading
Loading
@@ -63,6 +63,6 @@ int mp_div_2(mp_int * a, mp_int * b)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_div_2.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DIV_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
Loading
Loading
@@ -58,7 +58,7 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
/* shift any bit count < DIGIT_BIT */
D = (mp_digit) (b % DIGIT_BIT);
if (D != 0) {
register mp_digit *tmpc, mask, shift;
mp_digit *tmpc, mask, shift;
 
/* mask */
mask = (((mp_digit)1) << D) - 1;
Loading
Loading
@@ -92,6 +92,6 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_div_2d.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DIV_3_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* divide by three (based on routine from MPI and the GMP manual) */
Loading
Loading
@@ -74,6 +74,6 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_div_3.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DIV_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,14 +12,19 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
static int s_is_power_of_two(mp_digit b, int *p)
{
int x;
 
for (x = 1; x < DIGIT_BIT; x++) {
/* fast return if no power of two */
if ((b == 0) || ((b & (b-1)) != 0)) {
return 0;
}
for (x = 0; x < DIGIT_BIT; x++) {
if (b == (((mp_digit)1)<<x)) {
*p = x;
return 1;
Loading
Loading
@@ -42,7 +47,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
}
 
/* quick outs */
if (b == 1 || mp_iszero(a) == 1) {
if ((b == 1) || (mp_iszero(a) == MP_YES)) {
if (d != NULL) {
*d = 0;
}
Loading
Loading
@@ -105,6 +110,6 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_div_d.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DR_IS_MODULUS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* determines if a number is a valid DR modulus */
Loading
Loading
@@ -38,6 +38,6 @@ int mp_dr_is_modulus(mp_int *a)
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_dr_is_modulus.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DR_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
Loading
Loading
@@ -40,7 +40,7 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
m = n->used;
 
/* ensure that "x" has at least 2m digits */
if (x->alloc < m + m) {
if (x->alloc < (m + m)) {
if ((err = mp_grow (x, m + m)) != MP_OKAY) {
return err;
}
Loading
Loading
@@ -62,7 +62,7 @@ top:
 
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
for (i = 0; i < m; i++) {
r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
r = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu;
*tmpx1++ = (mp_digit)(r & MP_MASK);
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
}
Loading
Loading
@@ -82,13 +82,15 @@ top:
* Each successive "recursion" makes the input smaller and smaller.
*/
if (mp_cmp_mag (x, n) != MP_LT) {
s_mp_sub(x, n, x);
if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
return err;
}
goto top;
}
return MP_OKAY;
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_dr_reduce.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_DR_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* determines the setup value */
Loading
Loading
@@ -27,6 +27,6 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_dr_setup.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_EXCH_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* swap the elements of two integers, for cases where you can't simply swap the
Loading
Loading
@@ -29,6 +29,6 @@ mp_exch (mp_int * a, mp_int * b)
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_exch.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath_private.h>
#ifdef BN_MP_EXPORT_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, tstdenis82@gmail.com, http://libtom.org
*/
/* based on gmp's mpz_export.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
int mp_export(void* rop, size_t* countp, int order, size_t size,
int endian, size_t nails, mp_int* op) {
int result;
size_t odd_nails, nail_bytes, i, j, bits, count;
unsigned char odd_nail_mask;
mp_int t;
if ((result = mp_init_copy(&t, op)) != MP_OKAY) {
return result;
}
if (endian == 0) {
union {
unsigned int i;
char c[4];
} lint;
lint.i = 0x01020304;
endian = (lint.c[0] == 4) ? -1 : 1;
}
odd_nails = (nails % 8);
odd_nail_mask = 0xff;
for (i = 0; i < odd_nails; ++i) {
odd_nail_mask ^= (1 << (7 - i));
}
nail_bytes = nails / 8;
bits = mp_count_bits(&t);
count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) {
unsigned char* byte = (
(unsigned char*)rop +
(((order == -1) ? i : ((count - 1) - i)) * size) +
((endian == -1) ? j : ((size - 1) - j))
);
if (j >= (size - nail_bytes)) {
*byte = 0;
continue;
}
*byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
mp_clear(&t);
return result;
}
}
}
mp_clear(&t);
if (countp != NULL) {
*countp = count;
}
return MP_OKAY;
}
#endif
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_EXPT_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,46 +12,17 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* calculate c = a**b using a square-multiply algorithm */
/* wrapper function for mp_expt_d_ex() */
int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
{
int res, x;
mp_int g;
if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
return res;
}
/* set initial result */
mp_set (c, 1);
for (x = 0; x < (int) DIGIT_BIT; x++) {
/* square */
if ((res = mp_sqr (c, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
/* if the bit is set multiply */
if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
}
/* shift to next bit */
b <<= 1;
}
mp_clear (&g);
return MP_OKAY;
return mp_expt_d_ex(a, b, c, 0);
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_expt_d.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath_private.h>
#ifdef BN_MP_EXPT_D_EX_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, tstdenis82@gmail.com, http://libtom.org
*/
/* calculate c = a**b using a square-multiply algorithm */
int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
{
int res;
unsigned int x;
mp_int g;
if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
return res;
}
/* set initial result */
mp_set (c, 1);
if (fast != 0) {
while (b > 0) {
/* if the bit is set multiply */
if ((b & 1) != 0) {
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
}
/* square */
if (b > 1) {
if ((res = mp_sqr (&g, &g)) != MP_OKAY) {
mp_clear (&g);
return res;
}
}
/* shift to next bit */
b >>= 1;
}
}
else {
for (x = 0; x < DIGIT_BIT; x++) {
/* square */
if ((res = mp_sqr (c, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
/* if the bit is set multiply */
if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
mp_clear (&g);
return res;
}
}
/* shift to next bit */
b <<= 1;
}
} /* if ... else */
mp_clear (&g);
return MP_OKAY;
}
#endif
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_EXPTMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
 
Loading
Loading
@@ -89,7 +89,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
/* if the modulus is odd or dr != 0 use the montgomery method */
#ifdef BN_MP_EXPTMOD_FAST_C
if (mp_isodd (P) == 1 || dr != 0) {
if ((mp_isodd (P) == MP_YES) || (dr != 0)) {
return mp_exptmod_fast (G, X, P, Y, dr);
} else {
#endif
Loading
Loading
@@ -107,6 +107,6 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
 
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_exptmod.c,v $ */
/* $Revision: 1.4 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_EXPTMOD_FAST_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,7 +12,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
Loading
Loading
@@ -96,8 +96,8 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
 
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
if (((P->used * 2 + 1) < MP_WARRAY) &&
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
if ((((P->used * 2) + 1) < MP_WARRAY) &&
(P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
redux = fast_mp_montgomery_reduce;
} else
#endif
Loading
Loading
@@ -219,12 +219,12 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
* in the exponent. Technically this opt is not required but it
* does lower the # of trivial squaring/reductions used
*/
if (mode == 0 && y == 0) {
if ((mode == 0) && (y == 0)) {
continue;
}
 
/* if the bit is zero and mode == 1 then we square */
if (mode == 1 && y == 0) {
if ((mode == 1) && (y == 0)) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES;
}
Loading
Loading
@@ -266,7 +266,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
}
 
/* if bits remain then square/multiply */
if (mode == 2 && bitcpy > 0) {
if ((mode == 2) && (bitcpy > 0)) {
/* square then multiply if the bit is set */
for (x = 0; x < bitcpy; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
Loading
Loading
@@ -316,6 +316,6 @@ LBL_M:
#endif
 
 
/* $Source: /cvs/libtom/libtommath/bn_mp_exptmod_fast.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
#include <tommath.h>
#include <tommath_private.h>
#ifdef BN_MP_EXTEUCLID_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
Loading
Loading
@@ -12,10 +12,10 @@
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
 
/* Extended euclidean algorithm of (a, b) produces
/* Extended euclidean algorithm of (a, b) produces
a*u1 + b*u2 = u3
*/
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
Loading
Loading
@@ -29,41 +29,41 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
 
/* initialize, (u1,u2,u3) = (1,0,a) */
mp_set(&u1, 1);
if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto _ERR; }
 
/* initialize, (v1,v2,v3) = (0,1,b) */
mp_set(&v2, 1);
if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto _ERR; }
 
/* loop while v3 != 0 */
while (mp_iszero(&v3) == MP_NO) {
/* q = u3/v3 */
if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto _ERR; }
 
/* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto _ERR; }
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto _ERR; }
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto _ERR; }
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto _ERR; }
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto _ERR; }
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto _ERR; }
 
/* (u1,u2,u3) = (v1,v2,v3) */
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto _ERR; }
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto _ERR; }
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto _ERR; }
 
/* (v1,v2,v3) = (t1,t2,t3) */
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto _ERR; }
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto _ERR; }
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto _ERR; }
}
 
/* make sure U3 >= 0 */
if (u3.sign == MP_NEG) {
mp_neg(&u1, &u1);
mp_neg(&u2, &u2);
mp_neg(&u3, &u3);
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto _ERR; }
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto _ERR; }
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto _ERR; }
}
 
/* copy result out */
Loading
Loading
@@ -72,12 +72,11 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
if (U3 != NULL) { mp_exch(U3, &u3); }
 
err = MP_OKAY;
LBL_ERR:
mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
_ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
return err;
}
#endif
 
/* $Source: /cvs/libtom/libtommath/bn_mp_exteuclid.c,v $ */
/* $Revision: 1.3 $ */
/* $Date: 2006/03/31 14:18:44 $ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment