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.
5 * This file is part of GnuPG.
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.
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.
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
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.
30 #include "mpi-internal.h"
33 * Add the unsigned integer V to the mpi-integer U and store the
34 * result in W. U and V may be the same.
37 mpi_add_ui(MPI w, const MPI u, unsigned long v )
40 mpi_size_t usize, wsize;
47 /* If not space for W (and possible carry), increase space. */
49 if( w->alloced < wsize )
50 if (mpi_resize(w, wsize) < 0)
53 /* These must be after realloc (U may be the same as W). */
57 if( !usize ) { /* simple */
61 else if( !usign ) { /* mpi is not negative */
63 cy = mpihelp_add_1(wp, up, usize, v);
67 else { /* The signs are different. Need exact comparison to determine
68 * which operand to subtract from which. */
69 if( usize == 1 && up[0] < v ) {
74 mpihelp_sub_1(wp, up, usize, v);
75 /* Size can decrease with at most one limb. */
76 wsize = usize - (wp[usize-1]==0);
88 mpi_add(MPI w, MPI u, MPI v)
91 mpi_size_t usize, vsize, wsize;
92 int usign, vsign, wsign;
94 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
100 if (RESIZE_IF_NEEDED(w, wsize) < 0)
102 /* These must be after realloc (u or v may be the same as w). */
112 if (RESIZE_IF_NEEDED(w, wsize) < 0)
114 /* These must be after realloc (u or v may be the same as w). */
121 if( !vsize ) { /* simple */
122 MPN_COPY(wp, up, usize );
126 else if( usign != vsign ) { /* different sign */
127 /* This test is right since USIZE >= VSIZE */
128 if( usize != vsize ) {
129 mpihelp_sub(wp, up, usize, vp, vsize);
131 MPN_NORMALIZE(wp, wsize);
134 else if( mpihelp_cmp(up, vp, usize) < 0 ) {
135 mpihelp_sub_n(wp, vp, up, usize);
137 MPN_NORMALIZE(wp, wsize);
142 mpihelp_sub_n(wp, up, vp, usize);
144 MPN_NORMALIZE(wp, wsize);
149 else { /* U and V have same sign. Add them. */
150 mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
164 * Subtract the unsigned integer V from the mpi-integer U and store the
168 mpi_sub_ui(MPI w, MPI u, unsigned long v )
171 mpi_size_t usize, wsize;
178 /* If not space for W (and possible carry), increase space. */
180 if( w->alloced < wsize )
181 if (mpi_resize(w, wsize) < 0)
184 /* These must be after realloc (U may be the same as W). */
188 if( !usize ) { /* simple */
193 else if( usign ) { /* mpi and v are negative */
195 cy = mpihelp_add_1(wp, up, usize, v);
199 else { /* The signs are different. Need exact comparison to determine
200 * which operand to subtract from which. */
201 if( usize == 1 && up[0] < v ) {
207 mpihelp_sub_1(wp, up, usize, v);
208 /* Size can decrease with at most one limb. */
209 wsize = usize - (wp[usize-1]==0);
219 mpi_sub(MPI w, MPI u, MPI v)
225 if (mpi_copy(&vv, v) < 0)
227 vv->sign = !vv->sign;
228 rc = mpi_add( w, u, vv );
232 /* fixme: this is not thread-save (we temp. modify v) */
234 rc = mpi_add( w, u, v );
242 mpi_addm( MPI w, MPI u, MPI v, MPI m)
244 if (mpi_add(w, u, v) < 0 ||
245 mpi_fdiv_r( w, w, m ) < 0)
251 mpi_subm( MPI w, MPI u, MPI v, MPI m)
253 if (mpi_sub(w, u, v) < 0 ||
254 mpi_fdiv_r( w, w, m ) < 0)