3 * $Id: gfx-sqr.c,v 1.1 2000/10/08 15:49:37 mdw Exp $
5 * Sqaring binary polynomials
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2000/10/08 15:49:37 mdw
34 * First glimmerings of binary polynomial arithmetic.
38 /*----- Header files ------------------------------------------------------*/
41 /* #include "gfx.h" */
42 #include "gfx-sqr-tab.h"
44 /*----- Static variables --------------------------------------------------*/
46 static uint16 tab[256] = GFX_SQRTAB;
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @gfx_sqr@ --- *
52 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
53 * @const mpw *av, *avl@ = argument vector base and limit
57 * Use: Performs squaring of binary polynomials.
60 void gfx_sqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
63 unsigned b = 0, bb = 0;
65 /* --- Simple stuff --- */
71 /* --- The main algorithm --- *
73 * Our method depends on the fact that, in a field of characteristic 2, we
74 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
75 * sufficient just to put a zero bit between each of the bits of the
76 * original argument. We use a precomputed table for this, and work on
77 * entire octets at a time. Life is more complicated because we've got to
78 * be careful of bizarre architectures which don't have words with a
79 * multiple of 8 bits in them.
84 /* --- Input buffering --- */
93 /* --- Do the work in the middle --- */
95 aa |= (mpd)(tab[U8(a)]) << bb;
100 /* --- Output buffering --- */
111 /* --- Flush the input buffer --- */
114 aa |= (mpd)(tab[U8(a)]) << bb;
130 /* --- Flush the output buffer --- */
143 /* --- Zero the rest of everything --- */
148 /*----- Test rig ----------------------------------------------------------*/
152 #include <mLib/alloc.h>
153 #include <mLib/dstr.h>
154 #include <mLib/quis.h>
155 #include <mLib/testrig.h>
157 #define ALLOC(v, vl, sz) do { \
159 mpw *_vv = xmalloc(MPWS(_sz)); \
160 mpw *_vvl = _vv + _sz; \
165 #define LOAD(v, vl, d) do { \
166 const dstr *_d = (d); \
168 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
169 mpx_loadb(_v, _vl, _d->buf, _d->len); \
174 #define MAX(x, y) ((x) > (y) ? (x) : (y))
176 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
181 fprintf(stderr, " %08lx", (unsigned long)*--vl);
185 static int vsqr(dstr *v)
194 ALLOC(d, dl, 2 * (al - a));
196 gfx_sqr(d, dl, a, al);
197 if (!mpx_ueq(d, dl, b, bl)) {
198 fprintf(stderr, "\n*** vsqr failed\n");
200 dumpmp("expected", b, bl);
201 dumpmp(" result", d, dl);
205 free(a); free(b); free(d);
209 static test_chunk defs[] = {
210 { "sqr", vsqr, { &type_hex, &type_hex, 0 } },
214 int main(int argc, char *argv[])
216 test_run(argc, argv, defs, SRCDIR"/tests/gfx");
222 /*----- That's all, folks -------------------------------------------------*/