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