Commit edef6ca191e120a99d2a3f62a2c2aec74de26aca

Steffen Jaeckel 2015-12-02T22:51:48

Merge branch '20151112_lint' into develop This closes #40

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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
diff --git a/bn_fast_mp_montgomery_reduce.c b/bn_fast_mp_montgomery_reduce.c
index 1f2acfc..d2d55a8 100644
--- a/bn_fast_mp_montgomery_reduce.c
+++ b/bn_fast_mp_montgomery_reduce.c
@@ -32,7 +32,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
   olduse = x->used;
 
   /* grow a as required */
-  if (x->alloc < n->used + 1) {
+  if (x->alloc < (n->used + 1)) {
     if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
       return res;
     }
@@ -57,7 +57,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
     }
 
     /* zero the high words of W[a->used..m->used*2] */
-    for (; ix < n->used * 2 + 1; ix++) {
+    for (; ix < ((n->used * 2) + 1); ix++) {
       *_W++ = 0;
     }
   }
@@ -126,7 +126,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
     /* alias for next word, where the carry goes */
     _W = W + ++ix;
 
-    for (; ix <= n->used * 2 + 1; ix++) {
+    for (; ix <= ((n->used * 2) + 1); ix++) {
       *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
     }
 
@@ -143,7 +143,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
     /* alias for shifted double precision result */
     _W = W + n->used;
 
-    for (ix = 0; ix < n->used + 1; ix++) {
+    for (ix = 0; ix < (n->used + 1); ix++) {
       *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
     }
 
diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c
index a32e9a2..aaad804 100644
--- a/bn_fast_s_mp_mul_digs.c
+++ b/bn_fast_s_mp_mul_digs.c
@@ -87,7 +87,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
   {
     register mp_digit *tmpc;
     tmpc = c->dp;
-    for (ix = 0; ix < pa+1; ix++) {
+    for (ix = 0; ix < (pa + 1); ix++) {
       /* now extract the previous digit [below the carry] */
       *tmpc++ = W[ix];
     }
diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c
index 0960b49..775c76f 100644
--- a/bn_fast_s_mp_sqr.c
+++ b/bn_fast_s_mp_sqr.c
@@ -66,7 +66,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
        * we halve the distance since they approach at a rate of 2x
        * and we have to round because odd cases need to be executed
        */
-      iy = MIN(iy, (ty-tx+1)>>1);
+      iy = MIN(iy, ((ty-tx)+1)>>1);
 
       /* execute loop */
       for (iz = 0; iz < iy; iz++) {
diff --git a/bn_mp_2expt.c b/bn_mp_2expt.c
index 1b72ab7..2845814 100644
--- a/bn_mp_2expt.c
+++ b/bn_mp_2expt.c
@@ -29,12 +29,12 @@ mp_2expt (mp_int * a, int b)
   mp_zero (a);
 
   /* grow a to accomodate the single bit */
-  if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
+  if ((res = mp_grow (a, (b / DIGIT_BIT) + 1)) != MP_OKAY) {
     return res;
   }
 
   /* set the used count of where the bit will go */
-  a->used = b / DIGIT_BIT + 1;
+  a->used = (b / DIGIT_BIT) + 1;
 
   /* put the single bit in its place */
   a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
diff --git a/bn_mp_add_d.c b/bn_mp_add_d.c
index 81b0c28..4d4e1df 100644
--- a/bn_mp_add_d.c
+++ b/bn_mp_add_d.c
@@ -23,14 +23,14 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
   mp_digit *tmpa, *tmpc, mu;
 
   /* grow c as required */
-  if (c->alloc < a->used + 1) {
+  if (c->alloc < (a->used + 1)) {
      if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
         return res;
      }
   }
 
   /* if a is negative and |a| >= b, call c = |a| - b */
-  if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) {
+  if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
      /* temporarily fix sign of a */
      a->sign = MP_ZPOS;
 
diff --git a/bn_mp_clamp.c b/bn_mp_clamp.c
index c75d553..d4fb70d 100644
--- a/bn_mp_clamp.c
+++ b/bn_mp_clamp.c
@@ -28,7 +28,7 @@ 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] == 0)) {
     --(a->used);
   }
 
diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c
index b5a7a05..9d7eef8 100644
--- a/bn_mp_cnt_lsb.c
+++ b/bn_mp_cnt_lsb.c
@@ -31,7 +31,7 @@ int mp_cnt_lsb(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] == 0); x++) {}
    q = a->dp[x];
    x *= DIGIT_BIT;
 
diff --git a/bn_mp_div.c b/bn_mp_div.c
index da79523..3ca5d7f 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -71,7 +71,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
 
   /* now q == quotient and ta == remainder */
   n  = a->sign;
-  n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
+  n2 = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
   if (c != NULL) {
      mp_exch(c, &q);
      c->sign  = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
@@ -190,7 +190,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
     /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
      * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
     if (x.dp[i] == y.dp[t]) {
-      q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
+      q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
     } else {
       mp_word tmp;
       tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
@@ -199,7 +199,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
       if (tmp > (mp_word) MP_MASK) {
         tmp = MP_MASK;
       }
-      q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
+      q.dp[(i - t) - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
     }
 
     /* while (q{i-t-1} * (yt * b + y{t-1})) >
@@ -207,32 +207,32 @@ int mp_div (mp_int * a, 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] + 1) & 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] - 1) & 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) ? 0 : 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) {
+      if ((res = mp_mul_d (&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
         goto LBL_Y;
       }
 
       /* 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) ? 0 : x.dp[i - 2];
+      t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
       t2.dp[2] = x.dp[i];
       t2.used = 3;
     } while (mp_cmp_mag(&t1, &t2) == MP_GT);
 
     /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
-    if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
+    if ((res = mp_mul_d (&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
       goto LBL_Y;
     }
 
-    if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
+    if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
       goto LBL_Y;
     }
 
@@ -245,14 +245,14 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
       if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
         goto LBL_Y;
       }
-      if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
+      if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
         goto LBL_Y;
       }
       if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
         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;
     }
   }
 
@@ -261,7 +261,7 @@ int mp_div (mp_int * a, 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 = (x.used == 0) ? MP_ZPOS : a->sign;
 
   if (c != NULL) {
     mp_clamp (&q);
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index 205a9b1..4df1d36 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -47,7 +47,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
   }
 
   /* quick outs */
-  if (b == 1 || mp_iszero(a) == MP_YES) {
+  if ((b == 1) || (mp_iszero(a) == MP_YES)) {
      if (d != NULL) {
         *d = 0;
      }
diff --git a/bn_mp_dr_reduce.c b/bn_mp_dr_reduce.c
index caabf16..2273c79 100644
--- a/bn_mp_dr_reduce.c
+++ b/bn_mp_dr_reduce.c
@@ -40,7 +40,7 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
   m = n->used;
 
   /* ensure that "x" has at least 2m digits */
-  if (x->alloc < m + m) {
+  if (x->alloc < (m + m)) {
     if ((err = mp_grow (x, m + m)) != MP_OKAY) {
       return err;
     }
@@ -62,7 +62,7 @@ top:
 
   /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
   for (i = 0; i < m; i++) {
-      r         = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
+      r         = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu;
       *tmpx1++  = (mp_digit)(r & MP_MASK);
       mu        = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
   }
diff --git a/bn_mp_export.c b/bn_mp_export.c
index cb96bff..2455fc5 100644
--- a/bn_mp_export.c
+++ b/bn_mp_export.c
@@ -37,7 +37,7 @@ 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] == 4) ? -1 : 1;
 	}
 
 	odd_nails = (nails % 8);
@@ -48,14 +48,14 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
 	nail_bytes = nails / 8;
 
 	bits = mp_count_bits(&t);
-	count = bits / (size * 8 - nails) + ((bits % (size * 8 - nails) != 0) ? 1 : 0);
+	count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
 
 	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 - 1) - i)) * size) +
+				((endian == -1) ? j : ((size - 1) - j))
 			);
 
 			if (j >= (size - nail_bytes)) {
@@ -63,9 +63,9 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
 				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) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
 
-			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) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
 				mp_clear(&t);
 				return result;
 			}
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index eeb0aa1..0973e44 100644
--- a/bn_mp_exptmod.c
+++ b/bn_mp_exptmod.c
@@ -89,7 +89,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
     
   /* if the modulus is odd or dr != 0 use the montgomery method */
 #ifdef BN_MP_EXPTMOD_FAST_C
-  if (mp_isodd (P) == MP_YES || dr !=  0) {
+  if ((mp_isodd (P) == MP_YES) || (dr !=  0)) {
     return mp_exptmod_fast (G, X, P, Y, dr);
   } else {
 #endif
diff --git a/bn_mp_exptmod_fast.c b/bn_mp_exptmod_fast.c
index e5671e1..8d280bd 100644
--- a/bn_mp_exptmod_fast.c
+++ b/bn_mp_exptmod_fast.c
@@ -96,8 +96,8 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
 
      /* automatically pick the comba one if available (saves quite a few calls/ifs) */
 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
-     if (((P->used * 2 + 1) < MP_WARRAY) &&
-          P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
+     if ((((P->used * 2) + 1) < MP_WARRAY) &&
+          (P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
         redux = fast_mp_montgomery_reduce;
      } else 
 #endif
@@ -219,12 +219,12 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
      * in the exponent.  Technically this opt is not required but it
      * does lower the # of trivial squaring/reductions used
      */
-    if (mode == 0 && y == 0) {
+    if ((mode == 0) && (y == 0)) {
       continue;
     }
 
     /* if the bit is zero and mode == 1 then we square */
-    if (mode == 1 && y == 0) {
+    if ((mode == 1) && (y == 0)) {
       if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
         goto LBL_RES;
       }
@@ -266,7 +266,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
   }
 
   /* if bits remain then square/multiply */
-  if (mode == 2 && bitcpy > 0) {
+  if ((mode == 2) && (bitcpy > 0)) {
     /* square then multiply if the bit is set */
     for (x = 0; x < bitcpy; x++) {
       if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
diff --git a/bn_mp_get_int.c b/bn_mp_get_int.c
index d1addab..99fb850 100644
--- a/bn_mp_get_int.c
+++ b/bn_mp_get_int.c
@@ -26,7 +26,7 @@ unsigned long mp_get_int(mp_int * a)
   }
 
   /* get number of digits of the lsb we have to read */
-  i = MIN(a->used,(int)((sizeof(unsigned long)*CHAR_BIT+DIGIT_BIT-1)/DIGIT_BIT))-1;
+  i = MIN(a->used,(int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
 
   /* get most significant digit of result */
   res = DIGIT(a,i);
diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
index ec64b2e..7c3d0fe 100644
--- a/bn_mp_get_long.c
+++ b/bn_mp_get_long.c
@@ -26,12 +26,12 @@ unsigned long mp_get_long(mp_int * a)
   }
 
   /* get number of digits of the lsb we have to read */
-  i = MIN(a->used,(int)((sizeof(unsigned long)*CHAR_BIT+DIGIT_BIT-1)/DIGIT_BIT))-1;
+  i = MIN(a->used,(int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
 
   /* get most significant digit of result */
   res = DIGIT(a,i);
 
-#if ULONG_MAX != 0xffffffffuL || DIGIT_BIT < 32
+#if (ULONG_MAX != 0xffffffffuL) || (DIGIT_BIT < 32)
   while (--i >= 0) {
     res = (res << DIGIT_BIT) | DIGIT(a,i);
   }
diff --git a/bn_mp_get_long_long.c b/bn_mp_get_long_long.c
index 49faf5e..4b959e6 100644
--- a/bn_mp_get_long_long.c
+++ b/bn_mp_get_long_long.c
@@ -26,7 +26,7 @@ unsigned long long mp_get_long_long (mp_int * a)
   }
 
   /* get number of digits of the lsb we have to read */
-  i = MIN(a->used,(int)((sizeof(unsigned long long)*CHAR_BIT+DIGIT_BIT-1)/DIGIT_BIT))-1;
+  i = MIN(a->used,(int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
 
   /* get most significant digit of result */
   res = DIGIT(a,i);
diff --git a/bn_mp_import.c b/bn_mp_import.c
index 13b587c..ca2a5e9 100644
--- a/bn_mp_import.c
+++ b/bn_mp_import.c
@@ -33,7 +33,7 @@ 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] == 4) ? -1 : 1;
 	}
 
 	odd_nails = (nails % 8);
@@ -44,19 +44,19 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
 	nail_bytes = nails / 8;
 
 	for (i = 0; i < count; ++i) {
-		for (j = 0; j < size - nail_bytes; ++j) {
+		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 - 1) - i)) * size) +
+					((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))
 				);
 
 			if (
-				(result = mp_mul_2d(rop, (j == 0 ? 8 - odd_nails : 8), rop)) != MP_OKAY) {
+				(result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
 				return result;
 			}
 
-			rop->dp[0] |= (j == 0 ? (byte & odd_nail_mask) : byte);
+			rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte;
 			rop->used  += 1;
 		}
 	}
diff --git a/bn_mp_invmod.c b/bn_mp_invmod.c
index c8e65d1..44951e5 100644
--- a/bn_mp_invmod.c
+++ b/bn_mp_invmod.c
@@ -19,7 +19,7 @@
 int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
 {
   /* b cannot be negative */
-  if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) {
+  if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
     return MP_VAL;
   }
 
diff --git a/bn_mp_invmod_slow.c b/bn_mp_invmod_slow.c
index 0f73820..a21f947 100644
--- a/bn_mp_invmod_slow.c
+++ b/bn_mp_invmod_slow.c
@@ -22,7 +22,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
   int     res;
 
   /* b cannot be negative */
-  if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) {
+  if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
     return MP_VAL;
   }
 
@@ -41,7 +41,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
   }
 
   /* 2. [modified] if x,y are both even then return an error! */
-  if (mp_iseven (&x) == MP_YES && mp_iseven (&y) == MP_YES) {
+  if ((mp_iseven (&x) == MP_YES) && (mp_iseven (&y) == MP_YES)) {
     res = MP_VAL;
     goto LBL_ERR;
   }
@@ -64,7 +64,7 @@ top:
       goto LBL_ERR;
     }
     /* 4.2 if A or B is odd then */
-    if (mp_isodd (&A) == MP_YES || mp_isodd (&B) == MP_YES) {
+    if ((mp_isodd (&A) == MP_YES) || (mp_isodd (&B) == MP_YES)) {
       /* A = (A+y)/2, B = (B-x)/2 */
       if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
          goto LBL_ERR;
@@ -89,7 +89,7 @@ top:
       goto LBL_ERR;
     }
     /* 5.2 if C or D is odd then */
-    if (mp_isodd (&C) == MP_YES || mp_isodd (&D) == MP_YES) {
+    if ((mp_isodd (&C) == MP_YES) || (mp_isodd (&D) == MP_YES)) {
       /* C = (C+y)/2, D = (D-x)/2 */
       if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
          goto LBL_ERR;
diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c
index 6a1155d..8be097a 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -73,9 +73,9 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
     /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
     residue = n->dp[0] & 7;
 
-    if (residue == 1 || residue == 7) {
+    if ((residue == 1) || (residue == 7)) {
       s = 1;
-    } else if (residue == 3 || residue == 5) {
+    } else if ((residue == 3) || (residue == 5)) {
       s = -1;
     }
   }
diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c
index 5952651..0e0ea0a 100644
--- a/bn_mp_lshd.c
+++ b/bn_mp_lshd.c
@@ -26,7 +26,7 @@ int mp_lshd (mp_int * a, int b)
   }
 
   /* grow to fit the new digits */
-  if (a->alloc < a->used + b) {
+  if (a->alloc < (a->used + b)) {
      if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
        return res;
      }
@@ -42,7 +42,7 @@ int mp_lshd (mp_int * a, int b)
     top = a->dp + a->used - 1;
 
     /* base */
-    bottom = a->dp + a->used - 1 - b;
+    bottom = (a->dp + a->used - 1) - b;
 
     /* much like mp_rshd this is implemented using a sliding window
      * except the window goes the otherway around.  Copying from
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index c66ecb0..b67467d 100644
--- a/bn_mp_mod.c
+++ b/bn_mp_mod.c
@@ -31,7 +31,7 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
     return res;
   }
 
-  if (mp_iszero(&t) != MP_NO || t.sign == b->sign) {
+  if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
     res = MP_OKAY;
     mp_exch (&t, c);
   } else {
diff --git a/bn_mp_mod_2d.c b/bn_mp_mod_2d.c
index b515341..926f810 100644
--- a/bn_mp_mod_2d.c
+++ b/bn_mp_mod_2d.c
@@ -39,7 +39,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
   }
 
   /* zero digits above the last digit of the modulus */
-  for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) {
+  for (x = (b / DIGIT_BIT) + (((b % DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) {
     c->dp[x] = 0;
   }
   /* clear the digit that is not completely outside/inside the modulus */
diff --git a/bn_mp_montgomery_calc_normalization.c b/bn_mp_montgomery_calc_normalization.c
index 4617492..ea87cbd 100644
--- a/bn_mp_montgomery_calc_normalization.c
+++ b/bn_mp_montgomery_calc_normalization.c
@@ -29,7 +29,7 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
   bits = mp_count_bits (b) % DIGIT_BIT;
 
   if (b->used > 1) {
-     if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) {
+     if ((res = mp_2expt (a, ((b->used - 1) * DIGIT_BIT) + bits - 1)) != MP_OKAY) {
         return res;
      }
   } else {
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index 663fbe5..d2a65c2 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -28,10 +28,10 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
    * than the available columns [255 per default] since carries
    * are fixed up in the inner loop.
    */
-  digs = n->used * 2 + 1;
+  digs = (n->used * 2) + 1;
   if ((digs < MP_WARRAY) &&
-      n->used <
-      (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
+      (n->used <
+      (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
     return fast_mp_montgomery_reduce (x, n, rho);
   }
 
@@ -52,7 +52,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
      * following inner loop to reduce the
      * input one digit at a time
      */
-    mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK);
+    mu = (mp_digit) (((mp_word)x->dp[ix] * (mp_word)rho) & MP_MASK);
 
     /* a = a + mu * m * b**i */
     {
@@ -72,8 +72,8 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
       /* Multiply and add in place */
       for (iy = 0; iy < n->used; iy++) {
         /* compute product and sum */
-        r       = ((mp_word)mu) * ((mp_word)*tmpn++) +
-                  ((mp_word) u) + ((mp_word) * tmpx);
+        r       = ((mp_word)mu * (mp_word)*tmpn++) +
+                   (mp_word) u + (mp_word) *tmpx;
 
         /* get carry */
         u       = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
diff --git a/bn_mp_montgomery_setup.c b/bn_mp_montgomery_setup.c
index e27f739..264a2bd 100644
--- a/bn_mp_montgomery_setup.c
+++ b/bn_mp_montgomery_setup.c
@@ -36,15 +36,15 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho)
   }
 
   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 *= 2 - (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 *= 2 - (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 *= 2 - (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 *= 2 - (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 a22f6ed..ea53d5e 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -44,8 +44,8 @@ int mp_mul (mp_int * a, 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)))) {
+        (MIN(a->used, b->used) <= 
+         (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * 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 f6c4c69..0859c64 100644
--- a/bn_mp_mul_2.c
+++ b/bn_mp_mul_2.c
@@ -21,7 +21,7 @@ int mp_mul_2(mp_int * a, mp_int * b)
   int     x, res, oldused;
 
   /* grow to accomodate result */
-  if (b->alloc < a->used + 1) {
+  if (b->alloc < (a->used + 1)) {
     if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
       return res;
     }
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
index 50d4563..9506814 100644
--- a/bn_mp_mul_2d.c
+++ b/bn_mp_mul_2d.c
@@ -28,8 +28,8 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
      }
   }
 
-  if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
-     if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
+  if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) {
+     if ((res = mp_grow (c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
        return res;
      }
   }
diff --git a/bn_mp_mul_d.c b/bn_mp_mul_d.c
index 37253ad..e77da5d 100644
--- a/bn_mp_mul_d.c
+++ b/bn_mp_mul_d.c
@@ -24,7 +24,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
   int      ix, res, olduse;
 
   /* make sure c is big enough to hold a*b */
-  if (c->alloc < a->used + 1) {
+  if (c->alloc < (a->used + 1)) {
     if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
       return res;
     }
@@ -48,7 +48,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
   /* compute columns */
   for (ix = 0; ix < a->used; ix++) {
     /* compute product and carry sum for this term */
-    r       = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
+    r       = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
 
     /* mask off higher bits to get a single digit */
     *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
diff --git a/bn_mp_n_root_ex.c b/bn_mp_n_root_ex.c
index ace6647..79d1dfb 100644
--- a/bn_mp_n_root_ex.c
+++ b/bn_mp_n_root_ex.c
@@ -31,7 +31,7 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
   int     res, neg;
 
   /* input must be positive if b is even */
-  if ((b & 1) == 0 && a->sign == MP_NEG) {
+  if (((b & 1) == 0) && (a->sign == MP_NEG)) {
     return MP_VAL;
   }
 
diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index 7687365..be5ebe4 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -31,7 +31,7 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
   *result = MP_NO;
 
   /* valid value of t? */
-  if (t <= 0 || t > PRIME_SIZE) {
+  if ((t <= 0) || (t > PRIME_SIZE)) {
     return MP_VAL;
   }
 
diff --git a/bn_mp_prime_miller_rabin.c b/bn_mp_prime_miller_rabin.c
index f610ea5..7b5c8d2 100644
--- a/bn_mp_prime_miller_rabin.c
+++ b/bn_mp_prime_miller_rabin.c
@@ -67,10 +67,10 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
   }
 
   /* if y != 1 and y != n1 do */
-  if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) {
+  if ((mp_cmp_d (&y, 1) != MP_EQ) && (mp_cmp (&y, &n1) != MP_EQ)) {
     j = 1;
     /* while j <= s-1 and y != n1 */
-    while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) {
+    while ((j <= (s - 1)) && (mp_cmp (&y, &n1) != MP_EQ)) {
       if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
          goto LBL_Y;
       }
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index 3e08918..9951dc3 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -27,7 +27,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
    mp_int   b;
 
    /* ensure t is valid */
-   if (t <= 0 || t > PRIME_SIZE) {
+   if ((t <= 0) || (t > PRIME_SIZE)) {
       return MP_VAL;
    }
 
@@ -129,7 +129,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
                 y = 1;
              }
          }
-      } while (y == 1 && step < ((((mp_digit)1)<<DIGIT_BIT) - kstep));
+      } while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep)));
 
       /* add the step */
       if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
@@ -137,7 +137,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 >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) {
          continue;
       }
 
diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c
index 9d96b67..1efc4fc 100644
--- a/bn_mp_prime_random_ex.c
+++ b/bn_mp_prime_random_ex.c
@@ -36,7 +36,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
    int res, err, bsize, maskOR_msb_offset;
 
    /* sanity check the input */
-   if (size <= 1 || t <= 0) {
+   if ((size <= 1) || (t <= 0)) {
       return MP_VAL;
    }
 
diff --git a/bn_mp_radix_size.c b/bn_mp_radix_size.c
index f35aa21..e5d7772 100644
--- a/bn_mp_radix_size.c
+++ b/bn_mp_radix_size.c
@@ -25,7 +25,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
   *size = 0;
 
   /* make sure the radix is in range */
-  if (radix < 2 || radix > 64) {
+  if ((radix < 2) || (radix > 64)) {
     return MP_VAL;
   }
 
@@ -36,7 +36,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
 
   /* special case for binary */
   if (radix == 2) {
-    *size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1;
+    *size = mp_count_bits (a) + ((a->sign == MP_NEG) ? 1 : 0) + 1;
     return MP_OKAY;
   }
 
diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c
index 390540e..5c9eb5e 100644
--- a/bn_mp_read_radix.c
+++ b/bn_mp_read_radix.c
@@ -25,7 +25,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
   mp_zero(a);
 
   /* make sure the radix is ok */
-  if (radix < 2 || radix > 64) {
+  if ((radix < 2) || (radix > 64)) {
     return MP_VAL;
   }
 
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index e39c518..ad2099b 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -36,9 +36,9 @@ mp_sqr (mp_int * a, mp_int * b)
   {
 #ifdef BN_FAST_S_MP_SQR_C
     /* 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))) {
+    if ((((a->used * 2) + 1) < MP_WARRAY) &&
+         (a->used <
+         (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) {
       res = fast_s_mp_sqr (a, b);
     } else
 #endif
diff --git a/bn_mp_sqrtmod_prime.c b/bn_mp_sqrtmod_prime.c
index 7906f09..968729e 100644
--- a/bn_mp_sqrtmod_prime.c
+++ b/bn_mp_sqrtmod_prime.c
@@ -56,7 +56,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
   /* Q = prime - 1 */
   mp_zero(&S);
   /* S = 0 */
-  while (mp_iseven(&Q)) {
+  while (mp_iseven(&Q) != MP_NO) {
     if ((res = mp_div_2(&Q, &Q)) != MP_OKAY)                    goto cleanup;
     /* Q = Q / 2 */
     if ((res = mp_add_d(&S, 1, &S)) != MP_OKAY)                 goto cleanup;
@@ -64,7 +64,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
   }
 
   /* find a Z such that the Legendre symbol (Z|prime) == -1 */
-  mp_set_int(&Z, 2);
+  if ((res = mp_set_int(&Z, 2)) != MP_OKAY)                     goto cleanup;
   /* Z = 2 */
   while(1) {
     if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup;
@@ -96,7 +96,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
       i++;
     }
     if (i == 0) {
-      mp_copy(&R, ret);
+      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 e1c613e..f5a932f 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -23,7 +23,7 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
   int       res, ix, oldused;
 
   /* grow c as required */
-  if (c->alloc < a->used + 1) {
+  if (c->alloc < (a->used + 1)) {
      if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
         return res;
      }
@@ -49,7 +49,7 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
   tmpc    = c->dp;
 
   /* 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)) || (a->used == 0)) {
      if (a->used == 1) {
         *tmpc++ = b - *tmpa;
      } else {
@@ -67,13 +67,13 @@ mp_sub_d (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) * CHAR_BIT) - 1);
      *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) * CHAR_BIT) - 1);
         *tmpc++ &= MP_MASK;
      }
   }
diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c
index 0734353..f04352d 100644
--- a/bn_mp_toradix.c
+++ b/bn_mp_toradix.c
@@ -24,7 +24,7 @@ int mp_toradix (mp_int * a, char *str, int radix)
   char   *_s = str;
 
   /* check range of the radix */
-  if (radix < 2 || radix > 64) {
+  if ((radix < 2) || (radix > 64)) {
     return MP_VAL;
   }
 
diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c
index 9710e5d..19b61d7 100644
--- a/bn_mp_toradix_n.c
+++ b/bn_mp_toradix_n.c
@@ -27,7 +27,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
   char   *_s = str;
 
   /* check range of the maxlen, radix */
-  if (maxlen < 2 || radix < 2 || radix > 64) {
+  if ((maxlen < 2) || (radix < 2) || (radix > 64)) {
     return MP_VAL;
   }
 
diff --git a/bn_mp_unsigned_bin_size.c b/bn_mp_unsigned_bin_size.c
index 637b041..0312625 100644
--- a/bn_mp_unsigned_bin_size.c
+++ b/bn_mp_unsigned_bin_size.c
@@ -19,7 +19,7 @@
 int mp_unsigned_bin_size (mp_int * a)
 {
   int     size = mp_count_bits (a);
-  return (size / 8 + ((size & 7) != 0 ? 1 : 0));
+  return (size / 8) + (((size & 7) != 0) ? 1 : 0);
 }
 #endif
 
diff --git a/bn_s_mp_add.c b/bn_s_mp_add.c
index 4790cd9..47fdaf1 100644
--- a/bn_s_mp_add.c
+++ b/bn_s_mp_add.c
@@ -36,7 +36,7 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
   }
 
   /* init result */
-  if (c->alloc < max + 1) {
+  if (c->alloc < (max + 1)) {
     if ((res = mp_grow (c, max + 1)) != MP_OKAY) {
       return res;
     }
diff --git a/bn_s_mp_exptmod.c b/bn_s_mp_exptmod.c
index 269a7ba..63e1b1e 100644
--- a/bn_s_mp_exptmod.c
+++ b/bn_s_mp_exptmod.c
@@ -164,12 +164,12 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
      * in the exponent.  Technically this opt is not required but it
      * does lower the # of trivial squaring/reductions used
      */
-    if (mode == 0 && y == 0) {
+    if ((mode == 0) && (y == 0)) {
       continue;
     }
 
     /* if the bit is zero and mode == 1 then we square */
-    if (mode == 1 && y == 0) {
+    if ((mode == 1) && (y == 0)) {
       if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
         goto LBL_RES;
       }
@@ -211,7 +211,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
   }
 
   /* if bits remain then square/multiply */
-  if (mode == 2 && bitcpy > 0) {
+  if ((mode == 2) && (bitcpy > 0)) {
     /* square then multiply if the bit is set */
     for (x = 0; x < bitcpy; x++) {
       if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index 69e11d6..bd8553d 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -29,8 +29,8 @@ int s_mp_mul_digs (mp_int * a, 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)))) {
+      (MIN (a->used, b->used) < 
+          (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
     return fast_s_mp_mul_digs (a, b, c, digs);
   }
 
@@ -61,9 +61,9 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
     /* compute the columns of the output and propagate the carry */
     for (iy = 0; iy < pb; iy++) {
       /* compute the column as a mp_word */
-      r       = ((mp_word)*tmpt) +
-                ((mp_word)tmpx) * ((mp_word)*tmpy++) +
-                ((mp_word) u);
+      r       = (mp_word)*tmpt +
+                ((mp_word)tmpx * (mp_word)*tmpy++) +
+                (mp_word)u;
 
       /* the new column is the lower part of the result */
       *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
@@ -72,7 +72,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
       u       = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
     }
     /* set carry if it is placed below digs */
-    if (ix + iy < digs) {
+    if ((ix + iy) < digs) {
       *tmpt = u;
     }
   }
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index 3e3b48e..153cea4 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -30,7 +30,7 @@ s_mp_mul_high_digs (mp_int * a, 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) < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
     return fast_s_mp_mul_high_digs (a, b, c, digs);
   }
 #endif
@@ -57,9 +57,9 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
 
     for (iy = digs - ix; iy < pb; iy++) {
       /* calculate the double precision result */
-      r       = ((mp_word)*tmpt) +
-                ((mp_word)tmpx) * ((mp_word)*tmpy++) +
-                ((mp_word) u);
+      r       = (mp_word)*tmpt +
+                ((mp_word)tmpx * (mp_word)*tmpy++) +
+                (mp_word)u;
 
       /* get the lower part */
       *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
diff --git a/bn_s_mp_sqr.c b/bn_s_mp_sqr.c
index dd06b77..68c95bc 100644
--- a/bn_s_mp_sqr.c
+++ b/bn_s_mp_sqr.c
@@ -24,18 +24,18 @@ int s_mp_sqr (mp_int * a, mp_int * b)
   mp_digit u, tmpx, *tmpt;
 
   pa = a->used;
-  if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
+  if ((res = mp_init_size (&t, (2 * pa) + 1)) != MP_OKAY) {
     return res;
   }
 
   /* default used is maximum possible size */
-  t.used = 2*pa + 1;
+  t.used = (2 * pa) + 1;
 
   for (ix = 0; ix < pa; ix++) {
     /* first calculate the digit at 2*ix */
     /* calculate double precision result */
-    r = ((mp_word) t.dp[2*ix]) +
-        ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
+    r = (mp_word)t.dp[2*ix] +
+        ((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
 
     /* store lower part in result */
     t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
@@ -47,7 +47,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
     tmpx        = a->dp[ix];
 
     /* alias for where to store the results */
-    tmpt        = t.dp + (2*ix + 1);
+    tmpt        = t.dp + ((2 * ix) + 1);
     
     for (iy = ix + 1; iy < pa; iy++) {
       /* first calculate the product */
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index 4cf92d5..55260fe 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -47,14 +47,14 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
     u = 0;
     for (i = 0; i < min; i++) {
       /* T[i] = A[i] - B[i] - U */
-      *tmpc = *tmpa++ - *tmpb++ - u;
+      *tmpc = (*tmpa++ - *tmpb++) - u;
 
       /* U = carry bit of T[i]
        * Note this saves performing an AND operation since
        * 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)) - 1));
 
       /* Clear carry from T[i] */
       *tmpc++ &= MP_MASK;
@@ -66,7 +66,7 @@ s_mp_sub (mp_int * a, 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)) - 1));
 
       /* Clear carry from T[i] */
       *tmpc++ &= MP_MASK;
diff --git a/tommath.h b/tommath.h
index 8e784e8..4c66e15 100644
--- a/tommath.h
+++ b/tommath.h
@@ -208,8 +208,8 @@ int mp_init_size(mp_int *a, int size);
 
 /* ---> Basic Manipulations <--- */
 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
-#define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
-#define mp_isodd(a)  ((((a)->used > 0) && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
+#define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
+#define mp_isodd(a)  ((((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
 #define mp_isneg(a)  (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
 
 /* set to zero */
diff --git a/tommath_private.h b/tommath_private.h
index 986beac..bc7cd35 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -66,9 +66,9 @@ int mp_karatsuba_sqr(mp_int *a, mp_int *b);
 int mp_toom_sqr(mp_int *a, mp_int *b);
 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
-int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
-int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
-int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
+int fast_mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho);
+int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode);
+int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
 void bn_reverse(unsigned char *s, int len);
 
 extern const char *mp_s_rmap;
@@ -88,14 +88,14 @@ int func_name (mp_int * a, type b)                       \
   mp_zero (a);                                           \
                                                          \
   /* set four bits at a time */                          \
-  for (x = 0; x < (sizeof(type) * 2); x++) {             \
+  for (x = 0; x < (sizeof(type) * 2u); x++) {            \
     /* shift the number up four bits */                  \
     if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) {        \
       return res;                                        \
     }                                                    \
                                                          \
     /* OR in the top four bits of the source */          \
-    a->dp[0] |= (b >> ((sizeof(type) * 8) - 4)) & 15;    \
+    a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \
                                                          \
     /* shift the source up to the next four bits */      \
     b <<= 4;                                             \