Skip to content

Commit 1cc289d

Browse files
nijtmanssjaeckel
authored andcommitted
better use of mp_isneg() and mp_iszero()
1 parent 0bc5c32 commit 1cc289d

39 files changed

+91
-99
lines changed

etc/mersenne.c

+1-1
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

+4-4
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

+5-5
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

+2-2
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

+2-2
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

+1-1
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

+2-2
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

+1-1
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

+3-3
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

+1-1
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
}

mp_get_double.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ double mp_get_double(const mp_int *a)
1313
for (i = a->used; i --> 0;) {
1414
d = (d * fac) + (double)a->dp[i];
1515
}
16-
return (a->sign == MP_NEG) ? -d : d;
16+
return mp_isneg(a) ? -d : d;
1717
}
1818
#endif

mp_invmod.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
88
{
99
/* b cannot be negative and has to be >1 */
10-
if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
10+
if (mp_isneg(b) || (mp_cmp_d(b, 1uL) != MP_GT)) {
1111
return MP_VAL;
1212
}
1313

mp_is_square.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mp_err mp_is_square(const mp_int *arg, bool *ret)
3636
/* Default to Non-square :) */
3737
*ret = false;
3838

39-
if (arg->sign == MP_NEG) {
39+
if (mp_isneg(arg)) {
4040
return MP_VAL;
4141
}
4242

mp_kronecker.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
5757
k = table[a->dp[0] & 7u];
5858
}
5959

60-
if (p1.sign == MP_NEG) {
60+
if (mp_isneg(&p1)) {
6161
p1.sign = MP_ZPOS;
62-
if (a1.sign == MP_NEG) {
62+
if (mp_isneg(&a1)) {
6363
k = -k;
6464
}
6565
}
@@ -88,7 +88,7 @@ mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
8888
k = k * table[p1.dp[0] & 7u];
8989
}
9090

91-
if (a1.sign == MP_NEG) {
91+
if (mp_isneg(&a1)) {
9292
/*
9393
* Compute k = (-1)^((a1)*(p1-1)/4) * k
9494
* a1.dp[0] + 1 cannot overflow because the MSB

mp_log_u32.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55

66
mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
77
{
8-
if (a->sign == MP_NEG) {
9-
return MP_VAL;
10-
}
11-
12-
if (mp_iszero(a)) {
13-
return MP_VAL;
14-
}
15-
16-
if (base < 2u) {
8+
if (mp_isneg(a) || mp_iszero(a) || (base < 2u)) {
179
return MP_VAL;
1810
}
1911

mp_mul.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
1010
int min = MP_MIN(a->used, b->used),
1111
max = MP_MAX(a->used, b->used),
1212
digs = a->used + b->used + 1;
13-
mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
13+
bool neg = (a->sign != b->sign);
1414

1515
if ((a == b) &&
1616
MP_HAS(S_MP_SQR_TOOM) && /* use Toom-Cook? */
@@ -62,7 +62,7 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
6262
} else {
6363
err = MP_VAL;
6464
}
65-
c->sign = (c->used > 0) ? neg : MP_ZPOS;
65+
c->sign = ((c->used > 0) && neg) ? MP_NEG : MP_ZPOS;
6666
return err;
6767
}
6868
#endif

mp_neg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mp_err mp_neg(const mp_int *a, mp_int *b)
1111
return err;
1212
}
1313

14-
b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG;
14+
b->sign = ((!mp_iszero(b) && !mp_isneg(b)) ? MP_NEG : MP_ZPOS);
1515

1616
return MP_OKAY;
1717
}

mp_or.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mp_err mp_or(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_or(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_or(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_or(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_prime_strong_lucas_selfridge.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
216216
*/
217217
oddness = mp_isodd(&Uz);
218218
if ((err = mp_div_2(&Uz, &Uz)) != MP_OKAY) goto LBL_LS_ERR;
219-
if ((Uz.sign == MP_NEG) && oddness) {
219+
if (mp_isneg(&Uz) && oddness) {
220220
if ((err = mp_sub_d(&Uz, 1uL, &Uz)) != MP_OKAY) goto LBL_LS_ERR;
221221
}
222222
if ((err = mp_add(&T3z, &T4z, &Vz)) != MP_OKAY) goto LBL_LS_ERR;
@@ -225,7 +225,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
225225
}
226226
oddness = mp_isodd(&Vz);
227227
if ((err = mp_div_2(&Vz, &Vz)) != MP_OKAY) goto LBL_LS_ERR;
228-
if ((Vz.sign == MP_NEG) && oddness) {
228+
if (mp_isneg(&Vz) && oddness) {
229229
if ((err = mp_sub_d(&Vz, 1uL, &Vz)) != MP_OKAY) goto LBL_LS_ERR;
230230
}
231231
if ((err = mp_mod(&Uz, a, &Uz)) != MP_OKAY) goto LBL_LS_ERR;

mp_radix_size.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
2727
}
2828

2929
/* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
30-
*size = (size_t)b + 2U + ((a->sign == MP_NEG) ? 1U : 0U);
30+
*size = (size_t)b + 2U + (mp_isneg(a) ? 1U : 0U);
3131

3232
LBL_ERR:
3333
return err;

mp_read_radix.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mp_err mp_read_radix(mp_int *a, const char *str, int radix)
88
{
99
mp_err err;
10-
mp_sign neg = MP_ZPOS;
10+
mp_sign sign = MP_ZPOS;
1111

1212
/* make sure the radix is ok */
1313
if ((radix < 2) || (radix > 64)) {
@@ -19,7 +19,7 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
1919
*/
2020
if (*str == '-') {
2121
++str;
22-
neg = MP_NEG;
22+
sign = MP_NEG;
2323
}
2424

2525
/* set the integer to the default of zero */
@@ -62,7 +62,7 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
6262

6363
/* set the sign only if a != 0 */
6464
if (!mp_iszero(a)) {
65-
a->sign = neg;
65+
a->sign = sign;
6666
}
6767
return MP_OKAY;
6868
}

mp_reduce_is_2k.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* determines if mp_reduce_2k can be used */
77
bool mp_reduce_is_2k(const mp_int *a)
88
{
9-
if (a->used == 0) {
9+
if (mp_iszero(a)) {
1010
return false;
1111
} else if (a->used == 1) {
1212
return true;

mp_reduce_is_2k_l.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* determines if reduce_2k_l can be used */
77
bool mp_reduce_is_2k_l(const mp_int *a)
88
{
9-
if (a->used == 0) {
9+
if (mp_iszero(a)) {
1010
return false;
1111
} else if (a->used == 1) {
1212
return true;

mp_root_u32.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
2020
mp_err err;
2121

2222
/* input must be positive if b is even */
23-
if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
23+
if (((b & 1u) == 0u) && mp_isneg(a)) {
2424
return MP_VAL;
2525
}
2626

0 commit comments

Comments
 (0)