mpmul.[ch]: Move internal `HWM' and `LWM' constants to implementation.
[u/mdw/catacomb] / mp-gcd.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
d3409d5e 4 *
5 * Extended GCD calculation
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
d3409d5e 11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
45c0fd36 18 *
d3409d5e 19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
45c0fd36 23 *
d3409d5e 24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
d3409d5e 30/*----- Header files ------------------------------------------------------*/
31
32#include "mp.h"
33
34/*----- Main code ---------------------------------------------------------*/
35
36/* --- @mp_gcd@ --- *
37 *
38 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
39 * @mp *a, *b@ = sources (must be nonzero)
40 *
41 * Returns: ---
42 *
43 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
44 * @ax + by = gcd(a, b)@. This is useful for computing modular
ef5f4810 45 * inverses.
d3409d5e 46 */
47
48void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
49{
b2253c70 50 mp *x = MP_ONE, *X = MP_ZERO;
51 mp *y = MP_ZERO, *Y = MP_ONE;
d3409d5e 52 mp *u, *v;
3d6115d1 53 mp *q = MP_NEW, *t, *spare = MP_NEW;
ef5f4810 54 unsigned f = 0;
55
ceb3f0c0 56#define f_swap 1u
57#define f_aneg 2u
58#define f_bneg 4u
59#define f_ext 8u
ef5f4810 60
61 /* --- Sort out some initial flags --- */
d3409d5e 62
ef5f4810 63 if (xx || yy)
64 f |= f_ext;
d3409d5e 65
a69a3efd 66 if (MP_NEGP(a))
ef5f4810 67 f |= f_aneg;
a69a3efd 68 if (MP_NEGP(b))
ef5f4810 69 f |= f_bneg;
70
71 /* --- Ensure that @a@ is larger than @b@ --- *
72 *
73 * Use absolute values here!
74 */
75
76 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
3d6115d1 77 t = a; a = b; b = t;
ef5f4810 78 f |= f_swap;
79 }
80
81 /* --- Check for zeroness --- */
82
3d6115d1 83 if (MP_ZEROP(b)) {
ef5f4810 84
85 /* --- Store %$|a|$% as the GCD --- */
86
87 if (gcd) {
88 if (*gcd) MP_DROP(*gcd);
89 a = MP_COPY(a);
a69a3efd 90 if (MP_NEGP(a)) {
ef5f4810 91 MP_SPLIT(a);
92 a->f &= ~MP_NEG;
93 f |= f_aneg;
94 }
95 *gcd = a;
96 }
97
98 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
99
100 if (f & f_ext) {
101 if (f & f_swap) {
3d6115d1 102 mp **tt = xx; xx = yy; yy = tt;
ef5f4810 103 }
104 if (xx) {
105 if (*xx) MP_DROP(*xx);
b2253c70 106 if (MP_EQ(a, MP_ZERO))
ef5f4810 107 *xx = MP_ZERO;
108 else if (f & f_aneg)
109 *xx = MP_MONE;
110 else
111 *xx = MP_ONE;
112 }
113 if (yy) {
114 if (*yy) MP_DROP(*yy);
115 *yy = MP_ZERO;
116 }
117 }
118 return;
d3409d5e 119 }
120
cf76bcbb 121 /* --- Force the signs on the arguments and take copies --- */
d3409d5e 122
123 a = MP_COPY(a);
124 b = MP_COPY(b);
125
ef5f4810 126 MP_SPLIT(a); a->f &= ~MP_NEG;
127 MP_SPLIT(b); b->f &= ~MP_NEG;
128
d3409d5e 129 u = MP_COPY(a);
130 v = MP_COPY(b);
131
cf76bcbb
MW
132 /* --- Main extended Euclidean algorithm --- */
133
a69a3efd 134 while (!MP_ZEROP(v)) {
b2253c70 135 mp_div(&q, &u, u, v);
136 if (f & f_ext) {
3d6115d1 137 t = mp_mul(spare, X, q);
b2253c70 138 t = mp_sub(t, x, t);
3d6115d1
MW
139 spare = x; x = X; X = t;
140 t = mp_mul(spare, Y, q);
b2253c70 141 t = mp_sub(t, y, t);
3d6115d1 142 spare = y; y = Y; Y = t;
d3409d5e 143 }
b2253c70 144 t = u; u = v; v = t;
d3409d5e 145 }
146
3d6115d1 147 MP_DROP(q); if (spare) MP_DROP(spare);
ef5f4810 148 if (!gcd)
b2253c70 149 MP_DROP(u);
ef5f4810 150 else {
151 if (*gcd) MP_DROP(*gcd);
b2253c70 152 u->f &= ~MP_NEG;
153 *gcd = u;
ef5f4810 154 }
d3409d5e 155
156 /* --- Perform a little normalization --- *
157 *
158 * Ensure that the coefficient returned is positive, if there is only one.
ef5f4810 159 * If there are two, favour @y@. Of course, if the original arguments were
160 * negative then I'll need to twiddle their signs as well.
d3409d5e 161 */
162
ef5f4810 163 if (f & f_ext) {
164
165 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
166
167 if (f & f_swap) {
3d6115d1 168 t = x; x = y; y = t;
da83a561 169 t = a; a = b; b = t;
d3409d5e 170 }
ef5f4810 171
172 /* --- Sort out the signs --- *
173 *
174 * Note that %$ax + by = a(x - b) + b(y + a)$%.
c9110b35 175 *
176 * This is currently bodgy. It needs sorting out at some time.
ef5f4810 177 */
178
d3409d5e 179 if (yy) {
a69a3efd 180 if (MP_NEGP(y)) {
c9110b35 181 do {
182 y = mp_add(y, y, a);
183 x = mp_sub(x, x, b);
a69a3efd 184 } while (MP_NEGP(y));
c9110b35 185 } else {
186 while (MP_CMP(y, >=, a)) {
187 y = mp_sub(y, y, a);
188 x = mp_add(x, x, b);
189 }
d3409d5e 190 }
c9110b35 191 } else {
a69a3efd 192 if (MP_NEGP(x)) {
c9110b35 193 do
194 x = mp_add(x, x, b);
a69a3efd 195 while (MP_NEGP(x));
c9110b35 196 } else {
197 while (MP_CMP(x, >=, b))
198 x = mp_sub(x, x, b);
199 }
200 }
d3409d5e 201
ef5f4810 202 /* --- Twiddle the signs --- */
203
204 if (f & f_aneg)
205 x->f ^= MP_NEG;
206 if (f & f_bneg)
207 y->f ^= MP_NEG;
208
209 /* --- Store the results --- */
210
211 if (!xx)
212 MP_DROP(x);
213 else {
214 if (*xx) MP_DROP(*xx);
215 *xx = x;
216 }
217
218 if (!yy)
219 MP_DROP(y);
220 else {
221 if (*yy) MP_DROP(*yy);
222 *yy = y;
223 }
d3409d5e 224 }
225
b2253c70 226 MP_DROP(v);
d3409d5e 227 MP_DROP(X); MP_DROP(Y);
228 MP_DROP(a); MP_DROP(b);
229}
230
b817bfc6 231/* -- @mp_modinv@ --- *
232 *
233 * Arguments: @mp *d@ = destination
234 * @mp *x@ = argument
235 * @mp *p@ = modulus
236 *
237 * Returns: The inverse %$x^{-1} \bmod p$%.
238 *
239 * Use: Computes a modular inverse. An assertion fails if %$p$%
240 * has no inverse.
241 */
242
243mp *mp_modinv(mp *d, mp *x, mp *p)
244{
245 mp *g = MP_NEW;
246 mp_gcd(&g, 0, &d, p, x);
247 assert(MP_EQ(g, MP_ONE));
248 mp_drop(g);
249 return (d);
250}
251
d3409d5e 252/*----- Test rig ----------------------------------------------------------*/
253
254#ifdef TEST_RIG
255
7c404803
MW
256static int modinv(dstr *v)
257{
258 int ok = 1;
259 mp *x = *(mp **)v[0].buf;
260 mp *m = *(mp **)v[1].buf;
261 mp *r = *(mp **)v[2].buf;
262
263 mp *y = mp_modinv(MP_NEW, x, m);
264 if (!MP_EQ(y, r)) {
265 fputs("\n*** mp_modinv failed", stderr);
45c0fd36
MW
266 fputs("\nx = ", stderr); mp_writefile(x, stderr, 10);
267 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
7c404803
MW
268 fputs("\nexpect = ", stderr); mp_writefile(r, stderr, 10);
269 fputs("\nresult = ", stderr); mp_writefile(y, stderr, 10);
270 ok = 0;
271 }
272 MP_DROP(x); MP_DROP(m); MP_DROP(r); MP_DROP(y);
273 assert(mparena_count(MPARENA_GLOBAL) == 0);
45c0fd36 274 return (ok);
7c404803
MW
275}
276
d3409d5e 277static int gcd(dstr *v)
278{
279 int ok = 1;
280 mp *a = *(mp **)v[0].buf;
281 mp *b = *(mp **)v[1].buf;
282 mp *g = *(mp **)v[2].buf;
283 mp *x = *(mp **)v[3].buf;
284 mp *y = *(mp **)v[4].buf;
285
ef5f4810 286 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
d3409d5e 287 mp_gcd(&gg, &xx, &yy, a, b);
b2253c70 288 if (!MP_EQ(x, xx)) {
d3409d5e 289 fputs("\n*** mp_gcd(x) failed", stderr);
45c0fd36
MW
290 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
291 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 292 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
293 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
294 fputc('\n', stderr);
295 ok = 0;
296 }
b2253c70 297 if (!MP_EQ(y, yy)) {
d3409d5e 298 fputs("\n*** mp_gcd(y) failed", stderr);
45c0fd36
MW
299 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
300 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 301 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
302 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
303 fputc('\n', stderr);
304 ok = 0;
305 }
306
307 if (!ok) {
308 mp *ax = mp_mul(MP_NEW, a, xx);
309 mp *by = mp_mul(MP_NEW, b, yy);
310 ax = mp_add(ax, ax, by);
b2253c70 311 if (MP_EQ(ax, gg))
d3409d5e 312 fputs("\n*** (Alternative result found.)\n", stderr);
313 MP_DROP(ax);
314 MP_DROP(by);
315 }
316
b2253c70 317 if (!MP_EQ(g, gg)) {
d3409d5e 318 fputs("\n*** mp_gcd(gcd) failed", stderr);
45c0fd36
MW
319 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
320 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 321 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
322 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
323 fputc('\n', stderr);
324 ok = 0;
325 }
326 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
327 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
ef5f4810 328 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 329 return (ok);
330}
331
332static test_chunk tests[] = {
333 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
7c404803 334 { "modinv", modinv, { &type_mp, &type_mp, &type_mp, 0 } },
d3409d5e 335 { 0, 0, { 0 } }
336};
337
338int main(int argc, char *argv[])
339{
340 sub_init();
341 test_run(argc, argv, tests, SRCDIR "/tests/mp");
342 return (0);
343}
344
345#endif
346
347/*----- That's all, folks -------------------------------------------------*/