Commit f0c83aea6c397115f3ec3c28c4c1b35eeee5c80e

Daniel Mendler 2019-10-19T19:25:55

split mp_log_u32 for more configurability

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
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index ccbd1cc..d59f71c 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -881,6 +881,14 @@
 			>
 		</File>
 		<File
+			RelativePath="s_mp_log.c"
+			>
+		</File>
+		<File
+			RelativePath="s_mp_log_d.c"
+			>
+		</File>
+		<File
 			RelativePath="s_mp_montgomery_reduce_fast.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index 117c846..fb9c391 100644
--- a/makefile
+++ b/makefile
@@ -46,9 +46,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si
 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_balance_mul.o s_mp_exptmod.o \
 s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \
-s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \
-s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
-s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \
+s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \
+s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \
+s_mp_toom_mul.o s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.mingw b/makefile.mingw
index 1a222f0..c3a680f 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -49,9 +49,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si
 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_balance_mul.o s_mp_exptmod.o \
 s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \
-s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \
-s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
-s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \
+s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \
+s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \
+s_mp_toom_mul.o s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
diff --git a/makefile.msvc b/makefile.msvc
index 35215b2..84dce85 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -41,9 +41,10 @@ mp_set_ll.obj mp_set_u32.obj mp_set_u64.obj mp_set_ul.obj mp_set_ull.obj mp_shri
 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_balance_mul.obj s_mp_exptmod.obj \
 s_mp_exptmod_fast.obj s_mp_get_bit.obj s_mp_invmod_fast.obj s_mp_invmod_slow.obj s_mp_karatsuba_mul.obj \
-s_mp_karatsuba_sqr.obj s_mp_montgomery_reduce_fast.obj s_mp_mul_digs.obj s_mp_mul_digs_fast.obj \
-s_mp_mul_high_digs.obj s_mp_mul_high_digs_fast.obj s_mp_prime_is_divisible.obj s_mp_rand_jenkins.obj \
-s_mp_rand_platform.obj s_mp_reverse.obj s_mp_sqr.obj s_mp_sqr_fast.obj s_mp_sub.obj s_mp_toom_mul.obj s_mp_toom_sqr.obj
+s_mp_karatsuba_sqr.obj s_mp_log.obj s_mp_log_d.obj s_mp_montgomery_reduce_fast.obj s_mp_mul_digs.obj \
+s_mp_mul_digs_fast.obj s_mp_mul_high_digs.obj s_mp_mul_high_digs_fast.obj s_mp_prime_is_divisible.obj \
+s_mp_rand_jenkins.obj s_mp_rand_platform.obj s_mp_reverse.obj s_mp_sqr.obj s_mp_sqr_fast.obj s_mp_sub.obj \
+s_mp_toom_mul.obj s_mp_toom_sqr.obj
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
diff --git a/makefile.shared b/makefile.shared
index 4bfa019..336d648 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -43,9 +43,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si
 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_balance_mul.o s_mp_exptmod.o \
 s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \
-s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \
-s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
-s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \
+s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \
+s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \
+s_mp_toom_mul.o s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.unix b/makefile.unix
index 4b7c49f..6cad210 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -50,9 +50,10 @@ mp_set_ll.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_set_ull.o mp_shrink.o mp_si
 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_balance_mul.o s_mp_exptmod.o \
 s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod_fast.o s_mp_invmod_slow.o s_mp_karatsuba_mul.o \
-s_mp_karatsuba_sqr.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o s_mp_mul_digs_fast.o \
-s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o s_mp_rand_jenkins.o \
-s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o s_mp_toom_mul.o s_mp_toom_sqr.o
+s_mp_karatsuba_sqr.o s_mp_log.o s_mp_log_d.o s_mp_montgomery_reduce_fast.o s_mp_mul_digs.o \
+s_mp_mul_digs_fast.o s_mp_mul_high_digs.o s_mp_mul_high_digs_fast.o s_mp_prime_is_divisible.o \
+s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_reverse.o s_mp_sqr.o s_mp_sqr_fast.o s_mp_sub.o \
+s_mp_toom_mul.o s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h
 HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
diff --git a/mp_log_u32.c b/mp_log_u32.c
index 0523f7b..43748e5 100644
--- a/mp_log_u32.c
+++ b/mp_log_u32.c
@@ -3,78 +3,8 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* Compute log_{base}(a) */
-static mp_word s_pow(mp_word base, mp_word exponent)
-{
-   mp_word result = 1uLL;
-   while (exponent != 0u) {
-      if ((exponent & 1u) == 1u) {
-         result *= base;
-      }
-      exponent >>= 1;
-      base *= base;
-   }
-
-   return result;
-}
-
-static mp_digit s_digit_ilogb(mp_digit base, mp_digit n)
-{
-   mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N;
-   mp_digit ret, high = 1uL, low = 0uL, mid;
-
-   if (n < base) {
-      return 0uL;
-   }
-   if (n == base) {
-      return 1uL;
-   }
-
-   bracket_high = (mp_word) base ;
-   N = (mp_word) n;
-
-   while (bracket_high < N) {
-      low = high;
-      bracket_low = bracket_high;
-      high <<= 1;
-      bracket_high *= bracket_high;
-   }
-
-   while (((mp_digit)(high - low)) > 1uL) {
-      mid = (low + high) >> 1;
-      bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
-
-      if (N < bracket_mid) {
-         high = mid ;
-         bracket_high = bracket_mid ;
-      }
-      if (N > bracket_mid) {
-         low = mid ;
-         bracket_low = bracket_mid ;
-      }
-      if (N == bracket_mid) {
-         return (mp_digit) mid;
-      }
-   }
-
-   if (bracket_high == N) {
-      ret = high;
-   } else {
-      ret = low;
-   }
-
-   return ret;
-}
-
 mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
 {
-   mp_err err;
-   mp_ord cmp;
-   uint32_t high, low, mid;
-   mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
-
-   err = MP_OKAY;
-
    if (a->sign == MP_NEG) {
       return MP_VAL;
    }
@@ -98,79 +28,16 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
       return MP_OKAY;
    }
 
-   if (a->used == 1) {
-      *c = (uint32_t)s_digit_ilogb(base, a->dp[0]);
-      return err;
-   }
-
-   cmp = mp_cmp_d(a, base);
-   if ((cmp == MP_LT) || (cmp == MP_EQ)) {
-      *c = cmp == MP_EQ;
-      return err;
-   }
-
-   if ((err =
-           mp_init_multi(&bracket_low, &bracket_high,
-                         &bracket_mid, &t, &bi_base, NULL)) != MP_OKAY) {
-      return err;
-   }
-
-   low = 0u;
-   mp_set(&bracket_low, 1uL);
-   high = 1u;
-
-   mp_set(&bracket_high, base);
-
-   /*
-       A kind of Giant-step/baby-step algorithm.
-       Idea shamelessly stolen from https://programmingpraxis.com/2010/05/07/integer-logarithms/2/
-       The effect is asymptotic, hence needs benchmarks to test if the Giant-step should be skipped
-       for small n.
-    */
-   while (mp_cmp(&bracket_high, a) == MP_LT) {
-      low = high;
-      if ((err = mp_copy(&bracket_high, &bracket_low)) != MP_OKAY) {
-         goto LBL_ERR;
-      }
-      high <<= 1;
-      if ((err = mp_sqr(&bracket_high, &bracket_high)) != MP_OKAY) {
-         goto LBL_ERR;
-      }
+   if (MP_HAS(S_MP_LOG_D) && a->used == 1) {
+      *c = (uint32_t)s_mp_log_d(base, a->dp[0]);
+      return MP_OKAY;
    }
-   mp_set(&bi_base, base);
 
-   while ((high - low) > 1u) {
-      mid = (high + low) >> 1;
-
-      if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) {
-         goto LBL_ERR;
-      }
-      if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) {
-         goto LBL_ERR;
-      }
-      cmp = mp_cmp(a, &bracket_mid);
-      if (cmp == MP_LT) {
-         high = mid;
-         mp_exch(&bracket_mid, &bracket_high);
-      }
-      if (cmp == MP_GT) {
-         low = mid;
-         mp_exch(&bracket_mid, &bracket_low);
-      }
-      if (cmp == MP_EQ) {
-         *c = mid;
-         goto LBL_END;
-      }
+   if (MP_HAS(S_MP_LOG)) {
+      return s_mp_log(a, base, c);
    }
 
-   *c = (mp_cmp(&bracket_high, a) == MP_EQ) ? high : low;
-
-LBL_END:
-LBL_ERR:
-   mp_clear_multi(&bracket_low, &bracket_high, &bracket_mid,
-                  &t, &bi_base, NULL);
-   return err;
+   return MP_VAL;
 }
 
-
 #endif
diff --git a/s_mp_log.c b/s_mp_log.c
new file mode 100644
index 0000000..eba279e
--- /dev/null
+++ b/s_mp_log.c
@@ -0,0 +1,82 @@
+#include "tommath_private.h"
+#ifdef S_MP_LOG_C
+/* 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 err;
+   mp_ord cmp;
+   uint32_t high, low, mid;
+   mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
+
+   cmp = mp_cmp_d(a, base);
+   if ((cmp == MP_LT) || (cmp == MP_EQ)) {
+      *c = cmp == MP_EQ;
+      return MP_OKAY;
+   }
+
+   if ((err =
+           mp_init_multi(&bracket_low, &bracket_high,
+                         &bracket_mid, &t, &bi_base, NULL)) != MP_OKAY) {
+      return err;
+   }
+
+   low = 0u;
+   mp_set(&bracket_low, 1uL);
+   high = 1u;
+
+   mp_set(&bracket_high, base);
+
+   /*
+       A kind of Giant-step/baby-step algorithm.
+       Idea shamelessly stolen from https://programmingpraxis.com/2010/05/07/integer-logarithms/2/
+       The effect is asymptotic, hence needs benchmarks to test if the Giant-step should be skipped
+       for small n.
+    */
+   while (mp_cmp(&bracket_high, a) == MP_LT) {
+      low = high;
+      if ((err = mp_copy(&bracket_high, &bracket_low)) != MP_OKAY) {
+         goto LBL_END;
+      }
+      high <<= 1;
+      if ((err = mp_sqr(&bracket_high, &bracket_high)) != MP_OKAY) {
+         goto LBL_END;
+      }
+   }
+   mp_set(&bi_base, base);
+
+   while ((high - low) > 1u) {
+      mid = (high + low) >> 1;
+
+      if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) {
+         goto LBL_END;
+      }
+      if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) {
+         goto LBL_END;
+      }
+      cmp = mp_cmp(a, &bracket_mid);
+      if (cmp == MP_LT) {
+         high = mid;
+         mp_exch(&bracket_mid, &bracket_high);
+      }
+      if (cmp == MP_GT) {
+         low = mid;
+         mp_exch(&bracket_mid, &bracket_low);
+      }
+      if (cmp == MP_EQ) {
+         *c = mid;
+         goto LBL_END;
+      }
+   }
+
+   *c = (mp_cmp(&bracket_high, a) == MP_EQ) ? high : low;
+
+LBL_END:
+   mp_clear_multi(&bracket_low, &bracket_high, &bracket_mid,
+                  &t, &bi_base, NULL);
+   return err;
+}
+
+
+#endif
diff --git a/s_mp_log_d.c b/s_mp_log_d.c
new file mode 100644
index 0000000..44edd07
--- /dev/null
+++ b/s_mp_log_d.c
@@ -0,0 +1,68 @@
+#include "tommath_private.h"
+#ifdef S_MP_LOG_D_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+static mp_word s_pow(mp_word base, mp_word exponent)
+{
+   mp_word result = 1uLL;
+   while (exponent != 0u) {
+      if ((exponent & 1u) == 1u) {
+         result *= base;
+      }
+      exponent >>= 1;
+      base *= base;
+   }
+
+   return result;
+}
+
+mp_digit s_mp_log_d(mp_digit base, mp_digit n)
+{
+   mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N;
+   mp_digit ret, high = 1uL, low = 0uL, mid;
+
+   if (n < base) {
+      return 0uL;
+   }
+   if (n == base) {
+      return 1uL;
+   }
+
+   bracket_high = (mp_word) base ;
+   N = (mp_word) n;
+
+   while (bracket_high < N) {
+      low = high;
+      bracket_low = bracket_high;
+      high <<= 1;
+      bracket_high *= bracket_high;
+   }
+
+   while (((mp_digit)(high - low)) > 1uL) {
+      mid = (low + high) >> 1;
+      bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
+
+      if (N < bracket_mid) {
+         high = mid ;
+         bracket_high = bracket_mid ;
+      }
+      if (N > bracket_mid) {
+         low = mid ;
+         bracket_low = bracket_mid ;
+      }
+      if (N == bracket_mid) {
+         return (mp_digit) mid;
+      }
+   }
+
+   if (bracket_high == N) {
+      ret = high;
+   } else {
+      ret = low;
+   }
+
+   return ret;
+}
+
+#endif
diff --git a/tommath_class.h b/tommath_class.h
index 52c5e45..9b7f075 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -153,6 +153,8 @@
 #   define S_MP_INVMOD_SLOW_C
 #   define S_MP_KARATSUBA_MUL_C
 #   define S_MP_KARATSUBA_SQR_C
+#   define S_MP_LOG_C
+#   define S_MP_LOG_D_C
 #   define S_MP_MONTGOMERY_REDUCE_FAST_C
 #   define S_MP_MUL_DIGS_C
 #   define S_MP_MUL_DIGS_FAST_C
@@ -529,17 +531,9 @@
 #endif
 
 #if defined(MP_LOG_U32_C)
-#   define MP_CLEAR_MULTI_C
-#   define MP_CMP_C
-#   define MP_CMP_D_C
-#   define MP_COPY_C
 #   define MP_COUNT_BITS_C
-#   define MP_EXCH_C
-#   define MP_EXPT_U32_C
-#   define MP_INIT_MULTI_C
-#   define MP_MUL_C
-#   define MP_SET_C
-#   define MP_SQR_C
+#   define S_MP_LOG_C
+#   define S_MP_LOG_D_C
 #endif
 
 #if defined(MP_LSHD_C)
@@ -1121,6 +1115,22 @@
 #   define S_MP_SUB_C
 #endif
 
+#if defined(S_MP_LOG_C)
+#   define MP_CLEAR_MULTI_C
+#   define MP_CMP_C
+#   define MP_CMP_D_C
+#   define MP_COPY_C
+#   define MP_EXCH_C
+#   define MP_EXPT_U32_C
+#   define MP_INIT_MULTI_C
+#   define MP_MUL_C
+#   define MP_SET_C
+#   define MP_SQR_C
+#endif
+
+#if defined(S_MP_LOG_D_C)
+#endif
+
 #if defined(S_MP_MONTGOMERY_REDUCE_FAST_C)
 #   define MP_CLAMP_C
 #   define MP_CMP_MAG_C
diff --git a/tommath_private.h b/tommath_private.h
index 45e0930..e285abc 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -206,6 +206,8 @@ typedef int mp_prime_callback(unsigned char *dst, int len, void *dat);
 MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, mp_prime_callback cb, void *dat);
 MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len);
 MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
+MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n);
+MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c);
 
 /* TODO: jenkins prng is not thread safe as of now */
 MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;