1 /* mpi-add.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
5 * This file is part of Libgcrypt.
7 * Note: This code is heavily based on the GNU MP Library.
8 * Actually it's the same code with only minor changes in the
9 * way the data is stored; this is to support the abstraction
10 * of an optional secure memory allocation which may be used
11 * to avoid revealing of sensitive data due to paging etc.
14 #include "mpi-internal.h"
16 void mpi_add(MPI w, MPI u, MPI v)
19 mpi_size_t usize, vsize, wsize;
20 int usign, vsign, wsign;
22 if (u->nlimbs < v->nlimbs) { /* Swap U and V. */
28 RESIZE_IF_NEEDED(w, wsize);
29 /* These must be after realloc (u or v may be the same as w). */
38 RESIZE_IF_NEEDED(w, wsize);
39 /* These must be after realloc (u or v may be the same as w). */
46 if (!vsize) { /* simple */
47 MPN_COPY(wp, up, usize);
50 } else if (usign != vsign) { /* different sign */
51 /* This test is right since USIZE >= VSIZE */
53 mpihelp_sub(wp, up, usize, vp, vsize);
55 MPN_NORMALIZE(wp, wsize);
57 } else if (mpihelp_cmp(up, vp, usize) < 0) {
58 mpihelp_sub_n(wp, vp, up, usize);
60 MPN_NORMALIZE(wp, wsize);
64 mpihelp_sub_n(wp, up, vp, usize);
66 MPN_NORMALIZE(wp, wsize);
70 } else { /* U and V have same sign. Add them. */
71 mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
81 EXPORT_SYMBOL_GPL(mpi_add);
83 void mpi_sub(MPI w, MPI u, MPI v)
90 EXPORT_SYMBOL_GPL(mpi_sub);
92 void mpi_addm(MPI w, MPI u, MPI v, MPI m)
97 EXPORT_SYMBOL_GPL(mpi_addm);
99 void mpi_subm(MPI w, MPI u, MPI v, MPI m)
104 EXPORT_SYMBOL_GPL(mpi_subm);