raw
mpi-genesis             1 /* mpi-pow.c  -  MPI functions
mpi_second_cut 2 * Modified by No Such Labs. (C) 2015. See README.
mpi-genesis 3 *
mpi_second_cut 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
mpi_second_cut 5 * SHA256(gnupg-1.4.10.tar.gz):
mpi_second_cut 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
mpi_second_cut 7 * (C) 1994-2005 Free Software Foundation, Inc.
mpi-genesis 8 *
mpi_second_cut 9 * This program is free software: you can redistribute it and/or modify
mpi-genesis 10 * it under the terms of the GNU General Public License as published by
mpi_second_cut 11 * the Free Software Foundation, either version 3 of the License, or
mpi-genesis 12 * (at your option) any later version.
mpi-genesis 13 *
mpi_second_cut 14 * This program is distributed in the hope that it will be useful,
mpi-genesis 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 17 * GNU General Public License for more details.
mpi-genesis 18 *
mpi-genesis 19 * You should have received a copy of the GNU General Public License
mpi_second_cut 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
mpi-genesis 21 */
mpi-genesis 22
mpi-genesis 23 #include <stdio.h>
mpi-genesis 24 #include <stdlib.h>
mpi-genesis 25 #include <string.h>
mpi_second_cut 26 #include <assert.h>
mpi_second_cut 27
mpi_second_cut 28 #include "knobs.h"
mpi-genesis 29 #include "mpi-internal.h"
mpi-genesis 30 #include "longlong.h"
mpi_second_cut 31
mpi-genesis 32
mpi-genesis 33
mpi-genesis 34 /****************
mpi-genesis 35 * RES = BASE ^ EXP mod MOD
mpi-genesis 36 */
mpi-genesis 37 void
mpi-genesis 38 mpi_powm( MPI res, MPI base, MPI exponent, MPI mod)
mpi-genesis 39 {
mpi-genesis 40 mpi_ptr_t rp, ep, mp, bp;
mpi-genesis 41 mpi_size_t esize, msize, bsize, rsize;
mpi-genesis 42 int esign, msign, bsign, rsign;
mpi-genesis 43 int esec, msec, bsec, rsec;
mpi-genesis 44 mpi_size_t size;
mpi-genesis 45 int mod_shift_cnt;
mpi-genesis 46 int negative_result;
mpi-genesis 47 mpi_ptr_t mp_marker=NULL, bp_marker=NULL, ep_marker=NULL;
mpi-genesis 48 mpi_ptr_t xp_marker=NULL;
mpi-genesis 49 int assign_rp=0;
mpi-genesis 50 mpi_ptr_t tspace = NULL;
mpi-genesis 51 mpi_size_t tsize=0; /* to avoid compiler warning */
mpi-genesis 52 /* fixme: we should check that the warning is void*/
mpi-genesis 53
mpi-genesis 54 esize = exponent->nlimbs;
mpi-genesis 55 msize = mod->nlimbs;
mpi-genesis 56 size = 2 * msize;
mpi-genesis 57 esign = exponent->sign;
mpi-genesis 58 msign = mod->sign;
mpi-genesis 59
mpi-genesis 60 esec = mpi_is_secure(exponent);
mpi-genesis 61 msec = mpi_is_secure(mod);
mpi-genesis 62 bsec = mpi_is_secure(base);
mpi-genesis 63 rsec = mpi_is_secure(res);
mpi-genesis 64
mpi-genesis 65 rp = res->d;
mpi-genesis 66 ep = exponent->d;
mpi-genesis 67
mpi-genesis 68 if( !msize )
mpi-genesis 69 msize = 1 / msize; /* provoke a signal */
mpi-genesis 70
mpi-genesis 71 if( !esize ) {
mpi-genesis 72 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
mpi-genesis 73 * depending on if MOD equals 1. */
mpi-genesis 74 rp[0] = 1;
mpi-genesis 75 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
mpi-genesis 76 res->sign = 0;
mpi-genesis 77 goto leave;
mpi-genesis 78 }
mpi-genesis 79
mpi-genesis 80 /* Normalize MOD (i.e. make its most significant bit set) as required by
mpi-genesis 81 * mpn_divrem. This will make the intermediate values in the calculation
mpi-genesis 82 * slightly larger, but the correct result is obtained after a final
mpi-genesis 83 * reduction using the original MOD value. */
mpi-genesis 84 mp = mp_marker = mpi_alloc_limb_space(msize, msec);
mpi-genesis 85 count_leading_zeros( mod_shift_cnt, mod->d[msize-1] );
mpi-genesis 86 if( mod_shift_cnt )
mpi-genesis 87 mpihelp_lshift( mp, mod->d, msize, mod_shift_cnt );
mpi-genesis 88 else
mpi-genesis 89 MPN_COPY( mp, mod->d, msize );
mpi-genesis 90
mpi-genesis 91 bsize = base->nlimbs;
mpi-genesis 92 bsign = base->sign;
mpi-genesis 93 if( bsize > msize ) { /* The base is larger than the module. Reduce it. */
mpi-genesis 94 /* Allocate (BSIZE + 1) with space for remainder and quotient.
mpi-genesis 95 * (The quotient is (bsize - msize + 1) limbs.) */
mpi-genesis 96 bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec );
mpi-genesis 97 MPN_COPY( bp, base->d, bsize );
mpi-genesis 98 /* We don't care about the quotient, store it above the remainder,
mpi-genesis 99 * at BP + MSIZE. */
mpi-genesis 100 mpihelp_divrem( bp + msize, 0, bp, bsize, mp, msize );
mpi-genesis 101 bsize = msize;
mpi-genesis 102 /* Canonicalize the base, since we are going to multiply with it
mpi-genesis 103 * quite a few times. */
mpi-genesis 104 MPN_NORMALIZE( bp, bsize );
mpi-genesis 105 }
mpi-genesis 106 else
mpi-genesis 107 bp = base->d;
mpi-genesis 108
mpi-genesis 109 if( !bsize ) {
mpi-genesis 110 res->nlimbs = 0;
mpi-genesis 111 res->sign = 0;
mpi-genesis 112 goto leave;
mpi-genesis 113 }
mpi-genesis 114
mpi-genesis 115 if( res->alloced < size ) {
mpi-genesis 116 /* We have to allocate more space for RES. If any of the input
mpi-genesis 117 * parameters are identical to RES, defer deallocation of the old
mpi-genesis 118 * space. */
mpi-genesis 119 if( rp == ep || rp == mp || rp == bp ) {
mpi-genesis 120 rp = mpi_alloc_limb_space( size, rsec );
mpi-genesis 121 assign_rp = 1;
mpi-genesis 122 }
mpi-genesis 123 else {
mpi-genesis 124 mpi_resize( res, size );
mpi-genesis 125 rp = res->d;
mpi-genesis 126 }
mpi-genesis 127 }
mpi-genesis 128 else { /* Make BASE, EXPONENT and MOD not overlap with RES. */
mpi-genesis 129 if( rp == bp ) {
mpi-genesis 130 /* RES and BASE are identical. Allocate temp. space for BASE. */
mpi-genesis 131 assert( !bp_marker );
mpi-genesis 132 bp = bp_marker = mpi_alloc_limb_space( bsize, bsec );
mpi-genesis 133 MPN_COPY(bp, rp, bsize);
mpi-genesis 134 }
mpi-genesis 135 if( rp == ep ) {
mpi-genesis 136 /* RES and EXPONENT are identical.
mpi-genesis 137 Allocate temp. space for EXPONENT. */
mpi-genesis 138 ep = ep_marker = mpi_alloc_limb_space( esize, esec );
mpi-genesis 139 MPN_COPY(ep, rp, esize);
mpi-genesis 140 }
mpi-genesis 141 if( rp == mp ) {
mpi-genesis 142 /* RES and MOD are identical. Allocate temporary space for MOD.*/
mpi-genesis 143 assert( !mp_marker );
mpi-genesis 144 mp = mp_marker = mpi_alloc_limb_space( msize, msec );
mpi-genesis 145 MPN_COPY(mp, rp, msize);
mpi-genesis 146 }
mpi-genesis 147 }
mpi-genesis 148
mpi-genesis 149 MPN_COPY( rp, bp, bsize );
mpi-genesis 150 rsize = bsize;
mpi-genesis 151 rsign = bsign;
mpi-genesis 152
mpi-genesis 153 {
mpi-genesis 154 mpi_size_t i;
mpi-genesis 155 mpi_ptr_t xp = xp_marker = mpi_alloc_limb_space( 2 * (msize + 1), msec );
mpi-genesis 156 int c;
mpi-genesis 157 mpi_limb_t e;
mpi-genesis 158 mpi_limb_t carry_limb;
mpi-genesis 159 struct karatsuba_ctx karactx;
mpi-genesis 160
mpi-genesis 161 memset( &karactx, 0, sizeof karactx );
mpi-genesis 162 negative_result = (ep[0] & 1) && base->sign;
mpi-genesis 163
mpi-genesis 164 i = esize - 1;
mpi-genesis 165 e = ep[i];
mpi-genesis 166 count_leading_zeros (c, e);
mpi-genesis 167 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
mpi-genesis 168 c = BITS_PER_MPI_LIMB - 1 - c;
mpi-genesis 169
mpi-genesis 170 /* Main loop.
mpi-genesis 171 *
mpi-genesis 172 * Make the result be pointed to alternately by XP and RP. This
mpi-genesis 173 * helps us avoid block copying, which would otherwise be necessary
mpi-genesis 174 * with the overlap restrictions of mpihelp_divmod. With 50% probability
mpi-genesis 175 * the result after this loop will be in the area originally pointed
mpi-genesis 176 * by RP (==RES->d), and with 50% probability in the area originally
mpi-genesis 177 * pointed to by XP.
mpi-genesis 178 */
mpi-genesis 179
mpi-genesis 180 for(;;) {
mpi-genesis 181 while( c ) {
mpi-genesis 182 mpi_ptr_t tp;
mpi-genesis 183 mpi_size_t xsize;
mpi-genesis 184
mpi-genesis 185 /*mpihelp_mul_n(xp, rp, rp, rsize);*/
mpi-genesis 186 if( rsize < KARATSUBA_THRESHOLD )
mpi-genesis 187 mpih_sqr_n_basecase( xp, rp, rsize );
mpi-genesis 188 else {
mpi-genesis 189 if( !tspace ) {
mpi-genesis 190 tsize = 2 * rsize;
mpi-genesis 191 tspace = mpi_alloc_limb_space( tsize, 0 );
mpi-genesis 192 }
mpi-genesis 193 else if( tsize < (2*rsize) ) {
mpi-genesis 194 mpi_free_limb_space( tspace );
mpi-genesis 195 tsize = 2 * rsize;
mpi-genesis 196 tspace = mpi_alloc_limb_space( tsize, 0 );
mpi-genesis 197 }
mpi-genesis 198 mpih_sqr_n( xp, rp, rsize, tspace );
mpi-genesis 199 }
mpi-genesis 200
mpi-genesis 201 xsize = 2 * rsize;
mpi-genesis 202 if( xsize > msize ) {
mpi-genesis 203 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
mpi-genesis 204 xsize = msize;
mpi-genesis 205 }
mpi-genesis 206
mpi-genesis 207 tp = rp; rp = xp; xp = tp;
mpi-genesis 208 rsize = xsize;
mpi-genesis 209
mpi-genesis 210 if( (mpi_limb_signed_t)e < 0 ) {
mpi-genesis 211 /*mpihelp_mul( xp, rp, rsize, bp, bsize );*/
mpi-genesis 212 if( bsize < KARATSUBA_THRESHOLD ) {
mpi-genesis 213 mpihelp_mul( xp, rp, rsize, bp, bsize );
mpi-genesis 214 }
mpi-genesis 215 else {
mpi-genesis 216 mpihelp_mul_karatsuba_case(
mpi-genesis 217 xp, rp, rsize, bp, bsize, &karactx );
mpi-genesis 218 }
mpi-genesis 219
mpi-genesis 220 xsize = rsize + bsize;
mpi-genesis 221 if( xsize > msize ) {
mpi-genesis 222 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
mpi-genesis 223 xsize = msize;
mpi-genesis 224 }
mpi-genesis 225
mpi-genesis 226 tp = rp; rp = xp; xp = tp;
mpi-genesis 227 rsize = xsize;
mpi-genesis 228 }
mpi-genesis 229 e <<= 1;
mpi-genesis 230 c--;
mpi-genesis 231 }
mpi-genesis 232
mpi-genesis 233 i--;
mpi-genesis 234 if( i < 0 )
mpi-genesis 235 break;
mpi-genesis 236 e = ep[i];
mpi-genesis 237 c = BITS_PER_MPI_LIMB;
mpi-genesis 238 }
mpi-genesis 239
mpi-genesis 240 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
mpi-genesis 241 * steps. Adjust the result by reducing it with the original MOD.
mpi-genesis 242 *
mpi-genesis 243 * Also make sure the result is put in RES->d (where it already
mpi-genesis 244 * might be, see above).
mpi-genesis 245 */
mpi-genesis 246 if( mod_shift_cnt ) {
mpi-genesis 247 carry_limb = mpihelp_lshift( res->d, rp, rsize, mod_shift_cnt);
mpi-genesis 248 rp = res->d;
mpi-genesis 249 if( carry_limb ) {
mpi-genesis 250 rp[rsize] = carry_limb;
mpi-genesis 251 rsize++;
mpi-genesis 252 }
mpi-genesis 253 }
mpi-genesis 254 else {
mpi-genesis 255 MPN_COPY( res->d, rp, rsize);
mpi-genesis 256 rp = res->d;
mpi-genesis 257 }
mpi-genesis 258
mpi-genesis 259 if( rsize >= msize ) {
mpi-genesis 260 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
mpi-genesis 261 rsize = msize;
mpi-genesis 262 }
mpi-genesis 263
mpi-genesis 264 /* Remove any leading zero words from the result. */
mpi-genesis 265 if( mod_shift_cnt )
mpi-genesis 266 mpihelp_rshift( rp, rp, rsize, mod_shift_cnt);
mpi-genesis 267 MPN_NORMALIZE (rp, rsize);
mpi-genesis 268
mpi-genesis 269 mpihelp_release_karatsuba_ctx( &karactx );
mpi-genesis 270 }
mpi-genesis 271
mpi-genesis 272 if( negative_result && rsize ) {
mpi-genesis 273 if( mod_shift_cnt )
mpi-genesis 274 mpihelp_rshift( mp, mp, msize, mod_shift_cnt);
mpi-genesis 275 mpihelp_sub( rp, mp, msize, rp, rsize);
mpi-genesis 276 rsize = msize;
mpi-genesis 277 rsign = msign;
mpi-genesis 278 MPN_NORMALIZE(rp, rsize);
mpi-genesis 279 }
mpi-genesis 280 res->nlimbs = rsize;
mpi-genesis 281 res->sign = rsign;
mpi-genesis 282
mpi-genesis 283 leave:
mpi-genesis 284 if( assign_rp ) mpi_assign_limb_space( res, rp, size );
mpi-genesis 285 if( mp_marker ) mpi_free_limb_space( mp_marker );
mpi-genesis 286 if( bp_marker ) mpi_free_limb_space( bp_marker );
mpi-genesis 287 if( ep_marker ) mpi_free_limb_space( ep_marker );
mpi-genesis 288 if( xp_marker ) mpi_free_limb_space( xp_marker );
mpi-genesis 289 if( tspace ) mpi_free_limb_space( tspace );
mpi-genesis 290 }
mpi-genesis 291