Split mode macros into interface and implementation.
[u/mdw/catacomb] / mp-gcd.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
da83a561 3 * $Id: mp-gcd.c,v 1.2 1999/11/22 20:49:56 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 $
da83a561 33 * Revision 1.2 1999/11/22 20:49:56 mdw
34 * Fix bug which failed to favour `x' when `y' wasn't wanted and the two
35 * arguments needed swapping.
36 *
d3409d5e 37 * Revision 1.1 1999/11/17 18:02:16 mdw
38 * New multiprecision integer arithmetic suite.
39 *
40 */
41
42/*----- Header files ------------------------------------------------------*/
43
44#include "mp.h"
45
46/*----- Main code ---------------------------------------------------------*/
47
48/* --- @mp_gcd@ --- *
49 *
50 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
51 * @mp *a, *b@ = sources (must be nonzero)
52 *
53 * Returns: ---
54 *
55 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
56 * @ax + by = gcd(a, b)@. This is useful for computing modular
57 * inverses. Neither @a@ nor @b@ may be zero. Note that,
58 * unlike @mp_div@ for example, it is not possible to specify
59 * explicit destinations -- new MPs are always allocated.
60 */
61
62void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
63{
64 mp *X = MP_ONE, *Y = MP_ZERO;
65 mp *x = MP_ZERO, *y = MP_ONE;
66 mp *u, *v;
67 size_t shift = 0;
68 int ext = xx || yy;
69 int swap = 0;
70
71 /* --- Ensure that @a@ is larger than @b@ --- */
72
73 if (MP_CMP(a, <, b)) {
74 { mp *t = a; a = b; b = t; }
75 swap = 1;
76 }
77
78 /* --- Take a reference to the arguments --- */
79
80 a = MP_COPY(a);
81 b = MP_COPY(b);
82
83 /* --- Make sure @a@ and @b@ are not both even --- */
84
85 if (((a->v[0] | b->v[0]) & 1) == 0) {
86 mpscan asc, bsc;
87
88 /* --- Break off my copies --- */
89
90 MP_SPLIT(a);
91 MP_SPLIT(b);
92 MP_SCAN(&asc, a);
93 MP_SCAN(&bsc, b);
94
95 /* --- Start scanning --- */
96
97 for (;;) {
98 if (!MP_STEP(&asc) || !MP_STEP(&bsc))
99 assert(((void)"zero argument passed to mp_gcd", 0));
100 if (MP_BIT(&asc) || MP_BIT(&bsc))
101 break;
102 shift++;
103 }
104
105 /* --- Shift @a@ and @b@ down --- */
106
107 a = mp_lsr(a, a, shift);
108 b = mp_lsr(b, b, shift);
109 }
110
111 /* --- Set up @u@ and @v@ --- */
112
113 u = MP_COPY(a);
114 v = MP_COPY(b);
115
116 /* --- Start the main loop --- */
117
118 for (;;) {
119
120 /* --- While @u@ is even --- */
121
122 {
123 mpscan sc, xsc, ysc;
124 size_t n = 0, nn = 0;
125
126 MP_SCAN(&sc, u);
127 MP_SCAN(&xsc, X); MP_SCAN(&ysc, Y);
128 for (;;) {
129 MP_STEP(&sc);
130 MP_STEP(&xsc); MP_STEP(&ysc);
131 if (MP_BIT(&sc))
132 break;
133 if (ext && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
134 if (n) {
135 X = mp_lsr(X, X, n);
136 Y = mp_lsr(Y, Y, n);
137 n = 0;
138 }
139 X = mp_add(X, X, b);
140 Y = mp_sub(Y, Y, a);
141 MP_SCAN(&xsc, X);
142 MP_SCAN(&ysc, Y);
143 MP_STEP(&xsc); MP_STEP(&ysc);
144 }
145 n++; nn++;
146 }
147
148 if (nn) {
149 u = mp_lsr(u, u, nn);
150 if (ext && n) {
151 X = mp_lsr(X, X, n);
152 Y = mp_lsr(Y, Y, n);
153 }
154 }
155 }
156
157 /* --- While @v@ is even --- */
158
159 {
160 mpscan sc, xsc, ysc;
161 size_t n = 0, nn = 0;
162
163 MP_SCAN(&sc, v);
164 MP_SCAN(&xsc, x); MP_SCAN(&ysc, y);
165 for (;;) {
166 MP_STEP(&sc);
167 MP_STEP(&xsc); MP_STEP(&ysc);
168 if (MP_BIT(&sc))
169 break;
170 if (ext && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
171 if (n) {
172 x = mp_lsr(x, x, n);
173 y = mp_lsr(y, y, n);
174 n = 0;
175 }
176 x = mp_add(x, x, b);
177 y = mp_sub(y, y, a);
178 MP_SCAN(&xsc, x);
179 MP_SCAN(&ysc, y);
180 MP_STEP(&xsc); MP_STEP(&ysc);
181 }
182 n++; nn++;
183 }
184
185 if (nn) {
186 v = mp_lsr(v, v, nn);
187 if (ext && n) {
188 x = mp_lsr(x, x, n);
189 y = mp_lsr(y, y, n);
190 }
191 }
192 }
193
194 /* --- End-of-loop fiddling --- */
195
196 if (MP_CMP(u, >=, v)) {
197 u = mp_sub(u, u, v);
198 if (ext) {
199 X = mp_sub(X, X, x);
200 Y = mp_sub(Y, Y, y);
201 }
202 } else {
203 v = mp_sub(v, v, u);
204 if (ext) {
205 x = mp_sub(x, x, X);
206 y = mp_sub(y, y, Y);
207 }
208 }
209
210 if (MP_CMP(u, ==, MP_ZERO))
211 break;
212 }
213
214 /* --- Write the results out --- */
215
216 if (gcd)
217 *gcd = mp_lsl(v, v, shift);
218 else
219 MP_DROP(v);
220
221 /* --- Perform a little normalization --- *
222 *
223 * Ensure that the coefficient returned is positive, if there is only one.
224 * If there are two, favour @y@.
225 */
226
227 if (ext) {
228 if (swap) {
229 mp *t = x; x = y; y = t;
da83a561 230 t = a; a = b; b = t;
d3409d5e 231 }
232 if (yy) {
233 if (y->f & MP_NEG) {
234 y = mp_add(y, y, a);
235 x = mp_sub(x, x, b);
236 }
237 } else if (x->f & MP_NEG)
238 x = mp_add(x, x, b);
239
240 if (xx) *xx = x; else MP_DROP(x);
241 if (yy) *yy = y; else MP_DROP(y);
242 }
243
244 MP_DROP(u);
245 MP_DROP(X); MP_DROP(Y);
246 MP_DROP(a); MP_DROP(b);
247}
248
249/*----- Test rig ----------------------------------------------------------*/
250
251#ifdef TEST_RIG
252
253static int gcd(dstr *v)
254{
255 int ok = 1;
256 mp *a = *(mp **)v[0].buf;
257 mp *b = *(mp **)v[1].buf;
258 mp *g = *(mp **)v[2].buf;
259 mp *x = *(mp **)v[3].buf;
260 mp *y = *(mp **)v[4].buf;
261
262 mp *gg, *xx, *yy;
263 mp_gcd(&gg, &xx, &yy, a, b);
264 if (MP_CMP(x, !=, xx)) {
265 fputs("\n*** mp_gcd(x) failed", stderr);
266 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
267 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
268 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
269 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
270 fputc('\n', stderr);
271 ok = 0;
272 }
273 if (MP_CMP(y, !=, yy)) {
274 fputs("\n*** mp_gcd(y) failed", stderr);
275 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
276 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
277 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
278 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
279 fputc('\n', stderr);
280 ok = 0;
281 }
282
283 if (!ok) {
284 mp *ax = mp_mul(MP_NEW, a, xx);
285 mp *by = mp_mul(MP_NEW, b, yy);
286 ax = mp_add(ax, ax, by);
287 if (MP_CMP(ax, ==, gg))
288 fputs("\n*** (Alternative result found.)\n", stderr);
289 MP_DROP(ax);
290 MP_DROP(by);
291 }
292
293 if (MP_CMP(g, !=, gg)) {
294 fputs("\n*** mp_gcd(gcd) failed", stderr);
295 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
296 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
297 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
298 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
299 fputc('\n', stderr);
300 ok = 0;
301 }
302 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
303 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
304 return (ok);
305}
306
307static test_chunk tests[] = {
308 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
309 { 0, 0, { 0 } }
310};
311
312int main(int argc, char *argv[])
313{
314 sub_init();
315 test_run(argc, argv, tests, SRCDIR "/tests/mp");
316 return (0);
317}
318
319#endif
320
321/*----- That's all, folks -------------------------------------------------*/