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