3 * $Id: f-binpoly.c,v 1.6 2004/04/01 12:50:09 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.6 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.5 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
43 * Revision 1.4 2004/03/23 15:19:32 mdw
44 * Test elliptic curves more thoroughly.
46 * Revision 1.3 2004/03/23 12:08:26 mdw
47 * Random field-element selection.
49 * Revision 1.2 2004/03/21 22:52:06 mdw
50 * Merge and close elliptic curve branch.
52 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
53 * Elliptic curves on binary fields work.
57 /*----- Header files ------------------------------------------------------*/
66 /*----- Data structures ---------------------------------------------------*/
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- Field operations --- */
77 static void fdestroy(field
*ff
)
80 gfreduce_destroy(&f
->r
);
84 static mp
*frand(field
*f
, mp
*d
, grand
*r
)
86 return (mprand(d
, f
->nbits
, r
, 0));
89 static int fzerop(field
*ff
, mp
*x
)
94 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
96 return (gf_add(d
, x
, y
));
99 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
101 fctx
*f
= (fctx
*)ff
;
103 return (gfreduce_do(&f
->r
, d
, d
));
106 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
108 fctx
*f
= (fctx
*)ff
;
110 return (gfreduce_do(&f
->r
, d
, d
));
113 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
115 fctx
*f
= (fctx
*)ff
;
116 gf_gcd(0, 0, &d
, f
->r
.p
, x
);
120 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
122 fctx
*f
= (fctx
*)ff
;
123 return (gfreduce_do(&f
->r
, d
, x
));
126 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
128 fctx
*f
= (fctx
*)ff
;
129 return (gfreduce_sqrt(&f
->r
, d
, x
));
132 static mp
*fquadsolve(field
*ff
, mp
*d
, mp
*x
)
134 fctx
*f
= (fctx
*)ff
;
135 return (gfreduce_quadsolve(&f
->r
, d
, x
));
138 /* --- Field operations table --- */
140 static field_ops fops
= {
141 FTY_BINARY
, "binpoly",
142 fdestroy
, frand
, field_stdsamep
,
144 fzerop
, field_id
, fadd
, fadd
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
149 /* --- @field_binpoly@ --- *
151 * Arguments: @mp *p@ = the reduction polynomial
153 * Returns: A pointer to the field.
155 * Use: Creates a field structure for a binary field mod @p@.
158 field
*field_binpoly(mp
*p
)
160 fctx
*f
= CREATE(fctx
);
164 f
->f
.nbits
= mp_bits(p
) - 1;
165 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
166 gfreduce_create(&f
->r
, p
);
171 /*----- That's all, folks -------------------------------------------------*/