]> Git Repo - J-linux.git/blob - lib/crypto/mpi/mpi-add.c
Revert "lib/mpi: Extend the MPI library"
[J-linux.git] / lib / crypto / mpi / mpi-add.c
1 /* mpi-add.c  -  MPI functions
2  * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3  *               2003 Free Software Foundation, Inc.
4  *
5  * This file is part of Libgcrypt.
6  *
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.
12  */
13
14 #include "mpi-internal.h"
15
16 void mpi_add(MPI w, MPI u, MPI v)
17 {
18         mpi_ptr_t wp, up, vp;
19         mpi_size_t usize, vsize, wsize;
20         int usign, vsign, wsign;
21
22         if (u->nlimbs < v->nlimbs) { /* Swap U and V. */
23                 usize = v->nlimbs;
24                 usign = v->sign;
25                 vsize = u->nlimbs;
26                 vsign = u->sign;
27                 wsize = usize + 1;
28                 RESIZE_IF_NEEDED(w, wsize);
29                 /* These must be after realloc (u or v may be the same as w).  */
30                 up = v->d;
31                 vp = u->d;
32         } else {
33                 usize = u->nlimbs;
34                 usign = u->sign;
35                 vsize = v->nlimbs;
36                 vsign = v->sign;
37                 wsize = usize + 1;
38                 RESIZE_IF_NEEDED(w, wsize);
39                 /* These must be after realloc (u or v may be the same as w).  */
40                 up = u->d;
41                 vp = v->d;
42         }
43         wp = w->d;
44         wsign = 0;
45
46         if (!vsize) {  /* simple */
47                 MPN_COPY(wp, up, usize);
48                 wsize = usize;
49                 wsign = usign;
50         } else if (usign != vsign) { /* different sign */
51                 /* This test is right since USIZE >= VSIZE */
52                 if (usize != vsize) {
53                         mpihelp_sub(wp, up, usize, vp, vsize);
54                         wsize = usize;
55                         MPN_NORMALIZE(wp, wsize);
56                         wsign = usign;
57                 } else if (mpihelp_cmp(up, vp, usize) < 0) {
58                         mpihelp_sub_n(wp, vp, up, usize);
59                         wsize = usize;
60                         MPN_NORMALIZE(wp, wsize);
61                         if (!usign)
62                                 wsign = 1;
63                 } else {
64                         mpihelp_sub_n(wp, up, vp, usize);
65                         wsize = usize;
66                         MPN_NORMALIZE(wp, wsize);
67                         if (usign)
68                                 wsign = 1;
69                 }
70         } else { /* U and V have same sign. Add them. */
71                 mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
72                 wp[usize] = cy;
73                 wsize = usize + cy;
74                 if (usign)
75                         wsign = 1;
76         }
77
78         w->nlimbs = wsize;
79         w->sign = wsign;
80 }
81 EXPORT_SYMBOL_GPL(mpi_add);
82
83 void mpi_sub(MPI w, MPI u, MPI v)
84 {
85         MPI vv = mpi_copy(v);
86         vv->sign = !vv->sign;
87         mpi_add(w, u, vv);
88         mpi_free(vv);
89 }
90 EXPORT_SYMBOL_GPL(mpi_sub);
91
92 void mpi_addm(MPI w, MPI u, MPI v, MPI m)
93 {
94         mpi_add(w, u, v);
95         mpi_mod(w, w, m);
96 }
97 EXPORT_SYMBOL_GPL(mpi_addm);
98
99 void mpi_subm(MPI w, MPI u, MPI v, MPI m)
100 {
101         mpi_sub(w, u, v);
102         mpi_mod(w, w, m);
103 }
104 EXPORT_SYMBOL_GPL(mpi_subm);
This page took 0.031515 seconds and 4 git commands to generate.