35e9df07b4a79fff07f15ec6c5edb4b7d71f75df
[u/mdw/catacomb] / field.h
1 /* -*-c-*-
2 *
3 * $Id: field.h,v 1.1 2001/04/29 18:12:33 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.1 2001/04/29 18:12:33 mdw
34 * Prototype version.
35 *
36 */
37
38 #ifndef CATACOMB_FIELD_H
39 #define CATACOMB_FIELD_H
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /*----- Header files ------------------------------------------------------*/
46
47 #ifndef CATACOMB_MP_H
48 # include "mp.h"
49 #endif
50
51 /*----- Data structures ---------------------------------------------------*/
52
53 typedef struct field {
54 const struct field_ops *ops; /* Field operations */
55 mp *zero, *one; /* Identities in the field */
56 } field;
57
58 typedef struct field_ops {
59
60 /* --- Universal operations --- */
61
62 void (*destroy)(field */*f*/);
63
64 mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/);
65 mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/);
66
67 mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
68 mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
69 mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/);
70 mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/);
71 mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/);
72 mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/);
73
74 /* --- Operations for prime fields only --- */
75
76 mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/);
77 mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/);
78 mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/);
79
80 } field_ops;
81
82 #define F_DESTROY(f) (f)->ops->destroy((f))
83
84 #define F_IN(f, d, x) (f)->ops->in((f), (d), (x))
85 #define F_OUT(f, d, x) (f)->ops->out((f), (d), (x))
86 #define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y))
87 #define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y))
88 #define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y))
89 #define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x))
90 #define F_INV(f, d, x) (f)->ops->inv((f), (d), (x))
91 #define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x))
92
93 #define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x))
94 #define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x))
95 #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x))
96
97 /*----- Creating fields ---------------------------------------------------*/
98
99 /* --- @field_prime@ --- *
100 *
101 * Arguments: @mp *p@ = the characteristic of the field
102 *
103 * Returns: A pointer to the field.
104 *
105 * Use: Creates a field structure for a prime field of size %$p$%,
106 * using Montgomery reduction for arithmetic.
107 */
108
109 extern field *field_prime(mp */*p*/);
110
111 /* --- @field_binpoly@ --- *
112 *
113 * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$%
114 *
115 * Returns: A pointer to the field.
116 *
117 * Use: Creates a field structure for a binary field using naive
118 * arithmetic.
119 */
120
121 extern field *field_binpoly(mp */*p*/);
122
123 /* --- @field_binsparsepoly@ --- *
124 *
125 * Arguments: @unsigned deg@ = the degree of the field polynomial
126 * @const unsigned *c@ = degrees of nonzero coefficients
127 *
128 * Returns: A pointer to the field.
129 *
130 * Use: Creates a field structure for a binary field taking advantage
131 * of the sparseness of the given polynomial to improve
132 * reduction performance.
133 */
134
135 extern field *field_binsparsepoly(unsigned /*deg*/, const unsigned */*c*/);
136
137 /*----- That's all, folks -------------------------------------------------*/
138
139 #ifdef __cplusplus
140 }
141 #endif
142
143 #endif