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 ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- @gfx_sqr@ --- *
38 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
39 * @const mpw *av, *avl@ = argument vector base and limit
43 * Use: Performs squaring of binary polynomials.
46 void gfx_sqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
57 # error "unsupported limb width: extend `gfx-sqr.c'"
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
84 /* ..., 7, 6, 5, 4, 3, 2, 1, 0 */
85 SWIZZLE_EXCH(a, 0, 1); /* ..., 7, 6, 5, 4, 3, 2, 0, 1 */
86 SWIZZLE_EXCH(a, 0, 2); /* ..., 7, 6, 5, 4, 3, 1, 0, 2 */
87 SWIZZLE_EXCH(a, 0, 3); /* ..., 7, 6, 5, 4, 2, 1, 0, 3 */
89 SWIZZLE_EXCH(a, 0, 4); /* ..., 7, 6, 5, 3, 2, 1, 0, 4 */
92 SWIZZLE_EXCH(a, 0, 5); /* ..., 7, 6, 4, 3, 2, 1, 0, 5 */
95 SWIZZLE_EXCH(a, 0, 6); /* ..., 7, 5, 4, 3, 2, 1, 0, 6 */
98 # error "unsupported limb width: extend `gfx-sqr.c'"
101 /* Write out the low half, which consists of the low-order bits in
102 * even-numbered positions.
104 *dv++ = MPW(a&IXMASK(0)); if (dv >= dvl) break;
106 /* Write the high half. This is trickier, since, in general, the bits
107 * are split across even-numbered bits in the high part of the wider
108 * register and odd-numbered bits in the low part. Alas, we can't
109 * resolve this mess without special cases because shifts are broken in
112 t = ((a >> 1)&IXMASK(0)) << (REGWD - MPW_BITS);
114 t |= (a&IXMASK(0)) >> MPW_BITS;
116 *dv++ = MPW(t); if (dv >= dvl) break;
119 /* --- Zero the rest of everything --- */
126 /*----- Test rig ----------------------------------------------------------*/
130 #include <mLib/alloc.h>
131 #include <mLib/dstr.h>
132 #include <mLib/quis.h>
133 #include <mLib/testrig.h>
135 #define ALLOC(v, vl, sz) do { \
137 mpw *_vv = xmalloc(MPWS(_sz)); \
138 mpw *_vvl = _vv + _sz; \
143 #define LOAD(v, vl, d) do { \
144 const dstr *_d = (d); \
146 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
147 mpx_loadb(_v, _vl, _d->buf, _d->len); \
152 #define MAX(x, y) ((x) > (y) ? (x) : (y))
154 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
159 fprintf(stderr, " %08lx", (unsigned long)*--vl);
163 static int vsqr(dstr *v)
172 ALLOC(d, dl, 2 * (al - a));
174 gfx_sqr(d, dl, a, al);
175 if (!mpx_ueq(d, dl, b, bl)) {
176 fprintf(stderr, "\n*** vsqr failed\n");
178 dumpmp("expected", b, bl);
179 dumpmp(" result", d, dl);
183 xfree(a); xfree(b); xfree(d);
187 static test_chunk defs[] = {
188 { "sqr", vsqr, { &type_hex, &type_hex, 0 } },
192 int main(int argc, char *argv[])
194 test_run(argc, argv, defs, SRCDIR"/t/gfx");
200 /*----- That's all, folks -------------------------------------------------*/