diff --git a/bn_deprecated.c b/bn_deprecated.c index eb1a6e125..90a40640c 100644 --- a/bn_deprecated.c +++ b/bn_deprecated.c @@ -122,4 +122,28 @@ void bn_reverse(unsigned char *s, int len) s_mp_reverse(s, len); } #endif +#ifdef BN_MP_TC_AND_C +mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) +{ + return mp_and(a, b, c); +} +#endif +#ifdef BN_MP_TC_OR_C +mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) +{ + return mp_or(a, b, c); +} +#endif +#ifdef BN_MP_TC_XOR_C +mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) +{ + return mp_xor(a, b, c); +} +#endif +#ifdef BN_MP_TC_DIV_2D_C +mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) +{ + return mp_signed_rsh(a, b, c); +} +#endif #endif diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c index 5c0207708..f30157561 100644 --- a/bn_mp_add_d.c +++ b/bn_mp_add_d.c @@ -8,7 +8,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) { mp_err err; int ix, oldused; - mp_digit *tmpa, *tmpc, mu; + mp_digit *tmpa, *tmpc; /* grow c as required */ if (c->alloc < (a->used + 1)) { @@ -46,15 +46,9 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) /* if a is positive */ if (a->sign == MP_ZPOS) { - /* add digit, after this we're propagating - * the carry. - */ - *tmpc = *tmpa++ + b; - mu = *tmpc >> MP_DIGIT_BIT; - *tmpc++ &= MP_MASK; - - /* now handle rest of the digits */ - for (ix = 1; ix < a->used; ix++) { + /* add digits, mu is carry */ + mp_digit mu = b; + for (ix = 0; ix < a->used; ix++) { *tmpc = *tmpa++ + mu; mu = *tmpc >> MP_DIGIT_BIT; *tmpc++ &= MP_MASK; diff --git a/bn_mp_and.c b/bn_mp_and.c index c6c1efee6..1ee14e88d 100644 --- a/bn_mp_and.c +++ b/bn_mp_and.c @@ -3,38 +3,54 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* AND two ints together */ +/* two complement and */ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) { - int ix, px; + int used = MP_MAX(a->used, b->used) + 1, i; mp_err err; - mp_int t; - const mp_int *x; + 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 ((err = mp_init_copy(&t, a)) != MP_OKAY) { + if (c->alloc < used) { + if ((err = mp_grow(c, used)) != MP_OKAY) { return err; } - px = b->used; - x = b; - } else { - if ((err = mp_init_copy(&t, b)) != 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 ? 0 : a->dp[i]; + } - /* zero digits above the last from the smallest mp_int */ - MP_ZERO_DIGITS(t.dp + ix, t.used - ix); + /* 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 ? 0 : 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 diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c index d7b694456..82345809c 100644 --- a/bn_mp_lshd.c +++ b/bn_mp_lshd.c @@ -44,10 +44,7 @@ mp_err mp_lshd(mp_int *a, int b) } /* zero the lower digits */ - top = a->dp; - for (x = 0; x < b; x++) { - *top++ = 0; - } + MP_ZERO_DIGITS(a->dp, b); return MP_OKAY; } diff --git a/bn_mp_or.c b/bn_mp_or.c index 254a5f925..6102a8a0c 100644 --- a/bn_mp_or.c +++ b/bn_mp_or.c @@ -3,34 +3,54 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* OR two ints together */ +/* two complement or */ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) { - int ix, px; - mp_err err; - mp_int t; - const mp_int *x; + int used = MP_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 ((err = mp_init_copy(&t, a)) != MP_OKAY) { + if (c->alloc < used) { + if ((err = mp_grow(c, used)) != MP_OKAY) { return err; } - px = b->used; - x = b; - } else { - if ((err = mp_init_copy(&t, b)) != 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 ? 0 : 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 ? 0 : 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 diff --git a/bn_mp_rshd.c b/bn_mp_rshd.c index 1ab9ba4b8..bb8743e3b 100644 --- a/bn_mp_rshd.c +++ b/bn_mp_rshd.c @@ -43,9 +43,7 @@ void mp_rshd(mp_int *a, int b) } /* zero the top digits */ - for (; x < a->used; x++) { - *bottom++ = 0; - } + MP_ZERO_DIGITS(bottom, a->used - x); /* remove excess digits */ a->used -= b; diff --git a/bn_mp_signed_rsh.c b/bn_mp_signed_rsh.c new file mode 100644 index 000000000..8d8d8414d --- /dev/null +++ b/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/bn_mp_sub_d.c b/bn_mp_sub_d.c index d82605283..3ebf9b485 100644 --- a/bn_mp_sub_d.c +++ b/bn_mp_sub_d.c @@ -6,7 +6,7 @@ /* single digit subtraction */ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) { - mp_digit *tmpa, *tmpc, mu; + mp_digit *tmpa, *tmpc; mp_err err; int ix, oldused; @@ -50,17 +50,14 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) c->sign = MP_NEG; c->used = 1; } else { + mp_digit mu = b; + /* positive/size */ c->sign = MP_ZPOS; c->used = a->used; - /* subtract first digit */ - *tmpc = *tmpa++ - b; - mu = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); - *tmpc++ &= MP_MASK; - - /* handle rest of the digits */ - for (ix = 1; ix < a->used; ix++) { + /* subtract digits, mu is carry */ + for (ix = 0; ix < a->used; ix++) { *tmpc = *tmpa++ - mu; mu = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); *tmpc++ &= MP_MASK; diff --git a/bn_mp_tc_and.c b/bn_mp_tc_and.c deleted file mode 100644 index 8f4a7244d..000000000 --- a/bn_mp_tc_and.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_AND_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* two complement and */ -mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) -{ - mp_err err = MP_OKAY; - int bits, abits, bbits; - mp_sign sa = a->sign, sb = b->sign; - mp_int *mx = NULL, _mx, acpy, bcpy; - - if ((sa == MP_NEG) || (sb == MP_NEG)) { - abits = mp_count_bits(a); - bbits = mp_count_bits(b); - bits = MP_MAX(abits, bbits); - err = mp_init_set_int(&_mx, 1uL); - if (err != MP_OKAY) { - goto end; - } - - mx = &_mx; - err = mp_mul_2d(mx, bits + 1, mx); - if (err != MP_OKAY) { - goto end; - } - - if (sa == MP_NEG) { - err = mp_init(&acpy); - if (err != MP_OKAY) { - goto end; - } - - err = mp_add(mx, a, &acpy); - if (err != MP_OKAY) { - mp_clear(&acpy); - goto end; - } - a = &acpy; - } - if (sb == MP_NEG) { - err = mp_init(&bcpy); - if (err != MP_OKAY) { - goto end; - } - - err = mp_add(mx, b, &bcpy); - if (err != MP_OKAY) { - mp_clear(&bcpy); - goto end; - } - b = &bcpy; - } - } - - err = mp_and(a, b, c); - - if ((sa == MP_NEG) && (sb == MP_NEG) && (err == MP_OKAY)) { - err = 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 err; -} -#endif diff --git a/bn_mp_tc_div_2d.c b/bn_mp_tc_div_2d.c deleted file mode 100644 index d7102083b..000000000 --- a/bn_mp_tc_div_2d.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_DIV_2D_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* two complement right shift */ -mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) -{ - mp_err err; - if (a->sign == MP_ZPOS) { - return mp_div_2d(a, b, c, NULL); - } - - err = mp_add_d(a, 1uL, c); - if (err != MP_OKAY) { - return err; - } - - err = mp_div_2d(c, b, c, NULL); - return (err == MP_OKAY) ? mp_sub_d(c, 1uL, c) : err; -} -#endif diff --git a/bn_mp_tc_or.c b/bn_mp_tc_or.c deleted file mode 100644 index 2b9eeccfb..000000000 --- a/bn_mp_tc_or.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_OR_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* two complement or */ -mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) -{ - mp_err err = MP_OKAY; - int bits, abits, bbits; - mp_sign sa = a->sign, sb = b->sign; - mp_int *mx = NULL, _mx, acpy, bcpy; - - if ((sa == MP_NEG) || (sb == MP_NEG)) { - abits = mp_count_bits(a); - bbits = mp_count_bits(b); - bits = MP_MAX(abits, bbits); - err = mp_init_set_int(&_mx, 1uL); - if (err != MP_OKAY) { - goto end; - } - - mx = &_mx; - err = mp_mul_2d(mx, bits + 1, mx); - if (err != MP_OKAY) { - goto end; - } - - if (sa == MP_NEG) { - err = mp_init(&acpy); - if (err != MP_OKAY) { - goto end; - } - - err = mp_add(mx, a, &acpy); - if (err != MP_OKAY) { - mp_clear(&acpy); - goto end; - } - a = &acpy; - } - if (sb == MP_NEG) { - err = mp_init(&bcpy); - if (err != MP_OKAY) { - goto end; - } - - err = mp_add(mx, b, &bcpy); - if (err != MP_OKAY) { - mp_clear(&bcpy); - goto end; - } - b = &bcpy; - } - } - - err = mp_or(a, b, c); - - if (((sa == MP_NEG) || (sb == MP_NEG)) && (err == MP_OKAY)) { - err = 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 err; -} -#endif diff --git a/bn_mp_tc_xor.c b/bn_mp_tc_xor.c deleted file mode 100644 index 0af0ed24c..000000000 --- a/bn_mp_tc_xor.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TC_XOR_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* two complement xor */ -mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) -{ - mp_err err = MP_OKAY; - int bits, abits, bbits; - mp_sign sa = a->sign, sb = b->sign; - mp_int *mx = NULL, _mx, acpy, bcpy; - - if ((sa == MP_NEG) || (sb == MP_NEG)) { - abits = mp_count_bits(a); - bbits = mp_count_bits(b); - bits = MP_MAX(abits, bbits); - err = mp_init_set_int(&_mx, 1uL); - if (err != MP_OKAY) { - goto end; - } - - mx = &_mx; - err = mp_mul_2d(mx, bits + 1, mx); - if (err != MP_OKAY) { - goto end; - } - - if (sa == MP_NEG) { - err = mp_init(&acpy); - if (err != MP_OKAY) { - goto end; - } - - err = mp_add(mx, a, &acpy); - if (err != MP_OKAY) { - mp_clear(&acpy); - goto end; - } - a = &acpy; - } - if (sb == MP_NEG) { - err = mp_init(&bcpy); - if (err != MP_OKAY) { - goto end; - } - - err = mp_add(mx, b, &bcpy); - if (err != MP_OKAY) { - mp_clear(&bcpy); - goto end; - } - b = &bcpy; - } - } - - err = mp_xor(a, b, c); - - if ((((sa == MP_NEG) && (sb != MP_NEG)) || ((sa != MP_NEG) && (sb == MP_NEG))) && (err == MP_OKAY)) { - err = 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 err; -} -#endif diff --git a/bn_mp_xor.c b/bn_mp_xor.c index d64706254..c066e658e 100644 --- a/bn_mp_xor.c +++ b/bn_mp_xor.c @@ -3,34 +3,54 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* XOR two ints together */ +/* two complement xor */ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) { - int ix, px; - mp_err err; - mp_int t; - const mp_int *x; + int used = MP_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 ((err = mp_init_copy(&t, a)) != MP_OKAY) { + if (c->alloc < used) { + if ((err = mp_grow(c, used)) != MP_OKAY) { return err; } - px = b->used; - x = b; - } else { - if ((err = mp_init_copy(&t, b)) != 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 ? 0 : 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 ? 0 : 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 diff --git a/demo/test.c b/demo/test.c index 82825b5da..12d20787c 100644 --- a/demo/test.c +++ b/demo/test.c @@ -347,7 +347,7 @@ static int test_mp_complement(void) return EXIT_FAILURE; } -static int test_mp_tc_div_2d(void) +static int test_mp_signed_rsh(void) { int i; @@ -371,9 +371,9 @@ static int test_mp_tc_div_2d(void) if ((l >> em) < 0) mp_neg(&d, &d); - mp_tc_div_2d(&a, em, &b); + mp_signed_rsh(&a, em, &b); if (mp_cmp(&b, &d) != MP_EQ) { - printf("\nmp_tc_div_2d() bad result!"); + printf("\nmp_signed_rsh() bad result!"); goto LBL_ERR; } } @@ -386,7 +386,7 @@ static int test_mp_tc_div_2d(void) } -static int test_mp_tc_xor(void) +static int test_mp_xor(void) { int i; @@ -412,9 +412,9 @@ static int test_mp_tc_xor(void) if ((l ^ em) < 0) mp_neg(&d, &d); - mp_tc_xor(&a, &b, &c); + mp_xor(&a, &b, &c); if (mp_cmp(&c, &d) != MP_EQ) { - printf("\nmp_tc_xor() bad result!"); + printf("\nmp_xor() bad result!"); goto LBL_ERR; } } @@ -427,7 +427,7 @@ static int test_mp_tc_xor(void) } -static int test_mp_tc_or(void) +static int test_mp_or(void) { int i; @@ -453,9 +453,9 @@ static int test_mp_tc_or(void) if ((l | em) < 0) mp_neg(&d, &d); - mp_tc_or(&a, &b, &c); + mp_or(&a, &b, &c); if (mp_cmp(&c, &d) != MP_EQ) { - printf("\nmp_tc_or() bad result!"); + printf("\nmp_or() bad result!"); goto LBL_ERR; } } @@ -467,7 +467,7 @@ static int test_mp_tc_or(void) return EXIT_FAILURE; } -static int test_mp_tc_and(void) +static int test_mp_and(void) { int i; @@ -493,9 +493,9 @@ static int test_mp_tc_and(void) if ((l & em) < 0) mp_neg(&d, &d); - mp_tc_and(&a, &b, &c); + mp_and(&a, &b, &c); if (mp_cmp(&c, &d) != MP_EQ) { - printf("\nmp_tc_and() bad result!"); + printf("\nmp_and() bad result!"); goto LBL_ERR; } } @@ -2021,6 +2021,7 @@ int unit_tests(int argc, char **argv) } test[] = { #define T(n) { #n, test_##n } T(trivial_stuff), + T(mp_and), T(mp_cnt_lsb), T(mp_complement), T(mp_decr), @@ -2037,6 +2038,7 @@ int unit_tests(int argc, char **argv) T(mp_kronecker), T(mp_montgomery_reduce), T(mp_n_root), + T(mp_or), T(mp_prime_is_prime), T(mp_prime_rand), T(mp_rand), @@ -2044,12 +2046,10 @@ int unit_tests(int argc, char **argv) T(mp_reduce_2k), T(mp_reduce_2k_l), T(mp_set_double), + T(mp_signed_rsh), T(mp_sqrt), T(mp_sqrtmod_prime), - T(mp_tc_and), - T(mp_tc_div_2d), - T(mp_tc_or), - T(mp_tc_xor), + T(mp_xor), T(s_mp_balance_mul), T(s_mp_jacobi), T(s_mp_karatsuba_mul), diff --git a/doc/bn.tex b/doc/bn.tex index f4bfda571..f9f8f6b63 100644 --- a/doc/bn.tex +++ b/doc/bn.tex @@ -1238,13 +1238,6 @@ \subsection{Multiplication by two} value to signal that the remainder is not desired. The division itself is implemented as a left-shift operation of $a$ by $b$ bits. -\index{mp\_tc\_div\_2d}\label{arithrightshift} -\begin{alltt} -int mp_tc_div_2d (mp_int * a, int b, mp_int * c, mp_int * d); -\end{alltt} -The two-co,mplement version of the function above. This can be used to implement arbitrary-precision two-complement integers together with the two-complement bit-wise operations at page \ref{tcbitwiseops}. - - It is also not very uncommon to need just the power of two $2^b$; for example the startvalue for the Newton method. \index{mp\_2expt} @@ -1280,30 +1273,20 @@ \subsection{Polynomial Basis Operations} \subsection{AND, OR, XOR and COMPLEMENT Operations} -While AND, OR and XOR operations are not typical ``bignum functions'' they can be useful in several instances. The -three functions are prototyped as follows. +While AND, OR and XOR operations compute arbitrary-precision bitwise operations. Negative numbers +are treated as if they are in two-complement representation, while internally they are sign-magnitude however. -\index{mp\_or} \index{mp\_and} \index{mp\_xor} +\index{mp\_or} \index{mp\_and} \index{mp\_xor} \index{mp\_complement} \begin{alltt} int mp_or (mp_int * a, mp_int * b, mp_int * c); int mp_and (mp_int * a, mp_int * b, mp_int * c); int mp_xor (mp_int * a, mp_int * b, mp_int * c); -\end{alltt} - -Which compute $c = a \odot b$ where $\odot$ is one of OR, AND or XOR. - -The following four functions allow implementing arbitrary-precision two-complement numbers. - -\index{mp\_tc\_or} \index{mp\_tc\_and} \index{mp\_tc\_xor} \index{mp\_complement} \label{tcbitwiseops} -\begin{alltt} -int mp_tc_or (mp_int * a, mp_int * b, mp_int * c); -int mp_tc_and (mp_int * a, mp_int * b, mp_int * c); -int mp_tc_xor (mp_int * a, mp_int * b, mp_int * c); int mp_complement(const mp_int *a, mp_int *b); +int mp_signed_rsh(mp_int * a, int b, mp_int * c, mp_int * d); \end{alltt} -They compute $c = a \odot b$ as above if both $a$ and $b$ are positive. Negative values are converted into their two-complement representations first. The function \texttt{mp\_complement} computes a two-complement $b = \sim a$. - +The function \texttt{mp\_complement} computes a two-complement $b = \sim a$. The function \texttt{mp\_signed\_rsh} performs +sign extending right shift. For positive numbers it is equivalent to \texttt{mp\_div\_2d}. \subsection{Bit Picking} \index{mp\_get\_bit} diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj index 7b054169c..84e0b16e9 100644 --- a/libtommath_VS2008.vcproj +++ b/libtommath_VS2008.vcproj @@ -728,6 +728,10 @@ RelativePath="bn_mp_signed_bin_size.c" > + + @@ -756,22 +760,6 @@ RelativePath="bn_mp_submod.c" > - - - - - - - - diff --git a/makefile b/makefile index 16232cbb2..6dbe1ab1b 100644 --- a/makefile +++ b/makefile @@ -45,15 +45,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin. bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \ bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \ -bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \ -bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \ -bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \ -bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \ -bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \ -bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \ -bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \ -bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ -bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \ +bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \ +bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \ +bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \ +bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \ +bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \ +bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \ +bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \ +bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o #END_INS diff --git a/makefile.mingw b/makefile.mingw index 165920300..2ed79b3f1 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -48,15 +48,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin. bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \ bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \ -bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \ -bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \ -bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \ -bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \ -bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \ -bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \ -bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \ -bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ -bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \ +bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \ +bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \ +bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \ +bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \ +bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \ +bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \ +bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \ +bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) diff --git a/makefile.msvc b/makefile.msvc index b7a28c861..e957530c6 100644 --- a/makefile.msvc +++ b/makefile.msvc @@ -40,15 +40,15 @@ bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj bn_mp_read_unsigne bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj bn_mp_reduce_2k_setup.obj bn_mp_reduce_2k_setup_l.obj \ bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_rshd.obj bn_mp_set.obj \ bn_mp_set_double.obj bn_mp_set_int.obj bn_mp_set_long.obj bn_mp_set_long_long.obj bn_mp_shrink.obj \ -bn_mp_signed_bin_size.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj bn_mp_sub.obj \ -bn_mp_sub_d.obj bn_mp_submod.obj bn_mp_tc_and.obj bn_mp_tc_div_2d.obj bn_mp_tc_or.obj bn_mp_tc_xor.obj \ -bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin.obj bn_mp_to_unsigned_bin_n.obj \ -bn_mp_toradix.obj bn_mp_toradix_n.obj bn_mp_unsigned_bin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj \ -bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_get_bit.obj \ -bn_s_mp_invmod_fast.obj bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj \ -bn_s_mp_montgomery_reduce_fast.obj bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj \ -bn_s_mp_mul_high_digs_fast.obj bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj \ -bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj +bn_mp_signed_bin_size.obj bn_mp_signed_rsh.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj \ +bn_mp_sub.obj bn_mp_sub_d.obj bn_mp_submod.obj bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj \ +bn_mp_to_unsigned_bin.obj bn_mp_to_unsigned_bin_n.obj bn_mp_toradix.obj bn_mp_toradix_n.obj \ +bn_mp_unsigned_bin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj bn_s_mp_add.obj bn_s_mp_balance_mul.obj \ +bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_get_bit.obj bn_s_mp_invmod_fast.obj bn_s_mp_invmod_slow.obj \ +bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj bn_s_mp_montgomery_reduce_fast.obj bn_s_mp_mul_digs.obj \ +bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs_fast.obj bn_s_mp_rand_jenkins.obj \ +bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj \ +bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) diff --git a/makefile.shared b/makefile.shared index b4be47c33..371291b78 100644 --- a/makefile.shared +++ b/makefile.shared @@ -42,15 +42,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin. bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \ bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \ -bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \ -bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \ -bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \ -bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \ -bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \ -bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \ -bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \ -bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ -bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \ +bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \ +bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \ +bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \ +bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \ +bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \ +bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \ +bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \ +bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o #END_INS diff --git a/makefile.unix b/makefile.unix index 7a1548f1d..2831040dd 100644 --- a/makefile.unix +++ b/makefile.unix @@ -49,15 +49,15 @@ bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin. bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_rshd.o bn_mp_set.o \ bn_mp_set_double.o bn_mp_set_int.o bn_mp_set_long.o bn_mp_set_long_long.o bn_mp_shrink.o \ -bn_mp_signed_bin_size.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o \ -bn_mp_sub_d.o bn_mp_submod.o bn_mp_tc_and.o bn_mp_tc_div_2d.o bn_mp_tc_or.o bn_mp_tc_xor.o \ -bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o \ -bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o \ -bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o \ -bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o \ -bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o \ -bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ -bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_signed_bin_size.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o \ +bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o \ +bn_mp_to_unsigned_bin.o bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o \ +bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o \ +bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o \ +bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o \ +bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_rand_jenkins.o \ +bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \ +bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) diff --git a/tommath.h b/tommath.h index ba14826d4..2349306d6 100644 --- a/tommath.h +++ b/tommath.h @@ -367,14 +367,6 @@ extern void (*ltm_rng_callback)(void); #endif /* ---> binary operations <--- */ -/* c = a XOR b */ -mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; - -/* c = a OR b */ -mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; - -/* c = a AND b */ -mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; /* Checks the bit at position b and returns MP_YES * if the bit is 1, MP_NO if it is 0 and MP_VAL @@ -383,22 +375,26 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int *a, int b) MP_WUR; /* c = a XOR b (two complement) */ -mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; +MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; +mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; /* c = a OR b (two complement) */ -mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; +MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; +mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; /* c = a AND b (two complement) */ -mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; +MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; +mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; + +/* b = ~a (bitwise not, two complement) */ +mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR; -/* right shift (two complement) */ -mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR; +/* right shift with sign extension */ +MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR; +mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) MP_WUR; /* ---> Basic arithmetic <--- */ -/* b = ~a */ -mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR; - /* b = -a */ mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR; diff --git a/tommath_class.h b/tommath_class.h index d51c7f8b7..b7e59b255 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -114,6 +114,7 @@ # define BN_MP_SET_LONG_LONG_C # define BN_MP_SHRINK_C # define BN_MP_SIGNED_BIN_SIZE_C +# define BN_MP_SIGNED_RSH_C # define BN_MP_SQR_C # define BN_MP_SQRMOD_C # define BN_MP_SQRT_C @@ -121,10 +122,6 @@ # define BN_MP_SUB_C # 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_TC_OR_C -# define BN_MP_TC_XOR_C # define BN_MP_TO_SIGNED_BIN_C # define BN_MP_TO_SIGNED_BIN_N_C # define BN_MP_TO_UNSIGNED_BIN_C @@ -197,6 +194,14 @@ # define BN_MP_TOOM_SQR_C # define BN_S_MP_TOOM_SQR_C # define BN_S_MP_REVERSE_C +# define BN_MP_TC_AND_C +# define BN_MP_AND_C +# define BN_MP_TC_OR_C +# define BN_MP_OR_C +# define BN_MP_TC_XOR_C +# define BN_MP_XOR_C +# define BN_MP_TC_DIV_2D_C +# define BN_MP_SIGNED_RSH_C #endif #if defined(BN_MP_2EXPT_C) @@ -228,10 +233,8 @@ #endif #if defined(BN_MP_AND_C) -# define BN_MP_INIT_COPY_C +# define BN_MP_GROW_C # define BN_MP_CLAMP_C -# define BN_MP_EXCH_C -# define BN_MP_CLEAR_C #endif #if defined(BN_MP_CLAMP_C) @@ -632,10 +635,8 @@ #endif #if defined(BN_MP_OR_C) -# define BN_MP_INIT_COPY_C +# define BN_MP_GROW_C # define BN_MP_CLAMP_C -# define BN_MP_EXCH_C -# define BN_MP_CLEAR_C #endif #if defined(BN_MP_PRIME_FERMAT_C) @@ -887,6 +888,12 @@ # define BN_MP_UNSIGNED_BIN_SIZE_C #endif +#if defined(BN_MP_SIGNED_RSH_C) +# define BN_MP_DIV_2D_C +# define BN_MP_ADD_D_C +# define BN_MP_SUB_D_C +#endif + #if defined(BN_MP_SQR_C) # define BN_S_MP_TOOM_SQR_C # define BN_S_MP_KARATSUBA_SQR_C @@ -950,45 +957,6 @@ # define BN_MP_MOD_C #endif -#if defined(BN_MP_TC_AND_C) -# define BN_MP_COUNT_BITS_C -# define BN_MP_INIT_SET_INT_C -# define BN_MP_MUL_2D_C -# define BN_MP_INIT_C -# define BN_MP_ADD_C -# define BN_MP_CLEAR_C -# define BN_MP_AND_C -# define BN_MP_SUB_C -#endif - -#if defined(BN_MP_TC_DIV_2D_C) -# define BN_MP_DIV_2D_C -# define BN_MP_ADD_D_C -# define BN_MP_SUB_D_C -#endif - -#if defined(BN_MP_TC_OR_C) -# define BN_MP_COUNT_BITS_C -# define BN_MP_INIT_SET_INT_C -# define BN_MP_MUL_2D_C -# define BN_MP_INIT_C -# define BN_MP_ADD_C -# define BN_MP_CLEAR_C -# define BN_MP_OR_C -# define BN_MP_SUB_C -#endif - -#if defined(BN_MP_TC_XOR_C) -# define BN_MP_COUNT_BITS_C -# define BN_MP_INIT_SET_INT_C -# define BN_MP_MUL_2D_C -# define BN_MP_INIT_C -# define BN_MP_ADD_C -# define BN_MP_CLEAR_C -# define BN_MP_XOR_C -# define BN_MP_SUB_C -#endif - #if defined(BN_MP_TO_SIGNED_BIN_C) # define BN_MP_TO_UNSIGNED_BIN_C #endif @@ -1029,10 +997,8 @@ #endif #if defined(BN_MP_XOR_C) -# define BN_MP_INIT_COPY_C +# define BN_MP_GROW_C # define BN_MP_CLAMP_C -# define BN_MP_EXCH_C -# define BN_MP_CLEAR_C #endif #if defined(BN_MP_ZERO_C)