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