Implement efficient reduction for pleasant-looking primes.
[u/mdw/catacomb] / field.h
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
f46efa79 3 * $Id: field.h,v 1.7 2004/03/27 00:04:46 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
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: field.h,v $
f46efa79 33 * Revision 1.7 2004/03/27 00:04:46 mdw
34 * Implement efficient reduction for pleasant-looking primes.
35 *
bc985cef 36 * Revision 1.6 2004/03/23 15:19:32 mdw
37 * Test elliptic curves more thoroughly.
38 *
9b8b6877 39 * Revision 1.5 2004/03/23 12:08:26 mdw
40 * Random field-element selection.
41 *
c3caa2fa 42 * Revision 1.4 2004/03/21 22:52:06 mdw
43 * Merge and close elliptic curve branch.
44 *
ceb3f0c0 45 * Revision 1.3.4.2 2004/03/21 22:39:46 mdw
46 * Elliptic curves on binary fields work.
47 *
8823192f 48 * Revision 1.3.4.1 2004/03/20 00:13:31 mdw
49 * Projective coordinates for prime curves
50 *
b085fd91 51 * Revision 1.3 2002/01/13 13:48:44 mdw
52 * Further progress.
53 *
2685767a 54 * Revision 1.2 2001/05/07 17:30:13 mdw
55 * Add an internal-representation no-op function.
56 *
b0ab12e6 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
9b8b6877 71#ifndef CATACOMB_GRAND_H
72# include "grand.h"
73#endif
74
b0ab12e6 75#ifndef CATACOMB_MP_H
76# include "mp.h"
77#endif
78
79/*----- Data structures ---------------------------------------------------*/
80
81typedef struct field {
82 const struct field_ops *ops; /* Field operations */
83 mp *zero, *one; /* Identities in the field */
84} field;
85
bc985cef 86enum {
87 FTY_PRIME,
88 FTY_BINARY
89};
90
b0ab12e6 91typedef struct field_ops {
92
bc985cef 93 /* --- General information --- */
94
95 unsigned ty; /* What kind of field this is */
96 const char *name; /* Human-readable name string */
97
b0ab12e6 98 /* --- Universal operations --- */
99
100 void (*destroy)(field */*f*/);
9b8b6877 101 mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/);
b0ab12e6 102
103 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
104 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
105
8823192f 106 int (*zerop)(field */*f*/, mp */*x*/);
b085fd91 107 mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 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*/);
ceb3f0c0 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*/);
b0ab12e6 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*/);
8823192f 124 mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
125 mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
b0ab12e6 126
127} field_ops;
128
bc985cef 129#define F_TYPE(f) (f)->ops->ty
130#define F_NAME(f) (f)->ops->name
131
b0ab12e6 132#define F_DESTROY(f) (f)->ops->destroy((f))
bc985cef 133#define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
b0ab12e6 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))
8823192f 137
138#define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
b085fd91 139#define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
b0ab12e6 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))
ceb3f0c0 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))
b0ab12e6 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))
8823192f 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))
b0ab12e6 154
2685767a 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
169extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
170
b0ab12e6 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
183extern field *field_prime(mp */*p*/);
184
f46efa79 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
195extern field *field_niceprime(mp */*p*/);
196
b0ab12e6 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
207extern field *field_binpoly(mp */*p*/);
208
b0ab12e6 209/*----- That's all, folks -------------------------------------------------*/
210
211#ifdef __cplusplus
212 }
213#endif
214
215#endif