mpi-genesis 1
mpi-genesis 2 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
mpi-genesis 3 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
mpi-genesis 4 *
mpi-genesis 5 * This file is part of GnuPG.
mpi-genesis 6 *
mpi-genesis 7 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 8 * it under the terms of the GNU General Public License as published by
mpi-genesis 9 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 10 * (at your option) any later version.
mpi-genesis 11 *
mpi-genesis 12 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 15 * GNU General Public License for more details.
mpi-genesis 16 *
mpi-genesis 17 * You should have received a copy of the GNU General Public License
mpi-genesis 18 * along with this program; if not, see <http:
mpi-genesis 19 *
mpi-genesis 20 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis 21 * Actually it's the same code with only minor changes in the
mpi-genesis 22 * way the data is stored; this is to support the abstraction
mpi-genesis 23 * of an optional secure memory allocation which may be used
mpi-genesis 24 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis 25 * The GNU MP Library itself is published under the LGPL;
mpi-genesis 26 * however I decided to publish this code under the plain GPL.
mpi-genesis 27 */
mpi-genesis 28
mpi-genesis 29 #include <config.h>
mpi-genesis 30 #include <stdio.h>
mpi-genesis 31 #include <stdlib.h>
mpi-genesis 32 #include "mpi-internal.h"
mpi-genesis 33 #include "longlong.h"
mpi-genesis 34
mpi-genesis 35
mpi-genesis 36
mpi-genesis 37 void
mpi-genesis 38 mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor )
mpi-genesis 39 {
mpi-genesis 40 int divisor_sign = divisor->sign;
mpi-genesis 41 MPI temp_divisor = NULL;
mpi-genesis 42
mpi-genesis 43
mpi-genesis 44 * preliminary calculated. We have to copy it to temporary space if it's
mpi-genesis 45 * the same variable as REM. */
mpi-genesis 46 if( rem == divisor ) {
mpi-genesis 47 temp_divisor = mpi_copy( divisor );
mpi-genesis 48 divisor = temp_divisor;
mpi-genesis 49 }
mpi-genesis 50
mpi-genesis 51 mpi_tdiv_r( rem, dividend, divisor );
mpi-genesis 52
mpi-genesis 53 if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs )
mpi-genesis 54 mpi_add( rem, rem, divisor);
mpi-genesis 55
mpi-genesis 56 if( temp_divisor )
mpi-genesis 57 mpi_free(temp_divisor);
mpi-genesis 58 }
mpi-genesis 59
mpi-genesis 60
mpi-genesis 61
mpi-genesis 62
mpi-genesis 63 * Division rounding the quotient towards -infinity.
mpi-genesis 64 * The remainder gets the same sign as the denominator.
mpi-genesis 65 * rem is optional
mpi-genesis 66 */
mpi-genesis 67
mpi-genesis 68 ulong
mpi-genesis 69 mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor )
mpi-genesis 70 {
mpi-genesis 71 mpi_limb_t rlimb;
mpi-genesis 72
mpi-genesis 73 rlimb = mpihelp_mod_1( dividend->d, dividend->nlimbs, divisor );
mpi-genesis 74 if( rlimb && dividend->sign )
mpi-genesis 75 rlimb = divisor - rlimb;
mpi-genesis 76
mpi-genesis 77 if( rem ) {
mpi-genesis 78 rem->d[0] = rlimb;
mpi-genesis 79 rem->nlimbs = rlimb? 1:0;
mpi-genesis 80 }
mpi-genesis 81 return rlimb;
mpi-genesis 82 }
mpi-genesis 83
mpi-genesis 84
mpi-genesis 85 void
mpi-genesis 86 mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor )
mpi-genesis 87 {
mpi-genesis 88 MPI tmp = mpi_alloc( mpi_get_nlimbs(quot) );
mpi-genesis 89 mpi_fdiv_qr( quot, tmp, dividend, divisor);
mpi-genesis 90 mpi_free(tmp);
mpi-genesis 91 }
mpi-genesis 92
mpi-genesis 93 void
mpi-genesis 94 mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor )
mpi-genesis 95 {
mpi-genesis 96 int divisor_sign = divisor->sign;
mpi-genesis 97 MPI temp_divisor = NULL;
mpi-genesis 98
mpi-genesis 99 if( quot == divisor || rem == divisor ) {
mpi-genesis 100 temp_divisor = mpi_copy( divisor );
mpi-genesis 101 divisor = temp_divisor;
mpi-genesis 102 }
mpi-genesis 103
mpi-genesis 104 mpi_tdiv_qr( quot, rem, dividend, divisor );
mpi-genesis 105
mpi-genesis 106 if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) {
mpi-genesis 107 mpi_sub_ui( quot, quot, 1 );
mpi-genesis 108 mpi_add( rem, rem, divisor);
mpi-genesis 109 }
mpi-genesis 110
mpi-genesis 111 if( temp_divisor )
mpi-genesis 112 mpi_free(temp_divisor);
mpi-genesis 113 }
mpi-genesis 114
mpi-genesis 115
mpi-genesis 116
mpi-genesis 117 * If den == rem, den needs temporary storage.
mpi-genesis 118 * If num == quot, num needs temporary storage.
mpi-genesis 119 * If den has temporary storage, it can be normalized while being copied,
mpi-genesis 120 * i.e no extra storage should be allocated.
mpi-genesis 121 */
mpi-genesis 122
mpi-genesis 123 void
mpi-genesis 124 mpi_tdiv_r( MPI rem, MPI num, MPI den)
mpi-genesis 125 {
mpi-genesis 126 mpi_tdiv_qr(NULL, rem, num, den );
mpi-genesis 127 }
mpi-genesis 128
mpi-genesis 129 void
mpi-genesis 130 mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den)
mpi-genesis 131 {
mpi-genesis 132 mpi_ptr_t np, dp;
mpi-genesis 133 mpi_ptr_t qp, rp;
mpi-genesis 134 mpi_size_t nsize = num->nlimbs;
mpi-genesis 135 mpi_size_t dsize = den->nlimbs;
mpi-genesis 136 mpi_size_t qsize, rsize;
mpi-genesis 137 mpi_size_t sign_remainder = num->sign;
mpi-genesis 138 mpi_size_t sign_quotient = num->sign ^ den->sign;
mpi-genesis 139 unsigned normalization_steps;
mpi-genesis 140 mpi_limb_t q_limb;
mpi-genesis 141 mpi_ptr_t marker[5];
mpi-genesis 142 int markidx=0;
mpi-genesis 143
mpi-genesis 144
mpi-genesis 145 * We need space for an extra limb in the remainder, because it's
mpi-genesis 146 * up-shifted (normalized) below. */
mpi-genesis 147 rsize = nsize + 1;
mpi-genesis 148 mpi_resize( rem, rsize);
mpi-genesis 149
mpi-genesis 150 qsize = rsize - dsize;
mpi-genesis 151 if( qsize <= 0 ) {
mpi-genesis 152 if( num != rem ) {
mpi-genesis 153 rem->nlimbs = num->nlimbs;
mpi-genesis 154 rem->sign = num->sign;
mpi-genesis 155 MPN_COPY(rem->d, num->d, nsize);
mpi-genesis 156 }
mpi-genesis 157 if( quot ) {
mpi-genesis 158
mpi-genesis 159 * numerator and quotient are the same. */
mpi-genesis 160 quot->nlimbs = 0;
mpi-genesis 161 quot->sign = 0;
mpi-genesis 162 }
mpi-genesis 163 return;
mpi-genesis 164 }
mpi-genesis 165
mpi-genesis 166 if( quot )
mpi-genesis 167 mpi_resize( quot, qsize);
mpi-genesis 168
mpi-genesis 169
mpi-genesis 170 np = num->d;
mpi-genesis 171 dp = den->d;
mpi-genesis 172 rp = rem->d;
mpi-genesis 173
mpi-genesis 174
mpi-genesis 175 if( dsize == 1 ) {
mpi-genesis 176 mpi_limb_t rlimb;
mpi-genesis 177 if( quot ) {
mpi-genesis 178 qp = quot->d;
mpi-genesis 179 rlimb = mpihelp_divmod_1( qp, np, nsize, dp[0] );
mpi-genesis 180 qsize -= qp[qsize - 1] == 0;
mpi-genesis 181 quot->nlimbs = qsize;
mpi-genesis 182 quot->sign = sign_quotient;
mpi-genesis 183 }
mpi-genesis 184 else
mpi-genesis 185 rlimb = mpihelp_mod_1( np, nsize, dp[0] );
mpi-genesis 186 rp[0] = rlimb;
mpi-genesis 187 rsize = rlimb != 0?1:0;
mpi-genesis 188 rem->nlimbs = rsize;
mpi-genesis 189 rem->sign = sign_remainder;
mpi-genesis 190 return;
mpi-genesis 191 }
mpi-genesis 192
mpi-genesis 193
mpi-genesis 194 if( quot ) {
mpi-genesis 195 qp = quot->d;
mpi-genesis 196
mpi-genesis 197 * numerator would be gradually overwritten by the quotient limbs. */
mpi-genesis 198 if(qp == np) {
mpi-genesis 199 np = marker[markidx++] = mpi_alloc_limb_space(nsize,
mpi-genesis 200 mpi_is_secure(quot));
mpi-genesis 201 MPN_COPY(np, qp, nsize);
mpi-genesis 202 }
mpi-genesis 203 }
mpi-genesis 204 else
mpi-genesis 205 qp = rp + dsize;
mpi-genesis 206
mpi-genesis 207 count_leading_zeros( normalization_steps, dp[dsize - 1] );
mpi-genesis 208
mpi-genesis 209
mpi-genesis 210 * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
mpi-genesis 211 * numerator the same number of steps (to keep the quotient the same!).
mpi-genesis 212 */
mpi-genesis 213 if( normalization_steps ) {
mpi-genesis 214 mpi_ptr_t tp;
mpi-genesis 215 mpi_limb_t nlimb;
mpi-genesis 216
mpi-genesis 217
mpi-genesis 218 * the most significant word. Use temporary storage not to clobber
mpi-genesis 219 * the original contents of the denominator. */
mpi-genesis 220 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den));
mpi-genesis 221 mpihelp_lshift( tp, dp, dsize, normalization_steps );
mpi-genesis 222 dp = tp;
mpi-genesis 223
mpi-genesis 224
mpi-genesis 225 * significant word. Move the shifted numerator in the remainder
mpi-genesis 226 * meanwhile. */
mpi-genesis 227 nlimb = mpihelp_lshift(rp, np, nsize, normalization_steps);
mpi-genesis 228 if( nlimb ) {
mpi-genesis 229 rp[nsize] = nlimb;
mpi-genesis 230 rsize = nsize + 1;
mpi-genesis 231 }
mpi-genesis 232 else
mpi-genesis 233 rsize = nsize;
mpi-genesis 234 }
mpi-genesis 235 else {
mpi-genesis 236
mpi-genesis 237 * temporary space if it overlaps with the quotient or remainder. */
mpi-genesis 238 if( dp == rp || (quot && (dp == qp))) {
mpi-genesis 239 mpi_ptr_t tp;
mpi-genesis 240
mpi-genesis 241 tp = marker[markidx++] = mpi_alloc_limb_space(dsize, mpi_is_secure(den));
mpi-genesis 242 MPN_COPY( tp, dp, dsize );
mpi-genesis 243 dp = tp;
mpi-genesis 244 }
mpi-genesis 245
mpi-genesis 246
mpi-genesis 247 if( rp != np )
mpi-genesis 248 MPN_COPY(rp, np, nsize);
mpi-genesis 249
mpi-genesis 250 rsize = nsize;
mpi-genesis 251 }
mpi-genesis 252
mpi-genesis 253 q_limb = mpihelp_divrem( qp, 0, rp, rsize, dp, dsize );
mpi-genesis 254
mpi-genesis 255 if( quot ) {
mpi-genesis 256 qsize = rsize - dsize;
mpi-genesis 257 if(q_limb) {
mpi-genesis 258 qp[qsize] = q_limb;
mpi-genesis 259 qsize += 1;
mpi-genesis 260 }
mpi-genesis 261
mpi-genesis 262 quot->nlimbs = qsize;
mpi-genesis 263 quot->sign = sign_quotient;
mpi-genesis 264 }
mpi-genesis 265
mpi-genesis 266 rsize = dsize;
mpi-genesis 267 MPN_NORMALIZE (rp, rsize);
mpi-genesis 268
mpi-genesis 269 if( normalization_steps && rsize ) {
mpi-genesis 270 mpihelp_rshift(rp, rp, rsize, normalization_steps);
mpi-genesis 271 rsize -= rp[rsize - 1] == 0?1:0;
mpi-genesis 272 }
mpi-genesis 273
mpi-genesis 274 rem->nlimbs = rsize;
mpi-genesis 275 rem->sign = sign_remainder;
mpi-genesis 276 while( markidx )
mpi-genesis 277 mpi_free_limb_space(marker[--markidx]);
mpi-genesis 278 }
mpi-genesis 279
mpi-genesis 280 void
mpi-genesis 281 mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count )
mpi-genesis 282 {
mpi-genesis 283 mpi_size_t usize, wsize;
mpi-genesis 284 mpi_size_t limb_cnt;
mpi-genesis 285
mpi-genesis 286 usize = u->nlimbs;
mpi-genesis 287 limb_cnt = count / BITS_PER_MPI_LIMB;
mpi-genesis 288 wsize = usize - limb_cnt;
mpi-genesis 289 if( limb_cnt >= usize )
mpi-genesis 290 w->nlimbs = 0;
mpi-genesis 291 else {
mpi-genesis 292 mpi_ptr_t wp;
mpi-genesis 293 mpi_ptr_t up;
mpi-genesis 294
mpi-genesis 295 RESIZE_IF_NEEDED( w, wsize );
mpi-genesis 296 wp = w->d;
mpi-genesis 297 up = u->d;
mpi-genesis 298
mpi-genesis 299 count %= BITS_PER_MPI_LIMB;
mpi-genesis 300 if( count ) {
mpi-genesis 301 mpihelp_rshift( wp, up + limb_cnt, wsize, count );
mpi-genesis 302 wsize -= !wp[wsize - 1];
mpi-genesis 303 }
mpi-genesis 304 else {
mpi-genesis 305 MPN_COPY_INCR( wp, up + limb_cnt, wsize);
mpi-genesis 306 }
mpi-genesis 307
mpi-genesis 308 w->nlimbs = wsize;
mpi-genesis 309 }
mpi-genesis 310 }
mpi-genesis 311
mpi-genesis 312
mpi-genesis 313 * Check whether dividend is divisible by divisor
mpi-genesis 314 * (note: divisor must fit into a limb)
mpi-genesis 315 */
mpi-genesis 316 int
mpi-genesis 317 mpi_divisible_ui(MPI dividend, ulong divisor )
mpi-genesis 318 {
mpi-genesis 319 return !mpihelp_mod_1( dividend->d, dividend->nlimbs, divisor );
mpi-genesis 320 }
mpi-genesis 321