3 * $Id: field.h,v 1.8 2004/03/27 17:54:11 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.8 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
36 * Revision 1.7 2004/03/27 00:04:46 mdw
37 * Implement efficient reduction for pleasant-looking primes.
39 * Revision 1.6 2004/03/23 15:19:32 mdw
40 * Test elliptic curves more thoroughly.
42 * Revision 1.5 2004/03/23 12:08:26 mdw
43 * Random field-element selection.
45 * Revision 1.4 2004/03/21 22:52:06 mdw
46 * Merge and close elliptic curve branch.
48 * Revision 1.3.4.2 2004/03/21 22:39:46 mdw
49 * Elliptic curves on binary fields work.
51 * Revision 1.3.4.1 2004/03/20 00:13:31 mdw
52 * Projective coordinates for prime curves
54 * Revision 1.3 2002/01/13 13:48:44 mdw
57 * Revision 1.2 2001/05/07 17:30:13 mdw
58 * Add an internal-representation no-op function.
60 * Revision 1.1 2001/04/29 18:12:33 mdw
65 #ifndef CATACOMB_FIELD_H
66 #define CATACOMB_FIELD_H
72 /*----- Header files ------------------------------------------------------*/
74 #ifndef CATACOMB_GRAND_H
82 #ifndef CATACOMB_QDPARSE_H
86 /*----- Data structures ---------------------------------------------------*/
88 typedef struct field
{
89 const struct field_ops
*ops
; /* Field operations */
90 mp
*zero
, *one
; /* Identities in the field */
91 mp
*m
; /* Modulus (prime and binary) */
92 unsigned long nbits
; /* Length of field element in bits */
93 size_t noctets
; /* Length of element in octets */
101 typedef struct field_ops
{
103 /* --- General information --- */
105 unsigned ty
; /* What kind of field this is */
106 const char *name
; /* Human-readable name string */
108 /* --- Universal operations --- */
110 void (*destroy
)(field */
*f*/
);
111 mp
*(*rand
)(field */
*f*/
, mp */
*d*/
, grand */
*r*/
);
113 mp
*(*in
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
114 mp
*(*out
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
116 int (*zerop
)(field */
*f*/
, mp */
*x*/
);
117 mp
*(*neg
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
118 mp
*(*add
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
, mp */
*y*/
);
119 mp
*(*sub
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
, mp */
*y*/
);
120 mp
*(*mul
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
, mp */
*y*/
);
121 mp
*(*sqr
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
122 mp
*(*inv
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
123 mp
*(*reduce
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
124 mp
*(*sqrt
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
126 /* --- Operations for binary fields only --- */
128 mp
*(*quadsolve
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
130 /* --- Operations for prime fields only --- */
132 mp
*(*dbl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
133 mp
*(*tpl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
134 mp
*(*qdl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
135 mp
*(*hlv
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
139 #define F_TYPE(f) (f)->ops->ty
140 #define F_NAME(f) (f)->ops->name
142 #define F_DESTROY(f) (f)->ops->destroy((f))
143 #define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
145 #define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
146 #define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
148 #define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
149 #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
150 #define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
151 #define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
152 #define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
153 #define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
154 #define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
155 #define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
156 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
158 #define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
160 #define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
161 #define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
162 #define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x))
163 #define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x))
165 /*----- Helpful field operations ------------------------------------------*/
167 /* --- @field_id@ --- *
169 * Arguments: @field *f@ = pointer to a field
170 * @mp *d@ = a destination element
171 * @mp *x@ = a source element
173 * Returns: The result element.
175 * Use: An identity operation which can be used if your field has no
176 * internal representation.
179 extern mp
*field_id(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
181 /*----- Creating fields ---------------------------------------------------*/
183 /* --- @field_prime@ --- *
185 * Arguments: @mp *p@ = the characteristic of the field
187 * Returns: A pointer to the field.
189 * Use: Creates a field structure for a prime field of size %$p$%,
190 * using Montgomery reduction for arithmetic.
193 extern field
*field_prime(mp */
*p*/
);
195 /* --- @field_niceprime@ --- *
197 * Arguments: @mp *p@ = the characteristic of the field
199 * Returns: A pointer to the field.
201 * Use: Creates a field structure for a prime field of size %$p$%,
202 * using efficient reduction for nice primes.
205 extern field
*field_niceprime(mp */
*p*/
);
207 /* --- @field_binpoly@ --- *
209 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
211 * Returns: A pointer to the field.
213 * Use: Creates a field structure for a binary field using naive
217 extern field
*field_binpoly(mp */
*p*/
);
219 /* --- @field_parse@ --- *
221 * Arguments: @qd_parse *qd@ = parser context
223 * Returns: Field pointer if OK, or null.
225 * Use: Parses a field description, which has the form
227 * * `prime', `niceprime' or `binpoly'
229 * * the field modulus
232 extern field
*field_parse(qd_parse */
*qd*/
);
234 /*----- That's all, folks -------------------------------------------------*/