ceb3f0c0 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: gf-arith.c,v 1.1.2.1 2004/03/21 22:39:46 mdw Exp $ |
4 | * |
5 | * Basic arithmetic on binary polynomials |
6 | * |
7 | * (c) 2004 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: gf-arith.c,v $ |
33 | * Revision 1.1.2.1 2004/03/21 22:39:46 mdw |
34 | * Elliptic curves on binary fields work. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include "gf.h" |
41 | |
42 | /*----- Macros ------------------------------------------------------------*/ |
43 | |
44 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
45 | |
46 | /*----- Main code ---------------------------------------------------------*/ |
47 | |
48 | /* --- @gf_add@ --- * |
49 | * |
50 | * Arguments: @mp *d@ = destination |
51 | * @mp *a, *b@ = sources |
52 | * |
53 | * Returns: Result, @a@ added to @b@. |
54 | */ |
55 | |
56 | mp *gf_add(mp *d, mp *a, mp *b) |
57 | { |
58 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & MP_BURN); |
59 | gfx_add(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
60 | d->f = (a->f | b->f) & MP_BURN; |
61 | MP_SHRINK(d); |
62 | return (d); |
63 | } |
64 | |
65 | /* --- @gf_mul@ --- * |
66 | * |
67 | * Arguments: @mp *d@ = destination |
68 | * @mp *a, *b@ = sources |
69 | * |
70 | * Returns: Result, @a@ multiplied by @b@. |
71 | */ |
72 | |
73 | mp *gf_mul(mp *d, mp *a, mp *b) |
74 | { |
75 | a = MP_COPY(a); |
76 | b = MP_COPY(b); |
77 | |
78 | if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= GFK_THRESH) { |
79 | MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF); |
80 | gfx_mul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
81 | } else { |
82 | size_t m = MAX(MP_LEN(a), MP_LEN(b)); |
83 | mpw *s; |
84 | MP_DEST(d, 2 * m, a->f | b->f | MP_UNDEF); |
85 | s = mpalloc(d->a, 2 * m); |
86 | gfx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 2 * m); |
87 | mpfree(d->a, s); |
88 | } |
89 | |
90 | d->f = (a->f | b->f) & MP_BURN; |
91 | MP_SHRINK(d); |
92 | MP_DROP(a); |
93 | MP_DROP(b); |
94 | return (d); |
95 | } |
96 | |
97 | /* --- @gf_sqr@ --- * |
98 | * |
99 | * Arguments: @mp *d@ = destination |
100 | * @mp *a@ = source |
101 | * |
102 | * Returns: Result, @a@ squared. |
103 | */ |
104 | |
105 | mp *gf_sqr(mp *d, mp *a) |
106 | { |
107 | MP_COPY(a); |
108 | MP_DEST(d, 2 * MP_LEN(a), a->f & MP_BURN); |
109 | gfx_sqr(d->v, d->vl, a->v, a->vl); |
110 | d->f = a->f & MP_BURN; |
111 | MP_SHRINK(d); |
112 | MP_DROP(a); |
113 | return (d); |
114 | } |
115 | |
116 | /* --- @gf_div@ --- * |
117 | * |
118 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
119 | * @mp *a, *b@ = sources |
120 | * |
121 | * Use: Calculates the quotient and remainder when @a@ is divided by |
122 | * @b@. The destinations @*qq@ and @*rr@ must be distinct. |
123 | * Either of @qq@ or @rr@ may be null to indicate that the |
124 | * result is irrelevant. (Discarding both results is silly.) |
125 | * There is a performance advantage if @a == *rr@. |
126 | */ |
127 | |
128 | void gf_div(mp **qq, mp **rr, mp *a, mp *b) |
129 | { |
130 | mp *r = rr ? *rr : MP_NEW; |
131 | mp *q = qq ? *qq : MP_NEW; |
132 | |
133 | /* --- Set the remainder up right --- */ |
134 | |
135 | b = MP_COPY(b); |
136 | a = MP_COPY(a); |
137 | if (r) |
138 | MP_DROP(r); |
139 | r = a; |
140 | MP_DEST(r, MP_LEN(b) + 2, a->f | b->f); |
141 | |
142 | /* --- Fix up the quotient too --- */ |
143 | |
144 | r = MP_COPY(r); |
145 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); |
146 | MP_DROP(r); |
147 | |
148 | /* --- Perform the calculation --- */ |
149 | |
150 | gfx_div(q->v, q->vl, r->v, r->vl, b->v, b->vl); |
151 | |
152 | /* --- Sort out the sign of the results --- * |
153 | * |
154 | * If the signs of the arguments differ, and the remainder is nonzero, I |
155 | * must add one to the absolute value of the quotient and subtract the |
156 | * remainder from @b@. |
157 | */ |
158 | |
159 | q->f = (r->f | b->f) & MP_BURN; |
160 | r->f = (r->f | b->f) & MP_BURN; |
161 | |
162 | /* --- Store the return values --- */ |
163 | |
164 | MP_DROP(b); |
165 | |
166 | if (!qq) |
167 | MP_DROP(q); |
168 | else { |
169 | MP_SHRINK(q); |
170 | *qq = q; |
171 | } |
172 | |
173 | if (!rr) |
174 | MP_DROP(r); |
175 | else { |
176 | MP_SHRINK(r); |
177 | *rr = r; |
178 | } |
179 | } |
180 | |
181 | /*----- Test rig ----------------------------------------------------------*/ |
182 | |
183 | #ifdef TEST_RIG |
184 | |
185 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
186 | { |
187 | if (!MP_EQ(expect, result)) { |
188 | fprintf(stderr, "\n*** %s failed", op); |
189 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); |
190 | fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 16); |
191 | fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 16); |
192 | fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 16); |
193 | fputc('\n', stderr); |
194 | return (0); |
195 | } |
196 | return (1); |
197 | } |
198 | |
199 | #define RIG(name, op) \ |
200 | static int t##name(dstr *v) \ |
201 | { \ |
202 | mp *a = *(mp **)v[0].buf; \ |
203 | mp *b = *(mp **)v[1].buf; \ |
204 | mp *r = *(mp **)v[2].buf; \ |
205 | mp *c = op(MP_NEW, a, b); \ |
206 | int ok = verify(#name, r, c, a, b); \ |
207 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \ |
208 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
209 | return (ok); \ |
210 | } |
211 | |
212 | RIG(add, gf_add) |
213 | RIG(mul, gf_mul) |
214 | |
215 | #undef RIG |
216 | |
217 | static int tsqr(dstr *v) |
218 | { |
219 | mp *a = *(mp **)v[0].buf; |
220 | mp *r = *(mp **)v[1].buf; |
221 | mp *c = MP_NEW; |
222 | int ok = 1; |
223 | c = gf_sqr(MP_NEW, a); |
224 | ok &= verify("sqr", r, c, a, MP_ZERO); |
225 | mp_drop(a); mp_drop(r); mp_drop(c); |
226 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
227 | return (ok); |
228 | } |
229 | |
230 | static int tdiv(dstr *v) |
231 | { |
232 | mp *a = *(mp **)v[0].buf; |
233 | mp *b = *(mp **)v[1].buf; |
234 | mp *q = *(mp **)v[2].buf; |
235 | mp *r = *(mp **)v[3].buf; |
236 | mp *c = MP_NEW, *d = MP_NEW; |
237 | int ok = 1; |
238 | gf_div(&c, &d, a, b); |
239 | ok &= verify("div(quotient)", q, c, a, b); |
240 | ok &= verify("div(remainder)", r, d, a, b); |
241 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q); |
242 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
243 | return (ok); |
244 | } |
245 | |
246 | static test_chunk tests[] = { |
247 | { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, |
248 | { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, |
249 | { "sqr", tsqr, { &type_mp, &type_mp, 0 } }, |
250 | { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
251 | { 0, 0, { 0 } }, |
252 | }; |
253 | |
254 | int main(int argc, char *argv[]) |
255 | { |
256 | sub_init(); |
257 | test_run(argc, argv, tests, SRCDIR "/tests/gf"); |
258 | return (0); |
259 | } |
260 | |
261 | #endif |
262 | |
263 | /*----- That's all, folks -------------------------------------------------*/ |