Commit 820f4bb06001dc955af6fe6768a249ca1a5256d9

Steffen Jaeckel 2019-10-29T21:27:01

Merge pull request #435 from libtom/simple-simplifications first batch of simplifications

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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 6c49de2..1beab5f 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -139,9 +139,9 @@ static int mtest_opponent(void)
          /* test the sign/unsigned storage functions */
 
          rr = (unsigned)mp_sbin_size(&c);
-         DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
+         DO(mp_to_sbin(&c, (uint8_t *) cmd, (size_t)rr, NULL));
          memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
-         DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr));
+         DO(mp_from_sbin(&d, (uint8_t *) cmd, (size_t)rr));
          if (mp_cmp(&c, &d) != MP_EQ) {
             printf("mp_signed_bin failure!\n");
             draw(&c);
@@ -150,9 +150,9 @@ static int mtest_opponent(void)
          }
 
          rr = (unsigned)mp_ubin_size(&c);
-         DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
+         DO(mp_to_ubin(&c, (uint8_t *) cmd, (size_t)rr, NULL));
          memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
-         DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr));
+         DO(mp_from_ubin(&d, (uint8_t *) cmd, (size_t)rr));
          if (mp_cmp_mag(&c, &d) != MP_EQ) {
             printf("mp_unsigned_bin failure!\n");
             draw(&c);
diff --git a/demo/test.c b/demo/test.c
index 7a42330..8e37f71 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -2169,7 +2169,7 @@ static int test_mp_read_write_ubin(void)
 {
    mp_int a, b, c;
    size_t size, len;
-   unsigned char *buf = NULL;
+   uint8_t *buf = NULL;
 
    DOR(mp_init_multi(&a, &b, &c, NULL));
 
@@ -2207,7 +2207,7 @@ static int test_mp_read_write_sbin(void)
 {
    mp_int a, b, c;
    size_t size, len;
-   unsigned char *buf = NULL;
+   uint8_t *buf = NULL;
 
    DOR(mp_init_multi(&a, &b, &c, NULL));
 
@@ -2246,7 +2246,7 @@ static int test_mp_pack_unpack(void)
 {
    mp_int a, b;
    size_t written, count;
-   unsigned char *buf = NULL;
+   uint8_t *buf = NULL;
 
    mp_order order = MP_LSB_FIRST;
    mp_endian endianess = MP_NATIVE_ENDIAN;
diff --git a/doc/bn.tex b/doc/bn.tex
index 1e8ac74..b8a6404 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -2444,7 +2444,7 @@ $a$.
 
 \index{mp\_to\_ubin}
 \begin{alltt}
-mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
+mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
 \end{alltt}
 This will store $a$ into the buffer \texttt{buf} of size \texttt{maxlen} in big--endian format
 storing the number of bytes written in \texttt{len}.  Fortunately this is exactly what DER (or is
@@ -2452,7 +2452,7 @@ it ASN?) requires.  It does not store the sign of the integer.
 
 \index{mp\_from\_ubin}
 \begin{alltt}
-mp_err mp_from_ubin(mp_int *a, unsigned char *b, size_t size);
+mp_err mp_from_ubin(mp_int *a, uint8_t *b, size_t size);
 \end{alltt}
 This will read in an unsigned big--endian array of bytes (octets) from \texttt{b} of length
 \texttt{size} into $a$.  The resulting big--integer $a$ will always be positive.
@@ -2462,8 +2462,8 @@ versions of the previous functions.
 \index{mp\_sbin\_size} \index{mp\_from\_sbin} \index{mp\_to\_sbin}
 \begin{alltt}
 size_t mp_sbin_size(const mp_int *a);
-mp_err mp_from_sbin(mp_int *a, const unsigned char *b, size_t size);
-mp_err mp_to_sbin(const mp_int *a, unsigned char *b, size_t maxsize, size_t *len);
+mp_err mp_from_sbin(mp_int *a, const uint8_t *b, size_t size);
+mp_err mp_to_sbin(const mp_int *a, uint8_t *b, size_t maxsize, size_t *len);
 \end{alltt}
 They operate essentially the same as the unsigned copies except they prefix the data with zero or
 non--zero byte depending on the sign. If the sign is \texttt{MP\_ZPOS} (e.g. not negative) the
diff --git a/mp_abs.c b/mp_abs.c
index 4ad1a4a..902279e 100644
--- a/mp_abs.c
+++ b/mp_abs.c
@@ -9,10 +9,9 @@
  */
 mp_err mp_abs(const mp_int *a, mp_int *b)
 {
-   mp_err     err;
-
    /* copy a to b */
    if (a != b) {
+      mp_err err;
       if ((err = mp_copy(a, b)) != MP_OKAY) {
          return err;
       }
diff --git a/mp_add.c b/mp_add.c
index c78614b..bf7a61e 100644
--- a/mp_add.c
+++ b/mp_add.c
@@ -6,33 +6,24 @@
 /* high level addition (handles signs) */
 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   mp_sign sa, sb;
-   mp_err err;
-
-   /* get sign of both inputs */
-   sa = a->sign;
-   sb = b->sign;
-
    /* handle two cases, not four */
-   if (sa == sb) {
+   if (a->sign == b->sign) {
       /* both positive or both negative */
       /* add their magnitudes, copy the sign */
-      c->sign = sa;
-      err = s_mp_add(a, b, c);
-   } else {
-      /* one positive, the other negative */
-      /* subtract the one with the greater magnitude from */
-      /* the one of the lesser magnitude.  The result gets */
-      /* the sign of the one with the greater magnitude. */
-      if (mp_cmp_mag(a, b) == MP_LT) {
-         c->sign = sb;
-         err = s_mp_sub(b, a, c);
-      } else {
-         c->sign = sa;
-         err = s_mp_sub(a, b, c);
-      }
+      c->sign = a->sign;
+      return s_mp_add(a, b, c);
    }
-   return err;
+
+   /* one positive, the other negative */
+   /* subtract the one with the greater magnitude from */
+   /* the one of the lesser magnitude. The result gets */
+   /* the sign of the one with the greater magnitude. */
+   if (mp_cmp_mag(a, b) == MP_LT) {
+      MP_EXCH(const mp_int *, a, b);
+   }
+
+   c->sign = a->sign;
+   return s_mp_sub(a, b, c);
 }
 
 #endif
diff --git a/mp_clear_multi.c b/mp_clear_multi.c
index 74406c7..9c7aed8 100644
--- a/mp_clear_multi.c
+++ b/mp_clear_multi.c
@@ -7,12 +7,11 @@
 
 void mp_clear_multi(mp_int *mp, ...)
 {
-   mp_int *next_mp = mp;
    va_list args;
    va_start(args, mp);
-   while (next_mp != NULL) {
-      mp_clear(next_mp);
-      next_mp = va_arg(args, mp_int *);
+   while (mp != NULL) {
+      mp_clear(mp);
+      mp = va_arg(args, mp_int *);
    }
    va_end(args);
 }
diff --git a/mp_cmp.c b/mp_cmp.c
index a9bd910..b9c4592 100644
--- a/mp_cmp.c
+++ b/mp_cmp.c
@@ -8,19 +8,14 @@ mp_ord mp_cmp(const mp_int *a, const mp_int *b)
 {
    /* compare based on sign */
    if (a->sign != b->sign) {
-      if (a->sign == MP_NEG) {
-         return MP_LT;
-      } else {
-         return MP_GT;
-      }
+      return a->sign == MP_NEG ? MP_LT : MP_GT;
    }
 
-   /* compare digits */
+   /* if negative compare opposite direction */
    if (a->sign == MP_NEG) {
-      /* if negative compare opposite direction */
-      return mp_cmp_mag(b, a);
-   } else {
-      return mp_cmp_mag(a, b);
+      MP_EXCH(const mp_int *, a, b);
    }
+
+   return mp_cmp_mag(a, b);
 }
 #endif
diff --git a/mp_cmp_d.c b/mp_cmp_d.c
index 03d8e2c..0d98e05 100644
--- a/mp_cmp_d.c
+++ b/mp_cmp_d.c
@@ -17,12 +17,10 @@ mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
    }
 
    /* compare the only digit of a to b */
-   if (a->dp[0] > b) {
-      return MP_GT;
-   } else if (a->dp[0] < b) {
-      return MP_LT;
-   } else {
-      return MP_EQ;
+   if (a->dp[0] != b) {
+      return a->dp[0] > b ? MP_GT : MP_LT;
    }
+
+   return MP_EQ;
 }
 #endif
diff --git a/mp_cmp_mag.c b/mp_cmp_mag.c
index b3a7b04..e5e502b 100644
--- a/mp_cmp_mag.c
+++ b/mp_cmp_mag.c
@@ -6,34 +6,20 @@
 /* compare maginitude of two ints (unsigned) */
 mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
 {
-   int     n;
-   const mp_digit *tmpa, *tmpb;
+   int n;
 
    /* compare based on # of non-zero digits */
-   if (a->used > b->used) {
-      return MP_GT;
+   if (a->used != b->used) {
+      return a->used > b->used ? MP_GT : MP_LT;
    }
 
-   if (a->used < b->used) {
-      return MP_LT;
-   }
-
-   /* alias for a */
-   tmpa = a->dp + (a->used - 1);
-
-   /* alias for b */
-   tmpb = b->dp + (a->used - 1);
-
    /* compare based on digits  */
-   for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
-      if (*tmpa > *tmpb) {
-         return MP_GT;
-      }
-
-      if (*tmpa < *tmpb) {
-         return MP_LT;
+   for (n = a->used; n --> 0;) {
+      if (a->dp[n] != b->dp[n]) {
+         return a->dp[n] > b->dp[n] ? MP_GT : MP_LT;
       }
    }
+
    return MP_EQ;
 }
 #endif
diff --git a/mp_cnt_lsb.c b/mp_cnt_lsb.c
index 7ae8bc1..8519ad1 100644
--- a/mp_cnt_lsb.c
+++ b/mp_cnt_lsb.c
@@ -3,7 +3,7 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-static const int lnz[16] = {
+static const char lnz[16] = {
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
 };
 
@@ -11,7 +11,7 @@ static const int lnz[16] = {
 int mp_cnt_lsb(const mp_int *a)
 {
    int x;
-   mp_digit q, qq;
+   mp_digit q;
 
    /* easy out */
    if (mp_iszero(a)) {
@@ -25,11 +25,12 @@ int mp_cnt_lsb(const mp_int *a)
 
    /* now scan this digit until a 1 is found */
    if ((q & 1u) == 0u) {
+      mp_digit p;
       do {
-         qq  = q & 15u;
-         x  += lnz[qq];
+         p = q & 15u;
+         x += lnz[p];
          q >>= 4;
-      } while (qq == 0u);
+      } while (p == 0u);
    }
    return x;
 }
diff --git a/mp_copy.c b/mp_copy.c
index a7ac34a..cf4d5e0 100644
--- a/mp_copy.c
+++ b/mp_copy.c
@@ -7,8 +7,6 @@
 mp_err mp_copy(const mp_int *a, mp_int *b)
 {
    int n;
-   mp_digit *tmpa, *tmpb;
-   mp_err err;
 
    /* if dst == src do nothing */
    if (a == b) {
@@ -17,27 +15,21 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
 
    /* grow dest */
    if (b->alloc < a->used) {
+      mp_err err;
       if ((err = mp_grow(b, a->used)) != MP_OKAY) {
          return err;
       }
    }
 
    /* zero b and copy the parameters over */
-   /* pointer aliases */
-
-   /* source */
-   tmpa = a->dp;
-
-   /* destination */
-   tmpb = b->dp;
 
    /* copy all the digits */
    for (n = 0; n < a->used; n++) {
-      *tmpb++ = *tmpa++;
+      b->dp[n] = a->dp[n];
    }
 
    /* clear high digits */
-   MP_ZERO_DIGITS(tmpb, b->used - n);
+   MP_ZERO_DIGITS(b->dp + a->used, b->used - a->used);
 
    /* copy used count and sign */
    b->used = a->used;
diff --git a/mp_div.c b/mp_div.c
index 23a2acf..05b96dd 100644
--- a/mp_div.c
+++ b/mp_div.c
@@ -15,14 +15,14 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
    /* if a < b then q = 0, r = a */
    if (mp_cmp_mag(a, b) == MP_LT) {
       if (d != NULL) {
-         err = mp_copy(a, d);
-      } else {
-         err = MP_OKAY;
+         if ((err = mp_copy(a, d)) != MP_OKAY) {
+            return err;
+         }
       }
       if (c != NULL) {
          mp_zero(c);
       }
-      return err;
+      return MP_OKAY;
    }
 
    if (MP_HAS(S_MP_DIV_RECURSIVE)
@@ -31,11 +31,12 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
       err = s_mp_div_recursive(a, b, c, d);
    } else if (MP_HAS(S_MP_DIV_SCHOOL)) {
       err = s_mp_div_school(a, b, c, d);
-   } else {
+   } else if (MP_HAS(S_MP_DIV_SMALL)) {
       err = s_mp_div_small(a, b, c, d);
+   } else {
+      err = MP_VAL;
    }
 
    return err;
 }
 #endif
-
diff --git a/mp_div_3.c b/mp_div_3.c
index 5789b2d..c26692c 100644
--- a/mp_div_3.c
+++ b/mp_div_3.c
@@ -7,7 +7,7 @@
 mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
 {
    mp_int   q;
-   mp_word  w, t;
+   mp_word  w;
    mp_digit b;
    mp_err   err;
    int      ix;
@@ -22,7 +22,8 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
    q.used = a->used;
    q.sign = a->sign;
    w = 0;
-   for (ix = a->used - 1; ix >= 0; ix--) {
+   for (ix = a->used; ix --> 0;) {
+      mp_word t;
       w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix];
 
       if (w >= 3u) {
@@ -57,7 +58,7 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
    }
    mp_clear(&q);
 
-   return err;
+   return MP_OKAY;
 }
 
 #endif
diff --git a/mp_exch.c b/mp_exch.c
index 7bc4ee7..50b97d9 100644
--- a/mp_exch.c
+++ b/mp_exch.c
@@ -8,10 +8,6 @@
  */
 void mp_exch(mp_int *a, mp_int *b)
 {
-   mp_int  t;
-
-   t  = *a;
-   *a = *b;
-   *b = t;
+   MP_EXCH(mp_int, *a, *b);
 }
 #endif
diff --git a/mp_exptmod.c b/mp_exptmod.c
index e643ded..b917c0b 100644
--- a/mp_exptmod.c
+++ b/mp_exptmod.c
@@ -64,13 +64,15 @@ LBL_ERR:
    /* if the modulus is odd or dr != 0 use the montgomery method */
    if (MP_HAS(S_MP_EXPTMOD_FAST) && (mp_isodd(P) || (dr != 0))) {
       return s_mp_exptmod_fast(G, X, P, Y, dr);
-   } else if (MP_HAS(S_MP_EXPTMOD)) {
-      /* otherwise use the generic Barrett reduction technique */
+   }
+
+   /* otherwise use the generic Barrett reduction technique */
+   if (MP_HAS(S_MP_EXPTMOD)) {
       return s_mp_exptmod(G, X, P, Y, 0);
-   } else {
-      /* no exptmod for evens */
-      return MP_VAL;
    }
+
+   /* no exptmod for evens */
+   return MP_VAL;
 }
 
 #endif
diff --git a/mp_exteuclid.c b/mp_exteuclid.c
index eb8ad37..0d0bfd3 100644
--- a/mp_exteuclid.c
+++ b/mp_exteuclid.c
@@ -65,7 +65,6 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp
       mp_exch(U3, &u3);
    }
 
-   err = MP_OKAY;
 LBL_ERR:
    mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
    return err;
diff --git a/mp_fread.c b/mp_fread.c
index 767e5a3..005c62a 100644
--- a/mp_fread.c
+++ b/mp_fread.c
@@ -32,7 +32,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
    mp_zero(a);
 
    do {
-      int y;
+      uint8_t y;
       unsigned pos;
       ch = (radix <= 36) ? MP_TOUPPER(ch) : ch;
       pos = (unsigned)(ch - (int)'(');
@@ -40,7 +40,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
          break;
       }
 
-      y = (int)s_mp_rmap_reverse[pos];
+      y = s_mp_rmap_reverse[pos];
 
       if ((y == 0xff) || (y >= radix)) {
          break;
@@ -50,7 +50,7 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
       if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
          return err;
       }
-      if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
+      if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
          return err;
       }
    } while ((ch = fgetc(stream)) != EOF);
diff --git a/mp_from_sbin.c b/mp_from_sbin.c
index 4335d88..3ff7d5d 100644
--- a/mp_from_sbin.c
+++ b/mp_from_sbin.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* read signed bin, big endian, first byte is 0==positive or 1==negative */
-mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size)
+mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size)
 {
    mp_err err;
 
@@ -14,11 +14,7 @@ mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size)
    }
 
    /* first byte is 0 for positive, non-zero for negative */
-   if (buf[0] == (unsigned char)0) {
-      a->sign = MP_ZPOS;
-   } else {
-      a->sign = MP_NEG;
-   }
+   a->sign = (buf[0] == (uint8_t)0) ? MP_ZPOS : MP_NEG;
 
    return MP_OKAY;
 }
diff --git a/mp_from_ubin.c b/mp_from_ubin.c
index 315ff08..ae79be3 100644
--- a/mp_from_ubin.c
+++ b/mp_from_ubin.c
@@ -3,8 +3,8 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* reads a unsigned char array, assumes the msb is stored first [big endian] */
-mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size)
+/* reads a uint8_t array, assumes the msb is stored first [big endian] */
+mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size)
 {
    mp_err err;
 
diff --git a/mp_fwrite.c b/mp_fwrite.c
index be78f7f..42d7287 100644
--- a/mp_fwrite.c
+++ b/mp_fwrite.c
@@ -8,31 +8,24 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
 {
    char *buf;
    mp_err err;
-   size_t len, written;
+   size_t size, written;
 
-   /* TODO: this function is not in this PR */
-   if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
+   if ((err = mp_radix_size(a, radix, &size)) != MP_OKAY) {
       return err;
    }
 
-   buf = (char *) MP_MALLOC(len);
+   buf = (char *) MP_MALLOC(size);
    if (buf == NULL) {
       return MP_MEM;
    }
 
-   if ((err = mp_to_radix(a, buf, len, &written, radix)) != MP_OKAY) {
-      goto LBL_ERR;
+   if ((err = mp_to_radix(a, buf, size, &written, radix)) == MP_OKAY) {
+      if (fwrite(buf, written, 1uL, stream) != 1uL) {
+         err = MP_ERR;
+      }
    }
 
-   if (fwrite(buf, written, 1uL, stream) != 1uL) {
-      err = MP_ERR;
-      goto LBL_ERR;
-   }
-   err = MP_OKAY;
-
-
-LBL_ERR:
-   MP_FREE_BUFFER(buf, len);
+   MP_FREE_BUFFER(buf, size);
    return err;
 }
 #endif
diff --git a/mp_grow.c b/mp_grow.c
index 3354e59..25be5ed 100644
--- a/mp_grow.c
+++ b/mp_grow.c
@@ -6,9 +6,6 @@
 /* grow as required */
 mp_err mp_grow(mp_int *a, int size)
 {
-   int     i;
-   mp_digit *tmp;
-
    /* if the alloc size is smaller alloc more ram */
    if (a->alloc < size) {
       /* reallocate the array a->dp
@@ -17,21 +14,20 @@ mp_err mp_grow(mp_int *a, int size)
        * in case the operation failed we don't want
        * to overwrite the dp member of a.
        */
-      tmp = (mp_digit *) MP_REALLOC(a->dp,
-                                    (size_t)a->alloc * sizeof(mp_digit),
-                                    (size_t)size * sizeof(mp_digit));
-      if (tmp == NULL) {
+      mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp,
+                                             (size_t)a->alloc * sizeof(mp_digit),
+                                             (size_t)size * sizeof(mp_digit));
+      if (dp == NULL) {
          /* reallocation failed but "a" is still valid [can be freed] */
          return MP_MEM;
       }
 
       /* reallocation succeeded so set a->dp */
-      a->dp = tmp;
+      a->dp = dp;
 
       /* zero excess digits */
-      i        = a->alloc;
+      MP_ZERO_DIGITS(a->dp + a->alloc, size - a->alloc);
       a->alloc = size;
-      MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
    }
    return MP_OKAY;
 }
diff --git a/mp_is_square.c b/mp_is_square.c
index f92ecbf..47f8300 100644
--- a/mp_is_square.c
+++ b/mp_is_square.c
@@ -28,10 +28,10 @@ static const char rem_105[105] = {
 /* Store non-zero to ret if arg is square, and zero if not */
 mp_err mp_is_square(const mp_int *arg, bool *ret)
 {
-   mp_err        err;
-   mp_digit      c;
-   mp_int        t;
-   unsigned long r;
+   mp_err   err;
+   mp_digit c;
+   mp_int   t;
+   uint32_t r;
 
    /* Default to Non-square :) */
    *ret = false;
diff --git a/mp_kronecker.c b/mp_kronecker.c
index 0ac6338..b106f77 100644
--- a/mp_kronecker.c
+++ b/mp_kronecker.c
@@ -23,7 +23,7 @@ mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c)
    mp_err err;
    int v, k;
 
-   static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1};
+   static const char table[] = {0, 1, 0, -1, 0, -1, 0, 1};
 
    if (mp_iszero(p)) {
       if ((a->used == 1) && (a->dp[0] == 1u)) {
diff --git a/mp_lshd.c b/mp_lshd.c
index b0a8454..6c14402 100644
--- a/mp_lshd.c
+++ b/mp_lshd.c
@@ -7,8 +7,6 @@
 mp_err mp_lshd(mp_int *a, int b)
 {
    int x;
-   mp_err err;
-   mp_digit *top, *bottom;
 
    /* if its less than zero return */
    if (b <= 0) {
@@ -21,6 +19,7 @@ mp_err mp_lshd(mp_int *a, int b)
 
    /* grow to fit the new digits */
    if (a->alloc < (a->used + b)) {
+      mp_err err;
       if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
          return err;
       }
@@ -29,18 +28,12 @@ mp_err mp_lshd(mp_int *a, int b)
    /* increment the used by the shift amount then copy upwards */
    a->used += b;
 
-   /* top */
-   top = a->dp + a->used - 1;
-
-   /* base */
-   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
     * the bottom to the top.  see mp_rshd.c for more info.
     */
-   for (x = a->used - 1; x >= b; x--) {
-      *top-- = *bottom--;
+   for (x = a->used; x --> b;) {
+      a->dp[x] = a->dp[x - b];
    }
 
    /* zero the lower digits */
diff --git a/mp_mod_2d.c b/mp_mod_2d.c
index 651c79a..a94a314 100644
--- a/mp_mod_2d.c
+++ b/mp_mod_2d.c
@@ -9,8 +9,11 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
    int x;
    mp_err err;
 
-   /* if b is <= 0 then zero the int */
-   if (b <= 0) {
+   if (b < 0) {
+      return MP_VAL;
+   }
+
+   if (b == 0) {
       mp_zero(c);
       return MP_OKAY;
    }
@@ -20,7 +23,6 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
       return mp_copy(a, c);
    }
 
-   /* copy */
    if ((err = mp_copy(a, c)) != MP_OKAY) {
       return err;
    }
diff --git a/mp_montgomery_calc_normalization.c b/mp_montgomery_calc_normalization.c
index 0d0d5c4..cc07799 100644
--- a/mp_montgomery_calc_normalization.c
+++ b/mp_montgomery_calc_normalization.c
@@ -26,7 +26,6 @@ mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
       bits = 1;
    }
 
-
    /* now compute C = A * B mod b */
    for (x = bits - 1; x < (int)MP_DIGIT_BIT; x++) {
       if ((err = mp_mul_2(a, a)) != MP_OKAY) {
diff --git a/mp_neg.c b/mp_neg.c
index 2fc1854..f54ef3e 100644
--- a/mp_neg.c
+++ b/mp_neg.c
@@ -6,18 +6,14 @@
 /* b = -a */
 mp_err mp_neg(const mp_int *a, mp_int *b)
 {
-   mp_err err;
    if (a != b) {
+      mp_err err;
       if ((err = mp_copy(a, b)) != MP_OKAY) {
          return err;
       }
    }
 
-   if (!mp_iszero(b)) {
-      b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
-   } else {
-      b->sign = MP_ZPOS;
-   }
+   b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG;
 
    return MP_OKAY;
 }
diff --git a/mp_pack.c b/mp_pack.c
index ec0f62f..447f1fd 100644
--- a/mp_pack.c
+++ b/mp_pack.c
@@ -11,7 +11,7 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size
 {
    mp_err err;
    size_t odd_nails, nail_bytes, i, j, count;
-   unsigned char odd_nail_mask;
+   uint8_t odd_nail_mask;
 
    mp_int t;
 
@@ -32,22 +32,22 @@ mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size
    odd_nails = (nails % 8u);
    odd_nail_mask = 0xff;
    for (i = 0u; i < odd_nails; ++i) {
-      odd_nail_mask ^= (unsigned char)(1u << (7u - i));
+      odd_nail_mask ^= (uint8_t)(1u << (7u - i));
    }
    nail_bytes = nails / 8u;
 
    for (i = 0u; i < count; ++i) {
       for (j = 0u; j < size; ++j) {
-         unsigned char *byte = (unsigned char *)rop +
-                               (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) +
-                               ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j));
+         uint8_t *byte = (uint8_t *)rop +
+                         (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) +
+                         ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j));
 
          if (j >= (size - nail_bytes)) {
             *byte = 0;
             continue;
          }
 
-         *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
+         *byte = (uint8_t)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
 
          if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
             goto LBL_ERR;
diff --git a/mp_prime_rabin_miller_trials.c b/mp_prime_rabin_miller_trials.c
index 1728142..9f66f8d 100644
--- a/mp_prime_rabin_miller_trials.c
+++ b/mp_prime_rabin_miller_trials.c
@@ -36,7 +36,8 @@ int mp_prime_rabin_miller_trials(int size)
    for (x = 0; x < (int)(sizeof(sizes)/(sizeof(sizes[0]))); x++) {
       if (sizes[x].k == size) {
          return sizes[x].t;
-      } else if (sizes[x].k > size) {
+      }
+      if (sizes[x].k > size) {
          return (x == 0) ? sizes[0].t : sizes[x - 1].t;
       }
    }
diff --git a/mp_prime_rand.c b/mp_prime_rand.c
index ff6df9c..8476b4f 100644
--- a/mp_prime_rand.c
+++ b/mp_prime_rand.c
@@ -20,7 +20,7 @@
 /* This is possibly the mother of all prime generation functions, muahahahahaha! */
 mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
 {
-   unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
+   uint8_t *tmp, maskAND, maskOR_msb, maskOR_lsb;
    int bsize, maskOR_msb_offset;
    bool res;
    mp_err err;
@@ -39,19 +39,19 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
    bsize = (size>>3) + ((size&7)?1:0);
 
    /* we need a buffer of bsize bytes */
-   tmp = (unsigned char *) MP_MALLOC((size_t)bsize);
+   tmp = (uint8_t *) MP_MALLOC((size_t)bsize);
    if (tmp == NULL) {
       return MP_MEM;
    }
 
    /* calc the maskAND value for the MSbyte*/
-   maskAND = ((size&7) == 0) ? 0xFFu : (unsigned char)(0xFFu >> (8 - (size & 7)));
+   maskAND = ((size&7) == 0) ? 0xFFu : (uint8_t)(0xFFu >> (8 - (size & 7)));
 
    /* calc the maskOR_msb */
    maskOR_msb        = 0;
    maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
    if ((flags & MP_PRIME_2MSB_ON) != 0) {
-      maskOR_msb       |= (unsigned char)(0x80 >> ((9 - size) & 7));
+      maskOR_msb       |= (uint8_t)(0x80 >> ((9 - size) & 7));
    }
 
    /* get the maskOR_lsb */
@@ -68,7 +68,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
 
       /* work over the MSbyte */
       tmp[0]    &= maskAND;
-      tmp[0]    |= (unsigned char)(1 << ((size - 1) & 7));
+      tmp[0]    |= (uint8_t)(1 << ((size - 1) & 7));
 
       /* mix in the maskORs */
       tmp[maskOR_msb_offset]   |= maskOR_msb;
diff --git a/mp_read_radix.c b/mp_read_radix.c
index d4a3d1e..df8059a 100644
--- a/mp_read_radix.c
+++ b/mp_read_radix.c
@@ -31,13 +31,13 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
        * this allows numbers like 1AB and 1ab to represent the same  value
        * [e.g. in hex]
        */
-      int y;
+      uint8_t y;
       char ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str;
       unsigned pos = (unsigned)(ch - '(');
       if (MP_RMAP_REVERSE_SIZE < pos) {
          break;
       }
-      y = (int)s_mp_rmap_reverse[pos];
+      y = s_mp_rmap_reverse[pos];
 
       /* if the char was found in the map
        * and is less than the given radix add it
@@ -49,14 +49,14 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
       if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
          return err;
       }
-      if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
+      if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
          return err;
       }
       ++str;
    }
 
    /* if an illegal character was found, fail. */
-   if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) {
+   if ((*str != '\0') && (*str != '\r') && (*str != '\n')) {
       return MP_VAL;
    }
 
diff --git a/mp_reduce.c b/mp_reduce.c
index 1b4435c..5226fe7 100644
--- a/mp_reduce.c
+++ b/mp_reduce.c
@@ -24,19 +24,19 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
    /* according to HAC this optimization is ok */
    if ((mp_digit)um > ((mp_digit)1 << (MP_DIGIT_BIT - 1))) {
       if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) {
-         goto CLEANUP;
+         goto LBL_ERR;
       }
    } else if (MP_HAS(S_MP_MUL_HIGH_DIGS)) {
       if ((err = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
-         goto CLEANUP;
+         goto LBL_ERR;
       }
    } else if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)) {
       if ((err = s_mp_mul_high_digs_fast(&q, mu, &q, um)) != MP_OKAY) {
-         goto CLEANUP;
+         goto LBL_ERR;
       }
    } else {
       err = MP_VAL;
-      goto CLEANUP;
+      goto LBL_ERR;
    }
 
    /* q3 = q2 / b**(k+1) */
@@ -44,38 +44,38 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
 
    /* x = x mod b**(k+1), quick (no division) */
    if ((err = mp_mod_2d(x, MP_DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
-      goto CLEANUP;
+      goto LBL_ERR;
    }
 
    /* q = q * m mod b**(k+1), quick (no division) */
    if ((err = s_mp_mul_digs(&q, m, &q, um + 1)) != MP_OKAY) {
-      goto CLEANUP;
+      goto LBL_ERR;
    }
 
    /* x = x - q */
    if ((err = mp_sub(x, &q, x)) != MP_OKAY) {
-      goto CLEANUP;
+      goto LBL_ERR;
    }
 
    /* If x < 0, add b**(k+1) to it */
    if (mp_cmp_d(x, 0uL) == MP_LT) {
       mp_set(&q, 1uL);
       if ((err = mp_lshd(&q, um + 1)) != MP_OKAY) {
-         goto CLEANUP;
+         goto LBL_ERR;
       }
       if ((err = mp_add(x, &q, x)) != MP_OKAY) {
-         goto CLEANUP;
+         goto LBL_ERR;
       }
    }
 
    /* Back off if it's too big */
    while (mp_cmp(x, m) != MP_LT) {
       if ((err = s_mp_sub(x, m, x)) != MP_OKAY) {
-         goto CLEANUP;
+         goto LBL_ERR;
       }
    }
 
-CLEANUP:
+LBL_ERR:
    mp_clear(&q);
 
    return err;
diff --git a/mp_reduce_is_2k.c b/mp_reduce_is_2k.c
index 618ab54..a5798e6 100644
--- a/mp_reduce_is_2k.c
+++ b/mp_reduce_is_2k.c
@@ -6,17 +6,13 @@
 /* determines if mp_reduce_2k can be used */
 bool mp_reduce_is_2k(const mp_int *a)
 {
-   int ix, iy, iw;
-   mp_digit iz;
-
    if (a->used == 0) {
       return false;
    } else if (a->used == 1) {
       return true;
    } else if (a->used > 1) {
-      iy = mp_count_bits(a);
-      iz = 1;
-      iw = 1;
+      int ix, iy = mp_count_bits(a), iw = 1;
+      mp_digit iz = 1;
 
       /* Test every bit from the second digit up, must be 1 */
       for (ix = MP_DIGIT_BIT; ix < iy; ix++) {
diff --git a/mp_reduce_is_2k_l.c b/mp_reduce_is_2k_l.c
index 30fc10d..dca2d7e 100644
--- a/mp_reduce_is_2k_l.c
+++ b/mp_reduce_is_2k_l.c
@@ -6,14 +6,13 @@
 /* determines if reduce_2k_l can be used */
 bool mp_reduce_is_2k_l(const mp_int *a)
 {
-   int ix, iy;
-
    if (a->used == 0) {
       return false;
    } else if (a->used == 1) {
       return true;
    } else if (a->used > 1) {
       /* if more than half of the digits are -1 we're sold */
+      int ix, iy;
       for (iy = ix = 0; ix < a->used; ix++) {
          if (a->dp[ix] == MP_DIGIT_MAX) {
             ++iy;
diff --git a/mp_rshd.c b/mp_rshd.c
index 2eabb12..d798907 100644
--- a/mp_rshd.c
+++ b/mp_rshd.c
@@ -6,8 +6,7 @@
 /* shift right a certain amount of digits */
 void mp_rshd(mp_int *a, int b)
 {
-   int     x;
-   mp_digit *bottom, *top;
+   int x;
 
    /* if b <= 0 then ignore it */
    if (b <= 0) {
@@ -20,15 +19,8 @@ void mp_rshd(mp_int *a, int b)
       return;
    }
 
-   /* shift the digits down */
-
-   /* bottom */
-   bottom = a->dp;
-
-   /* top [offset into digits] */
-   top = a->dp + b;
-
-   /* this is implemented as a sliding window where
+   /* shift the digits down.
+    * this is implemented as a sliding window where
     * the window is b-digits long and digits from
     * the top of the window are copied to the bottom
     *
@@ -39,11 +31,11 @@ void mp_rshd(mp_int *a, int b)
                  \-------------------/      ---->
     */
    for (x = 0; x < (a->used - b); x++) {
-      *bottom++ = *top++;
+      a->dp[x] = a->dp[x + b];
    }
 
    /* zero the top digits */
-   MP_ZERO_DIGITS(bottom, a->used - x);
+   MP_ZERO_DIGITS(a->dp + a->used - b, b);
 
    /* remove excess digits */
    a->used -= b;
diff --git a/mp_set.c b/mp_set.c
index 0777f09..3ee5f81 100644
--- a/mp_set.c
+++ b/mp_set.c
@@ -6,9 +6,10 @@
 /* set to a digit */
 void mp_set(mp_int *a, mp_digit b)
 {
+   int oldused = a->used;
    a->dp[0] = b & MP_MASK;
    a->sign  = MP_ZPOS;
    a->used  = (a->dp[0] != 0u) ? 1 : 0;
-   MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);
+   MP_ZERO_DIGITS(a->dp + a->used, oldused - a->used);
 }
 #endif
diff --git a/mp_shrink.c b/mp_shrink.c
index 6c3c95b..e5814cb 100644
--- a/mp_shrink.c
+++ b/mp_shrink.c
@@ -6,15 +6,15 @@
 /* shrink a bignum */
 mp_err mp_shrink(mp_int *a)
 {
-   mp_digit *tmp;
    int alloc = MP_MAX(MP_MIN_PREC, a->used);
    if (a->alloc != alloc) {
-      if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
-                                         (size_t)a->alloc * sizeof(mp_digit),
-                                         (size_t)alloc * sizeof(mp_digit))) == NULL) {
+      mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp,
+                                             (size_t)a->alloc * sizeof(mp_digit),
+                                             (size_t)alloc * sizeof(mp_digit));
+      if (dp == NULL) {
          return MP_MEM;
       }
-      a->dp    = tmp;
+      a->dp    = dp;
       a->alloc = alloc;
    }
    return MP_OKAY;
diff --git a/mp_signed_rsh.c b/mp_signed_rsh.c
index c56dfba..ecaaa21 100644
--- a/mp_signed_rsh.c
+++ b/mp_signed_rsh.c
@@ -6,17 +6,16 @@
 /* shift right by a certain bit count with sign extension */
 mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c)
 {
-   mp_err res;
+   mp_err err;
    if (a->sign == MP_ZPOS) {
       return mp_div_2d(a, b, c, NULL);
    }
 
-   res = mp_add_d(a, 1uL, c);
-   if (res != MP_OKAY) {
-      return res;
+   if ((err = mp_add_d(a, 1uL, c)) != MP_OKAY) {
+      return err;
    }
 
-   res = mp_div_2d(c, b, c, NULL);
-   return (res == MP_OKAY) ? mp_sub_d(c, 1uL, c) : res;
+   err = mp_div_2d(c, b, c, NULL);
+   return (err == MP_OKAY) ? mp_sub_d(c, 1uL, c) : err;
 }
 #endif
diff --git a/mp_sqrt.c b/mp_sqrt.c
index b51f615..e36a81a 100644
--- a/mp_sqrt.c
+++ b/mp_sqrt.c
@@ -25,7 +25,7 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret)
    }
 
    if ((err = mp_init(&t2)) != MP_OKAY) {
-      goto E2;
+      goto LBL_ERR2;
    }
 
    /* First approx. (not very bad for large arg) */
@@ -33,33 +33,33 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret)
 
    /* t1 > 0  */
    if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
-      goto E1;
+      goto LBL_ERR1;
    }
    if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
-      goto E1;
+      goto LBL_ERR1;
    }
    if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) {
-      goto E1;
+      goto LBL_ERR1;
    }
    /* And now t1 > sqrt(arg) */
    do {
       if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
-         goto E1;
+         goto LBL_ERR1;
       }
       if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
-         goto E1;
+         goto LBL_ERR1;
       }
       if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) {
-         goto E1;
+         goto LBL_ERR1;
       }
       /* t1 >= sqrt(arg) >= t2 at this point */
    } while (mp_cmp_mag(&t1, &t2) == MP_GT);
 
    mp_exch(&t1, ret);
 
-E1:
+LBL_ERR1:
    mp_clear(&t2);
-E2:
+LBL_ERR2:
    mp_clear(&t1);
    return err;
 }
diff --git a/mp_sqrtmod_prime.c b/mp_sqrtmod_prime.c
index 96b2836..8930184 100644
--- a/mp_sqrtmod_prime.c
+++ b/mp_sqrtmod_prime.c
@@ -33,28 +33,28 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
     * compute directly: err = n^(prime+1)/4 mod prime
     * Handbook of Applied Cryptography algorithm 3.36
     */
-   if ((err = mp_mod_d(prime, 4uL, &i)) != MP_OKAY)               goto cleanup;
+   if ((err = mp_mod_d(prime, 4uL, &i)) != MP_OKAY)               goto LBL_END;
    if (i == 3u) {
-      if ((err = mp_add_d(prime, 1uL, &t1)) != MP_OKAY)           goto cleanup;
-      if ((err = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup;
-      if ((err = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup;
-      if ((err = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY)      goto cleanup;
+      if ((err = mp_add_d(prime, 1uL, &t1)) != MP_OKAY)           goto LBL_END;
+      if ((err = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto LBL_END;
+      if ((err = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto LBL_END;
+      if ((err = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY)      goto LBL_END;
       err = MP_OKAY;
-      goto cleanup;
+      goto LBL_END;
    }
 
    /* NOW: Tonelli-Shanks algorithm */
 
    /* factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S */
-   if ((err = mp_copy(prime, &Q)) != MP_OKAY)                    goto cleanup;
-   if ((err = mp_sub_d(&Q, 1uL, &Q)) != MP_OKAY)                 goto cleanup;
+   if ((err = mp_copy(prime, &Q)) != MP_OKAY)                    goto LBL_END;
+   if ((err = mp_sub_d(&Q, 1uL, &Q)) != MP_OKAY)                 goto LBL_END;
    /* Q = prime - 1 */
    mp_zero(&S);
    /* S = 0 */
    while (mp_iseven(&Q)) {
-      if ((err = mp_div_2(&Q, &Q)) != MP_OKAY)                    goto cleanup;
+      if ((err = mp_div_2(&Q, &Q)) != MP_OKAY)                    goto LBL_END;
       /* Q = Q / 2 */
-      if ((err = mp_add_d(&S, 1uL, &S)) != MP_OKAY)               goto cleanup;
+      if ((err = mp_add_d(&S, 1uL, &S)) != MP_OKAY)               goto LBL_END;
       /* S = S + 1 */
    }
 
@@ -62,55 +62,55 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    mp_set(&Z, 2uL);
    /* Z = 2 */
    for (;;) {
-      if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup;
+      if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY)     goto LBL_END;
       if (legendre == -1) break;
-      if ((err = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY)               goto cleanup;
+      if ((err = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY)               goto LBL_END;
       /* Z = Z + 1 */
    }
 
-   if ((err = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY)         goto cleanup;
+   if ((err = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY)         goto LBL_END;
    /* C = Z ^ Q mod prime */
-   if ((err = mp_add_d(&Q, 1uL, &t1)) != MP_OKAY)                goto cleanup;
-   if ((err = mp_div_2(&t1, &t1)) != MP_OKAY)                    goto cleanup;
+   if ((err = mp_add_d(&Q, 1uL, &t1)) != MP_OKAY)                goto LBL_END;
+   if ((err = mp_div_2(&t1, &t1)) != MP_OKAY)                    goto LBL_END;
    /* t1 = (Q + 1) / 2 */
-   if ((err = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY)         goto cleanup;
+   if ((err = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY)         goto LBL_END;
    /* R = n ^ ((Q + 1) / 2) mod prime */
-   if ((err = mp_exptmod(n, &Q, prime, &T)) != MP_OKAY)          goto cleanup;
+   if ((err = mp_exptmod(n, &Q, prime, &T)) != MP_OKAY)          goto LBL_END;
    /* T = n ^ Q mod prime */
-   if ((err = mp_copy(&S, &M)) != MP_OKAY)                       goto cleanup;
+   if ((err = mp_copy(&S, &M)) != MP_OKAY)                       goto LBL_END;
    /* M = S */
    mp_set(&two, 2uL);
 
    for (;;) {
-      if ((err = mp_copy(&T, &t1)) != MP_OKAY)                    goto cleanup;
+      if ((err = mp_copy(&T, &t1)) != MP_OKAY)                    goto LBL_END;
       i = 0;
       for (;;) {
          if (mp_cmp_d(&t1, 1uL) == MP_EQ) break;
-         if ((err = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup;
+         if ((err = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto LBL_END;
          i++;
       }
       if (i == 0u) {
-         if ((err = mp_copy(&R, ret)) != MP_OKAY)                  goto cleanup;
+         if ((err = mp_copy(&R, ret)) != MP_OKAY)                  goto LBL_END;
          err = MP_OKAY;
-         goto cleanup;
+         goto LBL_END;
       }
-      if ((err = mp_sub_d(&M, i, &t1)) != MP_OKAY)                goto cleanup;
-      if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY)             goto cleanup;
-      if ((err = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY)   goto cleanup;
+      if ((err = mp_sub_d(&M, i, &t1)) != MP_OKAY)                goto LBL_END;
+      if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY)             goto LBL_END;
+      if ((err = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY)   goto LBL_END;
       /* t1 = 2 ^ (M - i - 1) */
-      if ((err = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY)     goto cleanup;
+      if ((err = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY)     goto LBL_END;
       /* t1 = C ^ (2 ^ (M - i - 1)) mod prime */
-      if ((err = mp_sqrmod(&t1, prime, &C)) != MP_OKAY)           goto cleanup;
+      if ((err = mp_sqrmod(&t1, prime, &C)) != MP_OKAY)           goto LBL_END;
       /* C = (t1 * t1) mod prime */
-      if ((err = mp_mulmod(&R, &t1, prime, &R)) != MP_OKAY)       goto cleanup;
+      if ((err = mp_mulmod(&R, &t1, prime, &R)) != MP_OKAY)       goto LBL_END;
       /* R = (R * t1) mod prime */
-      if ((err = mp_mulmod(&T, &C, prime, &T)) != MP_OKAY)        goto cleanup;
+      if ((err = mp_mulmod(&T, &C, prime, &T)) != MP_OKAY)        goto LBL_END;
       /* T = (T * C) mod prime */
       mp_set(&M, i);
       /* M = i */
    }
 
-cleanup:
+LBL_END:
    mp_clear_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL);
    return err;
 }
diff --git a/mp_sub.c b/mp_sub.c
index c859026..8104740 100644
--- a/mp_sub.c
+++ b/mp_sub.c
@@ -6,35 +6,31 @@
 /* high level subtraction (handles signs) */
 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   mp_sign sa = a->sign, sb = b->sign;
-   mp_err err;
-
-   if (sa != sb) {
+   if (a->sign != b->sign) {
       /* subtract a negative from a positive, OR */
       /* subtract a positive from a negative. */
       /* In either case, ADD their magnitudes, */
       /* and use the sign of the first number. */
-      c->sign = sa;
-      err = s_mp_add(a, b, c);
+      c->sign = a->sign;
+      return s_mp_add(a, b, c);
+   }
+
+   /* subtract a positive from a positive, OR */
+   /* subtract a negative from a negative. */
+   /* First, take the difference between their */
+   /* magnitudes, then... */
+   if (mp_cmp_mag(a, b) == MP_LT) {
+      /* The second has a larger magnitude */
+      /* The result has the *opposite* sign from */
+      /* the first number. */
+      c->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
+      MP_EXCH(const mp_int *, a, b);
    } else {
-      /* subtract a positive from a positive, OR */
-      /* subtract a negative from a negative. */
-      /* First, take the difference between their */
-      /* magnitudes, then... */
-      if (mp_cmp_mag(a, b) != MP_LT) {
-         /* Copy the sign from the first */
-         c->sign = sa;
-         /* The first has a larger or equal magnitude */
-         err = s_mp_sub(a, b, c);
-      } else {
-         /* The result has the *opposite* sign from */
-         /* the first number. */
-         c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
-         /* The second has a larger magnitude */
-         err = s_mp_sub(b, a, c);
-      }
+      /* The first has a larger or equal magnitude */
+      /* Copy the sign from the first */
+      c->sign = a->sign;
    }
-   return err;
+   return s_mp_sub(a, b, c);
 }
 
 #endif
diff --git a/mp_to_radix.c b/mp_to_radix.c
index 78ad989..8b7728d 100644
--- a/mp_to_radix.c
+++ b/mp_to_radix.c
@@ -4,17 +4,11 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* reverse an array, used for radix code */
-static void s_mp_reverse(unsigned char *s, size_t len)
+static void s_mp_reverse(char *s, size_t len)
 {
-   size_t   ix, iy;
-   unsigned char t;
-
-   ix = 0u;
-   iy = len - 1u;
+   size_t ix = 0, iy = len - 1u;
    while (ix < iy) {
-      t     = s[ix];
-      s[ix] = s[iy];
-      s[iy] = t;
+      MP_EXCH(char, s[ix], s[iy]);
       ++ix;
       --iy;
    }
@@ -83,7 +77,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
    /* reverse the digits of the string.  In this case _s points
     * to the first digit [exluding the sign] of the number
     */
-   s_mp_reverse((unsigned char *)_s, digs);
+   s_mp_reverse(_s, digs);
 
    /* append a NULL so the string is properly terminated */
    *str = '\0';
diff --git a/mp_to_sbin.c b/mp_to_sbin.c
index ed21adc..8225d13 100644
--- a/mp_to_sbin.c
+++ b/mp_to_sbin.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* store in signed [big endian] format */
-mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
+mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
 {
    mp_err err;
    if (maxlen == 0u) {
@@ -16,7 +16,7 @@ mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
    if (written != NULL) {
       (*written)++;
    }
-   buf[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
+   buf[0] = (a->sign == MP_ZPOS) ? (uint8_t)0 : (uint8_t)1;
    return MP_OKAY;
 }
 #endif
diff --git a/mp_to_ubin.c b/mp_to_ubin.c
index 2583503..e8643cc 100644
--- a/mp_to_ubin.c
+++ b/mp_to_ubin.c
@@ -4,7 +4,7 @@
 /* SPDX-License-Identifier: Unlicense */
 
 /* store in unsigned [big endian] format */
-mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
+mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
 {
    size_t  x, count;
    mp_err  err;
@@ -20,7 +20,7 @@ mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
    }
 
    for (x = count; x --> 0u;) {
-      buf[x] = (unsigned char)(t.dp[0] & 255u);
+      buf[x] = (uint8_t)(t.dp[0] & 255u);
       if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
          goto LBL_ERR;
       }
diff --git a/mp_unpack.c b/mp_unpack.c
index 5305b43..f0127fa 100644
--- a/mp_unpack.c
+++ b/mp_unpack.c
@@ -11,7 +11,7 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
 {
    mp_err err;
    size_t odd_nails, nail_bytes, i, j;
-   unsigned char odd_nail_mask;
+   uint8_t odd_nail_mask;
 
    mp_zero(rop);
 
@@ -22,15 +22,15 @@ mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
    odd_nails = (nails % 8u);
    odd_nail_mask = 0xff;
    for (i = 0; i < odd_nails; ++i) {
-      odd_nail_mask ^= (unsigned char)(1u << (7u - i));
+      odd_nail_mask ^= (uint8_t)(1u << (7u - i));
    }
    nail_bytes = nails / 8u;
 
    for (i = 0; i < count; ++i) {
       for (j = 0; j < (size - nail_bytes); ++j) {
-         unsigned char byte = *((const unsigned char *)op +
-                                (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
-                                ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
+         uint8_t byte = *((const uint8_t *)op +
+                          (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
+                          ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
 
          if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
             return err;
diff --git a/mp_zero.c b/mp_zero.c
index 0b79f50..b7dddd2 100644
--- a/mp_zero.c
+++ b/mp_zero.c
@@ -7,7 +7,7 @@
 void mp_zero(mp_int *a)
 {
    a->sign = MP_ZPOS;
+   MP_ZERO_DIGITS(a->dp, a->used);
    a->used = 0;
-   MP_ZERO_DIGITS(a->dp, a->alloc);
 }
 #endif
diff --git a/s_mp_div_recursive.c b/s_mp_div_recursive.c
index d641123..7007aef 100644
--- a/s_mp_div_recursive.c
+++ b/s_mp_div_recursive.c
@@ -17,10 +17,9 @@
 static mp_err s_mp_recursion(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r)
 {
    mp_err err;
-   int m, k;
    mp_int A1, A2, B1, B0, Q1, Q0, R1, R0, t;
+   int m = a->used - b->used, k = m/2;
 
-   m = a->used - b->used;
    if (m < MP_KARATSUBA_MUL_CUTOFF) {
       return s_mp_div_school(a, b, q, r);
    }
@@ -29,9 +28,6 @@ static mp_err s_mp_recursion(const mp_int *a, const mp_int *b, mp_int *q, mp_int
       goto LBL_ERR;
    }
 
-   /* k = floor(m/2) */
-   k = m/2;
-
    /* B1 = b / beta^k, B0 = b % beta^k*/
    if ((err = mp_div_2d(b, k * MP_DIGIT_BIT, &B1, &B0)) != MP_OKAY)        goto LBL_ERR;
 
diff --git a/s_mp_div_school.c b/s_mp_div_school.c
index 6ff427a..cf34cc9 100644
--- a/s_mp_div_school.c
+++ b/s_mp_div_school.c
@@ -140,8 +140,6 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
       mp_exch(&x, d);
    }
 
-   err = MP_OKAY;
-
 LBL_Y:
    mp_clear(&y);
 LBL_X:
diff --git a/tommath.h b/tommath.h
index 8742126..86b19d3 100644
--- a/tommath.h
+++ b/tommath.h
@@ -570,12 +570,12 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
 int mp_count_bits(const mp_int *a) MP_WUR;
 
 size_t mp_ubin_size(const mp_int *a) MP_WUR;
-mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
-mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
+mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size) MP_WUR;
+mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) MP_WUR;
 
 size_t mp_sbin_size(const mp_int *a) MP_WUR;
-mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
-mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
+mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size) MP_WUR;
+mp_err mp_to_sbin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written) MP_WUR;
 
 mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
 mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;
diff --git a/tommath_private.h b/tommath_private.h
index 0f5ac93..31f1ea5 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -148,6 +148,8 @@ extern void MP_FREE(void *mem, size_t size);
 
 #define MP_TOUPPER(c) ((((c) >= 'a') && ((c) <= 'z')) ? (((c) + 'A') - 'a') : (c))
 
+#define MP_EXCH(t, a, b) do { t _c = a; a = b; b = _c; } while (0)
+
 /* Static assertion */
 #define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];
 
@@ -267,9 +269,9 @@ extern MP_PRIVATE const mp_digit s_mp_prime_tab[];
 #define MP_GET_MAG(name, type)                                                         \
     type name(const mp_int* a)                                                         \
     {                                                                                  \
-        unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
+        int i = MP_MIN(a->used, (int)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
         type res = 0u;                                                                 \
-        while (i --> 0u) {                                                             \
+        while (i --> 0) {                                                              \
             res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);       \
             res |= (type)a->dp[i];                                                     \
             if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; }                       \