3 * $Id: f-binpoly.c,v 1.3 2004/03/23 12:08:26 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 /*----- Revision history --------------------------------------------------*
32 * $Log: f-binpoly.c,v $
33 * Revision 1.3 2004/03/23 12:08:26 mdw
34 * Random field-element selection.
36 * Revision 1.2 2004/03/21 22:52:06 mdw
37 * Merge and close elliptic curve branch.
39 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
40 * Elliptic curves on binary fields work.
44 /*----- Header files ------------------------------------------------------*/
53 /*----- Data structures ---------------------------------------------------*/
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- Field operations --- */
64 static void fdestroy(field
*ff
)
67 gfreduce_destroy(&f
->r
);
71 static mp
*frand(field
*ff
, grand
*r
, mp
*d
)
74 return (mprand(d
, mp_octets(f
->r
.p
) - 1, r
, 0));
77 static int fzerop(field
*ff
, mp
*x
)
82 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
84 return (gf_add(d
, x
, y
));
87 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
91 return (gfreduce_do(&f
->r
, d
, d
));
94 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
98 return (gfreduce_do(&f
->r
, d
, d
));
101 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
103 fctx
*f
= (fctx
*)ff
;
104 gf_gcd(0, 0, &d
, f
->r
.p
, x
);
108 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
110 fctx
*f
= (fctx
*)ff
;
111 return (gfreduce_do(&f
->r
, d
, x
));
114 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
116 fctx
*f
= (fctx
*)ff
;
117 return (gfreduce_sqrt(&f
->r
, d
, x
));
120 static mp
*fquadsolve(field
*ff
, mp
*d
, mp
*x
)
122 fctx
*f
= (fctx
*)ff
;
123 return (gfreduce_quadsolve(&f
->r
, d
, x
));
126 /* --- Field operations table --- */
128 static field_ops fops
= {
131 fzerop
, field_id
, fadd
, fadd
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
136 /* --- @field_binpoly@ --- *
138 * Arguments: @mp *p@ = the reduction polynomial
140 * Returns: A pointer to the field.
142 * Use: Creates a field structure for a binary field mod @p@.
145 field
*field_binpoly(mp
*p
)
147 fctx
*f
= CREATE(fctx
);
151 gfreduce_create(&f
->r
, p
);
155 /*----- That's all, folks -------------------------------------------------*/