More changes. Still embryonic.
[u/mdw/catacomb] / mp-gcd.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
ef5f4810 3 * $Id: mp-gcd.c,v 1.3 1999/12/10 23:18:39 mdw Exp $
d3409d5e 4 *
5 * Extended GCD calculation
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mp-gcd.c,v $
ef5f4810 33 * Revision 1.3 1999/12/10 23:18:39 mdw
34 * Change interface for suggested destinations.
35 *
da83a561 36 * Revision 1.2 1999/11/22 20:49:56 mdw
37 * Fix bug which failed to favour `x' when `y' wasn't wanted and the two
38 * arguments needed swapping.
39 *
d3409d5e 40 * Revision 1.1 1999/11/17 18:02:16 mdw
41 * New multiprecision integer arithmetic suite.
42 *
43 */
44
45/*----- Header files ------------------------------------------------------*/
46
47#include "mp.h"
48
49/*----- Main code ---------------------------------------------------------*/
50
51/* --- @mp_gcd@ --- *
52 *
53 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
54 * @mp *a, *b@ = sources (must be nonzero)
55 *
56 * Returns: ---
57 *
58 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
59 * @ax + by = gcd(a, b)@. This is useful for computing modular
ef5f4810 60 * inverses.
d3409d5e 61 */
62
63void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
64{
65 mp *X = MP_ONE, *Y = MP_ZERO;
66 mp *x = MP_ZERO, *y = MP_ONE;
67 mp *u, *v;
68 size_t shift = 0;
ef5f4810 69 unsigned f = 0;
70
71 enum {
72 f_swap = 1u,
73 f_aneg = 2u,
74 f_bneg = 4u,
75 f_ext = 8u
76 };
77
78 /* --- Sort out some initial flags --- */
d3409d5e 79
ef5f4810 80 if (xx || yy)
81 f |= f_ext;
d3409d5e 82
ef5f4810 83 if (a->f & MP_NEG)
84 f |= f_aneg;
85 if (b->f & MP_NEG)
86 f |= f_bneg;
87
88 /* --- Ensure that @a@ is larger than @b@ --- *
89 *
90 * Use absolute values here!
91 */
92
93 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
d3409d5e 94 { mp *t = a; a = b; b = t; }
ef5f4810 95 f |= f_swap;
96 }
97
98 /* --- Check for zeroness --- */
99
100 if (MP_CMP(b, ==, MP_ZERO)) {
101
102 /* --- Store %$|a|$% as the GCD --- */
103
104 if (gcd) {
105 if (*gcd) MP_DROP(*gcd);
106 a = MP_COPY(a);
107 if (a->f & MP_NEG) {
108 MP_SPLIT(a);
109 a->f &= ~MP_NEG;
110 f |= f_aneg;
111 }
112 *gcd = a;
113 }
114
115 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
116
117 if (f & f_ext) {
118 if (f & f_swap) {
119 mp **t = xx; xx = yy; yy = t;
120 }
121 if (xx) {
122 if (*xx) MP_DROP(*xx);
123 if (MP_CMP(a, ==, MP_ZERO))
124 *xx = MP_ZERO;
125 else if (f & f_aneg)
126 *xx = MP_MONE;
127 else
128 *xx = MP_ONE;
129 }
130 if (yy) {
131 if (*yy) MP_DROP(*yy);
132 *yy = MP_ZERO;
133 }
134 }
135 return;
d3409d5e 136 }
137
138 /* --- Take a reference to the arguments --- */
139
140 a = MP_COPY(a);
141 b = MP_COPY(b);
142
143 /* --- Make sure @a@ and @b@ are not both even --- */
144
ef5f4810 145 MP_SPLIT(a); a->f &= ~MP_NEG;
146 MP_SPLIT(b); b->f &= ~MP_NEG;
147
d3409d5e 148 if (((a->v[0] | b->v[0]) & 1) == 0) {
149 mpscan asc, bsc;
150
151 /* --- Break off my copies --- */
152
d3409d5e 153 MP_SCAN(&asc, a);
154 MP_SCAN(&bsc, b);
155
156 /* --- Start scanning --- */
157
158 for (;;) {
159 if (!MP_STEP(&asc) || !MP_STEP(&bsc))
160 assert(((void)"zero argument passed to mp_gcd", 0));
161 if (MP_BIT(&asc) || MP_BIT(&bsc))
162 break;
163 shift++;
164 }
165
166 /* --- Shift @a@ and @b@ down --- */
167
168 a = mp_lsr(a, a, shift);
169 b = mp_lsr(b, b, shift);
170 }
171
172 /* --- Set up @u@ and @v@ --- */
173
174 u = MP_COPY(a);
175 v = MP_COPY(b);
176
177 /* --- Start the main loop --- */
178
179 for (;;) {
180
181 /* --- While @u@ is even --- */
182
183 {
184 mpscan sc, xsc, ysc;
185 size_t n = 0, nn = 0;
186
187 MP_SCAN(&sc, u);
188 MP_SCAN(&xsc, X); MP_SCAN(&ysc, Y);
189 for (;;) {
190 MP_STEP(&sc);
191 MP_STEP(&xsc); MP_STEP(&ysc);
192 if (MP_BIT(&sc))
193 break;
ef5f4810 194 if ((f & f_ext) && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
d3409d5e 195 if (n) {
196 X = mp_lsr(X, X, n);
197 Y = mp_lsr(Y, Y, n);
198 n = 0;
199 }
200 X = mp_add(X, X, b);
201 Y = mp_sub(Y, Y, a);
202 MP_SCAN(&xsc, X);
203 MP_SCAN(&ysc, Y);
204 MP_STEP(&xsc); MP_STEP(&ysc);
205 }
206 n++; nn++;
207 }
208
209 if (nn) {
210 u = mp_lsr(u, u, nn);
ef5f4810 211 if ((f & f_ext) && n) {
d3409d5e 212 X = mp_lsr(X, X, n);
213 Y = mp_lsr(Y, Y, n);
214 }
215 }
216 }
217
218 /* --- While @v@ is even --- */
219
220 {
221 mpscan sc, xsc, ysc;
222 size_t n = 0, nn = 0;
223
224 MP_SCAN(&sc, v);
225 MP_SCAN(&xsc, x); MP_SCAN(&ysc, y);
226 for (;;) {
227 MP_STEP(&sc);
228 MP_STEP(&xsc); MP_STEP(&ysc);
229 if (MP_BIT(&sc))
230 break;
ef5f4810 231 if ((f & f_ext) && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
d3409d5e 232 if (n) {
233 x = mp_lsr(x, x, n);
234 y = mp_lsr(y, y, n);
235 n = 0;
236 }
237 x = mp_add(x, x, b);
238 y = mp_sub(y, y, a);
239 MP_SCAN(&xsc, x);
240 MP_SCAN(&ysc, y);
241 MP_STEP(&xsc); MP_STEP(&ysc);
242 }
243 n++; nn++;
244 }
245
246 if (nn) {
247 v = mp_lsr(v, v, nn);
ef5f4810 248 if ((f & f_ext) && n) {
d3409d5e 249 x = mp_lsr(x, x, n);
250 y = mp_lsr(y, y, n);
251 }
252 }
253 }
254
255 /* --- End-of-loop fiddling --- */
256
257 if (MP_CMP(u, >=, v)) {
258 u = mp_sub(u, u, v);
ef5f4810 259 if (f & f_ext) {
d3409d5e 260 X = mp_sub(X, X, x);
261 Y = mp_sub(Y, Y, y);
262 }
263 } else {
264 v = mp_sub(v, v, u);
ef5f4810 265 if (f & f_ext) {
d3409d5e 266 x = mp_sub(x, x, X);
267 y = mp_sub(y, y, Y);
268 }
269 }
270
271 if (MP_CMP(u, ==, MP_ZERO))
272 break;
273 }
274
275 /* --- Write the results out --- */
276
ef5f4810 277 if (!gcd)
d3409d5e 278 MP_DROP(v);
ef5f4810 279 else {
280 if (*gcd) MP_DROP(*gcd);
281 *gcd = mp_lsl(v, v, shift);
282 }
d3409d5e 283
284 /* --- Perform a little normalization --- *
285 *
286 * Ensure that the coefficient returned is positive, if there is only one.
ef5f4810 287 * If there are two, favour @y@. Of course, if the original arguments were
288 * negative then I'll need to twiddle their signs as well.
d3409d5e 289 */
290
ef5f4810 291 if (f & f_ext) {
292
293 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
294
295 if (f & f_swap) {
d3409d5e 296 mp *t = x; x = y; y = t;
da83a561 297 t = a; a = b; b = t;
d3409d5e 298 }
ef5f4810 299
300 /* --- Sort out the signs --- *
301 *
302 * Note that %$ax + by = a(x - b) + b(y + a)$%.
303 */
304
d3409d5e 305 if (yy) {
306 if (y->f & MP_NEG) {
307 y = mp_add(y, y, a);
308 x = mp_sub(x, x, b);
309 }
310 } else if (x->f & MP_NEG)
311 x = mp_add(x, x, b);
312
ef5f4810 313 /* --- Twiddle the signs --- */
314
315 if (f & f_aneg)
316 x->f ^= MP_NEG;
317 if (f & f_bneg)
318 y->f ^= MP_NEG;
319
320 /* --- Store the results --- */
321
322 if (!xx)
323 MP_DROP(x);
324 else {
325 if (*xx) MP_DROP(*xx);
326 *xx = x;
327 }
328
329 if (!yy)
330 MP_DROP(y);
331 else {
332 if (*yy) MP_DROP(*yy);
333 *yy = y;
334 }
d3409d5e 335 }
336
337 MP_DROP(u);
338 MP_DROP(X); MP_DROP(Y);
339 MP_DROP(a); MP_DROP(b);
340}
341
342/*----- Test rig ----------------------------------------------------------*/
343
344#ifdef TEST_RIG
345
346static int gcd(dstr *v)
347{
348 int ok = 1;
349 mp *a = *(mp **)v[0].buf;
350 mp *b = *(mp **)v[1].buf;
351 mp *g = *(mp **)v[2].buf;
352 mp *x = *(mp **)v[3].buf;
353 mp *y = *(mp **)v[4].buf;
354
ef5f4810 355 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
d3409d5e 356 mp_gcd(&gg, &xx, &yy, a, b);
357 if (MP_CMP(x, !=, xx)) {
358 fputs("\n*** mp_gcd(x) failed", stderr);
359 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
360 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
361 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
362 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
363 fputc('\n', stderr);
364 ok = 0;
365 }
366 if (MP_CMP(y, !=, yy)) {
367 fputs("\n*** mp_gcd(y) failed", stderr);
368 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
369 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
370 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
371 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
372 fputc('\n', stderr);
373 ok = 0;
374 }
375
376 if (!ok) {
377 mp *ax = mp_mul(MP_NEW, a, xx);
378 mp *by = mp_mul(MP_NEW, b, yy);
379 ax = mp_add(ax, ax, by);
380 if (MP_CMP(ax, ==, gg))
381 fputs("\n*** (Alternative result found.)\n", stderr);
382 MP_DROP(ax);
383 MP_DROP(by);
384 }
385
386 if (MP_CMP(g, !=, gg)) {
387 fputs("\n*** mp_gcd(gcd) failed", stderr);
388 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
389 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
390 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
391 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
392 fputc('\n', stderr);
393 ok = 0;
394 }
395 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
396 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
ef5f4810 397 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 398 return (ok);
399}
400
401static test_chunk tests[] = {
402 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
403 { 0, 0, { 0 } }
404};
405
406int main(int argc, char *argv[])
407{
408 sub_init();
409 test_run(argc, argv, tests, SRCDIR "/tests/mp");
410 return (0);
411}
412
413#endif
414
415/*----- That's all, folks -------------------------------------------------*/