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