Expunge revision histories in files.
[u/mdw/catacomb] / mp-gcd.c
1 /* -*-c-*-
2 *
3 * $Id: mp-gcd.c,v 1.7 2004/04/08 01:36:15 mdw Exp $
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 /*----- 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
45 * inverses.
46 */
47
48 void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
49 {
50 mp *x = MP_ONE, *X = MP_ZERO;
51 mp *y = MP_ZERO, *Y = MP_ONE;
52 mp *u, *v;
53 mp *q = MP_NEW;
54 unsigned f = 0;
55
56 #define f_swap 1u
57 #define f_aneg 2u
58 #define f_bneg 4u
59 #define f_ext 8u
60
61 /* --- Sort out some initial flags --- */
62
63 if (xx || yy)
64 f |= f_ext;
65
66 if (a->f & MP_NEG)
67 f |= f_aneg;
68 if (b->f & MP_NEG)
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)) {
77 { mp *t = a; a = b; b = t; }
78 f |= f_swap;
79 }
80
81 /* --- Check for zeroness --- */
82
83 if (MP_EQ(b, MP_ZERO)) {
84
85 /* --- Store %$|a|$% as the GCD --- */
86
87 if (gcd) {
88 if (*gcd) MP_DROP(*gcd);
89 a = MP_COPY(a);
90 if (a->f & MP_NEG) {
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) {
102 mp **t = xx; xx = yy; yy = t;
103 }
104 if (xx) {
105 if (*xx) MP_DROP(*xx);
106 if (MP_EQ(a, MP_ZERO))
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;
119 }
120
121 /* --- Take a reference to the arguments --- */
122
123 a = MP_COPY(a);
124 b = MP_COPY(b);
125
126 /* --- Make sure @a@ and @b@ are not both even --- */
127
128 MP_SPLIT(a); a->f &= ~MP_NEG;
129 MP_SPLIT(b); b->f &= ~MP_NEG;
130
131 u = MP_COPY(a);
132 v = MP_COPY(b);
133
134 while (MP_LEN(v)) {
135 mp *t;
136 mp_div(&q, &u, u, v);
137 if (f & f_ext) {
138 t = mp_mul(MP_NEW, X, q);
139 t = mp_sub(t, x, t);
140 MP_DROP(x); x = X; X = t;
141 t = mp_mul(MP_NEW, Y, q);
142 t = mp_sub(t, y, t);
143 MP_DROP(y); y = Y; Y = t;
144 }
145 t = u; u = v; v = t;
146 }
147
148 MP_DROP(q);
149 if (!gcd)
150 MP_DROP(u);
151 else {
152 if (*gcd) MP_DROP(*gcd);
153 u->f &= ~MP_NEG;
154 *gcd = u;
155 }
156
157 /* --- Perform a little normalization --- *
158 *
159 * Ensure that the coefficient returned is positive, if there is only one.
160 * If there are two, favour @y@. Of course, if the original arguments were
161 * negative then I'll need to twiddle their signs as well.
162 */
163
164 if (f & f_ext) {
165
166 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
167
168 if (f & f_swap) {
169 mp *t = x; x = y; y = t;
170 t = a; a = b; b = t;
171 }
172
173 /* --- Sort out the signs --- *
174 *
175 * Note that %$ax + by = a(x - b) + b(y + a)$%.
176 *
177 * This is currently bodgy. It needs sorting out at some time.
178 */
179
180 if (yy) {
181 if (y->f & MP_NEG) {
182 do {
183 y = mp_add(y, y, a);
184 x = mp_sub(x, x, b);
185 } while (y->f & MP_NEG);
186 } else {
187 while (MP_CMP(y, >=, a)) {
188 y = mp_sub(y, y, a);
189 x = mp_add(x, x, b);
190 }
191 }
192 } else {
193 if (x->f & MP_NEG) {
194 do
195 x = mp_add(x, x, b);
196 while (x->f & MP_NEG);
197 } else {
198 while (MP_CMP(x, >=, b))
199 x = mp_sub(x, x, b);
200 }
201 }
202
203 /* --- Twiddle the signs --- */
204
205 if (f & f_aneg)
206 x->f ^= MP_NEG;
207 if (f & f_bneg)
208 y->f ^= MP_NEG;
209
210 /* --- Store the results --- */
211
212 if (!xx)
213 MP_DROP(x);
214 else {
215 if (*xx) MP_DROP(*xx);
216 *xx = x;
217 }
218
219 if (!yy)
220 MP_DROP(y);
221 else {
222 if (*yy) MP_DROP(*yy);
223 *yy = y;
224 }
225 }
226
227 MP_DROP(v);
228 MP_DROP(X); MP_DROP(Y);
229 MP_DROP(a); MP_DROP(b);
230 }
231
232 /* -- @mp_modinv@ --- *
233 *
234 * Arguments: @mp *d@ = destination
235 * @mp *x@ = argument
236 * @mp *p@ = modulus
237 *
238 * Returns: The inverse %$x^{-1} \bmod p$%.
239 *
240 * Use: Computes a modular inverse. An assertion fails if %$p$%
241 * has no inverse.
242 */
243
244 mp *mp_modinv(mp *d, mp *x, mp *p)
245 {
246 mp *g = MP_NEW;
247 mp_gcd(&g, 0, &d, p, x);
248 assert(MP_EQ(g, MP_ONE));
249 mp_drop(g);
250 return (d);
251 }
252
253 /*----- Test rig ----------------------------------------------------------*/
254
255 #ifdef TEST_RIG
256
257 static int gcd(dstr *v)
258 {
259 int ok = 1;
260 mp *a = *(mp **)v[0].buf;
261 mp *b = *(mp **)v[1].buf;
262 mp *g = *(mp **)v[2].buf;
263 mp *x = *(mp **)v[3].buf;
264 mp *y = *(mp **)v[4].buf;
265
266 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
267 mp_gcd(&gg, &xx, &yy, a, b);
268 if (!MP_EQ(x, xx)) {
269 fputs("\n*** mp_gcd(x) failed", stderr);
270 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
271 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
272 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
273 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
274 fputc('\n', stderr);
275 ok = 0;
276 }
277 if (!MP_EQ(y, yy)) {
278 fputs("\n*** mp_gcd(y) failed", stderr);
279 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
280 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
281 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
282 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
283 fputc('\n', stderr);
284 ok = 0;
285 }
286
287 if (!ok) {
288 mp *ax = mp_mul(MP_NEW, a, xx);
289 mp *by = mp_mul(MP_NEW, b, yy);
290 ax = mp_add(ax, ax, by);
291 if (MP_EQ(ax, gg))
292 fputs("\n*** (Alternative result found.)\n", stderr);
293 MP_DROP(ax);
294 MP_DROP(by);
295 }
296
297 if (!MP_EQ(g, gg)) {
298 fputs("\n*** mp_gcd(gcd) failed", stderr);
299 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
300 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
301 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
302 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
303 fputc('\n', stderr);
304 ok = 0;
305 }
306 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
307 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
308 assert(mparena_count(MPARENA_GLOBAL) == 0);
309 return (ok);
310 }
311
312 static test_chunk tests[] = {
313 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
314 { 0, 0, { 0 } }
315 };
316
317 int main(int argc, char *argv[])
318 {
319 sub_init();
320 test_run(argc, argv, tests, SRCDIR "/tests/mp");
321 return (0);
322 }
323
324 #endif
325
326 /*----- That's all, folks -------------------------------------------------*/