.gitignore: Ignore `auto-version' script.
[u/mdw/catacomb] / g-prime.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
34e4f738 4 *
5 * Abstraction for prime groups
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
34e4f738 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.
45c0fd36 18 *
34e4f738 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.
45c0fd36 23 *
34e4f738 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 <mLib/sub.h>
33
34#include "mpmont.h"
35#include "pgen.h"
36
37#define ge mp *
38#include "group.h"
f94b972d 39#include "group-guts.h"
34e4f738 40
41/*----- Main code ---------------------------------------------------------*/
42
43/* --- Group operations --- */
44
45static void gdestroygroup(group *gg) {
f94b972d 46 gctx_prime *g = (gctx_prime *)gg;
34e4f738 47 mp_drop(g->gen); mp_drop(g->g.r); mp_drop(g->g.h);
48 mpmont_destroy(&g->mm);
49 DESTROY(g);
50}
51
52static mp **gcreate(group *gg)
53 { mp **x = CREATE(mp *); *x = MP_COPY(*gg->i); return (x); }
54
55static void gcopy(group *gg, mp **d, mp **x)
56 { mp *t = MP_COPY(*x); MP_DROP(*d); *d = t; }
57
58static void gburn(group *gg, mp **x) { (*x)->f |= MP_BURN; }
59
60static void gdestroy(group *gg, mp **x) { MP_DROP(*x); DESTROY(x); }
61
02d7884d 62static int gsamep(group *gg, group *hh) {
f94b972d 63 gctx_prime *g = (gctx_prime *)gg, *h = (gctx_prime *)hh;
02d7884d 64 return (MP_EQ(g->mm.m, h->mm.m));
65}
34e4f738 66
67static int geq(group *gg, mp **x, mp **y) { return (MP_EQ(*x, *y)); }
68
69static const char *gcheck(group *gg, grand *gr) {
f94b972d 70 gctx_prime *g = (gctx_prime *)gg; int rc; mp *t;
34e4f738 71 if (!pgen_primep(g->mm.m, gr)) return ("p is not prime");
72 t = mp_mul(MP_NEW, g->g.r, g->g.h); t = mp_add(t, t, MP_ONE);
73 rc = MP_EQ(t, g->mm.m); MP_DROP(t); if (!rc) return ("not a subgroup");
74 return (group_stdcheck(gg, gr));
75}
76
77static void gmul(group *gg, mp **d, mp **x, mp **y)
f94b972d 78 { gctx_prime *g = (gctx_prime *)gg; *d = mpmont_mul(&g->mm, *d, *x, *y); }
34e4f738 79
80static void gsqr(group *gg, mp **d, mp **x) {
f94b972d 81 gctx_prime *g = (gctx_prime *)gg; mp *r = mp_sqr(*d, *x);
34e4f738 82 *d = mpmont_reduce(&g->mm, r, r);
83}
84
85static void ginv(group *gg, mp **d, mp **x) {
f94b972d 86 gctx_prime *g = (gctx_prime *)gg; mp *r = mpmont_reduce(&g->mm, *d, *x);
b817bfc6 87 r = mp_modinv(r, r, g->mm.m); *d = mpmont_mul(&g->mm, r, r, g->mm.r2);
34e4f738 88}
89
90static void gexp(group *gg, mp **d, mp **x, mp *n)
f94b972d 91 { gctx_prime *g = (gctx_prime *)gg; *d = mpmont_expr(&g->mm, *d, *x, n); }
34e4f738 92
93static void gmexp(group *gg, mp **d, const group_expfactor *f, size_t n) {
f94b972d 94 gctx_prime *g = (gctx_prime *)gg; size_t i;
34e4f738 95 mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor));
96 for (i = 0; i < n; i++) { ff[i].base = *f[i].base; ff[i].exp = f[i].exp; }
97 *d = mpmont_mexpr(&g->mm, *d, ff, n); xfree(ff);
98}
99
100static int gread(group *gg, mp **d, const mptext_ops *ops, void *p) {
f94b972d 101 gctx_prime *g = (gctx_prime *)gg; mp *t;
34e4f738 102 if ((t = mp_read(MP_NEW, 0, ops, p)) == 0) return (-1);
103 mp_drop(*d); *d = mpmont_mul(&g->mm, t, t, g->mm.r2); return (0);
104}
105
106static int gwrite(group *gg, mp **x, const mptext_ops *ops, void *p) {
f94b972d 107 gctx_prime *g = (gctx_prime *)gg;
108 mp *t = mpmont_reduce(&g->mm, MP_NEW, *x);
34e4f738 109 int rc = mp_write(t, 10, ops, p); MP_DROP(t); return (rc);
110}
111
f94b972d 112static mp *gtoint(group *gg, mp *d, mp **x) {
113 gctx_prime *g = (gctx_prime *)gg;
114 return (mpmont_reduce(&g->mm, d, *x));
115}
34e4f738 116
117static int gfromint(group *gg, mp **d, mp *x) {
f94b972d 118 gctx_prime *g = (gctx_prime *)gg; mp_div(0, d, x, g->mm.m);
b817bfc6 119 *d = mpmont_mul(&g->mm, *d, *d, g->mm.r2); return (0);
34e4f738 120}
121
122static int gtobuf(group *gg, buf *b, mp **x) {
f94b972d 123 gctx_prime *g = (gctx_prime *)gg;
124 mp *t = mpmont_reduce(&g->mm, MP_NEW, *x);
34e4f738 125 int rc = buf_putmp(b, t); MP_DROP(t); return (rc);
126}
127
128static int gfrombuf(group *gg, buf *b, mp **d) {
f94b972d 129 gctx_prime * g = (gctx_prime *)gg; mp *x;
130 if ((x = buf_getmp(b)) == 0) return (-1);
02d7884d 131 mp_div(0, &x, x, g->mm.m); mp_drop(*d);
34e4f738 132 *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0);
133}
134
0f3faccd 135static int gtoraw(group *gg, buf *b, mp **x) {
f94b972d 136 gctx_prime *g = (gctx_prime *)gg; octet *q;
137 mp *t = mpmont_reduce(&g->mm, MP_NEW, *x);
0f3faccd 138 if ((q = buf_get(b, g->g.noctets)) == 0) { MP_DROP(t); return (-1); }
139 mp_storeb(t, q, g->g.noctets); MP_DROP(t); return (0);
140}
141
142static int gfromraw(group *gg, buf *b, mp **d) {
f94b972d 143 gctx_prime * g = (gctx_prime *)gg; mp *x; octet *q;
0f3faccd 144 if ((q = buf_get(b, g->g.noctets)) == 0) return (-1);
145 x = mp_loadb(MP_NEW, q, g->g.noctets);
146 mp_div(0, &x, x, g->mm.m); mp_drop(*d);
147 *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0);
148}
149
34e4f738 150/* --- @group_prime@ --- *
151 *
152 * Arguments: @const gprime_param *gp@ = group parameters
153 *
02d7884d 154 * Returns: A pointer to the group, or null.
34e4f738 155 *
156 * Use: Constructs an abstract group interface for a subgroup of a
157 * prime field. Group elements are @mp *@ pointers.
158 */
159
160static const group_ops gops = {
f94b972d 161 GTY_PRIME, "prime",
34e4f738 162 gdestroygroup, gcreate, gcopy, gburn, gdestroy,
163 gsamep, geq, group_stdidentp,
164 gcheck,
165 gmul, gsqr, ginv, group_stddiv, gexp, gmexp,
166 gread, gwrite,
0f3faccd 167 gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf,
168 gtoraw, gfromraw
34e4f738 169};
170
171group *group_prime(const gprime_param *gp)
172{
f94b972d 173 gctx_prime *g;
34e4f738 174
a69a3efd 175 if (!MP_POSP(gp->p) || !MP_ODDP(gp->p))
02d7884d 176 return (0);
f94b972d 177 g = CREATE(gctx_prime);
34e4f738 178 g->g.ops = &gops;
179 g->g.nbits = mp_bits(gp->p);
180 g->g.noctets = (g->g.nbits + 7) >> 3;
181 mpmont_create(&g->mm, gp->p);
182 g->g.i = &g->mm.r;
183 g->gen = mpmont_mul(&g->mm, MP_NEW, gp->g, g->mm.r2);
184 g->g.g = &g->gen;
185 g->g.r = MP_COPY(gp->q);
186 g->g.h = MP_NEW; mp_div(&g->g.h, 0, gp->p, gp->q);
187 return (&g->g);
188}
189
190/*----- That's all, folks -------------------------------------------------*/