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