Projective coordinates for prime curves
[u/mdw/catacomb] / field.h
1 /* -*-c-*-
2 *
3 * $Id: field.h,v 1.3.4.1 2004/03/20 00:13:31 mdw Exp $
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 $
33 * Revision 1.3.4.1 2004/03/20 00:13:31 mdw
34 * Projective coordinates for prime curves
35 *
36 * Revision 1.3 2002/01/13 13:48:44 mdw
37 * Further progress.
38 *
39 * Revision 1.2 2001/05/07 17:30:13 mdw
40 * Add an internal-representation no-op function.
41 *
42 * Revision 1.1 2001/04/29 18:12:33 mdw
43 * Prototype version.
44 *
45 */
46
47 #ifndef CATACOMB_FIELD_H
48 #define CATACOMB_FIELD_H
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 /*----- Header files ------------------------------------------------------*/
55
56 #ifndef CATACOMB_MP_H
57 # include "mp.h"
58 #endif
59
60 /*----- Data structures ---------------------------------------------------*/
61
62 typedef struct field {
63 const struct field_ops *ops; /* Field operations */
64 mp *zero, *one; /* Identities in the field */
65 } field;
66
67 typedef struct field_ops {
68
69 /* --- Universal operations --- */
70
71 void (*destroy)(field */*f*/);
72
73 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
74 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
75
76 int (*zerop)(field */*f*/, mp */*x*/);
77 mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/);
78 mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
79 mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
80 mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
81 mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/);
82 mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/);
83 mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/);
84
85 /* --- Operations for prime fields only --- */
86
87 mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
88 mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
89 mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/);
90 mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/);
91 mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
92
93 } field_ops;
94
95 #define F_DESTROY(f) (f)->ops->destroy((f))
96
97 #define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
98 #define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
99
100 #define F_ZEROP(f, x) (f)->ops->zerop((f), (x))
101 #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x))
102 #define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
103 #define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
104 #define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
105 #define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
106 #define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
107 #define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
108
109 #define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
110 #define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
111 #define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x))
112 #define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x))
113 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
114
115 /*----- Helpful field operations ------------------------------------------*/
116
117 /* --- @field_id@ --- *
118 *
119 * Arguments: @field *f@ = pointer to a field
120 * @mp *d@ = a destination element
121 * @mp *x@ = a source element
122 *
123 * Returns: The result element.
124 *
125 * Use: An identity operation which can be used if your field has no
126 * internal representation.
127 */
128
129 extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/);
130
131 /*----- Creating fields ---------------------------------------------------*/
132
133 /* --- @field_prime@ --- *
134 *
135 * Arguments: @mp *p@ = the characteristic of the field
136 *
137 * Returns: A pointer to the field.
138 *
139 * Use: Creates a field structure for a prime field of size %$p$%,
140 * using Montgomery reduction for arithmetic.
141 */
142
143 extern field *field_prime(mp */*p*/);
144
145 /* --- @field_binpoly@ --- *
146 *
147 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
148 *
149 * Returns: A pointer to the field.
150 *
151 * Use: Creates a field structure for a binary field using naive
152 * arithmetic.
153 */
154
155 extern field *field_binpoly(mp */*p*/);
156
157 /* --- @field_binsparsepoly@ --- *
158 *
159 * Arguments: @unsigned deg@ = the degree of the field polynomial
160 * @const unsigned *c@ = degrees of nonzero coefficients
161 *
162 * Returns: A pointer to the field.
163 *
164 * Use: Creates a field structure for a binary field taking advantage
165 * of the sparseness of the given polynomial to improve
166 * reduction performance.
167 */
168
169 extern field *field_binsparsepoly(unsigned /*deg*/, const unsigned */*c*/);
170
171 /*----- That's all, folks -------------------------------------------------*/
172
173 #ifdef __cplusplus
174 }
175 #endif
176
177 #endif