3 * Definitions for field arithmetic
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_FIELD_H
29 #define CATACOMB_FIELD_H
35 /*----- Header files ------------------------------------------------------*/
37 #ifndef CATACOMB_GRAND_H
45 #ifndef CATACOMB_QDPARSE_H
49 /*----- Data structures ---------------------------------------------------*/
51 typedef struct field {
52 const struct field_ops *ops; /* Field operations */
53 mp *zero, *one; /* Identities in the field */
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 */
57 mp *q; /* Number of elements in field */
65 typedef struct field_ops {
67 /* --- General information --- */
69 unsigned ty; /* What kind of field this is */
70 const char *name; /* Human-readable name string */
72 /* --- Universal operations --- */
74 void (*destroy)(field */*f*/);
75 mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
76 int (*samep)(field */*f*/, field */*g*/);
78 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
79 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
81 int (*zerop)(field */*f*/, mp */*x*/);
82 mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
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*/);
89 mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
91 /* --- Operations for binary fields only --- */
93 mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/);
95 /* --- Operations for prime fields only --- */
97 mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
98 mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
99 mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
100 mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
104 #define F_TYPE(f) (f)->ops->ty
105 #define F_NAME(f) (f)->ops->name
107 #define F_DESTROY(f) (f)->ops->destroy((f))
108 #define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
109 #define F_SAMEP(f, g) (f)->ops->samep((f), (g))
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))
114 #define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
115 #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
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))
122 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
124 #define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
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))
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))
131 /*----- Helpful field operations ------------------------------------------*/
133 /* --- @field_id@ --- *
135 * Arguments: @field *f@ = pointer to a field
136 * @mp *d@ = a destination element
137 * @mp *x@ = a source element
139 * Returns: The result element.
141 * Use: An identity operation which can be used if your field has no
142 * internal representation.
145 extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
147 /* --- @field_samep@ --- *
149 * Arguments: @field *f, *g@ = two fields
151 * Returns: Nonzero if the fields are identical (not just isomorphic).
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.
158 extern int field_samep(field */*f*/, field */*g*/);
160 /* --- @field_stdsamep@ --- *
162 * Arguments: @field *f, *g@ = two fields
164 * Returns: Nonzero if the fields are identical (not just isomorphic).
166 * Use: Standard sameness check, based on equality of the @m@
170 extern int field_stdsamep(field */*f*/, field */*g*/);
172 /*----- Arithmetic --------------------------------------------------------*/
174 /* --- @field_exp@ --- *
176 * Arguments: @field *f@ = pointer to field
177 * @mp *d@ = fake destination
181 * Returns: Result, %$a^e$%.
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
189 extern mp *field_exp(field */*f*/, mp */*d*/, mp */*a*/, mp */*e*/);
191 /*----- Creating fields ---------------------------------------------------*/
193 /* --- @field_prime@ --- *
195 * Arguments: @mp *p@ = the characteristic of the field
197 * Returns: A pointer to the field.
199 * Use: Creates a field structure for a prime field of size %$p$%,
200 * using Montgomery reduction for arithmetic.
203 extern field *field_prime(mp */*p*/);
205 /* --- @field_niceprime@ --- *
207 * Arguments: @mp *p@ = the characteristic of the field
209 * Returns: A pointer to the field, or null.
211 * Use: Creates a field structure for a prime field of size %$p$%,
212 * using efficient reduction for nice primes.
215 extern field *field_niceprime(mp */*p*/);
217 /* --- @field_binpoly@ --- *
219 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
221 * Returns: A pointer to the field.
223 * Use: Creates a field structure for a binary field using naive
227 extern field *field_binpoly(mp */*p*/);
229 /* --- @field_binnorm@ --- *
231 * Arguments: @mp *p@ = the reduction polynomial
232 * @mp *beta@ = representation of normal point
234 * Returns: A pointer to the field.
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.
241 extern field *field_binnorm(mp */*p*/, mp */*beta*/);
243 /* --- @field_parse@ --- *
245 * Arguments: @qd_parse *qd@ = parser context
247 * Returns: Field pointer if OK, or null.
249 * Use: Parses a field description, which has the form
251 * * `prime', `niceprime', `binpoly', or `binnorm'
253 * * the field modulus
254 * * for `binnorm', an optional `,' and the beta value
257 extern field *field_parse(qd_parse */*qd*/);
259 /*----- That's all, folks -------------------------------------------------*/