3 * $Id: gfx-sqr.c,v 1.2 2004/03/21 22:52:06 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.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.1.4.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
39 * Revision 1.1 2000/10/08 15:49:37 mdw
40 * First glimmerings of binary polynomial arithmetic.
44 /*----- Header files ------------------------------------------------------*/
48 #include "gfx-sqr-tab.h"
50 /*----- Static variables --------------------------------------------------*/
52 static uint16 tab
[256] = GFX_SQRTAB
;
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @gfx_sqr@ --- *
58 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
59 * @const mpw *av, *avl@ = argument vector base and limit
63 * Use: Performs squaring of binary polynomials.
66 void gfx_sqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
69 unsigned b
= 0, bb
= 0;
71 /* --- Simple stuff --- */
77 /* --- The main algorithm --- *
79 * Our method depends on the fact that, in a field of characteristic 2, we
80 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
81 * sufficient just to put a zero bit between each of the bits of the
82 * original argument. We use a precomputed table for this, and work on
83 * entire octets at a time. Life is more complicated because we've got to
84 * be careful of bizarre architectures which don't have words with a
85 * multiple of 8 bits in them.
90 /* --- Input buffering --- */
99 /* --- Do the work in the middle --- */
101 aa
|= (mpd
)(tab
[U8(a
)]) << bb
;
106 /* --- Output buffering --- */
108 if (bb
>= MPW_BITS
) {
117 /* --- Flush the input buffer --- */
120 aa
|= (mpd
)(tab
[U8(a
)]) << bb
;
136 /* --- Flush the output buffer --- */
149 /* --- Zero the rest of everything --- */
154 /*----- Test rig ----------------------------------------------------------*/
158 #include <mLib/alloc.h>
159 #include <mLib/dstr.h>
160 #include <mLib/quis.h>
161 #include <mLib/testrig.h>
163 #define ALLOC(v, vl, sz) do { \
165 mpw *_vv = xmalloc(MPWS(_sz)); \
166 mpw *_vvl = _vv + _sz; \
171 #define LOAD(v, vl, d) do { \
172 const dstr *_d = (d); \
174 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
175 mpx_loadb(_v, _vl, _d->buf, _d->len); \
180 #define MAX(x, y) ((x) > (y) ? (x) : (y))
182 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
187 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
191 static int vsqr(dstr
*v
)
200 ALLOC(d
, dl
, 2 * (al
- a
));
202 gfx_sqr(d
, dl
, a
, al
);
203 if (!mpx_ueq(d
, dl
, b
, bl
)) {
204 fprintf(stderr
, "\n*** vsqr failed\n");
206 dumpmp("expected", b
, bl
);
207 dumpmp(" result", d
, dl
);
211 free(a
); free(b
); free(d
);
215 static test_chunk defs
[] = {
216 { "sqr", vsqr
, { &type_hex
, &type_hex
, 0 } },
220 int main(int argc
, char *argv
[])
222 test_run(argc
, argv
, defs
, SRCDIR
"/tests/gfx");
228 /*----- That's all, folks -------------------------------------------------*/