3 * $Id: f-binpoly.c,v 1.5 2004/03/27 17:54:11 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.5 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
36 * Revision 1.4 2004/03/23 15:19:32 mdw
37 * Test elliptic curves more thoroughly.
39 * Revision 1.3 2004/03/23 12:08:26 mdw
40 * Random field-element selection.
42 * Revision 1.2 2004/03/21 22:52:06 mdw
43 * Merge and close elliptic curve branch.
45 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
46 * Elliptic curves on binary fields work.
50 /*----- Header files ------------------------------------------------------*/
59 /*----- Data structures ---------------------------------------------------*/
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- Field operations --- */
70 static void fdestroy(field
*ff
)
73 gfreduce_destroy(&f
->r
);
77 static mp
*frand(field
*f
, mp
*d
, grand
*r
)
79 return (mprand(d
, f
->nbits
, r
, 0));
82 static int fzerop(field
*ff
, mp
*x
)
87 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
89 return (gf_add(d
, x
, y
));
92 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
96 return (gfreduce_do(&f
->r
, d
, d
));
99 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
101 fctx
*f
= (fctx
*)ff
;
103 return (gfreduce_do(&f
->r
, d
, d
));
106 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
108 fctx
*f
= (fctx
*)ff
;
109 gf_gcd(0, 0, &d
, f
->r
.p
, x
);
113 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
115 fctx
*f
= (fctx
*)ff
;
116 return (gfreduce_do(&f
->r
, d
, x
));
119 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
121 fctx
*f
= (fctx
*)ff
;
122 return (gfreduce_sqrt(&f
->r
, d
, x
));
125 static mp
*fquadsolve(field
*ff
, mp
*d
, mp
*x
)
127 fctx
*f
= (fctx
*)ff
;
128 return (gfreduce_quadsolve(&f
->r
, d
, x
));
131 /* --- Field operations table --- */
133 static field_ops fops
= {
134 FTY_BINARY
, "binpoly",
137 fzerop
, field_id
, fadd
, fadd
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
142 /* --- @field_binpoly@ --- *
144 * Arguments: @mp *p@ = the reduction polynomial
146 * Returns: A pointer to the field.
148 * Use: Creates a field structure for a binary field mod @p@.
151 field
*field_binpoly(mp
*p
)
153 fctx
*f
= CREATE(fctx
);
157 f
->f
.nbits
= mp_bits(p
) - 1;
158 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
159 gfreduce_create(&f
->r
, p
);
164 /*----- That's all, folks -------------------------------------------------*/