bn_mp_log_u32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_LOG_U32_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* Compute log_{base}(a) */
  6. static mp_word s_pow(mp_word base, mp_word exponent)
  7. {
  8. mp_word result = 1uLL;
  9. while (exponent != 0u) {
  10. if ((exponent & 1u) == 1u) {
  11. result *= base;
  12. }
  13. exponent >>= 1;
  14. base *= base;
  15. }
  16. return result;
  17. }
  18. static mp_digit s_digit_ilogb(mp_digit base, mp_digit n)
  19. {
  20. mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N;
  21. mp_digit ret, high = 1uL, low = 0uL, mid;
  22. if (n < base) {
  23. return 0uL;
  24. }
  25. if (n == base) {
  26. return 1uL;
  27. }
  28. bracket_high = (mp_word) base ;
  29. N = (mp_word) n;
  30. while (bracket_high < N) {
  31. low = high;
  32. bracket_low = bracket_high;
  33. high <<= 1;
  34. bracket_high *= bracket_high;
  35. }
  36. while (((mp_digit)(high - low)) > 1uL) {
  37. mid = (low + high) >> 1;
  38. bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
  39. if (N < bracket_mid) {
  40. high = mid ;
  41. bracket_high = bracket_mid ;
  42. }
  43. if (N > bracket_mid) {
  44. low = mid ;
  45. bracket_low = bracket_mid ;
  46. }
  47. if (N == bracket_mid) {
  48. return (mp_digit) mid;
  49. }
  50. }
  51. if (bracket_high == N) {
  52. ret = high;
  53. } else {
  54. ret = low;
  55. }
  56. return ret;
  57. }
  58. /* TODO: output could be "int" because the output of mp_radix_size is int, too,
  59. as is the output of mp_bitcount.
  60. With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only!
  61. */
  62. mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
  63. {
  64. mp_err err;
  65. mp_ord cmp;
  66. uint32_t high, low, mid;
  67. mp_int bracket_low, bracket_high, bracket_mid, t, bi_base;
  68. err = MP_OKAY;
  69. if (a->sign == MP_NEG) {
  70. return MP_VAL;
  71. }
  72. if (MP_IS_ZERO(a)) {
  73. return MP_VAL;
  74. }
  75. if (base < 2u) {
  76. return MP_VAL;
  77. }
  78. /* A small shortcut for bases that are powers of two. */
  79. if ((base & (base - 1u)) == 0u) {
  80. int y, bit_count;
  81. for (y=0; (y < 7) && ((base & 1u) == 0u); y++) {
  82. base >>= 1;
  83. }
  84. bit_count = mp_count_bits(a) - 1;
  85. *c = (uint32_t)(bit_count/y);
  86. return MP_OKAY;
  87. }
  88. if (a->used == 1) {
  89. *c = (uint32_t)s_digit_ilogb(base, a->dp[0]);
  90. return err;
  91. }
  92. cmp = mp_cmp_d(a, base);
  93. if ((cmp == MP_LT) || (cmp == MP_EQ)) {
  94. *c = cmp == MP_EQ;
  95. return err;
  96. }
  97. if ((err =
  98. mp_init_multi(&bracket_low, &bracket_high,
  99. &bracket_mid, &t, &bi_base, NULL)) != MP_OKAY) {
  100. return err;
  101. }
  102. low = 0u;
  103. mp_set(&bracket_low, 1uL);
  104. high = 1u;
  105. mp_set(&bracket_high, base);
  106. /*
  107. A kind of Giant-step/baby-step algorithm.
  108. Idea shamelessly stolen from https://programmingpraxis.com/2010/05/07/integer-logarithms/2/
  109. The effect is asymptotic, hence needs benchmarks to test if the Giant-step should be skipped
  110. for small n.
  111. */
  112. while (mp_cmp(&bracket_high, a) == MP_LT) {
  113. low = high;
  114. if ((err = mp_copy(&bracket_high, &bracket_low)) != MP_OKAY) {
  115. goto LBL_ERR;
  116. }
  117. high <<= 1;
  118. if ((err = mp_sqr(&bracket_high, &bracket_high)) != MP_OKAY) {
  119. goto LBL_ERR;
  120. }
  121. }
  122. mp_set(&bi_base, base);
  123. while ((high - low) > 1u) {
  124. mid = (high + low) >> 1;
  125. if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) {
  126. goto LBL_ERR;
  127. }
  128. if ((err = mp_mul(&bracket_low, &t, &bracket_mid)) != MP_OKAY) {
  129. goto LBL_ERR;
  130. }
  131. cmp = mp_cmp(a, &bracket_mid);
  132. if (cmp == MP_LT) {
  133. high = mid;
  134. mp_exch(&bracket_mid, &bracket_high);
  135. }
  136. if (cmp == MP_GT) {
  137. low = mid;
  138. mp_exch(&bracket_mid, &bracket_low);
  139. }
  140. if (cmp == MP_EQ) {
  141. *c = mid;
  142. goto LBL_END;
  143. }
  144. }
  145. *c = (mp_cmp(&bracket_high, a) == MP_EQ) ? high : low;
  146. LBL_END:
  147. LBL_ERR:
  148. mp_clear_multi(&bracket_low, &bracket_high, &bracket_mid,
  149. &t, &bi_base, NULL);
  150. return err;
  151. }
  152. #endif