Commit f6a7bedb95f0b0840454fbd7df1c738292017413

Daniel Mendler 2019-10-29T20:52:29

suffix _u32 -> _n of mp_(expt|log|root) functions, use int for now

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
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/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.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_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