math/mp-gcd.c: Avoid clobbering constants during the sign fixup.
[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) { MP_SPLIT(x); x->f ^= MP_NEG; }
203 if (f & f_bneg) { MP_SPLIT(y); y->f ^= MP_NEG; }
204
205 /* --- Store the results --- */
206
207 if (!xx)
208 MP_DROP(x);
209 else {
210 if (*xx) MP_DROP(*xx);
211 *xx = x;
212 }
213
214 if (!yy)
215 MP_DROP(y);
216 else {
217 if (*yy) MP_DROP(*yy);
218 *yy = y;
219 }
220 }
221
222 MP_DROP(v);
223 MP_DROP(X); MP_DROP(Y);
224 MP_DROP(a); MP_DROP(b);
225 }
226
227 /* -- @mp_modinv@ --- *
228 *
229 * Arguments: @mp *d@ = destination
230 * @mp *x@ = argument
231 * @mp *p@ = modulus
232 *
233 * Returns: The inverse %$x^{-1} \bmod p$%.
234 *
235 * Use: Computes a modular inverse. An assertion fails if %$p$%
236 * has no inverse.
237 */
238
239 mp *mp_modinv(mp *d, mp *x, mp *p)
240 {
241 mp *g = MP_NEW;
242 mp_gcd(&g, 0, &d, p, x);
243 assert(MP_EQ(g, MP_ONE));
244 mp_drop(g);
245 return (d);
246 }
247
248 /*----- Test rig ----------------------------------------------------------*/
249
250 #ifdef TEST_RIG
251
252 static int modinv(dstr *v)
253 {
254 int ok = 1;
255 mp *x = *(mp **)v[0].buf;
256 mp *m = *(mp **)v[1].buf;
257 mp *r = *(mp **)v[2].buf;
258
259 mp *y = mp_modinv(MP_NEW, x, m);
260 if (!MP_EQ(y, r)) {
261 fputs("\n*** mp_modinv failed", stderr);
262 fputs("\nx = ", stderr); mp_writefile(x, stderr, 10);
263 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
264 fputs("\nexpect = ", stderr); mp_writefile(r, stderr, 10);
265 fputs("\nresult = ", stderr); mp_writefile(y, stderr, 10);
266 ok = 0;
267 }
268 MP_DROP(x); MP_DROP(m); MP_DROP(r); MP_DROP(y);
269 assert(mparena_count(MPARENA_GLOBAL) == 0);
270 return (ok);
271 }
272
273 static int gcd(dstr *v)
274 {
275 int ok = 1;
276 mp *a = *(mp **)v[0].buf;
277 mp *b = *(mp **)v[1].buf;
278 mp *g = *(mp **)v[2].buf;
279 mp *x = *(mp **)v[3].buf;
280 mp *y = *(mp **)v[4].buf;
281
282 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
283 mp_gcd(&gg, &xx, &yy, a, b);
284 if (!MP_EQ(x, xx)) {
285 fputs("\n*** mp_gcd(x) failed", stderr);
286 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
287 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
288 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
289 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
290 fputc('\n', stderr);
291 ok = 0;
292 }
293 if (!MP_EQ(y, yy)) {
294 fputs("\n*** mp_gcd(y) 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(y, stderr, 10);
298 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
299 fputc('\n', stderr);
300 ok = 0;
301 }
302
303 if (!ok) {
304 mp *ax = mp_mul(MP_NEW, a, xx);
305 mp *by = mp_mul(MP_NEW, b, yy);
306 ax = mp_add(ax, ax, by);
307 if (MP_EQ(ax, gg))
308 fputs("\n*** (Alternative result found.)\n", stderr);
309 MP_DROP(ax);
310 MP_DROP(by);
311 }
312
313 if (!MP_EQ(g, gg)) {
314 fputs("\n*** mp_gcd(gcd) failed", stderr);
315 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
316 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
317 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
318 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
319 fputc('\n', stderr);
320 ok = 0;
321 }
322 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
323 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
324 assert(mparena_count(MPARENA_GLOBAL) == 0);
325 return (ok);
326 }
327
328 static test_chunk tests[] = {
329 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
330 { "modinv", modinv, { &type_mp, &type_mp, &type_mp, 0 } },
331 { 0, 0, { 0 } }
332 };
333
334 int main(int argc, char *argv[])
335 {
336 sub_init();
337 test_run(argc, argv, tests, SRCDIR "/t/mp");
338 return (0);
339 }
340
341 #endif
342
343 /*----- That's all, folks -------------------------------------------------*/