Commit 27e142bc438e6868478015c2e3d13c76d520df7a

Daniel Mendler 2019-10-24T17:52:03

remove unnecessary == MP_YES/MP_NO comparisons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
diff --git a/demo/test.c b/demo/test.c
index 41c4182..3cd3e3b 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -120,12 +120,12 @@ static int test_trivial_stuff(void)
    }
    /* a: -5-> b: 5 */
    mp_abs(&a, &b);
-   if (mp_isneg(&b) != MP_NO) {
+   if (mp_isneg(&b)) {
       goto LBL_ERR;
    }
    /* a: -5-> b: -4 */
    mp_add_d(&a, 1uL, &b);
-   if (mp_isneg(&b) != MP_YES) {
+   if (!mp_isneg(&b)) {
       goto LBL_ERR;
    }
    if (mp_get_i32(&b) != -4) {
@@ -841,7 +841,7 @@ static int test_mp_is_square(void)
          printf("\nfn:mp_is_square() error!");
          goto LBL_ERR;
       }
-      if (res == MP_NO) {
+      if (!res) {
          printf("\nfn:mp_is_square() bad result!");
          goto LBL_ERR;
       }
@@ -852,7 +852,7 @@ static int test_mp_is_square(void)
          printf("\nfp:mp_is_square() error!");
          goto LBL_ERR;
       }
-      if (res == MP_YES) {
+      if (res) {
          printf("\nfp:mp_is_square() bad result!");
          goto LBL_ERR;
       }
@@ -958,7 +958,7 @@ static int test_mp_prime_is_prime(void)
                  "91xLNF3roobhzgTzoFIG6P13ZqhOVYSN60Fa7Cj2jVR1g0k89zdahO9/kAiRprpfO1VAp1aBHucLFV/qLKLFb+zonV7R2Vxp1K13ClwUXStpV0oxTNQVjwybmFb5NBEHImZ6V7P6+udRJuH8VbMEnS0H8/pSqQrg82OoQQ2fPpAk6G1hkjqoCv5s/Yr",
                  64);
    mp_prime_is_prime(&a, mp_prime_rabin_miller_trials(mp_count_bits(&a)), &cnt);
-   if (cnt == MP_YES) {
+   if (cnt) {
       printf("Arnault's pseudoprime is not prime but mp_prime_is_prime says it is.\n");
       goto LBL_ERR;
    }
@@ -973,10 +973,10 @@ static int test_mp_prime_is_prime(void)
       printf("\nfailed with error: %s\n", mp_error_to_string(err));
    }
    /* large problem */
-   if (cnt == MP_NO) {
+   if (!cnt) {
       printf("A certified prime is a prime but mp_prime_is_prime says it is not.\n");
    }
-   if ((err != MP_OKAY) || (cnt == MP_NO)) {
+   if ((err != MP_OKAY) || !cnt) {
       printf("prime tested was: 0x");
       mp_fwrite(&a,16,stdout);
       putchar('\n');
@@ -1003,14 +1003,14 @@ static int test_mp_prime_is_prime(void)
          printf("\nfailed with error: %s\n", mp_error_to_string(err));
       }
       /* large problem */
-      if (cnt == MP_NO) {
+      if (!cnt) {
          printf("\nsub is not prime!\n");
       }
       mp_prime_frobenius_underwood(&b, &fu);
-      if (fu == MP_NO) {
+      if (!fu) {
          printf("\nfrobenius-underwood says sub is not prime!\n");
       }
-      if ((err != MP_OKAY) || (cnt == MP_NO)) {
+      if ((err != MP_OKAY) || !cnt) {
          printf("prime tested was: 0x");
          mp_fwrite(&a,16,stdout);
          putchar('\n');
@@ -1031,10 +1031,10 @@ static int test_mp_prime_is_prime(void)
       printf("\nmp_prime_strong_lucas_selfridge failed with error: %s\n", mp_error_to_string(err));
    }
    /* large problem */
-   if (cnt == MP_NO) {
+   if (!cnt) {
       printf("\n\nissue #143 - mp_prime_strong_lucas_selfridge FAILED!\n");
    }
-   if ((err != MP_OKAY) || (cnt == MP_NO)) {
+   if ((err != MP_OKAY) || !cnt) {
       printf("prime tested was: 0x");
       mp_fwrite(&a,16,stdout);
       putchar('\n');
@@ -1534,7 +1534,7 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
    mp_int  t;
    mp_digit d;
    *size = 0u;
-   if (mp_iszero(a) == MP_YES) {
+   if (mp_iszero(a)) {
       *size = 2u;
       return MP_OKAY;
    }
@@ -1546,7 +1546,7 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
       return res;
    }
    t.sign = MP_ZPOS;
-   while (mp_iszero(&t) == MP_NO) {
+   while (!mp_iszero(&t)) {
       if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
          mp_clear(&t);
          return res;
diff --git a/demo/timing.c b/demo/timing.c
index a421f8d..fa5669c 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -173,7 +173,7 @@ int main(int argc, char **argv)
                gg = (TIMFUNC() - gg) >> 1;
                if (tt > gg)
                   tt = gg;
-               if ((m == 0) && (n == MP_YES)) {
+               if ((m == 0) && n) {
                   printf("Arnault's pseudoprime is not prime but mp_prime_is_prime says it is.\n");
                   return EXIT_FAILURE;
                }
diff --git a/etc/2kprime.c b/etc/2kprime.c
index 95ed2de..6069e57 100644
--- a/etc/2kprime.c
+++ b/etc/2kprime.c
@@ -43,7 +43,7 @@ top:
 
             /* quick test on q */
             mp_prime_is_prime(&q, 1, &y);
-            if (y == MP_NO) {
+            if (!y) {
                continue;
             }
 
@@ -51,20 +51,20 @@ top:
             mp_sub_d(&q, 1uL, &p);
             mp_div_2(&p, &p);
             mp_prime_is_prime(&p, 3, &y);
-            if (y == MP_NO) {
+            if (!y) {
                continue;
             }
 
             /* test on q */
             mp_prime_is_prime(&q, 3, &y);
-            if (y == MP_NO) {
+            if (!y) {
                continue;
             }
 
             break;
          }
 
-         if (y == MP_NO) {
+         if (!y) {
             ++sizes[x];
             goto top;
          }
diff --git a/etc/drprime.c b/etc/drprime.c
index 64e31ef..6edb965 100644
--- a/etc/drprime.c
+++ b/etc/drprime.c
@@ -35,18 +35,18 @@ top:
             a.dp[0] += 4uL;
             if (a.dp[0] >= MP_MASK) break;
             mp_prime_is_prime(&a, 1, &res);
-            if (res == MP_NO) continue;
+            if (!res) continue;
             printf(".");
             fflush(stdout);
             mp_sub_d(&a, 1uL, &b);
             mp_div_2(&b, &b);
             mp_prime_is_prime(&b, 3, &res);
-            if (res == MP_NO) continue;
+            if (!res) continue;
             mp_prime_is_prime(&a, 3, &res);
-            if (res == MP_YES) break;
+            if (res) break;
          }
 
-         if (res != MP_YES) {
+         if (!res) {
             printf("Error not DR modulus\n");
             sizes[x] += 1;
             goto top;
diff --git a/etc/mersenne.c b/etc/mersenne.c
index 0c9f52f..e2159d1 100644
--- a/etc/mersenne.c
+++ b/etc/mersenne.c
@@ -56,9 +56,9 @@ static mp_err is_mersenne(long s, mp_bool *pp)
    }
 
    /* if u == 0 then its prime */
-   if (mp_iszero(&u) == MP_YES) {
+   if (mp_iszero(&u)) {
       mp_prime_is_prime(&n, 8, pp);
-      if (*pp != MP_YES) printf("FAILURE\n");
+      if (!*pp) printf("FAILURE\n");
    }
 
    res = MP_OKAY;
@@ -119,7 +119,7 @@ int main(void)
          return -1;
       }
 
-      if (pp == MP_YES) {
+      if (pp) {
          /* count time */
          tt = clock() - tt;
 
diff --git a/mp_exptmod.c b/mp_exptmod.c
index 8f8a9e9..e643ded 100644
--- a/mp_exptmod.c
+++ b/mp_exptmod.c
@@ -49,16 +49,16 @@ LBL_ERR:
 
    /* modified diminished radix reduction */
    if (MP_HAS(MP_REDUCE_IS_2K_L) && MP_HAS(MP_REDUCE_2K_L) && MP_HAS(S_MP_EXPTMOD) &&
-       (mp_reduce_is_2k_l(P) == MP_YES)) {
+       mp_reduce_is_2k_l(P)) {
       return s_mp_exptmod(G, X, P, Y, 1);
    }
 
    /* is it a DR modulus? default to no */
-   dr = (MP_HAS(MP_DR_IS_MODULUS) && (mp_dr_is_modulus(P) == MP_YES)) ? 1 : 0;
+   dr = (MP_HAS(MP_DR_IS_MODULUS) && mp_dr_is_modulus(P)) ? 1 : 0;
 
    /* if not, is it a unrestricted DR modulus? */
    if (MP_HAS(MP_REDUCE_IS_2K) && (dr == 0)) {
-      dr = (mp_reduce_is_2k(P) == MP_YES) ? 2 : 0;
+      dr = (mp_reduce_is_2k(P)) ? 2 : 0;
    }
 
    /* if the modulus is odd or dr != 0 use the montgomery method */
diff --git a/mp_is_square.c b/mp_is_square.c
index f1df73e..0f9cd1f 100644
--- a/mp_is_square.c
+++ b/mp_is_square.c
@@ -85,7 +85,7 @@ mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
       goto LBL_ERR;
    }
 
-   *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
+   *ret = (mp_cmp_mag(&t, arg) == MP_EQ);
 LBL_ERR:
    mp_clear(&t);
    return err;
diff --git a/mp_prime_frobenius_underwood.c b/mp_prime_frobenius_underwood.c
index bb56ea7..0eaa36d 100644
--- a/mp_prime_frobenius_underwood.c
+++ b/mp_prime_frobenius_underwood.c
@@ -92,7 +92,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
       if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY)              goto LBL_FU_ERR;
       if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY)                 goto LBL_FU_ERR;
       if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY)                goto LBL_FU_ERR;
-      if (s_mp_get_bit(&Np1z, (unsigned int)i) == MP_YES) {
+      if (s_mp_get_bit(&Np1z, (unsigned int)i)) {
          /*
           *  temp = (a+2) * sz + tz
           *  tz   = 2 * tz - sz
diff --git a/mp_prime_is_prime.c b/mp_prime_is_prime.c
index e4f9214..42d417e 100644
--- a/mp_prime_is_prime.c
+++ b/mp_prime_is_prime.c
@@ -46,7 +46,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
    if ((err = mp_is_square(a, &res)) != MP_OKAY) {
       return err;
    }
-   if (res != MP_NO) {
+   if (res) {
       return MP_OKAY;
    }
 
@@ -63,7 +63,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
    }
 
    /* return if it was trivially divisible */
-   if (res == MP_YES) {
+   if (res) {
       return MP_OKAY;
    }
 
@@ -77,7 +77,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
    if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
       goto LBL_B;
    }
-   if (res == MP_NO) {
+   if (!res) {
       goto LBL_B;
    }
    /*
@@ -89,7 +89,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
    if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
       goto LBL_B;
    }
-   if (res == MP_NO) {
+   if (!res) {
       goto LBL_B;
    }
 
@@ -105,14 +105,14 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
       if ((err != MP_OKAY) && (err != MP_ITER)) {
          goto LBL_B;
       }
-      if (res == MP_NO) {
+      if (!res) {
          goto LBL_B;
       }
 #else
       if ((err = mp_prime_strong_lucas_selfridge(a, &res)) != MP_OKAY) {
          goto LBL_B;
       }
-      if (res == MP_NO) {
+      if (!res) {
          goto LBL_B;
       }
 #endif
@@ -164,7 +164,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
             goto LBL_B;
          }
-         if (res == MP_NO) {
+         if (!res) {
             goto LBL_B;
          }
       }
@@ -260,7 +260,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
             goto LBL_B;
          }
-         if (res == MP_NO) {
+         if (!res) {
             goto LBL_B;
          }
       }
diff --git a/mp_prime_next_prime.c b/mp_prime_next_prime.c
index c8f7603..2165e3c 100644
--- a/mp_prime_next_prime.c
+++ b/mp_prime_next_prime.c
@@ -29,7 +29,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style)
             continue;
          }
          if (cmp != MP_GT) {
-            if ((bbs_style == MP_YES) && ((s_mp_prime_tab[x] & 3u) != 3u)) {
+            if ((bbs_style) && ((s_mp_prime_tab[x] & 3u) != 3u)) {
                /* try again until we get a prime congruent to 3 mod 4 */
                continue;
             } else {
@@ -42,7 +42,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style)
    }
 
    /* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */
-   if (bbs_style == MP_YES) {
+   if (bbs_style) {
       kstep   = 4;
    } else {
       kstep   = 2;
@@ -50,7 +50,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style)
 
    /* at this point we will use a combination of a sieve and Miller-Rabin */
 
-   if (bbs_style == MP_YES) {
+   if (bbs_style) {
       /* if a mod 4 != 3 subtract the correct value to make it so */
       if ((a->dp[0] & 3u) != 3u) {
          if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
@@ -118,7 +118,7 @@ mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style)
       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
          goto LBL_ERR;
       }
-      if (res == MP_YES) {
+      if (res) {
          break;
       }
    }
diff --git a/mp_prime_rand.c b/mp_prime_rand.c
index b37089b..2ee8e74 100644
--- a/mp_prime_rand.c
+++ b/mp_prime_rand.c
@@ -85,7 +85,7 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_call
       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
          goto error;
       }
-      if (res == MP_NO) {
+      if (!res) {
          continue;
       }
 
@@ -103,7 +103,7 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_call
             goto error;
          }
       }
-   } while (res == MP_NO);
+   } while (!res);
 
    if ((flags & MP_PRIME_SAFE) != 0) {
       /* restore a to the original value */
diff --git a/mp_prime_strong_lucas_selfridge.c b/mp_prime_strong_lucas_selfridge.c
index 0aee33d..a047651 100644
--- a/mp_prime_strong_lucas_selfridge.c
+++ b/mp_prime_strong_lucas_selfridge.c
@@ -192,7 +192,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
       if ((err = mp_mod(&Qmz, a, &Qmz)) != MP_OKAY)               goto LBL_LS_ERR;
       if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY)               goto LBL_LS_ERR;
 
-      if (s_mp_get_bit(&Dz, (unsigned int)u) == MP_YES) {
+      if (s_mp_get_bit(&Dz, (unsigned int)u)) {
          /* Formulas for addition of indices (carried out mod N);
           *
           * U_(m+n) = (U_m*V_n + U_n*V_m)/2
@@ -214,18 +214,18 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
           * Thomas R. Nicely used GMP's mpz_fdiv_q_2exp().
           * But mp_div_2() does not do so, it is truncating instead.
           */
-         oddness = mp_isodd(&Uz) ? MP_YES : MP_NO;
+         oddness = mp_isodd(&Uz);
          if ((err = mp_div_2(&Uz, &Uz)) != MP_OKAY)               goto LBL_LS_ERR;
-         if ((Uz.sign == MP_NEG) && (oddness != MP_NO)) {
+         if ((Uz.sign == MP_NEG) && oddness) {
             if ((err = mp_sub_d(&Uz, 1uL, &Uz)) != MP_OKAY)       goto LBL_LS_ERR;
          }
          if ((err = mp_add(&T3z, &T4z, &Vz)) != MP_OKAY)          goto LBL_LS_ERR;
          if (mp_isodd(&Vz)) {
             if ((err = mp_add(&Vz, a, &Vz)) != MP_OKAY)           goto LBL_LS_ERR;
          }
-         oddness = mp_isodd(&Vz) ? MP_YES : MP_NO;
+         oddness = mp_isodd(&Vz);
          if ((err = mp_div_2(&Vz, &Vz)) != MP_OKAY)               goto LBL_LS_ERR;
-         if ((Vz.sign == MP_NEG) && (oddness != MP_NO)) {
+         if ((Vz.sign == MP_NEG) && oddness) {
             if ((err = mp_sub_d(&Vz, 1uL, &Vz)) != MP_OKAY)       goto LBL_LS_ERR;
          }
          if ((err = mp_mod(&Uz, a, &Uz)) != MP_OKAY)              goto LBL_LS_ERR;
diff --git a/mp_reduce_is_2k_l.c b/mp_reduce_is_2k_l.c
index 1aa3d4e..cd6fc62 100644
--- a/mp_reduce_is_2k_l.c
+++ b/mp_reduce_is_2k_l.c
@@ -19,7 +19,7 @@ mp_bool mp_reduce_is_2k_l(const mp_int *a)
             ++iy;
          }
       }
-      return (iy >= (a->used/2)) ? MP_YES : MP_NO;
+      return (iy >= (a->used/2));
    } else {
       return MP_NO;
    }
diff --git a/s_mp_get_bit.c b/s_mp_get_bit.c
index 397499c..5114e9e 100644
--- a/s_mp_get_bit.c
+++ b/s_mp_get_bit.c
@@ -15,7 +15,7 @@ mp_bool s_mp_get_bit(const mp_int *a, unsigned int b)
    }
 
    bit = (mp_digit)1 << (b % MP_DIGIT_BIT);
-   return ((a->dp[limb] & bit) != 0u) ? MP_YES : MP_NO;
+   return ((a->dp[limb] & bit) != 0u);
 }
 
 #endif
diff --git a/tommath.h b/tommath.h
index 5d0d2fe..9a31bd3 100644
--- a/tommath.h
+++ b/tommath.h
@@ -208,10 +208,10 @@ mp_err mp_grow(mp_int *a, int size) MP_WUR;
 mp_err mp_init_size(mp_int *a, int size) MP_WUR;
 
 /* ---> Basic Manipulations <--- */
-#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
-#define mp_isneg(a)  (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
-#define mp_iseven(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u) ? MP_YES : MP_NO)
-#define mp_isodd(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u) ? MP_YES : MP_NO)
+#define mp_iszero(a) ((a)->used == 0)
+#define mp_isneg(a)  ((a)->sign != MP_ZPOS)
+#define mp_iseven(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
+#define mp_isodd(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
 
 /* set to zero */
 void mp_zero(mp_int *a);