3 * Basic arithmetic on binary polynomials
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Macros ------------------------------------------------------------*/
34 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
36 /*----- Main code ---------------------------------------------------------*/
40 * Arguments: @mp *d@ = destination
41 * @mp *a, *b@ = sources
43 * Returns: Result, @a@ added to @b@.
46 mp
*gf_add(mp
*d
, mp
*a
, mp
*b
)
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
;
57 * Arguments: @mp *d@ = destination
58 * @mp *a, *b@ = sources
60 * Returns: Result, @a@ multiplied by @b@.
63 mp
*gf_mul(mp
*d
, mp
*a
, mp
*b
)
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
);
72 size_t m
= MAX(MP_LEN(a
), MP_LEN(b
));
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
);
80 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
89 * Arguments: @mp *d@ = destination
92 * Returns: Result, @a@ squared.
95 mp
*gf_sqr(mp
*d
, mp
*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
;
106 /* --- @gf_div@ --- *
108 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
109 * @mp *a, *b@ = sources
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@.
118 void gf_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
120 mp
*r
= rr ?
*rr
: MP_NEW
;
121 mp
*q
= qq ?
*qq
: MP_NEW
;
123 /* --- Set the remainder up right --- */
130 MP_DEST(r
, MP_LEN(b
) + 2, a
->f
| b
->f
);
132 /* --- Fix up the quotient too --- */
135 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
138 /* --- Perform the calculation --- */
140 gfx_div(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
);
142 /* --- Sort out the sign of the results --- *
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@.
149 q
->f
= (r
->f
| b
->f
) & MP_BURN
;
150 r
->f
= (r
->f
| b
->f
) & MP_BURN
;
152 /* --- Store the return values --- */
171 /* --- @gf_irreduciblep@ --- *
173 * Arguments: @mp *f@ = a polynomial
175 * Returns: Nonzero if the polynomial is irreducible; otherwise zero.
178 int gf_irreduciblep(mp
*f
)
186 else if (MP_LEN(f
) == 1) {
187 if (f
->v
[0] < 2) return (0);
188 if (f
->v
[0] < 4) return (1);
190 m
= (mp_bits(f
) - 1)/2;
194 v
= gf_add(v
, u
, MP_TWO
);
195 gf_gcd(&v
, 0, 0, v
, f
);
196 if (!MP_EQ(v
, MP_ONE
)) break;
204 /*----- Test rig ----------------------------------------------------------*/
208 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
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);
222 #define RIG(name, op) \
223 static int t##name(dstr *v) \
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); \
241 static int tsqr(dstr
*v
)
243 mp
*a
= *(mp
**)v
[0].buf
;
244 mp
*r
= *(mp
**)v
[1].buf
;
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);
254 static int tdiv(dstr
*v
)
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
;
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);
270 static int tirred(dstr
*v
)
272 mp
*a
= *(mp
**)v
[0].buf
;
273 int r
= *(int *)v
[1].buf
;
274 int c
= gf_irreduciblep(a
);
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
);
284 assert(mparena_count(MPARENA_GLOBAL
) == 0);
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 } },
298 int main(int argc
, char *argv
[])
301 test_run(argc
, argv
, tests
, SRCDIR
"/t/gf");
307 /*----- That's all, folks -------------------------------------------------*/