Commit bdbed00079cecfbd71e66a0d55040da40c5a528f

Francois Perrad 2017-10-15T19:57:12

literal suffix

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
901
diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c
index 161f785..c99fd94 100644
--- a/bn_fast_s_mp_sqr.c
+++ b/bn_fast_s_mp_sqr.c
@@ -77,7 +77,7 @@ int fast_s_mp_sqr(const mp_int *a, mp_int *b)
       _W = _W + _W + W1;
 
       /* even columns have the square term in them */
-      if ((ix&1) == 0) {
+      if (((unsigned)ix & 1u) == 0u) {
          _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
       }
 
diff --git a/bn_mp_clamp.c b/bn_mp_clamp.c
index 3853914..79a5b20 100644
--- a/bn_mp_clamp.c
+++ b/bn_mp_clamp.c
@@ -27,7 +27,7 @@ void mp_clamp(mp_int *a)
    /* decrease used while the most significant digit is
     * zero.
     */
-   while ((a->used > 0) && (a->dp[a->used - 1] == 0)) {
+   while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) {
       --(a->used);
    }
 
diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c
index 9a94d3d..219c369 100644
--- a/bn_mp_cnt_lsb.c
+++ b/bn_mp_cnt_lsb.c
@@ -31,17 +31,17 @@ int mp_cnt_lsb(const mp_int *a)
    }
 
    /* scan lower digits until non-zero */
-   for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {}
+   for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
    q = a->dp[x];
    x *= DIGIT_BIT;
 
    /* now scan this digit until a 1 is found */
-   if ((q & 1) == 0) {
+   if ((q & 1u) == 0u) {
       do {
-         qq  = q & 15;
+         qq  = q & 15u;
          x  += lnz[qq];
          q >>= 4;
-      } while (qq == 0);
+      } while (qq == 0u);
    }
    return x;
 }
diff --git a/bn_mp_div.c b/bn_mp_div.c
index acf0d78..105802f 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -207,13 +207,13 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
 
          do q{i-t-1} -= 1;
       */
-      q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK;
+      q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1uL) & MP_MASK;
       do {
-         q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK;
+         q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & MP_MASK;
 
          /* find left hand */
          mp_zero(&t1);
-         t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1];
+         t1.dp[0] = ((t - 1) < 0) ? 0u : y.dp[t - 1];
          t1.dp[1] = y.dp[t];
          t1.used = 2;
          if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
@@ -221,8 +221,8 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
          }
 
          /* find right hand */
-         t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2];
-         t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
+         t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2];
+         t2.dp[1] = ((i - 1) < 0) ? 0u : x.dp[i - 1];
          t2.dp[2] = x.dp[i];
          t2.used = 3;
       } while (mp_cmp_mag(&t1, &t2) == MP_GT);
@@ -252,7 +252,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
             goto LBL_Y;
          }
 
-         q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK;
+         q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & MP_MASK;
       }
    }
 
diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c
index edc8982..2907a1b 100644
--- a/bn_mp_div_2.c
+++ b/bn_mp_div_2.c
@@ -42,7 +42,7 @@ int mp_div_2(const mp_int *a, mp_int *b)
       r = 0;
       for (x = b->used - 1; x >= 0; x--) {
          /* get the carry for the next iteration */
-         rr = *tmpa & 1;
+         rr = *tmpa & 1u;
 
          /* shift the current digit, add in carry and store */
          *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
diff --git a/bn_mp_div_2d.c b/bn_mp_div_2d.c
index eae3498..bdee8c7 100644
--- a/bn_mp_div_2d.c
+++ b/bn_mp_div_2d.c
@@ -50,11 +50,11 @@ int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
 
    /* shift any bit count < DIGIT_BIT */
    D = (mp_digit)(b % DIGIT_BIT);
-   if (D != 0) {
+   if (D != 0u) {
       mp_digit *tmpc, mask, shift;
 
       /* mask */
-      mask = (((mp_digit)1) << D) - 1;
+      mask = (1uL << D) - 1uL;
 
       /* shift for lsb */
       shift = DIGIT_BIT - D;
diff --git a/bn_mp_div_3.c b/bn_mp_div_3.c
index 9cc8caa..6106599 100644
--- a/bn_mp_div_3.c
+++ b/bn_mp_div_3.c
@@ -36,7 +36,7 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
    for (ix = a->used - 1; ix >= 0; ix--) {
       w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
 
-      if (w >= 3) {
+      if (w >= 3u) {
          /* multiply w by [1/3] */
          t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
 
@@ -46,9 +46,9 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
          /* fixup the remainder as required since
           * the optimization is not exact.
           */
-         while (w >= 3) {
-            t += 1;
-            w -= 3;
+         while (w >= 3u) {
+            t += 1u;
+            w -= 3u;
          }
       } else {
          t = 0;
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index db4a0a2..ecbf7b3 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -20,12 +20,12 @@ static int s_is_power_of_two(mp_digit b, int *p)
    int x;
 
    /* fast return if no power of two */
-   if ((b == 0) || ((b & (b-1)) != 0)) {
+   if ((b == 0u) || ((b & (b-1u)) != 0u)) {
       return 0;
    }
 
    for (x = 0; x < DIGIT_BIT; x++) {
-      if (b == (((mp_digit)1)<<x)) {
+      if (b == (1uL<<x)) {
          *p = x;
          return 1;
       }
@@ -42,12 +42,12 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
    int     res, ix;
 
    /* cannot divide by zero */
-   if (b == 0) {
+   if (b == 0u) {
       return MP_VAL;
    }
 
    /* quick outs */
-   if ((b == 1) || (mp_iszero(a) == MP_YES)) {
+   if ((b == 1u) || (mp_iszero(a) == MP_YES)) {
       if (d != NULL) {
          *d = 0;
       }
@@ -60,7 +60,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
    /* power of two ? */
    if (s_is_power_of_two(b, &ix) == 1) {
       if (d != NULL) {
-         *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
+         *d = a->dp[0] & ((1uL<<ix) - 1uL);
       }
       if (c != NULL) {
          return mp_div_2d(a, ix, c, NULL);
@@ -70,7 +70,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
 
 #ifdef BN_MP_DIV_3_C
    /* three? */
-   if (b == 3) {
+   if (b == 3u) {
       return mp_div_3(a, c, d);
    }
 #endif
diff --git a/bn_mp_export.c b/bn_mp_export.c
index 4276f4f..06dbfa5 100644
--- a/bn_mp_export.c
+++ b/bn_mp_export.c
@@ -38,33 +38,33 @@ int mp_export(void *rop, size_t *countp, int order, size_t size,
       } lint;
       lint.i = 0x01020304;
 
-      endian = (lint.c[0] == 4) ? -1 : 1;
+      endian = (lint.c[0] == '\x04') ? -1 : 1;
    }
 
-   odd_nails = (nails % 8);
+   odd_nails = (nails % 8u);
    odd_nail_mask = 0xff;
    for (i = 0; i < odd_nails; ++i) {
-      odd_nail_mask ^= (1 << (7 - i));
+      odd_nail_mask ^= (unsigned char)(1u << (7u - i));
    }
-   nail_bytes = nails / 8;
+   nail_bytes = nails / 8u;
 
    bits = mp_count_bits(&t);
-   count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
+   count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u);
 
    for (i = 0; i < count; ++i) {
       for (j = 0; j < size; ++j) {
          unsigned char *byte = (unsigned char *)rop +
-                               (((order == -1) ? i : ((count - 1) - i)) * size) +
-                               ((endian == -1) ? j : ((size - 1) - j));
+                               (((order == -1) ? i : ((count - 1u) - i)) * size) +
+                               ((endian == -1) ? j : ((size - 1u) - j));
 
          if (j >= (size - nail_bytes)) {
             *byte = 0;
             continue;
          }
 
-         *byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
+         *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
 
-         if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
+         if ((result = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
             mp_clear(&t);
             return result;
          }
diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c
index d8d6b45..9056334 100644
--- a/bn_mp_expt_d_ex.c
+++ b/bn_mp_expt_d_ex.c
@@ -31,9 +31,9 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
    mp_set(c, 1uL);
 
    if (fast != 0) {
-      while (b > 0) {
+      while (b > 0u) {
          /* if the bit is set multiply */
-         if ((b & 1) != 0) {
+         if ((b & 1u) != 0u) {
             if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
                mp_clear(&g);
                return res;
@@ -41,7 +41,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
          }
 
          /* square */
-         if (b > 1) {
+         if (b > 1u) {
             if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
                mp_clear(&g);
                return res;
@@ -60,7 +60,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
          }
 
          /* if the bit is set multiply */
-         if ((b & (mp_digit)(((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
+         if ((b & (mp_digit)(1uL << (DIGIT_BIT - 1))) != 0u) {
             if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
                mp_clear(&g);
                return res;
diff --git a/bn_mp_import.c b/bn_mp_import.c
index afd735e..b0bb6e5 100644
--- a/bn_mp_import.c
+++ b/bn_mp_import.c
@@ -34,27 +34,27 @@ int mp_import(mp_int *rop, size_t count, int order, size_t size,
       } lint;
       lint.i = 0x01020304;
 
-      endian = (lint.c[0] == 4) ? -1 : 1;
+      endian = (lint.c[0] == '\x04') ? -1 : 1;
    }
 
-   odd_nails = (nails % 8);
+   odd_nails = (nails % 8u);
    odd_nail_mask = 0xff;
    for (i = 0; i < odd_nails; ++i) {
-      odd_nail_mask ^= (1 << (7 - i));
+      odd_nail_mask ^= (unsigned char)(1u << (7u - i));
    }
-   nail_bytes = nails / 8;
+   nail_bytes = nails / 8u;
 
    for (i = 0; i < count; ++i) {
       for (j = 0; j < (size - nail_bytes); ++j) {
          unsigned char byte = *((unsigned char *)op +
-                                (((order == 1) ? i : ((count - 1) - i)) * size) +
-                                ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)));
+                                (((order == 1) ? i : ((count - 1u) - i)) * size) +
+                                ((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
 
-         if ((result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
+         if ((result = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
             return result;
          }
 
-         rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte;
+         rop->dp[0] |= (j == 0u) ? (byte & odd_nail_mask) : byte;
          rop->used  += 1;
       }
    }
diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c
index 9547a53..34ebcf0 100644
--- a/bn_mp_is_square.c
+++ b/bn_mp_is_square.c
@@ -58,7 +58,7 @@ int mp_is_square(const mp_int *arg, int *ret)
    }
 
    /* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
-   if (rem_128[127 & DIGIT(arg, 0)] == 1) {
+   if (rem_128[127u & DIGIT(arg, 0)] == 1) {
       return MP_OKAY;
    }
 
@@ -82,13 +82,13 @@ int mp_is_square(const mp_int *arg, int *ret)
     * free "t" so the easiest way is to goto ERR.  We know that res
     * is already equal to MP_OKAY from the mp_mod call
     */
-   if (((1L<<(r%11)) & 0x5C4L) != 0L)       goto ERR;
-   if (((1L<<(r%13)) & 0x9E4L) != 0L)       goto ERR;
-   if (((1L<<(r%17)) & 0x5CE8L) != 0L)      goto ERR;
-   if (((1L<<(r%19)) & 0x4F50CL) != 0L)     goto ERR;
-   if (((1L<<(r%23)) & 0x7ACCA0L) != 0L)    goto ERR;
-   if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L)   goto ERR;
-   if (((1L<<(r%31)) & 0x6DE2B848L) != 0L)  goto ERR;
+   if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL)         goto ERR;
+   if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL)         goto ERR;
+   if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL)        goto ERR;
+   if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL)       goto ERR;
+   if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL)      goto ERR;
+   if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL)     goto ERR;
+   if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL)    goto ERR;
 
    /* Final check - is sqr(sqrt(arg)) == arg ? */
    if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c
index 63df203..ef2e72f 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -72,21 +72,21 @@ int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
    }
 
    /* step 4.  if e is even set s=1 */
-   if ((k & 1) == 0) {
+   if (((unsigned)k & 1u) == 0u) {
       s = 1;
    } else {
       /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
-      residue = n->dp[0] & 7;
+      residue = n->dp[0] & 7u;
 
-      if ((residue == 1) || (residue == 7)) {
+      if ((residue == 1u) || (residue == 7u)) {
          s = 1;
-      } else if ((residue == 3) || (residue == 5)) {
+      } else if ((residue == 3u) || (residue == 5u)) {
          s = -1;
       }
    }
 
    /* step 5.  if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
-   if (((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
+   if (((n->dp[0] & 3u) == 3u) && ((a1.dp[0] & 3u) == 3u)) {
       s = -s;
    }
 
diff --git a/bn_mp_mod_2d.c b/bn_mp_mod_2d.c
index 8e69757..1a31e33 100644
--- a/bn_mp_mod_2d.c
+++ b/bn_mp_mod_2d.c
@@ -43,7 +43,7 @@ int mp_mod_2d(const mp_int *a, int b, mp_int *c)
    }
    /* clear the digit that is not completely outside/inside the modulus */
    c->dp[b / DIGIT_BIT] &=
-      (mp_digit)((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
+      (mp_digit)((1uL << (((mp_digit) b) % DIGIT_BIT)) - 1uL);
    mp_clamp(c);
    return MP_OKAY;
 }
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index a9c7752..7247923 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -31,7 +31,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
    if ((digs < MP_WARRAY) &&
        (x->used <= MP_WARRAY) &&
        (n->used <
-        (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
+        (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
       return fast_mp_montgomery_reduce(x, n, rho);
    }
 
@@ -85,7 +85,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
 
 
          /* propagate carries upwards as required*/
-         while (u != 0) {
+         while (u != 0u) {
             *tmpx   += u;
             u        = *tmpx >> DIGIT_BIT;
             *tmpx++ &= MP_MASK;
diff --git a/bn_mp_montgomery_setup.c b/bn_mp_montgomery_setup.c
index 685ba51..1c14d62 100644
--- a/bn_mp_montgomery_setup.c
+++ b/bn_mp_montgomery_setup.c
@@ -30,20 +30,20 @@ int mp_montgomery_setup(const mp_int *n, mp_digit *rho)
     */
    b = n->dp[0];
 
-   if ((b & 1) == 0) {
+   if ((b & 1u) == 0u) {
       return MP_VAL;
    }
 
-   x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
-   x *= 2 - (b * x);             /* here x*a==1 mod 2**8 */
+   x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
+   x *= 2u - (b * x);              /* here x*a==1 mod 2**8 */
 #if !defined(MP_8BIT)
-   x *= 2 - (b * x);             /* here x*a==1 mod 2**16 */
+   x *= 2u - (b * x);              /* here x*a==1 mod 2**16 */
 #endif
 #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
-   x *= 2 - (b * x);             /* here x*a==1 mod 2**32 */
+   x *= 2u - (b * x);              /* here x*a==1 mod 2**32 */
 #endif
 #ifdef MP_64BIT
-   x *= 2 - (b * x);             /* here x*a==1 mod 2**64 */
+   x *= 2u - (b * x);              /* here x*a==1 mod 2**64 */
 #endif
 
    /* rho = -1/m mod b */
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index 71d523d..090c0d3 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -45,7 +45,7 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
 #ifdef BN_FAST_S_MP_MUL_DIGS_C
          if ((digs < MP_WARRAY) &&
              (MIN(a->used, b->used) <=
-              (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
+              (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
             res = fast_s_mp_mul_digs(a, b, c, digs);
          } else
 #endif
diff --git a/bn_mp_mul_2.c b/bn_mp_mul_2.c
index 1744681..f93d8a4 100644
--- a/bn_mp_mul_2.c
+++ b/bn_mp_mul_2.c
@@ -49,7 +49,7 @@ int mp_mul_2(const mp_int *a, mp_int *b)
          rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
 
          /* now shift up this digit, add in the carry [from the previous] */
-         *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
+         *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
 
          /* copy the carry that would be from the source
           * digit into the next iteration
@@ -58,7 +58,7 @@ int mp_mul_2(const mp_int *a, mp_int *b)
       }
 
       /* new leading digit? */
-      if (r != 0) {
+      if (r != 0u) {
          /* add a MSB which is always 1 at this point */
          *tmpb = 1;
          ++(b->used);
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
index 4938e57..46e24bf 100644
--- a/bn_mp_mul_2d.c
+++ b/bn_mp_mul_2d.c
@@ -43,12 +43,12 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c)
 
    /* shift any bit count < DIGIT_BIT */
    d = (mp_digit)(b % DIGIT_BIT);
-   if (d != 0) {
+   if (d != 0u) {
       mp_digit *tmpc, shift, mask, r, rr;
       int x;
 
       /* bitmask for carries */
-      mask = (((mp_digit)1) << d) - 1;
+      mask = (1uL << d) - 1uL;
 
       /* shift for msbs */
       shift = DIGIT_BIT - d;
@@ -71,7 +71,7 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c)
       }
 
       /* set final carry */
-      if (r != 0) {
+      if (r != 0u) {
          c->dp[(c->used)++] = r;
       }
    }
diff --git a/bn_mp_n_root_ex.c b/bn_mp_n_root_ex.c
index f5d7da5..60c9929 100644
--- a/bn_mp_n_root_ex.c
+++ b/bn_mp_n_root_ex.c
@@ -31,7 +31,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
    int     res;
 
    /* input must be positive if b is even */
-   if (((b & 1) == 0) && (a->sign == MP_NEG)) {
+   if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
       return MP_VAL;
    }
 
@@ -63,7 +63,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
       /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
 
       /* t3 = t1**(b-1) */
-      if ((res = mp_expt_d_ex(&t1, b - 1, &t3, fast)) != MP_OKAY) {
+      if ((res = mp_expt_d_ex(&t1, b - 1u, &t3, fast)) != MP_OKAY) {
          goto LBL_T3;
       }
 
diff --git a/bn_mp_prime_is_divisible.c b/bn_mp_prime_is_divisible.c
index c1e1158..c49fdd2 100644
--- a/bn_mp_prime_is_divisible.c
+++ b/bn_mp_prime_is_divisible.c
@@ -35,7 +35,7 @@ int mp_prime_is_divisible(const mp_int *a, int *result)
       }
 
       /* is the residue zero? */
-      if (res == 0) {
+      if (res == 0u) {
          *result = MP_YES;
          return MP_OKAY;
       }
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index 1dfa2be..948e97e 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -46,10 +46,10 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
                 * however, the prime must be
                 * congruent to 3 mod 4
                 */
-               if ((ltm_prime_tab[x + 1] & 3) != 3) {
+               if ((ltm_prime_tab[x + 1] & 3u) != 3u) {
                   /* scan upwards for a prime congruent to 3 mod 4 */
                   for (y = x + 1; y < PRIME_SIZE; y++) {
-                     if ((ltm_prime_tab[y] & 3) == 3) {
+                     if ((ltm_prime_tab[y] & 3u) == 3u) {
                         mp_set(a, ltm_prime_tab[y]);
                         return MP_OKAY;
                      }
@@ -80,8 +80,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
 
    if (bbs_style == 1) {
       /* if a mod 4 != 3 subtract the correct value to make it so */
-      if ((a->dp[0] & 3) != 3) {
-         if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) {
+      if ((a->dp[0] & 3u) != 3u) {
+         if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
             return err;
          };
       }
@@ -127,11 +127,11 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
             }
 
             /* set flag if zero */
-            if (res_tab[x] == 0) {
+            if (res_tab[x] == 0u) {
                y = 1;
             }
          }
-      } while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep)));
+      } while ((y == 1) && (step < ((1uL << DIGIT_BIT) - kstep)));
 
       /* add the step */
       if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
@@ -139,7 +139,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
       }
 
       /* if didn't pass sieve and step == MAX then skip test */
-      if ((y == 1) && (step >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) {
+      if ((y == 1) && (step >= ((1uL << DIGIT_BIT) - kstep))) {
          continue;
       }
 
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index 92a9a97..83afe27 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -15,7 +15,7 @@
  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
  */
 
-#if MP_GEN_RANDOM_MAX == 0xffffffff
+#if MP_GEN_RANDOM_MAX == 0xffffffffu
 #define MP_GEN_RANDOM_SHIFT  32
 #elif MP_GEN_RANDOM_MAX == 32767
 /* SHRT_MAX */
@@ -54,7 +54,7 @@ int mp_rand(mp_int *a, int digits)
    /* first place a random non-zero digit */
    do {
       d = s_gen_random();
-   } while (d == 0);
+   } while (d == 0u);
 
    if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
       return res;
diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c
index ad9f05f..6398c43 100644
--- a/bn_mp_read_unsigned_bin.c
+++ b/bn_mp_read_unsigned_bin.c
@@ -41,7 +41,7 @@ int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
       a->used += 1;
 #else
       a->dp[0] = (*b & MP_MASK);
-      a->dp[1] |= ((*b++ >> 7U) & 1);
+      a->dp[1] |= ((*b++ >> 7) & 1u);
       a->used += 2;
 #endif
    }
diff --git a/bn_mp_reduce.c b/bn_mp_reduce.c
index 9e8b962..4dc724f 100644
--- a/bn_mp_reduce.c
+++ b/bn_mp_reduce.c
@@ -33,7 +33,7 @@ int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
    mp_rshd(&q, um - 1);
 
    /* according to HAC this optimization is ok */
-   if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
+   if (((mp_digit) um) > (1uL << (DIGIT_BIT - 1))) {
       if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
          goto CLEANUP;
       }
diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c
index 2922cad..e1e2bc8 100644
--- a/bn_mp_reduce_2k.c
+++ b/bn_mp_reduce_2k.c
@@ -32,7 +32,7 @@ top:
       goto ERR;
    }
 
-   if (d != 1) {
+   if (d != 1u) {
       /* q = q * d */
       if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
          goto ERR;
diff --git a/bn_mp_reduce_is_2k.c b/bn_mp_reduce_is_2k.c
index 932521e..f59d535 100644
--- a/bn_mp_reduce_is_2k.c
+++ b/bn_mp_reduce_is_2k.c
@@ -32,7 +32,7 @@ int mp_reduce_is_2k(const mp_int *a)
 
       /* Test every bit from the second digit up, must be 1 */
       for (ix = DIGIT_BIT; ix < iy; ix++) {
-         if ((a->dp[iw] & iz) == 0) {
+         if ((a->dp[iw] & iz) == 0u) {
             return MP_NO;
          }
          iz <<= 1;
diff --git a/bn_mp_set.c b/bn_mp_set.c
index eaf7fed..952d080 100644
--- a/bn_mp_set.c
+++ b/bn_mp_set.c
@@ -20,7 +20,7 @@ void mp_set(mp_int *a, mp_digit b)
 {
    mp_zero(a);
    a->dp[0] = b & MP_MASK;
-   a->used  = (a->dp[0] != 0) ? 1 : 0;
+   a->used  = (a->dp[0] != 0u) ? 1 : 0;
 }
 #endif
 
diff --git a/bn_mp_set_int.c b/bn_mp_set_int.c
index 4c71180..84312ae 100644
--- a/bn_mp_set_int.c
+++ b/bn_mp_set_int.c
@@ -30,7 +30,7 @@ int mp_set_int(mp_int *a, unsigned long b)
       }
 
       /* OR in the top four bits of the source */
-      a->dp[0] |= (b >> 28) & 15;
+      a->dp[0] |= (b >> 28) & 15uL;
 
       /* shift the source up to the next four bits */
       b <<= 4;
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index 2b71097..039d9eb 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -37,7 +37,7 @@ int mp_sqr(const mp_int *a, mp_int *b)
          /* can we use the fast comba multiplier? */
          if ((((a->used * 2) + 1) < MP_WARRAY) &&
              (a->used <
-              (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) {
+              (int)(1u << (((sizeof(mp_word) * (size_t)CHAR_BIT) - (2u * (size_t)DIGIT_BIT)) - 1u)))) {
             res = fast_s_mp_sqr(a, b);
          } else
 #endif
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index d76ef6b..d4cf3de 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -39,7 +39,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
     * Handbook of Applied Cryptography algorithm 3.36
     */
    if ((res = mp_mod_d(prime, 4uL, &i)) != MP_OKAY)               goto cleanup;
-   if (i == 3) {
+   if (i == 3u) {
       if ((res = mp_add_d(prime, 1uL, &t1)) != MP_OKAY)           goto cleanup;
       if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup;
       if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup;
@@ -64,7 +64,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    }
 
    /* find a Z such that the Legendre symbol (Z|prime) == -1 */
-   if ((res = mp_set_int(&Z, 2)) != MP_OKAY)                     goto cleanup;
+   if ((res = mp_set_int(&Z, 2uL)) != MP_OKAY)                    goto cleanup;
    /* Z = 2 */
    while (1) {
       if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup;
@@ -84,7 +84,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    /* T = n ^ Q mod prime */
    if ((res = mp_copy(&S, &M)) != MP_OKAY)                       goto cleanup;
    /* M = S */
-   if ((res = mp_set_int(&two, 2)) != MP_OKAY)                   goto cleanup;
+   if ((res = mp_set_int(&two, 2uL)) != MP_OKAY)                 goto cleanup;
 
    res = MP_VAL;
    while (1) {
@@ -95,7 +95,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
          if ((res = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup;
          i++;
       }
-      if (i == 0) {
+      if (i == 0u) {
          if ((res = mp_copy(&R, ret)) != MP_OKAY)                  goto cleanup;
          res = MP_OKAY;
          goto cleanup;
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index 4d66a90..e5fbfff 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -67,13 +67,13 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
 
       /* subtract first digit */
       *tmpc    = *tmpa++ - b;
-      mu       = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1);
+      mu       = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
       *tmpc++ &= MP_MASK;
 
       /* handle rest of the digits */
       for (ix = 1; ix < a->used; ix++) {
          *tmpc    = *tmpa++ - mu;
-         mu       = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1);
+         mu       = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
          *tmpc++ &= MP_MASK;
       }
    }
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index 9339cce..a53f711 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -28,9 +28,9 @@ int mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
    x = 0;
    while (mp_iszero(&t) == MP_NO) {
 #ifndef MP_8BIT
-      b[x++] = (unsigned char)(t.dp[0] & 255);
+      b[x++] = (unsigned char)(t.dp[0] & 255u);
 #else
-      b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 0x01) << 7));
+      b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
 #endif
       if ((res = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
          mp_clear(&t);
diff --git a/bn_mp_unsigned_bin_size.c b/bn_mp_unsigned_bin_size.c
index 04107fe..2b9ce8a 100644
--- a/bn_mp_unsigned_bin_size.c
+++ b/bn_mp_unsigned_bin_size.c
@@ -19,7 +19,7 @@
 int mp_unsigned_bin_size(const mp_int *a)
 {
    int     size = mp_count_bits(a);
-   return (size / 8) + (((size & 7) != 0) ? 1 : 0);
+   return (size / 8) + ((((unsigned)size & 7u) != 0u) ? 1 : 0);
 }
 #endif
 
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index af13a02..796c2dd 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -30,7 +30,7 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
    /* can we use the fast multiplier? */
    if (((digs) < MP_WARRAY) &&
        (MIN(a->used, b->used) <
-        (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
+        (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
       return fast_s_mp_mul_digs(a, b, c, digs);
    }
 
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index 37c108e..c3293e4 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -29,7 +29,7 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
    /* can we use the fast multiplier? */
 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
    if (((a->used + b->used + 1) < MP_WARRAY)
-       && (MIN(a->used, b->used) < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
+       && (MIN(a->used, b->used) < (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
       return fast_s_mp_mul_high_digs(a, b, c, digs);
    }
 #endif
diff --git a/bn_s_mp_sqr.c b/bn_s_mp_sqr.c
index aae06eb..67a1721 100644
--- a/bn_s_mp_sqr.c
+++ b/bn_s_mp_sqr.c
@@ -65,7 +65,7 @@ int s_mp_sqr(const mp_int *a, mp_int *b)
          u       = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
       }
       /* propagate upwards */
-      while (u != ((mp_digit) 0)) {
+      while (u != 0uL) {
          r       = ((mp_word) *tmpt) + ((mp_word) u);
          *tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
          u       = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index 52b8096..3d21243 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -53,7 +53,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
           * if a carry does occur it will propagate all the way to the
           * MSB.  As a result a single shift is enough to get the carry
           */
-         u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
+         u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1u));
 
          /* Clear carry from T[i] */
          *tmpc++ &= MP_MASK;
@@ -65,7 +65,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
          *tmpc = *tmpa++ - u;
 
          /* U = carry bit of T[i] */
-         u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
+         u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1u));
 
          /* Clear carry from T[i] */
          *tmpc++ &= MP_MASK;
diff --git a/tommath.h b/tommath.h
index 0119906..207609c 100644
--- a/tommath.h
+++ b/tommath.h
@@ -105,7 +105,7 @@ typedef mp_digit mp_min_u32;
 /* use arc4random on platforms that support it */
 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
 #   define MP_GEN_RANDOM()    arc4random()
-#   define MP_GEN_RANDOM_MAX  0xffffffff
+#   define MP_GEN_RANDOM_MAX  0xffffffffu
 #endif
 
 /* use rand() as fall-back if there's no better rand function */
@@ -160,7 +160,7 @@ extern int KARATSUBA_MUL_CUTOFF,
 #endif
 
 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
-#define MP_WARRAY               (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
+#define MP_WARRAY               (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
 
 /* the infamous mp_int structure */
 typedef struct  {
diff --git a/tommath_private.h b/tommath_private.h
index 7e47f18..40982de 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -101,7 +101,7 @@ int func_name (mp_int * a, type b)                       \
     }                                                    \
                                                          \
     /* OR in the top four bits of the source */          \
-    a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \
+    a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15uL;\
                                                          \
     /* shift the source up to the next four bits */      \
     b <<= 4;                                             \