From be861c2430ca13fd13b81ff3bcdc6fc0cb4ec8f4 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 31 May 2019 11:28:39 +0000 Subject: Rename mp_get_bit to s_mp_get_bit, rename mp_tc_div_2d to mp_signed_rsh, remove mp_tc_(add|or|xor) functions in favor of mp_(add|or|xor) which can now handle twos-complement. Following ongoing changes in libtommath development. --- generic/tclExecute.c | 2 +- generic/tclStrToD.c | 2 +- generic/tclStubInit.c | 8 +++- generic/tclTestObj.c | 4 +- generic/tclTomMath.decls | 2 +- generic/tclTomMath.h | 35 ++++++----------- generic/tclTomMathDecls.h | 19 +++++---- libtommath/bn_mp_get_bit.c | 44 --------------------- libtommath/bn_mp_signed_rsh.c | 22 +++++++++++ libtommath/bn_mp_tc_and.c | 90 ------------------------------------------- libtommath/bn_mp_tc_div_2d.c | 35 ----------------- libtommath/bn_mp_tc_or.c | 90 ------------------------------------------- libtommath/bn_mp_tc_xor.c | 90 ------------------------------------------- libtommath/bn_s_mp_get_bit.c | 21 ++++++++++ libtommath/tommath.h | 26 +++++-------- libtommath/tommath_class.h | 12 +++--- unix/Makefile.in | 28 ++++---------- win/Makefile.in | 7 +--- win/makefile.vc | 7 +--- 19 files changed, 104 insertions(+), 440 deletions(-) delete mode 100644 libtommath/bn_mp_get_bit.c create mode 100644 libtommath/bn_mp_signed_rsh.c delete mode 100644 libtommath/bn_mp_tc_and.c delete mode 100644 libtommath/bn_mp_tc_div_2d.c delete mode 100644 libtommath/bn_mp_tc_or.c delete mode 100644 libtommath/bn_mp_tc_xor.c create mode 100644 libtommath/bn_s_mp_get_bit.c diff --git a/generic/tclExecute.c b/generic/tclExecute.c index 812ee84..bf2d7bc 100644 --- a/generic/tclExecute.c +++ b/generic/tclExecute.c @@ -8642,7 +8642,7 @@ ExecuteExtendedBinaryMathOp( if (opcode == INST_LSHIFT) { mp_mul_2d(&big1, shift, &bigResult); } else { - mp_tc_div_2d(&big1, shift, &bigResult); + mp_signed_rsh(&big1, shift, &bigResult); } mp_clear(&big1); BIG_RESULT(&bigResult); diff --git a/generic/tclStrToD.c b/generic/tclStrToD.c index 6e1b131..4826d1c 100644 --- a/generic/tclStrToD.c +++ b/generic/tclStrToD.c @@ -4631,7 +4631,7 @@ TclBignumToDouble( */ mp_div_2d(a, -shift, &b, NULL); - if (mp_get_bit(&b, 0)) { + if (mp_isodd(&b)) { if (b.sign == MP_ZPOS) { mp_add_d(&b, 1, &b); } else { diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index 6290aa0..bf05158 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -51,6 +51,12 @@ #undef TclWinGetServByName #undef TclWinGetSockOpt #undef TclWinSetSockOpt +#undef TclBN_mp_tc_and +#undef TclBN_mp_tc_or +#undef TclBN_mp_tc_xor +#define TclBN_mp_tc_and TclBN_mp_and +#define TclBN_mp_tc_or TclBN_mp_or +#define TclBN_mp_tc_xor TclBN_mp_xor /* See bug 510001: TclSockMinimumBuffers needs plat imp */ #ifdef _WIN64 @@ -862,7 +868,7 @@ const TclTomMathStubs tclTomMathStubs = { TclBN_mp_tc_and, /* 73 */ TclBN_mp_tc_or, /* 74 */ TclBN_mp_tc_xor, /* 75 */ - TclBN_mp_tc_div_2d, /* 76 */ + TclBN_mp_signed_rsh, /* 76 */ TclBN_mp_get_bit, /* 77 */ }; diff --git a/generic/tclTestObj.c b/generic/tclTestObj.c index e395435..7f9b561 100644 --- a/generic/tclTestObj.c +++ b/generic/tclTestObj.c @@ -290,9 +290,9 @@ TestbignumobjCmd( return TCL_ERROR; } if (!Tcl_IsShared(varPtr[varIndex])) { - Tcl_SetIntObj(varPtr[varIndex], !mp_get_bit(&bignumValue, 0)); + Tcl_SetIntObj(varPtr[varIndex], !mp_isodd(&bignumValue)); } else { - SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(!mp_get_bit(&bignumValue, 0))); + SetVarToObj(varPtr, varIndex, Tcl_NewIntObj(!mp_isodd(&bignumValue))); } mp_clear(&bignumValue); break; diff --git a/generic/tclTomMath.decls b/generic/tclTomMath.decls index 6f4b4c3..8703082 100644 --- a/generic/tclTomMath.decls +++ b/generic/tclTomMath.decls @@ -252,7 +252,7 @@ declare 75 { int TclBN_mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) } declare 76 { - int TclBN_mp_tc_div_2d(const mp_int *a, int b, mp_int *c) + int TclBN_mp_signed_rsh(const mp_int *a, int b, mp_int *c) } declare 77 { diff --git a/generic/tclTomMath.h b/generic/tclTomMath.h index cc9f286..bbcb4bc 100644 --- a/generic/tclTomMath.h +++ b/generic/tclTomMath.h @@ -203,12 +203,14 @@ typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); /* error code to char* string */ -const char *mp_error_to_string(int code); +/* +const char *mp_error_to_string(mp_err code); +*/ /* ---> init and deinit bignum functions <--- */ /* init a bignum */ /* -int mp_init(mp_int *a); +mp_err mp_init(mp_int *a); */ /* free a bignum */ @@ -218,7 +220,7 @@ void mp_clear(mp_int *a); /* init a null terminated series of arguments */ /* -int mp_init_multi(mp_int *mp, ...); +mp_err mp_init_multi(mp_int *mp, ...); */ /* clear a null terminated series of arguments */ @@ -233,23 +235,23 @@ void mp_exch(mp_int *a, mp_int *b); /* shrink ram required for a bignum */ /* -int mp_shrink(mp_int *a); +mp_err mp_shrink(mp_int *a); */ /* grow an int to a given size */ /* -int mp_grow(mp_int *a, int size); +mp_err mp_grow(mp_int *a, int size); */ /* init to a given number of digits */ /* -int mp_init_size(mp_int *a, int size); +mp_err mp_init_size(mp_int *a, int size); */ /* ---> Basic Manipulations <--- */ #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) -#define mp_iseven(a) (!mp_get_bit((a),0)) -#define mp_isodd(a) mp_get_bit((a),0) +#define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) +#define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) /* set to zero */ @@ -410,24 +412,9 @@ int mp_or(const mp_int *a, const mp_int *b, mp_int *c); int mp_and(const mp_int *a, const mp_int *b, mp_int *c); */ -/* c = a XOR b (two complement) */ -/* -int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c); -*/ - -/* c = a OR b (two complement) */ -/* -int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c); -*/ - -/* c = a AND b (two complement) */ -/* -int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c); -*/ - /* right shift (two complement) */ /* -int mp_tc_div_2d(const mp_int *a, int b, mp_int *c); +int mp_signed_rsh(const mp_int *a, int b, mp_int *c); */ /* ---> Basic arithmetic <--- */ diff --git a/generic/tclTomMathDecls.h b/generic/tclTomMathDecls.h index beb34ef..214a131 100644 --- a/generic/tclTomMathDecls.h +++ b/generic/tclTomMathDecls.h @@ -71,6 +71,7 @@ #define mp_expt_d TclBN_mp_expt_d #define mp_expt_d_ex TclBN_mp_expt_d_ex #define mp_get_bit TclBN_mp_get_bit +#define s_mp_get_bit TclBN_mp_get_bit #define mp_grow TclBN_mp_grow #define mp_init TclBN_mp_init #define mp_init_copy TclBN_mp_init_copy @@ -102,10 +103,11 @@ #define mp_sqrt TclBN_mp_sqrt #define mp_sub TclBN_mp_sub #define mp_sub_d TclBN_mp_sub_d -#define mp_tc_and TclBN_mp_tc_and -#define mp_tc_div_2d TclBN_mp_tc_div_2d -#define mp_tc_or TclBN_mp_tc_or -#define mp_tc_xor TclBN_mp_tc_xor +#define mp_signed_rsh TclBN_mp_signed_rsh +#define mp_tc_and TclBN_mp_and +#define mp_tc_div_2d TclBN_mp_signed_rsh +#define mp_tc_or TclBN_mp_or +#define mp_tc_xor TclBN_mp_xor #define mp_to_unsigned_bin TclBN_mp_to_unsigned_bin #define mp_to_unsigned_bin_n TclBN_mp_to_unsigned_bin_n #define mp_toom_mul TclBN_mp_toom_mul @@ -329,7 +331,8 @@ EXTERN int TclBN_mp_tc_or(const mp_int *a, const mp_int *b, EXTERN int TclBN_mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c); /* 76 */ -EXTERN int TclBN_mp_tc_div_2d(const mp_int *a, int b, mp_int *c); +EXTERN int TclBN_mp_signed_rsh(const mp_int *a, int b, + mp_int *c); /* 77 */ EXTERN int TclBN_mp_get_bit(const mp_int *a, int b); @@ -413,7 +416,7 @@ typedef struct TclTomMathStubs { int (*tclBN_mp_tc_and) (const mp_int *a, const mp_int *b, mp_int *c); /* 73 */ int (*tclBN_mp_tc_or) (const mp_int *a, const mp_int *b, mp_int *c); /* 74 */ int (*tclBN_mp_tc_xor) (const mp_int *a, const mp_int *b, mp_int *c); /* 75 */ - int (*tclBN_mp_tc_div_2d) (const mp_int *a, int b, mp_int *c); /* 76 */ + int (*tclBN_mp_signed_rsh) (const mp_int *a, int b, mp_int *c); /* 76 */ int (*tclBN_mp_get_bit) (const mp_int *a, int b); /* 77 */ } TclTomMathStubs; @@ -577,8 +580,8 @@ extern const TclTomMathStubs *tclTomMathStubsPtr; (tclTomMathStubsPtr->tclBN_mp_tc_or) /* 74 */ #define TclBN_mp_tc_xor \ (tclTomMathStubsPtr->tclBN_mp_tc_xor) /* 75 */ -#define TclBN_mp_tc_div_2d \ - (tclTomMathStubsPtr->tclBN_mp_tc_div_2d) /* 76 */ +#define TclBN_mp_signed_rsh \ + (tclTomMathStubsPtr->tclBN_mp_signed_rsh) /* 76 */ #define TclBN_mp_get_bit \ (tclTomMathStubsPtr->tclBN_mp_get_bit) /* 77 */ diff --git a/libtommath/bn_mp_get_bit.c b/libtommath/bn_mp_get_bit.c deleted file mode 100644 index f5d2450..0000000 --- a/libtommath/bn_mp_get_bit.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_GET_BIT_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. - * - * SPDX-License-Identifier: Unlicense - */ - -/* Checks the bit at position b and returns MP_YES - if the bit is 1, MP_NO if it is 0 and MP_VAL - in case of error */ -int mp_get_bit(const mp_int *a, int b) -{ - int limb; - mp_digit bit, isset; - - if (b < 0) { - return MP_VAL; - } - - limb = b / DIGIT_BIT; - - if (limb >= a->used) { - return MP_NO; - } - - bit = (mp_digit)(1) << (b % DIGIT_BIT); - - isset = a->dp[limb] & bit; - return (isset != 0u) ? MP_YES : MP_NO; -} - -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/libtommath/bn_mp_signed_rsh.c b/libtommath/bn_mp_signed_rsh.c new file mode 100644 index 0000000..8d8d841 --- /dev/null +++ b/libtommath/bn_mp_signed_rsh.c @@ -0,0 +1,22 @@ +#include "tommath_private.h" +#ifdef BN_MP_SIGNED_RSH_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* shift right by a certain bit count with sign extension */ +mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) +{ + mp_err res; + if (a->sign == MP_ZPOS) { + return mp_div_2d(a, b, c, NULL); + } + + res = mp_add_d(a, 1uL, c); + if (res != MP_OKAY) { + return res; + } + + res = mp_div_2d(c, b, c, NULL); + return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res; +} +#endif diff --git a/libtommath/bn_mp_tc_and.c b/libtommath/bn_mp_tc_and.c deleted file mode 100644 index 9834dc6..0000000 --- a/libtommath/bn_mp_tc_and.c +++ /dev/null @@ -1,90 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_AND_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. - * - * SPDX-License-Identifier: Unlicense - */ - -/* two complement and */ -int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) -{ - int res = MP_OKAY, bits, abits, bbits; - int as = mp_isneg(a), bs = mp_isneg(b); - mp_int *mx = NULL, _mx, acpy, bcpy; - - if ((as != MP_NO) || (bs != MP_NO)) { - abits = mp_count_bits(a); - bbits = mp_count_bits(b); - bits = MAX(abits, bbits); - res = mp_init_set_int(&_mx, 1uL); - if (res != MP_OKAY) { - goto end; - } - - mx = &_mx; - res = mp_mul_2d(mx, bits + 1, mx); - if (res != MP_OKAY) { - goto end; - } - - if (as != MP_NO) { - res = mp_init(&acpy); - if (res != MP_OKAY) { - goto end; - } - - res = mp_add(mx, a, &acpy); - if (res != MP_OKAY) { - mp_clear(&acpy); - goto end; - } - a = &acpy; - } - if (bs != MP_NO) { - res = mp_init(&bcpy); - if (res != MP_OKAY) { - goto end; - } - - res = mp_add(mx, b, &bcpy); - if (res != MP_OKAY) { - mp_clear(&bcpy); - goto end; - } - b = &bcpy; - } - } - - res = mp_and(a, b, c); - - if ((as != MP_NO) && (bs != MP_NO) && (res == MP_OKAY)) { - res = mp_sub(c, mx, c); - } - -end: - if (a == &acpy) { - mp_clear(&acpy); - } - - if (b == &bcpy) { - mp_clear(&bcpy); - } - - if (mx == &_mx) { - mp_clear(mx); - } - - return res; -} -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/libtommath/bn_mp_tc_div_2d.c b/libtommath/bn_mp_tc_div_2d.c deleted file mode 100644 index 4ff0acf..0000000 --- a/libtommath/bn_mp_tc_div_2d.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_DIV_2D_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. - * - * SPDX-License-Identifier: Unlicense - */ - -/* two complement right shift */ -int mp_tc_div_2d(const mp_int *a, int b, mp_int *c) -{ - int res; - if (mp_isneg(a) == MP_NO) { - return mp_div_2d(a, b, c, NULL); - } - - res = mp_add_d(a, 1uL, c); - if (res != MP_OKAY) { - return res; - } - - res = mp_div_2d(c, b, c, NULL); - return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res; -} -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/libtommath/bn_mp_tc_or.c b/libtommath/bn_mp_tc_or.c deleted file mode 100644 index 0941468..0000000 --- a/libtommath/bn_mp_tc_or.c +++ /dev/null @@ -1,90 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_OR_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. - * - * SPDX-License-Identifier: Unlicense - */ - -/* two complement or */ -int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) -{ - int res = MP_OKAY, bits, abits, bbits; - int as = mp_isneg(a), bs = mp_isneg(b); - mp_int *mx = NULL, _mx, acpy, bcpy; - - if ((as != MP_NO) || (bs != MP_NO)) { - abits = mp_count_bits(a); - bbits = mp_count_bits(b); - bits = MAX(abits, bbits); - res = mp_init_set_int(&_mx, 1uL); - if (res != MP_OKAY) { - goto end; - } - - mx = &_mx; - res = mp_mul_2d(mx, bits + 1, mx); - if (res != MP_OKAY) { - goto end; - } - - if (as != MP_NO) { - res = mp_init(&acpy); - if (res != MP_OKAY) { - goto end; - } - - res = mp_add(mx, a, &acpy); - if (res != MP_OKAY) { - mp_clear(&acpy); - goto end; - } - a = &acpy; - } - if (bs != MP_NO) { - res = mp_init(&bcpy); - if (res != MP_OKAY) { - goto end; - } - - res = mp_add(mx, b, &bcpy); - if (res != MP_OKAY) { - mp_clear(&bcpy); - goto end; - } - b = &bcpy; - } - } - - res = mp_or(a, b, c); - - if (((as != MP_NO) || (bs != MP_NO)) && (res == MP_OKAY)) { - res = mp_sub(c, mx, c); - } - -end: - if (a == &acpy) { - mp_clear(&acpy); - } - - if (b == &bcpy) { - mp_clear(&bcpy); - } - - if (mx == &_mx) { - mp_clear(mx); - } - - return res; -} -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/libtommath/bn_mp_tc_xor.c b/libtommath/bn_mp_tc_xor.c deleted file mode 100644 index cdb1d40..0000000 --- a/libtommath/bn_mp_tc_xor.c +++ /dev/null @@ -1,90 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_XOR_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. - * - * SPDX-License-Identifier: Unlicense - */ - -/* two complement xor */ -int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) -{ - int res = MP_OKAY, bits, abits, bbits; - int as = mp_isneg(a), bs = mp_isneg(b); - mp_int *mx = NULL, _mx, acpy, bcpy; - - if ((as != MP_NO) || (bs != MP_NO)) { - abits = mp_count_bits(a); - bbits = mp_count_bits(b); - bits = MAX(abits, bbits); - res = mp_init_set_int(&_mx, 1uL); - if (res != MP_OKAY) { - goto end; - } - - mx = &_mx; - res = mp_mul_2d(mx, bits + 1, mx); - if (res != MP_OKAY) { - goto end; - } - - if (as != MP_NO) { - res = mp_init(&acpy); - if (res != MP_OKAY) { - goto end; - } - - res = mp_add(mx, a, &acpy); - if (res != MP_OKAY) { - mp_clear(&acpy); - goto end; - } - a = &acpy; - } - if (bs != MP_NO) { - res = mp_init(&bcpy); - if (res != MP_OKAY) { - goto end; - } - - res = mp_add(mx, b, &bcpy); - if (res != MP_OKAY) { - mp_clear(&bcpy); - goto end; - } - b = &bcpy; - } - } - - res = mp_xor(a, b, c); - - if ((as != bs) && (res == MP_OKAY)) { - res = mp_sub(c, mx, c); - } - -end: - if (a == &acpy) { - mp_clear(&acpy); - } - - if (b == &bcpy) { - mp_clear(&bcpy); - } - - if (mx == &_mx) { - mp_clear(mx); - } - - return res; -} -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/libtommath/bn_s_mp_get_bit.c b/libtommath/bn_s_mp_get_bit.c new file mode 100644 index 0000000..da9ccbb --- /dev/null +++ b/libtommath/bn_s_mp_get_bit.c @@ -0,0 +1,21 @@ +#include "tommath_private.h" +#ifdef BN_S_MP_GET_BIT_C + +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* Get bit at position b and return MP_YES if the bit is 1, MP_NO if it is 0 */ +mp_bool s_mp_get_bit(const mp_int *a, int b) +{ + mp_digit bit; + int limb = (int)((unsigned)b / MP_DIGIT_BIT); + + if (limb >= a->used) { + return MP_NO; + } + + bit = (mp_digit)1 << ((unsigned)b % MP_DIGIT_BIT); + return ((a->dp[limb] & bit) != 0u) ? MP_YES : MP_NO; +} + +#endif diff --git a/libtommath/tommath.h b/libtommath/tommath.h index 85814e7..76461de 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -1,14 +1,6 @@ -/* 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. - * - * SPDX-License-Identifier: Unlicense - */ +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + #ifndef BN_H_ #define BN_H_ @@ -157,18 +149,18 @@ void mp_clear_multi(mp_int *mp, ...); void mp_exch(mp_int *a, mp_int *b); /* shrink ram required for a bignum */ -int mp_shrink(mp_int *a); +mp_err mp_shrink(mp_int *a); /* grow an int to a given size */ -int mp_grow(mp_int *a, int size); +mp_err mp_grow(mp_int *a, int size); /* init to a given number of digits */ -int mp_init_size(mp_int *a, int size); +mp_err mp_init_size(mp_int *a, int size); /* ---> Basic Manipulations <--- */ #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) -#define mp_iseven(a) (!mp_get_bit((a),0)) -#define mp_isodd(a) mp_get_bit((a),0) +#define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) +#define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) /* set to zero */ @@ -292,7 +284,7 @@ int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c); int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c); /* right shift (two complement) */ -int mp_tc_div_2d(const mp_int *a, int b, mp_int *c); +int mp_signed_rsh(const mp_int *a, int b, mp_int *c); /* ---> Basic arithmetic <--- */ diff --git a/libtommath/tommath_class.h b/libtommath/tommath_class.h index 46f9996..b3c09fe 100644 --- a/libtommath/tommath_class.h +++ b/libtommath/tommath_class.h @@ -59,7 +59,7 @@ # define BN_MP_FREAD_C # define BN_MP_FWRITE_C # define BN_MP_GCD_C -# define BN_MP_GET_BIT_C +# define BN_S_MP_GET_BIT_C # define BN_MP_GET_DOUBLE_C # define BN_MP_GET_INT_C # define BN_MP_GET_LONG_C @@ -135,7 +135,7 @@ # define BN_MP_SUB_D_C # define BN_MP_SUBMOD_C # define BN_MP_TC_AND_C -# define BN_MP_TC_DIV_2D_C +# define BN_MP_SIGNED_RSH_C # define BN_MP_TC_OR_C # define BN_MP_TC_XOR_C # define BN_MP_TO_SIGNED_BIN_C @@ -442,7 +442,7 @@ # define BN_MP_CLEAR_C #endif -#if defined(BN_MP_GET_BIT_C) +#if defined(BN_S_MP_GET_BIT_C) # define BN_MP_ISZERO_C #endif @@ -715,7 +715,7 @@ # define BN_MP_MUL_C # define BN_MP_SUB_C # define BN_MP_MOD_C -# define BN_MP_GET_BIT_C +# define BN_S_MP_GET_BIT_C # define BN_MP_EXCH_C # define BN_MP_ISZERO_C # define BN_MP_CMP_C @@ -802,7 +802,7 @@ # define BN_MP_MOD_C # define BN_MP_SQR_C # define BN_MP_SUB_C -# define BN_MP_GET_BIT_C +# define BN_S_MP_GET_BIT_C # define BN_MP_ADD_C # define BN_MP_ISODD_C # define BN_MP_DIV_2_C @@ -1034,7 +1034,7 @@ # define BN_MP_SUB_C #endif -#if defined(BN_MP_TC_DIV_2D_C) +#if defined(BN_MP_SIGNED_RSH_C) # define BN_MP_ISNEG_C # define BN_MP_DIV_2D_C # define BN_MP_ADD_D_C diff --git a/unix/Makefile.in b/unix/Makefile.in index a4e7052..b3f0842 100644 --- a/unix/Makefile.in +++ b/unix/Makefile.in @@ -322,7 +322,7 @@ TOMMATH_OBJS = bn_reverse.o bn_fast_s_mp_mul_digs.o \ bn_mp_cnt_lsb.o bn_mp_copy.o \ bn_mp_count_bits.o bn_mp_div.o bn_mp_div_d.o bn_mp_div_2.o \ bn_mp_div_2d.o bn_mp_div_3.o bn_mp_exch.o bn_mp_expt_d.o \ - bn_mp_expt_d_ex.o bn_mp_get_bit.o bn_mp_grow.o bn_mp_init.o \ + bn_mp_expt_d_ex.o bn_s_mp_get_bit.o bn_mp_grow.o bn_mp_init.o \ bn_mp_init_copy.o bn_mp_init_multi.o bn_mp_init_set.o \ bn_mp_init_set_int.o bn_mp_init_size.o bn_mp_karatsuba_mul.o \ bn_mp_karatsuba_sqr.o \ @@ -332,7 +332,7 @@ TOMMATH_OBJS = bn_reverse.o bn_fast_s_mp_mul_digs.o \ bn_mp_read_radix.o bn_mp_rshd.o bn_mp_set.o bn_mp_set_int.o \ bn_mp_set_long.o bn_mp_shrink.o \ bn_mp_sqr.o bn_mp_sqrt.o bn_mp_sub.o bn_mp_sub_d.o \ - bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \ + bn_mp_signed_rsh.o \ bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \ bn_mp_toom_mul.o bn_mp_toom_sqr.o bn_mp_toradix_n.o \ bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_s_mp_add.o \ @@ -506,7 +506,7 @@ TOMMATH_SRCS = \ $(TOMMATH_DIR)/bn_mp_exch.c \ $(TOMMATH_DIR)/bn_mp_expt_d.c \ $(TOMMATH_DIR)/bn_mp_expt_d_ex.c \ - $(TOMMATH_DIR)/bn_mp_get_bit.c \ + $(TOMMATH_DIR)/bn_s_mp_get_bit.c \ $(TOMMATH_DIR)/bn_mp_grow.c \ $(TOMMATH_DIR)/bn_mp_init.c \ $(TOMMATH_DIR)/bn_mp_init_copy.c \ @@ -537,10 +537,7 @@ TOMMATH_SRCS = \ $(TOMMATH_DIR)/bn_mp_sqrt.c \ $(TOMMATH_DIR)/bn_mp_sub.c \ $(TOMMATH_DIR)/bn_mp_sub_d.c \ - $(TOMMATH_DIR)/bn_mp_tc_and.c \ - $(TOMMATH_DIR)/bn_mp_tc_div_2d.c \ - $(TOMMATH_DIR)/bn_mp_tc_or.c \ - $(TOMMATH_DIR)/bn_mp_tc_xor.c \ + $(TOMMATH_DIR)/bn_mp_signed_rsh.c \ $(TOMMATH_DIR)/bn_mp_to_unsigned_bin.c \ $(TOMMATH_DIR)/bn_mp_to_unsigned_bin_n.c \ $(TOMMATH_DIR)/bn_mp_toom_mul.c \ @@ -1429,8 +1426,8 @@ bn_mp_expt_d.o: $(TOMMATH_DIR)/bn_mp_expt_d.c $(MATHHDRS) bn_mp_expt_d_ex.o: $(TOMMATH_DIR)/bn_mp_expt_d_ex.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_expt_d_ex.c -bn_mp_get_bit.o: $(TOMMATH_DIR)/bn_mp_get_bit.c $(MATHHDRS) - $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_get_bit.c +bn_s_mp_get_bit.o: $(TOMMATH_DIR)/bn_s_mp_get_bit.c $(MATHHDRS) + $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_s_mp_get_bit.c bn_mp_grow.o: $(TOMMATH_DIR)/bn_mp_grow.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_grow.c @@ -1522,17 +1519,8 @@ bn_mp_sub.o: $(TOMMATH_DIR)/bn_mp_sub.c $(MATHHDRS) bn_mp_sub_d.o: $(TOMMATH_DIR)/bn_mp_sub_d.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_sub_d.c -bn_mp_tc_and.o: $(TOMMATH_DIR)/bn_mp_tc_and.c $(MATHHDRS) - $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_and.c - -bn_mp_tc_div_2d.o: $(TOMMATH_DIR)/bn_mp_tc_div_2d.c $(MATHHDRS) - $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_div_2d.c - -bn_mp_tc_or.o: $(TOMMATH_DIR)/bn_mp_tc_or.c $(MATHHDRS) - $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_or.c - -bn_mp_tc_xor.o: $(TOMMATH_DIR)/bn_mp_tc_xor.c $(MATHHDRS) - $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_tc_xor.c +bn_mp_signed_rsh.o: $(TOMMATH_DIR)/bn_mp_signed_rsh.c $(MATHHDRS) + $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_signed_rsh.c bn_mp_to_unsigned_bin.o: $(TOMMATH_DIR)/bn_mp_to_unsigned_bin.c $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(TOMMATH_DIR)/bn_mp_to_unsigned_bin.c diff --git a/win/Makefile.in b/win/Makefile.in index cc3de21..fc0cd2c 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -334,7 +334,7 @@ TOMMATH_OBJS = \ bn_mp_exch.${OBJEXT} \ bn_mp_expt_d.${OBJEXT} \ bn_mp_expt_d_ex.${OBJEXT} \ - bn_mp_get_bit.${OBJEXT} \ + bn_s_mp_get_bit.${OBJEXT} \ bn_mp_grow.${OBJEXT} \ bn_mp_init.${OBJEXT} \ bn_mp_init_copy.${OBJEXT} \ @@ -365,10 +365,7 @@ TOMMATH_OBJS = \ bn_mp_sqrt.${OBJEXT} \ bn_mp_sub.${OBJEXT} \ bn_mp_sub_d.${OBJEXT} \ - bn_mp_tc_and.${OBJEXT} \ - bn_mp_tc_div_2d.${OBJEXT} \ - bn_mp_tc_or.${OBJEXT} \ - bn_mp_tc_xor.${OBJEXT} \ + bn_mp_signed_rsh.${OBJEXT} \ bn_mp_to_unsigned_bin.${OBJEXT} \ bn_mp_to_unsigned_bin_n.${OBJEXT} \ bn_mp_toom_mul.${OBJEXT} \ diff --git a/win/makefile.vc b/win/makefile.vc index d1122a4..3edd266 100644 --- a/win/makefile.vc +++ b/win/makefile.vc @@ -274,7 +274,7 @@ TOMMATHOBJS = \ $(TMP_DIR)\bn_mp_exch.obj \ $(TMP_DIR)\bn_mp_expt_d.obj \ $(TMP_DIR)\bn_mp_expt_d_ex.obj \ - $(TMP_DIR)\bn_mp_get_bit.obj \ + $(TMP_DIR)\bn_s_mp_get_bit.obj \ $(TMP_DIR)\bn_mp_grow.obj \ $(TMP_DIR)\bn_mp_init.obj \ $(TMP_DIR)\bn_mp_init_copy.obj \ @@ -305,10 +305,7 @@ TOMMATHOBJS = \ $(TMP_DIR)\bn_mp_sqrt.obj \ $(TMP_DIR)\bn_mp_sub.obj \ $(TMP_DIR)\bn_mp_sub_d.obj \ - $(TMP_DIR)\bn_mp_tc_and.obj \ - $(TMP_DIR)\bn_mp_tc_div_2d.obj \ - $(TMP_DIR)\bn_mp_tc_or.obj \ - $(TMP_DIR)\bn_mp_tc_xor.obj \ + $(TMP_DIR)\bn_mp_signed_rsh.obj \ $(TMP_DIR)\bn_mp_to_unsigned_bin.obj \ $(TMP_DIR)\bn_mp_to_unsigned_bin_n.obj \ $(TMP_DIR)\bn_mp_toom_mul.obj \ -- cgit v0.12