Merge pull request #388 from libtom/various-fixes Various fixes + split log_u32
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
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 fb9dd5a..43748e5 100644
--- a/mp_log_u32.c
+++ b/mp_log_u32.c
@@ -3,82 +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;
-}
-
-/* TODO: output could be "int" because the output of mp_radix_size is int, too,
- as is the output of mp_bitcount.
- With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only!
-*/
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;
}
@@ -102,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/mp_prime_is_prime.c b/mp_prime_is_prime.c
index 75d44c5..1a61bb6 100644
--- a/mp_prime_is_prime.c
+++ b/mp_prime_is_prime.c
@@ -100,11 +100,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
*/
#ifndef LTM_USE_ONLY_MR
if (t >= 0) {
- /*
- * Use a Frobenius-Underwood test instead of the Lucas-Selfridge test for
- * MP_8BIT (It is unknown if the Lucas-Selfridge test works with 16-bit
- * integers but the necesssary analysis is on the todo-list).
- */
#ifdef LTM_USE_FROBENIUS_TEST
err = mp_prime_frobenius_underwood(a, &res);
if ((err != MP_OKAY) && (err != MP_ITER)) {
diff --git a/mp_prime_strong_lucas_selfridge.c b/mp_prime_strong_lucas_selfridge.c
index 6934336..895a481 100644
--- a/mp_prime_strong_lucas_selfridge.c
+++ b/mp_prime_strong_lucas_selfridge.c
@@ -52,7 +52,6 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
{
/* CZ TODO: choose better variable names! */
mp_int Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz;
- /* CZ TODO: Some of them need the full 32 bit, hence the (temporary) exclusion of MP_8BIT */
int32_t D, Ds, J, sign, P, Q, r, s, u, Nbits;
mp_err err;
mp_bool oddness;
diff --git a/mp_sqrtmod_prime.c b/mp_sqrtmod_prime.c
index bf72005..03ebf8a 100644
--- a/mp_sqrtmod_prime.c
+++ b/mp_sqrtmod_prime.c
@@ -59,7 +59,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
}
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
- mp_set_u32(&Z, 2u);
+ mp_set(&Z, 2u);
/* Z = 2 */
for (;;) {
if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
@@ -79,7 +79,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
/* T = n ^ Q mod prime */
if ((err = mp_copy(&S, &M)) != MP_OKAY) goto cleanup;
/* M = S */
- mp_set_u32(&two, 2u);
+ mp_set(&two, 2u);
for (;;) {
if ((err = mp_copy(&T, &t1)) != MP_OKAY) goto cleanup;
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.h b/tommath.h
index 29e9b20..0aac377 100644
--- a/tommath.h
+++ b/tommath.h
@@ -6,23 +6,11 @@
#include <stdint.h>
#include <stddef.h>
-#ifdef MP_8BIT
-# error "Support of 8-bit architectures has been dropped in this version of LTM."
-#endif
-
#ifndef MP_NO_FILE
# include <stdio.h>
#endif
-#ifdef MP_8BIT
-# ifdef _MSC_VER
-# pragma message("8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version.")
-# else
-# warning "8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version."
-# endif
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/tommath_class.h b/tommath_class.h
index 52c5e45..f7812da 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)
@@ -951,7 +945,6 @@
# define MP_MOD_D_C
# define MP_MULMOD_C
# define MP_SET_C
-# define MP_SET_U32_C
# define MP_SQRMOD_C
# define MP_SUB_D_C
# define MP_ZERO_C
@@ -1121,6 +1114,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 c44c72a..e285abc 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -168,8 +168,6 @@ typedef private_mp_word mp_word;
#ifndef MP_PREC
# ifndef MP_LOW_MEM
# define MP_PREC 32 /* default digits of precision */
-# elif defined(MP_8BIT)
-# define MP_PREC 16 /* default digits of precision */
# else
# define MP_PREC 8 /* default digits of precision */
# endif
@@ -208,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;
@@ -219,11 +219,7 @@ extern MP_PRIVATE const uint8_t s_mp_rmap_reverse[];
extern MP_PRIVATE const mp_digit s_mp_prime_tab[];
/* number of primes */
-#ifdef MP_8BIT
-# define MP_PRIME_TAB_SIZE 31
-#else
-# define MP_PRIME_TAB_SIZE 256
-#endif
+#define MP_PRIME_TAB_SIZE 256
#define MP_GET_ENDIANNESS(x) \
do{\