mpi-add.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* mpi-add.c - MPI functions
  2. * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  3. * Copyright (C) 1994, 1996 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. /****************
  31. * Add the unsigned integer V to the mpi-integer U and store the
  32. * result in W. U and V may be the same.
  33. */
  34. int mpi_add_ui(MPI w, const MPI u, unsigned long v)
  35. {
  36. mpi_ptr_t wp, up;
  37. mpi_size_t usize, wsize;
  38. int usign, wsign;
  39. usize = u->nlimbs;
  40. usign = u->sign;
  41. wsign = 0;
  42. /* If not space for W (and possible carry), increase space. */
  43. wsize = usize + 1;
  44. if (w->alloced < wsize)
  45. if (mpi_resize(w, wsize) < 0)
  46. return -ENOMEM;
  47. /* These must be after realloc (U may be the same as W). */
  48. up = u->d;
  49. wp = w->d;
  50. if (!usize) { /* simple */
  51. wp[0] = v;
  52. wsize = v ? 1 : 0;
  53. } else if (!usign) { /* mpi is not negative */
  54. mpi_limb_t cy;
  55. cy = mpihelp_add_1(wp, up, usize, v);
  56. wp[usize] = cy;
  57. wsize = usize + cy;
  58. } else { /* The signs are different. Need exact comparison to determine
  59. * which operand to subtract from which. */
  60. if (usize == 1 && up[0] < v) {
  61. wp[0] = v - up[0];
  62. wsize = 1;
  63. } else {
  64. mpihelp_sub_1(wp, up, usize, v);
  65. /* Size can decrease with at most one limb. */
  66. wsize = usize - (wp[usize - 1] == 0);
  67. wsign = 1;
  68. }
  69. }
  70. w->nlimbs = wsize;
  71. w->sign = wsign;
  72. return 0;
  73. }
  74. int mpi_add(MPI w, MPI u, MPI v)
  75. {
  76. mpi_ptr_t wp, up, vp;
  77. mpi_size_t usize, vsize, wsize;
  78. int usign, vsign, wsign;
  79. if (u->nlimbs < v->nlimbs) { /* Swap U and V. */
  80. usize = v->nlimbs;
  81. usign = v->sign;
  82. vsize = u->nlimbs;
  83. vsign = u->sign;
  84. wsize = usize + 1;
  85. if (RESIZE_IF_NEEDED(w, wsize) < 0)
  86. return -ENOMEM;
  87. /* These must be after realloc (u or v may be the same as w). */
  88. up = v->d;
  89. vp = u->d;
  90. } else {
  91. usize = u->nlimbs;
  92. usign = u->sign;
  93. vsize = v->nlimbs;
  94. vsign = v->sign;
  95. wsize = usize + 1;
  96. if (RESIZE_IF_NEEDED(w, wsize) < 0)
  97. return -ENOMEM;
  98. /* These must be after realloc (u or v may be the same as w). */
  99. up = u->d;
  100. vp = v->d;
  101. }
  102. wp = w->d;
  103. wsign = 0;
  104. if (!vsize) { /* simple */
  105. MPN_COPY(wp, up, usize);
  106. wsize = usize;
  107. wsign = usign;
  108. } else if (usign != vsign) { /* different sign */
  109. /* This test is right since USIZE >= VSIZE */
  110. if (usize != vsize) {
  111. mpihelp_sub(wp, up, usize, vp, vsize);
  112. wsize = usize;
  113. MPN_NORMALIZE(wp, wsize);
  114. wsign = usign;
  115. } else if (mpihelp_cmp(up, vp, usize) < 0) {
  116. mpihelp_sub_n(wp, vp, up, usize);
  117. wsize = usize;
  118. MPN_NORMALIZE(wp, wsize);
  119. if (!usign)
  120. wsign = 1;
  121. } else {
  122. mpihelp_sub_n(wp, up, vp, usize);
  123. wsize = usize;
  124. MPN_NORMALIZE(wp, wsize);
  125. if (usign)
  126. wsign = 1;
  127. }
  128. } else { /* U and V have same sign. Add them. */
  129. mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
  130. wp[usize] = cy;
  131. wsize = usize + cy;
  132. if (usign)
  133. wsign = 1;
  134. }
  135. w->nlimbs = wsize;
  136. w->sign = wsign;
  137. return 0;
  138. }
  139. /****************
  140. * Subtract the unsigned integer V from the mpi-integer U and store the
  141. * result in W.
  142. */
  143. int mpi_sub_ui(MPI w, MPI u, unsigned long v)
  144. {
  145. mpi_ptr_t wp, up;
  146. mpi_size_t usize, wsize;
  147. int usign, wsign;
  148. usize = u->nlimbs;
  149. usign = u->sign;
  150. wsign = 0;
  151. /* If not space for W (and possible carry), increase space. */
  152. wsize = usize + 1;
  153. if (w->alloced < wsize)
  154. if (mpi_resize(w, wsize) < 0)
  155. return -ENOMEM;
  156. /* These must be after realloc (U may be the same as W). */
  157. up = u->d;
  158. wp = w->d;
  159. if (!usize) { /* simple */
  160. wp[0] = v;
  161. wsize = v ? 1 : 0;
  162. wsign = 1;
  163. } else if (usign) { /* mpi and v are negative */
  164. mpi_limb_t cy;
  165. cy = mpihelp_add_1(wp, up, usize, v);
  166. wp[usize] = cy;
  167. wsize = usize + cy;
  168. } else { /* The signs are different. Need exact comparison to determine
  169. * which operand to subtract from which. */
  170. if (usize == 1 && up[0] < v) {
  171. wp[0] = v - up[0];
  172. wsize = 1;
  173. wsign = 1;
  174. } else {
  175. mpihelp_sub_1(wp, up, usize, v);
  176. /* Size can decrease with at most one limb. */
  177. wsize = usize - (wp[usize - 1] == 0);
  178. }
  179. }
  180. w->nlimbs = wsize;
  181. w->sign = wsign;
  182. return 0;
  183. }
  184. int mpi_sub(MPI w, MPI u, MPI v)
  185. {
  186. int rc;
  187. if (w == v) {
  188. MPI vv;
  189. if (mpi_copy(&vv, v) < 0)
  190. return -ENOMEM;
  191. vv->sign = !vv->sign;
  192. rc = mpi_add(w, u, vv);
  193. mpi_free(vv);
  194. } else {
  195. /* fixme: this is not thread-save (we temp. modify v) */
  196. v->sign = !v->sign;
  197. rc = mpi_add(w, u, v);
  198. v->sign = !v->sign;
  199. }
  200. return rc;
  201. }
  202. int mpi_addm(MPI w, MPI u, MPI v, MPI m)
  203. {
  204. if (mpi_add(w, u, v) < 0 || mpi_fdiv_r(w, w, m) < 0)
  205. return -ENOMEM;
  206. return 0;
  207. }
  208. int mpi_subm(MPI w, MPI u, MPI v, MPI m)
  209. {
  210. if (mpi_sub(w, u, v) < 0 || mpi_fdiv_r(w, w, m) < 0)
  211. return -ENOMEM;
  212. return 0;
  213. }