Commit fd2693893550ca6c7400af5fd6715dec21e2a8cb

Steffen Jaeckel 2019-05-27T16:53:55

Merge pull request #294 from libtom/deprecate-ex-funs deprecate mp_n_root_ex and mp_expt_d_ex

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
diff --git a/bn_deprecated.c b/bn_deprecated.c
index ded71c7..71fa645 100644
--- a/bn_deprecated.c
+++ b/bn_deprecated.c
@@ -191,4 +191,18 @@ mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
    return s_mp_prime_is_divisible(a, result);
 }
 #endif
+#ifdef BN_MP_EXPT_D_EX_C
+mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
+{
+   (void)fast;
+   return mp_expt_d(a, b, c);
+}
+#endif
+#ifdef BN_MP_N_ROOT_EX_C
+mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
+{
+   (void)fast;
+   return mp_n_root(a, b, c);
+}
+#endif
 #endif
diff --git a/bn_mp_expt_d.c b/bn_mp_expt_d.c
index 41a7cbe..ce471f1 100644
--- a/bn_mp_expt_d.c
+++ b/bn_mp_expt_d.c
@@ -3,10 +3,43 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* wrapper function for mp_expt_d_ex() */
+/* calculate c = a**b  using a square-multiply algorithm */
 mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
 {
-   return mp_expt_d_ex(a, b, c, 0);
+   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) {
+            mp_clear(&g);
+            return err;
+         }
+      }
+
+      /* square */
+      if (b > 1u) {
+         if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
+            mp_clear(&g);
+            return err;
+         }
+      }
+
+      /* shift to next bit */
+      b >>= 1;
+   }
+
+   mp_clear(&g);
+   return MP_OKAY;
 }
 
 #endif
diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c
deleted file mode 100644
index fdabf84..0000000
--- a/bn_mp_expt_d_ex.c
+++ /dev/null
@@ -1,66 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_EXPT_D_EX_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_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
-{
-   mp_err err;
-   unsigned int x;
-
-   mp_int  g;
-
-   if ((err = mp_init_copy(&g, a)) != MP_OKAY) {
-      return err;
-   }
-
-   /* set initial result */
-   mp_set(c, 1uL);
-
-   if (fast != 0) {
-      while (b > 0u) {
-         /* if the bit is set multiply */
-         if ((b & 1u) != 0u) {
-            if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
-               mp_clear(&g);
-               return err;
-            }
-         }
-
-         /* square */
-         if (b > 1u) {
-            if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
-               mp_clear(&g);
-               return err;
-            }
-         }
-
-         /* shift to next bit */
-         b >>= 1;
-      }
-   } else {
-      for (x = 0; x < (unsigned)MP_DIGIT_BIT; x++) {
-         /* square */
-         if ((err = mp_sqr(c, c)) != MP_OKAY) {
-            mp_clear(&g);
-            return err;
-         }
-
-         /* if the bit is set multiply */
-         if ((b & ((mp_digit)1 << (MP_DIGIT_BIT - 1))) != 0u) {
-            if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
-               mp_clear(&g);
-               return err;
-            }
-         }
-
-         /* shift to next bit */
-         b <<= 1;
-      }
-   } /* if ... else */
-
-   mp_clear(&g);
-   return MP_OKAY;
-}
-#endif
diff --git a/bn_mp_n_root.c b/bn_mp_n_root.c
index 5b5e743..3f959f1 100644
--- a/bn_mp_n_root.c
+++ b/bn_mp_n_root.c
@@ -3,12 +3,168 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* wrapper function for mp_n_root_ex()
- * computes c = (a)**(1/b) such that (c)**b <= a and (c+1)**b > a
+/* 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_n_root(const mp_int *a, mp_digit b, mp_int *c)
 {
-   return mp_n_root_ex(a, b, c, 0);
+   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) && (a->sign == MP_NEG)) {
+      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);
+
+   /*
+      GCC and clang do not understand the sizeof tests and complain,
+      icc (the Intel compiler) seems to understand, at least it doesn't complain.
+      2 of 3 say these macros are necessary, so there they are.
+   */
+#if ( !(defined MP_8BIT) && !(defined MP_16BIT) )
+   /*
+       The type of mp_digit might be larger than an int.
+       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 (sizeof(mp_digit) >= sizeof(int)) {
+      if (b > (mp_digit)(INT_MAX/2)) {
+         mp_set(c, 1uL);
+         c->sign = a->sign;
+         err = MP_OKAY;
+         goto LBL_ERR;
+      }
+   }
+#endif
+   /* "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_d(&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_d(&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_d(&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/bn_mp_n_root_ex.c b/bn_mp_n_root_ex.c
deleted file mode 100644
index a6ddfdb..0000000
--- a/bn_mp_n_root_ex.c
+++ /dev/null
@@ -1,169 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_MP_N_ROOT_EX_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_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
-{
-   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) && (a->sign == MP_NEG)) {
-      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);
-
-   /*
-      GCC and clang do not understand the sizeof tests and complain,
-      icc (the Intel compiler) seems to understand, at least it doesn't complain.
-      2 of 3 say these macros are necessary, so there they are.
-   */
-#if ( !(defined MP_8BIT) && !(defined MP_16BIT) )
-   /*
-       The type of mp_digit might be larger than an int.
-       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 (sizeof(mp_digit) >= sizeof(int)) {
-      if (b > (mp_digit)(INT_MAX/2)) {
-         mp_set(c, 1uL);
-         c->sign = a->sign;
-         err = MP_OKAY;
-         goto LBL_ERR;
-      }
-   }
-#endif
-   /* "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_d_ex(&t1, b - 1u, &t3, fast)) != 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_d_ex(&t1, b, &t2, fast)) != 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_d_ex(&t1, b, &t2, fast)) != 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/demo/test.c b/demo/test.c
index 8c67e25..8627bf2 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -787,8 +787,8 @@ static int test_mp_sqrt(void)
 {
    int i, n;
 
-   mp_int a, b, c, d;
-   if (mp_init_multi(&a, &b, &c, &d, NULL)!= MP_OKAY) {
+   mp_int a, b, c;
+   if (mp_init_multi(&a, &b, &c, NULL)!= MP_OKAY) {
       return EXIT_FAILURE;
    }
 
@@ -801,22 +801,17 @@ static int test_mp_sqrt(void)
          printf("\nmp_sqrt() error!");
          goto LBL_ERR;
       }
-      mp_n_root_ex(&a, 2uL, &c, 0);
-      mp_n_root_ex(&a, 2uL, &d, 1);
-      if (mp_cmp_mag(&c, &d) != MP_EQ) {
-         printf("\nmp_n_root_ex() bad result!");
-         goto LBL_ERR;
-      }
+      mp_n_root(&a, 2uL, &c);
       if (mp_cmp_mag(&b, &c) != MP_EQ) {
          printf("mp_sqrt() bad result!\n");
          goto LBL_ERR;
       }
    }
 
-   mp_clear_multi(&a, &b, &c, &d, NULL);
+   mp_clear_multi(&a, &b, &c, NULL);
    return EXIT_SUCCESS;
 LBL_ERR:
-   mp_clear_multi(&a, &b, &c, &d, NULL);
+   mp_clear_multi(&a, &b, &c, NULL);
    return EXIT_FAILURE;
 }
 
diff --git a/doc/bn.tex b/doc/bn.tex
index a183729..36daf8c 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -1735,29 +1735,11 @@ int 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\_d\_ex}
-\begin{alltt}
-int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
-\end{alltt}
-This function computes $c = a^b$.
-
-With parameter \textit{fast} set to $0$ the old version of the algorithm is used,
-when \textit{fast} is $1$, a faster but not statically timed version of the algorithm is used.
-
-The old version uses a simple binary left-to-right algorithm.
-It is faster than repeated multiplications by $a$ for all values of $b$ greater than three.
-
-The new version uses a binary right-to-left algorithm.
-
-The difference between the old and the new version is that the old version always
-executes $DIGIT\_BIT$ iterations. The new algorithm executes only $n$ iterations
-where $n$ is equal to the position of the highest bit that is set in $b$.
-
 \index{mp\_expt\_d}
 \begin{alltt}
 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
 \end{alltt}
-mp\_expt\_d(a, b, c) is a wrapper function to mp\_expt\_d\_ex(a, b, c, 0).
+This function computes $c = a^b$.
 
 \section{Modular Exponentiation}
 \index{mp\_exptmod}
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 40948e9..6e73c7f 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -441,10 +441,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_expt_d_ex.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_exptmod.c"
 			>
 		</File>
@@ -581,10 +577,6 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_mp_n_root_ex.c"
-			>
-		</File>
-		<File
 			RelativePath="bn_mp_neg.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index 74c557c..d7619f2 100644
--- a/makefile
+++ b/makefile
@@ -31,13 +31,13 @@ bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp
 bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
 bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
 bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
-bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
-bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
-bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
-bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_grow.o \
+bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o \
+bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
 bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
-bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
-bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_neg.o bn_mp_or.o \
+bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
 bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
 bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
 bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
diff --git a/makefile.mingw b/makefile.mingw
index 6023ebc..14bc720 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -34,13 +34,13 @@ bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp
 bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
 bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
 bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
-bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
-bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
-bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
-bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_grow.o \
+bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o \
+bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
 bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
-bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
-bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_neg.o bn_mp_or.o \
+bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
 bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
 bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
 bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
diff --git a/makefile.msvc b/makefile.msvc
index ef2b472..2381b17 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -26,13 +26,13 @@ bn_mp_addmod.obj bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi
 bn_mp_cmp_mag.obj bn_mp_cnt_lsb.obj bn_mp_complement.obj bn_mp_copy.obj bn_mp_count_bits.obj bn_mp_decr.obj \
 bn_mp_div.obj bn_mp_div_2.obj bn_mp_div_2d.obj bn_mp_div_3.obj bn_mp_div_d.obj bn_mp_dr_is_modulus.obj \
 bn_mp_dr_reduce.obj bn_mp_dr_setup.obj bn_mp_error_to_string.obj bn_mp_exch.obj bn_mp_export.obj bn_mp_expt_d.obj \
-bn_mp_expt_d_ex.obj bn_mp_exptmod.obj bn_mp_exteuclid.obj bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_gcd.obj \
-bn_mp_get_double.obj bn_mp_grow.obj bn_mp_ilogb.obj bn_mp_import.obj bn_mp_incr.obj bn_mp_init.obj bn_mp_init_copy.obj \
-bn_mp_init_multi.obj bn_mp_init_set.obj bn_mp_init_size.obj bn_mp_invmod.obj bn_mp_is_square.obj bn_mp_iseven.obj \
-bn_mp_isodd.obj bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_lshd.obj bn_mp_mod.obj bn_mp_mod_2d.obj bn_mp_mod_d.obj \
+bn_mp_exptmod.obj bn_mp_exteuclid.obj bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_gcd.obj bn_mp_get_double.obj bn_mp_grow.obj \
+bn_mp_ilogb.obj bn_mp_import.obj bn_mp_incr.obj bn_mp_init.obj bn_mp_init_copy.obj bn_mp_init_multi.obj \
+bn_mp_init_set.obj bn_mp_init_size.obj bn_mp_invmod.obj bn_mp_is_square.obj bn_mp_iseven.obj bn_mp_isodd.obj \
+bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_lshd.obj bn_mp_mod.obj bn_mp_mod_2d.obj bn_mp_mod_d.obj \
 bn_mp_montgomery_calc_normalization.obj bn_mp_montgomery_reduce.obj bn_mp_montgomery_setup.obj bn_mp_mul.obj \
-bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj bn_mp_n_root.obj bn_mp_n_root_ex.obj bn_mp_neg.obj \
-bn_mp_or.obj bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_prime.obj \
+bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj bn_mp_n_root.obj bn_mp_neg.obj bn_mp_or.obj \
+bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_prime.obj \
 bn_mp_prime_miller_rabin.obj bn_mp_prime_next_prime.obj bn_mp_prime_rabin_miller_trials.obj \
 bn_mp_prime_rand.obj bn_mp_prime_strong_lucas_selfridge.obj bn_mp_radix_size.obj bn_mp_radix_smap.obj \
 bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj bn_mp_read_unsigned_bin.obj bn_mp_reduce.obj \
diff --git a/makefile.shared b/makefile.shared
index fce7fab..b798371 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -28,13 +28,13 @@ bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp
 bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
 bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
 bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
-bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
-bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
-bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
-bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_grow.o \
+bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o \
+bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
 bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
-bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
-bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_neg.o bn_mp_or.o \
+bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
 bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
 bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
 bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
diff --git a/makefile.unix b/makefile.unix
index 942e37a..e545565 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -35,13 +35,13 @@ bn_mp_addmod.o bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp
 bn_mp_cmp_mag.o bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o \
 bn_mp_div.o bn_mp_div_2.o bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o \
 bn_mp_dr_reduce.o bn_mp_dr_setup.o bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_d.o \
-bn_mp_expt_d_ex.o bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o \
-bn_mp_get_double.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o \
-bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o \
-bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
+bn_mp_exptmod.o bn_mp_exteuclid.o bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_grow.o \
+bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_multi.o \
+bn_mp_init_set.o bn_mp_init_size.o bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o \
+bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o \
 bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o bn_mp_mul.o \
-bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_n_root_ex.o bn_mp_neg.o \
-bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
+bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_n_root.o bn_mp_neg.o bn_mp_or.o \
+bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \
 bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \
 bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \
 bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \
diff --git a/tommath.h b/tommath.h
index 388de26..8e68c4e 100644
--- a/tommath.h
+++ b/tommath.h
@@ -468,7 +468,7 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
 
 /* c = a**b */
 mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
-mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
+MP_DEPRECATED(mp_expt_d) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
 
 /* c = a mod b, 0 <= c < b  */
 mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
@@ -504,7 +504,7 @@ mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
  * returns error if a < 0 and b is even
  */
 mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
-mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
+MP_DEPRECATED(mp_n_root_ex) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
 
 /* special sqrt algo */
 mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
diff --git a/tommath_class.h b/tommath_class.h
index 93d1782..90c27e8 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -42,7 +42,6 @@
 #   define BN_MP_EXCH_C
 #   define BN_MP_EXPORT_C
 #   define BN_MP_EXPT_D_C
-#   define BN_MP_EXPT_D_EX_C
 #   define BN_MP_EXPTMOD_C
 #   define BN_MP_EXTEUCLID_C
 #   define BN_MP_FREAD_C
@@ -77,7 +76,6 @@
 #   define BN_MP_MUL_D_C
 #   define BN_MP_MULMOD_C
 #   define BN_MP_N_ROOT_C
-#   define BN_MP_N_ROOT_EX_C
 #   define BN_MP_NEG_C
 #   define BN_MP_OR_C
 #   define BN_MP_PRIME_FERMAT_C
@@ -225,6 +223,10 @@
 #   define BN_MP_GET_LONG_LONG_C
 #   define BN_MP_PRIME_IS_DIVISIBLE_C
 #   define BN_S_MP_PRIME_IS_DIVISIBLE_C
+#   define BN_MP_EXPT_D_EX_C
+#   define BN_MP_EXPT_D_C
+#   define BN_MP_N_ROOT_EX_C
+#   define BN_MP_N_ROOT_C
 #endif
 
 #if defined(BN_MP_2EXPT_C)
@@ -384,10 +386,6 @@
 #endif
 
 #if defined(BN_MP_EXPT_D_C)
-#   define BN_MP_EXPT_D_EX_C
-#endif
-
-#if defined(BN_MP_EXPT_D_EX_C)
 #   define BN_MP_INIT_COPY_C
 #   define BN_MP_SET_C
 #   define BN_MP_MUL_C
@@ -616,16 +614,12 @@
 #endif
 
 #if defined(BN_MP_N_ROOT_C)
-#   define BN_MP_N_ROOT_EX_C
-#endif
-
-#if defined(BN_MP_N_ROOT_EX_C)
 #   define BN_MP_INIT_MULTI_C
 #   define BN_MP_COUNT_BITS_C
 #   define BN_MP_SET_C
 #   define BN_MP_2EXPT_C
 #   define BN_MP_COPY_C
-#   define BN_MP_EXPT_D_EX_C
+#   define BN_MP_EXPT_D_C
 #   define BN_MP_MUL_C
 #   define BN_MP_SUB_C
 #   define BN_MP_MUL_D_C