General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / gf-arith.c
1 /* -*-c-*-
2 *
3 * $Id: gf-arith.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Basic arithmetic 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
30 /*----- Header files ------------------------------------------------------*/
31
32 #include "gf.h"
33
34 /*----- Macros ------------------------------------------------------------*/
35
36 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @gf_add@ --- *
41 *
42 * Arguments: @mp *d@ = destination
43 * @mp *a, *b@ = sources
44 *
45 * Returns: Result, @a@ added to @b@.
46 */
47
48 mp *gf_add(mp *d, mp *a, mp *b)
49 {
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;
53 MP_SHRINK(d);
54 return (d);
55 }
56
57 /* --- @gf_mul@ --- *
58 *
59 * Arguments: @mp *d@ = destination
60 * @mp *a, *b@ = sources
61 *
62 * Returns: Result, @a@ multiplied by @b@.
63 */
64
65 mp *gf_mul(mp *d, mp *a, mp *b)
66 {
67 a = MP_COPY(a);
68 b = MP_COPY(b);
69
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);
73 } else {
74 size_t m = MAX(MP_LEN(a), MP_LEN(b));
75 mpw *s;
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);
79 mpfree(d->a, s);
80 }
81
82 d->f = (a->f | b->f) & MP_BURN;
83 MP_SHRINK(d);
84 MP_DROP(a);
85 MP_DROP(b);
86 return (d);
87 }
88
89 /* --- @gf_sqr@ --- *
90 *
91 * Arguments: @mp *d@ = destination
92 * @mp *a@ = source
93 *
94 * Returns: Result, @a@ squared.
95 */
96
97 mp *gf_sqr(mp *d, mp *a)
98 {
99 MP_COPY(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;
103 MP_SHRINK(d);
104 MP_DROP(a);
105 return (d);
106 }
107
108 /* --- @gf_div@ --- *
109 *
110 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
111 * @mp *a, *b@ = sources
112 *
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@.
118 */
119
120 void gf_div(mp **qq, mp **rr, mp *a, mp *b)
121 {
122 mp *r = rr ? *rr : MP_NEW;
123 mp *q = qq ? *qq : MP_NEW;
124
125 /* --- Set the remainder up right --- */
126
127 b = MP_COPY(b);
128 a = MP_COPY(a);
129 if (r)
130 MP_DROP(r);
131 r = a;
132 MP_DEST(r, MP_LEN(b) + 2, a->f | b->f);
133
134 /* --- Fix up the quotient too --- */
135
136 r = MP_COPY(r);
137 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
138 MP_DROP(r);
139
140 /* --- Perform the calculation --- */
141
142 gfx_div(q->v, q->vl, r->v, r->vl, b->v, b->vl);
143
144 /* --- Sort out the sign of the results --- *
145 *
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@.
149 */
150
151 q->f = (r->f | b->f) & MP_BURN;
152 r->f = (r->f | b->f) & MP_BURN;
153
154 /* --- Store the return values --- */
155
156 MP_DROP(b);
157
158 if (!qq)
159 MP_DROP(q);
160 else {
161 MP_SHRINK(q);
162 *qq = q;
163 }
164
165 if (!rr)
166 MP_DROP(r);
167 else {
168 MP_SHRINK(r);
169 *rr = r;
170 }
171 }
172
173 /* --- @gf_irreduciblep@ --- *
174 *
175 * Arguments: @mp *f@ = a polynomial
176 *
177 * Returns: Nonzero if the polynomial is irreducible; otherwise zero.
178 */
179
180 int gf_irreduciblep(mp *f)
181 {
182 unsigned long m = mp_bits(f) - 1;
183 mp *u = MP_TWO;
184 mp *v = MP_NEW;
185
186 m /= 2;
187 while (m) {
188 u = gf_sqr(u, u);
189 gf_div(0, &u, u, f);
190 v = gf_add(v, u, MP_TWO);
191 gf_gcd(&v, 0, 0, v, f);
192 if (!MP_EQ(v, MP_ONE)) break;
193 m--;
194 }
195 MP_DROP(u);
196 MP_DROP(v);
197 return (!m);
198 }
199
200 /*----- Test rig ----------------------------------------------------------*/
201
202 #ifdef TEST_RIG
203
204 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
205 {
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);
212 fputc('\n', stderr);
213 return (0);
214 }
215 return (1);
216 }
217
218 #define RIG(name, op) \
219 static int t##name(dstr *v) \
220 { \
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); \
228 return (ok); \
229 }
230
231 RIG(add, gf_add)
232 RIG(mul, gf_mul)
233
234 #undef RIG
235
236 static int tsqr(dstr *v)
237 {
238 mp *a = *(mp **)v[0].buf;
239 mp *r = *(mp **)v[1].buf;
240 mp *c = MP_NEW;
241 int ok = 1;
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);
246 return (ok);
247 }
248
249 static int tdiv(dstr *v)
250 {
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;
256 int ok = 1;
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);
262 return (ok);
263 }
264
265 static int tirred(dstr *v)
266 {
267 mp *a = *(mp **)v[0].buf;
268 int r = *(int *)v[1].buf;
269 int c = gf_irreduciblep(a);
270 int ok = 1;
271 if (r != c) {
272 ok = 0;
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);
277 }
278 mp_drop(a);
279 assert(mparena_count(MPARENA_GLOBAL) == 0);
280 return (ok);
281 }
282
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 } },
289 { 0, 0, { 0 } },
290 };
291
292 int main(int argc, char *argv[])
293 {
294 sub_init();
295 test_run(argc, argv, tests, SRCDIR "/tests/gf");
296 return (0);
297 }
298
299 #endif
300
301 /*----- That's all, folks -------------------------------------------------*/