bn_mp_mul.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <tommath.h>
  2. #ifdef BN_MP_MUL_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. /* high level multiplication (handles sign) */
  18. int mp_mul (mp_int * a, mp_int * b, mp_int * c)
  19. {
  20. int res, neg;
  21. neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
  22. /* use Toom-Cook? */
  23. #ifdef BN_MP_TOOM_MUL_C
  24. if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
  25. res = mp_toom_mul(a, b, c);
  26. } else
  27. #endif
  28. #ifdef BN_MP_KARATSUBA_MUL_C
  29. /* use Karatsuba? */
  30. if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
  31. res = mp_karatsuba_mul (a, b, c);
  32. } else
  33. #endif
  34. {
  35. /* can we use the fast multiplier?
  36. *
  37. * The fast multiplier can be used if the output will
  38. * have less than MP_WARRAY digits and the number of
  39. * digits won't affect carry propagation
  40. */
  41. int digs = a->used + b->used + 1;
  42. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  43. if ((digs < MP_WARRAY) &&
  44. MIN(a->used, b->used) <=
  45. (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  46. res = fast_s_mp_mul_digs (a, b, c, digs);
  47. } else
  48. #endif
  49. #ifdef BN_S_MP_MUL_DIGS_C
  50. res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
  51. #else
  52. res = MP_VAL;
  53. #endif
  54. }
  55. c->sign = (c->used > 0) ? neg : MP_ZPOS;
  56. return res;
  57. }
  58. #endif
  59. /* $Source: /cvs/libtom/libtommath/bn_mp_mul.c,v $ */
  60. /* $Revision: 1.3 $ */
  61. /* $Date: 2006/03/31 14:18:44 $ */