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