bn_mp_mod_2d.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <tommath.h>
  2. #ifdef BN_MP_MOD_2D_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. /* calc a value mod 2**b */
  18. int
  19. mp_mod_2d (mp_int * a, int b, mp_int * c)
  20. {
  21. int x, res;
  22. /* if b is <= 0 then zero the int */
  23. if (b <= 0) {
  24. mp_zero (c);
  25. return MP_OKAY;
  26. }
  27. /* if the modulus is larger than the value than return */
  28. if (b >= (int) (a->used * DIGIT_BIT)) {
  29. res = mp_copy (a, c);
  30. return res;
  31. }
  32. /* copy */
  33. if ((res = mp_copy (a, c)) != MP_OKAY) {
  34. return res;
  35. }
  36. /* zero digits above the last digit of the modulus */
  37. for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) {
  38. c->dp[x] = 0;
  39. }
  40. /* clear the digit that is not completely outside/inside the modulus */
  41. c->dp[b / DIGIT_BIT] &=
  42. (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
  43. mp_clamp (c);
  44. return MP_OKAY;
  45. }
  46. #endif
  47. /* $Source: /cvs/libtom/libtommath/bn_mp_mod_2d.c,v $ */
  48. /* $Revision: 1.3 $ */
  49. /* $Date: 2006/03/31 14:18:44 $ */