bn_mp_set_int.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <tommath.h>
  2. #ifdef BN_MP_SET_INT_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. /* set a 32-bit const */
  18. int mp_set_int (mp_int * a, unsigned long b)
  19. {
  20. int x, res;
  21. mp_zero (a);
  22. /* set four bits at a time */
  23. for (x = 0; x < 8; x++) {
  24. /* shift the number up four bits */
  25. if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) {
  26. return res;
  27. }
  28. /* OR in the top four bits of the source */
  29. a->dp[0] |= (b >> 28) & 15;
  30. /* shift the source up to the next four bits */
  31. b <<= 4;
  32. /* ensure that digits are not clamped off */
  33. a->used += 1;
  34. }
  35. mp_clamp (a);
  36. return MP_OKAY;
  37. }
  38. #endif
  39. /* $Source: /cvs/libtom/libtommath/bn_mp_set_int.c,v $ */
  40. /* $Revision: 1.3 $ */
  41. /* $Date: 2006/03/31 14:18:44 $ */