chiark / gitweb /
Fix various assumptions about mpw sizes.
[catacomb] / group.h
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * General cyclic group abstraction
6  *
7  * (c) 2004 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 #ifndef CATACOMB_GROUP_H
31 #define CATACOMB_GROUP_H
32
33 #ifdef __cplusplus
34   extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <mLib/dstr.h>
40
41 #ifndef CATACOMB_BUF_H
42 #  include "buf.h"
43 #endif
44
45 #ifndef CATACOMB_DH_H
46 #  include "ec.h"
47 #endif
48
49 #ifndef CATACOMB_GRAND_H
50 #  include "grand.h"
51 #endif
52
53 #ifndef CATACOMB_MP_H
54 #  include "mp.h"
55 #endif
56
57 #ifndef CATACOMB_QDPARSE_H
58 #  include "qdparse.h"
59 #endif
60
61 /*----- Data structures ---------------------------------------------------*/
62
63 #ifndef ge
64   typedef struct ge ge;                 /* Group element (abstract type) */
65 #endif
66
67 typedef struct group_ {
68   const struct group_ops *ops;          /* Operations table */
69   size_t nbits;                         /* Size of an element in bits */
70   size_t noctets;                       /* Size of raw element in octets */
71   ge *i;                                /* Identity element */
72   ge *g;                                /* Generator element */
73   mp *r;                                /* Order of the generator */
74   mp *h;                                /* Cofactor */
75 } group;
76
77 typedef struct group_expfactor {
78   ge *base;                             /* The base */
79   mp *exp;                              /* The exponent */
80 } group_expfactor;
81
82 typedef struct group_ops {
83
84   /* --- General information --- */
85
86   unsigned ty;                          /* Type of this group */
87   const char *name;                     /* Textual name string */
88
89   /* --- Memory management --- */
90
91   void (*destroygroup)(group */*g*/);
92   ge *(*create)(group */*g*/);
93   void (*copy)(group */*g*/, ge */*d*/, ge */*x*/);
94   void (*burn)(group */*g*/, ge */*x*/);
95   void (*destroy)(group */*g*/, ge */*e*/);
96
97   /* --- Comparisons --- */
98
99   int (*samep)(group */*g*/, group */*h*/);
100   int (*eq)(group */*g*/, ge */*x*/, ge */*y*/);
101   int (*identp)(group */*g*/, ge */*x*/);
102
103   /* --- Other stuff --- */
104
105   const char *(*check)(group */*g*/, grand */*gr*/);
106
107   /* --- Arithmetic --- */
108
109   void (*mul)(group */*g*/, ge */*d*/, ge */*x*/, ge */*y*/);
110   void (*sqr)(group */*g*/, ge */*d*/, ge */*x*/);
111   void (*inv)(group */*g*/, ge */*d*/, ge */*x*/);
112   void (*div)(group */*g*/, ge */*d*/, ge */*x*/, ge */*y*/);
113   void (*exp)(group */*g*/, ge */*d*/, ge */*x*/, mp */*n*/);
114   void (*mexp)(group */*g*/, ge */*d*/,
115                const group_expfactor */*f*/, size_t /*n*/);
116
117   /* --- Debugging --- */
118
119   int (*read)(group */*g*/, ge */*d*/,
120               const mptext_ops */*ops*/, void */*p*/);
121   int (*write)(group */*g*/, ge */*x*/,
122                const mptext_ops */*ops*/, void */*p*/);
123
124   /* --- Conversions --- */
125
126   mp *(*toint)(group */*g*/, mp */*d*/, ge */*x*/);
127   int (*fromint)(group */*g*/, ge */*d*/, mp */*x*/);
128   int (*toec)(group */*g*/, ec */*d*/, ge */*x*/);
129   int (*fromec)(group */*g*/, ge */*d*/, const ec */*p*/);
130   int (*tobuf)(group */*h*/, buf */*b*/, ge */*x*/);
131   int (*frombuf)(group */*h*/, buf */*b*/, ge */*d*/);
132   int (*toraw)(group */*h*/, buf */*b*/, ge */*x*/);
133   int (*fromraw)(group */*h*/, buf */*b*/, ge */*d*/);
134
135 } group_ops;
136
137 enum {
138   GTY_PRIME,                            /* Prime field subgroup */
139   GTY_BINARY,                           /* Binary feld subgroup */
140   GTY_EC                                /* Elliptic curve group */
141 };
142
143 #define G_NAME(g)               (g)->ops->name
144 #define G_TYPE(g)               (g)->ops->ty
145
146 #define G_DESTROYGROUP(g)       (g)->ops->destroygroup((g))
147 #define G_CREATE(g)             (g)->ops->create((g))
148 #define G_COPY(g, d, x)         (g)->ops->copy((g), (d), (x))
149 #define G_BURN(g, x)            (g)->ops->burn((g), (x))
150 #define G_DESTROY(g, x)         (g)->ops->destroy((g), (x))
151
152 #define G_SAMEP(g, h)           (g)->ops->samep((g), (h))
153 #define G_EQ(g, x, y)           (g)->ops->eq((g), (x), (y))
154 #define G_IDENTP(g, x)          (g)->ops->identp((g), (x))
155
156 #define G_CHECK(g, gr)          (g)->ops->check((g), (gr))
157
158 #define G_MUL(g, d, x, y)       (g)->ops->mul((g), (d), (x), (y))
159 #define G_SQR(g, d, x)          (g)->ops->sqr((g), (d), (x))
160 #define G_INV(g, d, x)          (g)->ops->inv((g), (d), (x))
161 #define G_DIV(g, d, x, y)       (g)->ops->div((g), (d), (x), (y))
162 #define G_EXP(g, d, x, n)       (g)->ops->exp((g), (d), (x), (n))
163 #define G_MEXP(g, d, f, n)      (g)->ops->mexp((g), (d), (f), (n))
164
165 #define G_READ(g, d, o, p)      (g)->ops->read((g), (d), (o), (p))
166 #define G_WRITE(g, x, o, p)     (g)->ops->write((g), (x), (o), (p))
167
168 #define G_TOINT(g, d, x)        (g)->ops->toint((g), (d), (x))
169 #define G_FROMINT(g, d, x)      (g)->ops->fromint((g), (d), (x))
170 #define G_TOEC(g, d, x)         (g)->ops->toec((g), (d), (x))
171 #define G_FROMEC(g, d, p)       (g)->ops->fromec((g), (d), (p))
172 #define G_TOBUF(g, b, x)        (g)->ops->tobuf((g), (b), (x))
173 #define G_FROMBUF(g, b, d)      (g)->ops->frombuf((g), (b), (d))
174 #define G_TORAW(g, b, x)        (g)->ops->toraw((g), (b), (x))
175 #define G_FROMRAW(g, b, d)      (g)->ops->fromraw((g), (b), (d))
176
177 /*----- Handy functions ---------------------------------------------------*/
178
179 /* --- @group_check@ --- *
180  *
181  * Arguments:   @group *g@ = an abstract group
182  *              @ge *x@ = a group element
183  *
184  * Returns:     Zero on success, nonzero for failure.
185  *
186  * Use:         Checks that @x@ is a valid group element.  This may take a
187  *              while, since it checks that %$x^h \ne 1$%.
188  */
189
190 extern int group_check(group */*g*/, ge */*x*/);
191
192 /* --- @group_samep@ --- *
193  *
194  * Arguments:   @group *g, *h@ = two abstract groups
195  *
196  * Returns:     Nonzero if the groups are in fact identical (not just
197  *              isomorphic).
198  *
199  * Use:         Checks to see whether two groups are actually the same.  This
200  *              function does the full check: the group operatrion @samep@
201  *              just does the group-specific details.
202  */
203
204 extern int group_samep(group */*g*/, group */*h*/);
205
206 /*----- Textual I/O on group elements -------------------------------------*/
207
208 extern int group_readstring(group */*g*/, ge */*d*/,
209                             const char */*p*/, char **/*end*/);
210 extern int group_writestring(group */*g*/, ge */*d*/,
211                              char */*p*/, size_t /*sz*/);
212
213 extern int group_readfile(group */*g*/, ge */*d*/, FILE */*fp*/);
214 extern int group_writefile(group */*g*/, ge */*x*/, FILE */*fp*/);
215
216 extern int group_readdstr(group */*g*/, ge */*d*/,
217                           dstr */*dd*/, size_t */*off*/);
218 extern int group_writedstr(group */*g*/, ge */*x*/, dstr */*d*/);
219
220 /*----- Standard implementations ------------------------------------------*/
221
222 /* --- @group_stdidentp@ --- *
223  *
224  * Arguments:   @group *g@ = abstract group
225  *              @ge *x@ = group element
226  *
227  * Returns:     Nonzero if %$x$% is the group identity.
228  */
229
230 extern int group_stdidentp(group */*g*/, ge */*x*/);
231
232 /* --- @group_stdcheck@ --- *
233  *
234  * Arguments:   @group *g@ = abstract group
235  *              @grand *gr@ = random number source.
236  *
237  * Returns:     Null on success, or a pointer to an error message.
238  */
239
240 extern const char *group_stdcheck(group */*g*/, grand */*gr*/);
241
242 /* --- @group_stdsqr@ --- *
243  *
244  * Arguments:   @group *g@ = abstract group
245  *              @ge *d@ = destination pointer
246  *              @ge *x@ = group element
247  *
248  * Returns:     ---
249  *
250  * Use:         Computes %$d = x^2$% as %$d = x x$%.
251  */
252
253 extern void group_stdsqr(group */*g*/, ge */*d*/, ge */*x*/);
254
255 /* --- @group_stddiv@ --- *
256  *
257  * Arguments:   @group *g@ = abstract group
258  *              @ge *d@ = destination pointer
259  *              @ge *x@ = dividend
260  *              @ge *y@ = divisor
261  *
262  * Returns:     ---
263  *
264  * Use:         Computes %$d = x/y$% as %$d = x y^{-1}$%.
265  */
266
267 extern void group_stddiv(group */*g*/, ge */*d*/, ge */*x*/, ge */*y*/);
268
269 /* --- @group_stdexp@ --- *
270  *
271  * Arguments:   @group *g@ = abstract group
272  *              @ge *d@ = destination pointer
273  *              @ge *x@ = base element
274  *              @mp *n@ = exponent
275  *
276  * Returns:     ---
277  *
278  * Use:         Computes %$d = x^n$% efficiently.
279  */
280
281 extern void group_stdexp(group */*g*/, ge */*d*/, ge */*x*/, mp */*n*/);
282
283 /* --- @group_stdmexp@ --- *
284  *
285  * Arguments:   @group *g@ = abstract group
286  *              @ge *d@ = destination pointer
287  *              @const group_expfactor *f@ = vector of factors
288  *              @size_t n@ = number of factors
289  *
290  * Returns:     ---
291  *
292  * Use:         Computes %$d = g_0^{x_0} g_1^{x_1} \ldots$% efficiently.
293  */
294
295 extern void group_stdmexp(group */*g*/, ge */*d*/,
296                           const group_expfactor */*f*/, size_t /*n*/);
297
298 /* --- @group_stdtoec@ --- *
299  *
300  * Arguments:   @group *g@ = abstract group
301  *              @ec *d@ = destination point
302  *              @ge *x@ = group element
303  *
304  * Returns:     @-1@, indicating failure.
305  *
306  * Use:         Fails to convert a group element to an elliptic curve point.
307  */
308
309 extern int group_stdtoec(group */*g*/, ec */*d*/, ge */*x*/);
310
311 /* --- @group_stdfromec@ --- *
312  *
313  * Arguments:   @group *g@ = abstract group
314  *              @ge *d@ = destination pointer
315  *              @const ec *p@ = elliptic curve point
316  *
317  * Returns:     Zero for success, @-1@ on failure.
318  *
319  * Use:         Converts %$p$% to a group element by converting its %$x$%-
320  *              coordinate.
321  */
322
323 extern int group_stdfromec(group */*g*/, ge */*d*/, const ec */*p*/);
324
325 /*----- Prime field subgroups ---------------------------------------------*/
326
327 typedef struct gprime_param {
328   mp *p, *q;                            /* Prime numbers %$p$% and %$q$% */
329   mp *g;                                /* Generates order-%$q$% subgroup */
330 } gprime_param;
331
332 /* --- @group_prime@ --- *
333  *
334  * Arguments:   @const gprime_param *gp@ = group parameters
335  *
336  * Returns:     A pointer to the group, or null.
337  *
338  * Use:         Constructs an abstract group interface for a subgroup of a
339  *              prime field.  Group elements are @mp *@ pointers.
340  */
341
342 group *group_prime(const gprime_param */*gp*/);
343
344 /*----- Binary field subgroups --------------------------------------------*/
345
346 typedef gprime_param gbin_param;
347
348 /* --- @group_binary@ --- *
349  *
350  * Arguments:   @const gbin_param *gb@ = group parameters
351  *
352  * Returns:     A pointer to the group, or null.
353  *
354  * Use:         Constructs an abstract group interface for a subgroup of a
355  *              prime field.  Group elements are @mp *@ pointers.
356  */
357
358 group *group_binary(const gbin_param */*gp*/);
359
360 /*----- Elliptic curve groups ---------------------------------------------*/
361
362 /* --- @group_ec@ --- *
363  *
364  * Arguments:   @const ec_info *ei@ = elliptic curve parameters
365  *
366  * Returns:     A pointer to the group, or null.
367  *
368  * Use:         Constructs an abstract group interface for an elliptic curve
369  *              group.  Group elements are @ec@ structures.  The contents of
370  *              the @ec_info@ structure becomes the property of the @group@
371  *              object; you can (and should) free the structure itself, but
372  *              calling @ec_freeinfo@ on it is not allowed.
373  */
374
375 group *group_ec(const ec_info */*ei*/);
376
377 /*----- General group initialization --------------------------------------*/
378
379 /* --- @group_parse@ --- *
380  *
381  * Arguments:   @qd_parse *qd@ = quick-and-dirty parser
382  *
383  * Returns:     Group pointer, or null for failure.
384  *
385  * Use:         Parses a group description and returns the group.  This has
386  *              the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and
387  *              SPEC is the appropriate kind of group specification of the
388  *              given type.
389  */
390
391 extern group *group_parse(qd_parse */*qd*/);
392
393 /* --- @group_fromstring@ --- *
394  *
395  * Arguments:   @const char *p@ = pointer to string to read
396  *              @group **gg@ = where to put the group pointer
397  *
398  * Returns:     Null if OK, or an error string.
399  *
400  * Use:         Parses a group spec from a string, and returns the group.
401  */
402
403 extern const char *group_fromstring(const char */*p*/, group **/*gg*/);
404
405 /*----- That's all, folks -------------------------------------------------*/
406
407 #ifdef __cplusplus
408   }
409 #endif
410
411 #endif