-
+ BBB0C95D94298E81A2357FDA16069A02AB02DC1B36A48E6585200DF41E235C56EA25317C5A851AA7E9F5C5C5FBEA684D039377205030715C3FD8E06F31D3BA6D
smg_comms/mpi/mpi-pow.c
(0 . 0)(1 . 291)
5047 /* mpi-pow.c - MPI functions
5048 * Modified by No Such Labs. (C) 2015. See README.
5049 *
5050 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
5051 * SHA256(gnupg-1.4.10.tar.gz):
5052 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
5053 * (C) 1994-2005 Free Software Foundation, Inc.
5054 *
5055 * This program is free software: you can redistribute it and/or modify
5056 * it under the terms of the GNU General Public License as published by
5057 * the Free Software Foundation, either version 3 of the License, or
5058 * (at your option) any later version.
5059 *
5060 * This program is distributed in the hope that it will be useful,
5061 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5062 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5063 * GNU General Public License for more details.
5064 *
5065 * You should have received a copy of the GNU General Public License
5066 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5067 */
5068
5069 #include <stdio.h>
5070 #include <stdlib.h>
5071 #include <string.h>
5072 #include <assert.h>
5073
5074 #include "knobs.h"
5075 #include "mpi-internal.h"
5076 #include "longlong.h"
5077
5078
5079
5080 /****************
5081 * RES = BASE ^ EXP mod MOD
5082 */
5083 void
5084 mpi_powm( MPI res, MPI base, MPI exponent, MPI mod)
5085 {
5086 mpi_ptr_t rp, ep, mp, bp;
5087 mpi_size_t esize, msize, bsize, rsize;
5088 int esign, msign, bsign, rsign;
5089 int esec, msec, bsec, rsec;
5090 mpi_size_t size;
5091 int mod_shift_cnt;
5092 int negative_result;
5093 mpi_ptr_t mp_marker=NULL, bp_marker=NULL, ep_marker=NULL;
5094 mpi_ptr_t xp_marker=NULL;
5095 int assign_rp=0;
5096 mpi_ptr_t tspace = NULL;
5097 mpi_size_t tsize=0; /* to avoid compiler warning */
5098 /* fixme: we should check that the warning is void*/
5099
5100 esize = exponent->nlimbs;
5101 msize = mod->nlimbs;
5102 size = 2 * msize;
5103 esign = exponent->sign;
5104 msign = mod->sign;
5105
5106 esec = mpi_is_secure(exponent);
5107 msec = mpi_is_secure(mod);
5108 bsec = mpi_is_secure(base);
5109 rsec = mpi_is_secure(res);
5110
5111 rp = res->d;
5112 ep = exponent->d;
5113
5114 if( !msize )
5115 msize = 1 / msize; /* provoke a signal */
5116
5117 if( !esize ) {
5118 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
5119 * depending on if MOD equals 1. */
5120 rp[0] = 1;
5121 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
5122 res->sign = 0;
5123 goto leave;
5124 }
5125
5126 /* Normalize MOD (i.e. make its most significant bit set) as required by
5127 * mpn_divrem. This will make the intermediate values in the calculation
5128 * slightly larger, but the correct result is obtained after a final
5129 * reduction using the original MOD value. */
5130 mp = mp_marker = mpi_alloc_limb_space(msize, msec);
5131 count_leading_zeros( mod_shift_cnt, mod->d[msize-1] );
5132 if( mod_shift_cnt )
5133 mpihelp_lshift( mp, mod->d, msize, mod_shift_cnt );
5134 else
5135 MPN_COPY( mp, mod->d, msize );
5136
5137 bsize = base->nlimbs;
5138 bsign = base->sign;
5139 if( bsize > msize ) { /* The base is larger than the module. Reduce it. */
5140 /* Allocate (BSIZE + 1) with space for remainder and quotient.
5141 * (The quotient is (bsize - msize + 1) limbs.) */
5142 bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec );
5143 MPN_COPY( bp, base->d, bsize );
5144 /* We don't care about the quotient, store it above the remainder,
5145 * at BP + MSIZE. */
5146 mpihelp_divrem( bp + msize, 0, bp, bsize, mp, msize );
5147 bsize = msize;
5148 /* Canonicalize the base, since we are going to multiply with it
5149 * quite a few times. */
5150 MPN_NORMALIZE( bp, bsize );
5151 }
5152 else
5153 bp = base->d;
5154
5155 if( !bsize ) {
5156 res->nlimbs = 0;
5157 res->sign = 0;
5158 goto leave;
5159 }
5160
5161 if( res->alloced < size ) {
5162 /* We have to allocate more space for RES. If any of the input
5163 * parameters are identical to RES, defer deallocation of the old
5164 * space. */
5165 if( rp == ep || rp == mp || rp == bp ) {
5166 rp = mpi_alloc_limb_space( size, rsec );
5167 assign_rp = 1;
5168 }
5169 else {
5170 mpi_resize( res, size );
5171 rp = res->d;
5172 }
5173 }
5174 else { /* Make BASE, EXPONENT and MOD not overlap with RES. */
5175 if( rp == bp ) {
5176 /* RES and BASE are identical. Allocate temp. space for BASE. */
5177 assert( !bp_marker );
5178 bp = bp_marker = mpi_alloc_limb_space( bsize, bsec );
5179 MPN_COPY(bp, rp, bsize);
5180 }
5181 if( rp == ep ) {
5182 /* RES and EXPONENT are identical.
5183 Allocate temp. space for EXPONENT. */
5184 ep = ep_marker = mpi_alloc_limb_space( esize, esec );
5185 MPN_COPY(ep, rp, esize);
5186 }
5187 if( rp == mp ) {
5188 /* RES and MOD are identical. Allocate temporary space for MOD.*/
5189 assert( !mp_marker );
5190 mp = mp_marker = mpi_alloc_limb_space( msize, msec );
5191 MPN_COPY(mp, rp, msize);
5192 }
5193 }
5194
5195 MPN_COPY( rp, bp, bsize );
5196 rsize = bsize;
5197 rsign = bsign;
5198
5199 {
5200 mpi_size_t i;
5201 mpi_ptr_t xp = xp_marker = mpi_alloc_limb_space( 2 * (msize + 1), msec );
5202 int c;
5203 mpi_limb_t e;
5204 mpi_limb_t carry_limb;
5205 struct karatsuba_ctx karactx;
5206
5207 memset( &karactx, 0, sizeof karactx );
5208 negative_result = (ep[0] & 1) && base->sign;
5209
5210 i = esize - 1;
5211 e = ep[i];
5212 count_leading_zeros (c, e);
5213 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
5214 c = BITS_PER_MPI_LIMB - 1 - c;
5215
5216 /* Main loop.
5217 *
5218 * Make the result be pointed to alternately by XP and RP. This
5219 * helps us avoid block copying, which would otherwise be necessary
5220 * with the overlap restrictions of mpihelp_divmod. With 50% probability
5221 * the result after this loop will be in the area originally pointed
5222 * by RP (==RES->d), and with 50% probability in the area originally
5223 * pointed to by XP.
5224 */
5225
5226 for(;;) {
5227 while( c ) {
5228 mpi_ptr_t tp;
5229 mpi_size_t xsize;
5230
5231 /*mpihelp_mul_n(xp, rp, rp, rsize);*/
5232 if( rsize < KARATSUBA_THRESHOLD )
5233 mpih_sqr_n_basecase( xp, rp, rsize );
5234 else {
5235 if( !tspace ) {
5236 tsize = 2 * rsize;
5237 tspace = mpi_alloc_limb_space( tsize, 0 );
5238 }
5239 else if( tsize < (2*rsize) ) {
5240 mpi_free_limb_space( tspace );
5241 tsize = 2 * rsize;
5242 tspace = mpi_alloc_limb_space( tsize, 0 );
5243 }
5244 mpih_sqr_n( xp, rp, rsize, tspace );
5245 }
5246
5247 xsize = 2 * rsize;
5248 if( xsize > msize ) {
5249 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
5250 xsize = msize;
5251 }
5252
5253 tp = rp; rp = xp; xp = tp;
5254 rsize = xsize;
5255
5256 if( (mpi_limb_signed_t)e < 0 ) {
5257 /*mpihelp_mul( xp, rp, rsize, bp, bsize );*/
5258 if( bsize < KARATSUBA_THRESHOLD ) {
5259 mpihelp_mul( xp, rp, rsize, bp, bsize );
5260 }
5261 else {
5262 mpihelp_mul_karatsuba_case(
5263 xp, rp, rsize, bp, bsize, &karactx );
5264 }
5265
5266 xsize = rsize + bsize;
5267 if( xsize > msize ) {
5268 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
5269 xsize = msize;
5270 }
5271
5272 tp = rp; rp = xp; xp = tp;
5273 rsize = xsize;
5274 }
5275 e <<= 1;
5276 c--;
5277 }
5278
5279 i--;
5280 if( i < 0 )
5281 break;
5282 e = ep[i];
5283 c = BITS_PER_MPI_LIMB;
5284 }
5285
5286 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
5287 * steps. Adjust the result by reducing it with the original MOD.
5288 *
5289 * Also make sure the result is put in RES->d (where it already
5290 * might be, see above).
5291 */
5292 if( mod_shift_cnt ) {
5293 carry_limb = mpihelp_lshift( res->d, rp, rsize, mod_shift_cnt);
5294 rp = res->d;
5295 if( carry_limb ) {
5296 rp[rsize] = carry_limb;
5297 rsize++;
5298 }
5299 }
5300 else {
5301 MPN_COPY( res->d, rp, rsize);
5302 rp = res->d;
5303 }
5304
5305 if( rsize >= msize ) {
5306 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
5307 rsize = msize;
5308 }
5309
5310 /* Remove any leading zero words from the result. */
5311 if( mod_shift_cnt )
5312 mpihelp_rshift( rp, rp, rsize, mod_shift_cnt);
5313 MPN_NORMALIZE (rp, rsize);
5314
5315 mpihelp_release_karatsuba_ctx( &karactx );
5316 }
5317
5318 if( negative_result && rsize ) {
5319 if( mod_shift_cnt )
5320 mpihelp_rshift( mp, mp, msize, mod_shift_cnt);
5321 mpihelp_sub( rp, mp, msize, rp, rsize);
5322 rsize = msize;
5323 rsign = msign;
5324 MPN_NORMALIZE(rp, rsize);
5325 }
5326 res->nlimbs = rsize;
5327 res->sign = rsign;
5328
5329 leave:
5330 if( assign_rp ) mpi_assign_limb_space( res, rp, size );
5331 if( mp_marker ) mpi_free_limb_space( mp_marker );
5332 if( bp_marker ) mpi_free_limb_space( bp_marker );
5333 if( ep_marker ) mpi_free_limb_space( ep_marker );
5334 if( xp_marker ) mpi_free_limb_space( xp_marker );
5335 if( tspace ) mpi_free_limb_space( tspace );
5336 }
5337