Expunge revision histories in files.
[u/mdw/catacomb] / field.h
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: field.h,v 1.11 2004/04/08 01:36:15 mdw Exp $
b0ab12e6 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
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 */
b0ab12e6 59} field;
60
bc985cef 61enum {
62 FTY_PRIME,
63 FTY_BINARY
64};
65
b0ab12e6 66typedef struct field_ops {
67
bc985cef 68 /* --- General information --- */
69
70 unsigned ty; /* What kind of field this is */
71 const char *name; /* Human-readable name string */
72
b0ab12e6 73 /* --- Universal operations --- */
74
75 void (*destroy)(field */*f*/);
9b8b6877 76 mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
34e4f738 77 int (*samep)(field */*f*/, field */*g*/);
b0ab12e6 78
79 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
80 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
81
8823192f 82 int (*zerop)(field */*f*/, mp */*x*/);
b085fd91 83 mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 84 mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
85 mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
86 mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
87 mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/);
88 mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/);
89 mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/);
ceb3f0c0 90 mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
91
92 /* --- Operations for binary fields only --- */
93
94 mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 95
96 /* --- Operations for prime fields only --- */
97
98 mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
99 mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
8823192f 100 mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
101 mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 102
103} field_ops;
104
bc985cef 105#define F_TYPE(f) (f)->ops->ty
106#define F_NAME(f) (f)->ops->name
107
b0ab12e6 108#define F_DESTROY(f) (f)->ops->destroy((f))
bc985cef 109#define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
34e4f738 110#define F_SAMEP(f, g) (f)->ops->samep((f), (g))
b0ab12e6 111
112#define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
113#define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
8823192f 114
115#define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
b085fd91 116#define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
b0ab12e6 117#define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
118#define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
119#define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
120#define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
121#define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
122#define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
ceb3f0c0 123#define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
124
125#define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
b0ab12e6 126
127#define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
128#define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
8823192f 129#define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x))
130#define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x))
b0ab12e6 131
2685767a 132/*----- Helpful field operations ------------------------------------------*/
133
134/* --- @field_id@ --- *
135 *
136 * Arguments: @field *f@ = pointer to a field
137 * @mp *d@ = a destination element
138 * @mp *x@ = a source element
139 *
140 * Returns: The result element.
141 *
142 * Use: An identity operation which can be used if your field has no
143 * internal representation.
144 */
145
146extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
147
34e4f738 148/* --- @field_samep@ --- *
149 *
150 * Arguments: @field *f, *g@ = two fields
151 *
152 * Returns: Nonzero if the fields are identical (not just isomorphic).
153 *
154 * Use: Checks for sameness of fields. This function does the full
155 * check, not just the field-type-specific check done by the
156 * @sampep@ field operation.
157 */
158
159extern int field_samep(field */*f*/, field */*g*/);
160
161/* --- @field_stdsamep@ --- *
162 *
163 * Arguments: @field *f, *g@ = two fields
164 *
165 * Returns: Nonzero if the fields are identical (not just isomorphic).
166 *
167 * Use: Standard sameness check, based on equality of the @m@
168 * member.
169 */
170
171extern int field_stdsamep(field */*f*/, field */*g*/);
172
b0ab12e6 173/*----- Creating fields ---------------------------------------------------*/
174
175/* --- @field_prime@ --- *
176 *
177 * Arguments: @mp *p@ = the characteristic of the field
178 *
179 * Returns: A pointer to the field.
180 *
181 * Use: Creates a field structure for a prime field of size %$p$%,
182 * using Montgomery reduction for arithmetic.
183 */
184
185extern field *field_prime(mp */*p*/);
186
f46efa79 187/* --- @field_niceprime@ --- *
188 *
189 * Arguments: @mp *p@ = the characteristic of the field
190 *
191 * Returns: A pointer to the field.
192 *
193 * Use: Creates a field structure for a prime field of size %$p$%,
194 * using efficient reduction for nice primes.
195 */
196
197extern field *field_niceprime(mp */*p*/);
198
b0ab12e6 199/* --- @field_binpoly@ --- *
200 *
201 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
202 *
203 * Returns: A pointer to the field.
204 *
205 * Use: Creates a field structure for a binary field using naive
206 * arithmetic.
207 */
208
209extern field *field_binpoly(mp */*p*/);
210
4edc47b8 211/* --- @field_binnorm@ --- *
212 *
213 * Arguments: @mp *p@ = the reduction polynomial
214 * @mp *beta@ = representation of normal point
215 *
216 * Returns: A pointer to the field.
217 *
218 * Use: Creates a field structure for a binary field mod @p@ which
219 * uses a normal basis representation externally. Computations
220 * are still done on a polynomial-basis representation.
221 */
222
223extern field *field_binnorm(mp */*p*/, mp */*beta*/);
224
432c4e18 225/* --- @field_parse@ --- *
226 *
227 * Arguments: @qd_parse *qd@ = parser context
228 *
229 * Returns: Field pointer if OK, or null.
230 *
231 * Use: Parses a field description, which has the form
232 *
4edc47b8 233 * * `prime', `niceprime', `binpoly', or `binnorm'
432c4e18 234 * * an optional `:'
235 * * the field modulus
4edc47b8 236 * * for `binnorm', an optional `,' and the beta value
432c4e18 237 */
238
239extern field *field_parse(qd_parse */*qd*/);
240
b0ab12e6 241/*----- That's all, folks -------------------------------------------------*/
242
243#ifdef __cplusplus
244 }
245#endif
246
247#endif