summaryrefslogtreecommitdiffstats
path: root/libtommath
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2019-05-31 11:28:39 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2019-05-31 11:28:39 (GMT)
commitbe861c2430ca13fd13b81ff3bcdc6fc0cb4ec8f4 (patch)
tree1c7b9ff79fe0e418ec7f713b79f04fe0f846369b /libtommath
parent984c717026e7de57c6dab8db6f99bea6fcf08308 (diff)
downloadtcl-be861c2430ca13fd13b81ff3bcdc6fc0cb4ec8f4.zip
tcl-be861c2430ca13fd13b81ff3bcdc6fc0cb4ec8f4.tar.gz
tcl-be861c2430ca13fd13b81ff3bcdc6fc0cb4ec8f4.tar.bz2
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.
Diffstat (limited to 'libtommath')
-rw-r--r--libtommath/bn_mp_get_bit.c44
-rw-r--r--libtommath/bn_mp_signed_rsh.c22
-rw-r--r--libtommath/bn_mp_tc_and.c90
-rw-r--r--libtommath/bn_mp_tc_div_2d.c35
-rw-r--r--libtommath/bn_mp_tc_or.c90
-rw-r--r--libtommath/bn_mp_tc_xor.c90
-rw-r--r--libtommath/bn_s_mp_get_bit.c21
-rw-r--r--libtommath/tommath.h26
-rw-r--r--libtommath/tommath_class.h12
9 files changed, 58 insertions, 372 deletions
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