3 * $Id: field.h,v 1.6 2004/03/23 15:19:32 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.6 2004/03/23 15:19:32 mdw
34 * Test elliptic curves more thoroughly.
36 * Revision 1.5 2004/03/23 12:08:26 mdw
37 * Random field-element selection.
39 * Revision 1.4 2004/03/21 22:52:06 mdw
40 * Merge and close elliptic curve branch.
42 * Revision 1.3.4.2 2004/03/21 22:39:46 mdw
43 * Elliptic curves on binary fields work.
45 * Revision 1.3.4.1 2004/03/20 00:13:31 mdw
46 * Projective coordinates for prime curves
48 * Revision 1.3 2002/01/13 13:48:44 mdw
51 * Revision 1.2 2001/05/07 17:30:13 mdw
52 * Add an internal-representation no-op function.
54 * Revision 1.1 2001/04/29 18:12:33 mdw
59 #ifndef CATACOMB_FIELD_H
60 #define CATACOMB_FIELD_H
66 /*----- Header files ------------------------------------------------------*/
68 #ifndef CATACOMB_GRAND_H
76 /*----- Data structures ---------------------------------------------------*/
78 typedef struct field
{
79 const struct field_ops
*ops
; /* Field operations */
80 mp
*zero
, *one
; /* Identities in the field */
88 typedef struct field_ops
{
90 /* --- General information --- */
92 unsigned ty
; /* What kind of field this is */
93 const char *name
; /* Human-readable name string */
95 /* --- Universal operations --- */
97 void (*destroy
)(field */
*f*/
);
98 mp
*(*rand
)(field */
*f*/
, mp */
*d*/
, grand */
*r*/
);
100 mp
*(*in
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
101 mp
*(*out
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
103 int (*zerop
)(field */
*f*/
, mp */
*x*/
);
104 mp
*(*neg
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
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*/
);
111 mp
*(*sqrt
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
113 /* --- Operations for binary fields only --- */
115 mp
*(*quadsolve
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
117 /* --- Operations for prime fields only --- */
119 mp
*(*dbl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
120 mp
*(*tpl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
121 mp
*(*qdl
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
122 mp
*(*hlv
)(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
126 #define F_TYPE(f) (f)->ops->ty
127 #define F_NAME(f) (f)->ops->name
129 #define F_DESTROY(f) (f)->ops->destroy((f))
130 #define F_RAND(f, d, r) (f)->ops->rand((f), (d), (r))
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))
135 #define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
136 #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
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))
143 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
145 #define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x))
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))
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))
152 /*----- Helpful field operations ------------------------------------------*/
154 /* --- @field_id@ --- *
156 * Arguments: @field *f@ = pointer to a field
157 * @mp *d@ = a destination element
158 * @mp *x@ = a source element
160 * Returns: The result element.
162 * Use: An identity operation which can be used if your field has no
163 * internal representation.
166 extern mp
*field_id(field */
*f*/
, mp */
*d*/
, mp */
*x*/
);
168 /*----- Creating fields ---------------------------------------------------*/
170 /* --- @field_prime@ --- *
172 * Arguments: @mp *p@ = the characteristic of the field
174 * Returns: A pointer to the field.
176 * Use: Creates a field structure for a prime field of size %$p$%,
177 * using Montgomery reduction for arithmetic.
180 extern field
*field_prime(mp */
*p*/
);
182 /* --- @field_binpoly@ --- *
184 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
186 * Returns: A pointer to the field.
188 * Use: Creates a field structure for a binary field using naive
192 extern field
*field_binpoly(mp */
*p*/
);
194 /*----- That's all, folks -------------------------------------------------*/