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