3 * Standard group operations
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 /*----- Handy functions ---------------------------------------------------*/
35 /* --- @group_check@ --- *
37 * Arguments: @group *g@ = an abstract group
38 * @ge *x@ = a group element
40 * Returns: Zero on success, nonzero for failure.
42 * Use: Checks that @x@ is a valid group element. This may take a
43 * while, since it checks that %$x \ne 1$% and %$x^r = 1$%.
46 int group_check(group *g, ge *x)
52 rc = (G_IDENTP(g, d) && !G_IDENTP(g, x));
58 /* --- @group_samep@ --- *
60 * Arguments: @group *g, *h@ = two abstract groups
62 * Returns: Nonzero if the groups are in fact identical (not just
65 * Use: Checks to see whether two groups are actually the same. This
66 * function does the full check: the group operatrion @samep@
67 * just does the group-specific details.
70 int group_samep(group *g, group *h)
72 return (g == h || (g->ops == h->ops &&
73 MP_EQ(g->r, h->r) && MP_EQ(g->h, h->h) &&
74 G_EQ(g, g->i, h->i) && G_EQ(g, g->g, h->g) &&
78 /*----- Standard implementations ------------------------------------------*/
80 /* --- @group_stdidentp@ --- *
82 * Arguments: @group *g@ = abstract group
83 * @ge *x@ = group element
85 * Returns: Nonzero if %$x$% is the group identity.
88 int group_stdidentp(group *g, ge *x) { return (G_EQ(g, x, g->i)); }
90 /* --- @group_stdsqr@ --- *
92 * Arguments: @group *g@ = abstract group
93 * @ge *d@ = destination pointer
94 * @ge *x@ = group element
98 * Use: Computes %$d = x^2$% as %$d = x x$%.
101 void group_stdsqr(group *g, ge *d, ge *x) { G_MUL(g, d, x, x); }
103 /* --- @group_stddiv@ --- *
105 * Arguments: @group *g@ = abstract group
106 * @ge *d@ = destination pointer
112 * Use: Computes %$d = x/y$% as %$d = x y^{-1}$%.
115 void group_stddiv(group *g, ge *d, ge *x, ge *y)
116 { G_INV(g, d, y); G_MUL(g, d, x, d); }
118 /* --- @group_stdtoec@ --- *
120 * Arguments: @group *g@ = abstract group
121 * @ec *d@ = destination point
122 * @ge *x@ = group element
124 * Returns: @-1@, indicating failure.
126 * Use: Fails to convert a group element to an elliptic curve point.
129 int group_stdtoec(group *g, ec *d, ge *x) { return (-1); }
131 /* --- @group_stdfromec@ --- *
133 * Arguments: @group *g@ = abstract group
134 * @ge *d@ = destination pointer
135 * @const ec *p@ = elliptic curve point
137 * Returns: Zero for success, @-1@ on failure.
139 * Use: Converts %$p$% to a group element by converting its %$x$%-
143 int group_stdfromec(group *g, ge *d, const ec *p)
144 { if (EC_ATINF(p)) return (-1); return (G_FROMINT(g, d, p->x)); }
146 /* --- @group_stdcheck@ --- *
148 * Arguments: @group *g@ = abstract group
149 * @grand *gr@ = random number source.
151 * Returns: Null on success, or a pointer to an error message.
154 const char *group_stdcheck(group *g, grand *gr)
159 if (!pgen_primep(g->r, gr)) return ("group order not prime");
160 t = G_CREATE(g); G_EXP(g, t, g->g, g->r);
161 rc = G_IDENTP(g, t); G_DESTROY(g, t);
162 if (!rc) return ("generator not in the group");
166 /*----- That's all, folks -------------------------------------------------*/