| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later */ |
|---|
| 1 | 2 | /* mpi-internal.h - Internal to the Multi Precision Integers |
|---|
| 2 | 3 | * Copyright (C) 1994, 1996 Free Software Foundation, Inc. |
|---|
| 3 | 4 | * Copyright (C) 1998, 2000 Free Software Foundation, Inc. |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * 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 | 7 | * |
|---|
| 21 | 8 | * Note: This code is heavily based on the GNU MP Library. |
|---|
| 22 | 9 | * Actually it's the same code with only minor changes in the |
|---|
| .. | .. |
|---|
| 65 | 52 | typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */ |
|---|
| 66 | 53 | typedef int mpi_size_t; /* (must be a signed type) */ |
|---|
| 67 | 54 | |
|---|
| 55 | +#define RESIZE_IF_NEEDED(a, b) \ |
|---|
| 56 | + do { \ |
|---|
| 57 | + if ((a)->alloced < (b)) \ |
|---|
| 58 | + mpi_resize((a), (b)); \ |
|---|
| 59 | + } while (0) |
|---|
| 60 | + |
|---|
| 68 | 61 | /* Copy N limbs from S to D. */ |
|---|
| 69 | 62 | #define MPN_COPY(d, s, n) \ |
|---|
| 70 | 63 | do { \ |
|---|
| .. | .. |
|---|
| 72 | 65 | for (_i = 0; _i < (n); _i++) \ |
|---|
| 73 | 66 | (d)[_i] = (s)[_i]; \ |
|---|
| 74 | 67 | } while (0) |
|---|
| 68 | + |
|---|
| 69 | +#define MPN_COPY_INCR(d, s, n) \ |
|---|
| 70 | + do { \ |
|---|
| 71 | + mpi_size_t _i; \ |
|---|
| 72 | + for (_i = 0; _i < (n); _i++) \ |
|---|
| 73 | + (d)[_i] = (s)[_i]; \ |
|---|
| 74 | + } while (0) |
|---|
| 75 | + |
|---|
| 75 | 76 | |
|---|
| 76 | 77 | #define MPN_COPY_DECR(d, s, n) \ |
|---|
| 77 | 78 | do { \ |
|---|
| .. | .. |
|---|
| 104 | 105 | else \ |
|---|
| 105 | 106 | mul_n(prodp, up, vp, size, tspace); \ |
|---|
| 106 | 107 | } while (0); |
|---|
| 108 | + |
|---|
| 109 | +/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest |
|---|
| 110 | + * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB). |
|---|
| 111 | + * If this would yield overflow, DI should be the largest possible number |
|---|
| 112 | + * (i.e., only ones). For correct operation, the most significant bit of D |
|---|
| 113 | + * has to be set. Put the quotient in Q and the remainder in R. |
|---|
| 114 | + */ |
|---|
| 115 | +#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \ |
|---|
| 116 | + do { \ |
|---|
| 117 | + mpi_limb_t _ql __maybe_unused; \ |
|---|
| 118 | + mpi_limb_t _q, _r; \ |
|---|
| 119 | + mpi_limb_t _xh, _xl; \ |
|---|
| 120 | + umul_ppmm(_q, _ql, (nh), (di)); \ |
|---|
| 121 | + _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \ |
|---|
| 122 | + umul_ppmm(_xh, _xl, _q, (d)); \ |
|---|
| 123 | + sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl); \ |
|---|
| 124 | + if (_xh) { \ |
|---|
| 125 | + sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \ |
|---|
| 126 | + _q++; \ |
|---|
| 127 | + if (_xh) { \ |
|---|
| 128 | + sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \ |
|---|
| 129 | + _q++; \ |
|---|
| 130 | + } \ |
|---|
| 131 | + } \ |
|---|
| 132 | + if (_r >= (d)) { \ |
|---|
| 133 | + _r -= (d); \ |
|---|
| 134 | + _q++; \ |
|---|
| 135 | + } \ |
|---|
| 136 | + (r) = _r; \ |
|---|
| 137 | + (q) = _q; \ |
|---|
| 138 | + } while (0) |
|---|
| 139 | + |
|---|
| 107 | 140 | |
|---|
| 108 | 141 | /*-- mpiutil.c --*/ |
|---|
| 109 | 142 | mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs); |
|---|
| .. | .. |
|---|
| 148 | 181 | void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size); |
|---|
| 149 | 182 | void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size, |
|---|
| 150 | 183 | mpi_ptr_t tspace); |
|---|
| 184 | +void mpihelp_mul_n(mpi_ptr_t prodp, |
|---|
| 185 | + mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size); |
|---|
| 151 | 186 | |
|---|
| 152 | 187 | int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp, |
|---|
| 153 | 188 | mpi_ptr_t up, mpi_size_t usize, |
|---|
| .. | .. |
|---|
| 159 | 194 | mpi_size_t s1_size, mpi_limb_t s2_limb); |
|---|
| 160 | 195 | |
|---|
| 161 | 196 | /*-- mpih-div.c --*/ |
|---|
| 197 | +mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size, |
|---|
| 198 | + mpi_limb_t divisor_limb); |
|---|
| 162 | 199 | mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs, |
|---|
| 163 | 200 | mpi_ptr_t np, mpi_size_t nsize, |
|---|
| 164 | 201 | mpi_ptr_t dp, mpi_size_t dsize); |
|---|
| 202 | +mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr, |
|---|
| 203 | + mpi_ptr_t dividend_ptr, mpi_size_t dividend_size, |
|---|
| 204 | + mpi_limb_t divisor_limb); |
|---|
| 165 | 205 | |
|---|
| 166 | 206 | /*-- generic_mpih-[lr]shift.c --*/ |
|---|
| 167 | 207 | mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, |
|---|