3 * $Id: gf-arith.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
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
)
182 unsigned long m
= mp_bits(f
) - 1;
190 v
= gf_add(v
, u
, MP_TWO
);
191 gf_gcd(&v
, 0, 0, v
, f
);
192 if (!MP_EQ(v
, MP_ONE
)) break;
200 /*----- Test rig ----------------------------------------------------------*/
204 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
206 if (!MP_EQ(expect
, result
)) {
207 fprintf(stderr
, "\n*** %s failed", op
);
208 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 16);
209 fputs("\n*** b = ", stderr
); mp_writefile(b
, stderr
, 16);
210 fputs("\n*** result = ", stderr
); mp_writefile(result
, stderr
, 16);
211 fputs("\n*** expect = ", stderr
); mp_writefile(expect
, stderr
, 16);
218 #define RIG(name, op) \
219 static int t##name(dstr *v) \
221 mp *a = *(mp **)v[0].buf; \
222 mp *b = *(mp **)v[1].buf; \
223 mp *r = *(mp **)v[2].buf; \
224 mp *c = op(MP_NEW, a, b); \
225 int ok = verify(#name, r, c, a, b); \
226 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
227 assert(mparena_count(MPARENA_GLOBAL) == 0); \
236 static int tsqr(dstr
*v
)
238 mp
*a
= *(mp
**)v
[0].buf
;
239 mp
*r
= *(mp
**)v
[1].buf
;
242 c
= gf_sqr(MP_NEW
, a
);
243 ok
&= verify("sqr", r
, c
, a
, MP_ZERO
);
244 mp_drop(a
); mp_drop(r
); mp_drop(c
);
245 assert(mparena_count(MPARENA_GLOBAL
) == 0);
249 static int tdiv(dstr
*v
)
251 mp
*a
= *(mp
**)v
[0].buf
;
252 mp
*b
= *(mp
**)v
[1].buf
;
253 mp
*q
= *(mp
**)v
[2].buf
;
254 mp
*r
= *(mp
**)v
[3].buf
;
255 mp
*c
= MP_NEW
, *d
= MP_NEW
;
257 gf_div(&c
, &d
, a
, b
);
258 ok
&= verify("div(quotient)", q
, c
, a
, b
);
259 ok
&= verify("div(remainder)", r
, d
, a
, b
);
260 mp_drop(a
); mp_drop(b
); mp_drop(c
); mp_drop(d
); mp_drop(r
); mp_drop(q
);
261 assert(mparena_count(MPARENA_GLOBAL
) == 0);
265 static int tirred(dstr
*v
)
267 mp
*a
= *(mp
**)v
[0].buf
;
268 int r
= *(int *)v
[1].buf
;
269 int c
= gf_irreduciblep(a
);
273 fprintf(stderr
, "\n*** irred failed");
274 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 16);
275 fprintf(stderr
, "\n*** r = %d\n", r
);
276 fprintf(stderr
, "*** c = %d\n", c
);
279 assert(mparena_count(MPARENA_GLOBAL
) == 0);
283 static test_chunk tests
[] = {
284 { "add", tadd
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
285 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
286 { "sqr", tsqr
, { &type_mp
, &type_mp
, 0 } },
287 { "div", tdiv
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
288 { "irred", tirred
, { &type_mp
, &type_int
, 0 } },
292 int main(int argc
, char *argv
[])
295 test_run(argc
, argv
, tests
, SRCDIR
"/tests/gf");
301 /*----- That's all, folks -------------------------------------------------*/