3 * $Id: f-binpoly.c,v 1.4 2004/03/23 15:19:32 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.4 2004/03/23 15:19:32 mdw
34 * Test elliptic curves more thoroughly.
36 * Revision 1.3 2004/03/23 12:08:26 mdw
37 * Random field-element selection.
39 * Revision 1.2 2004/03/21 22:52:06 mdw
40 * Merge and close elliptic curve branch.
42 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
43 * Elliptic curves on binary fields work.
47 /*----- Header files ------------------------------------------------------*/
56 /*----- Data structures ---------------------------------------------------*/
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- Field operations --- */
67 static void fdestroy(field
*ff
)
70 gfreduce_destroy(&f
->r
);
74 static mp
*frand(field
*ff
, mp
*d
, grand
*r
)
77 return (mprand(d
, mp_octets(f
->r
.p
) - 1, r
, 0));
80 static int fzerop(field
*ff
, mp
*x
)
85 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
87 return (gf_add(d
, x
, y
));
90 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
94 return (gfreduce_do(&f
->r
, d
, d
));
97 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
101 return (gfreduce_do(&f
->r
, d
, d
));
104 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
106 fctx
*f
= (fctx
*)ff
;
107 gf_gcd(0, 0, &d
, f
->r
.p
, x
);
111 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
113 fctx
*f
= (fctx
*)ff
;
114 return (gfreduce_do(&f
->r
, d
, x
));
117 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
119 fctx
*f
= (fctx
*)ff
;
120 return (gfreduce_sqrt(&f
->r
, d
, x
));
123 static mp
*fquadsolve(field
*ff
, mp
*d
, mp
*x
)
125 fctx
*f
= (fctx
*)ff
;
126 return (gfreduce_quadsolve(&f
->r
, d
, x
));
129 /* --- Field operations table --- */
131 static field_ops fops
= {
132 FTY_BINARY
, "binpoly",
135 fzerop
, field_id
, fadd
, fadd
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
140 /* --- @field_binpoly@ --- *
142 * Arguments: @mp *p@ = the reduction polynomial
144 * Returns: A pointer to the field.
146 * Use: Creates a field structure for a binary field mod @p@.
149 field
*field_binpoly(mp
*p
)
151 fctx
*f
= CREATE(fctx
);
155 gfreduce_create(&f
->r
, p
);
159 /*----- That's all, folks -------------------------------------------------*/