Merge and close elliptic curve branch.
[u/mdw/catacomb] / f-binpoly.c
1 /* -*-c-*-
2 *
3 * $Id: f-binpoly.c,v 1.2 2004/03/21 22:52:06 mdw Exp $
4 *
5 * Binary fields with polynomial basis representation
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: f-binpoly.c,v $
33 * Revision 1.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
35 *
36 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <mLib/sub.h>
44
45 #include "field.h"
46 #include "gf.h"
47 #include "gfreduce.h"
48
49 /*----- Data structures ---------------------------------------------------*/
50
51 typedef struct fctx {
52 field f;
53 gfreduce r;
54 } fctx;
55
56 /*----- Main code ---------------------------------------------------------*/
57
58 /* --- Field operations --- */
59
60 static void fdestroy(field *ff)
61 {
62 fctx *f = (fctx *)ff;
63 gfreduce_destroy(&f->r);
64 DESTROY(f);
65 }
66
67 static int fzerop(field *ff, mp *x)
68 {
69 return (!MP_LEN(x));
70 }
71
72 static mp *fadd(field *ff, mp *d, mp *x, mp *y)
73 {
74 return (gf_add(d, x, y));
75 }
76
77 static mp *fmul(field *ff, mp *d, mp *x, mp *y)
78 {
79 fctx *f = (fctx *)ff;
80 d = gf_mul(d, x, y);
81 return (gfreduce_do(&f->r, d, d));
82 }
83
84 static mp *fsqr(field *ff, mp *d, mp *x)
85 {
86 fctx *f = (fctx *)ff;
87 d = gf_sqr(d, x);
88 return (gfreduce_do(&f->r, d, d));
89 }
90
91 static mp *finv(field *ff, mp *d, mp *x)
92 {
93 fctx *f = (fctx *)ff;
94 gf_gcd(0, 0, &d, f->r.p, x);
95 return (d);
96 }
97
98 static mp *freduce(field *ff, mp *d, mp *x)
99 {
100 fctx *f = (fctx *)ff;
101 return (gfreduce_do(&f->r, d, x));
102 }
103
104 static mp *fsqrt(field *ff, mp *d, mp *x)
105 {
106 fctx *f = (fctx *)ff;
107 return (gfreduce_sqrt(&f->r, d, x));
108 }
109
110 static mp *fquadsolve(field *ff, mp *d, mp *x)
111 {
112 fctx *f = (fctx *)ff;
113 return (gfreduce_quadsolve(&f->r, d, x));
114 }
115
116 /* --- Field operations table --- */
117
118 static field_ops fops = {
119 fdestroy,
120 freduce, field_id,
121 fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt,
122 fquadsolve,
123 0, 0, 0, 0
124 };
125
126 /* --- @field_binpoly@ --- *
127 *
128 * Arguments: @mp *p@ = the reduction polynomial
129 *
130 * Returns: A pointer to the field.
131 *
132 * Use: Creates a field structure for a binary field mod @p@.
133 */
134
135 field *field_binpoly(mp *p)
136 {
137 fctx *f = CREATE(fctx);
138 f->f.ops = &fops;
139 f->f.zero = MP_ZERO;
140 f->f.one = MP_ONE;
141 gfreduce_create(&f->r, p);
142 return (&f->f);
143 }
144
145 /*----- That's all, folks -------------------------------------------------*/