Commit bf9507a9d4cdd89ae779d1b5708f9abc7550ded5

Daniel Mendler 2019-10-24T22:02:29

replace mp_bool by stdbool * This gives the advantage that static analysis **understands** bool, but complains about using an enum type instead of bool. * If stdbool.h is not desired, true/false/bool can be replaced using sed as in the no-stdint-h branch. * We already include stdint.h and stdbool.h is not more harmful than this header

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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
diff --git a/demo/test.c b/demo/test.c
index 3cd3e3b..0d9b073 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -823,7 +823,7 @@ static int test_mp_is_square(void)
    int i, n;
 
    mp_int a, b;
-   mp_bool res;
+   bool res;
 
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
@@ -945,7 +945,7 @@ static int test_mp_prime_is_prime(void)
 {
    int ix;
    mp_err err;
-   mp_bool cnt, fu;
+   bool cnt, fu;
 
    mp_int a, b;
    if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
@@ -1061,7 +1061,7 @@ static int test_mp_prime_next_prime(void)
 
    /* edge cases */
    mp_set(&a, 0u);
-   if ((err = mp_prime_next_prime(&a, 5, MP_NO)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, false)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp_d(&a, 2u) != MP_EQ) {
@@ -1072,7 +1072,7 @@ static int test_mp_prime_next_prime(void)
    }
 
    mp_set(&a, 0u);
-   if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, true)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp_d(&a, 3u) != MP_EQ) {
@@ -1083,7 +1083,7 @@ static int test_mp_prime_next_prime(void)
    }
 
    mp_set(&a, 2u);
-   if ((err = mp_prime_next_prime(&a, 5, MP_NO)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, false)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp_d(&a, 3u) != MP_EQ) {
@@ -1094,7 +1094,7 @@ static int test_mp_prime_next_prime(void)
    }
 
    mp_set(&a, 2u);
-   if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, true)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp_d(&a, 3u) != MP_EQ) {
@@ -1104,7 +1104,7 @@ static int test_mp_prime_next_prime(void)
       goto LBL_ERR;
    }
    mp_set(&a, 8);
-   if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, true)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp_d(&a, 11u) != MP_EQ) {
@@ -1130,7 +1130,7 @@ static int test_mp_prime_next_prime(void)
    if ((err = mp_add(&b, &c, &b)) != MP_OKAY) {
       goto LBL_ERR;
    }
-   if ((err = mp_prime_next_prime(&a, 5, MP_NO)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, false)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp(&a, &b) != MP_EQ) {
@@ -1160,7 +1160,7 @@ static int test_mp_prime_next_prime(void)
    if ((err = mp_add(&b, &c, &b)) != MP_OKAY) {
       goto LBL_ERR;
    }
-   if ((err = mp_prime_next_prime(&a, 5, MP_YES)) != MP_OKAY) {
+   if ((err = mp_prime_next_prime(&a, 5, true)) != MP_OKAY) {
       goto LBL_ERR;
    }
    if (mp_cmp(&a, &b) != MP_EQ) {
@@ -1284,7 +1284,7 @@ static int test_mp_read_radix(void)
       char *s = fgets(buf, sizeof(buf), stdin);
       if (s != buf) break;
       mp_read_radix(&a, buf, 10);
-      mp_prime_next_prime(&a, 5, MP_YES);
+      mp_prime_next_prime(&a, 5, true);
       mp_to_radix(&a, buf, sizeof(buf), NULL, 10);
       printf("%s, %lu\n", buf, (unsigned long)a.dp[0] & 3uL);
    }
diff --git a/etc/2kprime.c b/etc/2kprime.c
index 6069e57..3a3e283 100644
--- a/etc/2kprime.c
+++ b/etc/2kprime.c
@@ -8,7 +8,7 @@ int main(void)
 {
    char buf[2000];
    size_t x;
-   mp_bool y;
+   bool y;
    mp_int q, p;
    FILE *out;
    clock_t t1;
diff --git a/etc/drprime.c b/etc/drprime.c
index 6edb965..31dff4e 100644
--- a/etc/drprime.c
+++ b/etc/drprime.c
@@ -5,7 +5,7 @@ static int sizes[] = { 1+256/MP_DIGIT_BIT, 1+512/MP_DIGIT_BIT, 1+768/MP_DIGIT_BI
 
 int main(void)
 {
-   mp_bool res;
+   bool res;
    int x, y;
    char buf[4096];
    FILE *out;
@@ -30,7 +30,7 @@ top:
          a.used = sizes[x];
 
          /* now loop */
-         res = MP_NO;
+         res = false;
          for (;;) {
             a.dp[0] += 4uL;
             if (a.dp[0] >= MP_MASK) break;
diff --git a/etc/mersenne.c b/etc/mersenne.c
index e2159d1..54b2360 100644
--- a/etc/mersenne.c
+++ b/etc/mersenne.c
@@ -5,13 +5,13 @@
 #include <time.h>
 #include <tommath.h>
 
-static mp_err is_mersenne(long s, mp_bool *pp)
+static mp_err is_mersenne(long s, bool *pp)
 {
    mp_int  n, u;
    mp_err  res;
    int     k;
 
-   *pp = MP_NO;
+   *pp = false;
 
    if ((res = mp_init(&n)) != MP_OKAY) {
       return res;
@@ -103,7 +103,7 @@ static int isprime(long k)
 
 int main(void)
 {
-   mp_bool pp;
+   bool pp;
    long    k;
    clock_t tt;
 
diff --git a/mp_dr_is_modulus.c b/mp_dr_is_modulus.c
index eed5e5f..72b3c96 100644
--- a/mp_dr_is_modulus.c
+++ b/mp_dr_is_modulus.c
@@ -4,13 +4,13 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines if a number is a valid DR modulus */
-mp_bool mp_dr_is_modulus(const mp_int *a)
+bool mp_dr_is_modulus(const mp_int *a)
 {
    int ix;
 
    /* must be at least two digits */
    if (a->used < 2) {
-      return MP_NO;
+      return false;
    }
 
    /* must be of the form b**k - a [a <= b] so all
@@ -18,10 +18,10 @@ mp_bool mp_dr_is_modulus(const mp_int *a)
     */
    for (ix = 1; ix < a->used; ix++) {
       if (a->dp[ix] != MP_MASK) {
-         return MP_NO;
+         return false;
       }
    }
-   return MP_YES;
+   return true;
 }
 
 #endif
diff --git a/mp_is_square.c b/mp_is_square.c
index 0f9cd1f..f92ecbf 100644
--- a/mp_is_square.c
+++ b/mp_is_square.c
@@ -26,7 +26,7 @@ static const char rem_105[105] = {
 };
 
 /* Store non-zero to ret if arg is square, and zero if not */
-mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
+mp_err mp_is_square(const mp_int *arg, bool *ret)
 {
    mp_err        err;
    mp_digit      c;
@@ -34,7 +34,7 @@ mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
    unsigned long r;
 
    /* Default to Non-square :) */
-   *ret = MP_NO;
+   *ret = false;
 
    if (arg->sign == MP_NEG) {
       return MP_VAL;
diff --git a/mp_prime_fermat.c b/mp_prime_fermat.c
index c6bc720..50d2e5e 100644
--- a/mp_prime_fermat.c
+++ b/mp_prime_fermat.c
@@ -11,13 +11,13 @@
  *
  * Sets result to 1 if the congruence holds, or zero otherwise.
  */
-mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result)
+mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, bool *result)
 {
    mp_int  t;
    mp_err  err;
 
    /* default to composite  */
-   *result = MP_NO;
+   *result = false;
 
    /* ensure b > 1 */
    if (mp_cmp_d(b, 1uL) != MP_GT) {
@@ -36,7 +36,7 @@ mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result)
 
    /* is it equal to b? */
    if (mp_cmp(&t, b) == MP_EQ) {
-      *result = MP_YES;
+      *result = true;
    }
 
    err = MP_OKAY;
diff --git a/mp_prime_frobenius_underwood.c b/mp_prime_frobenius_underwood.c
index 0eaa36d..543b8b4 100644
--- a/mp_prime_frobenius_underwood.c
+++ b/mp_prime_frobenius_underwood.c
@@ -20,14 +20,14 @@
  */
 #define LTM_FROBENIUS_UNDERWOOD_A 32764
 
-mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
+mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result)
 {
    mp_int T1z, T2z, Np1z, sz, tz;
 
    int a, ap2, length, i, j;
    mp_err err;
 
-   *result = MP_NO;
+   *result = false;
 
    if ((err = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) {
       return err;
@@ -113,7 +113,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
    mp_set_u32(&T1z, (uint32_t)((2 * a) + 5));
    if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY)                  goto LBL_FU_ERR;
    if (mp_iszero(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
-      *result = MP_YES;
+      *result = true;
    }
 
 LBL_FU_ERR:
diff --git a/mp_prime_is_prime.c b/mp_prime_is_prime.c
index 42d417e..d0eca2c 100644
--- a/mp_prime_is_prime.c
+++ b/mp_prime_is_prime.c
@@ -14,26 +14,26 @@ static unsigned int s_floor_ilog2(int value)
 }
 
 
-mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
+mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
 {
    mp_int  b;
    int     ix, p_max = 0, size_a, len;
-   mp_bool res;
+   bool res;
    mp_err  err;
    unsigned int fips_rand, mask;
 
    /* default to no */
-   *result = MP_NO;
+   *result = false;
 
    /* Some shortcuts */
    /* N > 3 */
    if (a->used == 1) {
       if ((a->dp[0] == 0u) || (a->dp[0] == 1u)) {
-         *result = MP_NO;
+         *result = false;
          return MP_OKAY;
       }
       if (a->dp[0] == 2u) {
-         *result = MP_YES;
+         *result = true;
          return MP_OKAY;
       }
    }
@@ -53,7 +53,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
    /* is the input equal to one of the primes in the table? */
    for (ix = 0; ix < MP_PRIME_TAB_SIZE; ix++) {
       if (mp_cmp_d(a, s_mp_prime_tab[ix]) == MP_EQ) {
-         *result = MP_YES;
+         *result = true;
          return MP_OKAY;
       }
    }
@@ -267,7 +267,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
    }
 
    /* passed the test */
-   *result = MP_YES;
+   *result = true;
 LBL_B:
    mp_clear(&b);
    return err;
diff --git a/mp_prime_miller_rabin.c b/mp_prime_miller_rabin.c
index f6d698b..a3af8bc 100644
--- a/mp_prime_miller_rabin.c
+++ b/mp_prime_miller_rabin.c
@@ -10,14 +10,14 @@
  * Randomly the chance of error is no more than 1/4 and often
  * very much lower.
  */
-mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result)
+mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result)
 {
    mp_int  n1, y, r;
    mp_err  err;
    int     s, j;
 
    /* default */
-   *result = MP_NO;
+   *result = false;
 
    /* ensure b > 1 */
    if (mp_cmp_d(b, 1uL) != MP_GT) {
@@ -79,7 +79,7 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result)
    }
 
    /* probably prime now */
-   *result = MP_YES;
+   *result = true;
 LBL_Y:
    mp_clear(&y);
 LBL_R:
diff --git a/mp_prime_next_prime.c b/mp_prime_next_prime.c
index 2165e3c..40c94a4 100644
--- a/mp_prime_next_prime.c
+++ b/mp_prime_next_prime.c
@@ -6,14 +6,14 @@
 /* finds the next prime after the number "a" using "t" trials
  * of Miller-Rabin.
  *
- * bbs_style = MP_YES means the prime must be congruent to 3 mod 4
+ * bbs_style = true means the prime must be congruent to 3 mod 4
  */
-mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style)
+mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style)
 {
    int      x, y;
    mp_ord   cmp;
    mp_err   err;
-   mp_bool  res = MP_NO;
+   bool  res = false;
    mp_digit res_tab[MP_PRIME_TAB_SIZE], step, kstep;
    mp_int   b;
 
diff --git a/mp_prime_rand.c b/mp_prime_rand.c
index cc19fa2..ff6df9c 100644
--- a/mp_prime_rand.c
+++ b/mp_prime_rand.c
@@ -22,7 +22,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
 {
    unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
    int bsize, maskOR_msb_offset;
-   mp_bool res;
+   bool res;
    mp_err err;
 
    /* sanity check the input */
diff --git a/mp_prime_strong_lucas_selfridge.c b/mp_prime_strong_lucas_selfridge.c
index a047651..df5de96 100644
--- a/mp_prime_strong_lucas_selfridge.c
+++ b/mp_prime_strong_lucas_selfridge.c
@@ -33,7 +33,7 @@ static mp_err s_mp_mul_si(const mp_int *a, int32_t d, mp_int *c)
 }
 /*
     Strong Lucas-Selfridge test.
-    returns MP_YES if it is a strong L-S prime, MP_NO if it is composite
+    returns true if it is a strong L-S prime, false if it is composite
 
     Code ported from  Thomas Ray Nicely's implementation of the BPSW test
     at http://www.trnicely.net/misc/bpsw.html
@@ -48,15 +48,15 @@ static mp_err s_mp_mul_si(const mp_int *a, int32_t d, mp_int *c)
     (If that name sounds familiar, he is the guy who found the fdiv bug in the
      Pentium (P5x, I think) Intel processor)
 */
-mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
+mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
 {
    /* CZ TODO: choose better variable names! */
    mp_int Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz;
    int32_t D, Ds, J, sign, P, Q, r, s, u, Nbits;
    mp_err err;
-   mp_bool oddness;
+   bool oddness;
 
-   *result = MP_NO;
+   *result = false;
    /*
    Find the first element D in the sequence {5, -7, 9, -11, 13, ...}
    such that Jacobi(D,N) = -1 (Selfridge's algorithm). Theory
@@ -240,7 +240,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
    /* If U_d or V_d is congruent to 0 mod N, then N is a prime or a
       strong Lucas pseudoprime. */
    if (mp_iszero(&Uz) || mp_iszero(&Vz)) {
-      *result = MP_YES;
+      *result = true;
       goto LBL_LS_ERR;
    }
 
@@ -263,7 +263,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
       if ((err = mp_sub(&Vz, &Q2kdz, &Vz)) != MP_OKAY)            goto LBL_LS_ERR;
       if ((err = mp_mod(&Vz, a, &Vz)) != MP_OKAY)                 goto LBL_LS_ERR;
       if (mp_iszero(&Vz)) {
-         *result = MP_YES;
+         *result = true;
          goto LBL_LS_ERR;
       }
       /* Calculate Q^{d*2^r} for next r (final iteration irrelevant). */
diff --git a/mp_reduce_is_2k.c b/mp_reduce_is_2k.c
index ec12338..618ab54 100644
--- a/mp_reduce_is_2k.c
+++ b/mp_reduce_is_2k.c
@@ -4,15 +4,15 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines if mp_reduce_2k can be used */
-mp_bool mp_reduce_is_2k(const mp_int *a)
+bool mp_reduce_is_2k(const mp_int *a)
 {
    int ix, iy, iw;
    mp_digit iz;
 
    if (a->used == 0) {
-      return MP_NO;
+      return false;
    } else if (a->used == 1) {
-      return MP_YES;
+      return true;
    } else if (a->used > 1) {
       iy = mp_count_bits(a);
       iz = 1;
@@ -21,7 +21,7 @@ mp_bool mp_reduce_is_2k(const mp_int *a)
       /* Test every bit from the second digit up, must be 1 */
       for (ix = MP_DIGIT_BIT; ix < iy; ix++) {
          if ((a->dp[iw] & iz) == 0u) {
-            return MP_NO;
+            return false;
          }
          iz <<= 1;
          if (iz > MP_DIGIT_MAX) {
@@ -29,9 +29,9 @@ mp_bool mp_reduce_is_2k(const mp_int *a)
             iz = 1;
          }
       }
-      return MP_YES;
+      return true;
    } else {
-      return MP_YES;
+      return true;
    }
 }
 
diff --git a/mp_reduce_is_2k_l.c b/mp_reduce_is_2k_l.c
index cd6fc62..30fc10d 100644
--- a/mp_reduce_is_2k_l.c
+++ b/mp_reduce_is_2k_l.c
@@ -4,14 +4,14 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* determines if reduce_2k_l can be used */
-mp_bool mp_reduce_is_2k_l(const mp_int *a)
+bool mp_reduce_is_2k_l(const mp_int *a)
 {
    int ix, iy;
 
    if (a->used == 0) {
-      return MP_NO;
+      return false;
    } else if (a->used == 1) {
-      return MP_YES;
+      return true;
    } else if (a->used > 1) {
       /* if more than half of the digits are -1 we're sold */
       for (iy = ix = 0; ix < a->used; ix++) {
@@ -21,7 +21,7 @@ mp_bool mp_reduce_is_2k_l(const mp_int *a)
       }
       return (iy >= (a->used/2));
    } else {
-      return MP_NO;
+      return false;
    }
 }
 
diff --git a/s_mp_get_bit.c b/s_mp_get_bit.c
index 5114e9e..f077f61 100644
--- a/s_mp_get_bit.c
+++ b/s_mp_get_bit.c
@@ -4,14 +4,14 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* Get bit at position b and return MP_YES if the bit is 1, MP_NO if it is 0 */
-mp_bool s_mp_get_bit(const mp_int *a, unsigned int b)
+/* Get bit at position b and return true if the bit is 1, false if it is 0 */
+bool s_mp_get_bit(const mp_int *a, unsigned int b)
 {
    mp_digit bit;
    int limb = (int)(b / MP_DIGIT_BIT);
 
    if (limb >= a->used) {
-      return MP_NO;
+      return false;
    }
 
    bit = (mp_digit)1 << (b % MP_DIGIT_BIT);
diff --git a/s_mp_prime_is_divisible.c b/s_mp_prime_is_divisible.c
index d8bdedc..0cca5a6 100644
--- a/s_mp_prime_is_divisible.c
+++ b/s_mp_prime_is_divisible.c
@@ -8,14 +8,14 @@
  *
  * sets result to 0 if not, 1 if yes
  */
-mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result)
+mp_err s_mp_prime_is_divisible(const mp_int *a, bool *result)
 {
    int      ix;
    mp_err   err;
    mp_digit res;
 
    /* default to not */
-   *result = MP_NO;
+   *result = false;
 
    for (ix = 0; ix < MP_PRIME_TAB_SIZE; ix++) {
       /* what is a mod LBL_prime_tab[ix] */
@@ -25,7 +25,7 @@ mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result)
 
       /* is the residue zero? */
       if (res == 0u) {
-         *result = MP_YES;
+         *result = true;
          return MP_OKAY;
       }
    }
diff --git a/tommath.h b/tommath.h
index 9a31bd3..a55c57f 100644
--- a/tommath.h
+++ b/tommath.h
@@ -6,6 +6,7 @@
 
 #include <stdint.h>
 #include <stddef.h>
+#include <stdbool.h>
 
 #ifndef MP_NO_FILE
 #  include <stdio.h>
@@ -94,11 +95,6 @@ typedef enum {
 } mp_ord;
 
 typedef enum {
-   MP_NO = 0,
-   MP_YES = 1
-} mp_bool;
-
-typedef enum {
    MP_OKAY  = 0,   /* no error */
    MP_ERR   = -1,  /* unknown error */
    MP_MEM   = -2,  /* out of mem */
@@ -119,7 +115,6 @@ typedef enum {
 } mp_endian;
 
 /* tunable cutoffs */
-
 #ifndef MP_FIXED_CUTOFFS
 extern int
 MP_KARATSUBA_MUL_CUTOFF,
@@ -441,7 +436,7 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
 mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR;
 
 /* is number a square? */
-mp_err mp_is_square(const mp_int *arg, mp_bool *ret) MP_WUR;
+mp_err mp_is_square(const mp_int *arg, bool *ret) MP_WUR;
 
 /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
 mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) MP_WUR;
@@ -468,7 +463,7 @@ mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) MP_WUR;
 mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
 
 /* returns 1 if a is a valid DR modulus */
-mp_bool mp_dr_is_modulus(const mp_int *a) MP_WUR;
+bool mp_dr_is_modulus(const mp_int *a) MP_WUR;
 
 /* sets the value of "d" required for mp_dr_reduce */
 void mp_dr_setup(const mp_int *a, mp_digit *d);
@@ -477,7 +472,7 @@ void mp_dr_setup(const mp_int *a, mp_digit *d);
 mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) MP_WUR;
 
 /* returns true if a can be reduced with mp_reduce_2k */
-mp_bool mp_reduce_is_2k(const mp_int *a) MP_WUR;
+bool mp_reduce_is_2k(const mp_int *a) MP_WUR;
 
 /* determines k value for 2k reduction */
 mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR;
@@ -486,7 +481,7 @@ mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR;
 mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) MP_WUR;
 
 /* returns true if a can be reduced with mp_reduce_2k_l */
-mp_bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR;
+bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR;
 
 /* determines k value for 2k reduction */
 mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR;
@@ -502,12 +497,12 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) 
 /* performs one Fermat test of "a" using base "b".
  * Sets result to 0 if composite or 1 if probable prime
  */
-mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
+mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, bool *result) MP_WUR;
 
 /* performs one Miller-Rabin test of "a" using base "b".
  * Sets result to 0 if composite or 1 if probable prime
  */
-mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
+mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result) MP_WUR;
 
 /* This gives [for a given bit size] the number of trials required
  * such that Miller-Rabin gives a prob of failure lower than 2^-96
@@ -517,12 +512,12 @@ int mp_prime_rabin_miller_trials(int size) MP_WUR;
 /* performs one strong Lucas-Selfridge test of "a".
  * Sets result to 0 if composite or 1 if probable prime
  */
-mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR;
+mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) MP_WUR;
 
 /* performs one Frobenius test of "a" as described by Paul Underwood.
  * Sets result to 0 if composite or 1 if probable prime
  */
-mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR;
+mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result) MP_WUR;
 
 /* performs t random rounds of Miller-Rabin on "a" additional to
  * bases 2 and 3.  Also performs an initial sieve of trial
@@ -538,14 +533,14 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR;
  *
  * Sets result to 1 if probably prime, 0 otherwise
  */
-mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR;
+mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result) MP_WUR;
 
 /* finds the next prime after the number "a" using "t" trials
  * of Miller-Rabin.
  *
- * bbs_style = MP_YES means the prime must be congruent to 3 mod 4
+ * bbs_style = true means the prime must be congruent to 3 mod 4
  */
-mp_err mp_prime_next_prime(mp_int *a, int t, mp_bool bbs_style) MP_WUR;
+mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style) MP_WUR;
 
 /* makes a truly random prime of a given size (bits),
  *
diff --git a/tommath_private.h b/tommath_private.h
index 8b30c67..2dbdca8 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -182,7 +182,7 @@ MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
 extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
 
 /* lowlevel functions, do not call! */
-MP_PRIVATE mp_bool s_mp_get_bit(const mp_int *a, unsigned int b);
+MP_PRIVATE bool s_mp_get_bit(const mp_int *a, unsigned int b);
 MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
@@ -202,7 +202,7 @@ MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_dig
 MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
 MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
 MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
-MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
+MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, bool *result);
 MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n);
 MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c);
 MP_PRIVATE uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base);