hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/lib/mpi/mpi-internal.h
....@@ -1,22 +1,9 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
12 /* mpi-internal.h - Internal to the Multi Precision Integers
23 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
34 * Copyright (C) 1998, 2000 Free Software Foundation, Inc.
45 *
56 * 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
207 *
218 * Note: This code is heavily based on the GNU MP Library.
229 * Actually it's the same code with only minor changes in the
....@@ -65,6 +52,12 @@
6552 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
6653 typedef int mpi_size_t; /* (must be a signed type) */
6754
55
+#define RESIZE_IF_NEEDED(a, b) \
56
+ do { \
57
+ if ((a)->alloced < (b)) \
58
+ mpi_resize((a), (b)); \
59
+ } while (0)
60
+
6861 /* Copy N limbs from S to D. */
6962 #define MPN_COPY(d, s, n) \
7063 do { \
....@@ -72,6 +65,14 @@
7265 for (_i = 0; _i < (n); _i++) \
7366 (d)[_i] = (s)[_i]; \
7467 } 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
+
7576
7677 #define MPN_COPY_DECR(d, s, n) \
7778 do { \
....@@ -104,6 +105,38 @@
104105 else \
105106 mul_n(prodp, up, vp, size, tspace); \
106107 } 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
+
107140
108141 /*-- mpiutil.c --*/
109142 mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
....@@ -148,6 +181,8 @@
148181 void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
149182 void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
150183 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);
151186
152187 int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
153188 mpi_ptr_t up, mpi_size_t usize,
....@@ -159,9 +194,14 @@
159194 mpi_size_t s1_size, mpi_limb_t s2_limb);
160195
161196 /*-- 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);
162199 mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
163200 mpi_ptr_t np, mpi_size_t nsize,
164201 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);
165205
166206 /*-- generic_mpih-[lr]shift.c --*/
167207 mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,