bn_mp_2expt.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <tommath.h>
  2. #ifdef BN_MP_2EXPT_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  5. * LibTomMath is a library that provides multiple-precision
  6. * integer arithmetic as well as number theoretic functionality.
  7. *
  8. * The library was designed directly after the MPI library by
  9. * Michael Fromberger but has been written from scratch with
  10. * additional optimizations in place.
  11. *
  12. * The library is free for all purposes without any express
  13. * guarantee it works.
  14. *
  15. * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
  16. */
  17. /* computes a = 2**b
  18. *
  19. * Simple algorithm which zeroes the int, grows it then just sets one bit
  20. * as required.
  21. */
  22. int
  23. mp_2expt (mp_int * a, int b)
  24. {
  25. int res;
  26. /* zero a as per default */
  27. mp_zero (a);
  28. /* grow a to accomodate the single bit */
  29. if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
  30. return res;
  31. }
  32. /* set the used count of where the bit will go */
  33. a->used = b / DIGIT_BIT + 1;
  34. /* put the single bit in its place */
  35. a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
  36. return MP_OKAY;
  37. }
  38. #endif
  39. /* $Source: /cvs/libtom/libtommath/bn_mp_2expt.c,v $ */
  40. /* $Revision: 1.3 $ */
  41. /* $Date: 2006/03/31 14:18:44 $ */