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