ct.c, ct.h: New constant-time operations.
[u/mdw/catacomb] / gf-arith.c
CommitLineData
ceb3f0c0 1/* -*-c-*-
2 *
f4535c64 3 * $Id$
ceb3f0c0 4 *
5 * Basic arithmetic on binary polynomials
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
ceb3f0c0 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.
45c0fd36 18 *
ceb3f0c0 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.
45c0fd36 23 *
ceb3f0c0 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
ceb3f0c0 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
48mp *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
65mp *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);
432c4e18 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);
ceb3f0c0 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
97mp *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
120void 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
432c4e18 173/* --- @gf_irreduciblep@ --- *
174 *
175 * Arguments: @mp *f@ = a polynomial
176 *
177 * Returns: Nonzero if the polynomial is irreducible; otherwise zero.
178 */
179
180int gf_irreduciblep(mp *f)
181{
00f096fc 182 unsigned long m;
432c4e18 183 mp *u = MP_TWO;
184 mp *v = MP_NEW;
185
00f096fc
MW
186 if (MP_ZEROP(f))
187 return (0);
188 else if (MP_LEN(f) == 1) {
189 if (f->v[0] < 2) return (0);
190 if (f->v[0] < 4) return (1);
191 }
192 m = (mp_bits(f) - 1)/2;
432c4e18 193 while (m) {
194 u = gf_sqr(u, u);
195 gf_div(0, &u, u, f);
196 v = gf_add(v, u, MP_TWO);
197 gf_gcd(&v, 0, 0, v, f);
198 if (!MP_EQ(v, MP_ONE)) break;
199 m--;
200 }
201 MP_DROP(u);
202 MP_DROP(v);
203 return (!m);
204}
205
ceb3f0c0 206/*----- Test rig ----------------------------------------------------------*/
207
208#ifdef TEST_RIG
209
210static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
211{
212 if (!MP_EQ(expect, result)) {
213 fprintf(stderr, "\n*** %s failed", op);
45c0fd36
MW
214 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
215 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 16);
ceb3f0c0 216 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 16);
217 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 16);
218 fputc('\n', stderr);
219 return (0);
220 }
221 return (1);
222}
223
224#define RIG(name, op) \
225 static int t##name(dstr *v) \
226 { \
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); \
234 return (ok); \
235 }
236
237RIG(add, gf_add)
238RIG(mul, gf_mul)
f4535c64 239RIG(exp, gf_exp)
ceb3f0c0 240
241#undef RIG
242
243static int tsqr(dstr *v)
244{
245 mp *a = *(mp **)v[0].buf;
246 mp *r = *(mp **)v[1].buf;
247 mp *c = MP_NEW;
248 int ok = 1;
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);
253 return (ok);
254}
255
256static int tdiv(dstr *v)
257{
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;
263 int ok = 1;
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);
269 return (ok);
270}
271
432c4e18 272static int tirred(dstr *v)
273{
274 mp *a = *(mp **)v[0].buf;
275 int r = *(int *)v[1].buf;
276 int c = gf_irreduciblep(a);
277 int ok = 1;
278 if (r != c) {
279 ok = 0;
280 fprintf(stderr, "\n*** irred failed");
45c0fd36
MW
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);
432c4e18 284 }
285 mp_drop(a);
286 assert(mparena_count(MPARENA_GLOBAL) == 0);
287 return (ok);
288}
289
ceb3f0c0 290static 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 } },
f4535c64 295 { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } },
432c4e18 296 { "irred", tirred, { &type_mp, &type_int, 0 } },
ceb3f0c0 297 { 0, 0, { 0 } },
298};
299
300int main(int argc, char *argv[])
301{
302 sub_init();
303 test_run(argc, argv, tests, SRCDIR "/tests/gf");
304 return (0);
305}
306
307#endif
308
309/*----- That's all, folks -------------------------------------------------*/