3 * Sqaring binary polynomials
5 * (c) 2000 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 #include "gfx-sqr-tab.h"
34 /*----- Static variables --------------------------------------------------*/
36 static const uint16 tab[256] = GFX_SQRTAB;
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @gfx_sqr@ --- *
42 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
43 * @const mpw *av, *avl@ = argument vector base and limit
47 * Use: Performs squaring of binary polynomials.
50 void gfx_sqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
53 unsigned b = 0, bb = 0;
55 /* --- Simple stuff --- */
61 /* --- The main algorithm --- *
63 * Our method depends on the fact that, in a field of characteristic 2, we
64 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
65 * sufficient just to put a zero bit between each of the bits of the
66 * original argument. We use a precomputed table for this, and work on
67 * entire octets at a time. Life is more complicated because we've got to
68 * be careful of bizarre architectures which don't have words with a
69 * multiple of 8 bits in them.
74 /* --- Input buffering --- */
83 /* --- Do the work in the middle --- */
85 aa |= (mpd)(tab[U8(a)]) << bb;
90 /* --- Output buffering --- */
101 /* --- Flush the input buffer --- */
104 aa |= (mpd)(tab[U8(a)]) << bb;
120 /* --- Flush the output buffer --- */
133 /* --- Zero the rest of everything --- */
138 /*----- Test rig ----------------------------------------------------------*/
142 #include <mLib/alloc.h>
143 #include <mLib/dstr.h>
144 #include <mLib/quis.h>
145 #include <mLib/testrig.h>
147 #define ALLOC(v, vl, sz) do { \
149 mpw *_vv = xmalloc(MPWS(_sz)); \
150 mpw *_vvl = _vv + _sz; \
155 #define LOAD(v, vl, d) do { \
156 const dstr *_d = (d); \
158 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
159 mpx_loadb(_v, _vl, _d->buf, _d->len); \
164 #define MAX(x, y) ((x) > (y) ? (x) : (y))
166 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
171 fprintf(stderr, " %08lx", (unsigned long)*--vl);
175 static int vsqr(dstr *v)
184 ALLOC(d, dl, 2 * (al - a));
186 gfx_sqr(d, dl, a, al);
187 if (!mpx_ueq(d, dl, b, bl)) {
188 fprintf(stderr, "\n*** vsqr failed\n");
190 dumpmp("expected", b, bl);
191 dumpmp(" result", d, dl);
195 xfree(a); xfree(b); xfree(d);
199 static test_chunk defs[] = {
200 { "sqr", vsqr, { &type_hex, &type_hex, 0 } },
204 int main(int argc, char *argv[])
206 test_run(argc, argv, defs, SRCDIR"/t/gfx");
212 /*----- That's all, folks -------------------------------------------------*/