math/g-*.c: Group implementations include `group.h' via `group-guts.h'.
[catacomb] / math / g-bin.c
1 /* -*-c-*-
2 *
3 * Abstraction for prime groups
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 <mLib/sub.h>
31
32 #include "mpmont.h"
33 #include "pgen.h"
34
35 #define ge mp *
36 #include "group-guts.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- Group operations --- */
41
42 static void gdestroygroup(group *gg) {
43 gctx_bin *g = (gctx_bin *)gg;
44 mp_drop(g->gen); mp_drop(g->g.r); mp_drop(g->g.h);
45 gfreduce_destroy(&g->r);
46 DESTROY(g);
47 }
48
49 static mp **gcreate(group *gg)
50 { mp **x = CREATE(mp *); *x = MP_COPY(*gg->i); return (x); }
51
52 static void gcopy(group *gg, mp **d, mp **x)
53 { mp *t = MP_COPY(*x); MP_DROP(*d); *d = t; }
54
55 static void gburn(group *gg, mp **x) { (*x)->f |= MP_BURN; }
56
57 static void gdestroy(group *gg, mp **x) { MP_DROP(*x); DESTROY(x); }
58
59 static int gsamep(group *gg, group *hh) {
60 gctx_bin *g = (gctx_bin *)gg, *h = (gctx_bin *)hh;
61 return (MP_EQ(g->r.p, h->r.p));
62 }
63
64 static int geq(group *gg, mp **x, mp **y) { return (MP_EQ(*x, *y)); }
65
66 static const char *gcheck(group *gg, grand *gr) {
67 gctx_bin *g = (gctx_bin *)gg; int rc; mp *t, *tt;
68 if (!gf_irreduciblep(g->r.p)) return ("p is not irreducible");
69 t = mp_mul(MP_NEW, g->g.r, g->g.h); t = mp_add(t, t, MP_ONE);
70 tt = mp_lsl(MP_NEW, MP_ONE, g->g.nbits);
71 rc = MP_EQ(t, tt); MP_DROP(t); MP_DROP(tt);
72 if (!rc) return ("not a subgroup");
73 return (group_stdcheck(gg, gr));
74 }
75
76 static void gmul(group *gg, mp **d, mp **x, mp **y) {
77 gctx_bin *g = (gctx_bin *)gg; mp *r = gf_mul(*d, *x, *y);
78 *d = gfreduce_do(&g->r, r, r);
79 }
80
81 static void gsqr(group *gg, mp **d, mp **x) {
82 gctx_bin *g = (gctx_bin *)gg; mp *r = gf_sqr(*d, *x);
83 *d = gfreduce_do(&g->r, r, r);
84 }
85
86 static void ginv(group *gg, mp **d, mp **x)
87 { gctx_bin *g = (gctx_bin *)gg; *d = gf_modinv(*d, *x, g->r.p); }
88
89 static void gexp(group *gg, mp **d, mp **x, mp *n)
90 { gctx_bin *g = (gctx_bin *)gg; *d = gfreduce_exp(&g->r, *d, *x, n); }
91
92 static int gread(group *gg, mp **d, const mptext_ops *ops, void *p) {
93 mp *t; if ((t = mp_read(MP_NEW, 0, ops, p)) == 0) return (-1);
94 mp_drop(*d); *d = t; return (0);
95 }
96
97 static int gwrite(group *gg, mp **x, const mptext_ops *ops, void *p) {
98 int rc = -1;
99 if (!ops->put("0x", 2, p) && !mp_write(*x, 16, ops, p)) rc = 0;
100 return (rc);
101 }
102
103 static mp *gtoint(group *gg, mp *d, mp **x) { return MP_COPY(*x); }
104
105 static int gfromint(group *gg, mp **d, mp *x) { *d = MP_COPY(x); return 0; }
106
107 static int gtobuf(group *gg, buf *b, mp **x)
108 { int rc = buf_putmp(b, *x); return (rc); }
109
110 static int gfrombuf(group *gg, buf *b, mp **d) {
111 gctx_bin *g = (gctx_bin *)gg; mp *x;
112 if ((x = buf_getmp(b)) == 0) return (-1);
113 MP_DROP(*d); *d = gfreduce_do(&g->r, x, x);
114 return (0);
115 }
116
117 static int gtoraw(group *gg, buf *b, mp **x) {
118 gctx_bin * g = (gctx_bin *)gg; octet *q;
119 if ((q = buf_get(b, g->g.noctets)) == 0) return (-1);
120 mp_storeb(*x, q, g->g.noctets); return (0);
121 }
122
123 static int gfromraw(group *gg, buf *b, mp **d) {
124 gctx_bin * g = (gctx_bin *)gg; mp *x; octet *q;
125 if ((q = buf_get(b, g->g.noctets)) == 0) return (-1);
126 x = mp_loadb(MP_NEW, q, g->g.noctets);
127 MP_DROP(*d); *d = gfreduce_do(&g->r, x, x);
128 return (0);
129 }
130
131 /* --- @group_binary@ --- *
132 *
133 * Arguments: @const gbin_param *gb@ = group parameters
134 *
135 * Returns: A pointer to the group, or null.
136 *
137 * Use: Constructs an abstract group interface for a subgroup of a
138 * prime field. Group elements are @mp *@ pointers.
139 */
140
141 static const group_ops gops = {
142 GTY_BINARY, "bin",
143 gdestroygroup, gcreate, gcopy, gburn, gdestroy,
144 gsamep, geq, group_stdidentp,
145 gcheck,
146 gmul, gsqr, ginv, group_stddiv, gexp, group_stdmexp,
147 gread, gwrite,
148 gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf,
149 gtoraw, gfromraw
150 };
151
152 group *group_binary(const gbin_param *gb)
153 {
154 gctx_bin *g;
155 mp *t;
156
157 if (!MP_POSP(gb->p))
158 return (0);
159 g = CREATE(gctx_bin);
160 g->g.ops = &gops;
161 g->g.nbits = mp_bits(gb->p) - 1;
162 g->g.noctets = (g->g.nbits + 7) >> 3;
163 gfreduce_create(&g->r, gb->p);
164 g->one = MP_ONE;
165 g->g.i = &g->one;
166 g->gen = MP_COPY(gb->g);
167 g->g.g = &g->gen;
168 g->g.r = MP_COPY(gb->q);
169 t = mp_lsl(MP_NEW, MP_ONE, g->g.nbits);
170 t = mp_sub(t, t, MP_ONE);
171 g->g.h = MP_NEW; mp_div(&g->g.h, 0, t, gb->q);
172 MP_DROP(t);
173 return (&g->g);
174 }
175
176 /*----- That's all, folks -------------------------------------------------*/