1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* mpihelp-div.c - MPI helper functions
3 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
4 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
6 * This file is part of GnuPG.
8 * Note: This code is heavily based on the GNU MP Library.
9 * Actually it's the same code with only minor changes in the
10 * way the data is stored; this is to support the abstraction
11 * of an optional secure memory allocation which may be used
12 * to avoid revealing of sensitive data due to paging etc.
13 * The GNU MP Library itself is published under the LGPL;
14 * however I decided to publish this code under the plain GPL.
17 #include "mpi-internal.h"
24 #define UDIV_TIME UMUL_TIME
27 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
28 * the NSIZE-DSIZE least significant quotient limbs at QP
29 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
30 * non-zero, generate that many fraction bits and append them after the
31 * other quotient limbs.
32 * Return the most significant limb of the quotient, this is always 0 or 1.
36 * 1. The most significant bit of the divisor must be set.
37 * 2. QP must either not overlap with the input operands at all, or
38 * QP + DSIZE >= NP must hold true. (This means that it's
39 * possible to put the quotient in the high part of NUM, right after the
41 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
45 mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
46 mpi_ptr_t np, mpi_size_t nsize, mpi_ptr_t dp, mpi_size_t dsize)
48 mpi_limb_t most_significant_q_limb = 0;
52 /* We are asked to divide by zero, so go ahead and do it! (To make
53 the compiler not remove this statement, return the value.) */
55 * existing clients of this function have been modified
56 * not to call it with dsize == 0, so this should not happen
71 most_significant_q_limb = 1;
75 for (i = nsize - 2; i >= 0; i--)
76 udiv_qrnnd(qp[i], n1, n1, np[i], d);
79 for (i = qextra_limbs - 1; i >= 0; i--)
80 udiv_qrnnd(qp[i], n1, n1, 0, d);
89 mpi_limb_t n1, n0, n2;
98 if (n1 >= d1 && (n1 > d1 || n0 >= d0)) {
99 sub_ddmmss(n1, n0, n1, n0, d1, d0);
100 most_significant_q_limb = 1;
103 for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--) {
107 if (i >= qextra_limbs)
113 /* Q should be either 111..111 or 111..110. Need special
114 * treatment of this rare case as normal division would
119 if (r < d1) { /* Carry in the addition? */
120 add_ssaaaa(n1, n0, r - d0,
125 n1 = d0 - (d0 != 0 ? 1 : 0);
128 udiv_qrnnd(q, r, n1, n0, d1);
129 umul_ppmm(n1, n0, d0, q);
134 if (n1 > r || (n1 == r && n0 > n2)) {
135 /* The estimated Q was too large. */
137 sub_ddmmss(n1, n0, n1, n0, 0, d0);
139 if (r >= d1) /* If not carry, test Q again. */
144 sub_ddmmss(n1, n0, r, n2, n1, n0);
154 mpi_limb_t dX, d1, n0;
163 || mpihelp_cmp(np, dp, dsize - 1) >= 0) {
164 mpihelp_sub_n(np, np, dp, dsize);
166 most_significant_q_limb = 1;
170 for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
175 if (i >= qextra_limbs) {
180 MPN_COPY_DECR(np + 1, np, dsize - 1);
185 /* This might over-estimate q, but it's probably not worth
186 * the extra code here to find out. */
191 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
192 umul_ppmm(n1, n0, d1, q);
196 && n0 > np[dsize - 2])) {
199 if (r < dX) /* I.e. "carry in previous addition?" */
206 /* Possible optimization: We already have (q * n0) and (1 * n1)
207 * after the calculation of q. Taking advantage of that, we
208 * could make this loop make two iterations less. */
209 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
212 mpihelp_add_n(np, np, dp, dsize);
222 return most_significant_q_limb;