Rename MP_IS* to MP_*P, for consistency's sake. Use these macros more often.
[u/mdw/catacomb] / f-binpoly.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id$
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/*----- Header files ------------------------------------------------------*/
31
32#include <mLib/sub.h>
33
34#include "field.h"
35#include "gf.h"
36#include "gfreduce.h"
37#include "mprand.h"
38#include "gfn.h"
39
40/*----- Polynomial basis --------------------------------------------------*/
41
42typedef struct fctx {
43 field f;
44 gfreduce r;
45} fctx;
46
47/* --- Field operations --- */
48
49static void fdestroy(field *ff)
50 { fctx *f = (fctx *)ff; gfreduce_destroy(&f->r); DESTROY(f); }
51
52static mp *frand(field *f, mp *d, grand *r)
53 { return (mprand(d, f->nbits, r, 0)); }
54
55static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
56
57static mp *fadd(field *ff, mp *d, mp *x, mp *y) { return (gf_add(d, x, y)); }
58
59static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
60 fctx *f = (fctx *)ff; d = gf_mul(d, x, y);
61 return (gfreduce_do(&f->r, d, d));
62}
63
64static mp *fsqr(field *ff, mp *d, mp *x) {
65 fctx *f = (fctx *)ff; d = gf_sqr(d, x);
66 return (gfreduce_do(&f->r, d, d));
67}
68
69static mp *finv(field *ff, mp *d, mp *x)
70 { fctx *f = (fctx *)ff; d = gf_modinv(d, x, f->r.p); return (d); }
71
72static mp *freduce(field *ff, mp *d, mp *x)
73 { fctx *f = (fctx *)ff; return (gfreduce_do(&f->r, d, x)); }
74
75static mp *fsqrt(field *ff, mp *d, mp *x)
76 { fctx *f = (fctx *)ff; return (gfreduce_sqrt(&f->r, d, x)); }
77
78static mp *fquadsolve(field *ff, mp *d, mp *x)
79 { fctx *f = (fctx *)ff; return (gfreduce_quadsolve(&f->r, d, x)); }
80
81/* --- Field operations table --- */
82
83static const field_ops fops = {
84 FTY_BINARY, "binpoly",
85 fdestroy, frand, field_stdsamep,
86 freduce, field_id,
87 fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt,
88 fquadsolve,
89 0, 0, 0, 0
90};
91
92/* --- @field_binpoly@ --- *
93 *
94 * Arguments: @mp *p@ = the reduction polynomial
95 *
96 * Returns: A pointer to the field.
97 *
98 * Use: Creates a field structure for a binary field mod @p@.
99 */
100
101field *field_binpoly(mp *p)
102{
103 fctx *f = CREATE(fctx);
104 f->f.ops = &fops;
105 f->f.zero = MP_ZERO;
106 f->f.one = MP_ONE;
107 f->f.nbits = mp_bits(p) - 1;
108 f->f.noctets = (f->f.nbits + 7) >> 3;
109 gfreduce_create(&f->r, p);
110 f->f.m = f->r.p;
111 return (&f->f);
112}
113
114/*----- Normal basis ------------------------------------------------------*/
115
116typedef struct fnctx {
117 fctx f;
118 gfn ntop, pton;
119} fnctx;
120
121/* --- Field operations --- */
122
123static void fndestroy(field *ff) {
124 fnctx *f = (fnctx *)ff; gfreduce_destroy(&f->f.r);
125 gfn_destroy(&f->ntop); gfn_destroy(&f->pton);
126 DESTROY(f);
127}
128
129static int fnsamep(field *ff, field *gg) {
130 fnctx *f = (fnctx *)ff, *g = (fnctx *)gg;
131 return (MP_EQ(f->ntop.r[0], g->ntop.r[0]) && field_stdsamep(ff, gg));
132}
133
134static mp *fnin(field *ff, mp *d, mp *x)
135 { fnctx *f = (fnctx *)ff; return (gfn_transform(&f->ntop, d, x)); }
136
137static mp *fnout(field *ff, mp *d, mp *x)
138 { fnctx *f = (fnctx *)ff; return (gfn_transform(&f->pton, d, x)); }
139
140/* --- Field operations table --- */
141
142static const field_ops fnops = {
143 FTY_BINARY, "binnorm",
144 fndestroy, frand, fnsamep,
145 fnin, fnout,
146 fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt,
147 fquadsolve,
148 0, 0, 0, 0
149};
150
151/* --- @field_binnorm@ --- *
152 *
153 * Arguments: @mp *p@ = the reduction polynomial
154 * @mp *beta@ = representation of normal point
155 *
156 * Returns: A pointer to the field.
157 *
158 * Use: Creates a field structure for a binary field mod @p@ which
159 * uses a normal basis representation externally. Computations
160 * are still done on a polynomial-basis representation.
161 */
162
163field *field_binnorm(mp *p, mp *beta)
164{
165 fnctx *f = CREATE(fnctx);
166 f->f.f.ops = &fnops;
167 f->f.f.zero = MP_ZERO;
168 f->f.f.one = MP_ONE;
169 f->f.f.nbits = mp_bits(p) - 1;
170 f->f.f.noctets = (f->f.f.nbits + 7) >> 3;
171 gfreduce_create(&f->f.r, p);
172 f->f.f.m = f->f.r.p;
173 gfn_create(p, beta, &f->ntop, &f->pton);
174 return (&f->f.f);
175}
176
177/*----- That's all, folks -------------------------------------------------*/