| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Standard group operations |
| 4 | * |
| 5 | * (c) 2004 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include "group.h" |
| 31 | #include "pgen.h" |
| 32 | |
| 33 | /*----- Handy functions ---------------------------------------------------*/ |
| 34 | |
| 35 | /* --- @group_check@ --- * |
| 36 | * |
| 37 | * Arguments: @group *g@ = an abstract group |
| 38 | * @ge *x@ = a group element |
| 39 | * |
| 40 | * Returns: Zero on success, nonzero for failure. |
| 41 | * |
| 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$%. |
| 44 | */ |
| 45 | |
| 46 | int group_check(group *g, ge *x) |
| 47 | { |
| 48 | ge *d = G_CREATE(g); |
| 49 | int rc; |
| 50 | |
| 51 | G_EXP(g, d, x, g->r); |
| 52 | rc = (G_IDENTP(g, d) && !G_IDENTP(g, x)); |
| 53 | G_DESTROY(g, d); |
| 54 | if (!rc) return (-1); |
| 55 | return (0); |
| 56 | } |
| 57 | |
| 58 | /* --- @group_samep@ --- * |
| 59 | * |
| 60 | * Arguments: @group *g, *h@ = two abstract groups |
| 61 | * |
| 62 | * Returns: Nonzero if the groups are in fact identical (not just |
| 63 | * isomorphic). |
| 64 | * |
| 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. |
| 68 | */ |
| 69 | |
| 70 | int group_samep(group *g, group *h) |
| 71 | { |
| 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) && |
| 75 | G_SAMEP(g, h))); |
| 76 | } |
| 77 | |
| 78 | /*----- Standard implementations ------------------------------------------*/ |
| 79 | |
| 80 | /* --- @group_stdidentp@ --- * |
| 81 | * |
| 82 | * Arguments: @group *g@ = abstract group |
| 83 | * @ge *x@ = group element |
| 84 | * |
| 85 | * Returns: Nonzero if %$x$% is the group identity. |
| 86 | */ |
| 87 | |
| 88 | int group_stdidentp(group *g, ge *x) { return (G_EQ(g, x, g->i)); } |
| 89 | |
| 90 | /* --- @group_stdsqr@ --- * |
| 91 | * |
| 92 | * Arguments: @group *g@ = abstract group |
| 93 | * @ge *d@ = destination pointer |
| 94 | * @ge *x@ = group element |
| 95 | * |
| 96 | * Returns: --- |
| 97 | * |
| 98 | * Use: Computes %$d = x^2$% as %$d = x x$%. |
| 99 | */ |
| 100 | |
| 101 | void group_stdsqr(group *g, ge *d, ge *x) { G_MUL(g, d, x, x); } |
| 102 | |
| 103 | /* --- @group_stddiv@ --- * |
| 104 | * |
| 105 | * Arguments: @group *g@ = abstract group |
| 106 | * @ge *d@ = destination pointer |
| 107 | * @ge *x@ = dividend |
| 108 | * @ge *y@ = divisor |
| 109 | * |
| 110 | * Returns: --- |
| 111 | * |
| 112 | * Use: Computes %$d = x/y$% as %$d = x y^{-1}$%. |
| 113 | */ |
| 114 | |
| 115 | void group_stddiv(group *g, ge *d, ge *x, ge *y) |
| 116 | { G_INV(g, d, y); G_MUL(g, d, x, d); } |
| 117 | |
| 118 | /* --- @group_stdtoec@ --- * |
| 119 | * |
| 120 | * Arguments: @group *g@ = abstract group |
| 121 | * @ec *d@ = destination point |
| 122 | * @ge *x@ = group element |
| 123 | * |
| 124 | * Returns: @-1@, indicating failure. |
| 125 | * |
| 126 | * Use: Fails to convert a group element to an elliptic curve point. |
| 127 | */ |
| 128 | |
| 129 | int group_stdtoec(group *g, ec *d, ge *x) { return (-1); } |
| 130 | |
| 131 | /* --- @group_stdfromec@ --- * |
| 132 | * |
| 133 | * Arguments: @group *g@ = abstract group |
| 134 | * @ge *d@ = destination pointer |
| 135 | * @const ec *p@ = elliptic curve point |
| 136 | * |
| 137 | * Returns: Zero for success, @-1@ on failure. |
| 138 | * |
| 139 | * Use: Converts %$p$% to a group element by converting its %$x$%- |
| 140 | * coordinate. |
| 141 | */ |
| 142 | |
| 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)); } |
| 145 | |
| 146 | /* --- @group_stdcheck@ --- * |
| 147 | * |
| 148 | * Arguments: @group *g@ = abstract group |
| 149 | * @grand *gr@ = random number source. |
| 150 | * |
| 151 | * Returns: Null on success, or a pointer to an error message. |
| 152 | */ |
| 153 | |
| 154 | const char *group_stdcheck(group *g, grand *gr) |
| 155 | { |
| 156 | ge *t; |
| 157 | int rc; |
| 158 | |
| 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"); |
| 163 | return (0); |
| 164 | } |
| 165 | |
| 166 | /*----- That's all, folks -------------------------------------------------*/ |