chiark / gitweb /
Implement efficient reduction for pleasant-looking primes.
[catacomb] / field.h
1 /* -*-c-*-
2  *
3  * $Id: field.h,v 1.7 2004/03/27 00:04:46 mdw Exp $
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
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: field.h,v $
33  * Revision 1.7  2004/03/27 00:04:46  mdw
34  * Implement efficient reduction for pleasant-looking primes.
35  *
36  * Revision 1.6  2004/03/23 15:19:32  mdw
37  * Test elliptic curves more thoroughly.
38  *
39  * Revision 1.5  2004/03/23 12:08:26  mdw
40  * Random field-element selection.
41  *
42  * Revision 1.4  2004/03/21 22:52:06  mdw
43  * Merge and close elliptic curve branch.
44  *
45  * Revision 1.3.4.2  2004/03/21 22:39:46  mdw
46  * Elliptic curves on binary fields work.
47  *
48  * Revision 1.3.4.1  2004/03/20 00:13:31  mdw
49  * Projective coordinates for prime curves
50  *
51  * Revision 1.3  2002/01/13 13:48:44  mdw
52  * Further progress.
53  *
54  * Revision 1.2  2001/05/07 17:30:13  mdw
55  * Add an internal-representation no-op function.
56  *
57  * Revision 1.1  2001/04/29 18:12:33  mdw
58  * Prototype version.
59  *
60  */
61
62 #ifndef CATACOMB_FIELD_H
63 #define CATACOMB_FIELD_H
64
65 #ifdef __cplusplus
66   extern "C" {
67 #endif
68
69 /*----- Header files ------------------------------------------------------*/
70
71 #ifndef CATACOMB_GRAND_H
72 #  include "grand.h"
73 #endif
74
75 #ifndef CATACOMB_MP_H
76 #  include "mp.h"
77 #endif
78
79 /*----- Data structures ---------------------------------------------------*/
80
81 typedef struct field {
82   const struct field_ops *ops;          /* Field operations */
83   mp *zero, *one;                       /* Identities in the field */
84 } field;
85
86 enum {
87   FTY_PRIME,
88   FTY_BINARY
89 };
90
91 typedef struct field_ops {
92
93   /* --- General information --- */
94
95   unsigned ty;                          /* What kind of field this is */
96   const char *name;                     /* Human-readable name string */
97
98   /* --- Universal operations --- */
99
100   void (*destroy)(field */*f*/);
101   mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
102
103   mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
104   mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
105
106   int (*zerop)(field */*f*/, mp */*x*/);
107   mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
108   mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
109   mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
110   mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
111   mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/);
112   mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/);
113   mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/);
114   mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
115
116   /* --- Operations for binary fields only --- */
117
118   mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/);
119
120   /* --- Operations for prime fields only --- */
121
122   mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
123   mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
124   mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
125   mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
126
127 } field_ops;
128
129 #define F_TYPE(f)               (f)->ops->ty
130 #define F_NAME(f)               (f)->ops->name
131
132 #define F_DESTROY(f)            (f)->ops->destroy((f))
133 #define F_RAND(f, d, r)         (f)->ops->rand((f), (d), (r))
134
135 #define F_IN(f, d, x)           (f)->ops->in((f), (d), (x))
136 #define F_OUT(f, d, x)          (f)->ops->out((f), (d), (x))
137
138 #define F_ZEROP(f, x)           (f)->ops->zerop((f), (x))
139 #define F_NEG(f, d, x)          (f)->ops->neg((f), (d), (x))
140 #define F_ADD(f, d, x, y)       (f)->ops->add((f), (d), (x), (y))
141 #define F_SUB(f, d, x, y)       (f)->ops->sub((f), (d), (x), (y))
142 #define F_MUL(f, d, x, y)       (f)->ops->mul((f), (d), (x), (y))
143 #define F_SQR(f, d, x)          (f)->ops->sqr((f), (d), (x))
144 #define F_INV(f, d, x)          (f)->ops->inv((f), (d), (x))
145 #define F_REDUCE(f, d, x)       (f)->ops->reduce((f), (d), (x))
146 #define F_SQRT(f, d, x)         (f)->ops->sqrt((f), (d), (x))
147
148 #define F_QUADSOLVE(f, d, x)    (f)->ops->quadsolve((f), (d), (x))
149
150 #define F_DBL(f, d, x)          (f)->ops->dbl((f), (d), (x))
151 #define F_TPL(f, d, x)          (f)->ops->tpl((f), (d), (x))
152 #define F_QDL(f, d, x)          (f)->ops->qdl((f), (d), (x))
153 #define F_HLV(f, d, x)          (f)->ops->hlv((f), (d), (x))
154
155 /*----- Helpful field operations ------------------------------------------*/
156
157 /* --- @field_id@ --- *
158  *
159  * Arguments:   @field *f@ = pointer to a field
160  *              @mp *d@ = a destination element
161  *              @mp *x@ = a source element
162  *
163  * Returns:     The result element.
164  *
165  * Use:         An identity operation which can be used if your field has no
166  *              internal representation.
167  */
168
169 extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
170
171 /*----- Creating fields ---------------------------------------------------*/
172
173 /* --- @field_prime@ --- *
174  *
175  * Arguments:   @mp *p@ = the characteristic of the field
176  *
177  * Returns:     A pointer to the field.
178  *
179  * Use:         Creates a field structure for a prime field of size %$p$%,
180  *              using Montgomery reduction for arithmetic.
181  */
182
183 extern field *field_prime(mp */*p*/);
184
185 /* --- @field_niceprime@ --- *
186  *
187  * Arguments:   @mp *p@ = the characteristic of the field
188  *
189  * Returns:     A pointer to the field.
190  *
191  * Use:         Creates a field structure for a prime field of size %$p$%,
192  *              using efficient reduction for nice primes.
193  */
194
195 extern field *field_niceprime(mp */*p*/);
196
197 /* --- @field_binpoly@ --- *
198  *
199  * Arguments:   @mp *p@ = an irreducible polynomial over %$\gf{2}$%
200  *
201  * Returns:     A pointer to the field.
202  *
203  * Use:         Creates a field structure for a binary field using naive
204  *              arithmetic.
205  */
206
207 extern field *field_binpoly(mp */*p*/);
208
209 /*----- That's all, folks -------------------------------------------------*/
210
211 #ifdef __cplusplus
212   }
213 #endif
214
215 #endif