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