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