raw
smg_comms_c_wrappers    1 /* mpi-mul.c  -  MPI functions
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://www.gnu.org/licenses/>.
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 void
smg_comms_c_wrappers 31 mpi_mul_ui( MPI prod, MPI mult, unsigned long small_mult )
smg_comms_c_wrappers 32 {
smg_comms_c_wrappers 33 mpi_size_t size, prod_size;
smg_comms_c_wrappers 34 mpi_ptr_t prod_ptr;
smg_comms_c_wrappers 35 mpi_limb_t cy;
smg_comms_c_wrappers 36 int sign;
smg_comms_c_wrappers 37
smg_comms_c_wrappers 38 size = mult->nlimbs;
smg_comms_c_wrappers 39 sign = mult->sign;
smg_comms_c_wrappers 40
smg_comms_c_wrappers 41 if( !size || !small_mult ) {
smg_comms_c_wrappers 42 prod->nlimbs = 0;
smg_comms_c_wrappers 43 prod->sign = 0;
smg_comms_c_wrappers 44 return;
smg_comms_c_wrappers 45 }
smg_comms_c_wrappers 46
smg_comms_c_wrappers 47 prod_size = size + 1;
smg_comms_c_wrappers 48 if( prod->alloced < prod_size )
smg_comms_c_wrappers 49 mpi_resize( prod, prod_size );
smg_comms_c_wrappers 50 prod_ptr = prod->d;
smg_comms_c_wrappers 51
smg_comms_c_wrappers 52 cy = mpihelp_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
smg_comms_c_wrappers 53 if( cy )
smg_comms_c_wrappers 54 prod_ptr[size++] = cy;
smg_comms_c_wrappers 55 prod->nlimbs = size;
smg_comms_c_wrappers 56 prod->sign = sign;
smg_comms_c_wrappers 57 }
smg_comms_c_wrappers 58
smg_comms_c_wrappers 59
smg_comms_c_wrappers 60 void
smg_comms_c_wrappers 61 mpi_mul_2exp( MPI w, MPI u, unsigned long cnt)
smg_comms_c_wrappers 62 {
smg_comms_c_wrappers 63 mpi_size_t usize, wsize, limb_cnt;
smg_comms_c_wrappers 64 mpi_ptr_t wp;
smg_comms_c_wrappers 65 mpi_limb_t wlimb;
smg_comms_c_wrappers 66 int usign, wsign;
smg_comms_c_wrappers 67
smg_comms_c_wrappers 68 usize = u->nlimbs;
smg_comms_c_wrappers 69 usign = u->sign;
smg_comms_c_wrappers 70
smg_comms_c_wrappers 71 if( !usize ) {
smg_comms_c_wrappers 72 w->nlimbs = 0;
smg_comms_c_wrappers 73 w->sign = 0;
smg_comms_c_wrappers 74 return;
smg_comms_c_wrappers 75 }
smg_comms_c_wrappers 76
smg_comms_c_wrappers 77 limb_cnt = cnt / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 78 wsize = usize + limb_cnt + 1;
smg_comms_c_wrappers 79 if( w->alloced < wsize )
smg_comms_c_wrappers 80 mpi_resize(w, wsize );
smg_comms_c_wrappers 81 wp = w->d;
smg_comms_c_wrappers 82 wsize = usize + limb_cnt;
smg_comms_c_wrappers 83 wsign = usign;
smg_comms_c_wrappers 84
smg_comms_c_wrappers 85 cnt %= BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 86 if( cnt ) {
smg_comms_c_wrappers 87 wlimb = mpihelp_lshift( wp + limb_cnt, u->d, usize, cnt );
smg_comms_c_wrappers 88 if( wlimb ) {
smg_comms_c_wrappers 89 wp[wsize] = wlimb;
smg_comms_c_wrappers 90 wsize++;
smg_comms_c_wrappers 91 }
smg_comms_c_wrappers 92 }
smg_comms_c_wrappers 93 else {
smg_comms_c_wrappers 94 MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
smg_comms_c_wrappers 95 }
smg_comms_c_wrappers 96
smg_comms_c_wrappers 97 /* Zero all whole limbs at low end. Do it here and not before calling
smg_comms_c_wrappers 98 * mpn_lshift, not to lose for U == W. */
smg_comms_c_wrappers 99 MPN_ZERO( wp, limb_cnt );
smg_comms_c_wrappers 100
smg_comms_c_wrappers 101 w->nlimbs = wsize;
smg_comms_c_wrappers 102 w->sign = wsign;
smg_comms_c_wrappers 103 }
smg_comms_c_wrappers 104
smg_comms_c_wrappers 105
smg_comms_c_wrappers 106
smg_comms_c_wrappers 107 void
smg_comms_c_wrappers 108 mpi_mul( MPI w, MPI u, MPI v)
smg_comms_c_wrappers 109 {
smg_comms_c_wrappers 110 mpi_size_t usize, vsize, wsize;
smg_comms_c_wrappers 111 mpi_ptr_t up, vp, wp;
smg_comms_c_wrappers 112 mpi_limb_t cy;
smg_comms_c_wrappers 113 int usign, vsign, usecure, vsecure, sign_product;
smg_comms_c_wrappers 114 int assign_wp=0;
smg_comms_c_wrappers 115 mpi_ptr_t tmp_limb=NULL;
smg_comms_c_wrappers 116
smg_comms_c_wrappers 117
smg_comms_c_wrappers 118 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
smg_comms_c_wrappers 119 usize = v->nlimbs;
smg_comms_c_wrappers 120 usign = v->sign;
smg_comms_c_wrappers 121 usecure = mpi_is_secure(v);
smg_comms_c_wrappers 122 up = v->d;
smg_comms_c_wrappers 123 vsize = u->nlimbs;
smg_comms_c_wrappers 124 vsign = u->sign;
smg_comms_c_wrappers 125 vsecure = mpi_is_secure(u);
smg_comms_c_wrappers 126 vp = u->d;
smg_comms_c_wrappers 127 }
smg_comms_c_wrappers 128 else {
smg_comms_c_wrappers 129 usize = u->nlimbs;
smg_comms_c_wrappers 130 usign = u->sign;
smg_comms_c_wrappers 131 usecure = mpi_is_secure(u);
smg_comms_c_wrappers 132 up = u->d;
smg_comms_c_wrappers 133 vsize = v->nlimbs;
smg_comms_c_wrappers 134 vsign = v->sign;
smg_comms_c_wrappers 135 vsecure = mpi_is_secure(v);
smg_comms_c_wrappers 136 vp = v->d;
smg_comms_c_wrappers 137 }
smg_comms_c_wrappers 138 sign_product = usign ^ vsign;
smg_comms_c_wrappers 139 wp = w->d;
smg_comms_c_wrappers 140
smg_comms_c_wrappers 141 /* Ensure W has space enough to store the result. */
smg_comms_c_wrappers 142 wsize = usize + vsize;
smg_comms_c_wrappers 143 if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
smg_comms_c_wrappers 144 /* w is not allocated in secure space but u or v is. To make sure
smg_comms_c_wrappers 145 * that no temporray results are stored in w, we temporary use
smg_comms_c_wrappers 146 * a newly allocated limb space for w */
smg_comms_c_wrappers 147 wp = mpi_alloc_limb_space( wsize, 1 );
smg_comms_c_wrappers 148 assign_wp = 2; /* mark it as 2 so that we can later copy it back to
smg_comms_c_wrappers 149 * mormal memory */
smg_comms_c_wrappers 150 }
smg_comms_c_wrappers 151 else if( w->alloced < wsize ) {
smg_comms_c_wrappers 152 if( wp == up || wp == vp ) {
smg_comms_c_wrappers 153 wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
smg_comms_c_wrappers 154 assign_wp = 1;
smg_comms_c_wrappers 155 }
smg_comms_c_wrappers 156 else {
smg_comms_c_wrappers 157 mpi_resize(w, wsize );
smg_comms_c_wrappers 158 wp = w->d;
smg_comms_c_wrappers 159 }
smg_comms_c_wrappers 160 }
smg_comms_c_wrappers 161 else { /* Make U and V not overlap with W. */
smg_comms_c_wrappers 162 if( wp == up ) {
smg_comms_c_wrappers 163 /* W and U are identical. Allocate temporary space for U. */
smg_comms_c_wrappers 164 up = tmp_limb = mpi_alloc_limb_space( usize, usecure );
smg_comms_c_wrappers 165 /* Is V identical too? Keep it identical with U. */
smg_comms_c_wrappers 166 if( wp == vp )
smg_comms_c_wrappers 167 vp = up;
smg_comms_c_wrappers 168 /* Copy to the temporary space. */
smg_comms_c_wrappers 169 MPN_COPY( up, wp, usize );
smg_comms_c_wrappers 170 }
smg_comms_c_wrappers 171 else if( wp == vp ) {
smg_comms_c_wrappers 172 /* W and V are identical. Allocate temporary space for V. */
smg_comms_c_wrappers 173 vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
smg_comms_c_wrappers 174 /* Copy to the temporary space. */
smg_comms_c_wrappers 175 MPN_COPY( vp, wp, vsize );
smg_comms_c_wrappers 176 }
smg_comms_c_wrappers 177 }
smg_comms_c_wrappers 178
smg_comms_c_wrappers 179 if( !vsize )
smg_comms_c_wrappers 180 wsize = 0;
smg_comms_c_wrappers 181 else {
smg_comms_c_wrappers 182 cy = mpihelp_mul( wp, up, usize, vp, vsize );
smg_comms_c_wrappers 183 wsize -= cy? 0:1;
smg_comms_c_wrappers 184 }
smg_comms_c_wrappers 185
smg_comms_c_wrappers 186 if( assign_wp ) {
smg_comms_c_wrappers 187 if (assign_wp == 2) {
smg_comms_c_wrappers 188 /* copy the temp wp from secure memory back to normal memory */
smg_comms_c_wrappers 189 mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
smg_comms_c_wrappers 190 MPN_COPY (tmp_wp, wp, wsize);
smg_comms_c_wrappers 191 mpi_free_limb_space (wp);
smg_comms_c_wrappers 192 wp = tmp_wp;
smg_comms_c_wrappers 193 }
smg_comms_c_wrappers 194 mpi_assign_limb_space( w, wp, wsize );
smg_comms_c_wrappers 195 }
smg_comms_c_wrappers 196 w->nlimbs = wsize;
smg_comms_c_wrappers 197 w->sign = sign_product;
smg_comms_c_wrappers 198 if( tmp_limb )
smg_comms_c_wrappers 199 mpi_free_limb_space( tmp_limb );
smg_comms_c_wrappers 200 }
smg_comms_c_wrappers 201
smg_comms_c_wrappers 202
smg_comms_c_wrappers 203 void
smg_comms_c_wrappers 204 mpi_mulm( MPI w, MPI u, MPI v, MPI m)
smg_comms_c_wrappers 205 {
smg_comms_c_wrappers 206 mpi_mul(w, u, v);
smg_comms_c_wrappers 207 mpi_fdiv_r( w, w, m );
smg_comms_c_wrappers 208 }
smg_comms_c_wrappers 209