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.
34
#include "mpi-internal.h"
38
* Add the unsigned integer V to the mpi-integer U and store the
39
* result in W. U and V may be the same.
42
mpi_add_ui(MPI w, MPI u, unsigned long v )
45
mpi_size_t usize, wsize;
52
/* If not space for W (and possible carry), increase space. */
54
if( w->alloced < wsize )
57
/* These must be after realloc (U may be the same as W). */
61
if( !usize ) { /* simple */
65
else if( !usign ) { /* mpi is not negative */
67
cy = mpihelp_add_1(wp, up, usize, v);
71
else { /* The signs are different. Need exact comparison to determine
72
* which operand to subtract from which. */
73
if( usize == 1 && up[0] < v ) {
78
mpihelp_sub_1(wp, up, usize, v);
79
/* Size can decrease with at most one limb. */
80
wsize = usize - (wp[usize-1]==0);
91
mpi_add(MPI w, MPI u, MPI v)
94
mpi_size_t usize, vsize, wsize;
95
int usign, vsign, wsign;
97
if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
103
RESIZE_IF_NEEDED(w, wsize);
104
/* These must be after realloc (u or v may be the same as w). */
114
RESIZE_IF_NEEDED(w, wsize);
115
/* These must be after realloc (u or v may be the same as w). */
122
if( !vsize ) { /* simple */
123
MPN_COPY(wp, up, usize );
127
else if( usign != vsign ) { /* different sign */
128
/* This test is right since USIZE >= VSIZE */
129
if( usize != vsize ) {
130
mpihelp_sub(wp, up, usize, vp, vsize);
132
MPN_NORMALIZE(wp, wsize);
135
else if( mpihelp_cmp(up, vp, usize) < 0 ) {
136
mpihelp_sub_n(wp, vp, up, usize);
138
MPN_NORMALIZE(wp, wsize);
143
mpihelp_sub_n(wp, up, vp, usize);
145
MPN_NORMALIZE(wp, wsize);
150
else { /* U and V have same sign. Add them. */
151
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
mpi_resize(w, wsize);
183
/* These must be after realloc (U may be the same as W). */
187
if( !usize ) { /* simple */
192
else if( usign ) { /* mpi and v are negative */
194
cy = mpihelp_add_1(wp, up, usize, v);
198
else { /* The signs are different. Need exact comparison to determine
199
* which operand to subtract from which. */
200
if( usize == 1 && up[0] < v ) {
206
mpihelp_sub_1(wp, up, usize, v);
207
/* Size can decrease with at most one limb. */
208
wsize = usize - (wp[usize-1]==0);
217
mpi_sub(MPI w, MPI u, MPI v)
220
MPI vv = mpi_copy(v);
221
vv->sign = !vv->sign;
226
/* fixme: this is not thread-save (we temp. modify v) */
235
mpi_addm( MPI w, MPI u, MPI v, MPI m)
238
mpi_fdiv_r( w, w, m );
242
mpi_subm( MPI w, MPI u, MPI v, MPI m)
245
mpi_fdiv_r( w, w, m );