Make tables of standard encryption schemes etc.
[u/mdw/catacomb] / f-binpoly.c
CommitLineData
ceb3f0c0 1/* -*-c-*-
2 *
4e66da02 3 * $Id: f-binpoly.c,v 1.8 2004/04/02 01:03:49 mdw Exp $
ceb3f0c0 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 $
4e66da02 33 * Revision 1.8 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
4edc47b8 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 *
34e4f738 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 *
432c4e18 48 * Revision 1.5 2004/03/27 17:54:11 mdw
49 * Standard curves and curve checking.
50 *
bc985cef 51 * Revision 1.4 2004/03/23 15:19:32 mdw
52 * Test elliptic curves more thoroughly.
53 *
9b8b6877 54 * Revision 1.3 2004/03/23 12:08:26 mdw
55 * Random field-element selection.
56 *
c3caa2fa 57 * Revision 1.2 2004/03/21 22:52:06 mdw
58 * Merge and close elliptic curve branch.
59 *
ceb3f0c0 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"
9b8b6877 72#include "mprand.h"
4edc47b8 73#include "gfn.h"
ceb3f0c0 74
4edc47b8 75/*----- Polynomial basis --------------------------------------------------*/
ceb3f0c0 76
77typedef struct fctx {
78 field f;
79 gfreduce r;
80} fctx;
81
ceb3f0c0 82/* --- Field operations --- */
83
84static void fdestroy(field *ff)
4edc47b8 85 { fctx *f = (fctx *)ff; gfreduce_destroy(&f->r); DESTROY(f); }
ceb3f0c0 86
432c4e18 87static mp *frand(field *f, mp *d, grand *r)
4edc47b8 88 { return (mprand(d, f->nbits, r, 0)); }
9b8b6877 89
4edc47b8 90static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
ceb3f0c0 91
4edc47b8 92static mp *fadd(field *ff, mp *d, mp *x, mp *y) { return (gf_add(d, x, y)); }
ceb3f0c0 93
4edc47b8 94static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
95 fctx *f = (fctx *)ff; d = gf_mul(d, x, y);
ceb3f0c0 96 return (gfreduce_do(&f->r, d, d));
97}
98
4edc47b8 99static mp *fsqr(field *ff, mp *d, mp *x) {
100 fctx *f = (fctx *)ff; d = gf_sqr(d, x);
ceb3f0c0 101 return (gfreduce_do(&f->r, d, d));
102}
103
104static mp *finv(field *ff, mp *d, mp *x)
4edc47b8 105 { fctx *f = (fctx *)ff; gf_gcd(0, 0, &d, f->r.p, x); return (d); }
ceb3f0c0 106
107static mp *freduce(field *ff, mp *d, mp *x)
4edc47b8 108 { fctx *f = (fctx *)ff; return (gfreduce_do(&f->r, d, x)); }
ceb3f0c0 109
110static mp *fsqrt(field *ff, mp *d, mp *x)
4edc47b8 111 { fctx *f = (fctx *)ff; return (gfreduce_sqrt(&f->r, d, x)); }
ceb3f0c0 112
113static mp *fquadsolve(field *ff, mp *d, mp *x)
4edc47b8 114 { fctx *f = (fctx *)ff; return (gfreduce_quadsolve(&f->r, d, x)); }
ceb3f0c0 115
116/* --- Field operations table --- */
117
4e66da02 118static const field_ops fops = {
bc985cef 119 FTY_BINARY, "binpoly",
34e4f738 120 fdestroy, frand, field_stdsamep,
ceb3f0c0 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
136field *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;
432c4e18 142 f->f.nbits = mp_bits(p) - 1;
143 f->f.noctets = (f->f.nbits + 7) >> 3;
ceb3f0c0 144 gfreduce_create(&f->r, p);
432c4e18 145 f->f.m = f->r.p;
ceb3f0c0 146 return (&f->f);
147}
148
4edc47b8 149/*----- Normal basis ------------------------------------------------------*/
150
151typedef struct fnctx {
152 fctx f;
153 gfn ntop, pton;
154} fnctx;
155
156/* --- Field operations --- */
157
158static 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
164static 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
169static mp *fnin(field *ff, mp *d, mp *x)
170 { fnctx *f = (fnctx *)ff; return (gfn_transform(&f->ntop, d, x)); }
171
172static 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
4e66da02 177static const field_ops fnops = {
4edc47b8 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
198field *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
ceb3f0c0 212/*----- That's all, folks -------------------------------------------------*/