3 * $Id: field.h,v 1.10 2004/04/01 21:28:41 mdw Exp $
5 * Definitions for field arithmetic
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.10 2004/04/01 21:28:41 mdw
34 * Normal basis support (translates to poly basis internally). Rewrite
35 * EC and prime group table generators in awk, so that they can reuse data
36 * for repeated constants.
38 * Revision 1.9 2004/04/01 12:50:09 mdw
39 * Add cyclic group abstraction, with test code. Separate off exponentation
40 * functions for better static linking. Fix a buttload of bugs on the way.
41 * Generally ensure that negative exponents do inversion correctly. Add
42 * table of standard prime-field subgroups. (Binary field subgroups are
43 * currently unimplemented but easy to add if anyone ever finds a good one.)
45 * Revision 1.8 2004/03/27 17:54:11 mdw
46 * Standard curves and curve checking.
48 * Revision 1.7 2004/03/27 00:04:46 mdw
49 * Implement efficient reduction for pleasant-looking primes.
51 * Revision 1.6 2004/03/23 15:19:32 mdw
52 * Test elliptic curves more thoroughly.
54 * Revision 1.5 2004/03/23 12:08:26 mdw
55 * Random field-element selection.
57 * Revision 1.4 2004/03/21 22:52:06 mdw
58 * Merge and close elliptic curve branch.
60 * Revision 1.3.4.2 2004/03/21 22:39:46 mdw
61 * Elliptic curves on binary fields work.
63 * Revision 1.3.4.1 2004/03/20 00:13:31 mdw
64 * Projective coordinates for prime curves
66 * Revision 1.3 2002/01/13 13:48:44 mdw
69 * Revision 1.2 2001/05/07 17:30:13 mdw
70 * Add an internal-representation no-op function.
72 * Revision 1.1 2001/04/29 18:12:33 mdw
77 #ifndef CATACOMB_FIELD_H
78 #define CATACOMB_FIELD_H
84 /*----- Header files ------------------------------------------------------*/
86 #ifndef CATACOMB_GRAND_H
94 #ifndef CATACOMB_QDPARSE_H
98 /*----- Data structures ---------------------------------------------------*/
100 typedef struct field
{
101 const struct field_ops
*ops
; /* Field operations */
102 mp
*zero
, *one
; /* Identities in the field */
103 mp
*m
; /* Modulus (prime and binary) */
104 unsigned long nbits
; /* Length of field element in bits */
105 size_t noctets
; /* Length of element in octets */
113 typedef struct field_ops
{
115 /* --- General information --- */
117 unsigned ty
; /* What kind of field this is */
118 const char *name
; /* Human-readable name string */
120 /* --- Universal operations --- */
122 void (*destroy
)(field */
*f*/
);
123 mp
*(*rand
)(field */
*f*/
, mp */
*d*/
, grand */
*r*/
);
124 int (*samep
)(field */
*f*/
, field */
*g*/
);
126 mp
*(*in
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
127 mp
*(*out
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
129 int (*zerop
)(field */
*f*/
, mp */
*x*/
);
130 mp
*(*neg
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
131 mp
*(*add
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
, mp */
*y*/
);
132 mp
*(*sub
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
, mp */
*y*/
);
133 mp
*(*mul
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
, mp */
*y*/
);
134 mp
*(*sqr
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
135 mp
*(*inv
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
136 mp
*(*reduce
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
137 mp
*(*sqrt
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
139 /* --- Operations for binary fields only --- */
141 mp
*(*quadsolve
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
143 /* --- Operations for prime fields only --- */
145 mp
*(*dbl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
146 mp
*(*tpl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
147 mp
*(*qdl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
148 mp
*(*hlv
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
152 #define F_TYPE(f) (f)->ops->ty
153 #define F_NAME(f) (f)->ops->name
155 #define F_DESTROY(f) (f)->ops->destroy((f))
156 #define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
157 #define F_SAMEP(f, g) (f)->ops->samep((f), (g))
159 #define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
160 #define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
162 #define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
163 #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
164 #define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
165 #define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
166 #define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
167 #define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
168 #define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
169 #define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
170 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
172 #define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
174 #define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
175 #define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
176 #define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x))
177 #define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x))
179 /*----- Helpful field operations ------------------------------------------*/
181 /* --- @field_id@ --- *
183 * Arguments: @field *f@ = pointer to a field
184 * @mp *d@ = a destination element
185 * @mp *x@ = a source element
187 * Returns: The result element.
189 * Use: An identity operation which can be used if your field has no
190 * internal representation.
193 extern mp
*field_id(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
195 /* --- @field_samep@ --- *
197 * Arguments: @field *f, *g@ = two fields
199 * Returns: Nonzero if the fields are identical (not just isomorphic).
201 * Use: Checks for sameness of fields. This function does the full
202 * check, not just the field-type-specific check done by the
203 * @sampep@ field operation.
206 extern int field_samep(field */
*f*/
, field */
*g*/
);
208 /* --- @field_stdsamep@ --- *
210 * Arguments: @field *f, *g@ = two fields
212 * Returns: Nonzero if the fields are identical (not just isomorphic).
214 * Use: Standard sameness check, based on equality of the @m@
218 extern int field_stdsamep(field */
*f*/
, field */
*g*/
);
220 /*----- Creating fields ---------------------------------------------------*/
222 /* --- @field_prime@ --- *
224 * Arguments: @mp *p@ = the characteristic of the field
226 * Returns: A pointer to the field.
228 * Use: Creates a field structure for a prime field of size %$p$%,
229 * using Montgomery reduction for arithmetic.
232 extern field
*field_prime(mp */
*p*/
);
234 /* --- @field_niceprime@ --- *
236 * Arguments: @mp *p@ = the characteristic of the field
238 * Returns: A pointer to the field.
240 * Use: Creates a field structure for a prime field of size %$p$%,
241 * using efficient reduction for nice primes.
244 extern field
*field_niceprime(mp */
*p*/
);
246 /* --- @field_binpoly@ --- *
248 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
250 * Returns: A pointer to the field.
252 * Use: Creates a field structure for a binary field using naive
256 extern field
*field_binpoly(mp */
*p*/
);
258 /* --- @field_binnorm@ --- *
260 * Arguments: @mp *p@ = the reduction polynomial
261 * @mp *beta@ = representation of normal point
263 * Returns: A pointer to the field.
265 * Use: Creates a field structure for a binary field mod @p@ which
266 * uses a normal basis representation externally. Computations
267 * are still done on a polynomial-basis representation.
270 extern field
*field_binnorm(mp */
*p*/
, mp */
*beta*/
);
272 /* --- @field_parse@ --- *
274 * Arguments: @qd_parse *qd@ = parser context
276 * Returns: Field pointer if OK, or null.
278 * Use: Parses a field description, which has the form
280 * * `prime', `niceprime', `binpoly', or `binnorm'
282 * * the field modulus
283 * * for `binnorm', an optional `,' and the beta value
286 extern field
*field_parse(qd_parse */
*qd*/
);
288 /*----- That's all, folks -------------------------------------------------*/