mpi-mul.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* mpi-mul.c - MPI functions
  2. * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
  3. * Copyright (C) 1998, 2001 Free Software Foundation, Inc.
  4. *
  5. * This file is part of GnuPG.
  6. *
  7. * GnuPG is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GnuPG is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. *
  21. * Note: This code is heavily based on the GNU MP Library.
  22. * Actually it's the same code with only minor changes in the
  23. * way the data is stored; this is to support the abstraction
  24. * of an optional secure memory allocation which may be used
  25. * to avoid revealing of sensitive data due to paging etc.
  26. * The GNU MP Library itself is published under the LGPL;
  27. * however I decided to publish this code under the plain GPL.
  28. */
  29. #include "mpi-internal.h"
  30. int mpi_mul_ui(MPI prod, MPI mult, unsigned long small_mult)
  31. {
  32. mpi_size_t size, prod_size;
  33. mpi_ptr_t prod_ptr;
  34. mpi_limb_t cy;
  35. int sign;
  36. size = mult->nlimbs;
  37. sign = mult->sign;
  38. if (!size || !small_mult) {
  39. prod->nlimbs = 0;
  40. prod->sign = 0;
  41. return 0;
  42. }
  43. prod_size = size + 1;
  44. if (prod->alloced < prod_size)
  45. if (mpi_resize(prod, prod_size) < 0)
  46. return -ENOMEM;
  47. prod_ptr = prod->d;
  48. cy = mpihelp_mul_1(prod_ptr, mult->d, size, (mpi_limb_t) small_mult);
  49. if (cy)
  50. prod_ptr[size++] = cy;
  51. prod->nlimbs = size;
  52. prod->sign = sign;
  53. return 0;
  54. }
  55. int mpi_mul_2exp(MPI w, MPI u, unsigned long cnt)
  56. {
  57. mpi_size_t usize, wsize, limb_cnt;
  58. mpi_ptr_t wp;
  59. mpi_limb_t wlimb;
  60. int usign, wsign;
  61. usize = u->nlimbs;
  62. usign = u->sign;
  63. if (!usize) {
  64. w->nlimbs = 0;
  65. w->sign = 0;
  66. return 0;
  67. }
  68. limb_cnt = cnt / BITS_PER_MPI_LIMB;
  69. wsize = usize + limb_cnt + 1;
  70. if (w->alloced < wsize)
  71. if (mpi_resize(w, wsize) < 0)
  72. return -ENOMEM;
  73. wp = w->d;
  74. wsize = usize + limb_cnt;
  75. wsign = usign;
  76. cnt %= BITS_PER_MPI_LIMB;
  77. if (cnt) {
  78. wlimb = mpihelp_lshift(wp + limb_cnt, u->d, usize, cnt);
  79. if (wlimb) {
  80. wp[wsize] = wlimb;
  81. wsize++;
  82. }
  83. } else {
  84. MPN_COPY_DECR(wp + limb_cnt, u->d, usize);
  85. }
  86. /* Zero all whole limbs at low end. Do it here and not before calling
  87. * mpn_lshift, not to lose for U == W. */
  88. MPN_ZERO(wp, limb_cnt);
  89. w->nlimbs = wsize;
  90. w->sign = wsign;
  91. return 0;
  92. }
  93. int mpi_mul(MPI w, MPI u, MPI v)
  94. {
  95. int rc = -ENOMEM;
  96. mpi_size_t usize, vsize, wsize;
  97. mpi_ptr_t up, vp, wp;
  98. mpi_limb_t cy;
  99. int usign, vsign, sign_product;
  100. int assign_wp = 0;
  101. mpi_ptr_t tmp_limb = NULL;
  102. if (u->nlimbs < v->nlimbs) { /* Swap U and V. */
  103. usize = v->nlimbs;
  104. usign = v->sign;
  105. up = v->d;
  106. vsize = u->nlimbs;
  107. vsign = u->sign;
  108. vp = u->d;
  109. } else {
  110. usize = u->nlimbs;
  111. usign = u->sign;
  112. up = u->d;
  113. vsize = v->nlimbs;
  114. vsign = v->sign;
  115. vp = v->d;
  116. }
  117. sign_product = usign ^ vsign;
  118. wp = w->d;
  119. /* Ensure W has space enough to store the result. */
  120. wsize = usize + vsize;
  121. if (w->alloced < (size_t) wsize) {
  122. if (wp == up || wp == vp) {
  123. wp = mpi_alloc_limb_space(wsize);
  124. if (!wp)
  125. goto nomem;
  126. assign_wp = 1;
  127. } else {
  128. if (mpi_resize(w, wsize) < 0)
  129. goto nomem;
  130. wp = w->d;
  131. }
  132. } else { /* Make U and V not overlap with W. */
  133. if (wp == up) {
  134. /* W and U are identical. Allocate temporary space for U. */
  135. up = tmp_limb = mpi_alloc_limb_space(usize);
  136. if (!up)
  137. goto nomem;
  138. /* Is V identical too? Keep it identical with U. */
  139. if (wp == vp)
  140. vp = up;
  141. /* Copy to the temporary space. */
  142. MPN_COPY(up, wp, usize);
  143. } else if (wp == vp) {
  144. /* W and V are identical. Allocate temporary space for V. */
  145. vp = tmp_limb = mpi_alloc_limb_space(vsize);
  146. if (!vp)
  147. goto nomem;
  148. /* Copy to the temporary space. */
  149. MPN_COPY(vp, wp, vsize);
  150. }
  151. }
  152. if (!vsize)
  153. wsize = 0;
  154. else {
  155. if (mpihelp_mul(wp, up, usize, vp, vsize, &cy) < 0)
  156. goto nomem;
  157. wsize -= cy ? 0 : 1;
  158. }
  159. if (assign_wp)
  160. mpi_assign_limb_space(w, wp, wsize);
  161. w->nlimbs = wsize;
  162. w->sign = sign_product;
  163. rc = 0;
  164. nomem:
  165. if (tmp_limb)
  166. mpi_free_limb_space(tmp_limb);
  167. return rc;
  168. }
  169. int mpi_mulm(MPI w, MPI u, MPI v, MPI m)
  170. {
  171. if (mpi_mul(w, u, v) < 0)
  172. return -ENOMEM;
  173. return mpi_fdiv_r(w, w, m);
  174. }