mpi-genesis 1
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:
mpi-genesis 21 */
mpi-genesis 22
mpi-genesis 23 #include <stdio.h>
mpi-genesis 24 #include <stdlib.h>
mpi_second_cut 25
mpi_second_cut 26 #include "knobs.h"
mpi-genesis 27 #include "mpi-internal.h"
mpi-genesis 28 #include "longlong.h"
mpi-genesis 29
mpi-genesis 30
mpi-genesis 31
mpi-genesis 32 void
mpi-genesis 33 mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor )
mpi-genesis 34 {
mpi-genesis 35 int divisor_sign = divisor->sign;
mpi-genesis 36 MPI temp_divisor = NULL;
mpi-genesis 37
mpi-genesis 38
mpi-genesis 39 * preliminary calculated. We have to copy it to temporary space if it's
mpi-genesis 40 * the same variable as REM. */
mpi-genesis 41 if( rem == divisor ) {
mpi-genesis 42 temp_divisor = mpi_copy( divisor );
mpi-genesis 43 divisor = temp_divisor;
mpi-genesis 44 }
mpi-genesis 45
mpi-genesis 46 mpi_tdiv_r( rem, dividend, divisor );
mpi-genesis 47
mpi-genesis 48 if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs )
mpi-genesis 49 mpi_add( rem, rem, divisor);
mpi-genesis 50
mpi-genesis 51 if( temp_divisor )
mpi-genesis 52 mpi_free(temp_divisor);
mpi-genesis 53 }
mpi-genesis 54
mpi-genesis 55
mpi-genesis 56
mpi-genesis 57
mpi-genesis 58 * Division rounding the quotient towards -infinity.
mpi-genesis 59 * The remainder gets the same sign as the denominator.
mpi-genesis 60 * rem is optional
mpi-genesis 61 */
mpi-genesis 62
mpi-genesis 63 ulong
mpi-genesis 64 mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor )
mpi-genesis 65 {
mpi-genesis 66 mpi_limb_t rlimb;
mpi-genesis 67
mpi-genesis 68 rlimb = mpihelp_mod_1( dividend->d, dividend->nlimbs, divisor );
mpi-genesis 69 if( rlimb && dividend->sign )
mpi-genesis 70 rlimb = divisor - rlimb;
mpi-genesis 71
mpi-genesis 72 if( rem ) {
mpi-genesis 73 rem->d[0] = rlimb;
mpi-genesis 74 rem->nlimbs = rlimb? 1:0;
mpi-genesis 75 }
mpi-genesis 76 return rlimb;
mpi-genesis 77 }
mpi-genesis 78
mpi-genesis 79
mpi-genesis 80 void
mpi-genesis 81 mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor )
mpi-genesis 82 {
mpi-genesis 83 MPI tmp = mpi_alloc( mpi_get_nlimbs(quot) );
mpi-genesis 84 mpi_fdiv_qr( quot, tmp, dividend, divisor);
mpi-genesis 85 mpi_free(tmp);
mpi-genesis 86 }
mpi-genesis 87
mpi-genesis 88 void
mpi-genesis 89 mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor )
mpi-genesis 90 {
mpi-genesis 91 int divisor_sign = divisor->sign;
mpi-genesis 92 MPI temp_divisor = NULL;
mpi-genesis 93
mpi-genesis 94 if( quot == divisor || rem == divisor ) {
mpi-genesis 95 temp_divisor = mpi_copy( divisor );
mpi-genesis 96 divisor = temp_divisor;
mpi-genesis 97 }
mpi-genesis 98
mpi-genesis 99 mpi_tdiv_qr( quot, rem, dividend, divisor );
mpi-genesis 100
mpi-genesis 101 if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) {
mpi-genesis 102 mpi_sub_ui( quot, quot, 1 );
mpi-genesis 103 mpi_add( rem, rem, divisor);
mpi-genesis 104 }
mpi-genesis 105
mpi-genesis 106 if( temp_divisor )
mpi-genesis 107 mpi_free(temp_divisor);
mpi-genesis 108 }
mpi-genesis 109
mpi-genesis 110
mpi-genesis 111
mpi-genesis 112 * If den == rem, den needs temporary storage.
mpi-genesis 113 * If num == quot, num needs temporary storage.
mpi-genesis 114 * If den has temporary storage, it can be normalized while being copied,
mpi-genesis 115 * i.e no extra storage should be allocated.
mpi-genesis 116 */
mpi-genesis 117
mpi-genesis 118 void
mpi-genesis 119 mpi_tdiv_r( MPI rem, MPI num, MPI den)
mpi-genesis 120 {
mpi-genesis 121 mpi_tdiv_qr(NULL, rem, num, den );
mpi-genesis 122 }
mpi-genesis 123
mpi-genesis 124 void
mpi-genesis 125 mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den)
mpi-genesis 126 {
mpi-genesis 127 mpi_ptr_t np, dp;
mpi-genesis 128 mpi_ptr_t qp, rp;
mpi-genesis 129 mpi_size_t nsize = num->nlimbs;
mpi-genesis 130 mpi_size_t dsize = den->nlimbs;
mpi-genesis 131 mpi_size_t qsize, rsize;
mpi-genesis 132 mpi_size_t sign_remainder = num->sign;
mpi-genesis 133 mpi_size_t sign_quotient = num->sign ^ den->sign;
mpi-genesis 134 unsigned normalization_steps;
mpi-genesis 135 mpi_limb_t q_limb;
mpi-genesis 136 mpi_ptr_t marker[5];
mpi-genesis 137 int markidx=0;
mpi-genesis 138
mpi-genesis 139
mpi-genesis 140 * We need space for an extra limb in the remainder, because it's
mpi-genesis 141 * up-shifted (normalized) below. */
mpi-genesis 142 rsize = nsize + 1;
mpi-genesis 143 mpi_resize( rem, rsize);
mpi-genesis 144
mpi-genesis 145 qsize = rsize - dsize;
mpi-genesis 146 if( qsize <= 0 ) {
mpi-genesis 147 if( num != rem ) {
mpi-genesis 148 rem->nlimbs = num->nlimbs;
mpi-genesis 149 rem->sign = num->sign;
mpi-genesis 150 MPN_COPY(rem->d, num->d, nsize);
mpi-genesis 151 }
mpi-genesis 152 if( quot ) {
mpi-genesis 153
mpi-genesis 154 * numerator and quotient are the same. */
mpi-genesis 155 quot->nlimbs = 0;
mpi-genesis 156 quot->sign = 0;
mpi-genesis 157 }
mpi-genesis 158 return;
mpi-genesis 159 }
mpi-genesis 160
mpi-genesis 161 if( quot )
mpi-genesis 162 mpi_resize( quot, qsize);
mpi-genesis 163
mpi-genesis 164
mpi-genesis 165 np = num->d;
mpi-genesis 166 dp = den->d;
mpi-genesis 167 rp = rem->d;
mpi-genesis 168
mpi-genesis 169
mpi-genesis 170 if( dsize == 1 ) {
mpi-genesis 171 mpi_limb_t rlimb;
mpi-genesis 172 if( quot ) {
mpi-genesis 173 qp = quot->d;
mpi-genesis 174 rlimb = mpihelp_divmod_1( qp, np, nsize, dp[0] );
mpi-genesis 175 qsize -= qp[qsize - 1] == 0;
mpi-genesis 176 quot->nlimbs = qsize;
mpi-genesis 177 quot->sign = sign_quotient;
mpi-genesis 178 }
mpi-genesis 179 else
mpi-genesis 180 rlimb = mpihelp_mod_1( np, nsize, dp[0] );
mpi-genesis 181 rp[0] = rlimb;
mpi-genesis 182 rsize = rlimb != 0?1:0;
mpi-genesis 183 rem->nlimbs = rsize;
mpi-genesis 184 rem->sign = sign_remainder;
mpi-genesis 185 return;
mpi-genesis 186 }
mpi-genesis 187
mpi-genesis 188
mpi-genesis 189 if( quot ) {
mpi-genesis 190 qp = quot->d;
mpi-genesis 191
mpi-genesis 192 * numerator would be gradually overwritten by the quotient limbs. */
mpi-genesis 193 if(qp == np) {
mpi-genesis 194 np = marker[markidx++] = mpi_alloc_limb_space(nsize,
mpi-genesis 195 mpi_is_secure(quot));
mpi-genesis 196 MPN_COPY(np, qp, nsize);
mpi-genesis 197 }
mpi-genesis 198 }
mpi-genesis 199 else
mpi-genesis 200 qp = rp + dsize;
mpi-genesis 201
mpi-genesis 202 count_leading_zeros( normalization_steps, dp[dsize - 1] );
mpi-genesis 203
mpi-genesis 204
mpi-genesis 205 * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
mpi-genesis 206 * numerator the same number of steps (to keep the quotient the same!).
mpi-genesis 207 */
mpi-genesis 208 if( normalization_steps ) {
mpi-genesis 209 mpi_ptr_t tp;
mpi-genesis 210 mpi_limb_t nlimb;
mpi-genesis 211
mpi-genesis 212
mpi-genesis 213 * the most significant word. Use temporary storage not to clobber
mpi-genesis 214 * the original contents of the denominator. */
mpi-genesis 215 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den));
mpi-genesis 216 mpihelp_lshift( tp, dp, dsize, normalization_steps );
mpi-genesis 217 dp = tp;
mpi-genesis 218
mpi-genesis 219
mpi-genesis 220 * significant word. Move the shifted numerator in the remainder
mpi-genesis 221 * meanwhile. */
mpi-genesis 222 nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
mpi-genesis 223 if( nlimb ) {
mpi-genesis 224 rp[nsize] = nlimb;
mpi-genesis 225 rsize = nsize + 1;
mpi-genesis 226 }
mpi-genesis 227 else
mpi-genesis 228 rsize = nsize;
mpi-genesis 229 }
mpi-genesis 230 else {
mpi-genesis 231
mpi-genesis 232 * temporary space if it overlaps with the quotient or remainder. */
mpi-genesis 233 if( dp == rp || (quot && (dp == qp))) {
mpi-genesis 234 mpi_ptr_t tp;
mpi-genesis 235
mpi-genesis 236 tp = marker[markidx++] = mpi_alloc_limb_space(dsize, mpi_is_secure(den));
mpi-genesis 237 MPN_COPY( tp, dp, dsize );
mpi-genesis 238 dp = tp;
mpi-genesis 239 }
mpi-genesis 240
mpi-genesis 241
mpi-genesis 242 if( rp != np )
mpi-genesis 243 MPN_COPY(rp, np, nsize);
mpi-genesis 244
mpi-genesis 245 rsize = nsize;
mpi-genesis 246 }
mpi-genesis 247
mpi-genesis 248 q_limb = mpihelp_divrem( qp, 0, rp, rsize, dp, dsize );
mpi-genesis 249
mpi-genesis 250 if( quot ) {
mpi-genesis 251 qsize = rsize - dsize;
mpi-genesis 252 if(q_limb) {
mpi-genesis 253 qp[qsize] = q_limb;
mpi-genesis 254 qsize += 1;
mpi-genesis 255 }
mpi-genesis 256
mpi-genesis 257 quot->nlimbs = qsize;
mpi-genesis 258 quot->sign = sign_quotient;
mpi-genesis 259 }
mpi-genesis 260
mpi-genesis 261 rsize = dsize;
mpi-genesis 262 MPN_NORMALIZE (rp, rsize);
mpi-genesis 263
mpi-genesis 264 if( normalization_steps && rsize ) {
mpi-genesis 265 mpihelp_rshift(rp, rp, rsize, normalization_steps);
mpi-genesis 266 rsize -= rp[rsize - 1] == 0?1:0;
mpi-genesis 267 }
mpi-genesis 268
mpi-genesis 269 rem->nlimbs = rsize;
mpi-genesis 270 rem->sign = sign_remainder;
mpi-genesis 271 while( markidx )
mpi-genesis 272 mpi_free_limb_space(marker[--markidx]);
mpi-genesis 273 }
mpi-genesis 274
mpi-genesis 275 void
mpi-genesis 276 mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count )
mpi-genesis 277 {
mpi-genesis 278 mpi_size_t usize, wsize;
mpi-genesis 279 mpi_size_t limb_cnt;
mpi-genesis 280
mpi-genesis 281 usize = u->nlimbs;
mpi-genesis 282 limb_cnt = count / BITS_PER_MPI_LIMB;
mpi-genesis 283 wsize = usize - limb_cnt;
mpi-genesis 284 if( limb_cnt >= usize )
mpi-genesis 285 w->nlimbs = 0;
mpi-genesis 286 else {
mpi-genesis 287 mpi_ptr_t wp;
mpi-genesis 288 mpi_ptr_t up;
mpi-genesis 289
mpi-genesis 290 RESIZE_IF_NEEDED( w, wsize );
mpi-genesis 291 wp = w->d;
mpi-genesis 292 up = u->d;
mpi-genesis 293
mpi-genesis 294 count %= BITS_PER_MPI_LIMB;
mpi-genesis 295 if( count ) {
mpi-genesis 296 mpihelp_rshift( wp, up + limb_cnt, wsize, count );
mpi-genesis 297 wsize -= !wp[wsize - 1];
mpi-genesis 298 }
mpi-genesis 299 else {
mpi-genesis 300 MPN_COPY_INCR( wp, up + limb_cnt, wsize);
mpi-genesis 301 }
mpi-genesis 302
mpi-genesis 303 w->nlimbs = wsize;
mpi-genesis 304 }
mpi-genesis 305 }
mpi-genesis 306
mpi-genesis 307
mpi-genesis 308 * Check whether dividend is divisible by divisor
mpi-genesis 309 * (note: divisor must fit into a limb)
mpi-genesis 310 */
mpi-genesis 311 int
mpi-genesis 312 mpi_divisible_ui(MPI dividend, ulong divisor )
mpi-genesis 313 {
mpi-genesis 314 return !mpihelp_mod_1( dividend->d, dividend->nlimbs, divisor );
mpi-genesis 315 }
mpi-genesis 316