Skip to content

Commit 59d62c9

Browse files
authored
Merge pull request #454 from libtom/better-use-of-isneg
Better use of isneg
2 parents 0bc5c32 + 1cc289d commit 59d62c9

39 files changed

+91
-99
lines changed

etc/mersenne.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static mp_err is_mersenne(long s, bool *pp)
4343
}
4444

4545
/* make sure u is positive */
46-
while (u.sign == MP_NEG) {
46+
while (mp_isneg(&u)) {
4747
if ((res = mp_add(&u, &n, &u)) != MP_OKAY) {
4848
goto LBL_MU;
4949
}

mp_add_d.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
1111

1212
/* fast path for a == c */
1313
if (a == c) {
14-
if ((c->sign == MP_ZPOS) &&
14+
if (!mp_isneg(c) &&
1515
!mp_iszero(c) &&
1616
((c->dp[0] + b) < MP_DIGIT_MAX)) {
1717
c->dp[0] += b;
1818
return MP_OKAY;
1919
}
20-
if ((c->sign == MP_NEG) &&
20+
if (mp_isneg(c) &&
2121
(c->dp[0] > b)) {
2222
c->dp[0] -= b;
2323
return MP_OKAY;
@@ -30,7 +30,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
3030
}
3131

3232
/* if a is negative and |a| >= b, call c = |a| - b */
33-
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
33+
if (mp_isneg(a) && ((a->used > 1) || (a->dp[0] >= b))) {
3434
mp_int a_ = *a;
3535
/* temporarily fix sign of a */
3636
a_.sign = MP_ZPOS;
@@ -51,7 +51,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
5151
oldused = c->used;
5252

5353
/* if a is positive */
54-
if (a->sign == MP_ZPOS) {
54+
if (!mp_isneg(a)) {
5555
/* add digits, mu is carry */
5656
int i;
5757
mp_digit mu = b;

mp_and.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
99
int used = MP_MAX(a->used, b->used) + 1, i;
1010
mp_err err;
1111
mp_digit ac = 1, bc = 1, cc = 1;
12-
mp_sign csign = ((a->sign == MP_NEG) && (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
12+
bool neg = (mp_isneg(a) && mp_isneg(b));
1313

1414
if ((err = mp_grow(c, used)) != MP_OKAY) {
1515
return err;
@@ -19,7 +19,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
1919
mp_digit x, y;
2020

2121
/* convert to two complement if negative */
22-
if (a->sign == MP_NEG) {
22+
if (mp_isneg(a)) {
2323
ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
2424
x = ac & MP_MASK;
2525
ac >>= MP_DIGIT_BIT;
@@ -28,7 +28,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
2828
}
2929

3030
/* convert to two complement if negative */
31-
if (b->sign == MP_NEG) {
31+
if (mp_isneg(b)) {
3232
bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
3333
y = bc & MP_MASK;
3434
bc >>= MP_DIGIT_BIT;
@@ -39,15 +39,15 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
3939
c->dp[i] = x & y;
4040

4141
/* convert to to sign-magnitude if negative */
42-
if (csign == MP_NEG) {
42+
if (neg) {
4343
cc += ~c->dp[i] & MP_MASK;
4444
c->dp[i] = cc & MP_MASK;
4545
cc >>= MP_DIGIT_BIT;
4646
}
4747
}
4848

4949
c->used = used;
50-
c->sign = csign;
50+
c->sign = (neg ? MP_NEG : MP_ZPOS);
5151
mp_clamp(c);
5252
return MP_OKAY;
5353
}

mp_clamp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ void mp_clamp(mp_int *a)
1919
--(a->used);
2020
}
2121

22-
/* reset the sign flag if used == 0 */
23-
if (a->used == 0) {
22+
/* reset the sign flag if zero */
23+
if (mp_iszero(a)) {
2424
a->sign = MP_ZPOS;
2525
}
2626
}

mp_cmp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ 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-
return a->sign == MP_NEG ? MP_LT : MP_GT;
11+
return mp_isneg(a) ? MP_LT : MP_GT;
1212
}
1313

1414
/* if negative compare opposite direction */
15-
if (a->sign == MP_NEG) {
15+
if (mp_isneg(a)) {
1616
MP_EXCH(const mp_int *, a, b);
1717
}
1818

mp_cmp_d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
88
{
99
/* compare based on sign */
10-
if (a->sign == MP_NEG) {
10+
if (mp_isneg(a)) {
1111
return MP_LT;
1212
}
1313

mp_exptmod.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
1313
int dr;
1414

1515
/* modulus P must be positive */
16-
if (P->sign == MP_NEG) {
16+
if (mp_isneg(P)) {
1717
return MP_VAL;
1818
}
1919

2020
/* if exponent X is negative we have to recurse */
21-
if (X->sign == MP_NEG) {
21+
if (mp_isneg(X)) {
2222
mp_int tmpG, tmpX;
2323
mp_err err;
2424

mp_exteuclid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp
4848
}
4949

5050
/* make sure U3 >= 0 */
51-
if (u3.sign == MP_NEG) {
51+
if (mp_isneg(&u3)) {
5252
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) goto LBL_ERR;
5353
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) goto LBL_ERR;
5454
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) goto LBL_ERR;

mp_fread.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
mp_err mp_fread(mp_int *a, int radix, FILE *stream)
99
{
1010
mp_err err;
11-
mp_sign neg = MP_ZPOS;
11+
mp_sign sign = MP_ZPOS;
1212
int ch;
1313

1414
/* make sure the radix is ok */
@@ -19,7 +19,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
1919
/* if first digit is - then set negative */
2020
ch = fgetc(stream);
2121
if (ch == (int)'-') {
22-
neg = MP_NEG;
22+
sign = MP_NEG;
2323
ch = fgetc(stream);
2424
}
2525

@@ -56,7 +56,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
5656
} while ((ch = fgetc(stream)) != EOF);
5757

5858
if (!mp_iszero(a)) {
59-
a->sign = neg;
59+
a->sign = sign;
6060
}
6161

6262
return MP_OKAY;

mp_from_sbin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +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-
a->sign = (buf[0] == (uint8_t)0) ? MP_ZPOS : MP_NEG;
17+
a->sign = (buf[0] != (uint8_t)0) ? MP_NEG : MP_ZPOS;
1818

1919
return MP_OKAY;
2020
}

0 commit comments

Comments
 (0)