Skip to content

Commit

Permalink
first batch of simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Oct 29, 2019
1 parent b9977ad commit 1d30e55
Show file tree
Hide file tree
Showing 40 changed files with 199 additions and 290 deletions.
3 changes: 1 addition & 2 deletions mp_abs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
*/
mp_err mp_abs(const mp_int *a, mp_int *b)
{
mp_err err;

/* copy a to b */
if (a != b) {
mp_err err;
if ((err = mp_copy(a, b)) != MP_OKAY) {
return err;
}
Expand Down
37 changes: 14 additions & 23 deletions mp_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,24 @@
/* high level addition (handles signs) */
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
{
mp_sign sa, sb;
mp_err err;

/* get sign of both inputs */
sa = a->sign;
sb = b->sign;

/* handle two cases, not four */
if (sa == sb) {
if (a->sign == b->sign) {
/* both positive or both negative */
/* add their magnitudes, copy the sign */
c->sign = sa;
err = s_mp_add(a, b, c);
} else {
/* one positive, the other negative */
/* subtract the one with the greater magnitude from */
/* the one of the lesser magnitude. The result gets */
/* the sign of the one with the greater magnitude. */
if (mp_cmp_mag(a, b) == MP_LT) {
c->sign = sb;
err = s_mp_sub(b, a, c);
} else {
c->sign = sa;
err = s_mp_sub(a, b, c);
}
c->sign = a->sign;
return s_mp_add(a, b, c);
}
return err;

/* one positive, the other negative */
/* subtract the one with the greater magnitude from */
/* the one of the lesser magnitude. The result gets */
/* the sign of the one with the greater magnitude. */
if (mp_cmp_mag(a, b) == MP_LT) {
MP_EXCH(const mp_int *, a, b);
}

c->sign = a->sign;
return s_mp_sub(a, b, c);
}

#endif
7 changes: 3 additions & 4 deletions mp_clear_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

void mp_clear_multi(mp_int *mp, ...)
{
mp_int *next_mp = mp;
va_list args;
va_start(args, mp);
while (next_mp != NULL) {
mp_clear(next_mp);
next_mp = va_arg(args, mp_int *);
while (mp != NULL) {
mp_clear(mp);
mp = va_arg(args, mp_int *);
}
va_end(args);
}
Expand Down
15 changes: 5 additions & 10 deletions mp_cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ mp_ord mp_cmp(const mp_int *a, const mp_int *b)
{
/* compare based on sign */
if (a->sign != b->sign) {
if (a->sign == MP_NEG) {
return MP_LT;
} else {
return MP_GT;
}
return a->sign == MP_NEG ? MP_LT : MP_GT;
}

/* compare digits */
/* if negative compare opposite direction */
if (a->sign == MP_NEG) {
/* if negative compare opposite direction */
return mp_cmp_mag(b, a);
} else {
return mp_cmp_mag(a, b);
MP_EXCH(const mp_int *, a, b);
}

return mp_cmp_mag(a, b);
}
#endif
10 changes: 4 additions & 6 deletions mp_cmp_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
}

/* compare the only digit of a to b */
if (a->dp[0] > b) {
return MP_GT;
} else if (a->dp[0] < b) {
return MP_LT;
} else {
return MP_EQ;
if (a->dp[0] != b) {
return a->dp[0] > b ? MP_GT : MP_LT;
}

return MP_EQ;
}
#endif
28 changes: 7 additions & 21 deletions mp_cmp_mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,20 @@
/* compare maginitude of two ints (unsigned) */
mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
{
int n;
const mp_digit *tmpa, *tmpb;
int n;

/* compare based on # of non-zero digits */
if (a->used > b->used) {
return MP_GT;
if (a->used != b->used) {
return a->used > b->used ? MP_GT : MP_LT;
}

if (a->used < b->used) {
return MP_LT;
}

/* alias for a */
tmpa = a->dp + (a->used - 1);

/* alias for b */
tmpb = b->dp + (a->used - 1);

/* compare based on digits */
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
if (*tmpa > *tmpb) {
return MP_GT;
}

if (*tmpa < *tmpb) {
return MP_LT;
for (n = a->used; n --> 0;) {
if (a->dp[n] != b->dp[n]) {
return a->dp[n] > b->dp[n] ? MP_GT : MP_LT;
}
}

return MP_EQ;
}
#endif
11 changes: 6 additions & 5 deletions mp_cnt_lsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

static const int lnz[16] = {
static const char lnz[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};

/* Counts the number of lsbs which are zero before the first zero bit */
int mp_cnt_lsb(const mp_int *a)
{
int x;
mp_digit q, qq;
mp_digit q;

/* easy out */
if (mp_iszero(a)) {
Expand All @@ -25,11 +25,12 @@ int mp_cnt_lsb(const mp_int *a)

/* now scan this digit until a 1 is found */
if ((q & 1u) == 0u) {
mp_digit p;
do {
qq = q & 15u;
x += lnz[qq];
p = q & 15u;
x += lnz[p];
q >>= 4;
} while (qq == 0u);
} while (p == 0u);
}
return x;
}
Expand Down
14 changes: 3 additions & 11 deletions mp_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
mp_err mp_copy(const mp_int *a, mp_int *b)
{
int n;
mp_digit *tmpa, *tmpb;
mp_err err;

/* if dst == src do nothing */
if (a == b) {
Expand All @@ -17,27 +15,21 @@ mp_err mp_copy(const mp_int *a, mp_int *b)

/* grow dest */
if (b->alloc < a->used) {
mp_err err;
if ((err = mp_grow(b, a->used)) != MP_OKAY) {
return err;
}
}

/* zero b and copy the parameters over */
/* pointer aliases */

/* source */
tmpa = a->dp;

/* destination */
tmpb = b->dp;

/* copy all the digits */
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
b->dp[n] = a->dp[n];
}

/* clear high digits */
MP_ZERO_DIGITS(tmpb, b->used - n);
MP_ZERO_DIGITS(b->dp + a->used, b->used - a->used);

/* copy used count and sign */
b->used = a->used;
Expand Down
13 changes: 7 additions & 6 deletions mp_div.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
/* if a < b then q = 0, r = a */
if (mp_cmp_mag(a, b) == MP_LT) {
if (d != NULL) {
err = mp_copy(a, d);
} else {
err = MP_OKAY;
if ((err = mp_copy(a, d)) != MP_OKAY) {
return err;
}
}
if (c != NULL) {
mp_zero(c);
}
return err;
return MP_OKAY;
}

if (MP_HAS(S_MP_DIV_RECURSIVE)
Expand All @@ -31,11 +31,12 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
err = s_mp_div_recursive(a, b, c, d);
} else if (MP_HAS(S_MP_DIV_SCHOOL)) {
err = s_mp_div_school(a, b, c, d);
} else {
} else if (MP_HAS(S_MP_DIV_SMALL)) {
err = s_mp_div_small(a, b, c, d);
} else {
err = MP_VAL;
}

return err;
}
#endif

7 changes: 4 additions & 3 deletions mp_div_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
{
mp_int q;
mp_word w, t;
mp_word w;
mp_digit b;
mp_err err;
int ix;
Expand All @@ -22,7 +22,8 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
q.used = a->used;
q.sign = a->sign;
w = 0;
for (ix = a->used - 1; ix >= 0; ix--) {
for (ix = a->used; ix --> 0;) {
mp_word t;
w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix];

if (w >= 3u) {
Expand Down Expand Up @@ -57,7 +58,7 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
}
mp_clear(&q);

return err;
return MP_OKAY;
}

#endif
6 changes: 1 addition & 5 deletions mp_exch.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
*/
void mp_exch(mp_int *a, mp_int *b)
{
mp_int t;

t = *a;
*a = *b;
*b = t;
MP_EXCH(mp_int, *a, *b);
}
#endif
12 changes: 7 additions & 5 deletions mp_exptmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
/* if the modulus is odd or dr != 0 use the montgomery method */
if (MP_HAS(S_MP_EXPTMOD_FAST) && (mp_isodd(P) || (dr != 0))) {
return s_mp_exptmod_fast(G, X, P, Y, dr);
} else if (MP_HAS(S_MP_EXPTMOD)) {
/* otherwise use the generic Barrett reduction technique */
}

/* otherwise use the generic Barrett reduction technique */
if (MP_HAS(S_MP_EXPTMOD)) {
return s_mp_exptmod(G, X, P, Y, 0);
} else {
/* no exptmod for evens */
return MP_VAL;
}

/* no exptmod for evens */
return MP_VAL;
}

#endif
1 change: 0 additions & 1 deletion mp_exteuclid.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp
mp_exch(U3, &u3);
}

err = MP_OKAY;
LBL_ERR:
mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
return err;
Expand Down
6 changes: 3 additions & 3 deletions mp_fread.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
mp_zero(a);

do {
int y;
uint8_t y;
unsigned pos;
ch = (radix <= 36) ? MP_TOUPPER(ch) : ch;
pos = (unsigned)(ch - (int)'(');
if (MP_RMAP_REVERSE_SIZE < pos) {
break;
}

y = (int)s_mp_rmap_reverse[pos];
y = s_mp_rmap_reverse[pos];

if ((y == 0xff) || (y >= radix)) {
break;
Expand All @@ -50,7 +50,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
return err;
}
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
return err;
}
} while ((ch = fgetc(stream)) != EOF);
Expand Down
6 changes: 1 addition & 5 deletions mp_from_sbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size)
}

/* first byte is 0 for positive, non-zero for negative */
if (buf[0] == (uint8_t)0) {
a->sign = MP_ZPOS;
} else {
a->sign = MP_NEG;
}
a->sign = (buf[0] == (uint8_t)0) ? MP_ZPOS : MP_NEG;

return MP_OKAY;
}
Expand Down
Loading

0 comments on commit 1d30e55

Please sign in to comment.