General robustification.
[u/mdw/catacomb] / g-prime.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
02d7884d 3 * $Id: g-prime.c,v 1.2 2004/04/03 03:32:05 mdw Exp $
34e4f738 4 *
5 * Abstraction for prime groups
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/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: g-prime.c,v $
02d7884d 33 * Revision 1.2 2004/04/03 03:32:05 mdw
34 * General robustification.
35 *
34e4f738 36 * Revision 1.1 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
42 *
43 */
44
45/*----- Header files ------------------------------------------------------*/
46
47#include <mLib/sub.h>
48
49#include "mpmont.h"
50#include "pgen.h"
51
52#define ge mp *
53#include "group.h"
54
55/*----- Data structures ---------------------------------------------------*/
56
57typedef struct gctx {
58 group g;
59 mp *gen;
60 mpmont mm;
61} gctx;
62
63/*----- Main code ---------------------------------------------------------*/
64
65/* --- Group operations --- */
66
67static void gdestroygroup(group *gg) {
68 gctx *g = (gctx *)gg;
69 mp_drop(g->gen); mp_drop(g->g.r); mp_drop(g->g.h);
70 mpmont_destroy(&g->mm);
71 DESTROY(g);
72}
73
74static mp **gcreate(group *gg)
75 { mp **x = CREATE(mp *); *x = MP_COPY(*gg->i); return (x); }
76
77static void gcopy(group *gg, mp **d, mp **x)
78 { mp *t = MP_COPY(*x); MP_DROP(*d); *d = t; }
79
80static void gburn(group *gg, mp **x) { (*x)->f |= MP_BURN; }
81
82static void gdestroy(group *gg, mp **x) { MP_DROP(*x); DESTROY(x); }
83
02d7884d 84static int gsamep(group *gg, group *hh) {
85 gctx *g = (gctx *)gg, *h = (gctx *)hh;
86 return (MP_EQ(g->mm.m, h->mm.m));
87}
34e4f738 88
89static int geq(group *gg, mp **x, mp **y) { return (MP_EQ(*x, *y)); }
90
91static const char *gcheck(group *gg, grand *gr) {
92 gctx *g = (gctx *)gg; int rc; mp *t;
93 if (!pgen_primep(g->mm.m, gr)) return ("p is not prime");
94 t = mp_mul(MP_NEW, g->g.r, g->g.h); t = mp_add(t, t, MP_ONE);
95 rc = MP_EQ(t, g->mm.m); MP_DROP(t); if (!rc) return ("not a subgroup");
96 return (group_stdcheck(gg, gr));
97}
98
99static void gmul(group *gg, mp **d, mp **x, mp **y)
100 { gctx *g = (gctx *)gg; *d = mpmont_mul(&g->mm, *d, *x, *y); }
101
102static void gsqr(group *gg, mp **d, mp **x) {
103 gctx *g = (gctx *)gg; mp *r = mp_sqr(*d, *x);
104 *d = mpmont_reduce(&g->mm, r, r);
105}
106
107static void ginv(group *gg, mp **d, mp **x) {
108 gctx *g = (gctx *)gg; mp *r = mpmont_reduce(&g->mm, *d, *x);
109 mp_gcd(0, 0, &r, g->mm.m, r); *d = mpmont_mul(&g->mm, r, r, g->mm.r2);
110}
111
112static void gexp(group *gg, mp **d, mp **x, mp *n)
113 { gctx *g = (gctx *)gg; *d = mpmont_expr(&g->mm, *d, *x, n); }
114
115static void gmexp(group *gg, mp **d, const group_expfactor *f, size_t n) {
116 gctx *g = (gctx *)gg; size_t i;
117 mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor));
118 for (i = 0; i < n; i++) { ff[i].base = *f[i].base; ff[i].exp = f[i].exp; }
119 *d = mpmont_mexpr(&g->mm, *d, ff, n); xfree(ff);
120}
121
122static int gread(group *gg, mp **d, const mptext_ops *ops, void *p) {
123 gctx *g = (gctx *)gg; mp *t;
124 if ((t = mp_read(MP_NEW, 0, ops, p)) == 0) return (-1);
125 mp_drop(*d); *d = mpmont_mul(&g->mm, t, t, g->mm.r2); return (0);
126}
127
128static int gwrite(group *gg, mp **x, const mptext_ops *ops, void *p) {
129 gctx *g = (gctx *)gg; mp *t = mpmont_reduce(&g->mm, MP_NEW, *x);
130 int rc = mp_write(t, 10, ops, p); MP_DROP(t); return (rc);
131}
132
133static mp *gtoint(group *gg, mp *d, mp **x)
134 { gctx *g = (gctx *)gg; return (mpmont_reduce(&g->mm, d, *x)); }
135
136static int gfromint(group *gg, mp **d, mp *x) {
137 gctx *g = (gctx *)gg; mp_div(0, &x, x, g->mm.m); mp_drop(*d);
138 *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return (0);
139}
140
141static int gtobuf(group *gg, buf *b, mp **x) {
142 gctx *g = (gctx *)gg; mp *t = mpmont_reduce(&g->mm, MP_NEW, *x);
143 int rc = buf_putmp(b, t); MP_DROP(t); return (rc);
144}
145
146static int gfrombuf(group *gg, buf *b, mp **d) {
147 gctx * g = (gctx *)gg; mp *x; if ((x = buf_getmp(b)) == 0) return (-1);
02d7884d 148 mp_div(0, &x, x, g->mm.m); mp_drop(*d);
34e4f738 149 *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0);
150}
151
152/* --- @group_prime@ --- *
153 *
154 * Arguments: @const gprime_param *gp@ = group parameters
155 *
02d7884d 156 * Returns: A pointer to the group, or null.
34e4f738 157 *
158 * Use: Constructs an abstract group interface for a subgroup of a
159 * prime field. Group elements are @mp *@ pointers.
160 */
161
162static const group_ops gops = {
163 GTY_PRIME,
164 gdestroygroup, gcreate, gcopy, gburn, gdestroy,
165 gsamep, geq, group_stdidentp,
166 gcheck,
167 gmul, gsqr, ginv, group_stddiv, gexp, gmexp,
168 gread, gwrite,
169 gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf
170};
171
172group *group_prime(const gprime_param *gp)
173{
02d7884d 174 gctx *g;
34e4f738 175
02d7884d 176 if (!MP_ISPOS(gp->p) || !MP_ISODD(gp->p))
177 return (0);
178 g = CREATE(gctx);
34e4f738 179 g->g.ops = &gops;
180 g->g.nbits = mp_bits(gp->p);
181 g->g.noctets = (g->g.nbits + 7) >> 3;
182 mpmont_create(&g->mm, gp->p);
183 g->g.i = &g->mm.r;
184 g->gen = mpmont_mul(&g->mm, MP_NEW, gp->g, g->mm.r2);
185 g->g.g = &g->gen;
186 g->g.r = MP_COPY(gp->q);
187 g->g.h = MP_NEW; mp_div(&g->g.h, 0, gp->p, gp->q);
188 return (&g->g);
189}
190
191/*----- That's all, folks -------------------------------------------------*/