math/mp-gcd.c: Avoid clobbering constants during the sign fixup.
[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
9b8ff1cf
MW
202 if (f & f_aneg) { MP_SPLIT(x); x->f ^= MP_NEG; }
203 if (f & f_bneg) { MP_SPLIT(y); y->f ^= MP_NEG; }
ef5f4810 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 }
d3409d5e 220 }
221
b2253c70 222 MP_DROP(v);
d3409d5e 223 MP_DROP(X); MP_DROP(Y);
224 MP_DROP(a); MP_DROP(b);
225}
226
b817bfc6 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
239mp *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
d3409d5e 248/*----- Test rig ----------------------------------------------------------*/
249
250#ifdef TEST_RIG
251
7c404803
MW
252static 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);
45c0fd36
MW
262 fputs("\nx = ", stderr); mp_writefile(x, stderr, 10);
263 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
7c404803
MW
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);
45c0fd36 270 return (ok);
7c404803
MW
271}
272
d3409d5e 273static 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
ef5f4810 282 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
d3409d5e 283 mp_gcd(&gg, &xx, &yy, a, b);
b2253c70 284 if (!MP_EQ(x, xx)) {
d3409d5e 285 fputs("\n*** mp_gcd(x) failed", stderr);
45c0fd36
MW
286 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
287 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 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 }
b2253c70 293 if (!MP_EQ(y, yy)) {
d3409d5e 294 fputs("\n*** mp_gcd(y) failed", stderr);
45c0fd36
MW
295 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
296 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 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);
b2253c70 307 if (MP_EQ(ax, gg))
d3409d5e 308 fputs("\n*** (Alternative result found.)\n", stderr);
309 MP_DROP(ax);
310 MP_DROP(by);
311 }
312
b2253c70 313 if (!MP_EQ(g, gg)) {
d3409d5e 314 fputs("\n*** mp_gcd(gcd) failed", stderr);
45c0fd36
MW
315 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
316 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
d3409d5e 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);
ef5f4810 324 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 325 return (ok);
326}
327
328static test_chunk tests[] = {
329 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
7c404803 330 { "modinv", modinv, { &type_mp, &type_mp, &type_mp, 0 } },
d3409d5e 331 { 0, 0, { 0 } }
332};
333
334int main(int argc, char *argv[])
335{
336 sub_init();
0f00dc4c 337 test_run(argc, argv, tests, SRCDIR "/t/mp");
d3409d5e 338 return (0);
339}
340
341#endif
342
343/*----- That's all, folks -------------------------------------------------*/