3 * $Id: f-binpoly.c,v 1.9 2004/04/08 01:36:15 mdw Exp $
5 * Binary fields with polynomial basis representation
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
40 /*----- Polynomial basis --------------------------------------------------*/
47 /* --- Field operations --- */
49 static void fdestroy(field
*ff
)
50 { fctx
*f
= (fctx
*)ff
; gfreduce_destroy(&f
->r
); DESTROY(f
); }
52 static mp
*frand(field
*f
, mp
*d
, grand
*r
)
53 { return (mprand(d
, f
->nbits
, r
, 0)); }
55 static int fzerop(field
*ff
, mp
*x
) { return (!MP_LEN(x
)); }
57 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
) { return (gf_add(d
, x
, y
)); }
59 static 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
));
64 static 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
));
69 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
70 { fctx
*f
= (fctx
*)ff
; d
= gf_modinv(d
, x
, f
->r
.p
); return (d
); }
72 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
73 { fctx
*f
= (fctx
*)ff
; return (gfreduce_do(&f
->r
, d
, x
)); }
75 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
76 { fctx
*f
= (fctx
*)ff
; return (gfreduce_sqrt(&f
->r
, d
, x
)); }
78 static mp
*fquadsolve(field
*ff
, mp
*d
, mp
*x
)
79 { fctx
*f
= (fctx
*)ff
; return (gfreduce_quadsolve(&f
->r
, d
, x
)); }
81 /* --- Field operations table --- */
83 static const field_ops fops
= {
84 FTY_BINARY
, "binpoly",
85 fdestroy
, frand
, field_stdsamep
,
87 fzerop
, field_id
, fadd
, fadd
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
92 /* --- @field_binpoly@ --- *
94 * Arguments: @mp *p@ = the reduction polynomial
96 * Returns: A pointer to the field.
98 * Use: Creates a field structure for a binary field mod @p@.
101 field
*field_binpoly(mp
*p
)
103 fctx
*f
= CREATE(fctx
);
107 f
->f
.nbits
= mp_bits(p
) - 1;
108 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
109 gfreduce_create(&f
->r
, p
);
114 /*----- Normal basis ------------------------------------------------------*/
116 typedef struct fnctx
{
121 /* --- Field operations --- */
123 static void fndestroy(field
*ff
) {
124 fnctx
*f
= (fnctx
*)ff
; gfreduce_destroy(&f
->f
.r
);
125 gfn_destroy(&f
->ntop
); gfn_destroy(&f
->pton
);
129 static 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
));
134 static mp
*fnin(field
*ff
, mp
*d
, mp
*x
)
135 { fnctx
*f
= (fnctx
*)ff
; return (gfn_transform(&f
->ntop
, d
, x
)); }
137 static mp
*fnout(field
*ff
, mp
*d
, mp
*x
)
138 { fnctx
*f
= (fnctx
*)ff
; return (gfn_transform(&f
->pton
, d
, x
)); }
140 /* --- Field operations table --- */
142 static const field_ops fnops
= {
143 FTY_BINARY
, "binnorm",
144 fndestroy
, frand
, fnsamep
,
146 fzerop
, field_id
, fadd
, fadd
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
151 /* --- @field_binnorm@ --- *
153 * Arguments: @mp *p@ = the reduction polynomial
154 * @mp *beta@ = representation of normal point
156 * Returns: A pointer to the field.
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.
163 field
*field_binnorm(mp
*p
, mp
*beta
)
165 fnctx
*f
= CREATE(fnctx
);
167 f
->f
.f
.zero
= MP_ZERO
;
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
);
173 gfn_create(p
, beta
, &f
->ntop
, &f
->pton
);
177 /*----- That's all, folks -------------------------------------------------*/