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