raw
ch1_mpi                 1 /* mpi-add.c  -  MPI functions
ch1_mpi 2 * Modified by No Such Labs. (C) 2015. See README.
ch1_mpi 3 *
ch1_mpi 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
ch1_mpi 5 * SHA256(gnupg-1.4.10.tar.gz):
ch1_mpi 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
ch1_mpi 7 * (C) 1994-2005 Free Software Foundation, Inc.
ch1_mpi 8 *
ch1_mpi 9 * This program is free software: you can redistribute it and/or modify
ch1_mpi 10 * it under the terms of the GNU General Public License as published by
ch1_mpi 11 * the Free Software Foundation, either version 3 of the License, or
ch1_mpi 12 * (at your option) any later version.
ch1_mpi 13 *
ch1_mpi 14 * This program is distributed in the hope that it will be useful,
ch1_mpi 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ch1_mpi 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ch1_mpi 17 * GNU General Public License for more details.
ch1_mpi 18 *
ch1_mpi 19 * You should have received a copy of the GNU General Public License
ch1_mpi 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ch1_mpi 21 */
ch1_mpi 22
ch1_mpi 23 #include <stdio.h>
ch1_mpi 24 #include <stdlib.h>
ch1_mpi 25
ch1_mpi 26 #include "knobs.h"
ch1_mpi 27 #include "mpi-internal.h"
ch1_mpi 28
ch1_mpi 29
ch1_mpi 30 /****************
ch1_mpi 31 * Add the unsigned integer V to the mpi-integer U and store the
ch1_mpi 32 * result in W. U and V may be the same.
ch1_mpi 33 */
ch1_mpi 34 void
ch1_mpi 35 mpi_add_ui(MPI w, MPI u, unsigned long v )
ch1_mpi 36 {
ch1_mpi 37 mpi_ptr_t wp, up;
ch1_mpi 38 mpi_size_t usize, wsize;
ch1_mpi 39 int usign, wsign;
ch1_mpi 40
ch1_mpi 41 usize = u->nlimbs;
ch1_mpi 42 usign = u->sign;
ch1_mpi 43 wsign = 0;
ch1_mpi 44
ch1_mpi 45 /* If not space for W (and possible carry), increase space. */
ch1_mpi 46 wsize = usize + 1;
ch1_mpi 47 if( w->alloced < wsize )
ch1_mpi 48 mpi_resize(w, wsize);
ch1_mpi 49
ch1_mpi 50 /* These must be after realloc (U may be the same as W). */
ch1_mpi 51 up = u->d;
ch1_mpi 52 wp = w->d;
ch1_mpi 53
ch1_mpi 54 if( !usize ) { /* simple */
ch1_mpi 55 wp[0] = v;
ch1_mpi 56 wsize = v? 1:0;
ch1_mpi 57 }
ch1_mpi 58 else if( !usign ) { /* mpi is not negative */
ch1_mpi 59 mpi_limb_t cy;
ch1_mpi 60 cy = mpihelp_add_1(wp, up, usize, v);
ch1_mpi 61 wp[usize] = cy;
ch1_mpi 62 wsize = usize + cy;
ch1_mpi 63 }
ch1_mpi 64 else { /* The signs are different. Need exact comparison to determine
ch1_mpi 65 * which operand to subtract from which. */
ch1_mpi 66 if( usize == 1 && up[0] < v ) {
ch1_mpi 67 wp[0] = v - up[0];
ch1_mpi 68 wsize = 1;
ch1_mpi 69 }
ch1_mpi 70 else {
ch1_mpi 71 mpihelp_sub_1(wp, up, usize, v);
ch1_mpi 72 /* Size can decrease with at most one limb. */
ch1_mpi 73 wsize = usize - (wp[usize-1]==0);
ch1_mpi 74 wsign = 1;
ch1_mpi 75 }
ch1_mpi 76 }
ch1_mpi 77
ch1_mpi 78 w->nlimbs = wsize;
ch1_mpi 79 w->sign = wsign;
ch1_mpi 80 }
ch1_mpi 81
ch1_mpi 82
ch1_mpi 83 void
ch1_mpi 84 mpi_add(MPI w, MPI u, MPI v)
ch1_mpi 85 {
ch1_mpi 86 mpi_ptr_t wp, up, vp;
ch1_mpi 87 mpi_size_t usize, vsize, wsize;
ch1_mpi 88 int usign, vsign, wsign;
ch1_mpi 89
ch1_mpi 90 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
ch1_mpi 91 usize = v->nlimbs;
ch1_mpi 92 usign = v->sign;
ch1_mpi 93 vsize = u->nlimbs;
ch1_mpi 94 vsign = u->sign;
ch1_mpi 95 wsize = usize + 1;
ch1_mpi 96 RESIZE_IF_NEEDED(w, wsize);
ch1_mpi 97 /* These must be after realloc (u or v may be the same as w). */
ch1_mpi 98 up = v->d;
ch1_mpi 99 vp = u->d;
ch1_mpi 100 }
ch1_mpi 101 else {
ch1_mpi 102 usize = u->nlimbs;
ch1_mpi 103 usign = u->sign;
ch1_mpi 104 vsize = v->nlimbs;
ch1_mpi 105 vsign = v->sign;
ch1_mpi 106 wsize = usize + 1;
ch1_mpi 107 RESIZE_IF_NEEDED(w, wsize);
ch1_mpi 108 /* These must be after realloc (u or v may be the same as w). */
ch1_mpi 109 up = u->d;
ch1_mpi 110 vp = v->d;
ch1_mpi 111 }
ch1_mpi 112 wp = w->d;
ch1_mpi 113 wsign = 0;
ch1_mpi 114
ch1_mpi 115 if( !vsize ) { /* simple */
ch1_mpi 116 MPN_COPY(wp, up, usize );
ch1_mpi 117 wsize = usize;
ch1_mpi 118 wsign = usign;
ch1_mpi 119 }
ch1_mpi 120 else if( usign != vsign ) { /* different sign */
ch1_mpi 121 /* This test is right since USIZE >= VSIZE */
ch1_mpi 122 if( usize != vsize ) {
ch1_mpi 123 mpihelp_sub(wp, up, usize, vp, vsize);
ch1_mpi 124 wsize = usize;
ch1_mpi 125 MPN_NORMALIZE(wp, wsize);
ch1_mpi 126 wsign = usign;
ch1_mpi 127 }
ch1_mpi 128 else if( mpihelp_cmp(up, vp, usize) < 0 ) {
ch1_mpi 129 mpihelp_sub_n(wp, vp, up, usize);
ch1_mpi 130 wsize = usize;
ch1_mpi 131 MPN_NORMALIZE(wp, wsize);
ch1_mpi 132 if( !usign )
ch1_mpi 133 wsign = 1;
ch1_mpi 134 }
ch1_mpi 135 else {
ch1_mpi 136 mpihelp_sub_n(wp, up, vp, usize);
ch1_mpi 137 wsize = usize;
ch1_mpi 138 MPN_NORMALIZE(wp, wsize);
ch1_mpi 139 if( usign )
ch1_mpi 140 wsign = 1;
ch1_mpi 141 }
ch1_mpi 142 }
ch1_mpi 143 else { /* U and V have same sign. Add them. */
ch1_mpi 144 mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
ch1_mpi 145 wp[usize] = cy;
ch1_mpi 146 wsize = usize + cy;
ch1_mpi 147 if( usign )
ch1_mpi 148 wsign = 1;
ch1_mpi 149 }
ch1_mpi 150
ch1_mpi 151 w->nlimbs = wsize;
ch1_mpi 152 w->sign = wsign;
ch1_mpi 153 }
ch1_mpi 154
ch1_mpi 155
ch1_mpi 156 /****************
ch1_mpi 157 * Subtract the unsigned integer V from the mpi-integer U and store the
ch1_mpi 158 * result in W.
ch1_mpi 159 */
ch1_mpi 160 void
ch1_mpi 161 mpi_sub_ui(MPI w, MPI u, unsigned long v )
ch1_mpi 162 {
ch1_mpi 163 mpi_ptr_t wp, up;
ch1_mpi 164 mpi_size_t usize, wsize;
ch1_mpi 165 int usign, wsign;
ch1_mpi 166
ch1_mpi 167 usize = u->nlimbs;
ch1_mpi 168 usign = u->sign;
ch1_mpi 169 wsign = 0;
ch1_mpi 170
ch1_mpi 171 /* If not space for W (and possible carry), increase space. */
ch1_mpi 172 wsize = usize + 1;
ch1_mpi 173 if( w->alloced < wsize )
ch1_mpi 174 mpi_resize(w, wsize);
ch1_mpi 175
ch1_mpi 176 /* These must be after realloc (U may be the same as W). */
ch1_mpi 177 up = u->d;
ch1_mpi 178 wp = w->d;
ch1_mpi 179
ch1_mpi 180 if( !usize ) { /* simple */
ch1_mpi 181 wp[0] = v;
ch1_mpi 182 wsize = v? 1:0;
ch1_mpi 183 wsign = 1;
ch1_mpi 184 }
ch1_mpi 185 else if( usign ) { /* mpi and v are negative */
ch1_mpi 186 mpi_limb_t cy;
ch1_mpi 187 cy = mpihelp_add_1(wp, up, usize, v);
ch1_mpi 188 wp[usize] = cy;
ch1_mpi 189 wsize = usize + cy;
ch1_mpi 190 }
ch1_mpi 191 else { /* The signs are different. Need exact comparison to determine
ch1_mpi 192 * which operand to subtract from which. */
ch1_mpi 193 if( usize == 1 && up[0] < v ) {
ch1_mpi 194 wp[0] = v - up[0];
ch1_mpi 195 wsize = 1;
ch1_mpi 196 wsign = 1;
ch1_mpi 197 }
ch1_mpi 198 else {
ch1_mpi 199 mpihelp_sub_1(wp, up, usize, v);
ch1_mpi 200 /* Size can decrease with at most one limb. */
ch1_mpi 201 wsize = usize - (wp[usize-1]==0);
ch1_mpi 202 }
ch1_mpi 203 }
ch1_mpi 204
ch1_mpi 205 w->nlimbs = wsize;
ch1_mpi 206 w->sign = wsign;
ch1_mpi 207 }
ch1_mpi 208
ch1_mpi 209 void
ch1_mpi 210 mpi_sub(MPI w, MPI u, MPI v)
ch1_mpi 211 {
ch1_mpi 212 if( w == v ) {
ch1_mpi 213 MPI vv = mpi_copy(v);
ch1_mpi 214 vv->sign = !vv->sign;
ch1_mpi 215 mpi_add( w, u, vv );
ch1_mpi 216 mpi_free(vv);
ch1_mpi 217 }
ch1_mpi 218 else {
ch1_mpi 219 /* fixme: this is not thread-save (we temp. modify v) */
ch1_mpi 220 v->sign = !v->sign;
ch1_mpi 221 mpi_add( w, u, v );
ch1_mpi 222 v->sign = !v->sign;
ch1_mpi 223 }
ch1_mpi 224 }
ch1_mpi 225
ch1_mpi 226
ch1_mpi 227 void
ch1_mpi 228 mpi_addm( MPI w, MPI u, MPI v, MPI m)
ch1_mpi 229 {
ch1_mpi 230 mpi_add(w, u, v);
ch1_mpi 231 mpi_fdiv_r( w, w, m );
ch1_mpi 232 }
ch1_mpi 233
ch1_mpi 234 void
ch1_mpi 235 mpi_subm( MPI w, MPI u, MPI v, MPI m)
ch1_mpi 236 {
ch1_mpi 237 mpi_sub(w, u, v);
ch1_mpi 238 mpi_fdiv_r( w, w, m );
ch1_mpi 239 }
ch1_mpi 240