Expunge revision histories in files.
[u/mdw/catacomb] / f-binpoly.c
CommitLineData
ceb3f0c0 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: f-binpoly.c,v 1.9 2004/04/08 01:36:15 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
ceb3f0c0 30/*----- Header files ------------------------------------------------------*/
31
32#include <mLib/sub.h>
33
34#include "field.h"
35#include "gf.h"
36#include "gfreduce.h"
9b8b6877 37#include "mprand.h"
4edc47b8 38#include "gfn.h"
ceb3f0c0 39
4edc47b8 40/*----- Polynomial basis --------------------------------------------------*/
ceb3f0c0 41
42typedef struct fctx {
43 field f;
44 gfreduce r;
45} fctx;
46
ceb3f0c0 47/* --- Field operations --- */
48
49static void fdestroy(field *ff)
4edc47b8 50 { fctx *f = (fctx *)ff; gfreduce_destroy(&f->r); DESTROY(f); }
ceb3f0c0 51
432c4e18 52static mp *frand(field *f, mp *d, grand *r)
4edc47b8 53 { return (mprand(d, f->nbits, r, 0)); }
9b8b6877 54
4edc47b8 55static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
ceb3f0c0 56
4edc47b8 57static mp *fadd(field *ff, mp *d, mp *x, mp *y) { return (gf_add(d, x, y)); }
ceb3f0c0 58
4edc47b8 59static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
60 fctx *f = (fctx *)ff; d = gf_mul(d, x, y);
ceb3f0c0 61 return (gfreduce_do(&f->r, d, d));
62}
63
4edc47b8 64static mp *fsqr(field *ff, mp *d, mp *x) {
65 fctx *f = (fctx *)ff; d = gf_sqr(d, x);
ceb3f0c0 66 return (gfreduce_do(&f->r, d, d));
67}
68
69static mp *finv(field *ff, mp *d, mp *x)
b817bfc6 70 { fctx *f = (fctx *)ff; d = gf_modinv(d, x, f->r.p); return (d); }
ceb3f0c0 71
72static mp *freduce(field *ff, mp *d, mp *x)
4edc47b8 73 { fctx *f = (fctx *)ff; return (gfreduce_do(&f->r, d, x)); }
ceb3f0c0 74
75static mp *fsqrt(field *ff, mp *d, mp *x)
4edc47b8 76 { fctx *f = (fctx *)ff; return (gfreduce_sqrt(&f->r, d, x)); }
ceb3f0c0 77
78static mp *fquadsolve(field *ff, mp *d, mp *x)
4edc47b8 79 { fctx *f = (fctx *)ff; return (gfreduce_quadsolve(&f->r, d, x)); }
ceb3f0c0 80
81/* --- Field operations table --- */
82
4e66da02 83static const field_ops fops = {
bc985cef 84 FTY_BINARY, "binpoly",
34e4f738 85 fdestroy, frand, field_stdsamep,
ceb3f0c0 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;
432c4e18 107 f->f.nbits = mp_bits(p) - 1;
108 f->f.noctets = (f->f.nbits + 7) >> 3;
ceb3f0c0 109 gfreduce_create(&f->r, p);
432c4e18 110 f->f.m = f->r.p;
ceb3f0c0 111 return (&f->f);
112}
113
4edc47b8 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
4e66da02 142static const field_ops fnops = {
4edc47b8 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
ceb3f0c0 177/*----- That's all, folks -------------------------------------------------*/