chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / field.h
1 /* -*-c-*-
2  *
3  * Definitions for field arithmetic
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 #ifndef CATACOMB_FIELD_H
29 #define CATACOMB_FIELD_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #ifndef CATACOMB_GRAND_H
38 #  include "grand.h"
39 #endif
40
41 #ifndef CATACOMB_MP_H
42 #  include "mp.h"
43 #endif
44
45 #ifndef CATACOMB_QDPARSE_H
46 #  include "qdparse.h"
47 #endif
48
49 /*----- Data structures ---------------------------------------------------*/
50
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 */
58 } field;
59
60 enum {
61   FTY_PRIME,
62   FTY_BINARY
63 };
64
65 typedef struct field_ops {
66
67   /* --- General information --- */
68
69   unsigned ty;                          /* What kind of field this is */
70   const char *name;                     /* Human-readable name string */
71
72   /* --- Universal operations --- */
73
74   void (*destroy)(field */*f*/);
75   mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
76   int (*samep)(field */*f*/, field */*g*/);
77
78   mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
79   mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
80
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*/);
90
91   /* --- Operations for binary fields only --- */
92
93   mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/);
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*/);
99   mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
100   mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
101
102 } field_ops;
103
104 #define F_TYPE(f)               (f)->ops->ty
105 #define F_NAME(f)               (f)->ops->name
106
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))
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))
113
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))
123
124 #define F_QUADSOLVE(f, d, x)    (f)->ops->quadsolve((f), (d), (x))
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))
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))
130
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
145 extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
146
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
158 extern 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
170 extern int field_stdsamep(field */*f*/, field */*g*/);
171
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
189 extern mp *field_exp(field */*f*/, mp */*d*/, mp */*a*/, mp */*e*/);
190
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
203 extern field *field_prime(mp */*p*/);
204
205 /* --- @field_niceprime@ --- *
206  *
207  * Arguments:   @mp *p@ = the characteristic of the field
208  *
209  * Returns:     A pointer to the field, or null.
210  *
211  * Use:         Creates a field structure for a prime field of size %$p$%,
212  *              using efficient reduction for nice primes.
213  */
214
215 extern field *field_niceprime(mp */*p*/);
216
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
227 extern field *field_binpoly(mp */*p*/);
228
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
241 extern field *field_binnorm(mp */*p*/, mp */*beta*/);
242
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  *
251  *                * `prime', `niceprime', `binpoly', or `binnorm'
252  *                * an optional `:'
253  *                * the field modulus
254  *                * for `binnorm', an optional `,' and the beta value
255  */
256
257 extern field *field_parse(qd_parse */*qd*/);
258
259 /*----- That's all, folks -------------------------------------------------*/
260
261 #ifdef __cplusplus
262   }
263 #endif
264
265 #endif