bf0428a8bd931cb5c298b3e71532ef2522605a6b
[u/mdw/catacomb] / field.h
1 /* -*-c-*-
2 *
3 * $Id: field.h,v 1.9 2004/04/01 12:50:09 mdw Exp $
4 *
5 * Definitions for field arithmetic
6 *
7 * (c) 2000 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: field.h,v $
33 * Revision 1.9 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.)
39 *
40 * Revision 1.8 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
42 *
43 * Revision 1.7 2004/03/27 00:04:46 mdw
44 * Implement efficient reduction for pleasant-looking primes.
45 *
46 * Revision 1.6 2004/03/23 15:19:32 mdw
47 * Test elliptic curves more thoroughly.
48 *
49 * Revision 1.5 2004/03/23 12:08:26 mdw
50 * Random field-element selection.
51 *
52 * Revision 1.4 2004/03/21 22:52:06 mdw
53 * Merge and close elliptic curve branch.
54 *
55 * Revision 1.3.4.2 2004/03/21 22:39:46 mdw
56 * Elliptic curves on binary fields work.
57 *
58 * Revision 1.3.4.1 2004/03/20 00:13:31 mdw
59 * Projective coordinates for prime curves
60 *
61 * Revision 1.3 2002/01/13 13:48:44 mdw
62 * Further progress.
63 *
64 * Revision 1.2 2001/05/07 17:30:13 mdw
65 * Add an internal-representation no-op function.
66 *
67 * Revision 1.1 2001/04/29 18:12:33 mdw
68 * Prototype version.
69 *
70 */
71
72 #ifndef CATACOMB_FIELD_H
73 #define CATACOMB_FIELD_H
74
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78
79 /*----- Header files ------------------------------------------------------*/
80
81 #ifndef CATACOMB_GRAND_H
82 # include "grand.h"
83 #endif
84
85 #ifndef CATACOMB_MP_H
86 # include "mp.h"
87 #endif
88
89 #ifndef CATACOMB_QDPARSE_H
90 # include "qdparse.h"
91 #endif
92
93 /*----- Data structures ---------------------------------------------------*/
94
95 typedef struct field {
96 const struct field_ops *ops; /* Field operations */
97 mp *zero, *one; /* Identities in the field */
98 mp *m; /* Modulus (prime and binary) */
99 unsigned long nbits; /* Length of field element in bits */
100 size_t noctets; /* Length of element in octets */
101 } field;
102
103 enum {
104 FTY_PRIME,
105 FTY_BINARY
106 };
107
108 typedef struct field_ops {
109
110 /* --- General information --- */
111
112 unsigned ty; /* What kind of field this is */
113 const char *name; /* Human-readable name string */
114
115 /* --- Universal operations --- */
116
117 void (*destroy)(field */*f*/);
118 mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
119 int (*samep)(field */*f*/, field */*g*/);
120
121 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
122 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
123
124 int (*zerop)(field */*f*/, mp */*x*/);
125 mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
126 mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
127 mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
128 mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
129 mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/);
130 mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/);
131 mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/);
132 mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
133
134 /* --- Operations for binary fields only --- */
135
136 mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/);
137
138 /* --- Operations for prime fields only --- */
139
140 mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
141 mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
142 mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
143 mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
144
145 } field_ops;
146
147 #define F_TYPE(f) (f)->ops->ty
148 #define F_NAME(f) (f)->ops->name
149
150 #define F_DESTROY(f) (f)->ops->destroy((f))
151 #define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
152 #define F_SAMEP(f, g) (f)->ops->samep((f), (g))
153
154 #define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
155 #define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
156
157 #define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
158 #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
159 #define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
160 #define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
161 #define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
162 #define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
163 #define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
164 #define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
165 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
166
167 #define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
168
169 #define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
170 #define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
171 #define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x))
172 #define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x))
173
174 /*----- Helpful field operations ------------------------------------------*/
175
176 /* --- @field_id@ --- *
177 *
178 * Arguments: @field *f@ = pointer to a field
179 * @mp *d@ = a destination element
180 * @mp *x@ = a source element
181 *
182 * Returns: The result element.
183 *
184 * Use: An identity operation which can be used if your field has no
185 * internal representation.
186 */
187
188 extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
189
190 /* --- @field_samep@ --- *
191 *
192 * Arguments: @field *f, *g@ = two fields
193 *
194 * Returns: Nonzero if the fields are identical (not just isomorphic).
195 *
196 * Use: Checks for sameness of fields. This function does the full
197 * check, not just the field-type-specific check done by the
198 * @sampep@ field operation.
199 */
200
201 extern int field_samep(field */*f*/, field */*g*/);
202
203 /* --- @field_stdsamep@ --- *
204 *
205 * Arguments: @field *f, *g@ = two fields
206 *
207 * Returns: Nonzero if the fields are identical (not just isomorphic).
208 *
209 * Use: Standard sameness check, based on equality of the @m@
210 * member.
211 */
212
213 extern int field_stdsamep(field */*f*/, field */*g*/);
214
215 /*----- Creating fields ---------------------------------------------------*/
216
217 /* --- @field_prime@ --- *
218 *
219 * Arguments: @mp *p@ = the characteristic of the field
220 *
221 * Returns: A pointer to the field.
222 *
223 * Use: Creates a field structure for a prime field of size %$p$%,
224 * using Montgomery reduction for arithmetic.
225 */
226
227 extern field *field_prime(mp */*p*/);
228
229 /* --- @field_niceprime@ --- *
230 *
231 * Arguments: @mp *p@ = the characteristic of the field
232 *
233 * Returns: A pointer to the field.
234 *
235 * Use: Creates a field structure for a prime field of size %$p$%,
236 * using efficient reduction for nice primes.
237 */
238
239 extern field *field_niceprime(mp */*p*/);
240
241 /* --- @field_binpoly@ --- *
242 *
243 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
244 *
245 * Returns: A pointer to the field.
246 *
247 * Use: Creates a field structure for a binary field using naive
248 * arithmetic.
249 */
250
251 extern field *field_binpoly(mp */*p*/);
252
253 /* --- @field_parse@ --- *
254 *
255 * Arguments: @qd_parse *qd@ = parser context
256 *
257 * Returns: Field pointer if OK, or null.
258 *
259 * Use: Parses a field description, which has the form
260 *
261 * * `prime', `niceprime' or `binpoly'
262 * * an optional `:'
263 * * the field modulus
264 */
265
266 extern field *field_parse(qd_parse */*qd*/);
267
268 /*----- That's all, folks -------------------------------------------------*/
269
270 #ifdef __cplusplus
271 }
272 #endif
273
274 #endif