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