Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions bn_deprecated.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 4 additions & 10 deletions bn_mp_add_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down
60 changes: 38 additions & 22 deletions bn_mp_and.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions bn_mp_lshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
60 changes: 40 additions & 20 deletions bn_mp_or.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions bn_mp_rshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions bn_mp_signed_rsh.c
Original file line number Diff line number Diff line change
@@ -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
13 changes: 5 additions & 8 deletions bn_mp_sub_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
78 changes: 0 additions & 78 deletions bn_mp_tc_and.c

This file was deleted.

22 changes: 0 additions & 22 deletions bn_mp_tc_div_2d.c

This file was deleted.

Loading