Commit 489bf69f65bf1bd260d9877a95eab3af8107e35e

Steffen Jaeckel 2015-11-12T01:33:57

Merge branch '20151025_lint' into develop This closes #38

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
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c
index c595247..25e145b 100644
--- a/bn_fast_mp_invmod.c
+++ b/bn_fast_mp_invmod.c
@@ -27,7 +27,7 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
   int     res, neg;
 
   /* 2. [modified] b must be odd   */
-  if (mp_iseven (b) == 1) {
+  if (mp_iseven (b) == MP_YES) {
     return MP_VAL;
   }
 
@@ -57,13 +57,13 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
 
 top:
   /* 4.  while u is even do */
-  while (mp_iseven (&u) == 1) {
+  while (mp_iseven (&u) == MP_YES) {
     /* 4.1 u = u/2 */
     if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
       goto LBL_ERR;
     }
     /* 4.2 if B is odd then */
-    if (mp_isodd (&B) == 1) {
+    if (mp_isodd (&B) == MP_YES) {
       if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
         goto LBL_ERR;
       }
@@ -75,13 +75,13 @@ top:
   }
 
   /* 5.  while v is even do */
-  while (mp_iseven (&v) == 1) {
+  while (mp_iseven (&v) == MP_YES) {
     /* 5.1 v = v/2 */
     if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
       goto LBL_ERR;
     }
     /* 5.2 if D is odd then */
-    if (mp_isodd (&D) == 1) {
+    if (mp_isodd (&D) == MP_YES) {
       /* D = (D-x)/2 */
       if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
         goto LBL_ERR;
@@ -115,7 +115,7 @@ top:
   }
 
   /* if not zero goto step 4 */
-  if (mp_iszero (&u) == 0) {
+  if (mp_iszero (&u) == MP_NO) {
     goto top;
   }
 
diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c
index adda3f5..4d4000c 100644
--- a/bn_fast_s_mp_mul_digs.c
+++ b/bn_fast_s_mp_mul_digs.c
@@ -78,7 +78,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
 
       /* make next carry */
       _W = _W >> ((mp_word)DIGIT_BIT);
- }
+  }
 
   /* setup dest */
   olduse  = c->used;
diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c
index a617767..92cc4e8 100644
--- a/bn_mp_cnt_lsb.c
+++ b/bn_mp_cnt_lsb.c
@@ -26,12 +26,12 @@ int mp_cnt_lsb(mp_int *a)
    mp_digit q, qq;
 
    /* easy out */
-   if (mp_iszero(a) == 1) {
+   if (mp_iszero(a) == MP_YES) {
       return 0;
    }
 
    /* 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 336a57b..2b87399 100644
--- a/bn_mp_div.c
+++ b/bn_mp_div.c
@@ -24,7 +24,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
    int    res, n, n2;
 
   /* is divisor zero ? */
-  if (mp_iszero (b) == 1) {
+  if (mp_iszero (b) == MP_YES) {
     return MP_VAL;
   }
 
@@ -40,7 +40,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
     }
     return res;
   }
-	
+
   /* init our temps */
   if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) {
      return res;
@@ -50,7 +50,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
   mp_set(&tq, 1);
   n = mp_count_bits(a) - mp_count_bits(b);
   if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
-      ((res = mp_abs(b, &tb)) != MP_OKAY) || 
+      ((res = mp_abs(b, &tb)) != MP_OKAY) ||
       ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
       ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
       goto LBL_ERR;
@@ -87,17 +87,17 @@ LBL_ERR:
 
 #else
 
-/* integer signed division. 
+/* integer signed division.
  * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
  * HAC pp.598 Algorithm 14.20
  *
- * Note that the description in HAC is horribly 
- * incomplete.  For example, it doesn't consider 
- * the case where digits are removed from 'x' in 
- * the inner loop.  It also doesn't consider the 
+ * Note that the description in HAC is horribly
+ * incomplete.  For example, it doesn't consider
+ * the case where digits are removed from 'x' in
+ * the inner loop.  It also doesn't consider the
  * case that y has fewer than three digits, etc..
  *
- * The overall algorithm is as described as 
+ * The overall algorithm is as described as
  * 14.20 from HAC but fixed to treat these cases.
 */
 int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
@@ -106,7 +106,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
   int     res, n, t, i, norm, neg;
 
   /* is divisor zero ? */
-  if (mp_iszero (b) == 1) {
+  if (mp_iszero (b) == MP_YES) {
     return MP_VAL;
   }
 
@@ -187,7 +187,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
       continue;
     }
 
-    /* step 3.1 if xi == yt then set q{i-t-1} to b-1, 
+    /* 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);
@@ -196,15 +196,16 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
       tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
       tmp |= ((mp_word) x.dp[i - 1]);
       tmp /= ((mp_word) y.dp[t]);
-      if (tmp > (mp_word) MP_MASK)
+      if (tmp > (mp_word) MP_MASK) {
         tmp = MP_MASK;
+      }
       q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
     }
 
-    /* while (q{i-t-1} * (yt * b + y{t-1})) > 
-             xi * b**2 + xi-1 * b + xi-2 
-     
-       do q{i-t-1} -= 1; 
+    /* while (q{i-t-1} * (yt * b + y{t-1})) >
+             xi * b**2 + xi-1 * b + xi-2
+
+       do q{i-t-1} -= 1;
     */
     q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
     do {
@@ -255,10 +256,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
     }
   }
 
-  /* now q is the quotient and x is the remainder 
-   * [which we have to normalize] 
+  /* now q is the quotient and x is the remainder
+   * [which we have to normalize]
    */
-  
+
   /* get sign before writing to c */
   x.sign = x.used == 0 ? MP_ZPOS : a->sign;
 
@@ -269,7 +270,9 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
   }
 
   if (d != NULL) {
-    mp_div_2d (&x, norm, &x, NULL);
+    if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) {
+      goto LBL_Y;
+    }
     mp_exch (&x, d);
   }
 
diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c
index 579221d..dbaeb10 100644
--- a/bn_mp_div_d.c
+++ b/bn_mp_div_d.c
@@ -20,7 +20,7 @@ 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))) {
+   if ((b == 0) || ((b & (b-1)) != 0)) {
       return 0;
    }
 
@@ -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) == 1) {
+  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 84c3006..7cfe462 100644
--- a/bn_mp_dr_reduce.c
+++ b/bn_mp_dr_reduce.c
@@ -82,7 +82,9 @@ top:
    * Each successive "recursion" makes the input smaller and smaller.
    */
   if (mp_cmp_mag (x, n) != MP_LT) {
-    s_mp_sub(x, n, x);
+    if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
+      return err;
+    }
     goto top;
   }
   return MP_OKAY;
diff --git a/bn_mp_export.c b/bn_mp_export.c
index 1a82e4a..7829315 100644
--- a/bn_mp_export.c
+++ b/bn_mp_export.c
@@ -34,7 +34,8 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
 		union {
 			unsigned int i;
 			char c[4];
-		} lint = {0x01020304};
+		} lint;
+		lint.i = 0x01020304;
 
 		endian = (lint.c[0] == 4 ? -1 : 1);
 	}
@@ -47,7 +48,7 @@ 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) ? 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) {
@@ -73,7 +74,7 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
 
 	mp_clear(&t);
 
-	if (countp) {
+	if (countp != NULL) {
 		*countp = count;
 	}
 
diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c
index 93f4f19..8b12d24 100644
--- a/bn_mp_expt_d_ex.c
+++ b/bn_mp_expt_d_ex.c
@@ -30,10 +30,10 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
   /* set initial result */
   mp_set (c, 1);
 
-  if (fast) {
+  if (fast != 0) {
     while (b > 0) {
       /* if the bit is set multiply */
-      if (b & 1) {
+      if ((b & 1) != 0) {
         if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
           mp_clear (&g);
           return res;
@@ -41,9 +41,11 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
       }
 
       /* square */
-      if (b > 1 && (res = mp_sqr (&g, &g)) != MP_OKAY) {
-        mp_clear (&g);
-        return res;
+      if (b > 1) {
+        if ((res = mp_sqr (&g, &g)) != MP_OKAY) {
+          mp_clear (&g);
+          return res;
+        }
       }
 
       /* shift to next bit */
@@ -69,9 +71,9 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
       /* shift to next bit */
       b <<= 1;
     }
-    } /* if ... else */
+  } /* if ... else */
 
-    mp_clear (&g);
+  mp_clear (&g);
   return MP_OKAY;
 }
 #endif
diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c
index da0a8d3..1d9059e 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) == 1 || 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_exteuclid.c b/bn_mp_exteuclid.c
index c486e1c..f04fca0 100644
--- a/bn_mp_exteuclid.c
+++ b/bn_mp_exteuclid.c
@@ -15,7 +15,7 @@
  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
  */
 
-/* Extended euclidean algorithm of (a, b) produces 
+/* Extended euclidean algorithm of (a, b) produces
    a*u1 + b*u2 = u3
  */
 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
@@ -61,9 +61,9 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
 
    /* make sure U3 >= 0 */
    if (u3.sign == MP_NEG) {
-      mp_neg(&u1, &u1);
-      mp_neg(&u2, &u2);
-      mp_neg(&u3, &u3);
+       if ((err = mp_neg(&u1, &u1)) != MP_OKAY)                                   { goto _ERR; }
+       if ((err = mp_neg(&u2, &u2)) != MP_OKAY)                                   { goto _ERR; }
+       if ((err = mp_neg(&u3, &u3)) != MP_OKAY)                                   { goto _ERR; }
    }
 
    /* copy result out */
diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c
index 7d1c1c6..8cf6e00 100644
--- a/bn_mp_gcd.c
+++ b/bn_mp_gcd.c
@@ -70,7 +70,7 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
      }
   }
 
-  while (mp_iszero(&v) == 0) {
+  while (mp_iszero(&v) == MP_NO) {
      /* make sure v is the largest */
      if (mp_cmp_mag(&u, &v) == MP_GT) {
         /* swap u and v to make sure v is >= u */
diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
index 8286082..7f2a54b 100644
--- a/bn_mp_get_long.c
+++ b/bn_mp_get_long.c
@@ -31,7 +31,7 @@ unsigned long mp_get_long(mp_int * a)
   /* 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_import.c b/bn_mp_import.c
index b5d4ed9..c106e02 100644
--- a/bn_mp_import.c
+++ b/bn_mp_import.c
@@ -30,7 +30,8 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
 		union {
 			unsigned int i;
 			char c[4];
-		} lint = {0x01020304};
+		} lint;
+		lint.i = 0x01020304;
 
 		endian = (lint.c[0] == 4 ? -1 : 1);
 	}
diff --git a/bn_mp_init_multi.c b/bn_mp_init_multi.c
index 830068c..388b813 100644
--- a/bn_mp_init_multi.c
+++ b/bn_mp_init_multi.c
@@ -37,7 +37,7 @@ int mp_init_multi(mp_int *mp, ...)
             /* now start cleaning up */            
             cur_arg = mp;
             va_start(clean_args, mp);
-            while (n--) {
+            while (n-- != 0) {
                 mp_clear(cur_arg);
                 cur_arg = va_arg(clean_args, mp_int*);
             }
diff --git a/bn_mp_invmod.c b/bn_mp_invmod.c
index 67a9a61..c0eb2c0 100644
--- a/bn_mp_invmod.c
+++ b/bn_mp_invmod.c
@@ -19,13 +19,13 @@
 int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
 {
   /* b cannot be negative */
-  if (b->sign == MP_NEG || mp_iszero(b) == 1) {
+  if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) {
     return MP_VAL;
   }
 
 #ifdef BN_FAST_MP_INVMOD_C
   /* if the modulus is odd we can use a faster routine instead */
-  if (mp_isodd (b) == 1) {
+  if (mp_isodd (b) == MP_YES) {
     return fast_mp_invmod (a, b, c);
   }
 #endif
diff --git a/bn_mp_invmod_slow.c b/bn_mp_invmod_slow.c
index e0adda9..50ce2c1 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) == 1) {
+  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) == 1 && mp_iseven (&y) == 1) {
+  if (mp_iseven (&x) == MP_YES && mp_iseven (&y) == MP_YES) {
     res = MP_VAL;
     goto LBL_ERR;
   }
@@ -58,13 +58,13 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
 
 top:
   /* 4.  while u is even do */
-  while (mp_iseven (&u) == 1) {
+  while (mp_iseven (&u) == MP_YES) {
     /* 4.1 u = u/2 */
     if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
       goto LBL_ERR;
     }
     /* 4.2 if A or B is odd then */
-    if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
+    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;
@@ -83,13 +83,13 @@ top:
   }
 
   /* 5.  while v is even do */
-  while (mp_iseven (&v) == 1) {
+  while (mp_iseven (&v) == MP_YES) {
     /* 5.1 v = v/2 */
     if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
       goto LBL_ERR;
     }
     /* 5.2 if C or D is odd then */
-    if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
+    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;
@@ -137,7 +137,7 @@ top:
   }
 
   /* if not zero goto step 4 */
-  if (mp_iszero (&u) == 0)
+  if (mp_iszero (&u) == MP_NO)
     goto top;
 
   /* now a = C, b = D, gcd == g*v */
diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c
index 5348ca9..8c2f221 100644
--- a/bn_mp_is_square.c
+++ b/bn_mp_is_square.c
@@ -82,13 +82,13 @@ int mp_is_square(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 )             goto ERR;
-  if ( (1L<<(r%13)) & 0x9E4L )             goto ERR;
-  if ( (1L<<(r%17)) & 0x5CE8L )            goto ERR;
-  if ( (1L<<(r%19)) & 0x4F50CL )           goto ERR;
-  if ( (1L<<(r%23)) & 0x7ACCA0L )          goto ERR;
-  if ( (1L<<(r%29)) & 0xC2EDD0CL )         goto ERR;
-  if ( (1L<<(r%31)) & 0x6DE2B848L )        goto ERR;
+  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;
 
   /* 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 b0722df..b8427a5 100644
--- a/bn_mp_jacobi.c
+++ b/bn_mp_jacobi.c
@@ -30,7 +30,7 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c)
   }
 
   /* step 1.  if a == 0, return 0 */
-  if (mp_iszero (a) == 1) {
+  if (mp_iszero (a) == MP_YES) {
     *c = 0;
     return MP_OKAY;
   }
diff --git a/bn_mp_mod.c b/bn_mp_mod.c
index 589972d..f7eb1c4 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) || 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_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index ffdd127..e92a988 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -85,7 +85,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
 
 
       /* propagate carries upwards as required*/
-      while (u) {
+      while (u != 0) {
         *tmpx   += u;
         u        = *tmpx >> DIGIT_BIT;
         *tmpx++ &= MP_MASK;
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index 74b5135..d7f0074 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -49,12 +49,13 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
       res = fast_s_mp_mul_digs (a, b, c, digs);
     } else 
 #endif
+    {
 #ifdef BN_S_MP_MUL_DIGS_C
       res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
 #else
       res = MP_VAL;
 #endif
-
+    }
   }
   c->sign = (c->used > 0) ? neg : MP_ZPOS;
   return res;
diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c
index e306f3b..a977a44 100644
--- a/bn_mp_prime_next_prime.c
+++ b/bn_mp_prime_next_prime.c
@@ -84,7 +84,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
          if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { return err; };
       }
    } else {
-      if (mp_iseven(a) == 1) {
+      if (mp_iseven(a) == MP_YES) {
          /* force odd */
          if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
             return err;
diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c
index 981a8fe..3dfc425 100644
--- a/bn_mp_prime_random_ex.c
+++ b/bn_mp_prime_random_ex.c
@@ -41,7 +41,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
    }
 
    /* LTM_PRIME_SAFE implies LTM_PRIME_BBS */
-   if (flags & LTM_PRIME_SAFE) {
+   if ((flags & LTM_PRIME_SAFE) != 0) {
       flags |= LTM_PRIME_BBS;
    }
 
@@ -60,13 +60,13 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
    /* calc the maskOR_msb */
    maskOR_msb        = 0;
    maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
-   if (flags & LTM_PRIME_2MSB_ON) {
+   if ((flags & LTM_PRIME_2MSB_ON) != 0) {
       maskOR_msb       |= 0x80 >> ((9 - size) & 7);
    }  
 
    /* get the maskOR_lsb */
    maskOR_lsb         = 1;
-   if (flags & LTM_PRIME_BBS) {
+   if ((flags & LTM_PRIME_BBS) != 0) {
       maskOR_lsb     |= 3;
    }
 
@@ -94,7 +94,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
          continue;
       }
 
-      if (flags & LTM_PRIME_SAFE) {
+      if ((flags & LTM_PRIME_SAFE) != 0) {
          /* see if (a-1)/2 is prime */
          if ((err = mp_sub_d(a, 1, a)) != MP_OKAY)                    { goto error; }
          if ((err = mp_div_2(a, a)) != MP_OKAY)                       { goto error; }
@@ -104,7 +104,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
       }
    } while (res == MP_NO);
 
-   if (flags & LTM_PRIME_SAFE) {
+   if ((flags & LTM_PRIME_SAFE) != 0) {
       /* restore a to the original value */
       if ((err = mp_mul_2(a, a)) != MP_OKAY)                          { goto error; }
       if ((err = mp_add_d(a, 1, a)) != MP_OKAY)                       { goto error; }
diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c
index 178339e..d407e9d 100644
--- a/bn_mp_read_radix.c
+++ b/bn_mp_read_radix.c
@@ -43,12 +43,12 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
   mp_zero (a);
   
   /* process each digit of the string */
-  while (*str) {
+  while (*str != '\0') {
     /* if the radix <= 36 the conversion is case insensitive
      * this allows numbers like 1AB and 1ab to represent the same  value
      * [e.g. in hex]
      */
-    ch = (char) ((radix <= 36) ? toupper ((int)*str) : *str);
+    ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
     for (y = 0; y < 64; y++) {
       if (ch == mp_s_rmap[y]) {
          break;
@@ -73,7 +73,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
   }
   
   /* set the sign only if a != 0 */
-  if (mp_iszero(a) != 1) {
+  if (mp_iszero(a) != MP_YES) {
      a->sign = neg;
   }
   return MP_OKAY;
diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c
index 3598ec8..4d0472c 100644
--- a/bn_mp_read_unsigned_bin.c
+++ b/bn_mp_read_unsigned_bin.c
@@ -37,12 +37,12 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
     }
 
 #ifndef MP_8BIT
-      a->dp[0] |= *b++;
-      a->used += 1;
+    a->dp[0] |= *b++;
+    a->used += 1;
 #else
-      a->dp[0] = (*b & MP_MASK);
-      a->dp[1] |= ((*b++ >> 7U) & 1);
-      a->used += 2;
+    a->dp[0] = (*b & MP_MASK);
+    a->dp[1] |= ((*b++ >> 7U) & 1);
+    a->used += 2;
 #endif
   }
   mp_clamp (a);
diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c
index cfa59c7..d19b847 100644
--- a/bn_mp_reduce_2k.c
+++ b/bn_mp_reduce_2k.c
@@ -20,35 +20,37 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
 {
    mp_int q;
    int    p, res;
-   
+
    if ((res = mp_init(&q)) != MP_OKAY) {
       return res;
    }
-   
-   p = mp_count_bits(n);    
+
+   p = mp_count_bits(n);
 top:
    /* q = a/2**p, a = a mod 2**p */
    if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
       goto ERR;
    }
-   
+
    if (d != 1) {
       /* q = q * d */
-      if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { 
+      if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
          goto ERR;
       }
    }
-   
+
    /* a = a + q */
    if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
       goto ERR;
    }
-   
+
    if (mp_cmp_mag(a, n) != MP_LT) {
-      s_mp_sub(a, n, a);
+      if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
+         goto ERR;
+      }
       goto top;
    }
-   
+
 ERR:
    mp_clear(&q);
    return res;
diff --git a/bn_mp_reduce_2k_l.c b/bn_mp_reduce_2k_l.c
index 076018c..675065f 100644
--- a/bn_mp_reduce_2k_l.c
+++ b/bn_mp_reduce_2k_l.c
@@ -15,7 +15,7 @@
  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
  */
 
-/* reduces a modulo n where n is of the form 2**p - d 
+/* reduces a modulo n where n is of the form 2**p - d
    This differs from reduce_2k since "d" can be larger
    than a single digit.
 */
@@ -23,33 +23,35 @@ int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
 {
    mp_int q;
    int    p, res;
-   
+
    if ((res = mp_init(&q)) != MP_OKAY) {
       return res;
    }
-   
-   p = mp_count_bits(n);    
+
+   p = mp_count_bits(n);
 top:
    /* q = a/2**p, a = a mod 2**p */
    if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
       goto ERR;
    }
-   
+
    /* q = q * d */
-   if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { 
+   if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
       goto ERR;
    }
-   
+
    /* a = a + q */
    if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
       goto ERR;
    }
-   
+
    if (mp_cmp_mag(a, n) != MP_LT) {
-      s_mp_sub(a, n, a);
+      if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
+         goto ERR;
+      }
       goto top;
    }
-   
+
 ERR:
    mp_clear(&q);
    return res;
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index 0eaa09e..bbc1208 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -21,8 +21,9 @@ int mp_shrink (mp_int * a)
   mp_digit *tmp;
   int used = 1;
   
-  if(a->used > 0)
+  if(a->used > 0) {
     used = a->used;
+  }
   
   if (a->alloc != used) {
     if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) {
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index f1d72ef..5167ce0 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -29,7 +29,7 @@ mp_sqr (mp_int * a, mp_int * b)
   } else 
 #endif
 #ifdef BN_MP_KARATSUBA_SQR_C
-if (a->used >= KARATSUBA_SQR_CUTOFF) {
+  if (a->used >= KARATSUBA_SQR_CUTOFF) {
     res = mp_karatsuba_sqr (a, b);
   } else 
 #endif
@@ -42,11 +42,13 @@ if (a->used >= KARATSUBA_SQR_CUTOFF) {
       res = fast_s_mp_sqr (a, b);
     } else
 #endif
+    {
 #ifdef BN_S_MP_SQR_C
       res = s_mp_sqr (a, b);
 #else
       res = MP_VAL;
 #endif
+    }
   }
   b->sign = MP_ZPOS;
   return res;
diff --git a/bn_mp_to_signed_bin.c b/bn_mp_to_signed_bin.c
index 0c0149e..2898908 100644
--- a/bn_mp_to_signed_bin.c
+++ b/bn_mp_to_signed_bin.c
@@ -23,7 +23,7 @@ int mp_to_signed_bin (mp_int * a, unsigned char *b)
   if ((res = mp_to_unsigned_bin (a, b + 1)) != MP_OKAY) {
     return res;
   }
-  b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1);
+  b[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
   return MP_OKAY;
 }
 #endif
diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c
index 9655b31..132a6ca 100644
--- a/bn_mp_to_unsigned_bin.c
+++ b/bn_mp_to_unsigned_bin.c
@@ -26,7 +26,7 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
   }
 
   x = 0;
-  while (mp_iszero (&t) == 0) {
+  while (mp_iszero (&t) == MP_NO) {
 #ifndef MP_8BIT
       b[x++] = (unsigned char) (t.dp[0] & 255);
 #else
diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c
index 3d8ab99..942680d 100644
--- a/bn_mp_toom_mul.c
+++ b/bn_mp_toom_mul.c
@@ -15,28 +15,28 @@
  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
  */
 
-/* multiplication using the Toom-Cook 3-way algorithm 
+/* multiplication using the Toom-Cook 3-way algorithm
  *
- * Much more complicated than Karatsuba but has a lower 
- * asymptotic running time of O(N**1.464).  This algorithm is 
- * only particularly useful on VERY large inputs 
+ * Much more complicated than Karatsuba but has a lower
+ * asymptotic running time of O(N**1.464).  This algorithm is
+ * only particularly useful on VERY large inputs
  * (we're talking 1000s of digits here...).
 */
 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
 {
     mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
     int res, B;
-        
+
     /* init temps */
-    if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, 
-                             &a0, &a1, &a2, &b0, &b1, 
+    if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
+                             &a0, &a1, &a2, &b0, &b1,
                              &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
        return res;
     }
-    
+
     /* B */
     B = MIN(a->used, b->used) / 3;
-    
+
     /* a = a2 * B**2 + a1 * B + a0 */
     if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
        goto ERR;
@@ -46,13 +46,15 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
        goto ERR;
     }
     mp_rshd(&a1, B);
-    mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
+    if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) {
+       goto ERR;
+    }
 
     if ((res = mp_copy(a, &a2)) != MP_OKAY) {
        goto ERR;
     }
     mp_rshd(&a2, B*2);
-    
+
     /* b = b2 * B**2 + b1 * B + b0 */
     if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
        goto ERR;
@@ -62,23 +64,23 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
        goto ERR;
     }
     mp_rshd(&b1, B);
-    mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
+    (void)mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
 
     if ((res = mp_copy(b, &b2)) != MP_OKAY) {
        goto ERR;
     }
     mp_rshd(&b2, B*2);
-    
+
     /* w0 = a0*b0 */
     if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     /* w4 = a2 * b2 */
     if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
     if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
        goto ERR;
@@ -92,7 +94,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
     if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
        goto ERR;
     }
@@ -105,11 +107,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
     if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
     if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
        goto ERR;
@@ -123,7 +125,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
     if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
        goto ERR;
     }
@@ -136,11 +138,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
     if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
        goto ERR;
     }
-    
+
     if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
        goto ERR;
     }
-    
+
 
     /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
     if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
@@ -158,125 +160,125 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
     if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
        goto ERR;
     }
-    
-    /* now solve the matrix 
-    
+
+    /* now solve the matrix
+
        0  0  0  0  1
        1  2  4  8  16
        1  1  1  1  1
        16 8  4  2  1
        1  0  0  0  0
-       
-       using 12 subtractions, 4 shifts, 
-              2 small divisions and 1 small multiplication 
+
+       using 12 subtractions, 4 shifts,
+              2 small divisions and 1 small multiplication
      */
-     
-     /* r1 - r4 */
-     if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - r0 */
-     if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1/2 */
-     if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3/2 */
-     if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r2 - r0 - r4 */
-     if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1 - r2 */
-     if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - r2 */
-     if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1 - 8r0 */
-     if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - 8r4 */
-     if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* 3r2 - r1 - r3 */
-     if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1 - r2 */
-     if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - r2 */
-     if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1/3 */
-     if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3/3 */
-     if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
-        goto ERR;
-     }
-     
-     /* at this point shift W[n] by B*n */
-     if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
-        goto ERR;
-     }     
-     
-     if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
-        goto ERR;
-     }     
-     
+
+    /* r1 - r4 */
+    if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - r0 */
+    if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1/2 */
+    if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3/2 */
+    if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r2 - r0 - r4 */
+    if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1 - r2 */
+    if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - r2 */
+    if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1 - 8r0 */
+    if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - 8r4 */
+    if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* 3r2 - r1 - r3 */
+    if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1 - r2 */
+    if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - r2 */
+    if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1/3 */
+    if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3/3 */
+    if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
+       goto ERR;
+    }
+
+    /* at this point shift W[n] by B*n */
+    if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
+       goto ERR;
+    }
+
+    if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
+       goto ERR;
+    }
+
 ERR:
-     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, 
-                    &a0, &a1, &a2, &b0, &b1, 
-                    &b2, &tmp1, &tmp2, NULL);
-     return res;
-}     
-     
+    mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
+                   &a0, &a1, &a2, &b0, &b1,
+                   &b2, &tmp1, &tmp2, NULL);
+    return res;
+}
+
 #endif
 
 /* $Source$ */
diff --git a/bn_mp_toom_sqr.c b/bn_mp_toom_sqr.c
index cbee45c..0fe967b 100644
--- a/bn_mp_toom_sqr.c
+++ b/bn_mp_toom_sqr.c
@@ -39,7 +39,9 @@ mp_toom_sqr(mp_int *a, mp_int *b)
        goto ERR;
     }
     mp_rshd(&a1, B);
-    mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
+    if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) {
+       goto ERR;
+    }
 
     if ((res = mp_copy(a, &a2)) != MP_OKAY) {
        goto ERR;
@@ -115,108 +117,108 @@ mp_toom_sqr(mp_int *a, mp_int *b)
        using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication.
      */
 
-     /* r1 - r4 */
-     if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - r0 */
-     if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1/2 */
-     if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3/2 */
-     if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r2 - r0 - r4 */
-     if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1 - r2 */
-     if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - r2 */
-     if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1 - 8r0 */
-     if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - 8r4 */
-     if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* 3r2 - r1 - r3 */
-     if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1 - r2 */
-     if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3 - r2 */
-     if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r1/3 */
-     if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
-        goto ERR;
-     }
-     /* r3/3 */
-     if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
-        goto ERR;
-     }
-
-     /* at this point shift W[n] by B*n */
-     if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
-        goto ERR;
-     }
-
-     if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
-        goto ERR;
-     }
-     if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
-        goto ERR;
-     }
+    /* r1 - r4 */
+    if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - r0 */
+    if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1/2 */
+    if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3/2 */
+    if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r2 - r0 - r4 */
+    if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1 - r2 */
+    if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - r2 */
+    if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1 - 8r0 */
+    if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - 8r4 */
+    if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* 3r2 - r1 - r3 */
+    if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1 - r2 */
+    if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3 - r2 */
+    if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r1/3 */
+    if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
+       goto ERR;
+    }
+    /* r3/3 */
+    if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
+       goto ERR;
+    }
+
+    /* at this point shift W[n] by B*n */
+    if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
+       goto ERR;
+    }
+
+    if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
+       goto ERR;
+    }
+    if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
+       goto ERR;
+    }
 
 ERR:
-     mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
-     return res;
+    mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
+    return res;
 }
 
 #endif
diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c
index f14fd07..94efc7a 100644
--- a/bn_mp_toradix.c
+++ b/bn_mp_toradix.c
@@ -29,7 +29,7 @@ int mp_toradix (mp_int * a, char *str, int radix)
   }
 
   /* quick out if its zero */
-  if (mp_iszero(a) == 1) {
+  if (mp_iszero(a) == MP_YES) {
      *str++ = '0';
      *str = '\0';
      return MP_OKAY;
@@ -47,7 +47,7 @@ int mp_toradix (mp_int * a, char *str, int radix)
   }
 
   digs = 0;
-  while (mp_iszero (&t) == 0) {
+  while (mp_iszero (&t) == MP_NO) {
     if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
       mp_clear (&t);
       return res;
diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c
index 173372b..eb8195f 100644
--- a/bn_mp_toradix_n.c
+++ b/bn_mp_toradix_n.c
@@ -56,7 +56,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
   }
 
   digs = 0;
-  while (mp_iszero (&t) == 0) {
+  while (mp_iszero (&t) == MP_NO) {
     if (--maxlen < 1) {
        /* no more room */
        break;
diff --git a/tommath.h b/tommath.h
index 0097b5e..16cd023 100644
--- a/tommath.h
+++ b/tommath.h
@@ -25,11 +25,11 @@
 #include <tommath_class.h>
 
 #ifndef MIN
-   #define MIN(x,y) ((x)<(y)?(x):(y))
+   #define MIN(x,y) (((x) < (y)) ? (x) : (y))
 #endif
 
 #ifndef MAX
-   #define MAX(x,y) ((x)>(y)?(x):(y))
+   #define MAX(x,y) (((x) > (y)) ? (x) : (y))
 #endif
 
 #ifdef __cplusplus
@@ -200,7 +200,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               (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
 
 /* the infamous mp_int structure */
 typedef struct  {
@@ -246,9 +246,9 @@ 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_isneg(a)  (((a)->sign) ? 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_isneg(a)  (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
 
 /* set to zero */
 void mp_zero(mp_int *a);
@@ -504,7 +504,7 @@ int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
 #endif
 
 /* table of first PRIME_SIZE primes */
-extern const mp_digit ltm_prime_tab[];
+extern const mp_digit ltm_prime_tab[PRIME_SIZE];
 
 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
 int mp_prime_is_divisible(mp_int *a, int *result);
@@ -639,14 +639,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) * 2); 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) * 8) - 4)) & 15;    \
                                                          \
     /* shift the source up to the next four bits */      \
     b <<= 4;                                             \