bn_mp_2expt.c 749 B

12345678910111213141516171819202122232425262728293031
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_2EXPT_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* computes a = 2**b
  6. *
  7. * Simple algorithm which zeroes the int, grows it then just sets one bit
  8. * as required.
  9. */
  10. mp_err mp_2expt(mp_int *a, int b)
  11. {
  12. mp_err err;
  13. /* zero a as per default */
  14. mp_zero(a);
  15. /* grow a to accomodate the single bit */
  16. if ((err = mp_grow(a, (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
  17. return err;
  18. }
  19. /* set the used count of where the bit will go */
  20. a->used = (b / MP_DIGIT_BIT) + 1;
  21. /* put the single bit in its place */
  22. a->dp[b / MP_DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT);
  23. return MP_OKAY;
  24. }
  25. #endif