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