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