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