Commit 1cc289d2151620de7559960e6b1e5f9ac8a9f84b

nijtmans 2019-11-09T20:23:03

better use of mp_isneg() and mp_iszero()

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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
diff --git a/etc/mersenne.c b/etc/mersenne.c
index 54b2360..4d3939e 100644
--- a/etc/mersenne.c
+++ b/etc/mersenne.c
@@ -43,7 +43,7 @@ static mp_err is_mersenne(long s, bool *pp)
       }
 
       /* make sure u is positive */
-      while (u.sign == MP_NEG) {
+      while (mp_isneg(&u)) {
          if ((res = mp_add(&u, &n, &u)) != MP_OKAY) {
             goto LBL_MU;
          }
diff --git a/mp_add_d.c b/mp_add_d.c
index de935bb..c57a80d 100644
--- a/mp_add_d.c
+++ b/mp_add_d.c
@@ -11,13 +11,13 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
 
    /* fast path for a == c */
    if (a == c) {
-      if ((c->sign == MP_ZPOS) &&
+      if (!mp_isneg(c) &&
           !mp_iszero(c) &&
           ((c->dp[0] + b) < MP_DIGIT_MAX)) {
          c->dp[0] += b;
          return MP_OKAY;
       }
-      if ((c->sign == MP_NEG) &&
+      if (mp_isneg(c) &&
           (c->dp[0] > b)) {
          c->dp[0] -= b;
          return MP_OKAY;
@@ -30,7 +30,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
    }
 
    /* if a is negative and |a| >= b, call c = |a| - b */
-   if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
+   if (mp_isneg(a) && ((a->used > 1) || (a->dp[0] >= b))) {
       mp_int a_ = *a;
       /* temporarily fix sign of a */
       a_.sign = MP_ZPOS;
@@ -51,7 +51,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
    oldused = c->used;
 
    /* if a is positive */
-   if (a->sign == MP_ZPOS) {
+   if (!mp_isneg(a)) {
       /* add digits, mu is carry */
       int i;
       mp_digit mu = b;
diff --git a/mp_and.c b/mp_and.c
index a865ae0..b5230c4 100644
--- a/mp_and.c
+++ b/mp_and.c
@@ -9,7 +9,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
    int used = MP_MAX(a->used, b->used) + 1, i;
    mp_err err;
    mp_digit ac = 1, bc = 1, cc = 1;
-   mp_sign csign = ((a->sign == MP_NEG) && (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
+   bool neg = (mp_isneg(a) && mp_isneg(b));
 
    if ((err = mp_grow(c, used)) != MP_OKAY) {
       return err;
@@ -19,7 +19,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
       mp_digit x, y;
 
       /* convert to two complement if negative */
-      if (a->sign == MP_NEG) {
+      if (mp_isneg(a)) {
          ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
          x = ac & MP_MASK;
          ac >>= MP_DIGIT_BIT;
@@ -28,7 +28,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
       }
 
       /* convert to two complement if negative */
-      if (b->sign == MP_NEG) {
+      if (mp_isneg(b)) {
          bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
          y = bc & MP_MASK;
          bc >>= MP_DIGIT_BIT;
@@ -39,7 +39,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
       c->dp[i] = x & y;
 
       /* convert to to sign-magnitude if negative */
-      if (csign == MP_NEG) {
+      if (neg) {
          cc += ~c->dp[i] & MP_MASK;
          c->dp[i] = cc & MP_MASK;
          cc >>= MP_DIGIT_BIT;
@@ -47,7 +47,7 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    c->used = used;
-   c->sign = csign;
+   c->sign = (neg ? MP_NEG : MP_ZPOS);
    mp_clamp(c);
    return MP_OKAY;
 }
diff --git a/mp_clamp.c b/mp_clamp.c
index 14c1814..ae59c40 100644
--- a/mp_clamp.c
+++ b/mp_clamp.c
@@ -19,8 +19,8 @@ void mp_clamp(mp_int *a)
       --(a->used);
    }
 
-   /* reset the sign flag if used == 0 */
-   if (a->used == 0) {
+   /* reset the sign flag if zero */
+   if (mp_iszero(a)) {
       a->sign = MP_ZPOS;
    }
 }
diff --git a/mp_cmp.c b/mp_cmp.c
index b9c4592..9f3847b 100644
--- a/mp_cmp.c
+++ b/mp_cmp.c
@@ -8,11 +8,11 @@ mp_ord mp_cmp(const mp_int *a, const mp_int *b)
 {
    /* compare based on sign */
    if (a->sign != b->sign) {
-      return a->sign == MP_NEG ? MP_LT : MP_GT;
+      return mp_isneg(a) ? MP_LT : MP_GT;
    }
 
    /* if negative compare opposite direction */
-   if (a->sign == MP_NEG) {
+   if (mp_isneg(a)) {
       MP_EXCH(const mp_int *, a, b);
    }
 
diff --git a/mp_cmp_d.c b/mp_cmp_d.c
index 0d98e05..42f7b16 100644
--- a/mp_cmp_d.c
+++ b/mp_cmp_d.c
@@ -7,7 +7,7 @@
 mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
 {
    /* compare based on sign */
-   if (a->sign == MP_NEG) {
+   if (mp_isneg(a)) {
       return MP_LT;
    }
 
diff --git a/mp_exptmod.c b/mp_exptmod.c
index b917c0b..b8a5dcc 100644
--- a/mp_exptmod.c
+++ b/mp_exptmod.c
@@ -13,12 +13,12 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
    int dr;
 
    /* modulus P must be positive */
-   if (P->sign == MP_NEG) {
+   if (mp_isneg(P)) {
       return MP_VAL;
    }
 
    /* if exponent X is negative we have to recurse */
-   if (X->sign == MP_NEG) {
+   if (mp_isneg(X)) {
       mp_int tmpG, tmpX;
       mp_err err;
 
diff --git a/mp_exteuclid.c b/mp_exteuclid.c
index 0d0bfd3..649c4ca 100644
--- a/mp_exteuclid.c
+++ b/mp_exteuclid.c
@@ -48,7 +48,7 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp
    }
 
    /* make sure U3 >= 0 */
-   if (u3.sign == MP_NEG) {
+   if (mp_isneg(&u3)) {
       if ((err = mp_neg(&u1, &u1)) != MP_OKAY)                    goto LBL_ERR;
       if ((err = mp_neg(&u2, &u2)) != MP_OKAY)                    goto LBL_ERR;
       if ((err = mp_neg(&u3, &u3)) != MP_OKAY)                    goto LBL_ERR;
diff --git a/mp_fread.c b/mp_fread.c
index f1b55d2..53c35e8 100644
--- a/mp_fread.c
+++ b/mp_fread.c
@@ -8,7 +8,7 @@
 mp_err mp_fread(mp_int *a, int radix, FILE *stream)
 {
    mp_err err;
-   mp_sign neg = MP_ZPOS;
+   mp_sign sign = MP_ZPOS;
    int ch;
 
    /* make sure the radix is ok */
@@ -19,7 +19,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
    /* if first digit is - then set negative */
    ch = fgetc(stream);
    if (ch == (int)'-') {
-      neg = MP_NEG;
+      sign = MP_NEG;
       ch = fgetc(stream);
    }
 
@@ -56,7 +56,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
    } while ((ch = fgetc(stream)) != EOF);
 
    if (!mp_iszero(a)) {
-      a->sign = neg;
+      a->sign = sign;
    }
 
    return MP_OKAY;
diff --git a/mp_from_sbin.c b/mp_from_sbin.c
index 3ff7d5d..26eb0f1 100644
--- a/mp_from_sbin.c
+++ b/mp_from_sbin.c
@@ -14,7 +14,7 @@ mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size)
    }
 
    /* first byte is 0 for positive, non-zero for negative */
-   a->sign = (buf[0] == (uint8_t)0) ? MP_ZPOS : MP_NEG;
+   a->sign = (buf[0] != (uint8_t)0) ? MP_NEG : MP_ZPOS;
 
    return MP_OKAY;
 }
diff --git a/mp_get_double.c b/mp_get_double.c
index 122b71b..f462eb8 100644
--- a/mp_get_double.c
+++ b/mp_get_double.c
@@ -13,6 +13,6 @@ double mp_get_double(const mp_int *a)
    for (i = a->used; i --> 0;) {
       d = (d * fac) + (double)a->dp[i];
    }
-   return (a->sign == MP_NEG) ? -d : d;
+   return mp_isneg(a) ? -d : d;
 }
 #endif
diff --git a/mp_invmod.c b/mp_invmod.c
index 94929cc..e19c067 100644
--- a/mp_invmod.c
+++ b/mp_invmod.c
@@ -7,7 +7,7 @@
 mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
 {
    /* b cannot be negative and has to be >1 */
-   if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
+   if (mp_isneg(b) || (mp_cmp_d(b, 1uL) != MP_GT)) {
       return MP_VAL;
    }
 
diff --git a/mp_is_square.c b/mp_is_square.c
index 47f8300..db1cb3f 100644
--- a/mp_is_square.c
+++ b/mp_is_square.c
@@ -36,7 +36,7 @@ mp_err mp_is_square(const mp_int *arg, bool *ret)
    /* Default to Non-square :) */
    *ret = false;
 
-   if (arg->sign == MP_NEG) {
+   if (mp_isneg(arg)) {
       return MP_VAL;
    }
 
diff --git a/mp_kronecker.c b/mp_kronecker.c
index b106f77..e6bedc8 100644
--- a/mp_kronecker.c
+++ b/mp_kronecker.c
@@ -57,9 +57,9 @@ mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
       k = table[a->dp[0] & 7u];
    }
 
-   if (p1.sign == MP_NEG) {
+   if (mp_isneg(&p1)) {
       p1.sign = MP_ZPOS;
-      if (a1.sign == MP_NEG) {
+      if (mp_isneg(&a1)) {
          k = -k;
       }
    }
@@ -88,7 +88,7 @@ mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
          k = k * table[p1.dp[0] & 7u];
       }
 
-      if (a1.sign == MP_NEG) {
+      if (mp_isneg(&a1)) {
          /*
           * Compute k = (-1)^((a1)*(p1-1)/4) * k
           * a1.dp[0] + 1 cannot overflow because the MSB
diff --git a/mp_log_u32.c b/mp_log_u32.c
index 949ef87..1450d3a 100644
--- a/mp_log_u32.c
+++ b/mp_log_u32.c
@@ -5,15 +5,7 @@
 
 mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
 {
-   if (a->sign == MP_NEG) {
-      return MP_VAL;
-   }
-
-   if (mp_iszero(a)) {
-      return MP_VAL;
-   }
-
-   if (base < 2u) {
+   if (mp_isneg(a) || mp_iszero(a) || (base < 2u)) {
       return MP_VAL;
    }
 
diff --git a/mp_mul.c b/mp_mul.c
index b2dbf7d..d35fa8e 100644
--- a/mp_mul.c
+++ b/mp_mul.c
@@ -10,7 +10,7 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
    int min = MP_MIN(a->used, b->used),
        max = MP_MAX(a->used, b->used),
        digs = a->used + b->used + 1;
-   mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
+   bool neg = (a->sign != b->sign);
 
    if ((a == b) &&
        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)
    } else {
       err = MP_VAL;
    }
-   c->sign = (c->used > 0) ? neg : MP_ZPOS;
+   c->sign = ((c->used > 0) && neg) ? MP_NEG : MP_ZPOS;
    return err;
 }
 #endif
diff --git a/mp_neg.c b/mp_neg.c
index bfb6eb9..b445cd4 100644
--- a/mp_neg.c
+++ b/mp_neg.c
@@ -11,7 +11,7 @@ mp_err mp_neg(const mp_int *a, mp_int *b)
       return err;
    }
 
-   b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG;
+   b->sign = ((!mp_iszero(b) && !mp_isneg(b)) ? MP_NEG : MP_ZPOS);
 
    return MP_OKAY;
 }
diff --git a/mp_or.c b/mp_or.c
index 5cf5255..1595852 100644
--- a/mp_or.c
+++ b/mp_or.c
@@ -9,7 +9,7 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
    int used = MP_MAX(a->used, b->used) + 1, i;
    mp_err err;
    mp_digit ac = 1, bc = 1, cc = 1;
-   mp_sign csign = ((a->sign == MP_NEG) || (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
+   bool neg = (mp_isneg(a) || mp_isneg(b));
 
    if ((err = mp_grow(c, used)) != MP_OKAY) {
       return err;
@@ -19,7 +19,7 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
       mp_digit x, y;
 
       /* convert to two complement if negative */
-      if (a->sign == MP_NEG) {
+      if (mp_isneg(a)) {
          ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
          x = ac & MP_MASK;
          ac >>= MP_DIGIT_BIT;
@@ -28,7 +28,7 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
       }
 
       /* convert to two complement if negative */
-      if (b->sign == MP_NEG) {
+      if (mp_isneg(b)) {
          bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
          y = bc & MP_MASK;
          bc >>= MP_DIGIT_BIT;
@@ -39,7 +39,7 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
       c->dp[i] = x | y;
 
       /* convert to to sign-magnitude if negative */
-      if (csign == MP_NEG) {
+      if (neg) {
          cc += ~c->dp[i] & MP_MASK;
          c->dp[i] = cc & MP_MASK;
          cc >>= MP_DIGIT_BIT;
@@ -47,7 +47,7 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    c->used = used;
-   c->sign = csign;
+   c->sign = (neg ? MP_NEG : MP_ZPOS);
    mp_clamp(c);
    return MP_OKAY;
 }
diff --git a/mp_prime_strong_lucas_selfridge.c b/mp_prime_strong_lucas_selfridge.c
index e4d06eb..ffbd9d3 100644
--- a/mp_prime_strong_lucas_selfridge.c
+++ b/mp_prime_strong_lucas_selfridge.c
@@ -216,7 +216,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
           */
          oddness = mp_isodd(&Uz);
          if ((err = mp_div_2(&Uz, &Uz)) != MP_OKAY)               goto LBL_LS_ERR;
-         if ((Uz.sign == MP_NEG) && oddness) {
+         if (mp_isneg(&Uz) && 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;
@@ -225,7 +225,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result)
          }
          oddness = mp_isodd(&Vz);
          if ((err = mp_div_2(&Vz, &Vz)) != MP_OKAY)               goto LBL_LS_ERR;
-         if ((Vz.sign == MP_NEG) && oddness) {
+         if (mp_isneg(&Vz) && 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_radix_size.c b/mp_radix_size.c
index 47f2f68..7f7cbc2 100644
--- a/mp_radix_size.c
+++ b/mp_radix_size.c
@@ -27,7 +27,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
    }
 
    /* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
-   *size = (size_t)b + 2U + ((a->sign == MP_NEG) ? 1U : 0U);
+   *size = (size_t)b + 2U + (mp_isneg(a) ? 1U : 0U);
 
 LBL_ERR:
    return err;
diff --git a/mp_read_radix.c b/mp_read_radix.c
index 9512fb8..28e6eb6 100644
--- a/mp_read_radix.c
+++ b/mp_read_radix.c
@@ -7,7 +7,7 @@
 mp_err mp_read_radix(mp_int *a, const char *str, int radix)
 {
    mp_err   err;
-   mp_sign  neg = MP_ZPOS;
+   mp_sign  sign = MP_ZPOS;
 
    /* make sure the radix is ok */
    if ((radix < 2) || (radix > 64)) {
@@ -19,7 +19,7 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
     */
    if (*str == '-') {
       ++str;
-      neg = MP_NEG;
+      sign = MP_NEG;
    }
 
    /* 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)
 
    /* set the sign only if a != 0 */
    if (!mp_iszero(a)) {
-      a->sign = neg;
+      a->sign = sign;
    }
    return MP_OKAY;
 }
diff --git a/mp_reduce_is_2k.c b/mp_reduce_is_2k.c
index a5798e6..9774f96 100644
--- a/mp_reduce_is_2k.c
+++ b/mp_reduce_is_2k.c
@@ -6,7 +6,7 @@
 /* determines if mp_reduce_2k can be used */
 bool mp_reduce_is_2k(const mp_int *a)
 {
-   if (a->used == 0) {
+   if (mp_iszero(a)) {
       return false;
    } else if (a->used == 1) {
       return true;
diff --git a/mp_reduce_is_2k_l.c b/mp_reduce_is_2k_l.c
index dca2d7e..101b4a1 100644
--- a/mp_reduce_is_2k_l.c
+++ b/mp_reduce_is_2k_l.c
@@ -6,7 +6,7 @@
 /* determines if reduce_2k_l can be used */
 bool mp_reduce_is_2k_l(const mp_int *a)
 {
-   if (a->used == 0) {
+   if (mp_iszero(a)) {
       return false;
    } else if (a->used == 1) {
       return true;
diff --git a/mp_root_u32.c b/mp_root_u32.c
index f682749..1f972d9 100644
--- a/mp_root_u32.c
+++ b/mp_root_u32.c
@@ -20,7 +20,7 @@ mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
    mp_err err;
 
    /* input must be positive if b is even */
-   if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
+   if (((b & 1u) == 0u) && mp_isneg(a)) {
       return MP_VAL;
    }
 
diff --git a/mp_signed_rsh.c b/mp_signed_rsh.c
index ecaaa21..3b7e232 100644
--- a/mp_signed_rsh.c
+++ b/mp_signed_rsh.c
@@ -7,7 +7,7 @@
 mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c)
 {
    mp_err err;
-   if (a->sign == MP_ZPOS) {
+   if (!mp_isneg(a)) {
       return mp_div_2d(a, b, c, NULL);
    }
 
diff --git a/mp_sqrt.c b/mp_sqrt.c
index e36a81a..1a9dca7 100644
--- a/mp_sqrt.c
+++ b/mp_sqrt.c
@@ -10,7 +10,7 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret)
    mp_int t1, t2;
 
    /* must be positive */
-   if (arg->sign == MP_NEG) {
+   if (mp_isneg(arg)) {
       return MP_VAL;
    }
 
diff --git a/mp_sub.c b/mp_sub.c
index 8104740..1c95ad5 100644
--- a/mp_sub.c
+++ b/mp_sub.c
@@ -23,7 +23,7 @@ mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
       /* The second has a larger magnitude */
       /* The result has the *opposite* sign from */
       /* the first number. */
-      c->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
+      c->sign = (!mp_isneg(a) ? MP_NEG : MP_ZPOS);
       MP_EXCH(const mp_int *, a, b);
    } else {
       /* The first has a larger or equal magnitude */
diff --git a/mp_sub_d.c b/mp_sub_d.c
index e80df3d..b2b4d72 100644
--- a/mp_sub_d.c
+++ b/mp_sub_d.c
@@ -46,7 +46,7 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
    oldused = c->used;
 
    /* if a <= b simply fix the single digit */
-   if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) {
+   if (((a->used == 1) && (a->dp[0] <= b)) || mp_iszero(a)) {
       c->dp[0] = (a->used == 1) ? b - a->dp[0] : b;
 
       /* negative/1digit */
diff --git a/mp_to_radix.c b/mp_to_radix.c
index ebe3f4e..abf85c0 100644
--- a/mp_to_radix.c
+++ b/mp_to_radix.c
@@ -50,7 +50,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
    }
 
    /* if it is negative output a - */
-   if (t.sign == MP_NEG) {
+   if (mp_isneg(&t)) {
       /* we have to reverse our digits later... but not the - sign!! */
       ++_s;
 
@@ -84,7 +84,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
    digs++;
 
    if (written != NULL) {
-      *written = (a->sign == MP_NEG) ? (digs + 1u): digs;
+      *written = mp_isneg(a) ? (digs + 1u): digs;
    }
 
 LBL_ERR:
diff --git a/mp_to_sbin.c b/mp_to_sbin.c
index 8225d13..00884c3 100644
--- a/mp_to_sbin.c
+++ b/mp_to_sbin.c
@@ -16,7 +16,7 @@ mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
    if (written != NULL) {
       (*written)++;
    }
-   buf[0] = (a->sign == MP_ZPOS) ? (uint8_t)0 : (uint8_t)1;
+   buf[0] = mp_isneg(a) ? (uint8_t)1 : (uint8_t)0;
    return MP_OKAY;
 }
 #endif
diff --git a/mp_xor.c b/mp_xor.c
index 2fe8618..ff26419 100644
--- a/mp_xor.c
+++ b/mp_xor.c
@@ -9,7 +9,7 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
    int used = MP_MAX(a->used, b->used) + 1, i;
    mp_err err;
    mp_digit ac = 1, bc = 1, cc = 1;
-   mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
+   bool neg = (a->sign != b->sign);
 
    if ((err = mp_grow(c, used)) != MP_OKAY) {
       return err;
@@ -19,7 +19,7 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
       mp_digit x, y;
 
       /* convert to two complement if negative */
-      if (a->sign == MP_NEG) {
+      if (mp_isneg(a)) {
          ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
          x = ac & MP_MASK;
          ac >>= MP_DIGIT_BIT;
@@ -28,7 +28,7 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
       }
 
       /* convert to two complement if negative */
-      if (b->sign == MP_NEG) {
+      if (mp_isneg(b)) {
          bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
          y = bc & MP_MASK;
          bc >>= MP_DIGIT_BIT;
@@ -39,7 +39,7 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
       c->dp[i] = x ^ y;
 
       /* convert to to sign-magnitude if negative */
-      if (csign == MP_NEG) {
+      if (neg) {
          cc += ~c->dp[i] & MP_MASK;
          c->dp[i] = cc & MP_MASK;
          cc >>= MP_DIGIT_BIT;
@@ -47,7 +47,7 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    c->used = used;
-   c->sign = csign;
+   c->sign = (neg ? MP_NEG : MP_ZPOS);
    mp_clamp(c);
    return MP_OKAY;
 }
diff --git a/mtest/mpi.c b/mtest/mpi.c
index 7e71ad6..faf09d8 100644
--- a/mtest/mpi.c
+++ b/mtest/mpi.c
@@ -846,7 +846,7 @@ mp_err mp_neg(mp_int *a, mp_int *b)
   if(s_mp_cmp_d(b, 0) == MP_EQ)
     SIGN(b) = MP_ZPOS;
   else
-    SIGN(b) = (SIGN(b) == MP_NEG) ? MP_ZPOS : MP_NEG;
+    SIGN(b) = (SIGN(b) != MP_NEG) ? MP_NEG : MP_ZPOS;
 
   return MP_OKAY;
 
@@ -1055,7 +1055,7 @@ mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c)
 
   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
 
-  sgn = (SIGN(a) == SIGN(b)) ? MP_ZPOS : MP_NEG;
+  sgn = (SIGN(a) != SIGN(b)) ? MP_NEG : MP_ZPOS;
 
   if(c == b) {
     if((res = s_mp_mul(c, a)) != MP_OKAY)
@@ -1069,7 +1069,7 @@ mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c)
       return res;
   }
 
-  if(sgn == MP_ZPOS || s_mp_cmp_d(c, 0) == MP_EQ)
+  if(sgn != MP_NEG || s_mp_cmp_d(c, 0) == MP_EQ)
     SIGN(c) = MP_ZPOS;
   else
     SIGN(c) = sgn;
@@ -1821,12 +1821,12 @@ int    mp_cmp(mp_int *a, mp_int *b)
     if((mag = s_mp_cmp(a, b)) == MP_EQ)
       return MP_EQ;
 
-    if(SIGN(a) == MP_ZPOS)
+    if(SIGN(a) != MP_NEG)
       return mag;
     else
       return -mag;
 
-  } else if(SIGN(a) == MP_ZPOS) {
+  } else if(SIGN(a) != MP_NEG) {
     return MP_GT;
   } else {
     return MP_LT;
@@ -1957,7 +1957,7 @@ mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
       goto CLEANUP;
 
     /* t = -v */
-    if(SIGN(&v) == MP_ZPOS)
+    if(SIGN(&v) != MP_NEG)
       SIGN(&t) = MP_NEG;
     else
       SIGN(&t) = MP_ZPOS;
@@ -1982,7 +1982,7 @@ mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
         goto CLEANUP;
 
       /* v = -t */
-      if(SIGN(&t) == MP_ZPOS)
+      if(SIGN(&t) != MP_NEG)
         SIGN(&v) = MP_NEG;
       else
         SIGN(&v) = MP_ZPOS;
diff --git a/s_mp_div_recursive.c b/s_mp_div_recursive.c
index e2b27cd..5491f22 100644
--- a/s_mp_div_recursive.c
+++ b/s_mp_div_recursive.c
@@ -83,7 +83,7 @@ mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r
 {
    int j, m, n, sigma;
    mp_err err;
-   mp_sign neg;
+   bool neg;
    mp_digit msb_b, msb;
    mp_int A, B, Q, Q1, R, A_div, A_mod;
 
@@ -126,7 +126,7 @@ mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r
    }
 
    /* fix the sign */
-   neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
+   neg = (a->sign != b->sign);
    A.sign = B.sign = MP_ZPOS;
 
    /*
@@ -159,11 +159,11 @@ mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r
    if ((err = mp_add(&Q, &Q1, &Q)) != MP_OKAY)                                   goto LBL_ERR;
 
    /* get sign before writing to c */
-   Q.sign = (Q.used == 0) ? MP_ZPOS : a->sign;
+   Q.sign = (mp_iszero(&Q) ? MP_ZPOS : a->sign);
 
    if (q != NULL) {
       mp_exch(&Q, q);
-      q->sign = neg;
+      q->sign = (neg ? MP_NEG : MP_ZPOS);
    }
    if (r != NULL) {
       /* de-normalize the remainder */
diff --git a/s_mp_div_school.c b/s_mp_div_school.c
index cf34cc9..b6a615d 100644
--- a/s_mp_div_school.c
+++ b/s_mp_div_school.c
@@ -18,10 +18,10 @@
 */
 mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
 {
-   mp_int  q, x, y, t1, t2;
-   int     n, t, i, norm;
-   mp_sign neg;
-   mp_err  err;
+   mp_int q, x, y, t1, t2;
+   int n, t, i, norm;
+   bool neg;
+   mp_err err;
 
    if ((err = mp_init_size(&q, a->used + 2)) != MP_OKAY) {
       return err;
@@ -34,7 +34,7 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
    if ((err = mp_init_copy(&y, b)) != MP_OKAY)                    goto LBL_X;
 
    /* fix the sign */
-   neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
+   neg = (a->sign != b->sign);
    x.sign = y.sign = MP_ZPOS;
 
    /* normalize both x and y, ensure that y >= b/2, [b == 2**MP_DIGIT_BIT] */
@@ -113,7 +113,7 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
       if ((err = mp_sub(&x, &t1, &x)) != MP_OKAY)                        goto LBL_Y;
 
       /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
-      if (x.sign == MP_NEG) {
+      if (mp_isneg(&x)) {
          if ((err = mp_copy(&y, &t1)) != MP_OKAY)                        goto LBL_Y;
          if ((err = mp_lshd(&t1, (i - t) - 1)) != MP_OKAY)               goto LBL_Y;
          if ((err = mp_add(&x, &t1, &x)) != MP_OKAY)                     goto LBL_Y;
@@ -127,12 +127,12 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
     */
 
    /* get sign before writing to c */
-   x.sign = (x.used == 0) ? MP_ZPOS : a->sign;
+   x.sign = mp_iszero(&x) ? MP_ZPOS : a->sign;
 
    if (c != NULL) {
       mp_clamp(&q);
       mp_exch(&q, c);
-      c->sign = neg;
+      c->sign = (neg ? MP_NEG : MP_ZPOS);
    }
 
    if (d != NULL) {
diff --git a/s_mp_div_small.c b/s_mp_div_small.c
index c89023a..2d951be 100644
--- a/s_mp_div_small.c
+++ b/s_mp_div_small.c
@@ -8,7 +8,7 @@ mp_err s_mp_div_small(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
 {
    mp_int ta, tb, tq, q;
    int n;
-   mp_sign sign;
+   bool neg;
    mp_err err;
 
    /* init our temps */
@@ -34,14 +34,14 @@ mp_err s_mp_div_small(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
 
    /* now q == quotient and ta == remainder */
 
-   sign = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
+   neg = (a->sign != b->sign);
    if (c != NULL) {
       mp_exch(c, &q);
-      c->sign  = mp_iszero(c) ? MP_ZPOS : sign;
+      c->sign = ((neg && !mp_iszero(c)) ? MP_NEG : MP_ZPOS);
    }
    if (d != NULL) {
       mp_exch(d, &ta);
-      d->sign = mp_iszero(d) ? MP_ZPOS : a->sign;
+      d->sign = (mp_iszero(d) ? MP_ZPOS : a->sign);
    }
 LBL_ERR:
    mp_clear_multi(&ta, &tb, &tq, &q, NULL);
diff --git a/s_mp_invmod_odd.c b/s_mp_invmod_odd.c
index 8b55d5b..e12dfa1 100644
--- a/s_mp_invmod_odd.c
+++ b/s_mp_invmod_odd.c
@@ -12,7 +12,7 @@
 mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
 {
    mp_int  x, y, u, v, B, D;
-   mp_sign neg;
+   mp_sign sign;
    mp_err  err;
 
    /* 2. [modified] b must be odd   */
@@ -95,8 +95,8 @@ mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    /* b is now the inverse */
-   neg = a->sign;
-   while (D.sign == MP_NEG) {
+   sign = a->sign;
+   while (mp_isneg(&D)) {
       if ((err = mp_add(&D, b, &D)) != MP_OKAY)                   goto LBL_ERR;
    }
 
@@ -106,7 +106,7 @@ mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
    }
 
    mp_exch(&D, c);
-   c->sign = neg;
+   c->sign = sign;
    err = MP_OKAY;
 
 LBL_ERR:
diff --git a/s_mp_log_d.c b/s_mp_log_d.c
index 44edd07..c9c8df2 100644
--- a/s_mp_log_d.c
+++ b/s_mp_log_d.c
@@ -5,7 +5,7 @@
 
 static mp_word s_pow(mp_word base, mp_word exponent)
 {
-   mp_word result = 1uLL;
+   mp_word result = 1u;
    while (exponent != 0u) {
       if ((exponent & 1u) == 1u) {
          result *= base;
@@ -19,7 +19,7 @@ static mp_word s_pow(mp_word base, mp_word exponent)
 
 mp_digit s_mp_log_d(mp_digit base, mp_digit n)
 {
-   mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N;
+   mp_word bracket_low = 1u, bracket_mid, bracket_high, N;
    mp_digit ret, high = 1uL, low = 0uL, mid;
 
    if (n < base) {
diff --git a/s_mp_rand_jenkins.c b/s_mp_rand_jenkins.c
index cbbe066..3a905ba 100644
--- a/s_mp_rand_jenkins.c
+++ b/s_mp_rand_jenkins.c
@@ -27,10 +27,10 @@ static uint64_t s_rand_jenkins_val(void)
 
 void s_mp_rand_jenkins_init(uint64_t seed)
 {
-   uint64_t i;
-   jenkins_x.a = 0xf1ea5eedULL;
+   int i;
+   jenkins_x.a = 0xF1EA5EEDuLL;
    jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
-   for (i = 0uLL; i < 20uLL; ++i) {
+   for (i = 0; i < 20; ++i) {
       (void)s_rand_jenkins_val();
    }
 }
diff --git a/tommath_private.h b/tommath_private.h
index f629502..938865f 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -261,7 +261,7 @@ extern MP_PRIVATE const mp_digit s_mp_prime_tab[];
     type name(const mp_int* a)                                \
     {                                                         \
         utype res = mag(a);                                   \
-        return (a->sign == MP_NEG) ? (type)-res : (type)res;  \
+        return mp_isneg(a) ? (type)-res : (type)res;          \
     }
 
 #endif