Debian Codegraph @

mpi/mpih-pow.c

raw
1 /* mpih-pow.c - MPI helper functions
2 * Copyright (C) 2025 g10 Code GmbH
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 *
20 */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "mpi-internal.h"
26 #include "longlong.h"
27
28 #if BITS_PER_MPI_LIMB <= 32
29 #define MAX_SCRATCH_SPACE 256*2
30 #define MAX_WINDOW 8
31 #else
32 #define MAX_SCRATCH_SPACE 256
33 #define MAX_WINDOW 4
34 #endif
35
36 /*
37 * Compute -N^(-1) mod 2^BITS_PER_MPI_LIMB
38 *
39 * (1) Compute N^(-1) mod 2^BITS_PER_MPI_LIMB
40 * (2) Then, negate the value of (1)
41 *
42 * For computing N^(-1) mod (power of two), see:
43 *
44 * Jeffrey Hurchalla
45 * An Improved Integer Modular Multiplicative Inverse (modulo 2^w)
46 * https://arxiv.org/abs/2204.04342
47 *
48 */
49 static mpi_limb_t
50 compute_minv (mpi_limb_t n)
51 {
52 mpi_limb_t x, y;
53
54 gcry_assert (n%2 == 1);
55
56 x = (3*n)^2;
57 y = 1 - n*x;
58 x = x*(1 + y);
59 y *= y;
60 x = x*(1 + y);
61 y *= y;
62 x = x*(1 + y);
63 #if BITS_PER_MPI_LIMB == 32
64 return -x;
65 #elif BITS_PER_MPI_LIMB == 64
66 y *= y;
67 x = x*(1 + y);
68 return -x;
69 #else
70 # error "Please implement multiplicative inverse mod power of 2"
71 #endif
72 }
73
74
75 /*
76 * Compute T * R^(-1) mod M (where R is represented by MINV)
77 *
78 * Reference:
79 * Handbook of Applied Cryptography
80 * Algorithm 14.82: Montgomery reduction
81 */
82 static void
83 mont_reduc (mpi_ptr_t rp, mpi_ptr_t tp,
84 mpi_ptr_t mp, mpi_size_t n, mpi_limb_t minv)
85 {
86 mpi_size_t i;
87 mpi_limb_t cy0;
88 mpi_limb_t cy1 = 0;
89
90 for (i = 0; i < n; i++)
91 {
92 mpi_limb_t ui = tp[i] * minv;
93
94 cy0 = _gcry_mpih_addmul_1 (tp + i, mp, n, ui);
95 cy1 += _gcry_mpih_add_1_lli (tp + n + i, n - i, cy0);
96 }
97
98 cy0 = _gcry_mpih_sub_n (rp, tp + n, mp, n);
99 _gcry_mpih_set_cond (rp, tp + n, n,
100 ct_ulong_gen_mask (mpih_limb_is_not_zero (cy0))
101 & mpih_limb_is_zero (cy1));
102 }
103
104 /*
105 * Compute X * Y * R^(-1) mod M (where R is represented by MINV)
106 *
107 * Reference:
108 * Handbook of Applied Cryptography
109 * Algorithm 14.86: Montgomery multiplication
110 *
111 * RP should have space of 2*N limbs.
112 *
113 */
114 static void
115 mont_mul (mpi_ptr_t rp, mpi_ptr_t xp, mpi_ptr_t yp, mpi_ptr_t mp,
116 mpi_size_t n, mpi_limb_t minv, mpi_ptr_t scratch_2n)
117 {
118 _gcry_mpih_mul_lli (scratch_2n, xp, n, yp, n);
119 mont_reduc (rp, scratch_2n, mp, n, minv);
120 }
121
122 /* Determine the window size for computing exponentiation. */
123 static int
124 window_size (mpi_size_t esize)
125 {
126 int W;
127
128 #if BITS_PER_MPI_LIMB <= 32
129 if (esize > 24)
130 W = 5;
131 else if (esize > 16)
132 W = 4;
133 else if (esize > 12)
134 W = 3;
135 else if (esize > 8)
136 W = 2;
137 else
138 W = 1;
139 return W;
140 #else
141 if (esize > 8)
142 W = 4;
143 else if (esize > 6)
144 W = 3;
145 else if (esize > 4)
146 W = 2;
147 else
148 W = 1;
149 return W;
150 #endif
151 return W;
152 }
153
154 /*
155 * Compute X ^ E mod M
156 *
157 * where X is in BP, M is in MP.
158 * The size of limbs for BP and MP are N.
159 * E is in EP, size of limbs EN.
160 *
161 * Result will be in RP with size N.
162 *
163 * Reference:
164 * Handbook of Applied Cryptography
165 * Algorithm 14.82: Left-to-right k-ary exponentiation
166 * Algorithm 14.94: Montgomery exponentiation
167 *
168 */
169 void
170 _gcry_mpih_powm_lli (mpi_ptr_t rp, mpi_ptr_t bp, mpi_ptr_t mp, mpi_size_t n,
171 mpi_ptr_t ep, mpi_size_t en)
172 {
173 mpi_size_t scratch_size;
174 #define temp0 (scratch)
175 #define temp2 (scratch+n*2)
176 #define a (scratch+n*4)
177 #define precomp (scratch+n*5)
178 mpi_ptr_t scratch;
179 mpi_limb_t minv;
180 mpi_size_t i;
181 int mod_shift_cnt;
182 int windowsize = window_size (en);
183 mpi_limb_t wmask = (((mpi_limb_t) 1 << windowsize) - 1);
184 #define temp1 (precomp+n)
185 #define x_tilde (precomp+n)
186
187 scratch_size = (5 + (1 << windowsize))*n;
188 scratch = _gcry_mpi_alloc_limb_space (scratch_size, 1);
189
190 minv = compute_minv (mp[0]);
191
192 gcry_assert (mp[0]*(-minv) == 1);
193
194 MPN_ZERO (temp0, n);
195
196 /* TEMP0 := R mod m */
197 count_leading_zeros (mod_shift_cnt, mp[n-1]);
198 if (mod_shift_cnt)
199 {
200 _gcry_mpih_lshift (temp2, mp, n, mod_shift_cnt);
201 temp0[n] = (mpi_limb_t)1 << mod_shift_cnt;
202 }
203 else
204 {
205 MPN_COPY (temp2, mp, n);
206 temp0[n] = 1;
207 }
208 _gcry_mpih_divrem (temp1, 0, temp0, n+1, temp2, n);
209 if (mod_shift_cnt)
210 _gcry_mpih_rshift (temp0, temp0, n, mod_shift_cnt);
211 /* PRECOMP[0] := R mod m */
212 MPN_COPY (precomp, temp0, n);
213
214 /* TEMP0 := (R mod m)^2 */
215 _gcry_mpih_sqr_n_basecase (temp0, precomp, n);
216
217 /* TEMP0 := R^2 mod m */
218 if (mod_shift_cnt)
219 _gcry_mpih_lshift (temp0, temp0, n*2, mod_shift_cnt);
220 _gcry_mpih_divrem (temp1, 0, temp0, n*2, temp2, n);
221 if (mod_shift_cnt)
222 _gcry_mpih_rshift (temp0, temp0, n, mod_shift_cnt);
223 /* x~ := Mont(x, R^2 mod m) */
224 mont_mul (x_tilde, bp, temp0, mp, n, minv, temp2);
225
226 /* PRECOMP[i] := x~ ^ i */
227 for (i = 0; i < (1 << windowsize) - 2; i += 2)
228 {
229 _gcry_mpih_sqr_n_basecase (temp0, precomp+n*(i/2+1), n);
230 mont_reduc (precomp+n*(i+2), temp0, mp, n, minv);
231 mont_mul (precomp+n*(i+3), x_tilde, precomp+n*(i+2), mp, n, minv, temp2);
232 }
233
234 MPN_COPY (a, precomp, n);
235 i = en * BITS_PER_MPI_LIMB;
236 do
237 {
238 mpi_limb_t e;
239 int w;
240
241 if (i < windowsize)
242 {
243 e = ep[0] & (((mpi_limb_t) 1 << i) - 1);
244 w = i;
245 i = 0;
246 }
247 else
248 {
249 mpi_limb_t v;
250 mpi_size_t shift;
251 mpi_size_t j;
252 int nbits_in_v;
253
254 i -= windowsize;
255 j = i / BITS_PER_MPI_LIMB;
256 shift = i % BITS_PER_MPI_LIMB;
257 v = ep[j] >> shift;
258 nbits_in_v = BITS_PER_MPI_LIMB - shift;
259 if (nbits_in_v < windowsize)
260 v += ep[j + 1] << nbits_in_v;
261 e = v & wmask;
262 w = windowsize;
263 }
264
265 do
266 {
267 _gcry_mpih_sqr_n_basecase (temp0, a, n);
268 mont_reduc (a, temp0, mp, n, minv);
269 }
270 while (--w);
271
272 _gcry_mpih_lookup_lli (temp0, precomp, n, (1 << windowsize), e);
273 mont_mul (a, a, temp0, mp, n, minv, temp2);
274 }
275 while (i);
276
277 MPN_ZERO (temp0, n);
278 temp0[0] = 1;
279 mont_mul (a, a, temp0, mp, n, minv, temp2);
280
281 MPN_COPY (rp, a, n);
282 _gcry_mpi_free_limb_space (scratch, scratch_size);
283 }