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 ------------------------------------------------------*/
33 /*----- Static variables --------------------------------------------------*/
35 extern const uint16 gfx_sqrtab
[256];
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @gfx_sqr@ --- *
41 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
42 * @const mpw *av, *avl@ = argument vector base and limit
46 * Use: Performs squaring of binary polynomials.
49 void gfx_sqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
52 unsigned b
= 0, bb
= 0;
54 /* --- Simple stuff --- */
60 /* --- The main algorithm --- *
62 * Our method depends on the fact that, in a field of characteristic 2, we
63 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
64 * sufficient just to put a zero bit between each of the bits of the
65 * original argument. We use a precomputed table for this, and work on
66 * entire octets at a time. Life is more complicated because we've got to
67 * be careful of bizarre architectures which don't have words with a
68 * multiple of 8 bits in them.
73 /* --- Input buffering --- */
82 /* --- Do the work in the middle --- */
84 aa
|= (mpd
)(gfx_sqrtab
[U8(a
)]) << bb
;
89 /* --- Output buffering --- */
100 /* --- Flush the input buffer --- */
103 aa
|= (mpd
)(gfx_sqrtab
[U8(a
)]) << bb
;
119 /* --- Flush the output buffer --- */
132 /* --- Zero the rest of everything --- */
137 /*----- Test rig ----------------------------------------------------------*/
141 #include <mLib/alloc.h>
142 #include <mLib/dstr.h>
143 #include <mLib/quis.h>
144 #include <mLib/testrig.h>
146 #define ALLOC(v, vl, sz) do { \
148 mpw *_vv = xmalloc(MPWS(_sz)); \
149 mpw *_vvl = _vv + _sz; \
154 #define LOAD(v, vl, d) do { \
155 const dstr *_d = (d); \
157 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
158 mpx_loadb(_v, _vl, _d->buf, _d->len); \
163 #define MAX(x, y) ((x) > (y) ? (x) : (y))
165 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
170 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
174 static int vsqr(dstr
*v
)
183 ALLOC(d
, dl
, 2 * (al
- a
));
185 gfx_sqr(d
, dl
, a
, al
);
186 if (!mpx_ueq(d
, dl
, b
, bl
)) {
187 fprintf(stderr
, "\n*** vsqr failed\n");
189 dumpmp("expected", b
, bl
);
190 dumpmp(" result", d
, dl
);
194 xfree(a
); xfree(b
); xfree(d
);
198 static test_chunk defs
[] = {
199 { "sqr", vsqr
, { &type_hex
, &type_hex
, 0 } },
203 int main(int argc
, char *argv
[])
205 test_run(argc
, argv
, defs
, SRCDIR
"/t/gfx");
211 /*----- That's all, folks -------------------------------------------------*/