summaryrefslogtreecommitdiffstats
path: root/libtommath
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2019-05-29 23:08:11 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2019-05-29 23:08:11 (GMT)
commit984c717026e7de57c6dab8db6f99bea6fcf08308 (patch)
treec36cbbaeec3b7b575e3795fb046765a0974390c3 /libtommath
parent6c6bec6689ecc32214a3d895fdc7c6d0e43fc25a (diff)
parent7a1b66b00db2dcc63f4743c36b4e8e6edcfc4211 (diff)
downloadtcl-984c717026e7de57c6dab8db6f99bea6fcf08308.zip
tcl-984c717026e7de57c6dab8db6f99bea6fcf08308.tar.gz
tcl-984c717026e7de57c6dab8db6f99bea6fcf08308.tar.bz2
Merge 8.5
Diffstat (limited to 'libtommath')
-rw-r--r--libtommath/bn_mp_and.c84
-rw-r--r--libtommath/bn_mp_cmp.c19
-rw-r--r--libtommath/bn_mp_cmp_d.c19
-rw-r--r--libtommath/bn_mp_cmp_mag.c21
-rw-r--r--libtommath/bn_mp_or.c82
-rw-r--r--libtommath/bn_mp_xor.c82
6 files changed, 143 insertions, 164 deletions
diff --git a/libtommath/bn_mp_and.c b/libtommath/bn_mp_and.c
index 789bb58..10fe28b 100644
--- a/libtommath/bn_mp_and.c
+++ b/libtommath/bn_mp_and.c
@@ -1,54 +1,56 @@
#include "tommath_private.h"
#ifdef BN_MP_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
- */
-
-/* AND two ints together */
-int mp_and(const mp_int *a, const mp_int *b, mp_int *c)
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* two complement and */
+mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
{
- int res, ix, px;
- mp_int t;
- const mp_int *x;
+ int used = MAX(a->used, b->used) + 1, i;
+ mp_err err;
+ mp_digit ac = 1, bc = 1, cc = 1;
+ mp_sign csign = ((a->sign == MP_NEG) && (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
- if (a->used > b->used) {
- if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
- return res;
- }
- px = b->used;
- x = b;
- } else {
- if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
- return res;
+ if (c->alloc < used) {
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
- px = a->used;
- x = a;
}
- for (ix = 0; ix < px; ix++) {
- t.dp[ix] &= x->dp[ix];
- }
+ for (i = 0; i < used; i++) {
+ mp_digit x, y;
+
+ /* convert to two complement if negative */
+ if (a->sign == MP_NEG) {
+ ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
+ x = ac & MP_MASK;
+ ac >>= MP_DIGIT_BIT;
+ } else {
+ x = (i >= a->used) ? 0uL : a->dp[i];
+ }
- /* zero digits above the last from the smallest mp_int */
- for (; ix < t.used; ix++) {
- t.dp[ix] = 0;
+ /* convert to two complement if negative */
+ if (b->sign == MP_NEG) {
+ bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
+ y = bc & MP_MASK;
+ bc >>= MP_DIGIT_BIT;
+ } else {
+ y = (i >= b->used) ? 0uL : b->dp[i];
+ }
+
+ c->dp[i] = x & y;
+
+ /* convert to to sign-magnitude if negative */
+ if (csign == MP_NEG) {
+ cc += ~c->dp[i] & MP_MASK;
+ c->dp[i] = cc & MP_MASK;
+ cc >>= MP_DIGIT_BIT;
+ }
}
- mp_clamp(&t);
- mp_exch(c, &t);
- mp_clear(&t);
+ c->used = used;
+ c->sign = csign;
+ mp_clamp(c);
return MP_OKAY;
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */
diff --git a/libtommath/bn_mp_cmp.c b/libtommath/bn_mp_cmp.c
index fdcb8d5..ced4840 100644
--- a/libtommath/bn_mp_cmp.c
+++ b/libtommath/bn_mp_cmp.c
@@ -1,19 +1,10 @@
#include "tommath_private.h"
#ifdef BN_MP_CMP_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
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
/* compare two ints (signed)*/
-int mp_cmp(const mp_int *a, const mp_int *b)
+mp_ord mp_cmp(const mp_int *a, const mp_int *b)
{
/* compare based on sign */
if (a->sign != b->sign) {
@@ -33,7 +24,3 @@ int mp_cmp(const mp_int *a, const mp_int *b)
}
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */
diff --git a/libtommath/bn_mp_cmp_d.c b/libtommath/bn_mp_cmp_d.c
index 643cac6..5a8337b 100644
--- a/libtommath/bn_mp_cmp_d.c
+++ b/libtommath/bn_mp_cmp_d.c
@@ -1,19 +1,10 @@
#include "tommath_private.h"
#ifdef BN_MP_CMP_D_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
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
/* compare a digit */
-int mp_cmp_d(const mp_int *a, mp_digit b)
+mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
{
/* compare based on sign */
if (a->sign == MP_NEG) {
@@ -35,7 +26,3 @@ int mp_cmp_d(const mp_int *a, mp_digit b)
}
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */
diff --git a/libtommath/bn_mp_cmp_mag.c b/libtommath/bn_mp_cmp_mag.c
index 7f6ce27..f144ea9 100644
--- a/libtommath/bn_mp_cmp_mag.c
+++ b/libtommath/bn_mp_cmp_mag.c
@@ -1,22 +1,13 @@
#include "tommath_private.h"
#ifdef BN_MP_CMP_MAG_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
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
/* compare maginitude of two ints (unsigned) */
-int mp_cmp_mag(const mp_int *a, const mp_int *b)
+mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
{
int n;
- mp_digit *tmpa, *tmpb;
+ const mp_digit *tmpa, *tmpb;
/* compare based on # of non-zero digits */
if (a->used > b->used) {
@@ -46,7 +37,3 @@ int mp_cmp_mag(const mp_int *a, const mp_int *b)
return MP_EQ;
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */
diff --git a/libtommath/bn_mp_or.c b/libtommath/bn_mp_or.c
index a0f2711..78a6b08 100644
--- a/libtommath/bn_mp_or.c
+++ b/libtommath/bn_mp_or.c
@@ -1,48 +1,56 @@
#include "tommath_private.h"
#ifdef BN_MP_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
- */
-
-/* OR two ints together */
-int mp_or(const mp_int *a, const mp_int *b, mp_int *c)
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* two complement or */
+mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
{
- int res, ix, px;
- mp_int t;
- const mp_int *x;
+ int used = MAX(a->used, b->used) + 1, i;
+ mp_err err;
+ mp_digit ac = 1, bc = 1, cc = 1;
+ mp_sign csign = ((a->sign == MP_NEG) || (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
- if (a->used > b->used) {
- if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
- return res;
- }
- px = b->used;
- x = b;
- } else {
- if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
- return res;
+ if (c->alloc < used) {
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
- px = a->used;
- x = a;
}
- for (ix = 0; ix < px; ix++) {
- t.dp[ix] |= x->dp[ix];
+ for (i = 0; i < used; i++) {
+ mp_digit x, y;
+
+ /* convert to two complement if negative */
+ if (a->sign == MP_NEG) {
+ ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
+ x = ac & MP_MASK;
+ ac >>= MP_DIGIT_BIT;
+ } else {
+ x = (i >= a->used) ? 0uL : a->dp[i];
+ }
+
+ /* convert to two complement if negative */
+ if (b->sign == MP_NEG) {
+ bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
+ y = bc & MP_MASK;
+ bc >>= MP_DIGIT_BIT;
+ } else {
+ y = (i >= b->used) ? 0uL : b->dp[i];
+ }
+
+ c->dp[i] = x | y;
+
+ /* convert to to sign-magnitude if negative */
+ if (csign == MP_NEG) {
+ cc += ~c->dp[i] & MP_MASK;
+ c->dp[i] = cc & MP_MASK;
+ cc >>= MP_DIGIT_BIT;
+ }
}
- mp_clamp(&t);
- mp_exch(c, &t);
- mp_clear(&t);
+
+ c->used = used;
+ c->sign = csign;
+ mp_clamp(c);
return MP_OKAY;
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */
diff --git a/libtommath/bn_mp_xor.c b/libtommath/bn_mp_xor.c
index bfcdbb9..073985e 100644
--- a/libtommath/bn_mp_xor.c
+++ b/libtommath/bn_mp_xor.c
@@ -1,48 +1,56 @@
#include "tommath_private.h"
#ifdef BN_MP_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
- */
-
-/* XOR two ints together */
-int mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* two complement xor */
+mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
{
- int res, ix, px;
- mp_int t;
- const mp_int *x;
+ int used = MAX(a->used, b->used) + 1, i;
+ mp_err err;
+ mp_digit ac = 1, bc = 1, cc = 1;
+ mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
- if (a->used > b->used) {
- if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
- return res;
- }
- px = b->used;
- x = b;
- } else {
- if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
- return res;
+ if (c->alloc < used) {
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
- px = a->used;
- x = a;
}
- for (ix = 0; ix < px; ix++) {
- t.dp[ix] ^= x->dp[ix];
+ for (i = 0; i < used; i++) {
+ mp_digit x, y;
+
+ /* convert to two complement if negative */
+ if (a->sign == MP_NEG) {
+ ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
+ x = ac & MP_MASK;
+ ac >>= MP_DIGIT_BIT;
+ } else {
+ x = (i >= a->used) ? 0uL : a->dp[i];
+ }
+
+ /* convert to two complement if negative */
+ if (b->sign == MP_NEG) {
+ bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
+ y = bc & MP_MASK;
+ bc >>= MP_DIGIT_BIT;
+ } else {
+ y = (i >= b->used) ? 0uL : b->dp[i];
+ }
+
+ c->dp[i] = x ^ y;
+
+ /* convert to to sign-magnitude if negative */
+ if (csign == MP_NEG) {
+ cc += ~c->dp[i] & MP_MASK;
+ c->dp[i] = cc & MP_MASK;
+ cc >>= MP_DIGIT_BIT;
+ }
}
- mp_clamp(&t);
- mp_exch(c, &t);
- mp_clear(&t);
+
+ c->used = used;
+ c->sign = csign;
+ mp_clamp(c);
return MP_OKAY;
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */