.gitignore: Ignore `auto-version' script.
[u/mdw/catacomb] / field.h
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
1d7857e7 3 * $Id$
b0ab12e6 4 *
5 * Definitions for field arithmetic
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
b0ab12e6 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.
45c0fd36 18 *
b0ab12e6 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.
45c0fd36 23 *
b0ab12e6 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
b0ab12e6 30#ifndef CATACOMB_FIELD_H
31#define CATACOMB_FIELD_H
32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
9b8b6877 39#ifndef CATACOMB_GRAND_H
40# include "grand.h"
41#endif
42
b0ab12e6 43#ifndef CATACOMB_MP_H
44# include "mp.h"
45#endif
46
432c4e18 47#ifndef CATACOMB_QDPARSE_H
48# include "qdparse.h"
49#endif
50
b0ab12e6 51/*----- Data structures ---------------------------------------------------*/
52
53typedef struct field {
54 const struct field_ops *ops; /* Field operations */
55 mp *zero, *one; /* Identities in the field */
432c4e18 56 mp *m; /* Modulus (prime and binary) */
57 unsigned long nbits; /* Length of field element in bits */
58 size_t noctets; /* Length of element in octets */
1d7857e7 59 mp *q; /* Number of elements in field */
b0ab12e6 60} field;
61
bc985cef 62enum {
63 FTY_PRIME,
64 FTY_BINARY
65};
66
b0ab12e6 67typedef struct field_ops {
68
bc985cef 69 /* --- General information --- */
70
71 unsigned ty; /* What kind of field this is */
72 const char *name; /* Human-readable name string */
73
b0ab12e6 74 /* --- Universal operations --- */
75
76 void (*destroy)(field */*f*/);
9b8b6877 77 mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
34e4f738 78 int (*samep)(field */*f*/, field */*g*/);
b0ab12e6 79
80 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
81 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
82
8823192f 83 int (*zerop)(field */*f*/, mp */*x*/);
b085fd91 84 mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 85 mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
86 mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
87 mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
88 mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/);
89 mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/);
90 mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/);
ceb3f0c0 91 mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
92
93 /* --- Operations for binary fields only --- */
94
95 mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 96
97 /* --- Operations for prime fields only --- */
98
99 mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
100 mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
8823192f 101 mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
102 mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 103
104} field_ops;
105
bc985cef 106#define F_TYPE(f) (f)->ops->ty
107#define F_NAME(f) (f)->ops->name
108
b0ab12e6 109#define F_DESTROY(f) (f)->ops->destroy((f))
bc985cef 110#define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
34e4f738 111#define F_SAMEP(f, g) (f)->ops->samep((f), (g))
b0ab12e6 112
113#define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
114#define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
8823192f 115
116#define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
b085fd91 117#define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
b0ab12e6 118#define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
119#define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
120#define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
121#define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
122#define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
123#define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
ceb3f0c0 124#define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
125
126#define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
b0ab12e6 127
128#define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
129#define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
8823192f 130#define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x))
131#define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x))
b0ab12e6 132
2685767a 133/*----- Helpful field operations ------------------------------------------*/
134
135/* --- @field_id@ --- *
136 *
137 * Arguments: @field *f@ = pointer to a field
138 * @mp *d@ = a destination element
139 * @mp *x@ = a source element
140 *
141 * Returns: The result element.
142 *
143 * Use: An identity operation which can be used if your field has no
144 * internal representation.
145 */
146
147extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
148
34e4f738 149/* --- @field_samep@ --- *
150 *
151 * Arguments: @field *f, *g@ = two fields
152 *
153 * Returns: Nonzero if the fields are identical (not just isomorphic).
154 *
155 * Use: Checks for sameness of fields. This function does the full
156 * check, not just the field-type-specific check done by the
157 * @sampep@ field operation.
158 */
159
160extern int field_samep(field */*f*/, field */*g*/);
161
162/* --- @field_stdsamep@ --- *
163 *
164 * Arguments: @field *f, *g@ = two fields
165 *
166 * Returns: Nonzero if the fields are identical (not just isomorphic).
167 *
168 * Use: Standard sameness check, based on equality of the @m@
169 * member.
170 */
171
172extern int field_stdsamep(field */*f*/, field */*g*/);
173
1d7857e7 174/*----- Arithmetic --------------------------------------------------------*/
175
176/* --- @field_exp@ --- *
177 *
178 * Arguments: @field *f@ = pointer to field
179 * @mp *d@ = fake destination
180 * @mp *a@ = base
181 * @mp *e@ = exponent
182 *
183 * Returns: Result, %$a^e$%.
184 *
185 * Use: Exponentiation in a finite field. Note that all quantities
186 * are in internal format. This is a generic implementation
187 * suitable for use with all fields and is not intended to be
188 * optimal.
189 */
190
191extern mp *field_exp(field */*f*/, mp */*d*/, mp */*a*/, mp */*e*/);
192
b0ab12e6 193/*----- Creating fields ---------------------------------------------------*/
194
195/* --- @field_prime@ --- *
196 *
197 * Arguments: @mp *p@ = the characteristic of the field
198 *
199 * Returns: A pointer to the field.
200 *
201 * Use: Creates a field structure for a prime field of size %$p$%,
202 * using Montgomery reduction for arithmetic.
203 */
204
205extern field *field_prime(mp */*p*/);
206
f46efa79 207/* --- @field_niceprime@ --- *
208 *
209 * Arguments: @mp *p@ = the characteristic of the field
210 *
f4535c64 211 * Returns: A pointer to the field, or null.
f46efa79 212 *
213 * Use: Creates a field structure for a prime field of size %$p$%,
214 * using efficient reduction for nice primes.
215 */
216
217extern field *field_niceprime(mp */*p*/);
218
b0ab12e6 219/* --- @field_binpoly@ --- *
220 *
221 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
222 *
223 * Returns: A pointer to the field.
224 *
225 * Use: Creates a field structure for a binary field using naive
226 * arithmetic.
227 */
228
229extern field *field_binpoly(mp */*p*/);
230
4edc47b8 231/* --- @field_binnorm@ --- *
232 *
233 * Arguments: @mp *p@ = the reduction polynomial
234 * @mp *beta@ = representation of normal point
235 *
236 * Returns: A pointer to the field.
237 *
238 * Use: Creates a field structure for a binary field mod @p@ which
239 * uses a normal basis representation externally. Computations
240 * are still done on a polynomial-basis representation.
241 */
242
243extern field *field_binnorm(mp */*p*/, mp */*beta*/);
244
432c4e18 245/* --- @field_parse@ --- *
246 *
247 * Arguments: @qd_parse *qd@ = parser context
248 *
249 * Returns: Field pointer if OK, or null.
250 *
251 * Use: Parses a field description, which has the form
252 *
4edc47b8 253 * * `prime', `niceprime', `binpoly', or `binnorm'
432c4e18 254 * * an optional `:'
255 * * the field modulus
45c0fd36 256 * * for `binnorm', an optional `,' and the beta value
432c4e18 257 */
258
259extern field *field_parse(qd_parse */*qd*/);
260
b0ab12e6 261/*----- That's all, folks -------------------------------------------------*/
262
263#ifdef __cplusplus
264 }
265#endif
266
267#endif