3 * $Id: gfx-sqr.c,v 1.1.4.1 2004/03/21 22:39:46 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.4.1 2004/03/21 22:39:46 mdw
34 * Elliptic curves on binary fields work.
36 * Revision 1.1 2000/10/08 15:49:37 mdw
37 * First glimmerings of binary polynomial arithmetic.
41 /*----- Header files ------------------------------------------------------*/
45 #include "gfx-sqr-tab.h"
47 /*----- Static variables --------------------------------------------------*/
49 static uint16 tab[256] = GFX_SQRTAB;
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @gfx_sqr@ --- *
55 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
56 * @const mpw *av, *avl@ = argument vector base and limit
60 * Use: Performs squaring of binary polynomials.
63 void gfx_sqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
66 unsigned b = 0, bb = 0;
68 /* --- Simple stuff --- */
74 /* --- The main algorithm --- *
76 * Our method depends on the fact that, in a field of characteristic 2, we
77 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
78 * sufficient just to put a zero bit between each of the bits of the
79 * original argument. We use a precomputed table for this, and work on
80 * entire octets at a time. Life is more complicated because we've got to
81 * be careful of bizarre architectures which don't have words with a
82 * multiple of 8 bits in them.
87 /* --- Input buffering --- */
96 /* --- Do the work in the middle --- */
98 aa |= (mpd)(tab[U8(a)]) << bb;
103 /* --- Output buffering --- */
105 if (bb >= MPW_BITS) {
114 /* --- Flush the input buffer --- */
117 aa |= (mpd)(tab[U8(a)]) << bb;
133 /* --- Flush the output buffer --- */
146 /* --- Zero the rest of everything --- */
151 /*----- Test rig ----------------------------------------------------------*/
155 #include <mLib/alloc.h>
156 #include <mLib/dstr.h>
157 #include <mLib/quis.h>
158 #include <mLib/testrig.h>
160 #define ALLOC(v, vl, sz) do { \
162 mpw *_vv = xmalloc(MPWS(_sz)); \
163 mpw *_vvl = _vv + _sz; \
168 #define LOAD(v, vl, d) do { \
169 const dstr *_d = (d); \
171 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
172 mpx_loadb(_v, _vl, _d->buf, _d->len); \
177 #define MAX(x, y) ((x) > (y) ? (x) : (y))
179 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
184 fprintf(stderr, " %08lx", (unsigned long)*--vl);
188 static int vsqr(dstr *v)
197 ALLOC(d, dl, 2 * (al - a));
199 gfx_sqr(d, dl, a, al);
200 if (!mpx_ueq(d, dl, b, bl)) {
201 fprintf(stderr, "\n*** vsqr failed\n");
203 dumpmp("expected", b, bl);
204 dumpmp(" result", d, dl);
208 free(a); free(b); free(d);
212 static test_chunk defs[] = {
213 { "sqr", vsqr, { &type_hex, &type_hex, 0 } },
217 int main(int argc, char *argv[])
219 test_run(argc, argv, defs, SRCDIR"/tests/gfx");
225 /*----- That's all, folks -------------------------------------------------*/