Miscellaneous constification.
[u/mdw/catacomb] / f-binpoly.c
1 /* -*-c-*-
2 *
3 * $Id: f-binpoly.c,v 1.8 2004/04/02 01:03:49 mdw Exp $
4 *
5 * Binary fields with polynomial basis representation
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: f-binpoly.c,v $
33 * Revision 1.8 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.7 2004/04/01 21:28:41 mdw
37 * Normal basis support (translates to poly basis internally). Rewrite
38 * EC and prime group table generators in awk, so that they can reuse data
39 * for repeated constants.
40 *
41 * Revision 1.6 2004/04/01 12:50:09 mdw
42 * Add cyclic group abstraction, with test code. Separate off exponentation
43 * functions for better static linking. Fix a buttload of bugs on the way.
44 * Generally ensure that negative exponents do inversion correctly. Add
45 * table of standard prime-field subgroups. (Binary field subgroups are
46 * currently unimplemented but easy to add if anyone ever finds a good one.)
47 *
48 * Revision 1.5 2004/03/27 17:54:11 mdw
49 * Standard curves and curve checking.
50 *
51 * Revision 1.4 2004/03/23 15:19:32 mdw
52 * Test elliptic curves more thoroughly.
53 *
54 * Revision 1.3 2004/03/23 12:08:26 mdw
55 * Random field-element selection.
56 *
57 * Revision 1.2 2004/03/21 22:52:06 mdw
58 * Merge and close elliptic curve branch.
59 *
60 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
61 * Elliptic curves on binary fields work.
62 *
63 */
64
65 /*----- Header files ------------------------------------------------------*/
66
67 #include <mLib/sub.h>
68
69 #include "field.h"
70 #include "gf.h"
71 #include "gfreduce.h"
72 #include "mprand.h"
73 #include "gfn.h"
74
75 /*----- Polynomial basis --------------------------------------------------*/
76
77 typedef struct fctx {
78 field f;
79 gfreduce r;
80 } fctx;
81
82 /* --- Field operations --- */
83
84 static void fdestroy(field *ff)
85 { fctx *f = (fctx *)ff; gfreduce_destroy(&f->r); DESTROY(f); }
86
87 static mp *frand(field *f, mp *d, grand *r)
88 { return (mprand(d, f->nbits, r, 0)); }
89
90 static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
91
92 static mp *fadd(field *ff, mp *d, mp *x, mp *y) { return (gf_add(d, x, y)); }
93
94 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
95 fctx *f = (fctx *)ff; d = gf_mul(d, x, y);
96 return (gfreduce_do(&f->r, d, d));
97 }
98
99 static mp *fsqr(field *ff, mp *d, mp *x) {
100 fctx *f = (fctx *)ff; d = gf_sqr(d, x);
101 return (gfreduce_do(&f->r, d, d));
102 }
103
104 static mp *finv(field *ff, mp *d, mp *x)
105 { fctx *f = (fctx *)ff; gf_gcd(0, 0, &d, f->r.p, x); return (d); }
106
107 static mp *freduce(field *ff, mp *d, mp *x)
108 { fctx *f = (fctx *)ff; return (gfreduce_do(&f->r, d, x)); }
109
110 static mp *fsqrt(field *ff, mp *d, mp *x)
111 { fctx *f = (fctx *)ff; return (gfreduce_sqrt(&f->r, d, x)); }
112
113 static mp *fquadsolve(field *ff, mp *d, mp *x)
114 { fctx *f = (fctx *)ff; return (gfreduce_quadsolve(&f->r, d, x)); }
115
116 /* --- Field operations table --- */
117
118 static const field_ops fops = {
119 FTY_BINARY, "binpoly",
120 fdestroy, frand, field_stdsamep,
121 freduce, field_id,
122 fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt,
123 fquadsolve,
124 0, 0, 0, 0
125 };
126
127 /* --- @field_binpoly@ --- *
128 *
129 * Arguments: @mp *p@ = the reduction polynomial
130 *
131 * Returns: A pointer to the field.
132 *
133 * Use: Creates a field structure for a binary field mod @p@.
134 */
135
136 field *field_binpoly(mp *p)
137 {
138 fctx *f = CREATE(fctx);
139 f->f.ops = &fops;
140 f->f.zero = MP_ZERO;
141 f->f.one = MP_ONE;
142 f->f.nbits = mp_bits(p) - 1;
143 f->f.noctets = (f->f.nbits + 7) >> 3;
144 gfreduce_create(&f->r, p);
145 f->f.m = f->r.p;
146 return (&f->f);
147 }
148
149 /*----- Normal basis ------------------------------------------------------*/
150
151 typedef struct fnctx {
152 fctx f;
153 gfn ntop, pton;
154 } fnctx;
155
156 /* --- Field operations --- */
157
158 static void fndestroy(field *ff) {
159 fnctx *f = (fnctx *)ff; gfreduce_destroy(&f->f.r);
160 gfn_destroy(&f->ntop); gfn_destroy(&f->pton);
161 DESTROY(f);
162 }
163
164 static int fnsamep(field *ff, field *gg) {
165 fnctx *f = (fnctx *)ff, *g = (fnctx *)gg;
166 return (MP_EQ(f->ntop.r[0], g->ntop.r[0]) && field_stdsamep(ff, gg));
167 }
168
169 static mp *fnin(field *ff, mp *d, mp *x)
170 { fnctx *f = (fnctx *)ff; return (gfn_transform(&f->ntop, d, x)); }
171
172 static mp *fnout(field *ff, mp *d, mp *x)
173 { fnctx *f = (fnctx *)ff; return (gfn_transform(&f->pton, d, x)); }
174
175 /* --- Field operations table --- */
176
177 static const field_ops fnops = {
178 FTY_BINARY, "binnorm",
179 fndestroy, frand, fnsamep,
180 fnin, fnout,
181 fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt,
182 fquadsolve,
183 0, 0, 0, 0
184 };
185
186 /* --- @field_binnorm@ --- *
187 *
188 * Arguments: @mp *p@ = the reduction polynomial
189 * @mp *beta@ = representation of normal point
190 *
191 * Returns: A pointer to the field.
192 *
193 * Use: Creates a field structure for a binary field mod @p@ which
194 * uses a normal basis representation externally. Computations
195 * are still done on a polynomial-basis representation.
196 */
197
198 field *field_binnorm(mp *p, mp *beta)
199 {
200 fnctx *f = CREATE(fnctx);
201 f->f.f.ops = &fnops;
202 f->f.f.zero = MP_ZERO;
203 f->f.f.one = MP_ONE;
204 f->f.f.nbits = mp_bits(p) - 1;
205 f->f.f.noctets = (f->f.f.nbits + 7) >> 3;
206 gfreduce_create(&f->f.r, p);
207 f->f.f.m = f->f.r.p;
208 gfn_create(p, beta, &f->ntop, &f->pton);
209 return (&f->f.f);
210 }
211
212 /*----- That's all, folks -------------------------------------------------*/