math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / math / mp-gcd.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
d3409d5e 3 * Extended GCD calculation
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d3409d5e 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.
45c0fd36 16 *
d3409d5e 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.
45c0fd36 21 *
d3409d5e 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
d3409d5e 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
ef5f4810 43 * inverses.
d3409d5e 44 */
45
46void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
47{
b2253c70 48 mp *x = MP_ONE, *X = MP_ZERO;
49 mp *y = MP_ZERO, *Y = MP_ONE;
d3409d5e 50 mp *u, *v;
3d6115d1 51 mp *q = MP_NEW, *t, *spare = MP_NEW;
ef5f4810 52 unsigned f = 0;
53
ceb3f0c0 54#define f_swap 1u
55#define f_aneg 2u
56#define f_bneg 4u
57#define f_ext 8u
ef5f4810 58
59 /* --- Sort out some initial flags --- */
d3409d5e 60
ef5f4810 61 if (xx || yy)
62 f |= f_ext;
d3409d5e 63
a69a3efd 64 if (MP_NEGP(a))
ef5f4810 65 f |= f_aneg;
a69a3efd 66 if (MP_NEGP(b))
ef5f4810 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)) {
3d6115d1 75 t = a; a = b; b = t;
ef5f4810 76 f |= f_swap;
77 }
78
79 /* --- Check for zeroness --- */
80
3d6115d1 81 if (MP_ZEROP(b)) {
ef5f4810 82
83 /* --- Store %$|a|$% as the GCD --- */
84
85 if (gcd) {
86 if (*gcd) MP_DROP(*gcd);
87 a = MP_COPY(a);
a69a3efd 88 if (MP_NEGP(a)) {
ef5f4810 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) {
3d6115d1 100 mp **tt = xx; xx = yy; yy = tt;
ef5f4810 101 }
102 if (xx) {
103 if (*xx) MP_DROP(*xx);
b2253c70 104 if (MP_EQ(a, MP_ZERO))
ef5f4810 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;
d3409d5e 117 }
118
cf76bcbb 119 /* --- Force the signs on the arguments and take copies --- */
d3409d5e 120
121 a = MP_COPY(a);
122 b = MP_COPY(b);
123
ef5f4810 124 MP_SPLIT(a); a->f &= ~MP_NEG;
125 MP_SPLIT(b); b->f &= ~MP_NEG;
126
d3409d5e 127 u = MP_COPY(a);
128 v = MP_COPY(b);
129
cf76bcbb
MW
130 /* --- Main extended Euclidean algorithm --- */
131
a69a3efd 132 while (!MP_ZEROP(v)) {
b2253c70 133 mp_div(&q, &u, u, v);
134 if (f & f_ext) {
3d6115d1 135 t = mp_mul(spare, X, q);
b2253c70 136 t = mp_sub(t, x, t);
3d6115d1
MW
137 spare = x; x = X; X = t;
138 t = mp_mul(spare, Y, q);
b2253c70 139 t = mp_sub(t, y, t);
3d6115d1 140 spare = y; y = Y; Y = t;
d3409d5e 141 }
b2253c70 142 t = u; u = v; v = t;
d3409d5e 143 }
144
3d6115d1 145 MP_DROP(q); if (spare) MP_DROP(spare);
ef5f4810 146 if (!gcd)
b2253c70 147 MP_DROP(u);
ef5f4810 148 else {
149 if (*gcd) MP_DROP(*gcd);
b2253c70 150 u->f &= ~MP_NEG;
151 *gcd = u;
ef5f4810 152 }
d3409d5e 153
154 /* --- Perform a little normalization --- *
155 *
156 * Ensure that the coefficient returned is positive, if there is only one.
ef5f4810 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.
d3409d5e 159 */
160
ef5f4810 161 if (f & f_ext) {
162
163 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
164
165 if (f & f_swap) {
3d6115d1 166 t = x; x = y; y = t;
da83a561 167 t = a; a = b; b = t;
d3409d5e 168 }
ef5f4810 169
170 /* --- Sort out the signs --- *
171 *
172 * Note that %$ax + by = a(x - b) + b(y + a)$%.
c9110b35 173 *
174 * This is currently bodgy. It needs sorting out at some time.
ef5f4810 175 */
176
d3409d5e 177 if (yy) {
a69a3efd 178 if (MP_NEGP(y)) {
c9110b35 179 do {
180 y = mp_add(y, y, a);
181 x = mp_sub(x, x, b);
a69a3efd 182 } while (MP_NEGP(y));
c9110b35 183 } else {
184 while (MP_CMP(y, >=, a)) {
185 y = mp_sub(y, y, a);
186 x = mp_add(x, x, b);
187 }
d3409d5e 188 }
c9110b35 189 } else {
a69a3efd 190 if (MP_NEGP(x)) {
c9110b35 191 do
192 x = mp_add(x, x, b);
a69a3efd 193 while (MP_NEGP(x));
c9110b35 194 } else {
195 while (MP_CMP(x, >=, b))
196 x = mp_sub(x, x, b);
197 }
198 }
d3409d5e 199
ef5f4810 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 }
d3409d5e 222 }
223
b2253c70 224 MP_DROP(v);
d3409d5e 225 MP_DROP(X); MP_DROP(Y);
226 MP_DROP(a); MP_DROP(b);
227}
228
b817bfc6 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
241mp *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
d3409d5e 250/*----- Test rig ----------------------------------------------------------*/
251
252#ifdef TEST_RIG
253
7c404803
MW
254static 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);
45c0fd36
MW
264 fputs("\nx = ", stderr); mp_writefile(x, stderr, 10);
265 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
7c404803
MW
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);
45c0fd36 272 return (ok);
7c404803
MW
273}
274
d3409d5e 275static 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
ef5f4810 284 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
d3409d5e 285 mp_gcd(&gg, &xx, &yy, a, b);
b2253c70 286 if (!MP_EQ(x, xx)) {
d3409d5e 287 fputs("\n*** mp_gcd(x) failed", stderr);
45c0fd36
MW
288 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
289 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 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 }
b2253c70 295 if (!MP_EQ(y, yy)) {
d3409d5e 296 fputs("\n*** mp_gcd(y) failed", stderr);
45c0fd36
MW
297 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
298 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 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);
b2253c70 309 if (MP_EQ(ax, gg))
d3409d5e 310 fputs("\n*** (Alternative result found.)\n", stderr);
311 MP_DROP(ax);
312 MP_DROP(by);
313 }
314
b2253c70 315 if (!MP_EQ(g, gg)) {
d3409d5e 316 fputs("\n*** mp_gcd(gcd) failed", stderr);
45c0fd36
MW
317 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
318 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 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);
ef5f4810 326 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 327 return (ok);
328}
329
330static test_chunk tests[] = {
331 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
7c404803 332 { "modinv", modinv, { &type_mp, &type_mp, &type_mp, 0 } },
d3409d5e 333 { 0, 0, { 0 } }
334};
335
336int main(int argc, char *argv[])
337{
338 sub_init();
0f00dc4c 339 test_run(argc, argv, tests, SRCDIR "/t/mp");
d3409d5e 340 return (0);
341}
342
343#endif
344
345/*----- That's all, folks -------------------------------------------------*/