5 * Basic arithmetic on binary polynomials
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
34 /*----- Macros ------------------------------------------------------------*/
36 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
38 /*----- Main code ---------------------------------------------------------*/
42 * Arguments: @mp *d@ = destination
43 * @mp *a, *b@ = sources
45 * Returns: Result, @a@ added to @b@.
48 mp *gf_add(mp *d, mp *a, mp *b)
50 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & MP_BURN);
51 gfx_add(d->v, d->vl, a->v, a->vl, b->v, b->vl);
52 d->f = (a->f | b->f) & MP_BURN;
59 * Arguments: @mp *d@ = destination
60 * @mp *a, *b@ = sources
62 * Returns: Result, @a@ multiplied by @b@.
65 mp *gf_mul(mp *d, mp *a, mp *b)
70 if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= GFK_THRESH) {
71 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
72 gfx_mul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
74 size_t m = MAX(MP_LEN(a), MP_LEN(b));
76 MP_DEST(d, 2 * m, a->f | b->f | MP_UNDEF);
77 s = mpalloc(d->a, 3 * m);
78 gfx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 3 * m);
82 d->f = (a->f | b->f) & MP_BURN;
91 * Arguments: @mp *d@ = destination
94 * Returns: Result, @a@ squared.
97 mp *gf_sqr(mp *d, mp *a)
100 MP_DEST(d, 2 * MP_LEN(a), a->f & MP_BURN);
101 gfx_sqr(d->v, d->vl, a->v, a->vl);
102 d->f = a->f & MP_BURN;
108 /* --- @gf_div@ --- *
110 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
111 * @mp *a, *b@ = sources
113 * Use: Calculates the quotient and remainder when @a@ is divided by
114 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
115 * Either of @qq@ or @rr@ may be null to indicate that the
116 * result is irrelevant. (Discarding both results is silly.)
117 * There is a performance advantage if @a == *rr@.
120 void gf_div(mp **qq, mp **rr, mp *a, mp *b)
122 mp *r = rr ? *rr : MP_NEW;
123 mp *q = qq ? *qq : MP_NEW;
125 /* --- Set the remainder up right --- */
132 MP_DEST(r, MP_LEN(b) + 2, a->f | b->f);
134 /* --- Fix up the quotient too --- */
137 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
140 /* --- Perform the calculation --- */
142 gfx_div(q->v, q->vl, r->v, r->vl, b->v, b->vl);
144 /* --- Sort out the sign of the results --- *
146 * If the signs of the arguments differ, and the remainder is nonzero, I
147 * must add one to the absolute value of the quotient and subtract the
148 * remainder from @b@.
151 q->f = (r->f | b->f) & MP_BURN;
152 r->f = (r->f | b->f) & MP_BURN;
154 /* --- Store the return values --- */
173 /* --- @gf_irreduciblep@ --- *
175 * Arguments: @mp *f@ = a polynomial
177 * Returns: Nonzero if the polynomial is irreducible; otherwise zero.
180 int gf_irreduciblep(mp *f)
188 else if (MP_LEN(f) == 1) {
189 if (f->v[0] < 2) return (0);
190 if (f->v[0] < 4) return (1);
192 m = (mp_bits(f) - 1)/2;
196 v = gf_add(v, u, MP_TWO);
197 gf_gcd(&v, 0, 0, v, f);
198 if (!MP_EQ(v, MP_ONE)) break;
206 /*----- Test rig ----------------------------------------------------------*/
210 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
212 if (!MP_EQ(expect, result)) {
213 fprintf(stderr, "\n*** %s failed", op);
214 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
215 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 16);
216 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 16);
217 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 16);
224 #define RIG(name, op) \
225 static int t##name(dstr *v) \
227 mp *a = *(mp **)v[0].buf; \
228 mp *b = *(mp **)v[1].buf; \
229 mp *r = *(mp **)v[2].buf; \
230 mp *c = op(MP_NEW, a, b); \
231 int ok = verify(#name, r, c, a, b); \
232 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
233 assert(mparena_count(MPARENA_GLOBAL) == 0); \
243 static int tsqr(dstr *v)
245 mp *a = *(mp **)v[0].buf;
246 mp *r = *(mp **)v[1].buf;
249 c = gf_sqr(MP_NEW, a);
250 ok &= verify("sqr", r, c, a, MP_ZERO);
251 mp_drop(a); mp_drop(r); mp_drop(c);
252 assert(mparena_count(MPARENA_GLOBAL) == 0);
256 static int tdiv(dstr *v)
258 mp *a = *(mp **)v[0].buf;
259 mp *b = *(mp **)v[1].buf;
260 mp *q = *(mp **)v[2].buf;
261 mp *r = *(mp **)v[3].buf;
262 mp *c = MP_NEW, *d = MP_NEW;
264 gf_div(&c, &d, a, b);
265 ok &= verify("div(quotient)", q, c, a, b);
266 ok &= verify("div(remainder)", r, d, a, b);
267 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
268 assert(mparena_count(MPARENA_GLOBAL) == 0);
272 static int tirred(dstr *v)
274 mp *a = *(mp **)v[0].buf;
275 int r = *(int *)v[1].buf;
276 int c = gf_irreduciblep(a);
280 fprintf(stderr, "\n*** irred failed");
281 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
282 fprintf(stderr, "\n*** r = %d\n", r);
283 fprintf(stderr, "*** c = %d\n", c);
286 assert(mparena_count(MPARENA_GLOBAL) == 0);
290 static test_chunk tests[] = {
291 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
292 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
293 { "sqr", tsqr, { &type_mp, &type_mp, 0 } },
294 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
295 { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } },
296 { "irred", tirred, { &type_mp, &type_int, 0 } },
300 int main(int argc, char *argv[])
303 test_run(argc, argv, tests, SRCDIR "/tests/gf");
309 /*----- That's all, folks -------------------------------------------------*/