math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / gf-arith.c
1 /* -*-c-*-
2 *
3 * Basic arithmetic on binary polynomials
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "gf.h"
31
32 /*----- Macros ------------------------------------------------------------*/
33
34 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
35
36 /*----- Main code ---------------------------------------------------------*/
37
38 /* --- @gf_add@ --- *
39 *
40 * Arguments: @mp *d@ = destination
41 * @mp *a, *b@ = sources
42 *
43 * Returns: Result, @a@ added to @b@.
44 */
45
46 mp *gf_add(mp *d, mp *a, mp *b)
47 {
48 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & MP_BURN);
49 gfx_add(d->v, d->vl, a->v, a->vl, b->v, b->vl);
50 d->f = (a->f | b->f) & MP_BURN;
51 MP_SHRINK(d);
52 return (d);
53 }
54
55 /* --- @gf_mul@ --- *
56 *
57 * Arguments: @mp *d@ = destination
58 * @mp *a, *b@ = sources
59 *
60 * Returns: Result, @a@ multiplied by @b@.
61 */
62
63 mp *gf_mul(mp *d, mp *a, mp *b)
64 {
65 a = MP_COPY(a);
66 b = MP_COPY(b);
67
68 if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= GFK_THRESH) {
69 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
70 gfx_mul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
71 } else {
72 size_t m = MAX(MP_LEN(a), MP_LEN(b));
73 mpw *s;
74 MP_DEST(d, 2 * m, a->f | b->f | MP_UNDEF);
75 s = mpalloc(d->a, 3 * m);
76 gfx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 3 * m);
77 mpfree(d->a, s);
78 }
79
80 d->f = (a->f | b->f) & MP_BURN;
81 MP_SHRINK(d);
82 MP_DROP(a);
83 MP_DROP(b);
84 return (d);
85 }
86
87 /* --- @gf_sqr@ --- *
88 *
89 * Arguments: @mp *d@ = destination
90 * @mp *a@ = source
91 *
92 * Returns: Result, @a@ squared.
93 */
94
95 mp *gf_sqr(mp *d, mp *a)
96 {
97 MP_COPY(a);
98 MP_DEST(d, 2 * MP_LEN(a), a->f & MP_BURN);
99 gfx_sqr(d->v, d->vl, a->v, a->vl);
100 d->f = a->f & MP_BURN;
101 MP_SHRINK(d);
102 MP_DROP(a);
103 return (d);
104 }
105
106 /* --- @gf_div@ --- *
107 *
108 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
109 * @mp *a, *b@ = sources
110 *
111 * Use: Calculates the quotient and remainder when @a@ is divided by
112 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
113 * Either of @qq@ or @rr@ may be null to indicate that the
114 * result is irrelevant. (Discarding both results is silly.)
115 * There is a performance advantage if @a == *rr@.
116 */
117
118 void gf_div(mp **qq, mp **rr, mp *a, mp *b)
119 {
120 mp *r = rr ? *rr : MP_NEW;
121 mp *q = qq ? *qq : MP_NEW;
122
123 /* --- Set the remainder up right --- */
124
125 b = MP_COPY(b);
126 a = MP_COPY(a);
127 if (r)
128 MP_DROP(r);
129 r = a;
130 MP_DEST(r, MP_LEN(b) + 2, a->f | b->f);
131
132 /* --- Fix up the quotient too --- */
133
134 r = MP_COPY(r);
135 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
136 MP_DROP(r);
137
138 /* --- Perform the calculation --- */
139
140 gfx_div(q->v, q->vl, r->v, r->vl, b->v, b->vl);
141
142 /* --- Sort out the sign of the results --- *
143 *
144 * If the signs of the arguments differ, and the remainder is nonzero, I
145 * must add one to the absolute value of the quotient and subtract the
146 * remainder from @b@.
147 */
148
149 q->f = (r->f | b->f) & MP_BURN;
150 r->f = (r->f | b->f) & MP_BURN;
151
152 /* --- Store the return values --- */
153
154 MP_DROP(b);
155
156 if (!qq)
157 MP_DROP(q);
158 else {
159 MP_SHRINK(q);
160 *qq = q;
161 }
162
163 if (!rr)
164 MP_DROP(r);
165 else {
166 MP_SHRINK(r);
167 *rr = r;
168 }
169 }
170
171 /* --- @gf_irreduciblep@ --- *
172 *
173 * Arguments: @mp *f@ = a polynomial
174 *
175 * Returns: Nonzero if the polynomial is irreducible; otherwise zero.
176 */
177
178 int gf_irreduciblep(mp *f)
179 {
180 unsigned long m;
181 mp *u = MP_TWO;
182 mp *v = MP_NEW;
183
184 if (MP_ZEROP(f))
185 return (0);
186 else if (MP_LEN(f) == 1) {
187 if (f->v[0] < 2) return (0);
188 if (f->v[0] < 4) return (1);
189 }
190 m = (mp_bits(f) - 1)/2;
191 while (m) {
192 u = gf_sqr(u, u);
193 gf_div(0, &u, u, f);
194 v = gf_add(v, u, MP_TWO);
195 gf_gcd(&v, 0, 0, v, f);
196 if (!MP_EQ(v, MP_ONE)) break;
197 m--;
198 }
199 MP_DROP(u);
200 MP_DROP(v);
201 return (!m);
202 }
203
204 /*----- Test rig ----------------------------------------------------------*/
205
206 #ifdef TEST_RIG
207
208 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
209 {
210 if (!MP_EQ(expect, result)) {
211 fprintf(stderr, "\n*** %s failed", op);
212 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
213 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 16);
214 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 16);
215 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 16);
216 fputc('\n', stderr);
217 return (0);
218 }
219 return (1);
220 }
221
222 #define RIG(name, op) \
223 static int t##name(dstr *v) \
224 { \
225 mp *a = *(mp **)v[0].buf; \
226 mp *b = *(mp **)v[1].buf; \
227 mp *r = *(mp **)v[2].buf; \
228 mp *c = op(MP_NEW, a, b); \
229 int ok = verify(#name, r, c, a, b); \
230 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
231 assert(mparena_count(MPARENA_GLOBAL) == 0); \
232 return (ok); \
233 }
234
235 RIG(add, gf_add)
236 RIG(mul, gf_mul)
237 RIG(exp, gf_exp)
238
239 #undef RIG
240
241 static int tsqr(dstr *v)
242 {
243 mp *a = *(mp **)v[0].buf;
244 mp *r = *(mp **)v[1].buf;
245 mp *c = MP_NEW;
246 int ok = 1;
247 c = gf_sqr(MP_NEW, a);
248 ok &= verify("sqr", r, c, a, MP_ZERO);
249 mp_drop(a); mp_drop(r); mp_drop(c);
250 assert(mparena_count(MPARENA_GLOBAL) == 0);
251 return (ok);
252 }
253
254 static int tdiv(dstr *v)
255 {
256 mp *a = *(mp **)v[0].buf;
257 mp *b = *(mp **)v[1].buf;
258 mp *q = *(mp **)v[2].buf;
259 mp *r = *(mp **)v[3].buf;
260 mp *c = MP_NEW, *d = MP_NEW;
261 int ok = 1;
262 gf_div(&c, &d, a, b);
263 ok &= verify("div(quotient)", q, c, a, b);
264 ok &= verify("div(remainder)", r, d, a, b);
265 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
266 assert(mparena_count(MPARENA_GLOBAL) == 0);
267 return (ok);
268 }
269
270 static int tirred(dstr *v)
271 {
272 mp *a = *(mp **)v[0].buf;
273 int r = *(int *)v[1].buf;
274 int c = gf_irreduciblep(a);
275 int ok = 1;
276 if (r != c) {
277 ok = 0;
278 fprintf(stderr, "\n*** irred failed");
279 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
280 fprintf(stderr, "\n*** r = %d\n", r);
281 fprintf(stderr, "*** c = %d\n", c);
282 }
283 mp_drop(a);
284 assert(mparena_count(MPARENA_GLOBAL) == 0);
285 return (ok);
286 }
287
288 static test_chunk tests[] = {
289 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
290 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
291 { "sqr", tsqr, { &type_mp, &type_mp, 0 } },
292 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
293 { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } },
294 { "irred", tirred, { &type_mp, &type_int, 0 } },
295 { 0, 0, { 0 } },
296 };
297
298 int main(int argc, char *argv[])
299 {
300 sub_init();
301 test_run(argc, argv, tests, SRCDIR "/t/gf");
302 return (0);
303 }
304
305 #endif
306
307 /*----- That's all, folks -------------------------------------------------*/