60e1b3c6e8bfbee0278820870c91e82bc7d531b1
[u/mdw/catacomb] / gfx-sqr.c
1 /* -*-c-*-
2 *
3 * $Id: gfx-sqr.c,v 1.3 2004/04/02 01:03:49 mdw Exp $
4 *
5 * Sqaring binary polynomials
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gfx-sqr.c,v $
33 * Revision 1.3 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.2 2004/03/21 22:52:06 mdw
37 * Merge and close elliptic curve branch.
38 *
39 * Revision 1.1.4.1 2004/03/21 22:39:46 mdw
40 * Elliptic curves on binary fields work.
41 *
42 * Revision 1.1 2000/10/08 15:49:37 mdw
43 * First glimmerings of binary polynomial arithmetic.
44 *
45 */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include "mpx.h"
50 #include "gfx.h"
51 #include "gfx-sqr-tab.h"
52
53 /*----- Static variables --------------------------------------------------*/
54
55 static const uint16 tab[256] = GFX_SQRTAB;
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 /* --- @gfx_sqr@ --- *
60 *
61 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
62 * @const mpw *av, *avl@ = argument vector base and limit
63 *
64 * Returns: ---
65 *
66 * Use: Performs squaring of binary polynomials.
67 */
68
69 void gfx_sqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
70 {
71 mpd a = 0, aa = 0;
72 unsigned b = 0, bb = 0;
73
74 /* --- Simple stuff --- */
75
76 if (dv >= dvl)
77 return;
78 MPX_SHRINK(av, avl);
79
80 /* --- The main algorithm --- *
81 *
82 * Our method depends on the fact that, in a field of characteristic 2, we
83 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
84 * sufficient just to put a zero bit between each of the bits of the
85 * original argument. We use a precomputed table for this, and work on
86 * entire octets at a time. Life is more complicated because we've got to
87 * be careful of bizarre architectures which don't have words with a
88 * multiple of 8 bits in them.
89 */
90
91 for (;;) {
92
93 /* --- Input buffering --- */
94
95 if (b < 8) {
96 if (av >= avl)
97 break;
98 a |= *av++ << b;
99 b += MPW_BITS;
100 }
101
102 /* --- Do the work in the middle --- */
103
104 aa |= (mpd)(tab[U8(a)]) << bb;
105 bb += 16;
106 a >>= 8;
107 b -= 8;
108
109 /* --- Output buffering --- */
110
111 if (bb >= MPW_BITS) {
112 *dv++ = MPW(aa);
113 if (dv >= dvl)
114 return;
115 aa >>= MPW_BITS;
116 bb -= MPW_BITS;
117 }
118 }
119
120 /* --- Flush the input buffer --- */
121
122 if (b) for (;;) {
123 aa |= (mpd)(tab[U8(a)]) << bb;
124 bb += 16;
125 if (bb > MPW_BITS) {
126 *dv++ = MPW(aa);
127 if (dv >= dvl)
128 return;
129 aa >>= MPW_BITS;
130 bb -= MPW_BITS;
131 }
132 a >>= 8;
133 if (b <= 8)
134 break;
135 else
136 b -= 8;
137 }
138
139 /* --- Flush the output buffer --- */
140
141 if (bb) for (;;) {
142 *dv++ = MPW(aa);
143 if (dv >= dvl)
144 return;
145 aa >>= MPW_BITS;
146 if (bb <= MPW_BITS)
147 break;
148 else
149 bb -= MPW_BITS;
150 }
151
152 /* --- Zero the rest of everything --- */
153
154 MPX_ZERO(dv, dvl);
155 }
156
157 /*----- Test rig ----------------------------------------------------------*/
158
159 #ifdef TEST_RIG
160
161 #include <mLib/alloc.h>
162 #include <mLib/dstr.h>
163 #include <mLib/quis.h>
164 #include <mLib/testrig.h>
165
166 #define ALLOC(v, vl, sz) do { \
167 size_t _sz = (sz); \
168 mpw *_vv = xmalloc(MPWS(_sz)); \
169 mpw *_vvl = _vv + _sz; \
170 (v) = _vv; \
171 (vl) = _vvl; \
172 } while (0)
173
174 #define LOAD(v, vl, d) do { \
175 const dstr *_d = (d); \
176 mpw *_v, *_vl; \
177 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
178 mpx_loadb(_v, _vl, _d->buf, _d->len); \
179 (v) = _v; \
180 (vl) = _vl; \
181 } while (0)
182
183 #define MAX(x, y) ((x) > (y) ? (x) : (y))
184
185 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
186 {
187 fputs(msg, stderr);
188 MPX_SHRINK(v, vl);
189 while (v < vl)
190 fprintf(stderr, " %08lx", (unsigned long)*--vl);
191 fputc('\n', stderr);
192 }
193
194 static int vsqr(dstr *v)
195 {
196 mpw *a, *al;
197 mpw *b, *bl;
198 mpw *d, *dl;
199 int ok = 1;
200
201 LOAD(a, al, &v[0]);
202 LOAD(b, bl, &v[1]);
203 ALLOC(d, dl, 2 * (al - a));
204
205 gfx_sqr(d, dl, a, al);
206 if (!mpx_ueq(d, dl, b, bl)) {
207 fprintf(stderr, "\n*** vsqr failed\n");
208 dumpmp(" a", a, al);
209 dumpmp("expected", b, bl);
210 dumpmp(" result", d, dl);
211 ok = 0;
212 }
213
214 free(a); free(b); free(d);
215 return (ok);
216 }
217
218 static test_chunk defs[] = {
219 { "sqr", vsqr, { &type_hex, &type_hex, 0 } },
220 { 0, 0, { 0 } }
221 };
222
223 int main(int argc, char *argv[])
224 {
225 test_run(argc, argv, defs, SRCDIR"/tests/gfx");
226 return (0);
227 }
228
229 #endif
230
231 /*----- That's all, folks -------------------------------------------------*/