bd4d8b0e715ac1a92a202f60b11eccbbc60370d4
[u/mdw/catacomb] / f-binpoly.c
1 /* -*-c-*-
2 *
3 * $Id: f-binpoly.c,v 1.5 2004/03/27 17:54:11 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.5 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
35 *
36 * Revision 1.4 2004/03/23 15:19:32 mdw
37 * Test elliptic curves more thoroughly.
38 *
39 * Revision 1.3 2004/03/23 12:08:26 mdw
40 * Random field-element selection.
41 *
42 * Revision 1.2 2004/03/21 22:52:06 mdw
43 * Merge and close elliptic curve branch.
44 *
45 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
46 * Elliptic curves on binary fields work.
47 *
48 */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include <mLib/sub.h>
53
54 #include "field.h"
55 #include "gf.h"
56 #include "gfreduce.h"
57 #include "mprand.h"
58
59 /*----- Data structures ---------------------------------------------------*/
60
61 typedef struct fctx {
62 field f;
63 gfreduce r;
64 } fctx;
65
66 /*----- Main code ---------------------------------------------------------*/
67
68 /* --- Field operations --- */
69
70 static void fdestroy(field *ff)
71 {
72 fctx *f = (fctx *)ff;
73 gfreduce_destroy(&f->r);
74 DESTROY(f);
75 }
76
77 static mp *frand(field *f, mp *d, grand *r)
78 {
79 return (mprand(d, f->nbits, r, 0));
80 }
81
82 static int fzerop(field *ff, mp *x)
83 {
84 return (!MP_LEN(x));
85 }
86
87 static mp *fadd(field *ff, mp *d, mp *x, mp *y)
88 {
89 return (gf_add(d, x, y));
90 }
91
92 static mp *fmul(field *ff, mp *d, mp *x, mp *y)
93 {
94 fctx *f = (fctx *)ff;
95 d = gf_mul(d, x, y);
96 return (gfreduce_do(&f->r, d, d));
97 }
98
99 static mp *fsqr(field *ff, mp *d, mp *x)
100 {
101 fctx *f = (fctx *)ff;
102 d = gf_sqr(d, x);
103 return (gfreduce_do(&f->r, d, d));
104 }
105
106 static mp *finv(field *ff, mp *d, mp *x)
107 {
108 fctx *f = (fctx *)ff;
109 gf_gcd(0, 0, &d, f->r.p, x);
110 return (d);
111 }
112
113 static mp *freduce(field *ff, mp *d, mp *x)
114 {
115 fctx *f = (fctx *)ff;
116 return (gfreduce_do(&f->r, d, x));
117 }
118
119 static mp *fsqrt(field *ff, mp *d, mp *x)
120 {
121 fctx *f = (fctx *)ff;
122 return (gfreduce_sqrt(&f->r, d, x));
123 }
124
125 static mp *fquadsolve(field *ff, mp *d, mp *x)
126 {
127 fctx *f = (fctx *)ff;
128 return (gfreduce_quadsolve(&f->r, d, x));
129 }
130
131 /* --- Field operations table --- */
132
133 static field_ops fops = {
134 FTY_BINARY, "binpoly",
135 fdestroy, frand,
136 freduce, field_id,
137 fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt,
138 fquadsolve,
139 0, 0, 0, 0
140 };
141
142 /* --- @field_binpoly@ --- *
143 *
144 * Arguments: @mp *p@ = the reduction polynomial
145 *
146 * Returns: A pointer to the field.
147 *
148 * Use: Creates a field structure for a binary field mod @p@.
149 */
150
151 field *field_binpoly(mp *p)
152 {
153 fctx *f = CREATE(fctx);
154 f->f.ops = &fops;
155 f->f.zero = MP_ZERO;
156 f->f.one = MP_ONE;
157 f->f.nbits = mp_bits(p) - 1;
158 f->f.noctets = (f->f.nbits + 7) >> 3;
159 gfreduce_create(&f->r, p);
160 f->f.m = f->r.p;
161 return (&f->f);
162 }
163
164 /*----- That's all, folks -------------------------------------------------*/