-
+ B320709B57B3CF0C00BC98277B037494C62FED93FD99BCA0412EA633F8662844D35ED62990CBE1FC02A47DEE5795D3A7B6B401440F6E80A8A00243868815C06Ampi/mpi-div.c(0 . 0)(1 . 321)
7047 /* mpi-div.c - MPI functions
7048 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
7049 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
7050 *
7051 * This file is part of GnuPG.
7052 *
7053 * GnuPG is free software; you can redistribute it and/or modify
7054 * it under the terms of the GNU General Public License as published by
7055 * the Free Software Foundation; either version 3 of the License, or
7056 * (at your option) any later version.
7057 *
7058 * GnuPG is distributed in the hope that it will be useful,
7059 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7060 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7061 * GNU General Public License for more details.
7062 *
7063 * You should have received a copy of the GNU General Public License
7064 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7065 *
7066 * Note: This code is heavily based on the GNU MP Library.
7067 * Actually it's the same code with only minor changes in the
7068 * way the data is stored; this is to support the abstraction
7069 * of an optional secure memory allocation which may be used
7070 * to avoid revealing of sensitive data due to paging etc.
7071 * The GNU MP Library itself is published under the LGPL;
7072 * however I decided to publish this code under the plain GPL.
7073 */
7074
7075 #include <config.h>
7076 #include <stdio.h>
7077 #include <stdlib.h>
7078 #include "mpi-internal.h"
7079 #include "longlong.h"
7080
7081
7082
7083 void
7084 mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor )
7085 {
7086 int divisor_sign = divisor->sign;
7087 MPI temp_divisor = NULL;
7088
7089 /* We need the original value of the divisor after the remainder has been
7090 * preliminary calculated. We have to copy it to temporary space if it's
7091 * the same variable as REM. */
7092 if( rem == divisor ) {
7093 temp_divisor = mpi_copy( divisor );
7094 divisor = temp_divisor;
7095 }
7096
7097 mpi_tdiv_r( rem, dividend, divisor );
7098
7099 if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs )
7100 mpi_add( rem, rem, divisor);
7101
7102 if( temp_divisor )
7103 mpi_free(temp_divisor);
7104 }
7105
7106
7107
7108 /****************
7109 * Division rounding the quotient towards -infinity.
7110 * The remainder gets the same sign as the denominator.
7111 * rem is optional
7112 */
7113
7114 ulong
7115 mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor )
7116 {
7117 mpi_limb_t rlimb;
7118
7119 rlimb = mpihelp_mod_1( dividend->d, dividend->nlimbs, divisor );
7120 if( rlimb && dividend->sign )
7121 rlimb = divisor - rlimb;
7122
7123 if( rem ) {
7124 rem->d[0] = rlimb;
7125 rem->nlimbs = rlimb? 1:0;
7126 }
7127 return rlimb;
7128 }
7129
7130
7131 void
7132 mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor )
7133 {
7134 MPI tmp = mpi_alloc( mpi_get_nlimbs(quot) );
7135 mpi_fdiv_qr( quot, tmp, dividend, divisor);
7136 mpi_free(tmp);
7137 }
7138
7139 void
7140 mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor )
7141 {
7142 int divisor_sign = divisor->sign;
7143 MPI temp_divisor = NULL;
7144
7145 if( quot == divisor || rem == divisor ) {
7146 temp_divisor = mpi_copy( divisor );
7147 divisor = temp_divisor;
7148 }
7149
7150 mpi_tdiv_qr( quot, rem, dividend, divisor );
7151
7152 if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) {
7153 mpi_sub_ui( quot, quot, 1 );
7154 mpi_add( rem, rem, divisor);
7155 }
7156
7157 if( temp_divisor )
7158 mpi_free(temp_divisor);
7159 }
7160
7161
7162 /* If den == quot, den needs temporary storage.
7163 * If den == rem, den needs temporary storage.
7164 * If num == quot, num needs temporary storage.
7165 * If den has temporary storage, it can be normalized while being copied,
7166 * i.e no extra storage should be allocated.
7167 */
7168
7169 void
7170 mpi_tdiv_r( MPI rem, MPI num, MPI den)
7171 {
7172 mpi_tdiv_qr(NULL, rem, num, den );
7173 }
7174
7175 void
7176 mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den)
7177 {
7178 mpi_ptr_t np, dp;
7179 mpi_ptr_t qp, rp;
7180 mpi_size_t nsize = num->nlimbs;
7181 mpi_size_t dsize = den->nlimbs;
7182 mpi_size_t qsize, rsize;
7183 mpi_size_t sign_remainder = num->sign;
7184 mpi_size_t sign_quotient = num->sign ^ den->sign;
7185 unsigned normalization_steps;
7186 mpi_limb_t q_limb;
7187 mpi_ptr_t marker[5];
7188 int markidx=0;
7189
7190 /* Ensure space is enough for quotient and remainder.
7191 * We need space for an extra limb in the remainder, because it's
7192 * up-shifted (normalized) below. */
7193 rsize = nsize + 1;
7194 mpi_resize( rem, rsize);
7195
7196 qsize = rsize - dsize; /* qsize cannot be bigger than this. */
7197 if( qsize <= 0 ) {
7198 if( num != rem ) {
7199 rem->nlimbs = num->nlimbs;
7200 rem->sign = num->sign;
7201 MPN_COPY(rem->d, num->d, nsize);
7202 }
7203 if( quot ) {
7204 /* This needs to follow the assignment to rem, in case the
7205 * numerator and quotient are the same. */
7206 quot->nlimbs = 0;
7207 quot->sign = 0;
7208 }
7209 return;
7210 }
7211
7212 if( quot )
7213 mpi_resize( quot, qsize);
7214
7215 /* Read pointers here, when reallocation is finished. */
7216 np = num->d;
7217 dp = den->d;
7218 rp = rem->d;
7219
7220 /* Optimize division by a single-limb divisor. */
7221 if( dsize == 1 ) {
7222 mpi_limb_t rlimb;
7223 if( quot ) {
7224 qp = quot->d;
7225 rlimb = mpihelp_divmod_1( qp, np, nsize, dp[0] );
7226 qsize -= qp[qsize - 1] == 0;
7227 quot->nlimbs = qsize;
7228 quot->sign = sign_quotient;
7229 }
7230 else
7231 rlimb = mpihelp_mod_1( np, nsize, dp[0] );
7232 rp[0] = rlimb;
7233 rsize = rlimb != 0?1:0;
7234 rem->nlimbs = rsize;
7235 rem->sign = sign_remainder;
7236 return;
7237 }
7238
7239
7240 if( quot ) {
7241 qp = quot->d;
7242 /* Make sure QP and NP point to different objects. Otherwise the
7243 * numerator would be gradually overwritten by the quotient limbs. */
7244 if(qp == np) { /* Copy NP object to temporary space. */
7245 np = marker[markidx++] = mpi_alloc_limb_space(nsize,
7246 mpi_is_secure(quot));
7247 MPN_COPY(np, qp, nsize);
7248 }
7249 }
7250 else /* Put quotient at top of remainder. */
7251 qp = rp + dsize;
7252
7253 count_leading_zeros( normalization_steps, dp[dsize - 1] );
7254
7255 /* Normalize the denominator, i.e. make its most significant bit set by
7256 * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
7257 * numerator the same number of steps (to keep the quotient the same!).
7258 */
7259 if( normalization_steps ) {
7260 mpi_ptr_t tp;
7261 mpi_limb_t nlimb;
7262
7263 /* Shift up the denominator setting the most significant bit of
7264 * the most significant word. Use temporary storage not to clobber
7265 * the original contents of the denominator. */
7266 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den));
7267 mpihelp_lshift( tp, dp, dsize, normalization_steps );
7268 dp = tp;
7269
7270 /* Shift up the numerator, possibly introducing a new most
7271 * significant word. Move the shifted numerator in the remainder
7272 * meanwhile. */
7273 nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
7274 if( nlimb ) {
7275 rp[nsize] = nlimb;
7276 rsize = nsize + 1;
7277 }
7278 else
7279 rsize = nsize;
7280 }
7281 else {
7282 /* The denominator is already normalized, as required. Copy it to
7283 * temporary space if it overlaps with the quotient or remainder. */
7284 if( dp == rp || (quot && (dp == qp))) {
7285 mpi_ptr_t tp;
7286
7287 tp = marker[markidx++] = mpi_alloc_limb_space(dsize, mpi_is_secure(den));
7288 MPN_COPY( tp, dp, dsize );
7289 dp = tp;
7290 }
7291
7292 /* Move the numerator to the remainder. */
7293 if( rp != np )
7294 MPN_COPY(rp, np, nsize);
7295
7296 rsize = nsize;
7297 }
7298
7299 q_limb = mpihelp_divrem( qp, 0, rp, rsize, dp, dsize );
7300
7301 if( quot ) {
7302 qsize = rsize - dsize;
7303 if(q_limb) {
7304 qp[qsize] = q_limb;
7305 qsize += 1;
7306 }
7307
7308 quot->nlimbs = qsize;
7309 quot->sign = sign_quotient;
7310 }
7311
7312 rsize = dsize;
7313 MPN_NORMALIZE (rp, rsize);
7314
7315 if( normalization_steps && rsize ) {
7316 mpihelp_rshift(rp, rp, rsize, normalization_steps);
7317 rsize -= rp[rsize - 1] == 0?1:0;
7318 }
7319
7320 rem->nlimbs = rsize;
7321 rem->sign = sign_remainder;
7322 while( markidx )
7323 mpi_free_limb_space(marker[--markidx]);
7324 }
7325
7326 void
7327 mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count )
7328 {
7329 mpi_size_t usize, wsize;
7330 mpi_size_t limb_cnt;
7331
7332 usize = u->nlimbs;
7333 limb_cnt = count / BITS_PER_MPI_LIMB;
7334 wsize = usize - limb_cnt;
7335 if( limb_cnt >= usize )
7336 w->nlimbs = 0;
7337 else {
7338 mpi_ptr_t wp;
7339 mpi_ptr_t up;
7340
7341 RESIZE_IF_NEEDED( w, wsize );
7342 wp = w->d;
7343 up = u->d;
7344
7345 count %= BITS_PER_MPI_LIMB;
7346 if( count ) {
7347 mpihelp_rshift( wp, up + limb_cnt, wsize, count );
7348 wsize -= !wp[wsize - 1];
7349 }
7350 else {
7351 MPN_COPY_INCR( wp, up + limb_cnt, wsize);
7352 }
7353
7354 w->nlimbs = wsize;
7355 }
7356 }
7357
7358 /****************
7359 * Check whether dividend is divisible by divisor
7360 * (note: divisor must fit into a limb)
7361 */
7362 int
7363 mpi_divisible_ui(MPI dividend, ulong divisor )
7364 {
7365 return !mpihelp_mod_1( dividend->d, dividend->nlimbs, divisor );
7366 }
7367