Commit 83b74bac6e3c36400fe2a85f56a1d7eb7c1c9ed1

Steffen Jaeckel 2019-11-14T11:08:33

Merge pull request #446 from libtom/suffix-renamings2 Alternative 2: Suffix renamings (_n suffix)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
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
diff --git a/demo/test.c b/demo/test.c
index 3e432cf..f153509 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -729,7 +729,7 @@ static int test_mp_sqrt(void)
          printf("\nmp_sqrt() error!");
          goto LBL_ERR;
       }
-      DO(mp_root_u32(&a, 2u, &c));
+      DO(mp_root_n(&a, 2u, &c));
       if (mp_cmp_mag(&b, &c) != MP_EQ) {
          printf("mp_sqrt() bad result!\n");
          goto LBL_ERR;
@@ -1396,10 +1396,10 @@ LBL_ERR:
 /* stripped down version of mp_radix_size. The faster version can be off by up t
 o +3  */
 /* TODO: This function should be removed, replaced by mp_radix_size, mp_radix_size_overestimate in 2.0 */
-static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
+static mp_err s_rs(const mp_int *a, int radix, int *size)
 {
    mp_err res;
-   uint32_t digs = 0u;
+   int digs = 0u;
    mp_int  t;
    mp_digit d;
    *size = 0u;
@@ -1408,7 +1408,7 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
       return MP_OKAY;
    }
    if (radix == 2) {
-      *size = (uint32_t)mp_count_bits(a) + 1u;
+      *size = mp_count_bits(a) + 1;
       return MP_OKAY;
    }
    DOR(mp_init_copy(&t, a));
@@ -1424,12 +1424,12 @@ static mp_err s_rs(const mp_int *a, int radix, uint32_t *size)
    *size = digs + 1;
    return MP_OKAY;
 }
-static int test_mp_log_u32(void)
+static int test_mp_log_n(void)
 {
    mp_int a;
    mp_digit d;
-   uint32_t base, lb, size;
-   const uint32_t max_base = MP_MIN(UINT32_MAX, MP_DIGIT_MAX);
+   int base, lb, size;
+   const int max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);
 
    DOR(mp_init(&a));
 
@@ -1440,11 +1440,11 @@ static int test_mp_log_u32(void)
    */
    mp_set(&a, 42u);
    base = 0u;
-   if (mp_log_u32(&a, base, &lb) != MP_VAL) {
+   if (mp_log_n(&a, base, &lb) != MP_VAL) {
       goto LBL_ERR;
    }
    base = 1u;
-   if (mp_log_u32(&a, base, &lb) != MP_VAL) {
+   if (mp_log_n(&a, base, &lb) != MP_VAL) {
       goto LBL_ERR;
    }
    /*
@@ -1456,14 +1456,14 @@ static int test_mp_log_u32(void)
    */
    base = 2u;
    mp_zero(&a);
-   if (mp_log_u32(&a, base, &lb) != MP_VAL) {
+   if (mp_log_n(&a, base, &lb) != MP_VAL) {
       goto LBL_ERR;
    }
 
    for (d = 1; d < 4; d++) {
       mp_set(&a, d);
-      DO(mp_log_u32(&a, base, &lb));
-      if (lb != ((d == 1)?0uL:1uL)) {
+      DO(mp_log_n(&a, base, &lb));
+      if (lb != ((d == 1)?0:1)) {
          goto LBL_ERR;
       }
    }
@@ -1476,13 +1476,13 @@ static int test_mp_log_u32(void)
    */
    base = 3u;
    mp_zero(&a);
-   if (mp_log_u32(&a, base, &lb) != MP_VAL) {
+   if (mp_log_n(&a, base, &lb) != MP_VAL) {
       goto LBL_ERR;
    }
    for (d = 1; d < 4; d++) {
       mp_set(&a, d);
-      DO(mp_log_u32(&a, base, &lb));
-      if (lb != ((d < base)?0uL:1uL)) {
+      DO(mp_log_n(&a, base, &lb));
+      if (lb != (((int)d < base)?0:1)) {
          goto LBL_ERR;
       }
    }
@@ -1493,8 +1493,8 @@ static int test_mp_log_u32(void)
      radix_size.
    */
    DO(mp_rand(&a, 10));
-   for (base = 2u; base < 65u; base++) {
-      DO(mp_log_u32(&a, base, &lb));
+   for (base = 2; base < 65; base++) {
+      DO(mp_log_n(&a, base, &lb));
       DO(s_rs(&a,(int)base, &size));
       /* radix_size includes the memory needed for '\0', too*/
       size -= 2;
@@ -1508,8 +1508,8 @@ static int test_mp_log_u32(void)
      test the part of mp_ilogb that uses native types.
    */
    DO(mp_rand(&a, 1));
-   for (base = 2u; base < 65u; base++) {
-      DO(mp_log_u32(&a, base, &lb));
+   for (base = 2; base < 65; base++) {
+      DO(mp_log_n(&a, base, &lb));
       DO(s_rs(&a,(int)base, &size));
       size -= 2;
       if (lb != size) {
@@ -1519,9 +1519,9 @@ static int test_mp_log_u32(void)
 
    /*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10  */
    mp_set(&a, max_base);
-   DO(mp_expt_u32(&a, 10u, &a));
-   DO(mp_add_d(&a, max_base / 2u, &a));
-   DO(mp_log_u32(&a, max_base, &lb));
+   DO(mp_expt_n(&a, 10uL, &a));
+   DO(mp_add_d(&a, max_base / 2, &a));
+   DO(mp_log_n(&a, max_base, &lb));
    if (lb != 10u) {
       goto LBL_ERR;
    }
@@ -1636,7 +1636,7 @@ LBL_ERR:
 }
 
 /*
-   Cannot test mp_exp(_d) without mp_root and vice versa.
+   Cannot test mp_exp(_d) without mp_root_n and vice versa.
    So one of the two has to be tested from scratch.
 
    Numbers generated by
@@ -1658,7 +1658,7 @@ LBL_ERR:
    low-mp branch.
 */
 
-static int test_mp_root_u32(void)
+static int test_mp_root_n(void)
 {
    mp_int a, c, r;
    int i, j;
@@ -1850,10 +1850,10 @@ static int test_mp_root_u32(void)
    for (i = 0; i < 10; i++) {
       DO(mp_read_radix(&a, input[i], 64));
       for (j = 3; j < 100; j++) {
-         DO(mp_root_u32(&a, (uint32_t)j, &c));
+         DO(mp_root_n(&a, j, &c));
          DO(mp_read_radix(&r, root[i][j-3], 10));
          if (mp_cmp(&r, &c) != MP_EQ) {
-            fprintf(stderr, "mp_root_u32 failed at input #%d, root #%d\n", i, j);
+            fprintf(stderr, "mp_root_n failed at input #%d, root #%d\n", i, j);
             goto LBL_ERR;
          }
       }
@@ -2037,8 +2037,8 @@ static int test_mp_radix_size(void)
    DOR(mp_init(&a));
 
    /* number to result in a different size for every base: 67^(4 * 67) */
-   mp_set(&a, 67u);
-   DO(mp_expt_u32(&a, 268u, &a));
+   mp_set(&a, 67);
+   DO(mp_expt_n(&a, 268, &a));
 
    for (radix = 2; radix < 65; radix++) {
       DO(mp_radix_size(&a, radix, &size));
@@ -2304,13 +2304,13 @@ static int unit_tests(int argc, char **argv)
       T1(mp_get_u32, MP_GET_I32),
       T1(mp_get_u64, MP_GET_I64),
       T1(mp_get_ul, MP_GET_L),
-      T1(mp_log_u32, MP_LOG_U32),
+      T1(mp_log_n, MP_LOG_N),
       T1(mp_incr, MP_ADD_D),
       T1(mp_invmod, MP_INVMOD),
       T1(mp_is_square, MP_IS_SQUARE),
       T1(mp_kronecker, MP_KRONECKER),
       T1(mp_montgomery_reduce, MP_MONTGOMERY_REDUCE),
-      T1(mp_root_u32, MP_ROOT_U32),
+      T1(mp_root_n, MP_ROOT_N),
       T1(mp_or, MP_OR),
       T1(mp_prime_is_prime, MP_PRIME_IS_PRIME),
       T1(mp_prime_next_prime, MP_PRIME_NEXT_PRIME),
@@ -2326,7 +2326,7 @@ static int unit_tests(int argc, char **argv)
       T1(mp_set_double, MP_SET_DOUBLE),
 #endif
       T1(mp_signed_rsh, MP_SIGNED_RSH),
-      T1(mp_sqrt, MP_SQRT),
+      T2(mp_sqrt, MP_SQRT, mp_root_n),
       T1(mp_sqrtmod_prime, MP_SQRTMOD_PRIME),
       T1(mp_xor, MP_XOR),
       T2(s_mp_div_recursive, S_MP_DIV_RECURSIVE, S_MP_DIV_SCHOOL),
diff --git a/doc/bn.tex b/doc/bn.tex
index 64a07ff..1d6cf83 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -1911,9 +1911,9 @@ mp_err mp_sqrmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
 
 \chapter{Exponentiation}
 \section{Single Digit Exponentiation}
-\index{mp\_expt\_u32}
+\index{mp\_expt\_n}
 \begin{alltt}
-mp_err mp_expt_u32 (const mp_int *a, uint32_t b, mp_int *c)
+mp_err mp_expt_n(const mp_int *a, int b, int *c)
 \end{alltt}
 This function computes $c = a^b$.
 
@@ -1940,9 +1940,9 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
 It calculates $c = a \mod 2^b$.
 
 \section{Root Finding}
-\index{mp\_root\_u32}
+\index{mp\_root\_n}
 \begin{alltt}
-mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
+mp_err mp_root_n(const mp_int *a, int b, mp_int *c)
 \end{alltt}
 This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. Will return a positive root
 only for even roots and return a root with the sign of the input for odd roots.  For example,
@@ -1964,9 +1964,9 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret)
 A logarithm function for positive integer input \texttt{a, base} computing  $\floor{\log_bx}$ such
 that $(\log_b x)^b \le x$.
 
-\index{mp\_log\_u32}
+\index{mp\_log\_n}
 \begin{alltt}
-mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
+mp_err mp_log_n(const mp_int *a, int base, int *c)
 \end{alltt}
 
 \subsection{Example}
@@ -1981,7 +1981,7 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
 int main(int argc, char **argv)
 {
    mp_int x, output;
-   uint32_t base;
+   int base;
    mp_err e;
 
    if (argc != 3) {
@@ -1994,12 +1994,8 @@ int main(int argc, char **argv)
               exit(EXIT_FAILURE);
    }
    errno = 0;
-#ifdef MP_64BIT
-   /* Check for overflow skipped  */
-   base = (uint32_t)strtoull(argv[1], NULL, 10);
-#else
-   base = (uint32_t)strtoul(argv[1], NULL, 10);
-#endif
+   base = (int)strtoul(argv[1], NULL, 10);
+
    if (errno == ERANGE) {
       fprintf(stderr,"strtoul(l) failed: input out of range\textbackslash{}n");
       exit(EXIT_FAILURE);
@@ -2009,8 +2005,8 @@ int main(int argc, char **argv)
                       mp_error_to_string(e));
       exit(EXIT_FAILURE);
    }
-   if ((e = mp_log_u32(&x, base, &output)) != MP_OKAY) {
-      fprintf(stderr,"mp_ilogb failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
+   if ((e = mp_log_n(&x, base, &output)) != MP_OKAY) {
+      fprintf(stderr,"mp_log_n failed: \textbackslash{}"%s\textbackslash{}"\textbackslash{}n",
                       mp_error_to_string(e));
       exit(EXIT_FAILURE);
    }
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 7e16199..a9ab863 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -417,7 +417,7 @@
 			>
 		</File>
 		<File
-			RelativePath="mp_expt_u32.c"
+			RelativePath="mp_expt_n.c"
 			>
 		</File>
 		<File
@@ -557,7 +557,7 @@
 			>
 		</File>
 		<File
-			RelativePath="mp_log_u32.c"
+			RelativePath="mp_log_n.c"
 			>
 		</File>
 		<File
@@ -697,7 +697,7 @@
 			>
 		</File>
 		<File
-			RelativePath="mp_root_u32.c"
+			RelativePath="mp_root_n.c"
 			>
 		</File>
 		<File
@@ -857,11 +857,11 @@
 			>
 		</File>
 		<File
-			RelativePath="s_mp_log_d.c"
+			RelativePath="s_mp_log_2expt.c"
 			>
 		</File>
 		<File
-			RelativePath="s_mp_log_pow2.c"
+			RelativePath="s_mp_log_d.c"
 			>
 		</File>
 		<File
diff --git a/makefile b/makefile
index 88eff79..1ce63ee 100644
--- a/makefile
+++ b/makefile
@@ -29,23 +29,23 @@ LCOV_ARGS=--directory .
 OBJECTS=mp_2expt.o mp_abs.o mp_add.o mp_add_d.o mp_addmod.o mp_and.o mp_clamp.o mp_clear.o mp_clear_multi.o \
 mp_cmp.o mp_cmp_d.o mp_cmp_mag.o mp_cnt_lsb.o mp_complement.o mp_copy.o mp_count_bits.o mp_cutoffs.o \
 mp_div.o mp_div_2.o mp_div_2d.o mp_div_d.o mp_dr_is_modulus.o mp_dr_reduce.o mp_dr_setup.o \
-mp_error_to_string.o mp_exch.o mp_expt_u32.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
+mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
 mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o mp_get_ll.o \
 mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
 mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
 mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
 mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
 mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
 mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
 mp_prime_strong_lucas_selfridge.o mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \
 mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o \
-mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
+mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
 mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o \
 mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \
 mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
 s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
-s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
+s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o s_mp_log_d.o s_mp_montgomery_reduce_comba.o \
 s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
 s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
 s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
diff --git a/makefile.mingw b/makefile.mingw
index 3a3bc63..72d54ae 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -31,23 +31,23 @@ LIBMAIN_D =libtommath.dll
 OBJECTS=mp_2expt.o mp_abs.o mp_add.o mp_add_d.o mp_addmod.o mp_and.o mp_clamp.o mp_clear.o mp_clear_multi.o \
 mp_cmp.o mp_cmp_d.o mp_cmp_mag.o mp_cnt_lsb.o mp_complement.o mp_copy.o mp_count_bits.o mp_cutoffs.o \
 mp_div.o mp_div_2.o mp_div_2d.o mp_div_d.o mp_dr_is_modulus.o mp_dr_reduce.o mp_dr_setup.o \
-mp_error_to_string.o mp_exch.o mp_expt_u32.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
+mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
 mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o mp_get_ll.o \
 mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
 mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
 mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
 mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
 mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
 mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
 mp_prime_strong_lucas_selfridge.o mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \
 mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o \
-mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
+mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
 mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o \
 mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \
 mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
 s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
-s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
+s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o s_mp_log_d.o s_mp_montgomery_reduce_comba.o \
 s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
 s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
 s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
diff --git a/makefile.msvc b/makefile.msvc
index a22267c..fe0dc26 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -24,23 +24,23 @@ LIBMAIN_S =tommath.lib
 OBJECTS=mp_2expt.obj mp_abs.obj mp_add.obj mp_add_d.obj mp_addmod.obj mp_and.obj mp_clamp.obj mp_clear.obj mp_clear_multi.obj \
 mp_cmp.obj mp_cmp_d.obj mp_cmp_mag.obj mp_cnt_lsb.obj mp_complement.obj mp_copy.obj mp_count_bits.obj mp_cutoffs.obj \
 mp_div.obj mp_div_2.obj mp_div_2d.obj mp_div_d.obj mp_dr_is_modulus.obj mp_dr_reduce.obj mp_dr_setup.obj \
-mp_error_to_string.obj mp_exch.obj mp_expt_u32.obj mp_exptmod.obj mp_exteuclid.obj mp_fread.obj mp_from_sbin.obj \
+mp_error_to_string.obj mp_exch.obj mp_expt_n.obj mp_exptmod.obj mp_exteuclid.obj mp_fread.obj mp_from_sbin.obj \
 mp_from_ubin.obj mp_fwrite.obj mp_gcd.obj mp_get_double.obj mp_get_i32.obj mp_get_i64.obj mp_get_l.obj mp_get_ll.obj \
 mp_get_mag_u32.obj mp_get_mag_u64.obj mp_get_mag_ul.obj mp_get_mag_ull.obj mp_grow.obj mp_init.obj mp_init_copy.obj \
 mp_init_i32.obj mp_init_i64.obj mp_init_l.obj mp_init_ll.obj mp_init_multi.obj mp_init_set.obj mp_init_size.obj \
 mp_init_u32.obj mp_init_u64.obj mp_init_ul.obj mp_init_ull.obj mp_invmod.obj mp_is_square.obj mp_kronecker.obj mp_lcm.obj \
-mp_log_u32.obj mp_lshd.obj mp_mod.obj mp_mod_2d.obj mp_montgomery_calc_normalization.obj mp_montgomery_reduce.obj \
+mp_log_n.obj mp_lshd.obj mp_mod.obj mp_mod_2d.obj mp_montgomery_calc_normalization.obj mp_montgomery_reduce.obj \
 mp_montgomery_setup.obj mp_mul.obj mp_mul_2.obj mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj mp_neg.obj mp_or.obj mp_pack.obj \
 mp_pack_count.obj mp_prime_fermat.obj mp_prime_frobenius_underwood.obj mp_prime_is_prime.obj \
 mp_prime_miller_rabin.obj mp_prime_next_prime.obj mp_prime_rabin_miller_trials.obj mp_prime_rand.obj \
 mp_prime_strong_lucas_selfridge.obj mp_radix_size.obj mp_rand.obj mp_read_radix.obj mp_reduce.obj mp_reduce_2k.obj \
 mp_reduce_2k_l.obj mp_reduce_2k_setup.obj mp_reduce_2k_setup_l.obj mp_reduce_is_2k.obj mp_reduce_is_2k_l.obj \
-mp_reduce_setup.obj mp_root_u32.obj mp_rshd.obj mp_sbin_size.obj mp_set.obj mp_set_double.obj mp_set_i32.obj mp_set_i64.obj \
+mp_reduce_setup.obj mp_root_n.obj mp_rshd.obj mp_sbin_size.obj mp_set.obj mp_set_double.obj mp_set_i32.obj mp_set_i64.obj \
 mp_set_l.obj mp_set_ll.obj mp_set_u32.obj mp_set_u64.obj mp_set_ul.obj mp_set_ull.obj mp_shrink.obj mp_signed_rsh.obj \
 mp_sqrmod.obj mp_sqrt.obj mp_sqrtmod_prime.obj mp_sub.obj mp_sub_d.obj mp_submod.obj mp_to_radix.obj mp_to_sbin.obj \
 mp_to_ubin.obj mp_ubin_size.obj mp_unpack.obj mp_xor.obj mp_zero.obj s_mp_add.obj s_mp_copy_digs.obj s_mp_div_3.obj \
 s_mp_div_recursive.obj s_mp_div_school.obj s_mp_div_small.obj s_mp_exptmod.obj s_mp_exptmod_fast.obj s_mp_get_bit.obj \
-s_mp_invmod.obj s_mp_invmod_odd.obj s_mp_log.obj s_mp_log_d.obj s_mp_log_pow2.obj s_mp_montgomery_reduce_comba.obj \
+s_mp_invmod.obj s_mp_invmod_odd.obj s_mp_log.obj s_mp_log_2expt.obj s_mp_log_d.obj s_mp_montgomery_reduce_comba.obj \
 s_mp_mul.obj s_mp_mul_balance.obj s_mp_mul_comba.obj s_mp_mul_high.obj s_mp_mul_high_comba.obj s_mp_mul_karatsuba.obj \
 s_mp_mul_toom.obj s_mp_prime_is_divisible.obj s_mp_prime_tab.obj s_mp_radix_map.obj s_mp_rand_jenkins.obj \
 s_mp_rand_platform.obj s_mp_sqr.obj s_mp_sqr_comba.obj s_mp_sqr_karatsuba.obj s_mp_sqr_toom.obj s_mp_sub.obj \
diff --git a/makefile.shared b/makefile.shared
index ad58e61..9c6ace0 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -26,23 +26,23 @@ LCOV_ARGS=--directory .libs --directory .
 OBJECTS=mp_2expt.o mp_abs.o mp_add.o mp_add_d.o mp_addmod.o mp_and.o mp_clamp.o mp_clear.o mp_clear_multi.o \
 mp_cmp.o mp_cmp_d.o mp_cmp_mag.o mp_cnt_lsb.o mp_complement.o mp_copy.o mp_count_bits.o mp_cutoffs.o \
 mp_div.o mp_div_2.o mp_div_2d.o mp_div_d.o mp_dr_is_modulus.o mp_dr_reduce.o mp_dr_setup.o \
-mp_error_to_string.o mp_exch.o mp_expt_u32.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
+mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
 mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o mp_get_ll.o \
 mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
 mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
 mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
 mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
 mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
 mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
 mp_prime_strong_lucas_selfridge.o mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \
 mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o \
-mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
+mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
 mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o \
 mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \
 mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
 s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
-s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
+s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o s_mp_log_d.o s_mp_montgomery_reduce_comba.o \
 s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
 s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
 s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
diff --git a/makefile.unix b/makefile.unix
index 1e0da73..8f1ba07 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -32,23 +32,23 @@ LIBMAIN_S = libtommath.a
 OBJECTS=mp_2expt.o mp_abs.o mp_add.o mp_add_d.o mp_addmod.o mp_and.o mp_clamp.o mp_clear.o mp_clear_multi.o \
 mp_cmp.o mp_cmp_d.o mp_cmp_mag.o mp_cnt_lsb.o mp_complement.o mp_copy.o mp_count_bits.o mp_cutoffs.o \
 mp_div.o mp_div_2.o mp_div_2d.o mp_div_d.o mp_dr_is_modulus.o mp_dr_reduce.o mp_dr_setup.o \
-mp_error_to_string.o mp_exch.o mp_expt_u32.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
+mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
 mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o mp_get_ll.o \
 mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_get_mag_ull.o mp_grow.o mp_init.o mp_init_copy.o \
 mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_ll.o mp_init_multi.o mp_init_set.o mp_init_size.o \
 mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_init_ull.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o \
-mp_log_u32.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
+mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o \
 mp_montgomery_setup.o mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o \
 mp_pack_count.o mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o \
 mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \
 mp_prime_strong_lucas_selfridge.o mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \
 mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o \
-mp_reduce_setup.o mp_root_u32.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
+mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
 mp_set_l.o mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_signed_rsh.o \
 mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \
 mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
 s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
-s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
+s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o s_mp_log_d.o s_mp_montgomery_reduce_comba.o \
 s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
 s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
 s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
diff --git a/mp_expt_n.c b/mp_expt_n.c
new file mode 100644
index 0000000..93f9249
--- /dev/null
+++ b/mp_expt_n.c
@@ -0,0 +1,43 @@
+#include "tommath_private.h"
+#ifdef MP_EXPT_N_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* calculate c = a**b  using a square-multiply algorithm */
+mp_err mp_expt_n(const mp_int *a, int b, mp_int *c)
+{
+   mp_err err;
+   mp_int  g;
+
+   if ((err = mp_init_copy(&g, a)) != MP_OKAY) {
+      return err;
+   }
+
+   /* set initial result */
+   mp_set(c, 1uL);
+
+   while (b > 0) {
+      /* if the bit is set multiply */
+      if ((b & 1) != 0) {
+         if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
+            goto LBL_ERR;
+         }
+      }
+
+      /* square */
+      if (b > 1) {
+         if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
+            goto LBL_ERR;
+         }
+      }
+
+      /* shift to next bit */
+      b >>= 1;
+   }
+
+LBL_ERR:
+   mp_clear(&g);
+   return err;
+}
+
+#endif
diff --git a/mp_expt_u32.c b/mp_expt_u32.c
deleted file mode 100644
index a580fbf..0000000
--- a/mp_expt_u32.c
+++ /dev/null
@@ -1,46 +0,0 @@
-#include "tommath_private.h"
-#ifdef MP_EXPT_U32_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* calculate c = a**b  using a square-multiply algorithm */
-mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
-{
-   mp_err err;
-
-   mp_int  g;
-
-   if ((err = mp_init_copy(&g, a)) != MP_OKAY) {
-      return err;
-   }
-
-   /* set initial result */
-   mp_set(c, 1uL);
-
-   while (b > 0u) {
-      /* if the bit is set multiply */
-      if ((b & 1u) != 0u) {
-         if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
-            goto LBL_ERR;
-         }
-      }
-
-      /* square */
-      if (b > 1u) {
-         if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
-            goto LBL_ERR;
-         }
-      }
-
-      /* shift to next bit */
-      b >>= 1;
-   }
-
-   err = MP_OKAY;
-
-LBL_ERR:
-   mp_clear(&g);
-   return err;
-}
-
-#endif
diff --git a/mp_log_n.c b/mp_log_n.c
new file mode 100644
index 0000000..4de1e39
--- /dev/null
+++ b/mp_log_n.c
@@ -0,0 +1,29 @@
+#include "tommath_private.h"
+#ifdef MP_LOG_N_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+mp_err mp_log_n(const mp_int *a, int base, int *c)
+{
+   if (mp_isneg(a) || mp_iszero(a) || (base < 2) || (unsigned)base > (unsigned)MP_DIGIT_MAX) {
+      return MP_VAL;
+   }
+
+   if (MP_HAS(S_MP_LOG_2EXPT) && MP_IS_2EXPT((mp_digit)base)) {
+      *c = s_mp_log_2expt(a, (mp_digit)base);
+      return MP_OKAY;
+   }
+
+   if (MP_HAS(S_MP_LOG_D) && (a->used == 1)) {
+      *c = s_mp_log_d((mp_digit)base, a->dp[0]);
+      return MP_OKAY;
+   }
+
+   if (MP_HAS(S_MP_LOG)) {
+      return s_mp_log(a, (mp_digit)base, c);
+   }
+
+   return MP_VAL;
+}
+
+#endif
diff --git a/mp_log_u32.c b/mp_log_u32.c
deleted file mode 100644
index 1450d3a..0000000
--- a/mp_log_u32.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "tommath_private.h"
-#ifdef MP_LOG_U32_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
-{
-   if (mp_isneg(a) || mp_iszero(a) || (base < 2u)) {
-      return MP_VAL;
-   }
-
-   if (MP_HAS(S_MP_LOG_POW2) && MP_IS_2EXPT(base)) {
-      *c = s_mp_log_pow2(a, base);
-      return MP_OKAY;
-   }
-
-   if (MP_HAS(S_MP_LOG_D) && (a->used == 1)) {
-      *c = (uint32_t)s_mp_log_d(base, a->dp[0]);
-      return MP_OKAY;
-   }
-
-   if (MP_HAS(S_MP_LOG)) {
-      return s_mp_log(a, base, c);
-   }
-
-   return MP_VAL;
-}
-
-#endif
diff --git a/mp_radix_size.c b/mp_radix_size.c
index 7f7cbc2..ca08438 100644
--- a/mp_radix_size.c
+++ b/mp_radix_size.c
@@ -8,7 +8,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
 {
    mp_err err;
    mp_int a_;
-   uint32_t b;
+   int b;
 
    /* make sure the radix is in range */
    if ((radix < 2) || (radix > 64)) {
@@ -22,14 +22,13 @@ mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
 
    a_ = *a;
    a_.sign = MP_ZPOS;
-   if ((err = mp_log_u32(&a_, (uint32_t)radix, &b)) != MP_OKAY) {
-      goto LBL_ERR;
+   if ((err = mp_log_n(&a_, radix, &b)) != MP_OKAY) {
+      return err;
    }
 
    /* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
    *size = (size_t)b + 2U + (mp_isneg(a) ? 1U : 0U);
 
-LBL_ERR:
-   return err;
+   return MP_OKAY;
 }
 #endif
diff --git a/mp_root_n.c b/mp_root_n.c
new file mode 100644
index 0000000..d904df8
--- /dev/null
+++ b/mp_root_n.c
@@ -0,0 +1,141 @@
+#include "tommath_private.h"
+#ifdef MP_ROOT_N_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* find the n'th root of an integer
+ *
+ * Result found such that (c)**b <= a and (c+1)**b > a
+ *
+ * This algorithm uses Newton's approximation
+ * x[i+1] = x[i] - f(x[i])/f'(x[i])
+ * which will find the root in log(N) time where
+ * each step involves a fair bit.
+ */
+mp_err mp_root_n(const mp_int *a, int b, mp_int *c)
+{
+   mp_int t1, t2, t3, a_;
+   int    ilog2;
+   mp_err err;
+
+   if (b < 0 || (unsigned)b > (unsigned)MP_DIGIT_MAX) {
+      return MP_VAL;
+   }
+
+   /* input must be positive if b is even */
+   if (((b & 1) == 0) && mp_isneg(a)) {
+      return MP_VAL;
+   }
+
+   if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
+      return err;
+   }
+
+   /* if a is negative fudge the sign but keep track */
+   a_ = *a;
+   a_.sign = MP_ZPOS;
+
+   /* Compute seed: 2^(log_2(n)/b + 2)*/
+   ilog2 = mp_count_bits(a);
+
+   /*
+     If "b" is larger than INT_MAX it is also larger than
+     log_2(n) because the bit-length of the "n" is measured
+     with an int and hence the root is always < 2 (two).
+   */
+   if (b > INT_MAX/2) {
+      mp_set(c, 1uL);
+      c->sign = a->sign;
+      err = MP_OKAY;
+      goto LBL_ERR;
+   }
+
+   /* "b" is smaller than INT_MAX, we can cast safely */
+   if (ilog2 < b) {
+      mp_set(c, 1uL);
+      c->sign = a->sign;
+      err = MP_OKAY;
+      goto LBL_ERR;
+   }
+   ilog2 =  ilog2 / b;
+   if (ilog2 == 0) {
+      mp_set(c, 1uL);
+      c->sign = a->sign;
+      err = MP_OKAY;
+      goto LBL_ERR;
+   }
+   /* Start value must be larger than root */
+   ilog2 += 2;
+   if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY)                    goto LBL_ERR;
+   do {
+      /* t1 = t2 */
+      if ((err = mp_copy(&t2, &t1)) != MP_OKAY)                   goto LBL_ERR;
+
+      /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
+
+      /* t3 = t1**(b-1) */
+      if ((err = mp_expt_n(&t1, b - 1, &t3)) != MP_OKAY)       goto LBL_ERR;
+
+      /* numerator */
+      /* t2 = t1**b */
+      if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY)               goto LBL_ERR;
+
+      /* t2 = t1**b - a */
+      if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY)               goto LBL_ERR;
+
+      /* denominator */
+      /* t3 = t1**(b-1) * b  */
+      if ((err = mp_mul_d(&t3, (mp_digit)b, &t3)) != MP_OKAY)               goto LBL_ERR;
+
+      /* t3 = (t1**b - a)/(b * t1**(b-1)) */
+      if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY)         goto LBL_ERR;
+
+      if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY)               goto LBL_ERR;
+
+      /*
+          Number of rounds is at most log_2(root). If it is more it
+          got stuck, so break out of the loop and do the rest manually.
+       */
+      if (ilog2-- == 0) {
+         break;
+      }
+   }  while (mp_cmp(&t1, &t2) != MP_EQ);
+
+   /* result can be off by a few so check */
+   /* Loop beneath can overshoot by one if found root is smaller than actual root */
+   for (;;) {
+      mp_ord cmp;
+      if ((err = mp_expt_n(&t1, b, &t2)) != MP_OKAY)            goto LBL_ERR;
+      cmp = mp_cmp(&t2, &a_);
+      if (cmp == MP_EQ) {
+         err = MP_OKAY;
+         goto LBL_ERR;
+      }
+      if (cmp == MP_LT) {
+         if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY)          goto LBL_ERR;
+      } else {
+         break;
+      }
+   }
+   /* correct overshoot from above or from recurrence */
+   for (;;) {
+      if ((err = mp_expt_n(&t1, b, &t2)) != MP_OKAY)            goto LBL_ERR;
+      if (mp_cmp(&t2, &a_) == MP_GT) {
+         if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY)          goto LBL_ERR;
+      } else {
+         break;
+      }
+   }
+
+   /* set the result */
+   mp_exch(&t1, c);
+
+   /* set the sign of the result */
+   c->sign = a->sign;
+
+LBL_ERR:
+   mp_clear_multi(&t1, &t2, &t3, NULL);
+   return err;
+}
+
+#endif
diff --git a/mp_root_u32.c b/mp_root_u32.c
deleted file mode 100644
index 1f972d9..0000000
--- a/mp_root_u32.c
+++ /dev/null
@@ -1,139 +0,0 @@
-#include "tommath_private.h"
-#ifdef MP_ROOT_U32_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* find the n'th root of an integer
- *
- * Result found such that (c)**b <= a and (c+1)**b > a
- *
- * This algorithm uses Newton's approximation
- * x[i+1] = x[i] - f(x[i])/f'(x[i])
- * which will find the root in log(N) time where
- * each step involves a fair bit.
- */
-mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
-{
-   mp_int t1, t2, t3, a_;
-   mp_ord cmp;
-   int    ilog2;
-   mp_err err;
-
-   /* input must be positive if b is even */
-   if (((b & 1u) == 0u) && mp_isneg(a)) {
-      return MP_VAL;
-   }
-
-   if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
-      return err;
-   }
-
-   /* if a is negative fudge the sign but keep track */
-   a_ = *a;
-   a_.sign = MP_ZPOS;
-
-   /* Compute seed: 2^(log_2(n)/b + 2)*/
-   ilog2 = mp_count_bits(a);
-
-   /*
-     If "b" is larger than INT_MAX it is also larger than
-     log_2(n) because the bit-length of the "n" is measured
-     with an int and hence the root is always < 2 (two).
-   */
-   if (b > (uint32_t)(INT_MAX/2)) {
-      mp_set(c, 1uL);
-      c->sign = a->sign;
-      err = MP_OKAY;
-      goto LBL_ERR;
-   }
-
-   /* "b" is smaller than INT_MAX, we can cast safely */
-   if (ilog2 < (int)b) {
-      mp_set(c, 1uL);
-      c->sign = a->sign;
-      err = MP_OKAY;
-      goto LBL_ERR;
-   }
-   ilog2 =  ilog2 / ((int)b);
-   if (ilog2 == 0) {
-      mp_set(c, 1uL);
-      c->sign = a->sign;
-      err = MP_OKAY;
-      goto LBL_ERR;
-   }
-   /* Start value must be larger than root */
-   ilog2 += 2;
-   if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY)                    goto LBL_ERR;
-   do {
-      /* t1 = t2 */
-      if ((err = mp_copy(&t2, &t1)) != MP_OKAY)                   goto LBL_ERR;
-
-      /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
-
-      /* t3 = t1**(b-1) */
-      if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY)       goto LBL_ERR;
-
-      /* numerator */
-      /* t2 = t1**b */
-      if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY)               goto LBL_ERR;
-
-      /* t2 = t1**b - a */
-      if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY)               goto LBL_ERR;
-
-      /* denominator */
-      /* t3 = t1**(b-1) * b  */
-      if ((err = mp_mul_d(&t3, b, &t3)) != MP_OKAY)               goto LBL_ERR;
-
-      /* t3 = (t1**b - a)/(b * t1**(b-1)) */
-      if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY)         goto LBL_ERR;
-
-      if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY)               goto LBL_ERR;
-
-      /*
-          Number of rounds is at most log_2(root). If it is more it
-          got stuck, so break out of the loop and do the rest manually.
-       */
-      if (ilog2-- == 0) {
-         break;
-      }
-   }  while (mp_cmp(&t1, &t2) != MP_EQ);
-
-   /* result can be off by a few so check */
-   /* Loop beneath can overshoot by one if found root is smaller than actual root */
-   for (;;) {
-      if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY)            goto LBL_ERR;
-      cmp = mp_cmp(&t2, &a_);
-      if (cmp == MP_EQ) {
-         err = MP_OKAY;
-         goto LBL_ERR;
-      }
-      if (cmp == MP_LT) {
-         if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY)          goto LBL_ERR;
-      } else {
-         break;
-      }
-   }
-   /* correct overshoot from above or from recurrence */
-   for (;;) {
-      if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY)            goto LBL_ERR;
-      if (mp_cmp(&t2, &a_) == MP_GT) {
-         if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY)          goto LBL_ERR;
-      } else {
-         break;
-      }
-   }
-
-   /* set the result */
-   mp_exch(&t1, c);
-
-   /* set the sign of the result */
-   c->sign = a->sign;
-
-   err = MP_OKAY;
-
-LBL_ERR:
-   mp_clear_multi(&t1, &t2, &t3, NULL);
-   return err;
-}
-
-#endif
diff --git a/s_mp_log.c b/s_mp_log.c
index eba279e..d1ac73b 100644
--- a/s_mp_log.c
+++ b/s_mp_log.c
@@ -3,14 +3,13 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c)
+mp_err s_mp_log(const mp_int *a, mp_digit base, int *c)
 {
    mp_err err;
-   mp_ord cmp;
-   uint32_t high, low, mid;
+   int high, low;
    mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
 
-   cmp = mp_cmp_d(a, base);
+   mp_ord cmp = mp_cmp_d(a, base);
    if ((cmp == MP_LT) || (cmp == MP_EQ)) {
       *c = cmp == MP_EQ;
       return MP_OKAY;
@@ -22,9 +21,9 @@ mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c)
       return err;
    }
 
-   low = 0u;
+   low = 0;
    mp_set(&bracket_low, 1uL);
-   high = 1u;
+   high = 1;
 
    mp_set(&bracket_high, base);
 
@@ -46,10 +45,10 @@ mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c)
    }
    mp_set(&bi_base, base);
 
-   while ((high - low) > 1u) {
-      mid = (high + low) >> 1;
+   while ((high - low) > 1) {
+      int mid = (high + low) >> 1;
 
-      if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) {
+      if ((err = mp_expt_n(&bi_base, mid - low, &t)) != MP_OKAY) {
          goto LBL_END;
       }
       if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) {
diff --git a/s_mp_log_2expt.c b/s_mp_log_2expt.c
new file mode 100644
index 0000000..ec0fda3
--- /dev/null
+++ b/s_mp_log_2expt.c
@@ -0,0 +1,12 @@
+#include "tommath_private.h"
+#ifdef S_MP_LOG_2EXPT_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+int s_mp_log_2expt(const mp_int *a, mp_digit base)
+{
+   int y;
+   for (y = 0; (base & 1) == 0; y++, base >>= 1) {}
+   return (mp_count_bits(a) - 1) / y;
+}
+#endif
diff --git a/s_mp_log_d.c b/s_mp_log_d.c
index c9c8df2..5ff6c1f 100644
--- a/s_mp_log_d.c
+++ b/s_mp_log_d.c
@@ -17,21 +17,18 @@ static mp_word s_pow(mp_word base, mp_word exponent)
    return result;
 }
 
-mp_digit s_mp_log_d(mp_digit base, mp_digit n)
+int s_mp_log_d(mp_digit base, mp_digit n)
 {
-   mp_word bracket_low = 1u, bracket_mid, bracket_high, N;
-   mp_digit ret, high = 1uL, low = 0uL, mid;
+   mp_word bracket_low = 1uLL, bracket_high = base, N = n;
+   int ret, high = 1, low = 0;
 
    if (n < base) {
-      return 0uL;
+      return 0;
    }
    if (n == base) {
-      return 1uL;
+      return 1;
    }
 
-   bracket_high = (mp_word) base ;
-   N = (mp_word) n;
-
    while (bracket_high < N) {
       low = high;
       bracket_low = bracket_high;
@@ -40,8 +37,8 @@ mp_digit s_mp_log_d(mp_digit base, mp_digit n)
    }
 
    while (((mp_digit)(high - low)) > 1uL) {
-      mid = (low + high) >> 1;
-      bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
+      int mid = (low + high) >> 1;
+      mp_word bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
 
       if (N < bracket_mid) {
          high = mid ;
@@ -52,7 +49,7 @@ mp_digit s_mp_log_d(mp_digit base, mp_digit n)
          bracket_low = bracket_mid ;
       }
       if (N == bracket_mid) {
-         return (mp_digit) mid;
+         return mid;
       }
    }
 
diff --git a/s_mp_log_pow2.c b/s_mp_log_pow2.c
deleted file mode 100644
index 74271c6..0000000
--- a/s_mp_log_pow2.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "tommath_private.h"
-#ifdef S_MP_LOG_POW2_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base)
-{
-   int y;
-   for (y = 0; (base & 1u) == 0u; y++, base >>= 1) {}
-   return (uint32_t)((mp_count_bits(a) - 1) / y);
-}
-#endif
diff --git a/tommath.def b/tommath.def
index 8bc6eac..52953f9 100644
--- a/tommath.def
+++ b/tommath.def
@@ -31,7 +31,7 @@ EXPORTS
     mp_dr_setup
     mp_error_to_string
     mp_exch
-    mp_expt_u32
+    mp_expt_n
     mp_exptmod
     mp_exteuclid
     mp_fread
@@ -66,7 +66,7 @@ EXPORTS
     mp_is_square
     mp_kronecker
     mp_lcm
-    mp_log_u32
+    mp_log_n
     mp_lshd
     mp_mod
     mp_mod_2d
@@ -101,7 +101,7 @@ EXPORTS
     mp_reduce_is_2k
     mp_reduce_is_2k_l
     mp_reduce_setup
-    mp_root_u32
+    mp_root_n
     mp_rshd
     mp_sbin_size
     mp_set
diff --git a/tommath.h b/tommath.h
index 5e75c98..4b2bc04 100644
--- a/tommath.h
+++ b/tommath.h
@@ -423,11 +423,17 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp
 /* c = [a, b] or (a*b)/(a, b) */
 mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
+/* Integer logarithm to integer base */
+mp_err mp_log_n(const mp_int *a, int base, int *c) MP_WUR;
+
+/* c = a**b */
+mp_err mp_expt_n(const mp_int *a, int b, mp_int *c) MP_WUR;
+
 /* finds one of the b'th root of a, such that |c|**b <= |a|
  *
  * returns error if a < 0 and b is even
  */
-mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
+mp_err mp_root_n(const mp_int *a, int b, mp_int *c) MP_WUR;
 
 /* special sqrt algo */
 mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
@@ -557,12 +563,6 @@ mp_err mp_prime_next_prime(mp_int *a, int t, bool bbs_style) MP_WUR;
  */
 mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR;
 
-/* Integer logarithm to integer base */
-mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR;
-
-/* c = a**b */
-mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR;
-
 /* ---> radix conversion <--- */
 int mp_count_bits(const mp_int *a) MP_WUR;
 
diff --git a/tommath_class.h b/tommath_class.h
index f5f9907..c8f4367 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -37,7 +37,7 @@
 #   define MP_DR_SETUP_C
 #   define MP_ERROR_TO_STRING_C
 #   define MP_EXCH_C
-#   define MP_EXPT_U32_C
+#   define MP_EXPT_N_C
 #   define MP_EXPTMOD_C
 #   define MP_EXTEUCLID_C
 #   define MP_FREAD_C
@@ -72,7 +72,7 @@
 #   define MP_IS_SQUARE_C
 #   define MP_KRONECKER_C
 #   define MP_LCM_C
-#   define MP_LOG_U32_C
+#   define MP_LOG_N_C
 #   define MP_LSHD_C
 #   define MP_MOD_C
 #   define MP_MOD_2D_C
@@ -107,7 +107,7 @@
 #   define MP_REDUCE_IS_2K_C
 #   define MP_REDUCE_IS_2K_L_C
 #   define MP_REDUCE_SETUP_C
-#   define MP_ROOT_U32_C
+#   define MP_ROOT_N_C
 #   define MP_RSHD_C
 #   define MP_SBIN_SIZE_C
 #   define MP_SET_C
@@ -147,8 +147,8 @@
 #   define S_MP_INVMOD_C
 #   define S_MP_INVMOD_ODD_C
 #   define S_MP_LOG_C
+#   define S_MP_LOG_2EXPT_C
 #   define S_MP_LOG_D_C
-#   define S_MP_LOG_POW2_C
 #   define S_MP_MONTGOMERY_REDUCE_COMBA_C
 #   define S_MP_MUL_C
 #   define S_MP_MUL_BALANCE_C
@@ -296,7 +296,7 @@
 #if defined(MP_EXCH_C)
 #endif
 
-#if defined(MP_EXPT_U32_C)
+#if defined(MP_EXPT_N_C)
 #   define MP_CLEAR_C
 #   define MP_INIT_COPY_C
 #   define MP_MUL_C
@@ -496,10 +496,10 @@
 #   define MP_MUL_C
 #endif
 
-#if defined(MP_LOG_U32_C)
+#if defined(MP_LOG_N_C)
+#   define S_MP_LOG_2EXPT_C
 #   define S_MP_LOG_C
 #   define S_MP_LOG_D_C
-#   define S_MP_LOG_POW2_C
 #endif
 
 #if defined(MP_LSHD_C)
@@ -708,7 +708,7 @@
 #endif
 
 #if defined(MP_RADIX_SIZE_C)
-#   define MP_LOG_U32_C
+#   define MP_LOG_N_C
 #endif
 
 #if defined(MP_RAND_C)
@@ -793,7 +793,7 @@
 #   define MP_DIV_C
 #endif
 
-#if defined(MP_ROOT_U32_C)
+#if defined(MP_ROOT_N_C)
 #   define MP_2EXPT_C
 #   define MP_ADD_D_C
 #   define MP_CLEAR_MULTI_C
@@ -802,7 +802,7 @@
 #   define MP_COUNT_BITS_C
 #   define MP_DIV_C
 #   define MP_EXCH_C
-#   define MP_EXPT_U32_C
+#   define MP_EXPT_N_C
 #   define MP_INIT_MULTI_C
 #   define MP_MUL_C
 #   define MP_MUL_D_C
@@ -1102,17 +1102,17 @@
 #   define MP_CMP_D_C
 #   define MP_COPY_C
 #   define MP_EXCH_C
-#   define MP_EXPT_U32_C
+#   define MP_EXPT_N_C
 #   define MP_INIT_MULTI_C
 #   define MP_MUL_C
 #   define MP_SET_C
 #endif
 
-#if defined(S_MP_LOG_D_C)
+#if defined(S_MP_LOG_2EXPT_C)
+#   define MP_COUNT_BITS_C
 #endif
 
-#if defined(S_MP_LOG_POW2_C)
-#   define MP_COUNT_BITS_C
+#if defined(S_MP_LOG_D_C)
 #endif
 
 #if defined(S_MP_MONTGOMERY_REDUCE_COMBA_C)
diff --git a/tommath_private.h b/tommath_private.h
index 938865f..60b3336 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -161,7 +161,8 @@ extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
 
 /* lowlevel functions, do not call! */
 MP_PRIVATE bool s_mp_get_bit(const mp_int *a, int b) MP_WUR;
-MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n) MP_WUR;
+MP_PRIVATE int s_mp_log_2expt(const mp_int *a, mp_digit base) MP_WUR;
+MP_PRIVATE int s_mp_log_d(mp_digit base, mp_digit n) MP_WUR;
 MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 MP_PRIVATE mp_err s_mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
 MP_PRIVATE mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r) MP_WUR;
@@ -171,7 +172,7 @@ MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P
 MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
 MP_PRIVATE mp_err s_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 MP_PRIVATE mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR;
+MP_PRIVATE mp_err s_mp_log(const mp_int *a, mp_digit base, int *c) MP_WUR;
 MP_PRIVATE mp_err s_mp_montgomery_reduce_comba(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
 MP_PRIVATE mp_err s_mp_mul(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
 MP_PRIVATE mp_err s_mp_mul_balance(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
@@ -187,7 +188,6 @@ MP_PRIVATE mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b) MP_WUR;
 MP_PRIVATE mp_err s_mp_sqr_karatsuba(const mp_int *a, mp_int *b) MP_WUR;
 MP_PRIVATE mp_err s_mp_sqr_toom(const mp_int *a, mp_int *b) MP_WUR;
 MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
-MP_PRIVATE uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base) MP_WUR;
 MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
 MP_PRIVATE void s_mp_zero_buf(void *mem, size_t size);
 MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits);
diff --git a/tommath_superclass.h b/tommath_superclass.h
index dd83cad..9e85d98 100644
--- a/tommath_superclass.h
+++ b/tommath_superclass.h
@@ -28,12 +28,12 @@
 #   define MP_NEG_C
 #   define MP_PRIME_FROBENIUS_UNDERWOOD_C
 #   define MP_RADIX_SIZE_C
-#   define MP_LOG_U32_C
+#   define MP_LOG_N_C
 #   define MP_RAND_C
 #   define MP_REDUCE_C
 #   define MP_REDUCE_2K_L_C
 #   define MP_FROM_SBIN_C
-#   define MP_ROOT_U32_C
+#   define MP_ROOT_N_C
 #   define MP_SET_L_C
 #   define MP_SET_UL_C
 #   define MP_SBIN_SIZE_C