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