7c09d3aba978a3953b27a7994dd19de5e9914f57
[u/mdw/catacomb] / gf-gcd.c
1 /* -*-c-*-
2 *
3 * $Id: gf-gcd.c,v 1.2 2004/03/21 22:52:06 mdw Exp $
4 *
5 * Euclidian algorithm on binary polynomials
6 *
7 * (c) 2004 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 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gf-gcd.c,v $
33 * Revision 1.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
35 *
36 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include "gf.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @gf_gcd@ --- *
48 *
49 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
50 * @mp *a, *b@ = sources (must be nonzero)
51 *
52 *
53 * Returns: ---
54 *
55 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
56 * @ax + by = gcd(a, b)@. This is useful for computing modular
57 * inverses.
58 */
59
60 void gf_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
61 {
62 mp *x = MP_ONE, *X = MP_ZERO;
63 mp *y = MP_ZERO, *Y = MP_ONE;
64 mp *u, *v;
65 mp *q = MP_NEW;
66 unsigned f = 0;
67
68 #define f_swap 1u
69 #define f_ext 2u
70
71 /* --- Sort out some initial flags --- */
72
73 if (xx || yy)
74 f |= f_ext;
75
76 /* --- Ensure that @a@ is larger than @b@ --- *
77 *
78 * If they're the same length we don't care which order they're in, so this
79 * unsigned comparison is fine.
80 */
81
82 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
83 { mp *t = a; a = b; b = t; }
84 f |= f_swap;
85 }
86
87 /* --- Check for zeroness --- */
88
89 if (MP_EQ(b, MP_ZERO)) {
90
91 /* --- Store %$|a|$% as the GCD --- */
92
93 if (gcd) {
94 if (*gcd) MP_DROP(*gcd);
95 a = MP_COPY(a);
96 *gcd = a;
97 }
98
99 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
100
101 if (f & f_ext) {
102 if (f & f_swap) {
103 mp **t = xx; xx = yy; yy = t;
104 }
105 if (xx) {
106 if (*xx) MP_DROP(*xx);
107 if (MP_EQ(a, MP_ZERO))
108 *xx = MP_ZERO;
109 else
110 *xx = MP_ONE;
111 }
112 if (yy) {
113 if (*yy) MP_DROP(*yy);
114 *yy = MP_ZERO;
115 }
116 }
117 return;
118 }
119
120 /* --- Take a reference to the arguments --- */
121
122 a = MP_COPY(a);
123 b = MP_COPY(b);
124
125 /* --- Make sure @a@ and @b@ are not both even --- */
126
127 MP_SPLIT(a); a->f &= ~MP_NEG;
128 MP_SPLIT(b); b->f &= ~MP_NEG;
129
130 u = MP_COPY(a);
131 v = MP_COPY(b);
132
133 while (MP_LEN(v)) {
134 mp *t;
135 gf_div(&q, &u, u, v);
136 if (f & f_ext) {
137 t = gf_mul(MP_NEW, X, q);
138 t = gf_add(t, x, t);
139 MP_DROP(x); x = X; X = t;
140 t = gf_mul(MP_NEW, Y, q);
141 t = gf_add(t, y, t);
142 MP_DROP(y); y = Y; Y = t;
143 }
144 t = u; u = v; v = t;
145 }
146
147 MP_DROP(q);
148 if (!gcd)
149 MP_DROP(u);
150 else {
151 if (*gcd) MP_DROP(*gcd);
152 u->f &= ~MP_NEG;
153 *gcd = u;
154 }
155
156 /* --- Perform a little normalization --- */
157
158 if (f & f_ext) {
159
160 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
161
162 if (f & f_swap) {
163 mp *t = x; x = y; y = t;
164 t = a; a = b; b = t;
165 }
166
167 /* --- Store the results --- */
168
169 if (!xx)
170 MP_DROP(x);
171 else {
172 if (*xx) MP_DROP(*xx);
173 *xx = x;
174 }
175
176 if (!yy)
177 MP_DROP(y);
178 else {
179 if (*yy) MP_DROP(*yy);
180 *yy = y;
181 }
182 }
183
184 MP_DROP(v);
185 MP_DROP(X); MP_DROP(Y);
186 MP_DROP(a); MP_DROP(b);
187 }
188
189 /*----- Test rig ----------------------------------------------------------*/
190
191 #ifdef TEST_RIG
192
193 static int gcd(dstr *v)
194 {
195 int ok = 1;
196 mp *a = *(mp **)v[0].buf;
197 mp *b = *(mp **)v[1].buf;
198 mp *g = *(mp **)v[2].buf;
199 mp *x = *(mp **)v[3].buf;
200 mp *y = *(mp **)v[4].buf;
201
202 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
203 gf_gcd(&gg, &xx, &yy, a, b);
204 if (!MP_EQ(x, xx)) {
205 fputs("\n*** mp_gcd(x) failed", stderr);
206 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
207 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
208 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 16);
209 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 16);
210 fputc('\n', stderr);
211 ok = 0;
212 }
213 if (!MP_EQ(y, yy)) {
214 fputs("\n*** mp_gcd(y) failed", stderr);
215 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
216 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
217 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 16);
218 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 16);
219 fputc('\n', stderr);
220 ok = 0;
221 }
222
223 if (!ok) {
224 mp *ax = gf_mul(MP_NEW, a, xx);
225 mp *by = gf_mul(MP_NEW, b, yy);
226 ax = gf_add(ax, ax, by);
227 if (MP_EQ(ax, gg))
228 fputs("\n*** (Alternative result found.)\n", stderr);
229 MP_DROP(ax);
230 MP_DROP(by);
231 }
232
233 if (!MP_EQ(g, gg)) {
234 fputs("\n*** mp_gcd(gcd) failed", stderr);
235 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
236 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
237 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 16);
238 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 16);
239 fputc('\n', stderr);
240 ok = 0;
241 }
242 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
243 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
244 assert(mparena_count(MPARENA_GLOBAL) == 0);
245 return (ok);
246 }
247
248 static test_chunk tests[] = {
249 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
250 { 0, 0, { 0 } }
251 };
252
253 int main(int argc, char *argv[])
254 {
255 sub_init();
256 test_run(argc, argv, tests, SRCDIR "/tests/gf");
257 return (0);
258 }
259
260 #endif
261
262 /*----- That's all, folks -------------------------------------------------*/