bn_fast_s_mp_mul_digs.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <tommath.h>
  2. #ifdef BN_FAST_S_MP_MUL_DIGS_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. /* Fast (comba) multiplier
  18. *
  19. * This is the fast column-array [comba] multiplier. It is
  20. * designed to compute the columns of the product first
  21. * then handle the carries afterwards. This has the effect
  22. * of making the nested loops that compute the columns very
  23. * simple and schedulable on super-scalar processors.
  24. *
  25. * This has been modified to produce a variable number of
  26. * digits of output so if say only a half-product is required
  27. * you don't have to compute the upper half (a feature
  28. * required for fast Barrett reduction).
  29. *
  30. * Based on Algorithm 14.12 on pp.595 of HAC.
  31. *
  32. */
  33. int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  34. {
  35. int olduse, res, pa, ix, iz;
  36. mp_digit W[MP_WARRAY];
  37. register mp_word _W;
  38. /* grow the destination as required */
  39. if (c->alloc < digs) {
  40. if ((res = mp_grow (c, digs)) != MP_OKAY) {
  41. return res;
  42. }
  43. }
  44. /* number of output digits to produce */
  45. pa = MIN(digs, a->used + b->used);
  46. /* clear the carry */
  47. _W = 0;
  48. for (ix = 0; ix < pa; ix++) {
  49. int tx, ty;
  50. int iy;
  51. mp_digit *tmpx, *tmpy;
  52. /* get offsets into the two bignums */
  53. ty = MIN(b->used-1, ix);
  54. tx = ix - ty;
  55. /* setup temp aliases */
  56. tmpx = a->dp + tx;
  57. tmpy = b->dp + ty;
  58. /* this is the number of times the loop will iterrate, essentially
  59. while (tx++ < a->used && ty-- >= 0) { ... }
  60. */
  61. iy = MIN(a->used-tx, ty+1);
  62. /* execute loop */
  63. for (iz = 0; iz < iy; ++iz) {
  64. _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
  65. }
  66. /* store term */
  67. W[ix] = ((mp_digit)_W) & MP_MASK;
  68. /* make next carry */
  69. _W = _W >> ((mp_word)DIGIT_BIT);
  70. }
  71. /* setup dest */
  72. olduse = c->used;
  73. c->used = pa;
  74. {
  75. register mp_digit *tmpc;
  76. tmpc = c->dp;
  77. for (ix = 0; ix < pa+1; ix++) {
  78. /* now extract the previous digit [below the carry] */
  79. *tmpc++ = W[ix];
  80. }
  81. /* clear unused digits [that existed in the old copy of c] */
  82. for (; ix < olduse; ix++) {
  83. *tmpc++ = 0;
  84. }
  85. }
  86. mp_clamp (c);
  87. return MP_OKAY;
  88. }
  89. #endif
  90. /* $Source: /cvs/libtom/libtommath/bn_fast_s_mp_mul_digs.c,v $ */
  91. /* $Revision: 1.7 $ */
  92. /* $Date: 2006/03/31 14:18:44 $ */