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