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