Skip to content

Commit 3cdcec4

Browse files
committed
first batch of simplifications
1 parent b9977ad commit 3cdcec4

40 files changed

+200
-291
lines changed

mp_abs.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
*/
1010
mp_err mp_abs(const mp_int *a, mp_int *b)
1111
{
12-
mp_err err;
13-
1412
/* copy a to b */
1513
if (a != b) {
14+
mp_err err;
1615
if ((err = mp_copy(a, b)) != MP_OKAY) {
1716
return err;
1817
}

mp_add.c

+14-23
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,24 @@
66
/* high level addition (handles signs) */
77
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
88
{
9-
mp_sign sa, sb;
10-
mp_err err;
11-
12-
/* get sign of both inputs */
13-
sa = a->sign;
14-
sb = b->sign;
15-
169
/* handle two cases, not four */
17-
if (sa == sb) {
10+
if (a->sign == b->sign) {
1811
/* both positive or both negative */
1912
/* add their magnitudes, copy the sign */
20-
c->sign = sa;
21-
err = s_mp_add(a, b, c);
22-
} else {
23-
/* one positive, the other negative */
24-
/* subtract the one with the greater magnitude from */
25-
/* the one of the lesser magnitude. The result gets */
26-
/* the sign of the one with the greater magnitude. */
27-
if (mp_cmp_mag(a, b) == MP_LT) {
28-
c->sign = sb;
29-
err = s_mp_sub(b, a, c);
30-
} else {
31-
c->sign = sa;
32-
err = s_mp_sub(a, b, c);
33-
}
13+
c->sign = a->sign;
14+
return s_mp_add(a, b, c);
3415
}
35-
return err;
16+
17+
/* one positive, the other negative */
18+
/* subtract the one with the greater magnitude from */
19+
/* the one of the lesser magnitude. The result gets */
20+
/* the sign of the one with the greater magnitude. */
21+
if (mp_cmp_mag(a, b) == MP_LT) {
22+
MP_EXCH(const mp_int *, a, b);
23+
}
24+
25+
c->sign = a->sign;
26+
return s_mp_sub(a, b, c);
3627
}
3728

3829
#endif

mp_clear_multi.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
void mp_clear_multi(mp_int *mp, ...)
99
{
10-
mp_int *next_mp = mp;
1110
va_list args;
1211
va_start(args, mp);
13-
while (next_mp != NULL) {
14-
mp_clear(next_mp);
15-
next_mp = va_arg(args, mp_int *);
12+
while (mp != NULL) {
13+
mp_clear(mp);
14+
mp = va_arg(args, mp_int *);
1615
}
1716
va_end(args);
1817
}

mp_cmp.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ mp_ord mp_cmp(const mp_int *a, const mp_int *b)
88
{
99
/* compare based on sign */
1010
if (a->sign != b->sign) {
11-
if (a->sign == MP_NEG) {
12-
return MP_LT;
13-
} else {
14-
return MP_GT;
15-
}
11+
return a->sign == MP_NEG ? MP_LT : MP_GT;
1612
}
1713

18-
/* compare digits */
14+
/* if negative compare opposite direction */
1915
if (a->sign == MP_NEG) {
20-
/* if negative compare opposite direction */
21-
return mp_cmp_mag(b, a);
22-
} else {
23-
return mp_cmp_mag(a, b);
16+
MP_EXCH(const mp_int *, a, b);
2417
}
18+
19+
return mp_cmp_mag(a, b);
2520
}
2621
#endif

mp_cmp_d.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
1717
}
1818

1919
/* compare the only digit of a to b */
20-
if (a->dp[0] > b) {
21-
return MP_GT;
22-
} else if (a->dp[0] < b) {
23-
return MP_LT;
24-
} else {
25-
return MP_EQ;
20+
if (a->dp[0] != b) {
21+
return a->dp[0] > b ? MP_GT : MP_LT;
2622
}
23+
24+
return MP_EQ;
2725
}
2826
#endif

mp_cmp_mag.c

+7-21
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,20 @@
66
/* compare maginitude of two ints (unsigned) */
77
mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
88
{
9-
int n;
10-
const mp_digit *tmpa, *tmpb;
9+
int n;
1110

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

17-
if (a->used < b->used) {
18-
return MP_LT;
19-
}
20-
21-
/* alias for a */
22-
tmpa = a->dp + (a->used - 1);
23-
24-
/* alias for b */
25-
tmpb = b->dp + (a->used - 1);
26-
2716
/* compare based on digits */
28-
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
29-
if (*tmpa > *tmpb) {
30-
return MP_GT;
31-
}
32-
33-
if (*tmpa < *tmpb) {
34-
return MP_LT;
17+
for (n = a->used; n --> 0;) {
18+
if (a->dp[n] != b->dp[n]) {
19+
return a->dp[n] > b->dp[n] ? MP_GT : MP_LT;
3520
}
3621
}
22+
3723
return MP_EQ;
3824
}
3925
#endif

mp_cnt_lsb.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

6-
static const int lnz[16] = {
6+
static const char lnz[16] = {
77
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
88
};
99

1010
/* Counts the number of lsbs which are zero before the first zero bit */
1111
int mp_cnt_lsb(const mp_int *a)
1212
{
1313
int x;
14-
mp_digit q, qq;
14+
mp_digit q;
1515

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

2626
/* now scan this digit until a 1 is found */
2727
if ((q & 1u) == 0u) {
28+
mp_digit p;
2829
do {
29-
qq = q & 15u;
30-
x += lnz[qq];
30+
p = q & 15u;
31+
x += lnz[p];
3132
q >>= 4;
32-
} while (qq == 0u);
33+
} while (p == 0u);
3334
}
3435
return x;
3536
}

mp_copy.c

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
mp_err mp_copy(const mp_int *a, mp_int *b)
88
{
99
int n;
10-
mp_digit *tmpa, *tmpb;
11-
mp_err err;
1210

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

1816
/* grow dest */
1917
if (b->alloc < a->used) {
18+
mp_err err;
2019
if ((err = mp_grow(b, a->used)) != MP_OKAY) {
2120
return err;
2221
}
2322
}
2423

2524
/* zero b and copy the parameters over */
26-
/* pointer aliases */
27-
28-
/* source */
29-
tmpa = a->dp;
30-
31-
/* destination */
32-
tmpb = b->dp;
3325

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

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

4234
/* copy used count and sign */
4335
b->used = a->used;

mp_div.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
1515
/* if a < b then q = 0, r = a */
1616
if (mp_cmp_mag(a, b) == MP_LT) {
1717
if (d != NULL) {
18-
err = mp_copy(a, d);
19-
} else {
20-
err = MP_OKAY;
18+
if ((err = mp_copy(a, d)) != MP_OKAY) {
19+
return err;
20+
}
2121
}
2222
if (c != NULL) {
2323
mp_zero(c);
2424
}
25-
return err;
25+
return MP_OKAY;
2626
}
2727

2828
if (MP_HAS(S_MP_DIV_RECURSIVE)
@@ -31,11 +31,12 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
3131
err = s_mp_div_recursive(a, b, c, d);
3232
} else if (MP_HAS(S_MP_DIV_SCHOOL)) {
3333
err = s_mp_div_school(a, b, c, d);
34-
} else {
34+
} else if (MP_HAS(S_MP_DIV_SMALL)) {
3535
err = s_mp_div_small(a, b, c, d);
36+
} else {
37+
err = MP_VAL;
3638
}
3739

3840
return err;
3941
}
4042
#endif
41-

mp_div_3.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
88
{
99
mp_int q;
10-
mp_word w, t;
10+
mp_word w;
1111
mp_digit b;
1212
mp_err err;
1313
int ix;
@@ -22,7 +22,8 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
2222
q.used = a->used;
2323
q.sign = a->sign;
2424
w = 0;
25-
for (ix = a->used - 1; ix >= 0; ix--) {
25+
for (ix = a->used; ix --> 0;) {
26+
mp_word t;
2627
w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix];
2728

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

60-
return err;
61+
return MP_OKAY;
6162
}
6263

6364
#endif

mp_exch.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
*/
99
void mp_exch(mp_int *a, mp_int *b)
1010
{
11-
mp_int t;
12-
13-
t = *a;
14-
*a = *b;
15-
*b = t;
11+
MP_EXCH(mp_int, *a, *b);
1612
}
1713
#endif

mp_exptmod.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
6464
/* if the modulus is odd or dr != 0 use the montgomery method */
6565
if (MP_HAS(S_MP_EXPTMOD_FAST) && (mp_isodd(P) || (dr != 0))) {
6666
return s_mp_exptmod_fast(G, X, P, Y, dr);
67-
} else if (MP_HAS(S_MP_EXPTMOD)) {
68-
/* otherwise use the generic Barrett reduction technique */
67+
}
68+
69+
/* otherwise use the generic Barrett reduction technique */
70+
if (MP_HAS(S_MP_EXPTMOD)) {
6971
return s_mp_exptmod(G, X, P, Y, 0);
70-
} else {
71-
/* no exptmod for evens */
72-
return MP_VAL;
7372
}
73+
74+
/* no exptmod for evens */
75+
return MP_VAL;
7476
}
7577

7678
#endif

mp_exteuclid.c

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp
6565
mp_exch(U3, &u3);
6666
}
6767

68-
err = MP_OKAY;
6968
LBL_ERR:
7069
mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
7170
return err;

mp_fread.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
3232
mp_zero(a);
3333

3434
do {
35-
int y;
35+
uint8_t y;
3636
unsigned pos;
3737
ch = (radix <= 36) ? MP_TOUPPER(ch) : ch;
3838
pos = (unsigned)(ch - (int)'(');
3939
if (MP_RMAP_REVERSE_SIZE < pos) {
4040
break;
4141
}
4242

43-
y = (int)s_mp_rmap_reverse[pos];
43+
y = s_mp_rmap_reverse[pos];
4444

4545
if ((y == 0xff) || (y >= radix)) {
4646
break;
@@ -50,7 +50,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
5050
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
5151
return err;
5252
}
53-
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
53+
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
5454
return err;
5555
}
5656
} while ((ch = fgetc(stream)) != EOF);

mp_from_sbin.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size)
1414
}
1515

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

2319
return MP_OKAY;
2420
}

0 commit comments

Comments
 (0)