Gather up another utility.
[u/mdw/catacomb] / group-stdops.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
5c3f75ec 3 * $Id: group-stdops.c,v 1.3 2004/04/17 09:58:37 mdw Exp $
34e4f738 4 *
5 * Standard group operations
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
34e4f738 30/*----- Header files ------------------------------------------------------*/
31
32#include "group.h"
33#include "pgen.h"
34
35/*----- Handy functions ---------------------------------------------------*/
36
37/* --- @group_check@ --- *
38 *
39 * Arguments: @group *g@ = an abstract group
40 * @ge *x@ = a group element
41 *
42 * Returns: Zero on success, nonzero for failure.
43 *
44 * Use: Checks that @x@ is a valid group element. This may take a
5c3f75ec 45 * while, since it checks that %$x \ne 1$% and %$x^r = 1$%.
34e4f738 46 */
47
48int group_check(group *g, ge *x)
49{
50 ge *d = G_CREATE(g);
51 int rc;
52
5c3f75ec 53 G_EXP(g, d, x, g->r);
54 rc = (G_IDENTP(g, d) && !G_IDENTP(g, x));
34e4f738 55 G_DESTROY(g, d);
56 if (!rc) return (-1);
57 return (0);
58}
59
60/* --- @group_samep@ --- *
61 *
62 * Arguments: @group *g, *h@ = two abstract groups
63 *
64 * Returns: Nonzero if the groups are in fact identical (not just
65 * isomorphic).
66 *
67 * Use: Checks to see whether two groups are actually the same. This
68 * function does the full check: the group operatrion @samep@
69 * just does the group-specific details.
70 */
71
72int group_samep(group *g, group *h)
73{
74 return (g->ops == h->ops &&
75 MP_EQ(g->r, h->r) && MP_EQ(g->h, h->h) &&
76 G_EQ(g, g->i, h->i) && G_EQ(g, g->g, h->g) &&
77 G_SAMEP(g, h));
78}
79
80/*----- Standard implementations ------------------------------------------*/
81
82/* --- @group_stdidentp@ --- *
83 *
84 * Arguments: @group *g@ = abstract group
85 * @ge *x@ = group element
86 *
87 * Returns: Nonzero if %$x$% is the group identity.
88 */
89
90int group_stdidentp(group *g, ge *x) { return (G_EQ(g, x, g->i)); }
91
92/* --- @group_stdsqr@ --- *
93 *
94 * Arguments: @group *g@ = abstract group
95 * @ge *d@ = destination pointer
96 * @ge *x@ = group element
97 *
98 * Returns: ---
99 *
100 * Use: Computes %$d = x^2$% as %$d = x x$%.
101 */
102
103void group_stdsqr(group *g, ge *d, ge *x) { G_MUL(g, d, x, x); }
104
105/* --- @group_stddiv@ --- *
106 *
107 * Arguments: @group *g@ = abstract group
108 * @ge *d@ = destination pointer
109 * @ge *x@ = dividend
110 * @ge *y@ = divisor
111 *
112 * Returns: ---
113 *
114 * Use: Computes %$d = x/y$% as %$d = x y^{-1}$%.
115 */
116
117void group_stddiv(group *g, ge *d, ge *x, ge *y)
118{
119 G_INV(g, d, y);
120 G_MUL(g, d, x, d);
121}
122
123/* --- @group_stdtoec@ --- *
124 *
125 * Arguments: @group *g@ = abstract group
126 * @ec *d@ = destination point
127 * @ge *x@ = group element
128 *
129 * Returns: @-1@, indicating failure.
130 *
131 * Use: Fails to convert a group element to an elliptic curve point.
132 */
133
134int group_stdtoec(group *g, ec *d, ge *x) { return (-1); }
135
136/* --- @group_stdfromec@ --- *
137 *
138 * Arguments: @group *g@ = abstract group
139 * @ge *d@ = destination pointer
5c3f75ec 140 * @const ec *p@ = elliptic curve point
34e4f738 141 *
142 * Returns: Zero for success, @-1@ on failure.
143 *
144 * Use: Converts %$p$% to a group element by converting its %$x$%-
145 * coordinate.
146 */
147
5c3f75ec 148int group_stdfromec(group *g, ge *d, const ec *p)
34e4f738 149 { if (EC_ATINF(p)) return (-1); return (G_FROMINT(g, d, p->x)); }
150
151/* --- @group_stdcheck@ --- *
152 *
153 * Arguments: @group *g@ = abstract group
154 * @grand *gr@ = random number source.
155 *
156 * Returns: Null on success, or a pointer to an error message.
157 */
158
159const char *group_stdcheck(group *g, grand *gr)
160{
161 ge *t;
162 int rc;
163
164 if (!pgen_primep(g->r, gr)) return ("group order not prime");
165 t = G_CREATE(g); G_EXP(g, t, g->g, g->r);
166 rc = G_IDENTP(g, t); G_DESTROY(g, t);
167 if (!rc) return ("generator not in the group");
168 return (0);
169}
170
171/*----- That's all, folks -------------------------------------------------*/