bn_mp_sqr.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <tommath.h>
  2. #ifdef BN_MP_SQR_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 b = a*a */
  18. int
  19. mp_sqr (mp_int * a, mp_int * b)
  20. {
  21. int res;
  22. #ifdef BN_MP_TOOM_SQR_C
  23. /* use Toom-Cook? */
  24. if (a->used >= TOOM_SQR_CUTOFF) {
  25. res = mp_toom_sqr(a, b);
  26. /* Karatsuba? */
  27. } else
  28. #endif
  29. #ifdef BN_MP_KARATSUBA_SQR_C
  30. if (a->used >= KARATSUBA_SQR_CUTOFF) {
  31. res = mp_karatsuba_sqr (a, b);
  32. } else
  33. #endif
  34. {
  35. #ifdef BN_FAST_S_MP_SQR_C
  36. /* can we use the fast comba multiplier? */
  37. if ((a->used * 2 + 1) < MP_WARRAY &&
  38. a->used <
  39. (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
  40. res = fast_s_mp_sqr (a, b);
  41. } else
  42. #endif
  43. #ifdef BN_S_MP_SQR_C
  44. res = s_mp_sqr (a, b);
  45. #else
  46. res = MP_VAL;
  47. #endif
  48. }
  49. b->sign = MP_ZPOS;
  50. return res;
  51. }
  52. #endif
  53. /* $Source: /cvs/libtom/libtommath/bn_mp_sqr.c,v $ */
  54. /* $Revision: 1.3 $ */
  55. /* $Date: 2006/03/31 14:18:44 $ */