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 int mpi_add(MPI w, MPI u, MPI v)
19 mpi_size_t usize, vsize, wsize;
20 int usign, vsign, wsign;
23 if (u->nlimbs < v->nlimbs) { /* Swap U and V. */
29 err = RESIZE_IF_NEEDED(w, wsize);
32 /* These must be after realloc (u or v may be the same as w). */
41 err = RESIZE_IF_NEEDED(w, wsize);
44 /* These must be after realloc (u or v may be the same as w). */
51 if (!vsize) { /* simple */
52 MPN_COPY(wp, up, usize);
55 } else if (usign != vsign) { /* different sign */
56 /* This test is right since USIZE >= VSIZE */
58 mpihelp_sub(wp, up, usize, vp, vsize);
60 MPN_NORMALIZE(wp, wsize);
62 } else if (mpihelp_cmp(up, vp, usize) < 0) {
63 mpihelp_sub_n(wp, vp, up, usize);
65 MPN_NORMALIZE(wp, wsize);
69 mpihelp_sub_n(wp, up, vp, usize);
71 MPN_NORMALIZE(wp, wsize);
75 } else { /* U and V have same sign. Add them. */
76 mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
87 EXPORT_SYMBOL_GPL(mpi_add);
89 int mpi_sub(MPI w, MPI u, MPI v)
99 err = mpi_add(w, u, vv);
104 EXPORT_SYMBOL_GPL(mpi_sub);
106 int mpi_addm(MPI w, MPI u, MPI v, MPI m)
108 return mpi_add(w, u, v) ?:
111 EXPORT_SYMBOL_GPL(mpi_addm);
113 int mpi_subm(MPI w, MPI u, MPI v, MPI m)
115 return mpi_sub(w, u, v) ?:
118 EXPORT_SYMBOL_GPL(mpi_subm);