Debian Codegraph @

mpi/mpi-div.c

raw
1 /* mpi-div.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt 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 Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
19 * SPDX-License-Identifier: LGPL-2.1-or-later
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 */
27
28 #include <config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "mpi-internal.h"
32 #include "longlong.h"
33 #include "g10lib.h"
34
35
36 void
37 _gcry_mpi_fdiv_r( gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor )
38 {
39 int divisor_sign = divisor->sign;
40 gcry_mpi_t 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 temp_divisor = mpi_copy( divisor );
47 divisor = temp_divisor;
48 }
49
50 _gcry_mpi_tdiv_r( rem, dividend, divisor );
51
52 if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs )
53 mpi_add (rem, rem, divisor);
54
55 if( temp_divisor )
56 mpi_free(temp_divisor);
57 }
58
59
60
61 /****************
62 * Division rounding the quotient towards -infinity.
63 * The remainder gets the same sign as the denominator.
64 * rem is optional
65 */
66
67 unsigned long
68 _gcry_mpi_fdiv_r_ui( gcry_mpi_t rem, gcry_mpi_t dividend,
69 unsigned long divisor )
70 {
71 mpi_limb_t rlimb;
72
73 rlimb = _gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor );
74 if( rlimb && dividend->sign )
75 rlimb = divisor - rlimb;
76
77 if( rem ) {
78 rem->d[0] = rlimb;
79 rem->nlimbs = rlimb? 1:0;
80 }
81 return rlimb;
82 }
83
84
85 void
86 _gcry_mpi_fdiv_q( gcry_mpi_t quot, gcry_mpi_t dividend, gcry_mpi_t divisor )
87 {
88 gcry_mpi_t tmp = mpi_alloc( mpi_get_nlimbs(quot) );
89 _gcry_mpi_fdiv_qr( quot, tmp, dividend, divisor);
90 mpi_free(tmp);
91 }
92
93 void
94 _gcry_mpi_fdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor )
95 {
96 int divisor_sign = divisor->sign;
97 gcry_mpi_t temp_divisor = NULL;
98
99 if( quot == divisor || rem == divisor ) {
100 temp_divisor = mpi_copy( divisor );
101 divisor = temp_divisor;
102 }
103
104 _gcry_mpi_tdiv_qr( quot, rem, dividend, divisor );
105
106 if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) {
107 mpi_sub_ui( quot, quot, 1 );
108 mpi_add( rem, rem, divisor);
109 }
110
111 if( temp_divisor )
112 mpi_free(temp_divisor);
113 }
114
115
116 /* If den == quot, den needs temporary storage.
117 * If den == rem, den needs temporary storage.
118 * If num == quot, num needs temporary storage.
119 * If den has temporary storage, it can be normalized while being copied,
120 * i.e no extra storage should be allocated.
121 */
122
123 void
124 _gcry_mpi_tdiv_r( gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den)
125 {
126 _gcry_mpi_tdiv_qr(NULL, rem, num, den );
127 }
128
129 void
130 _gcry_mpi_tdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den)
131 {
132 mpi_ptr_t np, dp;
133 mpi_ptr_t qp, rp;
134 mpi_size_t nsize = num->nlimbs;
135 mpi_size_t dsize = den->nlimbs;
136 mpi_size_t qsize, rsize;
137 mpi_size_t sign_remainder = num->sign;
138 mpi_size_t sign_quotient = num->sign ^ den->sign;
139 unsigned normalization_steps;
140 mpi_limb_t q_limb;
141 mpi_ptr_t marker[5];
142 unsigned int marker_nlimbs[5];
143 int markidx=0;
144
145 /* Ensure space is enough for quotient and remainder.
146 * We need space for an extra limb in the remainder, because it's
147 * up-shifted (normalized) below. */
148 rsize = nsize + 1;
149 mpi_resize( rem, rsize);
150
151 qsize = rsize - dsize; /* qsize cannot be bigger than this. */
152 if( qsize <= 0 ) {
153 if( num != rem ) {
154 rem->nlimbs = num->nlimbs;
155 rem->sign = num->sign;
156 MPN_COPY(rem->d, num->d, nsize);
157 }
158 if( quot ) {
159 /* This needs to follow the assignment to rem, in case the
160 * numerator and quotient are the same. */
161 quot->nlimbs = 0;
162 quot->sign = 0;
163 }
164 return;
165 }
166
167 if( quot )
168 mpi_resize( quot, qsize);
169
170 if (!dsize)
171 _gcry_divide_by_zero();
172
173 /* Read pointers here, when reallocation is finished. */
174 np = num->d;
175 dp = den->d;
176 rp = rem->d;
177
178 /* Optimize division by a single-limb divisor. */
179 if( dsize == 1 ) {
180 mpi_limb_t rlimb;
181 if( quot ) {
182 qp = quot->d;
183 rlimb = _gcry_mpih_divmod_1( qp, np, nsize, dp[0] );
184 qsize -= qp[qsize - 1] == 0;
185 quot->nlimbs = qsize;
186 quot->sign = sign_quotient;
187 }
188 else
189 rlimb = _gcry_mpih_mod_1( np, nsize, dp[0] );
190 rp[0] = rlimb;
191 rsize = rlimb != 0?1:0;
192 rem->nlimbs = rsize;
193 rem->sign = sign_remainder;
194 return;
195 }
196
197
198 if( quot ) {
199 qp = quot->d;
200 /* Make sure QP and NP point to different objects. Otherwise the
201 * numerator would be gradually overwritten by the quotient limbs. */
202 if(qp == np) { /* Copy NP object to temporary space. */
203 marker_nlimbs[markidx] = nsize;
204 np = marker[markidx++] = mpi_alloc_limb_space(nsize,
205 mpi_is_secure(quot));
206 MPN_COPY(np, qp, nsize);
207 }
208 }
209 else /* Put quotient at top of remainder. */
210 qp = rp + dsize;
211
212 count_leading_zeros( normalization_steps, dp[dsize - 1] );
213
214 /* Normalize the denominator, i.e. make its most significant bit set by
215 * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
216 * numerator the same number of steps (to keep the quotient the same!).
217 */
218 if( normalization_steps ) {
219 mpi_ptr_t tp;
220 mpi_limb_t nlimb;
221
222 /* Shift up the denominator setting the most significant bit of
223 * the most significant word. Use temporary storage not to clobber
224 * the original contents of the denominator. */
225 marker_nlimbs[markidx] = dsize;
226 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den));
227 _gcry_mpih_lshift( tp, dp, dsize, normalization_steps );
228 dp = tp;
229
230 /* Shift up the numerator, possibly introducing a new most
231 * significant word. Move the shifted numerator in the remainder
232 * meanwhile. */
233 nlimb = _gcry_mpih_lshift(rp, np, nsize, normalization_steps);
234 if( nlimb ) {
235 rp[nsize] = nlimb;
236 rsize = nsize + 1;
237 }
238 else
239 rsize = nsize;
240 }
241 else {
242 /* The denominator is already normalized, as required. Copy it to
243 * temporary space if it overlaps with the quotient or remainder. */
244 if( dp == rp || (quot && (dp == qp))) {
245 mpi_ptr_t tp;
246
247 marker_nlimbs[markidx] = dsize;
248 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,
249 mpi_is_secure(den));
250 MPN_COPY( tp, dp, dsize );
251 dp = tp;
252 }
253
254 /* Move the numerator to the remainder. */
255 if( rp != np )
256 MPN_COPY(rp, np, nsize);
257
258 rsize = nsize;
259 }
260
261 q_limb = _gcry_mpih_divrem( qp, 0, rp, rsize, dp, dsize );
262
263 if( quot ) {
264 qsize = rsize - dsize;
265 if(q_limb) {
266 qp[qsize] = q_limb;
267 qsize += 1;
268 }
269
270 quot->nlimbs = qsize;
271 quot->sign = sign_quotient;
272 }
273
274 rsize = dsize;
275 MPN_NORMALIZE (rp, rsize);
276
277 if( normalization_steps && rsize ) {
278 _gcry_mpih_rshift(rp, rp, rsize, normalization_steps);
279 rsize -= rp[rsize - 1] == 0?1:0;
280 }
281
282 rem->nlimbs = rsize;
283 rem->sign = sign_remainder;
284 while( markidx )
285 {
286 markidx--;
287 _gcry_mpi_free_limb_space (marker[markidx], marker_nlimbs[markidx]);
288 }
289 }
290
291 void
292 _gcry_mpi_tdiv_q_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned int count )
293 {
294 mpi_size_t usize, wsize;
295 mpi_size_t limb_cnt;
296
297 usize = u->nlimbs;
298 limb_cnt = count / BITS_PER_MPI_LIMB;
299 wsize = usize - limb_cnt;
300 if( limb_cnt >= usize )
301 w->nlimbs = 0;
302 else {
303 mpi_ptr_t wp;
304 mpi_ptr_t up;
305
306 RESIZE_IF_NEEDED( w, wsize );
307 wp = w->d;
308 up = u->d;
309
310 count %= BITS_PER_MPI_LIMB;
311 if( count ) {
312 _gcry_mpih_rshift( wp, up + limb_cnt, wsize, count );
313 wsize -= !wp[wsize - 1];
314 }
315 else {
316 MPN_COPY_INCR( wp, up + limb_cnt, wsize);
317 }
318
319 w->nlimbs = wsize;
320 }
321 }
322
323 /****************
324 * Check whether dividend is divisible by divisor
325 * (note: divisor must fit into a limb)
326 */
327 int
328 _gcry_mpi_divisible_ui(gcry_mpi_t dividend, unsigned long divisor )
329 {
330 return !_gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor );
331 }
332
333
334 void
335 _gcry_mpi_div (gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend,
336 gcry_mpi_t divisor, int round)
337 {
338 if (!round)
339 {
340 if (!rem)
341 {
342 gcry_mpi_t tmp = mpi_alloc (mpi_get_nlimbs(quot));
343 _gcry_mpi_tdiv_qr (quot, tmp, dividend, divisor);
344 mpi_free (tmp);
345 }
346 else
347 _gcry_mpi_tdiv_qr (quot, rem, dividend, divisor);
348 }
349 else if (round < 0)
350 {
351 if (!rem)
352 _gcry_mpi_fdiv_q (quot, dividend, divisor);
353 else if (!quot)
354 _gcry_mpi_fdiv_r (rem, dividend, divisor);
355 else
356 _gcry_mpi_fdiv_qr (quot, rem, dividend, divisor);
357 }
358 else
359 log_bug ("mpi rounding to ceiling not yet implemented\n");
360 }