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